MDL-20204 new url_select widget

This commit is contained in:
Petr Skoda 2010-02-10 10:22:25 +00:00
parent 0b2cb1327a
commit 4d10e57985
4 changed files with 185 additions and 11 deletions

View File

@ -26,7 +26,7 @@
require('../config.php');
$jump = optional_param('jump', '', PARAM_RAW);
$jump = required_param('jump', PARAM_RAW);
$PAGE->set_url('/course/jumpto.php');
@ -34,13 +34,8 @@ if (!confirm_sesskey()) {
print_error('confirmsesskeybad');
}
if (strpos($jump, $CFG->wwwroot) === 0) { // Anything on this site
redirect(new moodle_url(urldecode($jump)));
} else if (preg_match('/^[a-z]+\.php\?/', $jump)) {
redirect(new moodle_url(urldecode($jump)));
if (strpos($jump, '/') === 0) {
redirect(new moodle_url($jump));
} else {
print_error('error');
}
if(isset($_SERVER['HTTP_REFERER'])) {
redirect(new moodle_url($_SERVER['HTTP_REFERER'])); // Return to sender, just in case
}

View File

@ -395,6 +395,20 @@ M.util.init_single_select = function(Y, formid, selectid, nothing) {
});
};
/**
* Attach handler to url_select
*/
M.util.init_url_select = function(Y, formid, selectid, nothing) {
YUI(M.yui.loader).use('node', function(Y) {
Y.on('change', function() {
if ((nothing == false && Y.Lang.isBoolean(nothing)) || Y.one('#'+selectid).get('value') != nothing) {
window.location = M.cfg.wwwroot+Y.one('#'+selectid).get('value');
}
},
'#'+selectid);
});
};
//=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code ===
function popupchecker(msg) {

View File

@ -399,6 +399,103 @@ class single_select implements renderable {
}
/**
* Simple URL selection widget description.
* @copyright 2009 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.0
*/
class url_select implements renderable {
/**
* @var array $urls associative array value=>label ex.:
* array(1=>'One, 2=>Two)
* it is also possible to specify optgroup as complex label array ex.:
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
*/
var $urls;
/**
* Selected option
* @var string
*/
var $selected;
/**
* Nothing selected
* @var array
*/
var $nothing;
/**
* Extra select field attributes
* @var array
*/
var $attributes = array();
/**
* Button label
* @var string
*/
var $label = '';
/**
* Wrapping div class
* @var string
* */
var $class = 'urlselect';
/**
* True if button disabled, false if normal
* @var boolean
*/
var $disabled = false;
/**
* Button tooltip
* @var string
*/
var $tooltip = null;
/**
* Form id
* @var string
*/
var $formid = null;
/**
* List of attached actions
* @var array of component_action
*/
var $helpicon = null;
/**
* Constructor
* @param array $urls list of options
* @param string $selected selected element
* @param array $nothing
* @param string $formid
*/
public function __construct(array $urls, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
$this->urls = $urls;
$this->selected = $selected;
$this->nothing = $nothing;
$this->formid = $formid;
}
/**
* Constructor: sets up the other components in case they are needed
* @param string $page The keyword that defines a help page
* @param string $title A descriptive text for accesibility only
* @param string $component
* @param bool $linktext add extra text to icon
* @return void
*/
public function set_help_icon($helppage, $title, $component = 'moodle') {
$this->helpicon = new help_icon($helppage, $title, $component);
}
/**
* Set's select lable
* @param string $label
* @return void
*/
public function set_label($label) {
$this->label = $label;
}
}
/**
* Data structure describing html link with special action attached.
* @copyright 2010 Petr Skoda

View File

@ -1127,7 +1127,7 @@ class core_renderer extends renderer_base {
if ($select->disabled) {
$select->attributes['disabled'] = 'disabled';
}
if ($select->tooltip) {
$select->attributes['title'] = $select->tooltip;
}
@ -1161,6 +1161,74 @@ class core_renderer extends renderer_base {
return html_writer::tag('div', array('class' => $select->class), $output);
}
/**
* Returns a form with a single button.
* @param array $urls list of urls - array('/course/view.php?id=1'=>'Frontpage', ....)
* @param string $selected selected element
* @param array $nothing
* @param string $formid
* @return string HTML fragment
*/
public function url_select(array $urls, $selected, $nothing=array(''=>'choosedots'), $formid=null) {
$select = new url_select($urls, $selected, $nothing, $formid);
return $this->render($select);
}
/**
* Internal implementation of single_select rendering
* @param single_select $select
* @return string HTML fragment
*/
protected function render_url_select(url_select $select) {
$select = clone($select);
if (empty($select->formid)) {
$select->formid = html_writer::random_id('url_select_f');
}
if (empty($select->attributes['id'])) {
$select->attributes['id'] = html_writer::random_id('url_select');
}
if ($select->disabled) {
$select->attributes['disabled'] = 'disabled';
}
if ($select->tooltip) {
$select->attributes['title'] = $select->tooltip;
}
$output = '';
if ($select->label) {
$output .= html_writer::tag('label', array('for'=>$select->attributes['id']), $select->label);
}
if ($select->helpicon instanceof help_icon) {
$output .= $this->render($select->helpicon);
}
$output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
$output .= html_writer::select($select->urls, 'jump', $select->selected, $select->nothing, $select->attributes);
$go = html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('go')));
$output .= html_writer::tag('noscript', array('style'=>'inline'), $go);
$nothing = empty($select->nothing) ? false : key($select->nothing);
$output .= $this->page->requires->js_init_call('M.util.init_url_select', array($select->formid, $select->attributes['id'], $nothing));
// then div wrapper for xhtml strictness
$output = html_writer::tag('div', array(), $output);
// now the form itself around it
$formattributes = array('method' => 'post',
'action' => new moodle_url('/course/jumpto.php'),
'id' => $select->formid);
$output = html_writer::tag('form', $formattributes, $output);
// and finally one more wrapper with class
return html_writer::tag('div', array('class' => $select->class), $output);
}
/**
* Given a html_form component and an optional rendered submit button,
* outputs a HTML form with correct divs and inputs and a single submit button.