From c46996ea5af1c32e1109ef7cce2d3e43bd5fd887 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Thu, 3 Oct 2013 12:15:34 +1300 Subject: [PATCH] MDL-31830 course: improvements to JS coding style --- .../moodle-course-management-debug.js | 451 ++++++++++-------- .../moodle-course-management-min.js | 6 +- .../moodle-course-management.js | 379 ++++++++------- course/yui/src/management/js/category.js | 154 +++--- course/yui/src/management/js/console.js | 196 ++++---- course/yui/src/management/js/course.js | 34 +- course/yui/src/management/js/dd.js | 15 +- course/yui/src/management/js/item.js | 52 +- 8 files changed, 724 insertions(+), 563 deletions(-) diff --git a/course/yui/build/moodle-course-management/moodle-course-management-debug.js b/course/yui/build/moodle-course-management/moodle-course-management-debug.js index 5cd228a3830..253017394af 100644 --- a/course/yui/build/moodle-course-management/moodle-course-management-debug.js +++ b/course/yui/build/moodle-course-management/moodle-course-management-debug.js @@ -3,7 +3,7 @@ YUI.add('moodle-course-management', function (Y, NAME) { /** * Provides drop down menus for list of action links. * - * @module moodle-core_course-management + * @module moodle-course-management */ /** @@ -11,7 +11,7 @@ YUI.add('moodle-course-management', function (Y, NAME) { * * Provides the organisation for course and category management JS. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Console * @constructor * @extends Y.Base @@ -29,7 +29,7 @@ Console.ATTRS = { */ element : { setter : function(node) { - if (typeof(node) === 'string') { + if (typeof node === 'string') { node = Y.one('#'+node); } return node; @@ -40,15 +40,21 @@ Console.ATTRS = { * The category listing container node. * @attribute categorylisting * @type Node + * @default null */ - categorylisting : {}, + categorylisting : { + value : null + }, /** * The course listing container node. * @attribute courselisting * @type Node + * @default null */ - courselisting : {}, + courselisting : { + value : null + }, /** * The course details container node. @@ -57,22 +63,28 @@ Console.ATTRS = { * @default null */ coursedetails : { - value: null + value : null }, /** * The id of the currently active category. * @attribute activecategoryid - * @type Int + * @type Number + * @default null */ - activecategoryid : {}, + activecategoryid : { + value : null + }, /** * The id of the currently active course. * @attribute activecourseid - * @type Int + * @type Number + * @default Null */ - activecourseid : {}, + activecourseid : { + value : null + }, /** * The categories that are currently available through the management interface. @@ -95,17 +107,12 @@ Console.ATTRS = { /** * The courses that are currently available through the management interface. * @attribute courses - * @type Array - * @default [] + * @type Course[] + * @default Array */ courses : { - setter : function(item, name) { - if (Y.Lang.isArray(item)) { - return item; - } - var items = this.get(name); - items.push(item); - return items; + validator : function(val) { + return Y.Lang.isArray(val); }, value : [] }, @@ -113,7 +120,7 @@ Console.ATTRS = { /** * The currently displayed page of courses. * @attribute page - * @type Int + * @type Number * @default null */ page : { @@ -130,7 +137,7 @@ Console.ATTRS = { /** * The total pages of courses that can be shown for this category. * @attribute totalpages - * @type Int + * @type Number * @default null */ totalpages : { @@ -147,7 +154,7 @@ Console.ATTRS = { /** * The total number of courses belonging to this category. * @attribute totalcourses - * @type Int + * @type Number * @default null */ totalcourses : { @@ -202,7 +209,7 @@ Console.prototype = { * @method initializer */ initializer : function() { - Y.log('Initialising course category management console', 'note', 'core_course'); + Y.log('Initialising course category management console', 'info', 'moodle-course-management'); this.set('element', 'coursecat-management'); var element = this.get('element'), categorylisting = element.one('#category-listing'), @@ -225,8 +232,8 @@ Console.prototype = { if (selectedcourse) { this.set('activecourseid', selectedcourse.getData('id')); } - this.initialise_categories(categorylisting); - this.initialise_courses(); + this.initialiseCategories(categorylisting); + this.initialiseCourses(); if (courselisting) { // No need for dragdrop if we don't have a course listing. @@ -236,11 +243,11 @@ Console.prototype = { /** * Initialises all the categories being shown. - * @method initialise_categories + * @method initialiseCategories * @private * @returns {boolean} */ - initialise_categories : function(listing) { + initialiseCategories : function(listing) { var count = 0; if (!listing) { return false; @@ -253,110 +260,143 @@ Console.prototype = { count++; }, this); if (!this.categoriesinit) { - this.get('categorylisting').delegate('click', this.handle_category_delegation, 'a[data-action]', this); + this.get('categorylisting').delegate('click', this.handleCategoryDelegation, 'a[data-action]', this); this.categoriesinit = true; - Y.log(count+' categories being managed', 'note', 'core_course'); + Y.log(count+' categories being managed', 'info', 'moodle-course-management'); } else { - Y.log(count+' new categories being managed', 'note', 'core_course'); + Y.log(count+' new categories being managed', 'info', 'moodle-course-management'); } }, /** * Initialises all the categories being shown. - * @method initialise_courses + * @method initialiseCourses * @private * @returns {boolean} */ - initialise_courses : function() { - var category = this.get_category_by_id(this.get('activecategoryid')), + initialiseCourses : function() { + var category = this.getCategoryById(this.get('activecategoryid')), listing = this.get('courselisting'), count = 0; if (!listing) { return false; } + if (!category) { + Y.log('Couldn\'t find the current category object.', 'warn', 'moodle-course-management'); + return false; + } listing.all('.listitem[data-id]').each(function(node){ - this.set('courses', new Course({ + this.registerCourse(new Course({ node : node, console : this, category : category })); count++; }, this); - listing.delegate('click', this.handle_course_delegation, 'a[data-action]', this); - Y.log(count+' courses being managed', 'note', 'core_course'); + listing.delegate('click', this.handleCourseDelegation, 'a[data-action]', this); + Y.log(count+' courses being managed', 'info', 'moodle-course-management'); + }, + + /** + * Registers a course within the management display. + * @method registerCourse + * @param {Course} course + */ + registerCourse : function(course) { + var courses = this.get('courses'); + courses.push(course); + this.set('courses', courses); }, /** * Handles the event fired by a delegated course listener. * - * @method handle_course_delegation + * @method handleCourseDelegation * @protected * @param {EventFacade} e */ - handle_course_delegation : function(e) { + handleCourseDelegation : function(e) { var target = e.currentTarget, action = target.getData('action'), courseid = target.ancestor('.listitem').getData('id'), - course = this.get_course_by_id(courseid); - course.handle(action, e); + course = this.getCourseById(courseid); + if (course) { + course.handle(action, e); + } else { + Y.log('Course with ID '+courseid+' could not be found for delegation', 'error', 'moodle-course-management'); + } }, /** * Handles the event fired by a delegated course listener. * - * @method handle_category_delegation + * @method handleCategoryDelegation * @protected * @param {EventFacade} e */ - handle_category_delegation : function(e) { + handleCategoryDelegation : function(e) { var target = e.currentTarget, action = target.getData('action'), categoryid = target.ancestor('.listitem').getData('id'), - category = this.get_category_by_id(categoryid); - category.handle(action, e); + category = this.getCategoryById(categoryid); + if (category) { + category.handle(action, e); + } else { + Y.log('Could not find category to delegate to.', 'error', 'moodle-course-management'); + } }, /** * Returns the category with the given ID. - * @method get_category_by_id - * @param {Int} id - * @returns {Category|Int} The category or the categoryid given if there is no matching category. + * @method getCategoryById + * @param {Number} id + * @returns {Category|Boolean} The category or false if it can't be found. */ - get_category_by_id : function(id) { - var i, category, categories = this.get('categories'), length = categories.length; + getCategoryById : function(id) { + var i, + category, + categories = this.get('categories'), + length = categories.length; for (i = 0; i < length; i++) { category = categories[i]; if (category.get('categoryid') === id) { return category; } } - return id; - }, - - /** - * Returns the course with the given id. - * @method get_course_by_id - * @param {Int} id - * @returns {Category|Int} The course or the courseid given if there is no matching category. - */ - get_course_by_id : function(id) { - var i, course, courses = this.get('courses'), length = courses.length; - for (i = 0; i < length; i++) { - course = courses[i]; - if (course.get('courseid') === id) { - return course; - } - } return false; }, /** - * Removes the course with the given ID. - * @method remove_course_by_id - * @param {Int} id + * Returns the course with the given id. + * @method getCourseById + * @param {Number} id + * @returns {Course|Boolean} The course or false if not found/ */ - remove_course_by_id : function() { + getCourseById : function(id) { + var i, + course, + courses = this.get('courses'), + length = courses.length; + for (i = 0; i < length; i++) { + if (!courses.hadOwnPropery(i)) { + course = courses[i]; + if (course.get('courseid') === id) { + return course; + } + } + } + return id; + }, + + /** + * Removes the course with the given ID. + * @method removeCourseById + * @param {Number} id + */ + removeCourseById : function() { var courses = this.get('courses'), + length = courses.length, + course, i; for (i = 0; i < length; i++) { course = courses[i]; @@ -370,13 +410,13 @@ Console.prototype = { /** * Performs an AJAX action. * - * @method perform_ajax_action + * @method performAjaxAction * @param {String} action The action to perform. * @param {Object} args The arguments to pass through with teh request. * @param {Function} callback The function to call when all is done. * @param {Object} context The object to use as the context for the callback. */ - perform_ajax_action : function(action, args, callback, context) { + performAjaxAction : function(action, args, callback, context) { var io = new Y.IO(); args.action = action; args.ajax = '1'; @@ -394,25 +434,21 @@ Console.prototype = { }; Y.extend(Console, Y.Base, Console.prototype); -/** - * Course namespace. - * @static - * @namespace M - * @class course - */ M.course = M.course || {}; +M.course.management = M.course.management || {}; +M.course.management.console = null; /** * Initalises the course management console. * @static * @param {Object} config */ -M.course.init_management = function(config) { - M.course.console = new Console(config); +M.course.management.init = function(config) { + M.course.management.console = new Console(config); };/** * Drag and Drop handler * - * @namespace M.core_course.management + * @namespace M.course.management * @class DragDrop * @constructor * @extends Y.Base @@ -650,19 +686,20 @@ DragDrop.prototype = { previousid; if (!drag.test('.listitem-course')) { - Y.log('Wasn\'t what I expected', 'warn', 'core_course'); + Y.log('Wasn\'t what I expected', 'warn', 'moodle-course-management'); alert(drag); return false; } courseid = drag.getData('id'); - course = console.get_course_by_id(courseid); - if (iscategory) { categoryid = drop.ancestor('.listitem-category').getData('id'); Y.log('Course ' + courseid + ' dragged into category ' + categoryid); - category = console.get_category_by_id(categoryid); + category = console.getCategoryById(categoryid); if (category) { - category.moveCourseTo(course); + course = console.getCourseById(courseid); + if (course) { + category.moveCourseTo(course); + } } } else if (iscourse || drop.ancestor('#course-listing')) { previoussibling = drag.get('previousSibling'); @@ -672,14 +709,14 @@ DragDrop.prototype = { course.moveAfter(aftercourseid, previousid); } } else { - Y.log('Course dropped over unhandled target.', 'info', 'core_course'); + Y.log('Course dropped over unhandled target.', 'info', 'moodle-course-management'); } } }; Y.extend(DragDrop, Y.Base, DragDrop.prototype);/** * A managed course. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Item * @constructor * @extends Y.Base @@ -728,20 +765,20 @@ Item.prototype = { /** * Checks and parses an AJAX response for an item. * - * @method check_ajax_response + * @method checkAjaxResponse * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Object|Boolean} */ - check_ajax_response : function(transactionid, response, args) { + checkAjaxResponse : function(transactionid, response, args) { if (response.status !== 200) { - Y.log('Error: AJAX response resulted in non 200 status.', 'error', 'Item.check_ajax_response'); + Y.log('Error: AJAX response resulted in non 200 status.', 'error', 'Item.checkAjaxResponse'); return false; } if (transactionid === null || args === null) { - Y.log('Error: Invalid AJAX response details provided.', 'error', 'Item.check_ajax_response'); + Y.log('Error: Invalid AJAX response details provided.', 'error', 'Item.checkAjaxResponse'); return false; } var outcome = Y.JSON.parse(response.responseText); @@ -758,7 +795,7 @@ Item.prototype = { * Moves an item up by one. * * @method moveup - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} @@ -771,9 +808,9 @@ Item.prototype = { previousup, previousdown, tmpnode, - outcome = this.check_ajax_response(transactionid, response, args); + outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to move '+this.get('itemname')+' up failed by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to move '+this.get('itemname')+' up failed by outcome.', 'warn', 'moodle-course-management'); return false; } node = this.get('node'); @@ -802,9 +839,9 @@ Item.prototype = { } } this.updated(true); - Y.log('Success: '+this.get('itemname')+' moved up by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' moved up by AJAX.', 'info', 'moodle-course-management'); } else { - Y.log(this.get('itemname')+' cannot be moved up as its the top item', 'warn', 'core_course'); + Y.log(this.get('itemname')+' cannot be moved up as its the top item', 'warn', 'moodle-course-management'); } }, @@ -812,7 +849,7 @@ Item.prototype = { * Moves an item down by one. * * @method movedown - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} @@ -825,9 +862,9 @@ Item.prototype = { nextup, nextdown, tmpnode, - outcome = this.check_ajax_response(transactionid, response, args); + outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to move '+this.get('itemname')+' down failed by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to move '+this.get('itemname')+' down failed by outcome.', 'warn', 'moodle-course-management'); return false; } node = this.get('node'); @@ -856,9 +893,9 @@ Item.prototype = { } } this.updated(true); - Y.log('Success: '+this.get('itemname')+' moved down by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' moved down by AJAX.', 'info', 'moodle-course-management'); } else { - Y.log(this.get('itemname')+' cannot be moved down as its the last item', 'warn', 'core_course'); + Y.log(this.get('itemname')+' cannot be moved down as its the last item', 'warn', 'moodle-course-management'); } }, @@ -866,21 +903,21 @@ Item.prototype = { * Makes an item visible. * * @method show - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ show : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } this.markVisible(); this.updated(); - Y.log('Success: '+this.get('itemname')+' made visible by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' made visible by AJAX.', 'info', 'moodle-course-management'); }, /** @@ -889,7 +926,7 @@ Item.prototype = { */ markVisible : function() { this.get('node').setAttribute('data-visible', '1'); - Y.log('Marked '+this.get('itemname')+' as visible', 'note', 'core_course'); + Y.log('Marked '+this.get('itemname')+' as visible', 'info', 'moodle-course-management'); return true; }, @@ -897,20 +934,20 @@ Item.prototype = { * Hides an item. * * @method hide - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ hide : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } this.markHidden(); this.updated(); - Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'moodle-course-management'); }, /** @@ -919,7 +956,7 @@ Item.prototype = { */ markHidden : function() { this.get('node').setAttribute('data-visible', '0'); - Y.log('Marked '+this.get('itemname')+' as hidden', 'note', 'core_course'); + Y.log('Marked '+this.get('itemname')+' as hidden', 'info', 'moodle-course-management'); return true; }, @@ -955,7 +992,7 @@ Item.prototype = { Y.extend(Item, Y.Base, Item.prototype);/** * A managed category. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Category * @constructor * @extends Item @@ -969,9 +1006,21 @@ Category.ATTRS = { /** * The category ID relating to this category. * @attribute categoryid - * @type Int + * @type Number + * @writeOnce + * @default null */ - categoryid : {}, + categoryid : { + getter : function (value, name) { + if (value === null) { + value = this.get('node').getData('id'); + this.set(name, value); + } + return value; + }, + value : null, + writeOnce : true + }, /** * True if this category is the currently selected category. @@ -994,19 +1043,14 @@ Category.ATTRS = { }, /** - * An array fo courses belonging to this category + * An array of courses belonging to this category. * @attribute courses * @type Course[] - * @default [] + * @default Array */ courses : { - setter : function(item, name) { - if (Y.Lang.isArray(item)) { - return item; - } - var items = this.get(name); - items.push(item); - return items; + validator : function(val) { + return Y.Lang.isArray(val); }, value : [] } @@ -1017,8 +1061,6 @@ Category.prototype = { * @method initializer */ initializer : function() { - var node = this.get('node'); - this.set('categoryid', node.getData('id')); this.set('itemname', 'category'); }, @@ -1033,11 +1075,13 @@ Category.prototype = { /** * Registers a course as belonging to this category. - * @method register_course + * @method registerCourse * @param {Course} course */ - register_course : function(course) { - this.set('courses', course); + registerCourse : function(course) { + var courses = this.get('courses'); + courses.push(course); + this.set('courses', courses); }, /** @@ -1052,35 +1096,35 @@ Category.prototype = { var catarg = {categoryid : this.get('categoryid')}; switch (action) { case 'moveup': - e.halt(); - this.get('console').perform_ajax_action('movecategoryup', catarg, this.moveup, this); + e.preventDefault(); + this.get('console').performAjaxAction('movecategoryup', catarg, this.moveup, this); break; case 'movedown': - e.halt(); - this.get('console').perform_ajax_action('movecategorydown', catarg, this.movedown, this); + e.preventDefault(); + this.get('console').performAjaxAction('movecategorydown', catarg, this.movedown, this); break; case 'show': - e.halt(); - this.get('console').perform_ajax_action('showcategory', catarg, this.show, this); + e.preventDefault(); + this.get('console').performAjaxAction('showcategory', catarg, this.show, this); break; case 'hide': - e.halt(); - this.get('console').perform_ajax_action('hidecategory', catarg, this.hide, this); + e.preventDefault(); + this.get('console').performAjaxAction('hidecategory', catarg, this.hide, this); break; case 'expand': - e.halt(); + e.preventDefault(); if (this.get('node').getData('expanded') === '0') { - this.get('node').setData('expanded', true); - this.get('console').perform_ajax_action('getsubcategorieshtml', catarg, this.loadSubcategories, this); + this.get('node').setAttribute('data-expanded', '1').setData('expanded', 'true'); + this.get('console').performAjaxAction('getsubcategorieshtml', catarg, this.loadSubcategories, this); } this.expand(); break; case 'collapse': - e.halt(); + e.preventDefault(); this.collapse(); break; default: - Y.log('Invalid AJAX action requested of managed category.', 'warn', 'core_course'); + Y.log('Invalid AJAX action requested of managed category.', 'warn', 'moodle-course-management'); return false; } }, @@ -1093,8 +1137,7 @@ Category.prototype = { var node = this.get('node'), action = node.one('a[data-action=expand]'); node.removeClass('collapsed'); - action.setAttribute('data-action', 'collapse'); - action.one('img').setAttrs({ + action.setAttribute('data-action', 'collapse').one('img').setAttrs({ src : M.util.image_url('t/switch_minus', 'moodle'), title : M.util.get_string('collapse', 'moodle'), alt : M.util.get_string('collapse', 'moodle') @@ -1109,8 +1152,7 @@ Category.prototype = { var node = this.get('node'), action = node.one('a[data-action=collapse]'); node.addClass('collapsed'); - action.setAttribute('data-action', 'expand'); - action.one('img').setAttrs({ + action.setAttribute('data-action', 'expand').one('img').setAttrs({ src : M.util.image_url('t/switch_plus', 'moodle'), title : M.util.get_string('expand', 'moodle'), alt : M.util.get_string('expand', 'moodle') @@ -1122,25 +1164,26 @@ Category.prototype = { * * @method loadSubcategories * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. - * @returns {Boolean} + * @return {Boolean} Returns true on success - false otherwise. */ loadSubcategories : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), node = this.get('node'), console = this.get('console'); if (outcome === false) { - Y.log('AJAX failed to load sub categories for '+this.get('itemname'), 'warn', 'core_course'); + Y.log('AJAX failed to load sub categories for '+this.get('itemname'), 'warn', 'moodle-course-management'); return false; } - Y.log('AJAX loaded subcategories for '+this.get('itemname'), 'info', 'core_course'); + Y.log('AJAX loaded subcategories for '+this.get('itemname'), 'info', 'moodle-course-management'); node.append(outcome.html); - console.initialise_categories(node); + console.initialiseCategories(node); if (M.core && M.core.actionmenu && M.core.actionmenu.newDOMNode) { M.core.actionmenu.newDOMNode(node); } + return true; }, /** @@ -1164,7 +1207,7 @@ Category.prototype = { confirm.on('complete-yes', function() { confirm.hide(); confirm.destroy(); - this.get('console').perform_ajax_action('movecourseintocategory', { + this.get('console').performAjaxAction('movecourseintocategory', { categoryid : this.get('categoryid'), courseid : course.get('courseid') }, this.completeMoveCourse, this); @@ -1177,20 +1220,24 @@ Category.prototype = { * Completes moving a course to this category. * @method completeMoveCourse * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ completeMoveCourse : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), course; if (outcome === false) { - Y.log('AJAX failed to move courses into this category: '+this.get('itemname'), 'warn', 'core_course'); + Y.log('AJAX failed to move courses into this category: '+this.get('itemname'), 'warn', 'moodle-course-management'); return false; } - course = this.get('console').get_course_by_id(args.courseid); - Y.log('Moved the course ('+course.getName()+') into this category ('+this.getName()+')', 'info', 'core_course'); + course = this.get('console').getCourseById(args.courseid); + if (!course) { + Y.log('Course was moved but the course listing could not be found to reflect this', 'warn', 'moodle-course-management'); + return false; + } + Y.log('Moved the course ('+course.getName()+') into this category ('+this.getName()+')', 'info', 'moodle-course-management'); this.highlight(); if (course) { course.remove(); @@ -1202,15 +1249,15 @@ Category.prototype = { * Makes an item visible. * * @method show - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ show : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } @@ -1222,22 +1269,22 @@ Category.prototype = { this.updateCourseVisiblity(outcome.coursevisibility); } this.updated(); - Y.log('Success: category made visible by AJAX.', 'info', 'core_course'); + Y.log('Success: category made visible by AJAX.', 'info', 'moodle-course-management'); }, /** * Hides an item. * * @method hide - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ hide : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } this.markHidden(); @@ -1248,12 +1295,13 @@ Category.prototype = { this.updateCourseVisiblity(outcome.coursevisibility); } this.updated(); - Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'moodle-course-management'); }, /** * Updates the visibility of child courses if required. * @method updateCourseVisiblity + * @chainable * @param courses */ updateCourseVisiblity : function(courses) { @@ -1262,23 +1310,27 @@ Category.prototype = { course; try { for (key in courses) { - course = console.get_course_by_id(courses[key].id); - if (course.get) { - if (courses[key].show === "1") { - course.markVisible(); - } else { - course.markHidden(); + if (courses.hasOwnProperty[key]) { + course = console.getCourseById(courses[key].id); + if (course) { + if (courses[key].show === "1") { + course.markVisible(); + } else { + course.markHidden(); + } } } } } catch (err) { - Y.log('Error trying to update course visibility: ' + err.message, 'warn', 'core_course'); + Y.log('Error trying to update course visibility: ' + err.message, 'warn', 'moodle-course-management'); } + return this; }, /** * Updates the visibility of subcategories if required. * @method updateChildVisibility + * @chainable * @param categories */ updateChildVisibility : function(categories) { @@ -1287,24 +1339,27 @@ Category.prototype = { category; try { for (key in categories) { - category = console.get_category_by_id(categories[key].id); - if (category.get) { - if (categories[key].show === "1") { - category.markVisible(); - } else { - category.markHidden(); + if (categories.hasOwnProperty[key]) { + category = console.getCategoryById(categories[key].id); + if (category) { + if (categories[key].show === "1") { + category.markVisible(); + } else { + category.markHidden(); + } } } } } catch (err) { - Y.log('Error trying to update category visibility: ' + err.message, 'warn', 'core_course'); + Y.log('Error trying to update category visibility: ' + err.message, 'warn', 'moodle-course-management'); } + return this; } }; Y.extend(Category, Item, Category.prototype);/** * A managed course. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Course * @constructor * @extends Item @@ -1319,7 +1374,7 @@ Course.ATTRS = { /** * The course ID of this course. * @attribute courseid - * @type Int + * @type Number */ courseid : {}, @@ -1371,8 +1426,8 @@ Course.prototype = { var node = this.get('node'), category = this.get('category'); this.set('courseid', node.getData('id')); - if (category && category.register_course) { - category.register_course(this); + if (category && category.registerCourse) { + category.registerCourse(this); } this.set('itemname', 'course'); }, @@ -1399,22 +1454,22 @@ Course.prototype = { switch (action) { case 'moveup': e.halt(); - console.perform_ajax_action('movecourseup', args, this.moveup, this); + console.performAjaxAction('movecourseup', args, this.moveup, this); break; case 'movedown': e.halt(); - console.perform_ajax_action('movecoursedown', args, this.movedown, this); + console.performAjaxAction('movecoursedown', args, this.movedown, this); break; case 'show': e.halt(); - console.perform_ajax_action('showcourse', args, this.show, this); + console.performAjaxAction('showcourse', args, this.show, this); break; case 'hide': e.halt(); - console.perform_ajax_action('hidecourse', args, this.hide, this); + console.performAjaxAction('hidecourse', args, this.hide, this); break; default: - Y.log('Invalid AJAX action requested of managed course.', 'warn', 'core_course'); + Y.log('Invalid AJAX action requested of managed course.', 'warn', 'moodle-course-management'); return false; } }, @@ -1424,7 +1479,7 @@ Course.prototype = { * @method remove */ remove : function() { - this.get('console').remove_course_by_id(this.get('courseid')); + this.get('console').removeCourseById(this.get('courseid')); this.get('node').remove(); }, @@ -1432,8 +1487,8 @@ Course.prototype = { * Moves this course after another course. * * @method moveAfter - * @param {Int} moveaftercourse The course to move after or 0 to put it at the top. - * @param {Int} previousid the course it was previously after in case we need to revert. + * @param {Number} moveaftercourse The course to move after or 0 to put it at the top. + * @param {Number} previousid the course it was previously after in case we need to revert. */ moveAfter : function(moveaftercourse, previousid) { var console = this.get('console'), @@ -1442,7 +1497,7 @@ Course.prototype = { moveafter : moveaftercourse, previous : previousid }; - console.perform_ajax_action('movecourseafter', args, this.moveAfterResponse, this); + console.performAjaxAction('movecourseafter', args, this.moveAfterResponse, this); }, /** @@ -1450,18 +1505,18 @@ Course.prototype = { * * @method moveAfterResponse * @protected - * @param {Int} transactionid The transaction ID for the request. + * @param {Number} transactionid The transaction ID for the request. * @param {Object} response The response to the request. * @param {Objects} args The arguments that were given with the request. * @returns {Boolean} */ moveAfterResponse : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), node = this.get('node'), previous; if (outcome === false) { previous = node.ancestor('ul').one('li[data-id='+args.previous+']'); - Y.log('AJAX failed to move this course after the requested course', 'warn', 'core_course'); + Y.log('AJAX failed to move this course after the requested course', 'warn', 'moodle-course-management'); if (previous) { // After the last previous. previous.insertAfter(node, 'after'); @@ -1471,7 +1526,7 @@ Course.prototype = { } return false; } - Y.log('AJAX successfully moved course ('+this.getName()+')', 'info', 'core_course'); + Y.log('AJAX successfully moved course ('+this.getName()+')', 'info', 'moodle-course-management'); this.highlight(); } }; diff --git a/course/yui/build/moodle-course-management/moodle-course-management-min.js b/course/yui/build/moodle-course-management/moodle-course-management-min.js index a610f24ea4e..a5405d82956 100644 --- a/course/yui/build/moodle-course-management/moodle-course-management-min.js +++ b/course/yui/build/moodle-course-management/moodle-course-management-min.js @@ -1,3 +1,3 @@ -YUI.add("moodle-course-management",function(e,t){function n(){n.superclass.constructor.apply(this,arguments)}function r(e){n.superclass.constructor.apply(this,[e])}function i(){i.superclass.constructor.apply(this,arguments)}function s(){s.superclass.constructor.apply(this,arguments)}function o(){o.superclass.constructor.apply(this,arguments)}n.NAME="moodle-course-management",n.CSS_PREFIX="management",n.ATTRS={element:{setter:function(t){return typeof t=="string"&&(t=e.one("#"+t)),t}},categorylisting:{},courselisting:{},coursedetails:{value:null},activecategoryid:{},activecourseid:{},categories:{setter:function(t,n){if(e.Lang.isArray(t))return t;var r=this.get(n);return r.push(t),r},value:[]},courses:{setter:function(t,n){if(e.Lang.isArray(t))return t;var r=this.get(n);return r.push(t),r},value:[]},page:{getter:function(e,t){return e===null&&(e=this.get("element").getData(t),this.set(t,e)),e},value:null},totalpages:{getter:function(e,t){return e===null&&(e=this.get("element").getData(t),this.set(t,e)),e},value:null},totalcourses:{getter:function(e,t){return e===null&&(e=this.get("element").getData(t),this.set(t,e)),e},value:null},ajaxurl:{getter:function(e){return e===null&&(e=M.cfg.wwwroot+"/course/ajax/management.php"),e},value:null},dragdrop:{value:null}},n.prototype={categoriesinit:!1,initializer:function(){this.set("element","coursecat-management");var e=this.get("element"),t=e.one("#category-listing"),n=e.one("#course-listing"),i=null,s=null;t&&(i=t.one('.listitem[data-selected="1"]')),n&&(s=n.one('.listitem[data-selected="1"]')),this.set("categorylisting",t),this.set("courselisting",n),this.set("coursedetails",e.one("#course-detail")),i&&this.set("activecategoryid",i.getData("id")),s&&this.set("activecourseid",s.getData("id")),this.initialise_categories(t),this.initialise_courses(),n&&this.set("dragdrop",new r({console:this}))},initialise_categories:function(e){var t=0;if(!e)return!1;e.all(".listitem[data-id]").each(function(e){this.set("categories",new s({node:e,console:this})),t++},this),this.categoriesinit||(this.get("categorylisting").delegate("click",this.handle_category_delegation,"a[data-action]",this),this.categoriesinit=!0)},initialise_courses:function(){var e=this.get_category_by_id(this.get("activecategoryid")),t=this.get("courselisting"),n=0;if(!t)return!1;t.all(".listitem[data-id]").each(function(t){this.set("courses",new o({node:t,console:this,category:e})),n++},this),t.delegate("click",this.handle_course_delegation,"a[data-action]",this)},handle_course_delegation:function(e){var t=e.currentTarget,n=t.getData("action"),r=t.ancestor(".listitem").getData("id"),i=this.get_course_by_id(r);i.handle(n,e)},handle_category_delegation:function(e){var t=e.currentTarget,n=t.getData("action"),r=t.ancestor(".listitem").getData("id"),i=this.get_category_by_id(r);i.handle(n,e)},get_category_by_id:function(e){var t,n,r=this.get("categories"),i=r.length;for(t=0;t .course-listing"),s=r?r.one("ul.ml"):null,o=i?i.one("ul.ml"):null,u=i?i.getData("canmoveoutof"):!1,a=u?n:o;if(!o)return!1;o.all("> li").each(function(e){this.initCourseListing(e,a)},this),o.setData("dd",new e.DD.Drop({node:o})),u&&s&&s.all("li > div").each(function(e){this.initCategoryListitem(e)},this),e.DD.DDM.on("drag:start",this.dragStart,this),e.DD.DDM.on("drag:end",this.dragEnd,this),e.DD.DDM.on("drag:drag",this.dragDrag,this),e.DD.DDM.on("drop:over",this.dropOver,this),e.DD.DDM.on("drop:enter",this.dropEnter,this),e.DD.DDM.on("drop:exit",this.dropExit,this),e.DD.DDM.on("drop:hit",this.dropHit,this)},initCourseListing:function(t,n){t.setData("dd",(new e.DD.Drag({node:t,target:{padding:"0 0 0 20"}})).addHandle(".drag-handle").plug(e.Plugin.DDProxy,{moveOnEnd:!1,borderStyle:!1}).plug(e.Plugin.DDConstrained,{constrain2node:n}))},initCategoryListitem:function(t){t.setData("dd",new e.DD.Drop({node:t}))},dragStart:function(e){var t=e.target,n=t.get("node"),r=t.get("dragNode");n.addClass("course-being-dragged"),r.addClass("course-being-dragged-proxy").set("innerHTML",n.one("a.coursename").get("innerHTML")),this.previoussibling=n.get("previousSibling")},dragEnd:function(e){var t=e.target,n=t.get("node");n.removeClass("course-being-dragged"),this.get("console").get("element").all("#category-listing li.highlight").removeClass("highlight")},dragDrag:function(e){var t=e.target.lastXY[1];t div a.action-moveup"),o=i.one(" > div a.action-movedown");if(!a||!o)s=i.one(" > div a.action-moveup"),f=u.one(" > div a.action-movedown"),!a&&!o?(l=e.Node.create(' '),f.replace(l),s.replace(f),l.replace(s),l.destroy()):o||s.insert(f,"after");this.updated(!0)}},movedown:function(t,n,r){var i,s,o,u,a,f,l,c=this.check_ajax_response(t,n,r);if(c===!1)return!1;i=this.get("node"),s=i.next(".listitem");if(s){i.insert(s,"before"),f=s.one(" > div a.action-movedown"),o=i.one(" > div a.action-moveup");if(!f||!o)a=s.one(" > div a.action-moveup"),u=i.one(" > div a.action-movedown"),!f&&!o?(l=e.Node.create(' '),a.replace(l),u.replace(a),l.replace(u),l.destroy()):o||u.insert(a,"before");this.updated(!0)}},show:function(e,t,n){var r=this.check_ajax_response(e,t,n);if(r===!1)return!1;this.markVisible(),this.updated()},markVisible:function(){return this.get("node").setAttribute("data-visible","1"),!0},hide:function(e,t,n){var r=this.check_ajax_response(e,t,n);if(r===!1)return!1;this.markHidden(),this.updated()},markHidden:function(){return this.get("node").setAttribute("data-visible","0"),!0},updated:function(e){e&&this.highlight()},highlight:function(){var e=this.get("node");e.siblings(".highlight").removeClass("highlight"),e.addClass("highlight"),this.highlighttimeout&&window.clearTimeout(this.highlighttimeout),this.highlighttimeout=window.setTimeout(function(){e.removeClass("highlight")},2500)}},e.extend(i,e.Base,i.prototype),s.NAME="moodle-course-management-category",s.CSS_PREFIX="management-category",s.ATTRS={categoryid:{},selected:{getter:function(e,t){return e===null&&(e=this.get("node").getData(t),e===null&&(e=!1),this.set(t,e)),e},value:null},courses:{setter:function(t,n){if(e.Lang.isArray(t))return t;var r=this.get(n);return r.push(t),r},value:[]}},s.prototype={initializer:function(){var e=this.get("node");this.set("categoryid",e.getData("id")),this.set("itemname","category")},getName:function(){return this.get("node").one("a.categoryname").get("innerHTML")},register_course:function(e){this.set("courses",e)},handle:function(e,t){var n={categoryid:this.get("categoryid")};switch(e){case"moveup":t.halt(),this.get("console").perform_ajax_action("movecategoryup",n,this.moveup,this);break;case"movedown":t.halt(),this.get("console").perform_ajax_action("movecategorydown",n,this.movedown,this);break;case"show":t.halt(),this.get("console").perform_ajax_action("showcategory",n,this.show,this);break;case"hide":t.halt(),this.get("console").perform_ajax_action("hidecategory",n,this.hide,this);break;case"expand":t.halt(),this.get("node").getData("expanded")==="0"&&(this.get("node").setData("expanded",!0),this.get("console").perform_ajax_action("getsubcategorieshtml",n,this.loadSubcategories,this)),this.expand();break;case"collapse":t.halt(),this.collapse();break;default:return!1}},expand:function(){var e=this.get("node"),t=e.one("a[data-action=expand]");e.removeClass("collapsed"),t.setAttribute("data-action","collapse"),t.one("img").setAttrs({src:M.util.image_url("t/switch_minus","moodle"),title:M.util.get_string("collapse","moodle"),alt:M.util.get_string("collapse","moodle")})},collapse:function(){var e=this.get("node"),t=e.one("a[data-action=collapse]");e.addClass("collapsed"),t.setAttribute("data-action","expand"),t.one("img").setAttrs({src:M.util.image_url("t/switch_plus","moodle"),title:M.util.get_string("expand","moodle"),alt:M.util.get_string("expand","moodle")})},loadSubcategories:function(e,t,n){var r=this.check_ajax_response(e,t,n),i=this.get("node"),s=this.get("console");if(r===!1)return!1;i.append(r.html),s.initialise_categories(i),M.core&&M.core.actionmenu&&M.core.actionmenu.newDOMNode&&M.core.actionmenu.newDOMNode(i)},moveCourseTo:function(t){var n=this;e.use("moodle-core-notification-confirm",function(){var e=new M.core.confirm({title:M.util.get_string("confirm","moodle"),question:M.util.get_string("confirmcoursemove","moodle",{course:t.getName(),category:n.getName()}),yesLabel:M.util.get_string("yes","moodle"),noLabel:M.util.get_string("no","moodle")});e.on("complete-yes",function(){e.hide(),e.destroy(),this.get("console").perform_ajax_action("movecourseintocategory",{categoryid:this.get("categoryid"),courseid:t.get("courseid")},this.completeMoveCourse,this)},n),e.show()})},completeMoveCourse:function(e,t,n){var r=this.check_ajax_response(e,t,n),i;return r===!1?!1:(i=this.get("console").get_course_by_id(n.courseid),this.highlight(),i&&i.remove(),!0)},show:function(e,t,n){var r=this.check_ajax_response(e,t,n);if(r===!1)return!1;this.markVisible(),r.categoryvisibility&&this.updateChildVisibility(r.categoryvisibility),r.coursevisibility&&this.updateCourseVisiblity(r.coursevisibility),this.updated()},hide:function(e,t,n){var r=this.check_ajax_response(e,t,n);if(r===!1)return!1;this.markHidden(),r.categoryvisibility&&this.updateChildVisibility(r.categoryvisibility),r.coursevisibility&&this -.updateCourseVisiblity(r.coursevisibility),this.updated()},updateCourseVisiblity:function(e){var t=this.get("console"),n,r;try{for(n in e)r=t.get_course_by_id(e[n].id),r.get&&(e[n].show==="1"?r.markVisible():r.markHidden())}catch(i){}},updateChildVisibility:function(e){var t=this.get("console"),n,r;try{for(n in e)r=t.get_category_by_id(e[n].id),r.get&&(e[n].show==="1"?r.markVisible():r.markHidden())}catch(i){}}},e.extend(s,i,s.prototype),o.NAME="moodle-course-management-course",o.CSS_PREFIX="management-course",o.ATTRS={courseid:{},selected:{getter:function(e,t){return e===null&&(e=this.get("node").getData(t),this.set(t,e)),e},value:null},node:{},console:{writeOnce:"initOnly"},category:{writeOnce:"initOnly"}},o.prototype={initializer:function(){var e=this.get("node"),t=this.get("category");this.set("courseid",e.getData("id")),t&&t.register_course&&t.register_course(this),this.set("itemname","course")},getName:function(){return this.get("node").one("a.coursename").get("innerHTML")},handle:function(e,t){var n=this.get("console"),r={courseid:this.get("courseid")};switch(e){case"moveup":t.halt(),n.perform_ajax_action("movecourseup",r,this.moveup,this);break;case"movedown":t.halt(),n.perform_ajax_action("movecoursedown",r,this.movedown,this);break;case"show":t.halt(),n.perform_ajax_action("showcourse",r,this.show,this);break;case"hide":t.halt(),n.perform_ajax_action("hidecourse",r,this.hide,this);break;default:return!1}},remove:function(){this.get("console").remove_course_by_id(this.get("courseid")),this.get("node").remove()},moveAfter:function(e,t){var n=this.get("console"),r={courseid:this.get("courseid"),moveafter:e,previous:t};n.perform_ajax_action("movecourseafter",r,this.moveAfterResponse,this)},moveAfterResponse:function(e,t,n){var r=this.check_ajax_response(e,t,n),i=this.get("node"),s;if(r===!1)return s=i.ancestor("ul").one("li[data-id="+n.previous+"]"),s?s.insertAfter(i,"after"):i.ancestor("ul").one("li").insert(i,"before"),!1;this.highlight()}},e.extend(o,i,o.prototype)},"@VERSION@",{requires:["base","node","io-base","moodle-core-notification-exception","json-parse","dd-constrain","dd-proxy","dd-drop","dd-delegate","node-event-delegate"]}); +YUI.add("moodle-course-management",function(e,t){function n(){n.superclass.constructor.apply(this,arguments)}function r(e){n.superclass.constructor.apply(this,[e])}function i(){i.superclass.constructor.apply(this,arguments)}function s(){s.superclass.constructor.apply(this,arguments)}function o(){o.superclass.constructor.apply(this,arguments)}n.NAME="moodle-course-management",n.CSS_PREFIX="management",n.ATTRS={element:{setter:function(t){return typeof t=="string"&&(t=e.one("#"+t)),t}},categorylisting:{value:null},courselisting:{value:null},coursedetails:{value:null},activecategoryid:{value:null},activecourseid:{value:null},categories:{setter:function(t,n){if(e.Lang.isArray(t))return t;var r=this.get(n);return r.push(t),r},value:[]},courses:{validator:function(t){return e.Lang.isArray(t)},value:[]},page:{getter:function(e,t){return e===null&&(e=this.get("element").getData(t),this.set(t,e)),e},value:null},totalpages:{getter:function(e,t){return e===null&&(e=this.get("element").getData(t),this.set(t,e)),e},value:null},totalcourses:{getter:function(e,t){return e===null&&(e=this.get("element").getData(t),this.set(t,e)),e},value:null},ajaxurl:{getter:function(e){return e===null&&(e=M.cfg.wwwroot+"/course/ajax/management.php"),e},value:null},dragdrop:{value:null}},n.prototype={categoriesinit:!1,initializer:function(){this.set("element","coursecat-management");var e=this.get("element"),t=e.one("#category-listing"),n=e.one("#course-listing"),i=null,s=null;t&&(i=t.one('.listitem[data-selected="1"]')),n&&(s=n.one('.listitem[data-selected="1"]')),this.set("categorylisting",t),this.set("courselisting",n),this.set("coursedetails",e.one("#course-detail")),i&&this.set("activecategoryid",i.getData("id")),s&&this.set("activecourseid",s.getData("id")),this.initialiseCategories(t),this.initialiseCourses(),n&&this.set("dragdrop",new r({console:this}))},initialiseCategories:function(e){var t=0;if(!e)return!1;e.all(".listitem[data-id]").each(function(e){this.set("categories",new s({node:e,console:this})),t++},this),this.categoriesinit||(this.get("categorylisting").delegate("click",this.handleCategoryDelegation,"a[data-action]",this),this.categoriesinit=!0)},initialiseCourses:function(){var e=this.getCategoryById(this.get("activecategoryid")),t=this.get("courselisting"),n=0;if(!t)return!1;if(!e)return!1;t.all(".listitem[data-id]").each(function(t){this.registerCourse(new o({node:t,console:this,category:e})),n++},this),t.delegate("click",this.handleCourseDelegation,"a[data-action]",this)},registerCourse:function(e){var t=this.get("courses");t.push(e),this.set("courses",t)},handleCourseDelegation:function(e){var t=e.currentTarget,n=t.getData("action"),r=t.ancestor(".listitem").getData("id"),i=this.getCourseById(r);i&&i.handle(n,e)},handleCategoryDelegation:function(e){var t=e.currentTarget,n=t.getData("action"),r=t.ancestor(".listitem").getData("id"),i=this.getCategoryById(r);i&&i.handle(n,e)},getCategoryById:function(e){var t,n,r=this.get("categories"),i=r.length;for(t=0;t .course-listing"),s=r?r.one("ul.ml"):null,o=i?i.one("ul.ml"):null,u=i?i.getData("canmoveoutof"):!1,a=u?n:o;if(!o)return!1;o.all("> li").each(function(e){this.initCourseListing(e,a)},this),o.setData("dd",new e.DD.Drop({node:o})),u&&s&&s.all("li > div").each(function(e){this.initCategoryListitem(e)},this),e.DD.DDM.on("drag:start",this.dragStart,this),e.DD.DDM.on("drag:end",this.dragEnd,this),e.DD.DDM.on("drag:drag",this.dragDrag,this),e.DD.DDM.on("drop:over",this.dropOver,this),e.DD.DDM.on("drop:enter",this.dropEnter,this),e.DD.DDM.on("drop:exit",this.dropExit,this),e.DD.DDM.on("drop:hit",this.dropHit,this)},initCourseListing:function(t,n){t.setData("dd",(new e.DD.Drag({node:t,target:{padding:"0 0 0 20"}})).addHandle(".drag-handle").plug(e.Plugin.DDProxy,{moveOnEnd:!1,borderStyle:!1}).plug(e.Plugin.DDConstrained,{constrain2node:n}))},initCategoryListitem:function(t){t.setData("dd",new e.DD.Drop({node:t}))},dragStart:function(e){var t=e.target,n=t.get("node"),r=t.get("dragNode");n.addClass("course-being-dragged"),r.addClass("course-being-dragged-proxy").set("innerHTML",n.one("a.coursename").get("innerHTML")),this.previoussibling=n.get("previousSibling")},dragEnd:function(e){var t=e.target,n=t.get("node");n.removeClass("course-being-dragged"),this.get("console").get("element").all("#category-listing li.highlight").removeClass("highlight")},dragDrag:function(e){var t=e.target.lastXY[1];t div a.action-moveup"),o=i.one(" > div a.action-movedown");if(!a||!o)s=i.one(" > div a.action-moveup"),f=u.one(" > div a.action-movedown"),!a&&!o?(l=e.Node.create(' '),f.replace(l),s.replace(f),l.replace(s),l.destroy()):o||s.insert(f,"after");this.updated(!0)}},movedown:function(t,n,r){var i,s,o,u,a,f,l,c=this.checkAjaxResponse(t,n,r);if(c===!1)return!1;i=this.get("node"),s=i.next(".listitem");if(s){i.insert(s,"before"),f=s.one(" > div a.action-movedown"),o=i.one(" > div a.action-moveup");if(!f||!o)a=s.one(" > div a.action-moveup"),u=i.one(" > div a.action-movedown"),!f&&!o?(l=e.Node.create(' '),a.replace(l),u.replace(a),l.replace(u),l.destroy()):o||u.insert(a,"before");this.updated(!0)}},show:function(e,t,n){var r=this.checkAjaxResponse(e,t,n);if(r===!1)return!1;this.markVisible(),this.updated()},markVisible:function(){return this.get("node").setAttribute("data-visible","1"),!0},hide:function(e,t,n){var r=this.checkAjaxResponse(e,t,n);if(r===!1)return!1;this.markHidden(),this.updated()},markHidden:function(){return this.get("node").setAttribute("data-visible","0"),!0},updated:function(e){e&&this.highlight()},highlight:function(){var e=this.get("node");e.siblings(".highlight").removeClass("highlight"),e.addClass("highlight"),this.highlighttimeout&&window.clearTimeout(this.highlighttimeout),this.highlighttimeout=window.setTimeout(function(){e.removeClass("highlight")},2500)}},e.extend(i,e.Base,i.prototype),s.NAME="moodle-course-management-category",s.CSS_PREFIX="management-category",s.ATTRS={categoryid:{getter:function(e,t){return e===null&&(e=this.get("node").getData("id"),this.set(t,e)),e},value:null,writeOnce:!0},selected:{getter:function(e,t){return e===null&&(e=this.get("node").getData(t),e===null&&(e=!1),this.set(t,e)),e},value:null},courses:{validator:function(t){return e.Lang.isArray(t)},value:[]}},s.prototype={initializer:function(){this.set("itemname","category")},getName:function(){return this.get("node").one("a.categoryname").get("innerHTML")},registerCourse:function(e){var t=this.get("courses");t.push(e),this.set("courses",t)},handle:function(e,t){var n={categoryid:this.get("categoryid")};switch(e){case"moveup":t.preventDefault(),this.get("console").performAjaxAction("movecategoryup",n,this.moveup,this);break;case"movedown":t.preventDefault(),this.get("console").performAjaxAction("movecategorydown",n,this.movedown,this);break;case"show":t.preventDefault(),this.get("console").performAjaxAction("showcategory",n,this.show,this);break;case"hide":t.preventDefault(),this.get("console").performAjaxAction("hidecategory",n,this.hide,this);break;case"expand":t.preventDefault(),this.get("node").getData("expanded")==="0"&&(this.get("node").setAttribute("data-expanded","1").setData("expanded","true"),this.get("console").performAjaxAction("getsubcategorieshtml",n,this.loadSubcategories,this)),this.expand();break;case"collapse":t.preventDefault(),this.collapse();break;default:return!1}},expand:function(){var e=this.get("node"),t=e.one("a[data-action=expand]");e.removeClass("collapsed"),t.setAttribute("data-action","collapse").one("img").setAttrs({src:M.util.image_url("t/switch_minus","moodle"),title:M.util.get_string("collapse","moodle"),alt:M.util.get_string("collapse","moodle")})},collapse:function(){var e=this.get("node"),t=e.one("a[data-action=collapse]");e.addClass("collapsed"),t.setAttribute("data-action","expand").one("img").setAttrs({src:M.util.image_url("t/switch_plus","moodle"),title:M.util.get_string("expand","moodle"),alt:M.util.get_string("expand","moodle")})},loadSubcategories:function(e,t,n){var r=this.checkAjaxResponse(e,t,n),i=this.get("node"),s=this.get("console");return r===!1?!1:(i.append(r.html),s.initialiseCategories(i),M.core&&M.core.actionmenu&&M.core.actionmenu.newDOMNode&&M.core.actionmenu.newDOMNode(i),!0)},moveCourseTo:function(t){var n=this;e.use("moodle-core-notification-confirm",function(){var e=new M.core.confirm({title:M.util.get_string("confirm","moodle"),question:M.util.get_string("confirmcoursemove","moodle",{course:t.getName(),category:n.getName()}),yesLabel:M.util.get_string("yes","moodle"),noLabel:M.util.get_string("no","moodle")});e.on("complete-yes",function(){e.hide(),e.destroy(),this.get("console").performAjaxAction("movecourseintocategory",{categoryid:this.get("categoryid"),courseid:t.get("courseid")},this.completeMoveCourse,this)},n),e.show()})},completeMoveCourse:function(e,t,n){var r=this.checkAjaxResponse(e,t,n),i;return r===!1?!1:(i=this.get("console").getCourseById(n.courseid),i?(this.highlight(),i&&i.remove(),!0):!1)},show:function(e,t,n){var r=this.checkAjaxResponse(e,t,n);if(r===!1)return!1;this.markVisible(),r.categoryvisibility&&this.updateChildVisibility +(r.categoryvisibility),r.coursevisibility&&this.updateCourseVisiblity(r.coursevisibility),this.updated()},hide:function(e,t,n){var r=this.checkAjaxResponse(e,t,n);if(r===!1)return!1;this.markHidden(),r.categoryvisibility&&this.updateChildVisibility(r.categoryvisibility),r.coursevisibility&&this.updateCourseVisiblity(r.coursevisibility),this.updated()},updateCourseVisiblity:function(e){var t=this.get("console"),n,r;try{for(n in e)e.hasOwnProperty[n]&&(r=t.getCourseById(e[n].id),r&&(e[n].show==="1"?r.markVisible():r.markHidden()))}catch(i){}return this},updateChildVisibility:function(e){var t=this.get("console"),n,r;try{for(n in e)e.hasOwnProperty[n]&&(r=t.getCategoryById(e[n].id),r&&(e[n].show==="1"?r.markVisible():r.markHidden()))}catch(i){}return this}},e.extend(s,i,s.prototype),o.NAME="moodle-course-management-course",o.CSS_PREFIX="management-course",o.ATTRS={courseid:{},selected:{getter:function(e,t){return e===null&&(e=this.get("node").getData(t),this.set(t,e)),e},value:null},node:{},console:{writeOnce:"initOnly"},category:{writeOnce:"initOnly"}},o.prototype={initializer:function(){var e=this.get("node"),t=this.get("category");this.set("courseid",e.getData("id")),t&&t.registerCourse&&t.registerCourse(this),this.set("itemname","course")},getName:function(){return this.get("node").one("a.coursename").get("innerHTML")},handle:function(e,t){var n=this.get("console"),r={courseid:this.get("courseid")};switch(e){case"moveup":t.halt(),n.performAjaxAction("movecourseup",r,this.moveup,this);break;case"movedown":t.halt(),n.performAjaxAction("movecoursedown",r,this.movedown,this);break;case"show":t.halt(),n.performAjaxAction("showcourse",r,this.show,this);break;case"hide":t.halt(),n.performAjaxAction("hidecourse",r,this.hide,this);break;default:return!1}},remove:function(){this.get("console").removeCourseById(this.get("courseid")),this.get("node").remove()},moveAfter:function(e,t){var n=this.get("console"),r={courseid:this.get("courseid"),moveafter:e,previous:t};n.performAjaxAction("movecourseafter",r,this.moveAfterResponse,this)},moveAfterResponse:function(e,t,n){var r=this.checkAjaxResponse(e,t,n),i=this.get("node"),s;if(r===!1)return s=i.ancestor("ul").one("li[data-id="+n.previous+"]"),s?s.insertAfter(i,"after"):i.ancestor("ul").one("li").insert(i,"before"),!1;this.highlight()}},e.extend(o,i,o.prototype)},"@VERSION@",{requires:["base","node","io-base","moodle-core-notification-exception","json-parse","dd-constrain","dd-proxy","dd-drop","dd-delegate","node-event-delegate"]}); diff --git a/course/yui/build/moodle-course-management/moodle-course-management.js b/course/yui/build/moodle-course-management/moodle-course-management.js index 56773b9c680..e79293dbeb4 100644 --- a/course/yui/build/moodle-course-management/moodle-course-management.js +++ b/course/yui/build/moodle-course-management/moodle-course-management.js @@ -3,7 +3,7 @@ YUI.add('moodle-course-management', function (Y, NAME) { /** * Provides drop down menus for list of action links. * - * @module moodle-core_course-management + * @module moodle-course-management */ /** @@ -11,7 +11,7 @@ YUI.add('moodle-course-management', function (Y, NAME) { * * Provides the organisation for course and category management JS. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Console * @constructor * @extends Y.Base @@ -29,7 +29,7 @@ Console.ATTRS = { */ element : { setter : function(node) { - if (typeof(node) === 'string') { + if (typeof node === 'string') { node = Y.one('#'+node); } return node; @@ -40,15 +40,21 @@ Console.ATTRS = { * The category listing container node. * @attribute categorylisting * @type Node + * @default null */ - categorylisting : {}, + categorylisting : { + value : null + }, /** * The course listing container node. * @attribute courselisting * @type Node + * @default null */ - courselisting : {}, + courselisting : { + value : null + }, /** * The course details container node. @@ -57,22 +63,28 @@ Console.ATTRS = { * @default null */ coursedetails : { - value: null + value : null }, /** * The id of the currently active category. * @attribute activecategoryid - * @type Int + * @type Number + * @default null */ - activecategoryid : {}, + activecategoryid : { + value : null + }, /** * The id of the currently active course. * @attribute activecourseid - * @type Int + * @type Number + * @default Null */ - activecourseid : {}, + activecourseid : { + value : null + }, /** * The categories that are currently available through the management interface. @@ -95,17 +107,12 @@ Console.ATTRS = { /** * The courses that are currently available through the management interface. * @attribute courses - * @type Array - * @default [] + * @type Course[] + * @default Array */ courses : { - setter : function(item, name) { - if (Y.Lang.isArray(item)) { - return item; - } - var items = this.get(name); - items.push(item); - return items; + validator : function(val) { + return Y.Lang.isArray(val); }, value : [] }, @@ -113,7 +120,7 @@ Console.ATTRS = { /** * The currently displayed page of courses. * @attribute page - * @type Int + * @type Number * @default null */ page : { @@ -130,7 +137,7 @@ Console.ATTRS = { /** * The total pages of courses that can be shown for this category. * @attribute totalpages - * @type Int + * @type Number * @default null */ totalpages : { @@ -147,7 +154,7 @@ Console.ATTRS = { /** * The total number of courses belonging to this category. * @attribute totalcourses - * @type Int + * @type Number * @default null */ totalcourses : { @@ -224,8 +231,8 @@ Console.prototype = { if (selectedcourse) { this.set('activecourseid', selectedcourse.getData('id')); } - this.initialise_categories(categorylisting); - this.initialise_courses(); + this.initialiseCategories(categorylisting); + this.initialiseCourses(); if (courselisting) { // No need for dragdrop if we don't have a course listing. @@ -235,11 +242,11 @@ Console.prototype = { /** * Initialises all the categories being shown. - * @method initialise_categories + * @method initialiseCategories * @private * @returns {boolean} */ - initialise_categories : function(listing) { + initialiseCategories : function(listing) { var count = 0; if (!listing) { return false; @@ -252,7 +259,7 @@ Console.prototype = { count++; }, this); if (!this.categoriesinit) { - this.get('categorylisting').delegate('click', this.handle_category_delegation, 'a[data-action]', this); + this.get('categorylisting').delegate('click', this.handleCategoryDelegation, 'a[data-action]', this); this.categoriesinit = true; } else { } @@ -260,99 +267,129 @@ Console.prototype = { /** * Initialises all the categories being shown. - * @method initialise_courses + * @method initialiseCourses * @private * @returns {boolean} */ - initialise_courses : function() { - var category = this.get_category_by_id(this.get('activecategoryid')), + initialiseCourses : function() { + var category = this.getCategoryById(this.get('activecategoryid')), listing = this.get('courselisting'), count = 0; if (!listing) { return false; } + if (!category) { + return false; + } listing.all('.listitem[data-id]').each(function(node){ - this.set('courses', new Course({ + this.registerCourse(new Course({ node : node, console : this, category : category })); count++; }, this); - listing.delegate('click', this.handle_course_delegation, 'a[data-action]', this); + listing.delegate('click', this.handleCourseDelegation, 'a[data-action]', this); + }, + + /** + * Registers a course within the management display. + * @method registerCourse + * @param {Course} course + */ + registerCourse : function(course) { + var courses = this.get('courses'); + courses.push(course); + this.set('courses', courses); }, /** * Handles the event fired by a delegated course listener. * - * @method handle_course_delegation + * @method handleCourseDelegation * @protected * @param {EventFacade} e */ - handle_course_delegation : function(e) { + handleCourseDelegation : function(e) { var target = e.currentTarget, action = target.getData('action'), courseid = target.ancestor('.listitem').getData('id'), - course = this.get_course_by_id(courseid); - course.handle(action, e); + course = this.getCourseById(courseid); + if (course) { + course.handle(action, e); + } else { + } }, /** * Handles the event fired by a delegated course listener. * - * @method handle_category_delegation + * @method handleCategoryDelegation * @protected * @param {EventFacade} e */ - handle_category_delegation : function(e) { + handleCategoryDelegation : function(e) { var target = e.currentTarget, action = target.getData('action'), categoryid = target.ancestor('.listitem').getData('id'), - category = this.get_category_by_id(categoryid); - category.handle(action, e); + category = this.getCategoryById(categoryid); + if (category) { + category.handle(action, e); + } else { + } }, /** * Returns the category with the given ID. - * @method get_category_by_id - * @param {Int} id - * @returns {Category|Int} The category or the categoryid given if there is no matching category. + * @method getCategoryById + * @param {Number} id + * @returns {Category|Boolean} The category or false if it can't be found. */ - get_category_by_id : function(id) { - var i, category, categories = this.get('categories'), length = categories.length; + getCategoryById : function(id) { + var i, + category, + categories = this.get('categories'), + length = categories.length; for (i = 0; i < length; i++) { category = categories[i]; if (category.get('categoryid') === id) { return category; } } - return id; - }, - - /** - * Returns the course with the given id. - * @method get_course_by_id - * @param {Int} id - * @returns {Category|Int} The course or the courseid given if there is no matching category. - */ - get_course_by_id : function(id) { - var i, course, courses = this.get('courses'), length = courses.length; - for (i = 0; i < length; i++) { - course = courses[i]; - if (course.get('courseid') === id) { - return course; - } - } return false; }, /** - * Removes the course with the given ID. - * @method remove_course_by_id - * @param {Int} id + * Returns the course with the given id. + * @method getCourseById + * @param {Number} id + * @returns {Course|Boolean} The course or false if not found/ */ - remove_course_by_id : function() { + getCourseById : function(id) { + var i, + course, + courses = this.get('courses'), + length = courses.length; + for (i = 0; i < length; i++) { + if (!courses.hadOwnPropery(i)) { + course = courses[i]; + if (course.get('courseid') === id) { + return course; + } + } + } + return id; + }, + + /** + * Removes the course with the given ID. + * @method removeCourseById + * @param {Number} id + */ + removeCourseById : function() { var courses = this.get('courses'), + length = courses.length, + course, i; for (i = 0; i < length; i++) { course = courses[i]; @@ -366,13 +403,13 @@ Console.prototype = { /** * Performs an AJAX action. * - * @method perform_ajax_action + * @method performAjaxAction * @param {String} action The action to perform. * @param {Object} args The arguments to pass through with teh request. * @param {Function} callback The function to call when all is done. * @param {Object} context The object to use as the context for the callback. */ - perform_ajax_action : function(action, args, callback, context) { + performAjaxAction : function(action, args, callback, context) { var io = new Y.IO(); args.action = action; args.ajax = '1'; @@ -390,25 +427,21 @@ Console.prototype = { }; Y.extend(Console, Y.Base, Console.prototype); -/** - * Course namespace. - * @static - * @namespace M - * @class course - */ M.course = M.course || {}; +M.course.management = M.course.management || {}; +M.course.management.console = null; /** * Initalises the course management console. * @static * @param {Object} config */ -M.course.init_management = function(config) { - M.course.console = new Console(config); +M.course.management.init = function(config) { + M.course.management.console = new Console(config); };/** * Drag and Drop handler * - * @namespace M.core_course.management + * @namespace M.course.management * @class DragDrop * @constructor * @extends Y.Base @@ -650,13 +683,14 @@ DragDrop.prototype = { return false; } courseid = drag.getData('id'); - course = console.get_course_by_id(courseid); - if (iscategory) { categoryid = drop.ancestor('.listitem-category').getData('id'); - category = console.get_category_by_id(categoryid); + category = console.getCategoryById(categoryid); if (category) { - category.moveCourseTo(course); + course = console.getCourseById(courseid); + if (course) { + category.moveCourseTo(course); + } } } else if (iscourse || drop.ancestor('#course-listing')) { previoussibling = drag.get('previousSibling'); @@ -672,7 +706,7 @@ DragDrop.prototype = { Y.extend(DragDrop, Y.Base, DragDrop.prototype);/** * A managed course. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Item * @constructor * @extends Y.Base @@ -721,14 +755,14 @@ Item.prototype = { /** * Checks and parses an AJAX response for an item. * - * @method check_ajax_response + * @method checkAjaxResponse * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Object|Boolean} */ - check_ajax_response : function(transactionid, response, args) { + checkAjaxResponse : function(transactionid, response, args) { if (response.status !== 200) { return false; } @@ -749,7 +783,7 @@ Item.prototype = { * Moves an item up by one. * * @method moveup - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} @@ -762,7 +796,7 @@ Item.prototype = { previousup, previousdown, tmpnode, - outcome = this.check_ajax_response(transactionid, response, args); + outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { return false; } @@ -800,7 +834,7 @@ Item.prototype = { * Moves an item down by one. * * @method movedown - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} @@ -813,7 +847,7 @@ Item.prototype = { nextup, nextdown, tmpnode, - outcome = this.check_ajax_response(transactionid, response, args); + outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { return false; } @@ -851,13 +885,13 @@ Item.prototype = { * Makes an item visible. * * @method show - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ show : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { return false; } @@ -879,13 +913,13 @@ Item.prototype = { * Hides an item. * * @method hide - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ hide : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { return false; } @@ -934,7 +968,7 @@ Item.prototype = { Y.extend(Item, Y.Base, Item.prototype);/** * A managed category. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Category * @constructor * @extends Item @@ -948,9 +982,21 @@ Category.ATTRS = { /** * The category ID relating to this category. * @attribute categoryid - * @type Int + * @type Number + * @writeOnce + * @default null */ - categoryid : {}, + categoryid : { + getter : function (value, name) { + if (value === null) { + value = this.get('node').getData('id'); + this.set(name, value); + } + return value; + }, + value : null, + writeOnce : true + }, /** * True if this category is the currently selected category. @@ -973,19 +1019,14 @@ Category.ATTRS = { }, /** - * An array fo courses belonging to this category + * An array of courses belonging to this category. * @attribute courses * @type Course[] - * @default [] + * @default Array */ courses : { - setter : function(item, name) { - if (Y.Lang.isArray(item)) { - return item; - } - var items = this.get(name); - items.push(item); - return items; + validator : function(val) { + return Y.Lang.isArray(val); }, value : [] } @@ -996,8 +1037,6 @@ Category.prototype = { * @method initializer */ initializer : function() { - var node = this.get('node'); - this.set('categoryid', node.getData('id')); this.set('itemname', 'category'); }, @@ -1012,11 +1051,13 @@ Category.prototype = { /** * Registers a course as belonging to this category. - * @method register_course + * @method registerCourse * @param {Course} course */ - register_course : function(course) { - this.set('courses', course); + registerCourse : function(course) { + var courses = this.get('courses'); + courses.push(course); + this.set('courses', courses); }, /** @@ -1031,31 +1072,31 @@ Category.prototype = { var catarg = {categoryid : this.get('categoryid')}; switch (action) { case 'moveup': - e.halt(); - this.get('console').perform_ajax_action('movecategoryup', catarg, this.moveup, this); + e.preventDefault(); + this.get('console').performAjaxAction('movecategoryup', catarg, this.moveup, this); break; case 'movedown': - e.halt(); - this.get('console').perform_ajax_action('movecategorydown', catarg, this.movedown, this); + e.preventDefault(); + this.get('console').performAjaxAction('movecategorydown', catarg, this.movedown, this); break; case 'show': - e.halt(); - this.get('console').perform_ajax_action('showcategory', catarg, this.show, this); + e.preventDefault(); + this.get('console').performAjaxAction('showcategory', catarg, this.show, this); break; case 'hide': - e.halt(); - this.get('console').perform_ajax_action('hidecategory', catarg, this.hide, this); + e.preventDefault(); + this.get('console').performAjaxAction('hidecategory', catarg, this.hide, this); break; case 'expand': - e.halt(); + e.preventDefault(); if (this.get('node').getData('expanded') === '0') { - this.get('node').setData('expanded', true); - this.get('console').perform_ajax_action('getsubcategorieshtml', catarg, this.loadSubcategories, this); + this.get('node').setAttribute('data-expanded', '1').setData('expanded', 'true'); + this.get('console').performAjaxAction('getsubcategorieshtml', catarg, this.loadSubcategories, this); } this.expand(); break; case 'collapse': - e.halt(); + e.preventDefault(); this.collapse(); break; default: @@ -1071,8 +1112,7 @@ Category.prototype = { var node = this.get('node'), action = node.one('a[data-action=expand]'); node.removeClass('collapsed'); - action.setAttribute('data-action', 'collapse'); - action.one('img').setAttrs({ + action.setAttribute('data-action', 'collapse').one('img').setAttrs({ src : M.util.image_url('t/switch_minus', 'moodle'), title : M.util.get_string('collapse', 'moodle'), alt : M.util.get_string('collapse', 'moodle') @@ -1087,8 +1127,7 @@ Category.prototype = { var node = this.get('node'), action = node.one('a[data-action=collapse]'); node.addClass('collapsed'); - action.setAttribute('data-action', 'expand'); - action.one('img').setAttrs({ + action.setAttribute('data-action', 'expand').one('img').setAttrs({ src : M.util.image_url('t/switch_plus', 'moodle'), title : M.util.get_string('expand', 'moodle'), alt : M.util.get_string('expand', 'moodle') @@ -1100,23 +1139,24 @@ Category.prototype = { * * @method loadSubcategories * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. - * @returns {Boolean} + * @return {Boolean} Returns true on success - false otherwise. */ loadSubcategories : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), node = this.get('node'), console = this.get('console'); if (outcome === false) { return false; } node.append(outcome.html); - console.initialise_categories(node); + console.initialiseCategories(node); if (M.core && M.core.actionmenu && M.core.actionmenu.newDOMNode) { M.core.actionmenu.newDOMNode(node); } + return true; }, /** @@ -1140,7 +1180,7 @@ Category.prototype = { confirm.on('complete-yes', function() { confirm.hide(); confirm.destroy(); - this.get('console').perform_ajax_action('movecourseintocategory', { + this.get('console').performAjaxAction('movecourseintocategory', { categoryid : this.get('categoryid'), courseid : course.get('courseid') }, this.completeMoveCourse, this); @@ -1153,18 +1193,21 @@ Category.prototype = { * Completes moving a course to this category. * @method completeMoveCourse * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ completeMoveCourse : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), course; if (outcome === false) { return false; } - course = this.get('console').get_course_by_id(args.courseid); + course = this.get('console').getCourseById(args.courseid); + if (!course) { + return false; + } this.highlight(); if (course) { course.remove(); @@ -1176,13 +1219,13 @@ Category.prototype = { * Makes an item visible. * * @method show - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ show : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { return false; } @@ -1201,13 +1244,13 @@ Category.prototype = { * Hides an item. * * @method hide - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ hide : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { return false; } @@ -1224,6 +1267,7 @@ Category.prototype = { /** * Updates the visibility of child courses if required. * @method updateCourseVisiblity + * @chainable * @param courses */ updateCourseVisiblity : function(courses) { @@ -1232,22 +1276,26 @@ Category.prototype = { course; try { for (key in courses) { - course = console.get_course_by_id(courses[key].id); - if (course.get) { - if (courses[key].show === "1") { - course.markVisible(); - } else { - course.markHidden(); + if (courses.hasOwnProperty[key]) { + course = console.getCourseById(courses[key].id); + if (course) { + if (courses[key].show === "1") { + course.markVisible(); + } else { + course.markHidden(); + } } } } } catch (err) { } + return this; }, /** * Updates the visibility of subcategories if required. * @method updateChildVisibility + * @chainable * @param categories */ updateChildVisibility : function(categories) { @@ -1256,23 +1304,26 @@ Category.prototype = { category; try { for (key in categories) { - category = console.get_category_by_id(categories[key].id); - if (category.get) { - if (categories[key].show === "1") { - category.markVisible(); - } else { - category.markHidden(); + if (categories.hasOwnProperty[key]) { + category = console.getCategoryById(categories[key].id); + if (category) { + if (categories[key].show === "1") { + category.markVisible(); + } else { + category.markHidden(); + } } } } } catch (err) { } + return this; } }; Y.extend(Category, Item, Category.prototype);/** * A managed course. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Course * @constructor * @extends Item @@ -1287,7 +1338,7 @@ Course.ATTRS = { /** * The course ID of this course. * @attribute courseid - * @type Int + * @type Number */ courseid : {}, @@ -1339,8 +1390,8 @@ Course.prototype = { var node = this.get('node'), category = this.get('category'); this.set('courseid', node.getData('id')); - if (category && category.register_course) { - category.register_course(this); + if (category && category.registerCourse) { + category.registerCourse(this); } this.set('itemname', 'course'); }, @@ -1367,19 +1418,19 @@ Course.prototype = { switch (action) { case 'moveup': e.halt(); - console.perform_ajax_action('movecourseup', args, this.moveup, this); + console.performAjaxAction('movecourseup', args, this.moveup, this); break; case 'movedown': e.halt(); - console.perform_ajax_action('movecoursedown', args, this.movedown, this); + console.performAjaxAction('movecoursedown', args, this.movedown, this); break; case 'show': e.halt(); - console.perform_ajax_action('showcourse', args, this.show, this); + console.performAjaxAction('showcourse', args, this.show, this); break; case 'hide': e.halt(); - console.perform_ajax_action('hidecourse', args, this.hide, this); + console.performAjaxAction('hidecourse', args, this.hide, this); break; default: return false; @@ -1391,7 +1442,7 @@ Course.prototype = { * @method remove */ remove : function() { - this.get('console').remove_course_by_id(this.get('courseid')); + this.get('console').removeCourseById(this.get('courseid')); this.get('node').remove(); }, @@ -1399,8 +1450,8 @@ Course.prototype = { * Moves this course after another course. * * @method moveAfter - * @param {Int} moveaftercourse The course to move after or 0 to put it at the top. - * @param {Int} previousid the course it was previously after in case we need to revert. + * @param {Number} moveaftercourse The course to move after or 0 to put it at the top. + * @param {Number} previousid the course it was previously after in case we need to revert. */ moveAfter : function(moveaftercourse, previousid) { var console = this.get('console'), @@ -1409,7 +1460,7 @@ Course.prototype = { moveafter : moveaftercourse, previous : previousid }; - console.perform_ajax_action('movecourseafter', args, this.moveAfterResponse, this); + console.performAjaxAction('movecourseafter', args, this.moveAfterResponse, this); }, /** @@ -1417,13 +1468,13 @@ Course.prototype = { * * @method moveAfterResponse * @protected - * @param {Int} transactionid The transaction ID for the request. + * @param {Number} transactionid The transaction ID for the request. * @param {Object} response The response to the request. * @param {Objects} args The arguments that were given with the request. * @returns {Boolean} */ moveAfterResponse : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), node = this.get('node'), previous; if (outcome === false) { diff --git a/course/yui/src/management/js/category.js b/course/yui/src/management/js/category.js index 59b662adf6f..39585c21c8e 100644 --- a/course/yui/src/management/js/category.js +++ b/course/yui/src/management/js/category.js @@ -1,7 +1,7 @@ /** * A managed category. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Category * @constructor * @extends Item @@ -15,9 +15,21 @@ Category.ATTRS = { /** * The category ID relating to this category. * @attribute categoryid - * @type Int + * @type Number + * @writeOnce + * @default null */ - categoryid : {}, + categoryid : { + getter : function (value, name) { + if (value === null) { + value = this.get('node').getData('id'); + this.set(name, value); + } + return value; + }, + value : null, + writeOnce : true + }, /** * True if this category is the currently selected category. @@ -40,19 +52,14 @@ Category.ATTRS = { }, /** - * An array fo courses belonging to this category + * An array of courses belonging to this category. * @attribute courses * @type Course[] - * @default [] + * @default Array */ courses : { - setter : function(item, name) { - if (Y.Lang.isArray(item)) { - return item; - } - var items = this.get(name); - items.push(item); - return items; + validator : function(val) { + return Y.Lang.isArray(val); }, value : [] } @@ -63,8 +70,6 @@ Category.prototype = { * @method initializer */ initializer : function() { - var node = this.get('node'); - this.set('categoryid', node.getData('id')); this.set('itemname', 'category'); }, @@ -79,11 +84,13 @@ Category.prototype = { /** * Registers a course as belonging to this category. - * @method register_course + * @method registerCourse * @param {Course} course */ - register_course : function(course) { - this.set('courses', course); + registerCourse : function(course) { + var courses = this.get('courses'); + courses.push(course); + this.set('courses', courses); }, /** @@ -98,35 +105,35 @@ Category.prototype = { var catarg = {categoryid : this.get('categoryid')}; switch (action) { case 'moveup': - e.halt(); - this.get('console').perform_ajax_action('movecategoryup', catarg, this.moveup, this); + e.preventDefault(); + this.get('console').performAjaxAction('movecategoryup', catarg, this.moveup, this); break; case 'movedown': - e.halt(); - this.get('console').perform_ajax_action('movecategorydown', catarg, this.movedown, this); + e.preventDefault(); + this.get('console').performAjaxAction('movecategorydown', catarg, this.movedown, this); break; case 'show': - e.halt(); - this.get('console').perform_ajax_action('showcategory', catarg, this.show, this); + e.preventDefault(); + this.get('console').performAjaxAction('showcategory', catarg, this.show, this); break; case 'hide': - e.halt(); - this.get('console').perform_ajax_action('hidecategory', catarg, this.hide, this); + e.preventDefault(); + this.get('console').performAjaxAction('hidecategory', catarg, this.hide, this); break; case 'expand': - e.halt(); + e.preventDefault(); if (this.get('node').getData('expanded') === '0') { - this.get('node').setData('expanded', true); - this.get('console').perform_ajax_action('getsubcategorieshtml', catarg, this.loadSubcategories, this); + this.get('node').setAttribute('data-expanded', '1').setData('expanded', 'true'); + this.get('console').performAjaxAction('getsubcategorieshtml', catarg, this.loadSubcategories, this); } this.expand(); break; case 'collapse': - e.halt(); + e.preventDefault(); this.collapse(); break; default: - Y.log('Invalid AJAX action requested of managed category.', 'warn', 'core_course'); + Y.log('Invalid AJAX action requested of managed category.', 'warn', 'moodle-course-management'); return false; } }, @@ -139,8 +146,7 @@ Category.prototype = { var node = this.get('node'), action = node.one('a[data-action=expand]'); node.removeClass('collapsed'); - action.setAttribute('data-action', 'collapse'); - action.one('img').setAttrs({ + action.setAttribute('data-action', 'collapse').one('img').setAttrs({ src : M.util.image_url('t/switch_minus', 'moodle'), title : M.util.get_string('collapse', 'moodle'), alt : M.util.get_string('collapse', 'moodle') @@ -155,8 +161,7 @@ Category.prototype = { var node = this.get('node'), action = node.one('a[data-action=collapse]'); node.addClass('collapsed'); - action.setAttribute('data-action', 'expand'); - action.one('img').setAttrs({ + action.setAttribute('data-action', 'expand').one('img').setAttrs({ src : M.util.image_url('t/switch_plus', 'moodle'), title : M.util.get_string('expand', 'moodle'), alt : M.util.get_string('expand', 'moodle') @@ -168,25 +173,26 @@ Category.prototype = { * * @method loadSubcategories * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. - * @returns {Boolean} + * @return {Boolean} Returns true on success - false otherwise. */ loadSubcategories : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), node = this.get('node'), console = this.get('console'); if (outcome === false) { - Y.log('AJAX failed to load sub categories for '+this.get('itemname'), 'warn', 'core_course'); + Y.log('AJAX failed to load sub categories for '+this.get('itemname'), 'warn', 'moodle-course-management'); return false; } - Y.log('AJAX loaded subcategories for '+this.get('itemname'), 'info', 'core_course'); + Y.log('AJAX loaded subcategories for '+this.get('itemname'), 'info', 'moodle-course-management'); node.append(outcome.html); - console.initialise_categories(node); + console.initialiseCategories(node); if (M.core && M.core.actionmenu && M.core.actionmenu.newDOMNode) { M.core.actionmenu.newDOMNode(node); } + return true; }, /** @@ -210,7 +216,7 @@ Category.prototype = { confirm.on('complete-yes', function() { confirm.hide(); confirm.destroy(); - this.get('console').perform_ajax_action('movecourseintocategory', { + this.get('console').performAjaxAction('movecourseintocategory', { categoryid : this.get('categoryid'), courseid : course.get('courseid') }, this.completeMoveCourse, this); @@ -223,20 +229,24 @@ Category.prototype = { * Completes moving a course to this category. * @method completeMoveCourse * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ completeMoveCourse : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), course; if (outcome === false) { - Y.log('AJAX failed to move courses into this category: '+this.get('itemname'), 'warn', 'core_course'); + Y.log('AJAX failed to move courses into this category: '+this.get('itemname'), 'warn', 'moodle-course-management'); return false; } - course = this.get('console').get_course_by_id(args.courseid); - Y.log('Moved the course ('+course.getName()+') into this category ('+this.getName()+')', 'info', 'core_course'); + course = this.get('console').getCourseById(args.courseid); + if (!course) { + Y.log('Course was moved but the course listing could not be found to reflect this', 'warn', 'moodle-course-management'); + return false; + } + Y.log('Moved the course ('+course.getName()+') into this category ('+this.getName()+')', 'info', 'moodle-course-management'); this.highlight(); if (course) { course.remove(); @@ -248,15 +258,15 @@ Category.prototype = { * Makes an item visible. * * @method show - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ show : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } @@ -268,22 +278,22 @@ Category.prototype = { this.updateCourseVisiblity(outcome.coursevisibility); } this.updated(); - Y.log('Success: category made visible by AJAX.', 'info', 'core_course'); + Y.log('Success: category made visible by AJAX.', 'info', 'moodle-course-management'); }, /** * Hides an item. * * @method hide - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ hide : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } this.markHidden(); @@ -294,12 +304,13 @@ Category.prototype = { this.updateCourseVisiblity(outcome.coursevisibility); } this.updated(); - Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'moodle-course-management'); }, /** * Updates the visibility of child courses if required. * @method updateCourseVisiblity + * @chainable * @param courses */ updateCourseVisiblity : function(courses) { @@ -308,23 +319,27 @@ Category.prototype = { course; try { for (key in courses) { - course = console.get_course_by_id(courses[key].id); - if (course.get) { - if (courses[key].show === "1") { - course.markVisible(); - } else { - course.markHidden(); + if (courses.hasOwnProperty[key]) { + course = console.getCourseById(courses[key].id); + if (course) { + if (courses[key].show === "1") { + course.markVisible(); + } else { + course.markHidden(); + } } } } } catch (err) { - Y.log('Error trying to update course visibility: ' + err.message, 'warn', 'core_course'); + Y.log('Error trying to update course visibility: ' + err.message, 'warn', 'moodle-course-management'); } + return this; }, /** * Updates the visibility of subcategories if required. * @method updateChildVisibility + * @chainable * @param categories */ updateChildVisibility : function(categories) { @@ -333,18 +348,21 @@ Category.prototype = { category; try { for (key in categories) { - category = console.get_category_by_id(categories[key].id); - if (category.get) { - if (categories[key].show === "1") { - category.markVisible(); - } else { - category.markHidden(); + if (categories.hasOwnProperty[key]) { + category = console.getCategoryById(categories[key].id); + if (category) { + if (categories[key].show === "1") { + category.markVisible(); + } else { + category.markHidden(); + } } } } } catch (err) { - Y.log('Error trying to update category visibility: ' + err.message, 'warn', 'core_course'); + Y.log('Error trying to update category visibility: ' + err.message, 'warn', 'moodle-course-management'); } + return this; } }; Y.extend(Category, Item, Category.prototype); \ No newline at end of file diff --git a/course/yui/src/management/js/console.js b/course/yui/src/management/js/console.js index b8f07db27db..e968d3b6fac 100644 --- a/course/yui/src/management/js/console.js +++ b/course/yui/src/management/js/console.js @@ -1,7 +1,7 @@ /** * Provides drop down menus for list of action links. * - * @module moodle-core_course-management + * @module moodle-course-management */ /** @@ -9,7 +9,7 @@ * * Provides the organisation for course and category management JS. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Console * @constructor * @extends Y.Base @@ -27,7 +27,7 @@ Console.ATTRS = { */ element : { setter : function(node) { - if (typeof(node) === 'string') { + if (typeof node === 'string') { node = Y.one('#'+node); } return node; @@ -38,15 +38,21 @@ Console.ATTRS = { * The category listing container node. * @attribute categorylisting * @type Node + * @default null */ - categorylisting : {}, + categorylisting : { + value : null + }, /** * The course listing container node. * @attribute courselisting * @type Node + * @default null */ - courselisting : {}, + courselisting : { + value : null + }, /** * The course details container node. @@ -55,22 +61,28 @@ Console.ATTRS = { * @default null */ coursedetails : { - value: null + value : null }, /** * The id of the currently active category. * @attribute activecategoryid - * @type Int + * @type Number + * @default null */ - activecategoryid : {}, + activecategoryid : { + value : null + }, /** * The id of the currently active course. * @attribute activecourseid - * @type Int + * @type Number + * @default Null */ - activecourseid : {}, + activecourseid : { + value : null + }, /** * The categories that are currently available through the management interface. @@ -93,17 +105,12 @@ Console.ATTRS = { /** * The courses that are currently available through the management interface. * @attribute courses - * @type Array - * @default [] + * @type Course[] + * @default Array */ courses : { - setter : function(item, name) { - if (Y.Lang.isArray(item)) { - return item; - } - var items = this.get(name); - items.push(item); - return items; + validator : function(val) { + return Y.Lang.isArray(val); }, value : [] }, @@ -111,7 +118,7 @@ Console.ATTRS = { /** * The currently displayed page of courses. * @attribute page - * @type Int + * @type Number * @default null */ page : { @@ -128,7 +135,7 @@ Console.ATTRS = { /** * The total pages of courses that can be shown for this category. * @attribute totalpages - * @type Int + * @type Number * @default null */ totalpages : { @@ -145,7 +152,7 @@ Console.ATTRS = { /** * The total number of courses belonging to this category. * @attribute totalcourses - * @type Int + * @type Number * @default null */ totalcourses : { @@ -200,7 +207,7 @@ Console.prototype = { * @method initializer */ initializer : function() { - Y.log('Initialising course category management console', 'note', 'core_course'); + Y.log('Initialising course category management console', 'info', 'moodle-course-management'); this.set('element', 'coursecat-management'); var element = this.get('element'), categorylisting = element.one('#category-listing'), @@ -223,8 +230,8 @@ Console.prototype = { if (selectedcourse) { this.set('activecourseid', selectedcourse.getData('id')); } - this.initialise_categories(categorylisting); - this.initialise_courses(); + this.initialiseCategories(categorylisting); + this.initialiseCourses(); if (courselisting) { // No need for dragdrop if we don't have a course listing. @@ -234,11 +241,11 @@ Console.prototype = { /** * Initialises all the categories being shown. - * @method initialise_categories + * @method initialiseCategories * @private * @returns {boolean} */ - initialise_categories : function(listing) { + initialiseCategories : function(listing) { var count = 0; if (!listing) { return false; @@ -251,110 +258,143 @@ Console.prototype = { count++; }, this); if (!this.categoriesinit) { - this.get('categorylisting').delegate('click', this.handle_category_delegation, 'a[data-action]', this); + this.get('categorylisting').delegate('click', this.handleCategoryDelegation, 'a[data-action]', this); this.categoriesinit = true; - Y.log(count+' categories being managed', 'note', 'core_course'); + Y.log(count+' categories being managed', 'info', 'moodle-course-management'); } else { - Y.log(count+' new categories being managed', 'note', 'core_course'); + Y.log(count+' new categories being managed', 'info', 'moodle-course-management'); } }, /** * Initialises all the categories being shown. - * @method initialise_courses + * @method initialiseCourses * @private * @returns {boolean} */ - initialise_courses : function() { - var category = this.get_category_by_id(this.get('activecategoryid')), + initialiseCourses : function() { + var category = this.getCategoryById(this.get('activecategoryid')), listing = this.get('courselisting'), count = 0; if (!listing) { return false; } + if (!category) { + Y.log('Couldn\'t find the current category object.', 'warn', 'moodle-course-management'); + return false; + } listing.all('.listitem[data-id]').each(function(node){ - this.set('courses', new Course({ + this.registerCourse(new Course({ node : node, console : this, category : category })); count++; }, this); - listing.delegate('click', this.handle_course_delegation, 'a[data-action]', this); - Y.log(count+' courses being managed', 'note', 'core_course'); + listing.delegate('click', this.handleCourseDelegation, 'a[data-action]', this); + Y.log(count+' courses being managed', 'info', 'moodle-course-management'); + }, + + /** + * Registers a course within the management display. + * @method registerCourse + * @param {Course} course + */ + registerCourse : function(course) { + var courses = this.get('courses'); + courses.push(course); + this.set('courses', courses); }, /** * Handles the event fired by a delegated course listener. * - * @method handle_course_delegation + * @method handleCourseDelegation * @protected * @param {EventFacade} e */ - handle_course_delegation : function(e) { + handleCourseDelegation : function(e) { var target = e.currentTarget, action = target.getData('action'), courseid = target.ancestor('.listitem').getData('id'), - course = this.get_course_by_id(courseid); - course.handle(action, e); + course = this.getCourseById(courseid); + if (course) { + course.handle(action, e); + } else { + Y.log('Course with ID '+courseid+' could not be found for delegation', 'error', 'moodle-course-management'); + } }, /** * Handles the event fired by a delegated course listener. * - * @method handle_category_delegation + * @method handleCategoryDelegation * @protected * @param {EventFacade} e */ - handle_category_delegation : function(e) { + handleCategoryDelegation : function(e) { var target = e.currentTarget, action = target.getData('action'), categoryid = target.ancestor('.listitem').getData('id'), - category = this.get_category_by_id(categoryid); - category.handle(action, e); + category = this.getCategoryById(categoryid); + if (category) { + category.handle(action, e); + } else { + Y.log('Could not find category to delegate to.', 'error', 'moodle-course-management'); + } }, /** * Returns the category with the given ID. - * @method get_category_by_id - * @param {Int} id - * @returns {Category|Int} The category or the categoryid given if there is no matching category. + * @method getCategoryById + * @param {Number} id + * @returns {Category|Boolean} The category or false if it can't be found. */ - get_category_by_id : function(id) { - var i, category, categories = this.get('categories'), length = categories.length; + getCategoryById : function(id) { + var i, + category, + categories = this.get('categories'), + length = categories.length; for (i = 0; i < length; i++) { category = categories[i]; if (category.get('categoryid') === id) { return category; } } - return id; - }, - - /** - * Returns the course with the given id. - * @method get_course_by_id - * @param {Int} id - * @returns {Category|Int} The course or the courseid given if there is no matching category. - */ - get_course_by_id : function(id) { - var i, course, courses = this.get('courses'), length = courses.length; - for (i = 0; i < length; i++) { - course = courses[i]; - if (course.get('courseid') === id) { - return course; - } - } return false; }, /** - * Removes the course with the given ID. - * @method remove_course_by_id - * @param {Int} id + * Returns the course with the given id. + * @method getCourseById + * @param {Number} id + * @returns {Course|Boolean} The course or false if not found/ */ - remove_course_by_id : function() { + getCourseById : function(id) { + var i, + course, + courses = this.get('courses'), + length = courses.length; + for (i = 0; i < length; i++) { + if (!courses.hadOwnPropery(i)) { + course = courses[i]; + if (course.get('courseid') === id) { + return course; + } + } + } + return id; + }, + + /** + * Removes the course with the given ID. + * @method removeCourseById + * @param {Number} id + */ + removeCourseById : function() { var courses = this.get('courses'), + length = courses.length, + course, i; for (i = 0; i < length; i++) { course = courses[i]; @@ -368,13 +408,13 @@ Console.prototype = { /** * Performs an AJAX action. * - * @method perform_ajax_action + * @method performAjaxAction * @param {String} action The action to perform. * @param {Object} args The arguments to pass through with teh request. * @param {Function} callback The function to call when all is done. * @param {Object} context The object to use as the context for the callback. */ - perform_ajax_action : function(action, args, callback, context) { + performAjaxAction : function(action, args, callback, context) { var io = new Y.IO(); args.action = action; args.ajax = '1'; @@ -392,19 +432,15 @@ Console.prototype = { }; Y.extend(Console, Y.Base, Console.prototype); -/** - * Course namespace. - * @static - * @namespace M - * @class course - */ M.course = M.course || {}; +M.course.management = M.course.management || {}; +M.course.management.console = null; /** * Initalises the course management console. * @static * @param {Object} config */ -M.course.init_management = function(config) { - M.course.console = new Console(config); +M.course.management.init = function(config) { + M.course.management.console = new Console(config); }; \ No newline at end of file diff --git a/course/yui/src/management/js/course.js b/course/yui/src/management/js/course.js index fc0c252618f..aea78a3babc 100644 --- a/course/yui/src/management/js/course.js +++ b/course/yui/src/management/js/course.js @@ -1,7 +1,7 @@ /** * A managed course. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Course * @constructor * @extends Item @@ -16,7 +16,7 @@ Course.ATTRS = { /** * The course ID of this course. * @attribute courseid - * @type Int + * @type Number */ courseid : {}, @@ -68,8 +68,8 @@ Course.prototype = { var node = this.get('node'), category = this.get('category'); this.set('courseid', node.getData('id')); - if (category && category.register_course) { - category.register_course(this); + if (category && category.registerCourse) { + category.registerCourse(this); } this.set('itemname', 'course'); }, @@ -96,22 +96,22 @@ Course.prototype = { switch (action) { case 'moveup': e.halt(); - console.perform_ajax_action('movecourseup', args, this.moveup, this); + console.performAjaxAction('movecourseup', args, this.moveup, this); break; case 'movedown': e.halt(); - console.perform_ajax_action('movecoursedown', args, this.movedown, this); + console.performAjaxAction('movecoursedown', args, this.movedown, this); break; case 'show': e.halt(); - console.perform_ajax_action('showcourse', args, this.show, this); + console.performAjaxAction('showcourse', args, this.show, this); break; case 'hide': e.halt(); - console.perform_ajax_action('hidecourse', args, this.hide, this); + console.performAjaxAction('hidecourse', args, this.hide, this); break; default: - Y.log('Invalid AJAX action requested of managed course.', 'warn', 'core_course'); + Y.log('Invalid AJAX action requested of managed course.', 'warn', 'moodle-course-management'); return false; } }, @@ -121,7 +121,7 @@ Course.prototype = { * @method remove */ remove : function() { - this.get('console').remove_course_by_id(this.get('courseid')); + this.get('console').removeCourseById(this.get('courseid')); this.get('node').remove(); }, @@ -129,8 +129,8 @@ Course.prototype = { * Moves this course after another course. * * @method moveAfter - * @param {Int} moveaftercourse The course to move after or 0 to put it at the top. - * @param {Int} previousid the course it was previously after in case we need to revert. + * @param {Number} moveaftercourse The course to move after or 0 to put it at the top. + * @param {Number} previousid the course it was previously after in case we need to revert. */ moveAfter : function(moveaftercourse, previousid) { var console = this.get('console'), @@ -139,7 +139,7 @@ Course.prototype = { moveafter : moveaftercourse, previous : previousid }; - console.perform_ajax_action('movecourseafter', args, this.moveAfterResponse, this); + console.performAjaxAction('movecourseafter', args, this.moveAfterResponse, this); }, /** @@ -147,18 +147,18 @@ Course.prototype = { * * @method moveAfterResponse * @protected - * @param {Int} transactionid The transaction ID for the request. + * @param {Number} transactionid The transaction ID for the request. * @param {Object} response The response to the request. * @param {Objects} args The arguments that were given with the request. * @returns {Boolean} */ moveAfterResponse : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args), + var outcome = this.checkAjaxResponse(transactionid, response, args), node = this.get('node'), previous; if (outcome === false) { previous = node.ancestor('ul').one('li[data-id='+args.previous+']'); - Y.log('AJAX failed to move this course after the requested course', 'warn', 'core_course'); + Y.log('AJAX failed to move this course after the requested course', 'warn', 'moodle-course-management'); if (previous) { // After the last previous. previous.insertAfter(node, 'after'); @@ -168,7 +168,7 @@ Course.prototype = { } return false; } - Y.log('AJAX successfully moved course ('+this.getName()+')', 'info', 'core_course'); + Y.log('AJAX successfully moved course ('+this.getName()+')', 'info', 'moodle-course-management'); this.highlight(); } }; diff --git a/course/yui/src/management/js/dd.js b/course/yui/src/management/js/dd.js index 37dd2b04c4e..8e24c144967 100644 --- a/course/yui/src/management/js/dd.js +++ b/course/yui/src/management/js/dd.js @@ -1,7 +1,7 @@ /** * Drag and Drop handler * - * @namespace M.core_course.management + * @namespace M.course.management * @class DragDrop * @constructor * @extends Y.Base @@ -239,19 +239,20 @@ DragDrop.prototype = { previousid; if (!drag.test('.listitem-course')) { - Y.log('Wasn\'t what I expected', 'warn', 'core_course'); + Y.log('Wasn\'t what I expected', 'warn', 'moodle-course-management'); alert(drag); return false; } courseid = drag.getData('id'); - course = console.get_course_by_id(courseid); - if (iscategory) { categoryid = drop.ancestor('.listitem-category').getData('id'); Y.log('Course ' + courseid + ' dragged into category ' + categoryid); - category = console.get_category_by_id(categoryid); + category = console.getCategoryById(categoryid); if (category) { - category.moveCourseTo(course); + course = console.getCourseById(courseid); + if (course) { + category.moveCourseTo(course); + } } } else if (iscourse || drop.ancestor('#course-listing')) { previoussibling = drag.get('previousSibling'); @@ -261,7 +262,7 @@ DragDrop.prototype = { course.moveAfter(aftercourseid, previousid); } } else { - Y.log('Course dropped over unhandled target.', 'info', 'core_course'); + Y.log('Course dropped over unhandled target.', 'info', 'moodle-course-management'); } } }; diff --git a/course/yui/src/management/js/item.js b/course/yui/src/management/js/item.js index 1cc45188b01..c4928805a45 100644 --- a/course/yui/src/management/js/item.js +++ b/course/yui/src/management/js/item.js @@ -1,7 +1,7 @@ /** * A managed course. * - * @namespace M.core_course.management + * @namespace M.course.management * @class Item * @constructor * @extends Y.Base @@ -50,20 +50,20 @@ Item.prototype = { /** * Checks and parses an AJAX response for an item. * - * @method check_ajax_response + * @method checkAjaxResponse * @protected - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Object|Boolean} */ - check_ajax_response : function(transactionid, response, args) { + checkAjaxResponse : function(transactionid, response, args) { if (response.status !== 200) { - Y.log('Error: AJAX response resulted in non 200 status.', 'error', 'Item.check_ajax_response'); + Y.log('Error: AJAX response resulted in non 200 status.', 'error', 'Item.checkAjaxResponse'); return false; } if (transactionid === null || args === null) { - Y.log('Error: Invalid AJAX response details provided.', 'error', 'Item.check_ajax_response'); + Y.log('Error: Invalid AJAX response details provided.', 'error', 'Item.checkAjaxResponse'); return false; } var outcome = Y.JSON.parse(response.responseText); @@ -80,7 +80,7 @@ Item.prototype = { * Moves an item up by one. * * @method moveup - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} @@ -93,9 +93,9 @@ Item.prototype = { previousup, previousdown, tmpnode, - outcome = this.check_ajax_response(transactionid, response, args); + outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to move '+this.get('itemname')+' up failed by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to move '+this.get('itemname')+' up failed by outcome.', 'warn', 'moodle-course-management'); return false; } node = this.get('node'); @@ -124,9 +124,9 @@ Item.prototype = { } } this.updated(true); - Y.log('Success: '+this.get('itemname')+' moved up by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' moved up by AJAX.', 'info', 'moodle-course-management'); } else { - Y.log(this.get('itemname')+' cannot be moved up as its the top item', 'warn', 'core_course'); + Y.log(this.get('itemname')+' cannot be moved up as its the top item', 'warn', 'moodle-course-management'); } }, @@ -134,7 +134,7 @@ Item.prototype = { * Moves an item down by one. * * @method movedown - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} @@ -147,9 +147,9 @@ Item.prototype = { nextup, nextdown, tmpnode, - outcome = this.check_ajax_response(transactionid, response, args); + outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to move '+this.get('itemname')+' down failed by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to move '+this.get('itemname')+' down failed by outcome.', 'warn', 'moodle-course-management'); return false; } node = this.get('node'); @@ -178,9 +178,9 @@ Item.prototype = { } } this.updated(true); - Y.log('Success: '+this.get('itemname')+' moved down by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' moved down by AJAX.', 'info', 'moodle-course-management'); } else { - Y.log(this.get('itemname')+' cannot be moved down as its the last item', 'warn', 'core_course'); + Y.log(this.get('itemname')+' cannot be moved down as its the last item', 'warn', 'moodle-course-management'); } }, @@ -188,21 +188,21 @@ Item.prototype = { * Makes an item visible. * * @method show - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ show : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to show '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } this.markVisible(); this.updated(); - Y.log('Success: '+this.get('itemname')+' made visible by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' made visible by AJAX.', 'info', 'moodle-course-management'); }, /** @@ -211,7 +211,7 @@ Item.prototype = { */ markVisible : function() { this.get('node').setAttribute('data-visible', '1'); - Y.log('Marked '+this.get('itemname')+' as visible', 'note', 'core_course'); + Y.log('Marked '+this.get('itemname')+' as visible', 'info', 'moodle-course-management'); return true; }, @@ -219,20 +219,20 @@ Item.prototype = { * Hides an item. * * @method hide - * @param {Int} transactionid The transaction ID of the AJAX request (unique) + * @param {Number} transactionid The transaction ID of the AJAX request (unique) * @param {Object} response The response from the AJAX request. * @param {Object} args The arguments given to the request. * @returns {Boolean} */ hide : function(transactionid, response, args) { - var outcome = this.check_ajax_response(transactionid, response, args); + var outcome = this.checkAjaxResponse(transactionid, response, args); if (outcome === false) { - Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'core_course'); + Y.log('AJAX request to hide '+this.get('itemname')+' by outcome.', 'warn', 'moodle-course-management'); return false; } this.markHidden(); this.updated(); - Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'core_course'); + Y.log('Success: '+this.get('itemname')+' made hidden by AJAX.', 'info', 'moodle-course-management'); }, /** @@ -241,7 +241,7 @@ Item.prototype = { */ markHidden : function() { this.get('node').setAttribute('data-visible', '0'); - Y.log('Marked '+this.get('itemname')+' as hidden', 'note', 'core_course'); + Y.log('Marked '+this.get('itemname')+' as hidden', 'info', 'moodle-course-management'); return true; },