MDL-35569 AJAX Move auto-submitting selects to separate YUI module

This commit is contained in:
Andrew Robert Nicols 2012-09-21 15:17:22 +01:00
parent ecfe814e0f
commit 7266bd3e59
10 changed files with 173 additions and 15 deletions

View File

@ -425,8 +425,11 @@ if (!$courses) {
$movetocategories[$category->id] = get_string('moveselectedcoursesto');
echo '<tr><td colspan="3" align="right">';
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 '<input type="hidden" name="id" value="'.$category->id.'" />';
echo '</td></tr>';
}

View File

@ -378,8 +378,11 @@ if ($courses) {
echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strdeselectall\" />\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 "</td>\n</tr>\n";
echo "</table>\n</form>";
@ -432,4 +435,4 @@ function print_navigation_bar($totalcount, $page, $perpage, $encodedsearch, $mod
echo "<a href=\"search.php?search=$encodedsearch".$modulelink."&amp;perpage=".$defaultperpage."\">".get_string("showperpage", "", $defaultperpage)."</a>";
echo "</p></center>";
}
}
}

View File

@ -376,8 +376,15 @@ M.util.init_maximised_embed = function(Y, id) {
/**
* Attach handler to single_select
*
* This code was deprecated in Moodle 2.4 and will be removed in Moodle 2.6
*
* Please see lib/yui/formautosubmit/formautosubmit.js for its replacement
*/
M.util.init_select_autosubmit = function(Y, formid, selectid, nothing) {
if (M.cfg.developerdebug) {
Y.log("You are using a deprecated function call (M.util.init_select_autosubmit). Please look at rewriting your call to use moodle-core-formautosubmit");
}
Y.use('event-key', function() {
var select = Y.one('#'+selectid);
if (select) {
@ -451,6 +458,9 @@ M.util.init_select_autosubmit = function(Y, formid, selectid, nothing) {
* This function has accessability issues and also does not use the formid passed through as a parameter.
*/
M.util.init_url_select = function(Y, formid, selectid, nothing) {
if (M.cfg.developerdebug) {
Y.log("You are using a deprecated function call (M.util.init_url_select). Please look at rewriting your call to use moodle-core-formautosubmit");
}
YUI().use('node', function(Y) {
Y.on('change', function() {
if ((nothing == false && Y.Lang.isBoolean(nothing)) || Y.one('#'+selectid).get('value') != nothing) {

View File

@ -1352,6 +1352,11 @@ class core_renderer extends renderer_base {
$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);
}
@ -1367,7 +1372,10 @@ class core_renderer extends renderer_base {
$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);
@ -1433,6 +1441,11 @@ class core_renderer extends renderer_base {
$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) {
@ -1490,7 +1503,10 @@ class core_renderer extends renderer_base {
$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_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))
);
} else {
$output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>$select->showbutton));
}

View File

@ -20,6 +20,8 @@ information provided here is intended especially for developers.
YUI changes:
* moodle-enrol-notification has been renamed to moodle-core-notification
* YUI2 code must now use 2in3, see http://yuilibrary.com/yui/docs/yui/yui-yui2.html
* M.util.init_select_autosubmit() and M.util.init_url_select() have been deprecated. Code using this should be updated
to use moodle-core-formautosubmit
Unit testing changes:
* output debugging() is not sent to standard output any more,

108
lib/yui/formautosubmit/formautosubmit.js vendored Normal file
View File

@ -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']
}
);

View File

@ -499,8 +499,11 @@ function prepare_choice_show_results($choice, $course, $cm, $allresponses, $forc
echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('deselectall').'</a> ';
echo '&nbsp;&nbsp;';
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 '<noscript id="noscriptmenuaction" style="display:inline">';
echo '<div>';
echo '<input type="submit" value="'.get_string('go').'" /></div></noscript>';

View File

@ -185,10 +185,12 @@ if ($courseitemfilter > 0) {
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 '<hr />';
$itemnr = 0;

View File

@ -317,8 +317,11 @@ if ($action === 'delete') {
$checklinks = '<a href="javascript: checkall();">'.get_string('selectall').'</a> / ';
$checklinks .= '<a href="javascript: checknone();">'.get_string('deselectall').'</a>';
$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 '</form>';
}

View File

@ -749,6 +749,11 @@ class theme_mymobile_core_renderer extends core_renderer {
$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 @@ class theme_mymobile_core_renderer extends core_renderer {
$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);