MDL-50308 tool_lp: Big javascript cleanup

This includes:
* Move all JS modules from lib/amd to admin/tool/lp/amd
* Add all jsdocs
* fix all jshint warnings
* Rewrite the actionmenu wrapper to a real amd module
This commit is contained in:
Damyon Wiese 2015-05-14 13:35:38 +08:00 committed by Frederic Massart
parent d629323f72
commit 7e8d4dac9b
41 changed files with 1805 additions and 398 deletions

View file

@ -1 +0,0 @@
define(["core/str","core/yui"],function(a,b){var c=function(a){var b=a.drag.get("node"),c=a.drop.get("node");this.callback(b.getDOMNode(),c.getDOMNode())};return{dragdrop:function(d,e,f,g,h,i,j,k){a.get_strings([{key:"emptydragdropregion",component:"moodle"},{key:"movecontent",component:"moodle"},{key:"tocontent",component:"moodle"}]).done(function(){b.use("moodle-core-dragdrop-reorder",function(){var a={callback:k};M.core.dragdrop_reorder({group:d,dragHandleText:e,sameNodeText:f,parentNodeText:g,sameNodeClass:h,parentNodeClass:i,dragHandleInsertClass:j,callback:b.bind(c,a)})})})}}});

View file

@ -1 +0,0 @@
define(["jquery","core/templates","core/notification","core/yui"],function(a,b,c,d){var e=function(b){var c=a(b);c.find("li a").each(function(b,c){a(c).addClass("menu-action"),a(c).attr("role","menuitem")}),a(this).replaceWith(c),d.use("moodle-core-actionmenu",function(){if(null===M.core.actionmenu.instance)M.core.actionmenu.init();else{var a=d.one(c.get(0));M.core.actionmenu.newDOMNode(a.ancestor())}})};return{menu:function(d,f){a(f).each(function(f,g){var h=[];a(g).find("li").each(function(b,c){h.push(a(c).html().trim())});var i={triggerMessage:d,links:h},j=a.proxy(e,g);b.render("core/menu",i).done(j).fail(c.exception)})}}});

View file

@ -1,76 +0,0 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Wrapper for the YUI M.core.notification class. Allows us to
* use the YUI version in AMD code until it is replaced.
*
* @module core/dialogue
* @package core
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/yui'], function(Y) {
// Private variables and functions.
/**
* Constructor
*
* @param {string} title Title for the window.
* @param {string} content The content for the window.
* @param {function} afterShow Callback executed after the window is opened.
*/
var dialogue = function(title, content, afterShow) {
this.yuiDialogue = null;
var parent = this;
Y.use('moodle-core-notification', function () {
parent.yuiDialogue = new M.core.dialogue({
headerContent: title,
bodyContent: content,
draggable: true,
visible: false,
center: true,
modal: true
});
parent.yuiDialogue.after('visibleChange', function(e) {
if (e.newVal) {
afterShow(parent);
}
});
parent.yuiDialogue.show();
});
};
/**
* Close this window.
*/
dialogue.prototype.close = function() {
this.yuiDialogue.hide();
this.yuiDialogue.destroy();
};
/**
* Get content.
*/
dialogue.prototype.getContent = function() {
return this.yuiDialogue.bodyNode.getDOMNode();
};
return /** @alias module:core/dialogue */ dialogue;
});

View file

