diff --git a/course/category.php b/course/category.php index cb1d0c79fa5f4..793b120485342 100644 --- a/course/category.php +++ b/course/category.php @@ -434,8 +434,11 @@ $movetocategories[$category->id] = get_string('moveselectedcoursesto'); echo ''; echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide')); - echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid')); - $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false)); + echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid', 'class' => 'autosubmit')); + $PAGE->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => 'movetoid', 'nothing' => $category->id)) + ); echo ''; echo ''; } diff --git a/course/search.php b/course/search.php index 8c9f85cd1568b..86b99322bf751 100644 --- a/course/search.php +++ b/course/search.php @@ -378,8 +378,11 @@ echo "\n"; // Select box should only show categories in which user has min capability to move course. echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide')); - echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid')); - $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false)); + echo html_writer::select($usercatlist, 'moveto', '', array(''=>get_string('moveselectedcoursesto')), array('id'=>'movetoid', 'class' => 'autosubmit')); + $PAGE->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => 'movetoid', 'nothing' => false)) + ); echo "\n\n"; echo "\n"; @@ -432,4 +435,4 @@ function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $mod echo "".get_string("showperpage", "", $defaultperpage).""; echo "

"; } -} \ No newline at end of file +} diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 85816b16f3822..775e1f65261d6 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -1336,6 +1336,11 @@ protected function render_single_select(single_select $select) { $select->attributes['title'] = $select->tooltip; } + $select->attributes['class'] = 'autosubmit'; + if ($select->class) { + $select->attributes['class'] .= ' ' . $select->class; + } + if ($select->label) { $output .= html_writer::label($select->label, $select->attributes['id'], false, $select->labelattributes); } @@ -1351,7 +1356,10 @@ protected function render_single_select(single_select $select) { $output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('style'=>'inline')); $nothing = empty($select->nothing) ? false : key($select->nothing); - $this->page->requires->js_init_call('M.util.init_select_autosubmit', array($select->formid, $select->attributes['id'], $nothing)); + $this->page->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing)) + ); // then div wrapper for xhtml strictness $output = html_writer::tag('div', $output); @@ -1417,6 +1425,11 @@ protected function render_url_select(url_select $select) { $output .= html_writer::label($select->label, $select->attributes['id'], false, $select->labelattributes); } + $select->attributes['class'] = 'autosubmit'; + if ($select->class) { + $select->attributes['class'] .= ' ' . $select->class; + } + if ($select->helpicon instanceof help_icon) { $output .= $this->render($select->helpicon); } else if ($select->helpicon instanceof old_help_icon) { @@ -1474,7 +1487,10 @@ protected function render_url_select(url_select $select) { $go = html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('go'))); $output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('style'=>'inline')); $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)); + $this->page->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing)) + ); } else { $output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>$select->showbutton)); } diff --git a/lib/yui/formautosubmit/formautosubmit.js b/lib/yui/formautosubmit/formautosubmit.js new file mode 100644 index 0000000000000..01d666879c801 --- /dev/null +++ b/lib/yui/formautosubmit/formautosubmit.js @@ -0,0 +1,108 @@ +YUI.add('moodle-core-formautosubmit', + function(Y) { + // The CSS selectors we use + var CSS = { + AUTOSUBMIT : 'autosubmit' + }; + + var FORMAUTOSUBMITNAME = 'core-formautosubmit'; + + var FORMAUTOSUBMIT = function() { + FORMAUTOSUBMIT.superclass.constructor.apply(this, arguments); + } + + // We only want to initialize the module fully once + var INITIALIZED = false; + + Y.extend(FORMAUTOSUBMIT, Y.Base, { + + /** + * Initialize the module + */ + initializer : function(config) { + // We only apply the delegation once + if (!INITIALIZED) { + INITIALIZED = true; + var applyto = Y.one('body'); + + // We don't listen for change events by default as using the keyboard triggers these too. + applyto.delegate('key', this.process_changes, 'press:13', 'select.' + CSS.AUTOSUBMIT, this); + applyto.delegate('click', this.process_changes, 'select.' + CSS.AUTOSUBMIT, this); + + if (Y.UA.os == 'macintosh' && Y.UA.webkit) { + // Macintosh webkit browsers like change events, but non-macintosh webkit browsers don't. + applyto.delegate('change', this.process_changes, 'select.' + CSS.AUTOSUBMIT, this); + } + if (Y.UA.ios) { + // IOS doesn't trigger click events because it's touch-based. + applyto.delegate('change', this.process_changes, 'select.' + CSS.AUTOSUBMIT, this); + } + } + + // Assign this select items 'nothing' value and lastindex (current value) + var thisselect = Y.one('select#' + this.get('selectid')); + thisselect.setData('nothing', this.get('nothing')); + thisselect.setData('startindex', thisselect.get('selectedIndex')); + }, + + /** + * Check whether the select element was changed + */ + check_changed : function(e) { + var select = e.target.ancestor('select.' + CSS.AUTOSUBMIT, true); + if (!select) { + return false; + } + + var nothing = select.getData('nothing'); + var startindex = select.getData('startindex'); + var currentindex = select.get('selectedIndex'); + + var previousindex = select.getAttribute('data-previousindex'); + select.setAttribute('data-previousindex', currentindex); + if (!previousindex) { + previousindex = startindex; + } + + // Check whether the field has changed, and is not the 'nothing' value + if ((nothing===false || select.get('value') != nothing) && startindex != select.get('selectedIndex') && currentindex != previousindex) { + return select; + } + return false; + }, + + /** + * Process any changes + */ + process_changes : function(e) { + var select = this.check_changed(e); + if (select) { + var form = select.ancestor('form', true); + form.submit(); + } + } + }, + { + NAME : FORMAUTOSUBMITNAME, + ATTRS : { + selectid : { + 'value' : '' + }, + nothing : { + 'value' : '' + }, + ignorechangeevent : { + 'value' : false + } + } + }); + + M.core = M.core || {}; + M.core.init_formautosubmit = M.core.init_formautosubmit || function(config) { + return new FORMAUTOSUBMIT(config); + }; + }, + '@VERSION@', { + requires : ['base', 'event-key'] + } +); diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 2b37b382c48ff..2e1bdb1c202e3 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -501,8 +501,11 @@ function prepare_choice_show_results($choice, $course, $cm, $allresponses, $forc echo ''.get_string('deselectall').' '; echo '  '; echo html_writer::label(get_string('withselected', 'choice'), 'menuaction'); - echo html_writer::select(array('delete' => get_string('delete')), 'action', '', array(''=>get_string('withselectedusers')), array('id'=>'menuaction')); - $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('attemptsform', 'menuaction', '')); + echo html_writer::select(array('delete' => get_string('delete')), 'action', '', array(''=>get_string('withselectedusers')), array('id'=>'menuaction', 'class' => 'autosubmit')); + $PAGE->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => 'menuaction')) + ); echo ''; diff --git a/mod/feedback/analysis_course.php b/mod/feedback/analysis_course.php index faf95fff973c0..ccbcaefa5d43d 100644 --- a/mod/feedback/analysis_course.php +++ b/mod/feedback/analysis_course.php @@ -187,10 +187,12 @@ echo ' '. html_writer::label(get_string('filter_by_course', 'feedback'), 'coursefilterid'). ': '; echo html_writer::select($courses, 'coursefilter', $coursefilter, - null, array('id'=>'coursefilterid')); + null, array('id'=>'coursefilterid', 'class' => 'autosubmit')); - $PAGE->requires->js_init_call('M.util.init_select_autosubmit', - array('analysis-form', 'coursefilterid', false)); + $PAGE->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => 'coursefilterid', 'nothing' => false)) + ); } echo '
'; $itemnr = 0; diff --git a/mod/lesson/report.php b/mod/lesson/report.php index 37d6cba715104..889b43372e0bd 100644 --- a/mod/lesson/report.php +++ b/mod/lesson/report.php @@ -315,8 +315,11 @@ $checklinks = ''.get_string('selectall').' / '; $checklinks .= ''.get_string('deselectall').''; $checklinks .= html_writer::label('action', 'menuaction', false, array('class' => 'accesshide')); - $checklinks .= html_writer::select(array('delete' => get_string('deleteselected')), 'action', 0, array(''=>'choosedots'), array('id'=>'actionid')); - $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('theform', 'actionid', '')); + $checklinks .= html_writer::select(array('delete' => get_string('deleteselected')), 'action', 0, array(''=>'choosedots'), array('id'=>'actionid', 'class' => 'autosubmit')); + $PAGE->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => 'actionid', 'nothing' => false)) + ); echo $OUTPUT->box($checklinks, 'center'); echo ''; } diff --git a/theme/mymobile/renderers.php b/theme/mymobile/renderers.php index 2b340d9521267..3a4d0c7575de0 100644 --- a/theme/mymobile/renderers.php +++ b/theme/mymobile/renderers.php @@ -749,6 +749,11 @@ protected function render_single_select(single_select $select) { $select->attributes['title'] = $select->tooltip; } + $select->attributes['class'] = 'autosubmit'; + if ($select->class) { + $select->attributes['class'] .= ' ' . $select->class; + } + if ($select->label) { $output .= html_writer::label($select->label, $select->attributes['id']); } @@ -767,7 +772,10 @@ protected function render_single_select(single_select $select) { $output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('style' => 'inline')); $nothing = empty($select->nothing) ? false : key($select->nothing); - $this->page->requires->js_init_call('M.util.init_select_autosubmit', array($select->formid, $select->attributes['id'], $nothing)); + $this->page->requires->yui_module('moodle-core-formautosubmit', + 'M.core.init_formautosubmit', + array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing)) + ); // then div wrapper for xhtml strictness $output = html_writer::tag('div', $output);