MDL-18014 Atto: Assorted coding style changes from peer review

This commit is contained in:
Damyon Wiese 2014-08-05 13:59:23 +08:00
parent 6bfd450a6d
commit 19549f8bed
5 changed files with 129 additions and 118 deletions

View File

@ -472,12 +472,16 @@ Y.namespace('M.editor_atto.Editor').init = function(config) {
* A notify function for the Atto editor.
*
* @module moodle-editor_atto-notify
* @submodule notify-base
* @submodule notify
* @package editor_atto
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
var LOGNAME_NOTIFY = 'moodle-editor_atto-editor-notify',
NOTIFY_INFO = 'info',
NOTIFY_WARNING = 'warning';
function EditorNotify() {}
EditorNotify.ATTRS= {
@ -486,10 +490,10 @@ EditorNotify.ATTRS= {
EditorNotify.prototype = {
/**
* A single Y.Overlay for this editor. There is only ever one - it is replaced if a new message comes in.
* A single Y.Node for this editor. There is only ever one - it is replaced if a new message comes in.
*
* @property messageOverlay
* @type {Y.Overlay}
* @type {Node}
*/
messageOverlay: null,
@ -521,12 +525,15 @@ EditorNotify.prototype = {
* Show a notification in a floaty overlay somewhere in the atto editor text area.
*
* @method showMessage
* @param {String} message - The translated message (use get_string)
* @param {String} type - Must be either "info" or "warning"
* @param {Integer} timeout - Time in milliseconds to show this message for.
* @param {String} message The translated message (use get_string)
* @param {String} type Must be either "info" or "warning"
* @param {Number} timeout Time in milliseconds to show this message for.
* @chainable
*/
showMessage: function(message, type, timeout) {
var messageTypeIcon = '',
intTimeout,
bodyContent;
if (this.messageOverlay === null) {
this.messageOverlay = Y.Node.create('<div class="editor_atto_notification"></div>');
@ -543,29 +550,28 @@ EditorNotify.prototype = {
this.hideTimer.cancel();
}
var messageTypeIcon = '';
if (type === "warning") {
if (type === NOTIFY_WARNING) {
messageTypeIcon = '<img src="' +
M.util.image_url('i/warning', 'moodle') +
'" alt="' + M.util.get_string('warning', 'moodle') + '"/>';
} else if (type === "info") {
} else if (type === NOTIFY_INFO) {
messageTypeIcon = '<img src="' +
M.util.image_url('i/info', 'moodle') +
'" alt="' + M.util.get_string('info', 'moodle') + '"/>';
} else {
Y.log('Invalid message type specified: ' + type + '. Must be either "info" or "warning".', 'debug');
Y.log('Invalid message type specified: ' + type + '. Must be either "info" or "warning".', 'debug', LOGNAME_NOTIFY);
}
// Parse the timeout value.
var inttimeout = parseInt(timeout, 10);
if (inttimeout <= 0) {
inttimeout = 60000;
intTimeout = parseInt(timeout, 10);
if (intTimeout <= 0) {
intTimeout = 60000;
}
// Convert class to atto_info (for example).
type = 'atto_' + type;
var bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
messageTypeIcon + ' ' +
Y.Escape.html(message) +
'</div>');
@ -573,8 +579,8 @@ EditorNotify.prototype = {
this.messageOverlay.append(bodyContent);
this.messageOverlay.show();
this.hideTimer = Y.later(inttimeout, this, function() {
Y.log('Hide Atto notification.', 'debug');
this.hideTimer = Y.later(intTimeout, this, function() {
Y.log('Hide Atto notification.', 'debug', LOGNAME_NOTIFY);
this.hideTimer = null;
this.messageOverlay.hide();
});
@ -722,7 +728,8 @@ Y.Base.mix(Y.M.editor_atto.Editor, [EditorTextArea]);
*/
var SUCCESS_MESSAGE_TIMEOUT = 5000,
RECOVER_MESSAGE_TIMEOUT = 60000;
RECOVER_MESSAGE_TIMEOUT = 60000,
LOGNAME_AUTOSAVE = 'moodle-editor_atto-editor-autosave';
function EditorAutosave() {}
@ -743,7 +750,7 @@ EditorAutosave.ATTRS= {
* The time between autosaves (in seconds).
*
* @attribute autosaveFrequency
* @type Integer
* @type Number
* @default 60
* @writeOnce
*/
@ -811,22 +818,21 @@ EditorAutosave.prototype = {
Y.io(url, {
method: 'POST',
data: params,
context: this,
on: {
success: function(id,o) {
if (typeof o.responseText !== "undefined" &&
o.responseText !== "" &&
o.responseText !== this.textarea.get('value')) {
Y.log('Autosave text found - confirm recovery.');
Y.log('Autosave text found - confirm recovery.', 'debug', LOGNAME_AUTOSAVE);
this.recoverText(o.responseText);
}
}
},
context: this
}
});
// Now setup the timer for periodic saves.
Y.log(this.get('autosaveFrequency'));
var delay = parseInt(this.get('autosaveFrequency'), 10) * 1000;
Y.later(delay, this, this.saveDraft, false, true);
@ -853,7 +859,6 @@ EditorAutosave.prototype = {
pagedomid: Y.one('body').get('id')
};
// We don't even wait!
Y.io(url, {
method: 'POST',
data: params,
@ -876,7 +881,7 @@ EditorAutosave.prototype = {
this.updateOriginal();
this.lastText = text;
this.showMessage(M.util.get_string('textrecovered', 'editor_atto'), 'info', RECOVER_MESSAGE_TIMEOUT);
this.showMessage(M.util.get_string('textrecovered', 'editor_atto'), NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
return this;
},
@ -892,7 +897,7 @@ EditorAutosave.prototype = {
var newText = this.textarea.get('value');
if (newText !== this.lastText) {
Y.log('Autosave text');
Y.log('Autosave text', 'debug', LOGNAME_AUTOSAVE);
// Make an ajax request.
url = M.cfg.wwwroot + '/lib/editor/atto/autosave-ajax.php';
@ -905,35 +910,32 @@ EditorAutosave.prototype = {
pagedomid: Y.one('body').get('id'),
pageinstance: this.autosaveInstance
};
var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
// Reusable error handler - must be passed the correct context.
var ajaxErrorFunction = function(code, response) {
var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
Y.log('Error while autosaving text:' + code, 'warn', LOGNAME_AUTOSAVE);
Y.log(response, 'warn', LOGNAME_AUTOSAVE);
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), NOTIFY_WARNING, errorDuration);
};
Y.io(url, {
method: 'POST',
data: params,
context: this,
on: {
error: function(code, response) {
Y.log('Error while autosaving text:' + code, 'warn');
Y.log(response, 'warn');
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
},
failure: function(code, response) {
Y.log('Failure while autosaving text:' + code, 'warn');
Y.log(response, 'warn');
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
},
error: ajaxErrorFunction,
failure: ajaxErrorFunction,
success: function(code, response) {
if (response.response !== "") {
Y.log('Failure while autosaving text.', 'warn');
Y.log(response, 'debug');
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
ajaxErrorFunction(code, response);
} else {
// All working.
this.lastText = newText;
this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'), 'info', SUCCESS_MESSAGE_TIMEOUT);
this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'), NOTIFY_INFO, SUCCESS_MESSAGE_TIMEOUT);
}
}
},
context: this
}
});
}
return this;

File diff suppressed because one or more lines are too long

View File

@ -469,12 +469,16 @@ Y.namespace('M.editor_atto.Editor').init = function(config) {
* A notify function for the Atto editor.
*
* @module moodle-editor_atto-notify
* @submodule notify-base
* @submodule notify
* @package editor_atto
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
var LOGNAME_NOTIFY = 'moodle-editor_atto-editor-notify',
NOTIFY_INFO = 'info',
NOTIFY_WARNING = 'warning';
function EditorNotify() {}
EditorNotify.ATTRS= {
@ -483,10 +487,10 @@ EditorNotify.ATTRS= {
EditorNotify.prototype = {
/**
* A single Y.Overlay for this editor. There is only ever one - it is replaced if a new message comes in.
* A single Y.Node for this editor. There is only ever one - it is replaced if a new message comes in.
*
* @property messageOverlay
* @type {Y.Overlay}
* @type {Node}
*/
messageOverlay: null,
@ -518,12 +522,15 @@ EditorNotify.prototype = {
* Show a notification in a floaty overlay somewhere in the atto editor text area.
*
* @method showMessage
* @param {String} message - The translated message (use get_string)
* @param {String} type - Must be either "info" or "warning"
* @param {Integer} timeout - Time in milliseconds to show this message for.
* @param {String} message The translated message (use get_string)
* @param {String} type Must be either "info" or "warning"
* @param {Number} timeout Time in milliseconds to show this message for.
* @chainable
*/
showMessage: function(message, type, timeout) {
var messageTypeIcon = '',
intTimeout,
bodyContent;
if (this.messageOverlay === null) {
this.messageOverlay = Y.Node.create('<div class="editor_atto_notification"></div>');
@ -540,12 +547,11 @@ EditorNotify.prototype = {
this.hideTimer.cancel();
}
var messageTypeIcon = '';
if (type === "warning") {
if (type === NOTIFY_WARNING) {
messageTypeIcon = '<img src="' +
M.util.image_url('i/warning', 'moodle') +
'" alt="' + M.util.get_string('warning', 'moodle') + '"/>';
} else if (type === "info") {
} else if (type === NOTIFY_INFO) {
messageTypeIcon = '<img src="' +
M.util.image_url('i/info', 'moodle') +
'" alt="' + M.util.get_string('info', 'moodle') + '"/>';
@ -553,15 +559,15 @@ EditorNotify.prototype = {
}
// Parse the timeout value.
var inttimeout = parseInt(timeout, 10);
if (inttimeout <= 0) {
inttimeout = 60000;
intTimeout = parseInt(timeout, 10);
if (intTimeout <= 0) {
intTimeout = 60000;
}
// Convert class to atto_info (for example).
type = 'atto_' + type;
var bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
messageTypeIcon + ' ' +
Y.Escape.html(message) +
'</div>');
@ -569,7 +575,7 @@ EditorNotify.prototype = {
this.messageOverlay.append(bodyContent);
this.messageOverlay.show();
this.hideTimer = Y.later(inttimeout, this, function() {
this.hideTimer = Y.later(intTimeout, this, function() {
this.hideTimer = null;
this.messageOverlay.hide();
});
@ -717,7 +723,8 @@ Y.Base.mix(Y.M.editor_atto.Editor, [EditorTextArea]);
*/
var SUCCESS_MESSAGE_TIMEOUT = 5000,
RECOVER_MESSAGE_TIMEOUT = 60000;
RECOVER_MESSAGE_TIMEOUT = 60000,
LOGNAME_AUTOSAVE = 'moodle-editor_atto-editor-autosave';
function EditorAutosave() {}
@ -738,7 +745,7 @@ EditorAutosave.ATTRS= {
* The time between autosaves (in seconds).
*
* @attribute autosaveFrequency
* @type Integer
* @type Number
* @default 60
* @writeOnce
*/
@ -806,6 +813,7 @@ EditorAutosave.prototype = {
Y.io(url, {
method: 'POST',
data: params,
context: this,
on: {
success: function(id,o) {
if (typeof o.responseText !== "undefined" &&
@ -814,8 +822,7 @@ EditorAutosave.prototype = {
this.recoverText(o.responseText);
}
}
},
context: this
}
});
// Now setup the timer for periodic saves.
@ -846,7 +853,6 @@ EditorAutosave.prototype = {
pagedomid: Y.one('body').get('id')
};
// We don't even wait!
Y.io(url, {
method: 'POST',
data: params,
@ -869,7 +875,7 @@ EditorAutosave.prototype = {
this.updateOriginal();
this.lastText = text;
this.showMessage(M.util.get_string('textrecovered', 'editor_atto'), 'info', RECOVER_MESSAGE_TIMEOUT);
this.showMessage(M.util.get_string('textrecovered', 'editor_atto'), NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
return this;
},
@ -897,29 +903,30 @@ EditorAutosave.prototype = {
pagedomid: Y.one('body').get('id'),
pageinstance: this.autosaveInstance
};
var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
// Reusable error handler - must be passed the correct context.
var ajaxErrorFunction = function(code, response) {
var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), NOTIFY_WARNING, errorDuration);
};
Y.io(url, {
method: 'POST',
data: params,
context: this,
on: {
error: function(code, response) {
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
},
failure: function(code, response) {
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
},
error: ajaxErrorFunction,
failure: ajaxErrorFunction,
success: function(code, response) {
if (response.response !== "") {
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
ajaxErrorFunction(code, response);
} else {
// All working.
this.lastText = newText;
this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'), 'info', SUCCESS_MESSAGE_TIMEOUT);
this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'), NOTIFY_INFO, SUCCESS_MESSAGE_TIMEOUT);
}
}
},
context: this
}
});
}
return this;

View File

@ -24,7 +24,8 @@
*/
var SUCCESS_MESSAGE_TIMEOUT = 5000,
RECOVER_MESSAGE_TIMEOUT = 60000;
RECOVER_MESSAGE_TIMEOUT = 60000,
LOGNAME_AUTOSAVE = 'moodle-editor_atto-editor-autosave';
function EditorAutosave() {}
@ -45,7 +46,7 @@ EditorAutosave.ATTRS= {
* The time between autosaves (in seconds).
*
* @attribute autosaveFrequency
* @type Integer
* @type Number
* @default 60
* @writeOnce
*/
@ -113,22 +114,21 @@ EditorAutosave.prototype = {
Y.io(url, {
method: 'POST',
data: params,
context: this,
on: {
success: function(id,o) {
if (typeof o.responseText !== "undefined" &&
o.responseText !== "" &&
o.responseText !== this.textarea.get('value')) {
Y.log('Autosave text found - confirm recovery.');
Y.log('Autosave text found - confirm recovery.', 'debug', LOGNAME_AUTOSAVE);
this.recoverText(o.responseText);
}
}
},
context: this
}
});
// Now setup the timer for periodic saves.
Y.log(this.get('autosaveFrequency'));
var delay = parseInt(this.get('autosaveFrequency'), 10) * 1000;
Y.later(delay, this, this.saveDraft, false, true);
@ -155,7 +155,6 @@ EditorAutosave.prototype = {
pagedomid: Y.one('body').get('id')
};
// We don't even wait!
Y.io(url, {
method: 'POST',
data: params,
@ -178,7 +177,7 @@ EditorAutosave.prototype = {
this.updateOriginal();
this.lastText = text;
this.showMessage(M.util.get_string('textrecovered', 'editor_atto'), 'info', RECOVER_MESSAGE_TIMEOUT);
this.showMessage(M.util.get_string('textrecovered', 'editor_atto'), NOTIFY_INFO, RECOVER_MESSAGE_TIMEOUT);
return this;
},
@ -194,7 +193,7 @@ EditorAutosave.prototype = {
var newText = this.textarea.get('value');
if (newText !== this.lastText) {
Y.log('Autosave text');
Y.log('Autosave text', 'debug', LOGNAME_AUTOSAVE);
// Make an ajax request.
url = M.cfg.wwwroot + '/lib/editor/atto/autosave-ajax.php';
@ -207,35 +206,32 @@ EditorAutosave.prototype = {
pagedomid: Y.one('body').get('id'),
pageinstance: this.autosaveInstance
};
var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
// Reusable error handler - must be passed the correct context.
var ajaxErrorFunction = function(code, response) {
var errorDuration = parseInt(this.get('autosaveFrequency'), 10) * 1000;
Y.log('Error while autosaving text:' + code, 'warn', LOGNAME_AUTOSAVE);
Y.log(response, 'warn', LOGNAME_AUTOSAVE);
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), NOTIFY_WARNING, errorDuration);
};
Y.io(url, {
method: 'POST',
data: params,
context: this,
on: {
error: function(code, response) {
Y.log('Error while autosaving text:' + code, 'warn');
Y.log(response, 'warn');
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
},
failure: function(code, response) {
Y.log('Failure while autosaving text:' + code, 'warn');
Y.log(response, 'warn');
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
},
error: ajaxErrorFunction,
failure: ajaxErrorFunction,
success: function(code, response) {
if (response.response !== "") {
Y.log('Failure while autosaving text.', 'warn');
Y.log(response, 'debug');
this.showMessage(M.util.get_string('autosavefailed', 'editor_atto'), 'warning', errorDuration);
ajaxErrorFunction(code, response);
} else {
// All working.
this.lastText = newText;
this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'), 'info', SUCCESS_MESSAGE_TIMEOUT);
this.showMessage(M.util.get_string('autosavesucceeded', 'editor_atto'), NOTIFY_INFO, SUCCESS_MESSAGE_TIMEOUT);
}
}
},
context: this
}
});
}
return this;

View File

@ -17,12 +17,16 @@
* A notify function for the Atto editor.
*
* @module moodle-editor_atto-notify
* @submodule notify-base
* @submodule notify
* @package editor_atto
* @copyright 2014 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
var LOGNAME_NOTIFY = 'moodle-editor_atto-editor-notify',
NOTIFY_INFO = 'info',
NOTIFY_WARNING = 'warning';
function EditorNotify() {}
EditorNotify.ATTRS= {
@ -31,10 +35,10 @@ EditorNotify.ATTRS= {
EditorNotify.prototype = {
/**
* A single Y.Overlay for this editor. There is only ever one - it is replaced if a new message comes in.
* A single Y.Node for this editor. There is only ever one - it is replaced if a new message comes in.
*
* @property messageOverlay
* @type {Y.Overlay}
* @type {Node}
*/
messageOverlay: null,
@ -66,12 +70,15 @@ EditorNotify.prototype = {
* Show a notification in a floaty overlay somewhere in the atto editor text area.
*
* @method showMessage
* @param {String} message - The translated message (use get_string)
* @param {String} type - Must be either "info" or "warning"
* @param {Integer} timeout - Time in milliseconds to show this message for.
* @param {String} message The translated message (use get_string)
* @param {String} type Must be either "info" or "warning"
* @param {Number} timeout Time in milliseconds to show this message for.
* @chainable
*/
showMessage: function(message, type, timeout) {
var messageTypeIcon = '',
intTimeout,
bodyContent;
if (this.messageOverlay === null) {
this.messageOverlay = Y.Node.create('<div class="editor_atto_notification"></div>');
@ -88,29 +95,28 @@ EditorNotify.prototype = {
this.hideTimer.cancel();
}
var messageTypeIcon = '';
if (type === "warning") {
if (type === NOTIFY_WARNING) {
messageTypeIcon = '<img src="' +
M.util.image_url('i/warning', 'moodle') +
'" alt="' + M.util.get_string('warning', 'moodle') + '"/>';
} else if (type === "info") {
} else if (type === NOTIFY_INFO) {
messageTypeIcon = '<img src="' +
M.util.image_url('i/info', 'moodle') +
'" alt="' + M.util.get_string('info', 'moodle') + '"/>';
} else {
Y.log('Invalid message type specified: ' + type + '. Must be either "info" or "warning".', 'debug');
Y.log('Invalid message type specified: ' + type + '. Must be either "info" or "warning".', 'debug', LOGNAME_NOTIFY);
}
// Parse the timeout value.
var inttimeout = parseInt(timeout, 10);
if (inttimeout <= 0) {
inttimeout = 60000;
intTimeout = parseInt(timeout, 10);
if (intTimeout <= 0) {
intTimeout = 60000;
}
// Convert class to atto_info (for example).
type = 'atto_' + type;
var bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
bodyContent = Y.Node.create('<div class="' + type + '" role="alert" aria-live="assertive">' +
messageTypeIcon + ' ' +
Y.Escape.html(message) +
'</div>');
@ -118,8 +124,8 @@ EditorNotify.prototype = {
this.messageOverlay.append(bodyContent);
this.messageOverlay.show();
this.hideTimer = Y.later(inttimeout, this, function() {
Y.log('Hide Atto notification.', 'debug');
this.hideTimer = Y.later(intTimeout, this, function() {
Y.log('Hide Atto notification.', 'debug', LOGNAME_NOTIFY);
this.hideTimer = null;
this.messageOverlay.hide();
});