Skip to content

Commit

Permalink
MDL-35569 AJAX Move auto-submitting selects to separate YUI module
Browse files Browse the repository at this point in the history
sam note: Some changes removed from 2.4 version (deprecation warning, lib/upgrade.txt).
  • Loading branch information
Andrew Robert Nicols authored and sammarshallou committed Dec 18, 2012
1 parent 5fa7d72 commit 5eadb30
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 15 deletions.
7 changes: 5 additions & 2 deletions course/category.php
Expand Up @@ -434,8 +434,11 @@
$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>';
}
Expand Down
9 changes: 6 additions & 3 deletions course/search.php
Expand Up @@ -378,8 +378,11 @@
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>";

Expand Down Expand Up @@ -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>";
}
}
}
20 changes: 18 additions & 2 deletions lib/outputrenderers.php
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down
108 changes: 108 additions & 0 deletions 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']
}
);
7 changes: 5 additions & 2 deletions mod/choice/lib.php
Expand Up @@ -501,8 +501,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>';
Expand Down
8 changes: 5 additions & 3 deletions mod/feedback/analysis_course.php
Expand Up @@ -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 '<hr />';
$itemnr = 0;
Expand Down
7 changes: 5 additions & 2 deletions mod/lesson/report.php
Expand Up @@ -315,8 +315,11 @@
$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>';
}
Expand Down
10 changes: 9 additions & 1 deletion theme/mymobile/renderers.php
Expand Up @@ -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']);
}
Expand All @@ -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);
Expand Down

0 comments on commit 5eadb30

Please sign in to comment.