@ -1,89 +0,0 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Wrapper for the YUI M.core.dragdrop class. Allows us to
* use the YUI version in AMD code until it is replaced.
*
* @module core/dragdrop-reorder
* @package core
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/str', 'core/yui'], function(str, Y) {
// Private variables and functions.
/**
* Translate the drophit event from YUI
* into simple drag and drop nodes.
* @param {Y.Event} e The yui drop event.
*/
var proxyCallback = function(e) {
var dragNode = e.drag.get('node');
var dropNode = e.drop.get('node');
this.callback(dragNode.getDOMNode(), dropNode.getDOMNode());
};
return /** @alias module:core/dragdrop-reorder */ {
// Public variables and functions.
/**
* Create an instance of M.core.dragdrop
*
* @param {string} group Unique string to identify this interaction.
* @param {string} dragHandleText Alt text for the drag handle.
* @param {string} sameNodeText Used in keyboard drag drop for the list of items target.
* @param {string} parentNodeText Used in keyboard drag drop for the parent target.
* @param {string} sameNodeClass class used to find the each of the list of items.
* @param {string} parentNodeClass class used to find the container for the list of items.
* @param {string} dragHandleInsertClass class used to find the location to insert the drag handles.
* @param {function} callback Drop hit handler.
*/
dragdrop: function(group,
dragHandleText,
sameNodeText,
parentNodeText,
sameNodeClass,
parentNodeClass,
dragHandleInsertClass,
callback) {
// Here we are wrapping YUI. This allows us to start transitioning, but
// wait for a good alternative without having inconsistent UIs.
str.get_strings([
{ key: 'emptydragdropregion', component: 'moodle' },
{ key: 'movecontent', component: 'moodle' },
{ key: 'tocontent', component: 'moodle' },
]).done( function () {
Y.use('moodle-core-dragdrop-reorder', function () {
var context = {
callback: callback
};
M.core.dragdrop_reorder({
group: group,
dragHandleText: dragHandleText,
sameNodeText: sameNodeText,
parentNodeText: parentNodeText,
sameNodeClass: sameNodeClass,
parentNodeClass: parentNodeClass,
dragHandleInsertClass: dragHandleInsertClass,
callback: Y.bind(proxyCallback, context)
});
});
});
}
};
});

View file

@ -1,82 +0,0 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Wrapper for the YUI M.core.actionmenu class. Allows us to
* use the YUI version in AMD code until it is replaced.
*
* @module core/menu
* @package core
* @copyright 2015 Damyon Wiese <damyon@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/templates', 'core/notification', 'core/yui'], function($, templates, notification, Y) {
// Private variables and functions.
/**
* Replace the menu node in the dom by this new HTML
* rendered from a template and then call enhance on it.
*
* @param {string} newHTML
*/
var enhanceMenu = function(newHTML) {
var newMenu = $(newHTML);
// Insert some more aria attrs into the menu entries.
newMenu.find('li a').each(function(index, element) {
$(element).addClass('menu-action');
$(element).attr('role', 'menuitem');
});
$(this).replaceWith(newMenu);
Y.use('moodle-core-actionmenu', function() {
if (M.core.actionmenu.instance === null) {
M.core.actionmenu.init();
} else {
var yuiNode = Y.one(newMenu.get(0));
M.core.actionmenu.newDOMNode(yuiNode.ancestor());
}
});
};
return /** @alias module:core/menu */ {
// Public variables and functions.
/**
* Wrap M.core.actionmenu.
* @param {string} triggerMessage Text for the button to open the menu.
* @param {string} selector CSS selector for a list of links to turn into a menu (can match mulitple menus).
*/
menu: function(triggerMessage, selector) {
// First we need to modify the list(s) to have markup compatible with actionmenu.
$(selector).each(function (index, element) {
var links = [];
$(element).find('li').each(function (index, link) {
links.push($(link).html().trim());
});
var data = {
triggerMessage: triggerMessage,
links: links
};
var enhanceCallback = $.proxy(enhanceMenu, element);
templates.render('core/menu', data)
.done(enhanceCallback)
.fail(notification.exception);
});
}
};
});

View file

@ -70,7 +70,7 @@ define(['core/config'], function(config) {
}
// Fix admin urls.
if (typeof config.admin != "undefined" && config.admin !== 'admin') {
if (config.admin !== 'admin') {
relativePath = relativePath.replace(/^\/admin\//, '/' + config.admin + '/');
}
return config.wwwroot + relativePath;