MDL-35673 Improve performance of formchangechecker event creation

This commit is contained in:
Andrew Robert Nicols 2012-09-28 10:06:03 +01:00
parent 5d6285c220
commit d6418e531d

View File

@ -11,6 +11,10 @@ YUI.add('moodle-core-formchangechecker',
}
Y.extend(FORMCHANGECHECKER, Y.Base, {
// The delegated listeners we need to detach after the initial value has been stored once
initialvaluelisteners : [],
/**
* Initialize the module
*/
@ -18,14 +22,15 @@ YUI.add('moodle-core-formchangechecker',
var formid = 'form#' + this.get('formid');
// Add change events to the form elements
Y.all(formid + ' input').once('change', M.core_formchangechecker.set_form_changed, this);
Y.all(formid + ' textarea').once('change', M.core_formchangechecker.set_form_changed, this);
Y.all(formid + ' select').once('change', M.core_formchangechecker.set_form_changed, this);
var currentform = Y.one(formid);
currentform.delegate('change', M.core_formchangechecker.set_form_changed, 'input', this);
currentform.delegate('change', M.core_formchangechecker.set_form_changed, 'textarea', this);
currentform.delegate('change', M.core_formchangechecker.set_form_changed, 'select', this);
// Add a focus event to check for changes which are made without triggering a change event
Y.all(formid + ' input').on('focus', this.store_initial_value, this);
Y.all(formid + ' textarea').on('focus', this.store_initial_value, this);
Y.all(formid + ' select').on('focus', this.store_initial_value, this);
this.initialvaluelisteners.push(currentform.delegate('focus', this.store_initial_value, 'input', this));
this.initialvaluelisteners.push(currentform.delegate('focus', this.store_initial_value, 'textarea', this));
this.initialvaluelisteners.push(currentform.delegate('focus', this.store_initial_value, 'select', this));
// We need any submit buttons on the form to set the submitted flag
Y.one(formid).on('submit', M.core_formchangechecker.set_form_submitted, this);
@ -52,9 +57,12 @@ YUI.add('moodle-core-formchangechecker',
// we no longer need to call this function
var formid = 'form#' + this.get('formid');
Y.all(formid + ' input').detach('focus', this.store_initial_value, this);
Y.all(formid + ' textarea').detach('focus', this.store_initial_value, this);
Y.all(formid + ' select').detach('focus', this.store_initial_value, this);
// Detach all listen events to prevent duplicate initial value setting
var thisevent;
while (thisevent = this.initialvaluelisteners.shift()) {
thisevent.detach();
}
return;
}
@ -176,6 +184,6 @@ YUI.add('moodle-core-formchangechecker',
};
},
'@VERSION@', {
requires : ['base']
requires : ['base', 'event-focus']
}
);