mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 08:22:07 +02:00
MDL-45780 mod_quiz: Autosave should listen for textarea:change events too
This commit is contained in:
parent
4dbc02f664
commit
83d68fde48
@ -91,7 +91,7 @@ M.mod_quiz.autosave = {
|
||||
*/
|
||||
SELECTORS: {
|
||||
QUIZ_FORM: '#responseform',
|
||||
VALUE_CHANGE_ELEMENTS: 'input, textarea',
|
||||
VALUE_CHANGE_ELEMENTS: 'input, textarea, [contenteditable="true"]',
|
||||
CHANGE_ELEMENTS: 'input, select',
|
||||
HIDDEN_INPUTS: 'input[type=hidden]',
|
||||
CONNECTION_ERROR: '#connection-error',
|
||||
@ -194,7 +194,7 @@ M.mod_quiz.autosave = {
|
||||
init: function(delay) {
|
||||
this.form = Y.one(this.SELECTORS.QUIZ_FORM);
|
||||
if (!this.form) {
|
||||
Y.log('No response form found. Why did you try to set up autosave?');
|
||||
Y.log('No response form found. Why did you try to set up autosave?', 'debug', 'moodle-mod_quiz-autosave');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -256,12 +256,12 @@ M.mod_quiz.autosave = {
|
||||
if (repeatcount > 0) {
|
||||
Y.later(this.TINYMCE_DETECTION_DELAY, this, this.init_tinymce, [repeatcount - 1]);
|
||||
} else {
|
||||
Y.log('Gave up looking for TinyMCE.');
|
||||
Y.log('Gave up looking for TinyMCE.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Y.log('Found TinyMCE.');
|
||||
Y.log('Found TinyMCE.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.editor_change_handler = Y.bind(this.editor_changed, this);
|
||||
tinyMCE.onAddEditor.add(Y.bind(this.init_tinymce_editor, this));
|
||||
},
|
||||
@ -274,7 +274,7 @@ M.mod_quiz.autosave = {
|
||||
* @param {Object} editor The TinyMCE editor object
|
||||
*/
|
||||
init_tinymce_editor: function(e, editor) {
|
||||
Y.log('Found TinyMCE editor ' + editor.id + '.');
|
||||
Y.log('Found TinyMCE editor ' + editor.id + '.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
editor.onChange.add(this.editor_change_handler);
|
||||
editor.onRedo.add(this.editor_change_handler);
|
||||
editor.onUndo.add(this.editor_change_handler);
|
||||
@ -282,16 +282,19 @@ M.mod_quiz.autosave = {
|
||||
},
|
||||
|
||||
value_changed: function(e) {
|
||||
if (e.target.get('name') === 'thispage' || e.target.get('name') === 'scrollpos' ||
|
||||
e.target.get('name').match(/_:flagged$/)) {
|
||||
var name = e.target.getAttribute('name');
|
||||
if (name === 'thispage' || name === 'scrollpos' || (name && name.match(/_:flagged$/))) {
|
||||
return; // Not interesting.
|
||||
}
|
||||
Y.log('Detected a value change in element ' + e.target.get('name') + '.');
|
||||
|
||||
// Fallback to the ID when the name is not present (in the case of content editable).
|
||||
name = name || '#' + e.target.getAttribute('id');
|
||||
Y.log('Detected a value change in element ' + name + '.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.start_save_timer_if_necessary();
|
||||
},
|
||||
|
||||
editor_changed: function(editor) {
|
||||
Y.log('Detected a value change in editor ' + editor.id + '.');
|
||||
Y.log('Detected a value change in editor ' + editor.id + '.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.start_save_timer_if_necessary();
|
||||
},
|
||||
|
||||
@ -323,12 +326,12 @@ M.mod_quiz.autosave = {
|
||||
this.dirty = false;
|
||||
|
||||
if (this.is_time_nearly_over()) {
|
||||
Y.log('No more saving, time is nearly over.');
|
||||
Y.log('No more saving, time is nearly over.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.stop_autosaving();
|
||||
return;
|
||||
}
|
||||
|
||||
Y.log('Doing a save.');
|
||||
Y.log('Doing a save.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
if (typeof tinyMCE !== 'undefined') {
|
||||
tinyMCE.triggerSave();
|
||||
}
|
||||
@ -351,11 +354,11 @@ M.mod_quiz.autosave = {
|
||||
return;
|
||||
}
|
||||
|
||||
Y.log('Save completed.');
|
||||
Y.log('Save completed.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.save_transaction = null;
|
||||
|
||||
if (this.dirty) {
|
||||
Y.log('Dirty after save.');
|
||||
Y.log('Dirty after save.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.start_save_timer();
|
||||
}
|
||||
|
||||
@ -370,7 +373,7 @@ M.mod_quiz.autosave = {
|
||||
},
|
||||
|
||||
save_failed: function() {
|
||||
Y.log('Save failed.');
|
||||
Y.log('Save failed.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.save_transaction = null;
|
||||
|
||||
// We want to retry soon.
|
||||
|
@ -1 +1 @@
|
||||
YUI.add("moodle-mod_quiz-autosave",function(e,t){M.mod_quiz=M.mod_quiz||{},M.mod_quiz.autosave={TINYMCE_DETECTION_DELAY:500,TINYMCE_DETECTION_REPEATS:20,WATCH_HIDDEN_DELAY:1e3,FAILURES_BEFORE_NOTIFY:1,FIRST_SUCCESSFUL_SAVE:-1,SELECTORS:{QUIZ_FORM:"#responseform",VALUE_CHANGE_ELEMENTS:"input, textarea",CHANGE_ELEMENTS:"input, select",HIDDEN_INPUTS:"input[type=hidden]",CONNECTION_ERROR:"#connection-error",CONNECTION_OK:"#connection-ok"},AUTOSAVE_HANDLER:M.cfg.wwwroot+"/mod/quiz/autosave.ajax.php",delay:12e4,form:null,dirty:!1,delay_timer:null,save_transaction:null,savefailures:0,editor_change_handler:null,hidden_field_values:{},init:function(t){this.form=e.one(this.SELECTORS.QUIZ_FORM);if(!this.form)return;this.delay=t*1e3,this.form.delegate("valuechange",this.value_changed,this.SELECTORS.VALUE_CHANGE_ELEMENTS,this),this.form.delegate("change",this.value_changed,this.SELECTORS.CHANGE_ELEMENTS,this),this.form.on("submit",this.stop_autosaving,this),this.init_tinymce(this.TINYMCE_DETECTION_REPEATS),this.save_hidden_field_values(),this.watch_hidden_fields()},save_hidden_field_values:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name");if(!t)return;this.hidden_field_values[t]=e.get("value")},this)},watch_hidden_fields:function(){this.detect_hidden_field_changes(),e.later(this.WATCH_HIDDEN_DELAY,this,this.watch_hidden_fields)},detect_hidden_field_changes:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name"),n=e.get("value");if(!t)return;if(!(t in this.hidden_field_values)||n!==this.hidden_field_values[t])this.hidden_field_values[t]=n,this.value_changed({target:e})},this)},init_tinymce:function(t){if(typeof tinyMCE=="undefined"){t>0&&e.later(this.TINYMCE_DETECTION_DELAY,this,this.init_tinymce,[t-1]);return}this.editor_change_handler=e.bind(this.editor_changed,this),tinyMCE.onAddEditor.add(e.bind(this.init_tinymce_editor,this))},init_tinymce_editor:function(e,t){t.onChange.add(this.editor_change_handler),t.onRedo.add(this.editor_change_handler),t.onUndo.add(this.editor_change_handler),t.onKeyDown.add(this.editor_change_handler)},value_changed:function(e){if(e.target.get("name")==="thispage"||e.target.get("name")==="scrollpos"||e.target.get("name").match(/_:flagged$/))return;this.start_save_timer_if_necessary()},editor_changed:function(e){this.start_save_timer_if_necessary()},start_save_timer_if_necessary:function(){this.dirty=!0;if(this.delay_timer||this.save_transaction)return;this.start_save_timer()},start_save_timer:function(){this.cancel_delay(),this.delay_timer=e.later(this.delay,this,this.save_changes)},cancel_delay:function(){this.delay_timer&&this.delay_timer!==!0&&this.delay_timer.cancel(),this.delay_timer=null},save_changes:function(){this.cancel_delay(),this.dirty=!1;if(this.is_time_nearly_over()){this.stop_autosaving();return}typeof tinyMCE!="undefined"&&tinyMCE.triggerSave(),this.save_transaction=e.io(this.AUTOSAVE_HANDLER,{method:"POST",form:{id:this.form},on:{success:this.save_done,failure:this.save_failed},context:this})},save_done:function(t,n){if(n.responseText!=="OK"){this.save_failed(t,n);return}this.save_transaction=null,this.dirty&&this.start_save_timer(),this.savefailures>0?(e.one(this.SELECTORS.CONNECTION_ERROR).hide(),e.one(this.SELECTORS.CONNECTION_OK).show(),this.savefailures=this.FIRST_SUCCESSFUL_SAVE):this.savefailures===this.FIRST_SUCCESSFUL_SAVE&&(e.one(this.SELECTORS.CONNECTION_OK).hide(),this.savefailures=0)},save_failed:function(){this.save_transaction=null,this.start_save_timer(),this.savefailures=Math.max(1,this.savefailures+1),this.savefailures===this.FAILURES_BEFORE_NOTIFY&&(e.one(this.SELECTORS.CONNECTION_ERROR).show(),e.one(this.SELECTORS.CONNECTION_OK).hide())},is_time_nearly_over:function(){return M.mod_quiz.timer&&M.mod_quiz.timer.endtime&&(new Date).getTime()+2*this.delay>M.mod_quiz.timer.endtime},stop_autosaving:function(){this.cancel_delay(),this.delay_timer=!0,this.save_transaction&&this.save_transaction.abort()}}},"@VERSION@",{requires:["base","node","event","event-valuechange","node-event-delegate","io-form"]});
|
||||
YUI.add("moodle-mod_quiz-autosave",function(e,t){M.mod_quiz=M.mod_quiz||{},M.mod_quiz.autosave={TINYMCE_DETECTION_DELAY:500,TINYMCE_DETECTION_REPEATS:20,WATCH_HIDDEN_DELAY:1e3,FAILURES_BEFORE_NOTIFY:1,FIRST_SUCCESSFUL_SAVE:-1,SELECTORS:{QUIZ_FORM:"#responseform",VALUE_CHANGE_ELEMENTS:'input, textarea, [contenteditable="true"]',CHANGE_ELEMENTS:"input, select",HIDDEN_INPUTS:"input[type=hidden]",CONNECTION_ERROR:"#connection-error",CONNECTION_OK:"#connection-ok"},AUTOSAVE_HANDLER:M.cfg.wwwroot+"/mod/quiz/autosave.ajax.php",delay:12e4,form:null,dirty:!1,delay_timer:null,save_transaction:null,savefailures:0,editor_change_handler:null,hidden_field_values:{},init:function(t){this.form=e.one(this.SELECTORS.QUIZ_FORM);if(!this.form)return;this.delay=t*1e3,this.form.delegate("valuechange",this.value_changed,this.SELECTORS.VALUE_CHANGE_ELEMENTS,this),this.form.delegate("change",this.value_changed,this.SELECTORS.CHANGE_ELEMENTS,this),this.form.on("submit",this.stop_autosaving,this),this.init_tinymce(this.TINYMCE_DETECTION_REPEATS),this.save_hidden_field_values(),this.watch_hidden_fields()},save_hidden_field_values:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name");if(!t)return;this.hidden_field_values[t]=e.get("value")},this)},watch_hidden_fields:function(){this.detect_hidden_field_changes(),e.later(this.WATCH_HIDDEN_DELAY,this,this.watch_hidden_fields)},detect_hidden_field_changes:function(){this.form.all(this.SELECTORS.HIDDEN_INPUTS).each(function(e){var t=e.get("name"),n=e.get("value");if(!t)return;if(!(t in this.hidden_field_values)||n!==this.hidden_field_values[t])this.hidden_field_values[t]=n,this.value_changed({target:e})},this)},init_tinymce:function(t){if(typeof tinyMCE=="undefined"){t>0&&e.later(this.TINYMCE_DETECTION_DELAY,this,this.init_tinymce,[t-1]);return}this.editor_change_handler=e.bind(this.editor_changed,this),tinyMCE.onAddEditor.add(e.bind(this.init_tinymce_editor,this))},init_tinymce_editor:function(e,t){t.onChange.add(this.editor_change_handler),t.onRedo.add(this.editor_change_handler),t.onUndo.add(this.editor_change_handler),t.onKeyDown.add(this.editor_change_handler)},value_changed:function(e){var t=e.target.getAttribute("name");if(t==="thispage"||t==="scrollpos"||t&&t.match(/_:flagged$/))return;t=t||"#"+e.target.getAttribute("id"),this.start_save_timer_if_necessary()},editor_changed:function(e){this.start_save_timer_if_necessary()},start_save_timer_if_necessary:function(){this.dirty=!0;if(this.delay_timer||this.save_transaction)return;this.start_save_timer()},start_save_timer:function(){this.cancel_delay(),this.delay_timer=e.later(this.delay,this,this.save_changes)},cancel_delay:function(){this.delay_timer&&this.delay_timer!==!0&&this.delay_timer.cancel(),this.delay_timer=null},save_changes:function(){this.cancel_delay(),this.dirty=!1;if(this.is_time_nearly_over()){this.stop_autosaving();return}typeof tinyMCE!="undefined"&&tinyMCE.triggerSave(),this.save_transaction=e.io(this.AUTOSAVE_HANDLER,{method:"POST",form:{id:this.form},on:{success:this.save_done,failure:this.save_failed},context:this})},save_done:function(t,n){if(n.responseText!=="OK"){this.save_failed(t,n);return}this.save_transaction=null,this.dirty&&this.start_save_timer(),this.savefailures>0?(e.one(this.SELECTORS.CONNECTION_ERROR).hide(),e.one(this.SELECTORS.CONNECTION_OK).show(),this.savefailures=this.FIRST_SUCCESSFUL_SAVE):this.savefailures===this.FIRST_SUCCESSFUL_SAVE&&(e.one(this.SELECTORS.CONNECTION_OK).hide(),this.savefailures=0)},save_failed:function(){this.save_transaction=null,this.start_save_timer(),this.savefailures=Math.max(1,this.savefailures+1),this.savefailures===this.FAILURES_BEFORE_NOTIFY&&(e.one(this.SELECTORS.CONNECTION_ERROR).show(),e.one(this.SELECTORS.CONNECTION_OK).hide())},is_time_nearly_over:function(){return M.mod_quiz.timer&&M.mod_quiz.timer.endtime&&(new Date).getTime()+2*this.delay>M.mod_quiz.timer.endtime},stop_autosaving:function(){this.cancel_delay(),this.delay_timer=!0,this.save_transaction&&this.save_transaction.abort()}}},"@VERSION@",{requires:["base","node","event","event-valuechange","node-event-delegate","io-form"]});
|
||||
|
@ -91,7 +91,7 @@ M.mod_quiz.autosave = {
|
||||
*/
|
||||
SELECTORS: {
|
||||
QUIZ_FORM: '#responseform',
|
||||
VALUE_CHANGE_ELEMENTS: 'input, textarea',
|
||||
VALUE_CHANGE_ELEMENTS: 'input, textarea, [contenteditable="true"]',
|
||||
CHANGE_ELEMENTS: 'input, select',
|
||||
HIDDEN_INPUTS: 'input[type=hidden]',
|
||||
CONNECTION_ERROR: '#connection-error',
|
||||
@ -278,10 +278,13 @@ M.mod_quiz.autosave = {
|
||||
},
|
||||
|
||||
value_changed: function(e) {
|
||||
if (e.target.get('name') === 'thispage' || e.target.get('name') === 'scrollpos' ||
|
||||
e.target.get('name').match(/_:flagged$/)) {
|
||||
var name = e.target.getAttribute('name');
|
||||
if (name === 'thispage' || name === 'scrollpos' || (name && name.match(/_:flagged$/))) {
|
||||
return; // Not interesting.
|
||||
}
|
||||
|
||||
// Fallback to the ID when the name is not present (in the case of content editable).
|
||||
name = name || '#' + e.target.getAttribute('id');
|
||||
this.start_save_timer_if_necessary();
|
||||
},
|
||||
|
||||
|
31
mod/quiz/yui/src/autosave/js/autosave.js
vendored
31
mod/quiz/yui/src/autosave/js/autosave.js
vendored
@ -89,7 +89,7 @@ M.mod_quiz.autosave = {
|
||||
*/
|
||||
SELECTORS: {
|
||||
QUIZ_FORM: '#responseform',
|
||||
VALUE_CHANGE_ELEMENTS: 'input, textarea',
|
||||
VALUE_CHANGE_ELEMENTS: 'input, textarea, [contenteditable="true"]',
|
||||
CHANGE_ELEMENTS: 'input, select',
|
||||
HIDDEN_INPUTS: 'input[type=hidden]',
|
||||
CONNECTION_ERROR: '#connection-error',
|
||||
@ -192,7 +192,7 @@ M.mod_quiz.autosave = {
|
||||
init: function(delay) {
|
||||
this.form = Y.one(this.SELECTORS.QUIZ_FORM);
|
||||
if (!this.form) {
|
||||
Y.log('No response form found. Why did you try to set up autosave?');
|
||||
Y.log('No response form found. Why did you try to set up autosave?', 'debug', 'moodle-mod_quiz-autosave');
|
||||
return;
|
||||
}
|
||||
|
||||
@ -254,12 +254,12 @@ M.mod_quiz.autosave = {
|
||||
if (repeatcount > 0) {
|
||||
Y.later(this.TINYMCE_DETECTION_DELAY, this, this.init_tinymce, [repeatcount - 1]);
|
||||
} else {
|
||||
Y.log('Gave up looking for TinyMCE.');
|
||||
Y.log('Gave up looking for TinyMCE.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Y.log('Found TinyMCE.');
|
||||
Y.log('Found TinyMCE.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.editor_change_handler = Y.bind(this.editor_changed, this);
|
||||
tinyMCE.onAddEditor.add(Y.bind(this.init_tinymce_editor, this));
|
||||
},
|
||||
@ -272,7 +272,7 @@ M.mod_quiz.autosave = {
|
||||
* @param {Object} editor The TinyMCE editor object
|
||||
*/
|
||||
init_tinymce_editor: function(e, editor) {
|
||||
Y.log('Found TinyMCE editor ' + editor.id + '.');
|
||||
Y.log('Found TinyMCE editor ' + editor.id + '.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
editor.onChange.add(this.editor_change_handler);
|
||||
editor.onRedo.add(this.editor_change_handler);
|
||||
editor.onUndo.add(this.editor_change_handler);
|
||||
@ -280,16 +280,19 @@ M.mod_quiz.autosave = {
|
||||
},
|
||||
|
||||
value_changed: function(e) {
|
||||
if (e.target.get('name') === 'thispage' || e.target.get('name') === 'scrollpos' ||
|
||||
e.target.get('name').match(/_:flagged$/)) {
|
||||
var name = e.target.getAttribute('name');
|
||||
if (name === 'thispage' || name === 'scrollpos' || (name && name.match(/_:flagged$/))) {
|
||||
return; // Not interesting.
|
||||
}
|
||||
Y.log('Detected a value change in element ' + e.target.get('name') + '.');
|
||||
|
||||
// Fallback to the ID when the name is not present (in the case of content editable).
|
||||
name = name || '#' + e.target.getAttribute('id');
|
||||
Y.log('Detected a value change in element ' + name + '.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.start_save_timer_if_necessary();
|
||||
},
|
||||
|
||||
editor_changed: function(editor) {
|
||||
Y.log('Detected a value change in editor ' + editor.id + '.');
|
||||
Y.log('Detected a value change in editor ' + editor.id + '.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.start_save_timer_if_necessary();
|
||||
},
|
||||
|
||||
@ -321,12 +324,12 @@ M.mod_quiz.autosave = {
|
||||
this.dirty = false;
|
||||
|
||||
if (this.is_time_nearly_over()) {
|
||||
Y.log('No more saving, time is nearly over.');
|
||||
Y.log('No more saving, time is nearly over.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.stop_autosaving();
|
||||
return;
|
||||
}
|
||||
|
||||
Y.log('Doing a save.');
|
||||
Y.log('Doing a save.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
if (typeof tinyMCE !== 'undefined') {
|
||||
tinyMCE.triggerSave();
|
||||
}
|
||||
@ -349,11 +352,11 @@ M.mod_quiz.autosave = {
|
||||
return;
|
||||
}
|
||||
|
||||
Y.log('Save completed.');
|
||||
Y.log('Save completed.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.save_transaction = null;
|
||||
|
||||
if (this.dirty) {
|
||||
Y.log('Dirty after save.');
|
||||
Y.log('Dirty after save.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.start_save_timer();
|
||||
}
|
||||
|
||||
@ -368,7 +371,7 @@ M.mod_quiz.autosave = {
|
||||
},
|
||||
|
||||
save_failed: function() {
|
||||
Y.log('Save failed.');
|
||||
Y.log('Save failed.', 'debug', 'moodle-mod_quiz-autosave');
|
||||
this.save_transaction = null;
|
||||
|
||||
// We want to retry soon.
|
||||
|
Loading…
x
Reference in New Issue
Block a user