From af28ceb94572584ec6809cc33f3f9c5a4d2995fb Mon Sep 17 00:00:00 2001 From: EizEddin AlKatrib Date: Fri, 11 Nov 2016 14:48:31 +0000 Subject: [PATCH] MDL-42977 course: Fixed the default behaviour of the Collapse/Expand all link --- course/renderer.php | 7 +-- .../moodle-course-categoryexpander-debug.js | 62 +++++++++++++++++-- .../moodle-course-categoryexpander-min.js | 3 +- .../moodle-course-categoryexpander.js | 62 +++++++++++++++++-- .../categoryexpander/js/categoryexpander.js | 62 +++++++++++++++++-- 5 files changed, 178 insertions(+), 18 deletions(-) diff --git a/course/renderer.php b/course/renderer.php index 96d1690b79a..ff6065354d2 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -1506,14 +1506,11 @@ class core_course_renderer extends plugin_renderer_base { if ($coursecat->get_children_count()) { $classes = array( 'collapseexpand', - 'collapse-all', ); - if ($chelper->get_subcat_depth() == 1) { - $classes[] = 'disabled'; - } + // Only show the collapse/expand if there are children to expand. $content .= html_writer::start_tag('div', array('class' => 'collapsible-actions')); - $content .= html_writer::link('#', get_string('collapseall'), + $content .= html_writer::link('#', get_string('expandall'), array('class' => implode(' ', $classes))); $content .= html_writer::end_tag('div'); $this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle'); diff --git a/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-debug.js b/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-debug.js index 92f0c020fd7..e4f866cc737 100644 --- a/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-debug.js +++ b/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-debug.js @@ -25,10 +25,12 @@ var CSS = { HASCHILDREN: 'with_children' }, SELECTORS = { + WITHCHILDRENTREES: '.with_children', LOADEDTREES: '.with_children.loaded', CONTENTNODE: '.content', CATEGORYLISTENLINK: '.category .info .categoryname', CATEGORYSPINNERLOCATION: '.categoryname', + CATEGORYWITHCOLLAPSEDCHILDREN: '.category.with_children.collapsed', CATEGORYWITHCOLLAPSEDLOADEDCHILDREN: '.category.with_children.loaded.collapsed', CATEGORYWITHMAXIMISEDLOADEDCHILDREN: '.category.with_children.loaded:not(.collapsed)', COLLAPSEEXPAND: '.collapseexpand', @@ -82,6 +84,58 @@ NS.setup_keyboard_listeners = function() { Y.one(Y.config.doc).delegate('key', this.collapse_expand_all, 'enter', SELECTORS.COLLAPSEEXPAND, this); }; +/** + * Expand all categories. + * + * @method expand_category + * @private + * @param {Node} categorynode The node to expand + */ +NS.expand_category = function(categorynode) { + // Load the actual dependencies now that we've been called. + Y.use('io-base', 'json-parse', 'moodle-core-notification', 'anim-node-plugin', function() { + // Overload the expand_category with the _expand_category function to ensure that + // this function isn't called in the future, and call it for the first time. + NS.expand_category = NS._expand_category; + NS.expand_category(categorynode); + }); +}; + +NS._expand_category = function(categorynode) { + var categoryid, + depth; + + if (!categorynode.hasClass(CSS.HASCHILDREN)) { + // Nothing to do here - this category has no children. + return; + } + + if (categorynode.hasClass(CSS.LOADED)) { + // We've already loaded this content so we just need to toggle the view of it. + this.run_expansion(categorynode); + return; + } + + // We use Data attributes to store the category. + categoryid = categorynode.getData('categoryid'); + depth = categorynode.getData('depth'); + if (typeof categoryid === "undefined" || typeof depth === "undefined") { + return; + } + + this._toggle_generic_expansion({ + parentnode: categorynode, + childnode: categorynode.one(SELECTORS.CONTENTNODE), + spinnerhandle: SELECTORS.CATEGORYSPINNERLOCATION, + data: { + categoryid: categoryid, + depth: depth, + showcourses: categorynode.getData('showcourses'), + type: TYPE_CATEGORY + } + }); +}; + /** * Toggle the animation of the clicked category node. * @@ -310,12 +364,12 @@ NS._collapse_expand_all = function(e) { NS.expand_all = function(ancestor) { var finalexpansions = []; - ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN) + ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDCHILDREN) .each(function(c) { - if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)) { + if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDCHILDREN)) { // Expand the hidden children first without animation. c.removeClass(CSS.SECTIONCOLLAPSED); - c.all(SELECTORS.LOADEDTREES).removeClass(CSS.SECTIONCOLLAPSED); + c.all(SELECTORS.WITHCHILDRENTREES).removeClass(CSS.SECTIONCOLLAPSED); } else { finalexpansions.push(c); } @@ -323,7 +377,7 @@ NS.expand_all = function(ancestor) { // Run the final expansion with animation on the visible items. Y.all(finalexpansions).each(function(c) { - this.run_expansion(c); + this.expand_category(c); }, this); }; diff --git a/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-min.js b/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-min.js index 49f5138141a..f655b42ba88 100644 --- a/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-min.js +++ b/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander-min.js @@ -1 +1,2 @@ -YUI.add("moodle-course-categoryexpander",function(e,t){var n={CONTENTNODE:"content",COLLAPSEALL:"collapse-all",DISABLED:"disabled",LOADED:"loaded",NOTLOADED:"notloaded",SECTIONCOLLAPSED:"collapsed",HASCHILDREN:"with_children"},r={LOADEDTREES:".with_children.loaded",CONTENTNODE:".content",CATEGORYLISTENLINK:".category .info .categoryname",CATEGORYSPINNERLOCATION:".categoryname",CATEGORYWITHCOLLAPSEDLOADEDCHILDREN:".category.with_children.loaded.collapsed",CATEGORYWITHMAXIMISEDLOADEDCHILDREN:".category.with_children.loaded:not(.collapsed)",COLLAPSEEXPAND:".collapseexpand",COURSEBOX:".coursebox",COURSEBOXLISTENLINK:".coursebox .moreinfo",COURSEBOXSPINNERLOCATION:".coursename a",COURSECATEGORYTREE:".course_category_tree",PARENTWITHCHILDREN:".category"},i=e.namespace("Moodle.course.categoryexpander"),s=0,o=1,u=M.cfg.wwwroot+"/course/category.ajax.php";i.init=function(){var t=e.one(e.config.doc);t.delegate("click",this.toggle_category_expansion,r.CATEGORYLISTENLINK,this),t.delegate("click",this.toggle_coursebox_expansion,r.COURSEBOXLISTENLINK,this),t.delegate("click",this.collapse_expand_all,r.COLLAPSEEXPAND,this),t.once("key",this.setup_keyboard_listeners,"tab",this)},i.setup_keyboard_listeners=function(){var t=e.one(e.config.doc);t.all(r.CATEGORYLISTENLINK,r.COURSEBOXLISTENLINK,r.COLLAPSEEXPAND).setAttribute("tabindex","0"),e.one(e.config.doc).delegate("key",this.toggle_category_expansion,"enter",r.CATEGORYLISTENLINK,this),e.one(e.config.doc).delegate("key",this.toggle_coursebox_expansion,"enter",r.COURSEBOXLISTENLINK,this),e.one(e.config.doc).delegate("key",this.collapse_expand_all,"enter",r.COLLAPSEEXPAND,this)},i.toggle_category_expansion=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.toggle_category_expansion=i._toggle_category_expansion,i.toggle_category_expansion(t)})},i.toggle_coursebox_expansion=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.toggle_coursebox_expansion=i._toggle_coursebox_expansion,i.toggle_coursebox_expansion(t)}),t.preventDefault()},i._toggle_coursebox_expansion=function(e){var t;t=e.target.ancestor(r.COURSEBOX,!0),e.preventDefault();if(t.hasClass(n.LOADED)){this.run_expansion(t);return}this._toggle_generic_expansion({parentnode:t,childnode:t.one(r.CONTENTNODE),spinnerhandle:r.COURSEBOXSPINNERLOCATION,data:{courseid:t.getData("courseid"),type:o}})},i._toggle_category_expansion=function(e){var t,i,o;if(e.target.test("a")||e.target.test("img"))return;t=e.target.ancestor(r.PARENTWITHCHILDREN,!0);if(!t.hasClass(n.HASCHILDREN))return;if(t.hasClass(n.LOADED)){this.run_expansion(t);return}i=t.getData("categoryid"),o=t.getData("depth");if(typeof i=="undefined"||typeof o=="undefined")return;this._toggle_generic_expansion({parentnode:t,childnode:t.one(r.CONTENTNODE),spinnerhandle:r.CATEGORYSPINNERLOCATION,data:{categoryid:i,depth:o,showcourses:t.getData("showcourses"),type:s}})},i._toggle_generic_expansion=function(t){var n;t.spinnerhandle&&(n=M.util.add_spinner(e,t.parentnode.one(t.spinnerhandle)).show()),e.io(u,{method:"POST",context:this,on:{complete:this.process_results},data:t.data,arguments:{parentnode:t.parentnode,childnode:t.childnode,spinner:n}})},i.run_expansion=function(e){var t=e.one(r.CONTENTNODE),i=this,s=e.ancestor(r.COURSECATEGORYTREE);this.add_animation(t),e.hasClass(n.SECTIONCOLLAPSED)?(t.setStyle("height","0"),e.removeClass(n.SECTIONCOLLAPSED),e.setAttribute("aria-expanded","true"),t.fx.set("reverse",!1)):(t.fx.set("reverse",!0),t.fx.once("end",function(e,t){t.addClass(n.SECTIONCOLLAPSED),t.setAttribute("aria-expanded","false")},this,e)),t.fx.once("end",function(e,t){t.setStyles({height:"",opacity:""}),this.destroy(),i.update_collapsible_actions(s)},t.fx,t),t.fx.run()},i.collapse_expand_all=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.collapse_expand_all=i._collapse_expand_all,i.collapse_expand_all(t)}),t.preventDefault()},i._collapse_expand_all=function(e){e.preventDefault();if(e.currentTarget.hasClass(n.DISABLED))return;var t=e.currentTarget.ancestor(r.COURSECATEGORYTREE);if(!t)return;var i=t.one(r.COLLAPSEEXPAND);i.hasClass(n.COLLAPSEALL)?this.collapse_all(t):this.expand_all(t),this.update_collapsible_actions(t)},i.expand_all=function(t){var i=[];t.all(r.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN).each(function(e){e.ancestor(r.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)?(e.removeClass(n.SECTIONCOLLAPSED),e.all(r.LOADEDTREES).removeClass(n.SECTIONCOLLAPSED)):i.push(e)},this),e.all(i).each(function(e){this.run_expansion(e)},this)},i.collapse_all=function(t){var i=[];t.all(r.CATEGORYWITHMAXIMISEDLOADEDCHILDREN).each(function(e){e.ancestor(r.CATEGORYWITHMAXIMISEDLOADEDCHILDREN)?i.push(e):this.run_expansion(e)},this),e.all(i).each(function(e){e.addClass(n.SECTIONCOLLAPSED),e.all(r.LOADEDTREES).addClass(n.SECTIONCOLLAPSED)},this)},i.update_collapsible_actions=function(e){var t=!1,i=e.one(r.COLLAPSEEXPAND);if(!i)return;e.all(r.CATEGORYWITHMAXIMISEDLOADEDCHILDREN).each(function(e){return e.ancestor(r.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)?!1:(t=!0,!0)}),t?i.setHTML(M.util.get_string("collapseall","moodle")).addClass(n.COLLAPSEALL).removeClass(n.DISABLED):i.setHTML(M.util.get_string("expandall","moodle")).removeClass(n.COLLAPSEALL).removeClass(n.DISABLED)},i.process_results=function(t,r,i){var s,o;try{o=e.JSON.parse(r.responseText);if(o.error)return new M.core.ajaxException(o)}catch(u){return new M.core.exception(u)}s=e.Node.create(o),i.childnode.appendChild(s),i.parentnode.addClass(n.LOADED).removeClass(n.NOTLOADED),this.run_expansion(i.parentnode),i.spinner&&i.spinner.hide().destroy()},i.add_animation=function(t){return typeof t.fx!="undefined"?t:(t.plug(e.Plugin.NodeFX,{from:{height:0,opacity:0},to:{height:function(e){return e.get("scrollHeight")},opacity:1},duration:.2}),t)}},"@VERSION@",{requires:["node","event-key"]}); +YUI.add("moodle-course-categoryexpander",function(e,t){var n={CONTENTNODE:"content",COLLAPSEALL:"collapse-all",DISABLED:"disabled",LOADED:"loaded",NOTLOADED:"notloaded",SECTIONCOLLAPSED:"collapsed",HASCHILDREN:"with_children"},r={WITHCHILDRENTREES:".with_children",LOADEDTREES:".with_children.loaded",CONTENTNODE:".content",CATEGORYLISTENLINK:".category .info .categoryname",CATEGORYSPINNERLOCATION:".categoryname",CATEGORYWITHCOLLAPSEDCHILDREN:".category.with_children.collapsed",CATEGORYWITHCOLLAPSEDLOADEDCHILDREN:".category.with_children.loaded.collapsed",CATEGORYWITHMAXIMISEDLOADEDCHILDREN:".category.with_children.loaded:not(.collapsed)",COLLAPSEEXPAND:".collapseexpand",COURSEBOX:".coursebox",COURSEBOXLISTENLINK:".coursebox .moreinfo",COURSEBOXSPINNERLOCATION:".coursename a",COURSECATEGORYTREE:".course_category_tree",PARENTWITHCHILDREN:".category"},i=e.namespace("Moodle.course.categoryexpander"),s=0,o=1,u=M.cfg.wwwroot+"/course/category.ajax.php";i.init=function(){var t=e.one(e.config.doc);t.delegate("click",this.toggle_category_expansion,r.CATEGORYLISTENLINK,this),t.delegate("click",this.toggle_coursebox_expansion,r.COURSEBOXLISTENLINK,this),t.delegate("click",this.collapse_expand_all,r.COLLAPSEEXPAND,this),t.once("key",this.setup_keyboard_listeners,"tab",this)},i.setup_keyboard_listeners=function(){var t=e.one(e.config.doc);t.all(r.CATEGORYLISTENLINK,r.COURSEBOXLISTENLINK,r.COLLAPSEEXPAND).setAttribute("tabindex","0"),e.one(e.config.doc).delegate("key",this.toggle_category_expansion,"enter",r.CATEGORYLISTENLINK,this),e.one(e.config.doc).delegate("key",this.toggle_coursebox_expansion,"enter",r.COURSEBOXLISTENLINK,this),e.one(e.config.doc).delegate("key",this.collapse_expand_all,"enter",r.COLLAPSEEXPAND,this)},i.expand_category=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.expand_category=i._expand_category,i.expand_category(t)})},i._expand_category=function(e){var t,i;if(!e.hasClass(n.HASCHILDREN))return;if(e.hasClass(n.LOADED)){this.run_expansion(e);return}t=e.getData("categoryid"),i=e.getData("depth");if(typeof t=="undefined"||typeof i=="undefined")return;this._toggle_generic_expansion({parentnode:e,childnode:e.one(r.CONTENTNODE),spinnerhandle:r.CATEGORYSPINNERLOCATION,data:{categoryid:t,depth:i,showcourses:e.getData("showcourses"),type:s}})},i.toggle_category_expansion=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.toggle_category_expansion=i._toggle_category_expansion,i.toggle_category_expansion(t)})},i.toggle_coursebox_expansion=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.toggle_coursebox_expansion=i._toggle_coursebox_expansion,i.toggle_coursebox_expansion(t)}),t.preventDefault()},i._toggle_coursebox_expansion=function(e){var t;t=e.target.ancestor(r.COURSEBOX,!0),e.preventDefault();if(t.hasClass(n.LOADED)){this.run_expansion(t);return}this._toggle_generic_expansion({parentnode:t,childnode:t.one(r.CONTENTNODE),spinnerhandle:r.COURSEBOXSPINNERLOCATION,data:{courseid:t.getData("courseid"),type:o}})},i._toggle_category_expansion=function(e){var t,i,o;if(e.target.test("a")||e.target.test("img"))return;t=e.target.ancestor(r.PARENTWITHCHILDREN,!0);if(!t.hasClass(n.HASCHILDREN))return;if(t.hasClass(n.LOADED)){this.run_expansion(t);return}i=t.getData("categoryid"),o=t.getData("depth");if(typeof i=="undefined"||typeof o=="undefined")return;this._toggle_generic_expansion({parentnode:t,childnode:t.one(r.CONTENTNODE),spinnerhandle:r.CATEGORYSPINNERLOCATION,data:{categoryid:i,depth:o,showcourses:t.getData("showcourses"),type:s}})},i._toggle_generic_expansion=function(t){var n;t.spinnerhandle&&(n=M.util.add_spinner(e,t.parentnode.one(t.spinnerhandle)).show()),e.io(u,{method:"POST",context:this,on:{complete:this.process_results},data:t.data,arguments:{parentnode:t.parentnode,childnode:t.childnode,spinner:n}})},i.run_expansion=function(e){var t=e.one(r.CONTENTNODE),i=this,s=e.ancestor(r.COURSECATEGORYTREE);this.add_animation(t),e.hasClass(n.SECTIONCOLLAPSED)?(t.setStyle("height","0"),e.removeClass(n.SECTIONCOLLAPSED),e.setAttribute("aria-expanded","true"),t.fx.set("reverse",!1)):(t.fx.set("reverse",!0),t.fx.once("end",function(e,t){t.addClass(n.SECTIONCOLLAPSED),t.setAttribute("aria-expanded","false")},this,e)),t.fx.once("end",function(e,t){t.setStyles({height:"",opacity:""}),this.destroy(),i.update_collapsible_actions(s)},t.fx,t),t.fx.run()},i.collapse_expand_all=function(t){e.use("io-base","json-parse","moodle-core-notification","anim-node-plugin",function(){i.collapse_expand_all=i._collapse_expand_all,i.collapse_expand_all(t)}),t.preventDefault()},i._collapse_expand_all=function(e){e.preventDefault();if(e.currentTarget.hasClass(n.DISABLED))return;var t=e.currentTarget.ancestor(r.COURSECATEGORYTREE);if(!t)return;var i=t.one(r.COLLAPSEEXPAND);i.hasClass(n.COLLAPSEALL)?this.collapse_all(t):this.expand_all(t),this.update_collapsible_actions(t)},i.expand_all=function(t){var i=[];t.all(r.CATEGORYWITHCOLLAPSEDCHILDREN).each(function(e){e.ancestor(r.CATEGORYWITHCOLLAPSEDCHILDREN)?(e.removeClass(n.SECTIONCOLLAPSED),e.all(r.WITHCHILDRENTREES).removeClass(n.SECTIONCOLLAPSED)):i.push(e)},this),e.all(i).each(function(e){this.expand_category(e)},this)},i.collapse_all=function(t){var i=[];t.all(r.CATEGORYWITHMAXIMISEDLOADEDCHILDREN).each(function(e){e.ancestor(r.CATEGORYWITHMAXIMISEDLOADEDCHILDREN)?i.push(e):this.run_expansion(e)},this),e.all(i).each(function(e){e.addClass(n.SECTIONCOLLAPSED),e.all(r.LOADEDTREES).addClass(n.SECTIONCOLLAPSED)},this)},i.update_collapsible_actions=function(e){var t=!1,i=e.one(r.COLLAPSEEXPAND);if(!i)return;e.all(r.CATEGORYWITHMAXIMISEDLOADEDCHILDREN).each(function(e){return e.ancestor(r.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)?!1:(t=!0,!0)}),t?i.setHTML(M.util.get_string("collapseall","moodle")).addClass(n.COLLAPSEALL).removeClass(n.DISABLED):i.setHTML(M.util.get_string("expandall","moodle")).removeClass(n.COLLAPSEALL).removeClass +(n.DISABLED)},i.process_results=function(t,r,i){var s,o;try{o=e.JSON.parse(r.responseText);if(o.error)return new M.core.ajaxException(o)}catch(u){return new M.core.exception(u)}s=e.Node.create(o),i.childnode.appendChild(s),i.parentnode.addClass(n.LOADED).removeClass(n.NOTLOADED),this.run_expansion(i.parentnode),i.spinner&&i.spinner.hide().destroy()},i.add_animation=function(t){return typeof t.fx!="undefined"?t:(t.plug(e.Plugin.NodeFX,{from:{height:0,opacity:0},to:{height:function(e){return e.get("scrollHeight")},opacity:1},duration:.2}),t)}},"@VERSION@",{requires:["node","event-key"]}); diff --git a/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander.js b/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander.js index b7e1e9bdb99..38a5b34bdf3 100644 --- a/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander.js +++ b/course/yui/build/moodle-course-categoryexpander/moodle-course-categoryexpander.js @@ -25,10 +25,12 @@ var CSS = { HASCHILDREN: 'with_children' }, SELECTORS = { + WITHCHILDRENTREES: '.with_children', LOADEDTREES: '.with_children.loaded', CONTENTNODE: '.content', CATEGORYLISTENLINK: '.category .info .categoryname', CATEGORYSPINNERLOCATION: '.categoryname', + CATEGORYWITHCOLLAPSEDCHILDREN: '.category.with_children.collapsed', CATEGORYWITHCOLLAPSEDLOADEDCHILDREN: '.category.with_children.loaded.collapsed', CATEGORYWITHMAXIMISEDLOADEDCHILDREN: '.category.with_children.loaded:not(.collapsed)', COLLAPSEEXPAND: '.collapseexpand', @@ -81,6 +83,58 @@ NS.setup_keyboard_listeners = function() { Y.one(Y.config.doc).delegate('key', this.collapse_expand_all, 'enter', SELECTORS.COLLAPSEEXPAND, this); }; +/** + * Expand all categories. + * + * @method expand_category + * @private + * @param {Node} categorynode The node to expand + */ +NS.expand_category = function(categorynode) { + // Load the actual dependencies now that we've been called. + Y.use('io-base', 'json-parse', 'moodle-core-notification', 'anim-node-plugin', function() { + // Overload the expand_category with the _expand_category function to ensure that + // this function isn't called in the future, and call it for the first time. + NS.expand_category = NS._expand_category; + NS.expand_category(categorynode); + }); +}; + +NS._expand_category = function(categorynode) { + var categoryid, + depth; + + if (!categorynode.hasClass(CSS.HASCHILDREN)) { + // Nothing to do here - this category has no children. + return; + } + + if (categorynode.hasClass(CSS.LOADED)) { + // We've already loaded this content so we just need to toggle the view of it. + this.run_expansion(categorynode); + return; + } + + // We use Data attributes to store the category. + categoryid = categorynode.getData('categoryid'); + depth = categorynode.getData('depth'); + if (typeof categoryid === "undefined" || typeof depth === "undefined") { + return; + } + + this._toggle_generic_expansion({ + parentnode: categorynode, + childnode: categorynode.one(SELECTORS.CONTENTNODE), + spinnerhandle: SELECTORS.CATEGORYSPINNERLOCATION, + data: { + categoryid: categoryid, + depth: depth, + showcourses: categorynode.getData('showcourses'), + type: TYPE_CATEGORY + } + }); +}; + /** * Toggle the animation of the clicked category node. * @@ -309,12 +363,12 @@ NS._collapse_expand_all = function(e) { NS.expand_all = function(ancestor) { var finalexpansions = []; - ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN) + ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDCHILDREN) .each(function(c) { - if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)) { + if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDCHILDREN)) { // Expand the hidden children first without animation. c.removeClass(CSS.SECTIONCOLLAPSED); - c.all(SELECTORS.LOADEDTREES).removeClass(CSS.SECTIONCOLLAPSED); + c.all(SELECTORS.WITHCHILDRENTREES).removeClass(CSS.SECTIONCOLLAPSED); } else { finalexpansions.push(c); } @@ -322,7 +376,7 @@ NS.expand_all = function(ancestor) { // Run the final expansion with animation on the visible items. Y.all(finalexpansions).each(function(c) { - this.run_expansion(c); + this.expand_category(c); }, this); }; diff --git a/course/yui/src/categoryexpander/js/categoryexpander.js b/course/yui/src/categoryexpander/js/categoryexpander.js index a679c796088..2176497558b 100644 --- a/course/yui/src/categoryexpander/js/categoryexpander.js +++ b/course/yui/src/categoryexpander/js/categoryexpander.js @@ -23,10 +23,12 @@ var CSS = { HASCHILDREN: 'with_children' }, SELECTORS = { + WITHCHILDRENTREES: '.with_children', LOADEDTREES: '.with_children.loaded', CONTENTNODE: '.content', CATEGORYLISTENLINK: '.category .info .categoryname', CATEGORYSPINNERLOCATION: '.categoryname', + CATEGORYWITHCOLLAPSEDCHILDREN: '.category.with_children.collapsed', CATEGORYWITHCOLLAPSEDLOADEDCHILDREN: '.category.with_children.loaded.collapsed', CATEGORYWITHMAXIMISEDLOADEDCHILDREN: '.category.with_children.loaded:not(.collapsed)', COLLAPSEEXPAND: '.collapseexpand', @@ -80,6 +82,58 @@ NS.setup_keyboard_listeners = function() { Y.one(Y.config.doc).delegate('key', this.collapse_expand_all, 'enter', SELECTORS.COLLAPSEEXPAND, this); }; +/** + * Expand all categories. + * + * @method expand_category + * @private + * @param {Node} categorynode The node to expand + */ +NS.expand_category = function(categorynode) { + // Load the actual dependencies now that we've been called. + Y.use('io-base', 'json-parse', 'moodle-core-notification', 'anim-node-plugin', function() { + // Overload the expand_category with the _expand_category function to ensure that + // this function isn't called in the future, and call it for the first time. + NS.expand_category = NS._expand_category; + NS.expand_category(categorynode); + }); +}; + +NS._expand_category = function(categorynode) { + var categoryid, + depth; + + if (!categorynode.hasClass(CSS.HASCHILDREN)) { + // Nothing to do here - this category has no children. + return; + } + + if (categorynode.hasClass(CSS.LOADED)) { + // We've already loaded this content so we just need to toggle the view of it. + this.run_expansion(categorynode); + return; + } + + // We use Data attributes to store the category. + categoryid = categorynode.getData('categoryid'); + depth = categorynode.getData('depth'); + if (typeof categoryid === "undefined" || typeof depth === "undefined") { + return; + } + + this._toggle_generic_expansion({ + parentnode: categorynode, + childnode: categorynode.one(SELECTORS.CONTENTNODE), + spinnerhandle: SELECTORS.CATEGORYSPINNERLOCATION, + data: { + categoryid: categoryid, + depth: depth, + showcourses: categorynode.getData('showcourses'), + type: TYPE_CATEGORY + } + }); +}; + /** * Toggle the animation of the clicked category node. * @@ -308,12 +362,12 @@ NS._collapse_expand_all = function(e) { NS.expand_all = function(ancestor) { var finalexpansions = []; - ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN) + ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDCHILDREN) .each(function(c) { - if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)) { + if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDCHILDREN)) { // Expand the hidden children first without animation. c.removeClass(CSS.SECTIONCOLLAPSED); - c.all(SELECTORS.LOADEDTREES).removeClass(CSS.SECTIONCOLLAPSED); + c.all(SELECTORS.WITHCHILDRENTREES).removeClass(CSS.SECTIONCOLLAPSED); } else { finalexpansions.push(c); } @@ -321,7 +375,7 @@ NS.expand_all = function(ancestor) { // Run the final expansion with animation on the visible items. Y.all(finalexpansions).each(function(c) { - this.run_expansion(c); + this.expand_category(c); }, this); };