2 * Adds toggling of subcategory with automatic loading using AJAX.
4 * This also includes application of an animation to improve user experience.
6 * @module moodle-course-categoryexpander
10 * The course category expander.
13 * @class Y.Moodle.course.categoryexpander
17 CONTENTNODE: 'content',
18 COLLAPSEALL: 'collapse-all',
21 NOTLOADED: 'notloaded',
22 SECTIONCOLLAPSED: 'collapsed',
23 HASCHILDREN: 'with_children'
26 LOADEDTREES: '.with_children.loaded',
27 CONTENTNODE: '.content',
28 CATEGORYLISTENLINK: '.category .info .name',
29 CATEGORYSPINNERLOCATION: '.name',
30 CATEGORYWITHCOLLAPSEDLOADEDCHILDREN: '.category.with_children.loaded.collapsed',
31 CATEGORYWITHMAXIMISEDLOADEDCHILDREN: '.category.with_children.loaded:not(.collapsed)',
32 COLLAPSEEXPAND: '.collapseexpand',
33 COURSEBOX: '.coursebox',
34 COURSEBOXLISTENLINK: '.coursebox .moreinfo',
35 COURSEBOXSPINNERLOCATION: '.name a',
36 COURSECATEGORYTREE: '.course_category_tree',
37 PARENTWITHCHILDREN: '.category'
39 NS = Y.namespace('Moodle.course.categoryexpander'),
42 URL = M.cfg.wwwroot + '/course/category.ajax.php';
45 * Set up the category expander.
47 * No arguments are required.
51 NS.init = function() {
52 var doc = Y.one(Y.config.doc);
53 doc.delegate('click', this.toggle_category_expansion, SELECTORS.CATEGORYLISTENLINK, this);
54 doc.delegate('click', this.toggle_coursebox_expansion, SELECTORS.COURSEBOXLISTENLINK, this);
55 doc.delegate('click', this.collapse_expand_all, SELECTORS.COLLAPSEEXPAND, this);
57 // Only set up they keybaord listeners when tab is first pressed - it
58 // may never happen and modifying the DOM on a large number of nodes
59 // can be very expensive.
60 doc.once('key', this.setup_keyboard_listeners, 'tab', this);
64 * Set up keyboard expansion for course content.
66 * This includes setting up the delegation but also adding the nodes to the
69 * @method setup_keyboard_listeners
71 NS.setup_keyboard_listeners = function() {
72 var doc = Y.one(Y.config.doc);
74 Y.log('Setting the tabindex for all expandable course nodes', 'info', 'moodle-course-categoryexpander');
75 doc.all(SELECTORS.CATEGORYLISTENLINK, SELECTORS.COURSEBOXLISTENLINK, SELECTORS.COLLAPSEEXPAND).setAttribute('tabindex', '0');
78 Y.one(Y.config.doc).delegate('key', this.toggle_category_expansion, 'enter', SELECTORS.CATEGORYLISTENLINK, this);
79 Y.one(Y.config.doc).delegate('key', this.toggle_coursebox_expansion, 'enter', SELECTORS.COURSEBOXLISTENLINK, this);
80 Y.one(Y.config.doc).delegate('key', this.collapse_expand_all, 'enter', SELECTORS.COLLAPSEEXPAND, this);
84 * Toggle the animation of the clicked category node.
86 * @method toggle_category_expansion
88 * @param {EventFacade} e
90 NS.toggle_category_expansion = function(e) {
91 // Load the actual dependencies now that we've been called.
92 Y.use('io-base', 'json-parse', 'moodle-core-notification', 'anim', function() {
93 // Overload the toggle_category_expansion with the _toggle_category_expansion function to ensure that
94 // this function isn't called in the future, and call it for the first time.
95 NS.toggle_category_expansion = NS._toggle_category_expansion;
96 NS.toggle_category_expansion(e);
101 * Toggle the animation of the clicked coursebox node.
103 * @method toggle_coursebox_expansion
105 * @param {EventFacade} e
107 NS.toggle_coursebox_expansion = function(e) {
108 // Load the actual dependencies now that we've been called.
109 Y.use('io-base', 'json-parse', 'moodle-core-notification', 'anim', function() {
110 // Overload the toggle_coursebox_expansion with the _toggle_coursebox_expansion function to ensure that
111 // this function isn't called in the future, and call it for the first time.
112 NS.toggle_coursebox_expansion = NS._toggle_coursebox_expansion;
113 NS.toggle_coursebox_expansion(e);
119 NS._toggle_coursebox_expansion = function(e) {
122 // Grab the parent category container - this is where the new content will be added.
123 courseboxnode = e.target.ancestor(SELECTORS.COURSEBOX, true);
126 if (courseboxnode.hasClass(CSS.LOADED)) {
127 // We've already loaded this content so we just need to toggle the view of it.
128 this.run_expansion(courseboxnode);
132 this._toggle_generic_expansion({
133 parentnode: courseboxnode,
134 childnode: courseboxnode.one(SELECTORS.CONTENTNODE),
135 spinnerhandle: SELECTORS.COURSEBOXSPINNERLOCATION,
137 courseid: courseboxnode.getData('courseid'),
143 NS._toggle_category_expansion = function(e) {
148 if (e.target.test('a') || e.target.test('img')) {
149 // Return early if either an anchor or an image were clicked.
153 // Grab the parent category container - this is where the new content will be added.
154 categorynode = e.target.ancestor(SELECTORS.PARENTWITHCHILDREN, true);
156 if (!categorynode.hasClass(CSS.HASCHILDREN)) {
157 // Nothing to do here - this category has no children.
161 if (categorynode.hasClass(CSS.LOADED)) {
162 // We've already loaded this content so we just need to toggle the view of it.
163 this.run_expansion(categorynode);
167 // We use Data attributes to store the category.
168 categoryid = categorynode.getData('categoryid');
169 depth = categorynode.getData('depth');
170 if (typeof categoryid === "undefined" || typeof depth === "undefined") {
174 this._toggle_generic_expansion({
175 parentnode: categorynode,
176 childnode: categorynode.one(SELECTORS.CONTENTNODE),
177 spinnerhandle: SELECTORS.CATEGORYSPINNERLOCATION,
179 categoryid: categoryid,
181 showcourses: categorynode.getData('showcourses'),
188 * Wrapper function to handle toggling of generic types.
190 * @method _toggle_generic_expansion
192 * @param {Object} config
194 NS._toggle_generic_expansion = function(config) {
195 if (config.spinnerhandle) {
196 // Add a spinner to give some feedback to the user.
197 spinner = M.util.add_spinner(Y, config.parentnode.one(config.spinnerhandle)).show();
205 complete: this.process_results
209 parentnode: config.parentnode,
210 childnode: config.childnode,
217 * Apply the animation on the supplied node.
219 * @method run_expansion
221 * @param {Node} categorynode The node to apply the animation to
223 NS.run_expansion = function(categorynode) {
224 var categorychildren = categorynode.one(SELECTORS.CONTENTNODE),
226 ancestor = categorynode.ancestor(SELECTORS.COURSECATEGORYTREE);
228 // Add our animation to the categorychildren.
229 this.add_animation(categorychildren);
232 // If we already have the class, remove it before showing otherwise we perform the
233 // animation whilst the node is hidden.
234 if (categorynode.hasClass(CSS.SECTIONCOLLAPSED)) {
235 // To avoid a jump effect, we need to set the height of the children to 0 here before removing the SECTIONCOLLAPSED class.
236 categorychildren.setStyle('height', '0');
237 categorynode.removeClass(CSS.SECTIONCOLLAPSED);
238 categorynode.setAttribute('aria-expanded', 'true');
239 categorychildren.fx.set('reverse', false);
241 categorychildren.fx.set('reverse', true);
242 categorychildren.fx.once('end', function(e, categorynode) {
243 categorynode.addClass(CSS.SECTIONCOLLAPSED);
244 categorynode.setAttribute('aria-expanded', 'false');
245 }, this, categorynode);
248 categorychildren.fx.once('end', function(e, categorychildren) {
249 // Remove the styles that the animation has set.
250 categorychildren.setStyles({
255 // To avoid memory gobbling, remove the animation. It will be added back if called again.
257 self.update_collapsible_actions(ancestor);
258 }, categorychildren.fx, categorychildren);
260 // Now that everything has been set up, run the animation.
261 categorychildren.fx.run();
264 NS.collapse_expand_all = function(e) {
265 // The collapse/expand button has no actual target but we need to prevent it's default
266 // action to ensure we don't make the page reload/jump.
269 if (e.currentTarget.hasClass(CSS.DISABLED)) {
270 // The collapse/expand is currently disabled.
274 var ancestor = e.currentTarget.ancestor(SELECTORS.COURSECATEGORYTREE);
279 var collapseall = ancestor.one(SELECTORS.COLLAPSEEXPAND);
280 if (collapseall.hasClass(CSS.COLLAPSEALL)) {
281 this.collapse_all(ancestor);
283 this.expand_all(ancestor);
285 this.update_collapsible_actions(ancestor);
288 NS.expand_all = function(ancestor) {
289 var finalexpansions = [];
291 ancestor.all(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)
293 if (c.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)) {
294 // Expand the hidden children first without animation.
295 c.removeClass(CSS.SECTIONCOLLAPSED);
296 c.all(SELECTORS.LOADEDTREES).removeClass(CSS.SECTIONCOLLAPSED);
298 finalexpansions.push(c);
302 // Run the final expansion with animation on the visible items.
303 Y.all(finalexpansions).each(function(c) {
304 this.run_expansion(c);
309 NS.collapse_all = function(ancestor) {
310 var finalcollapses = [];
312 ancestor.all(SELECTORS.CATEGORYWITHMAXIMISEDLOADEDCHILDREN)
314 if (c.ancestor(SELECTORS.CATEGORYWITHMAXIMISEDLOADEDCHILDREN)) {
315 finalcollapses.push(c);
317 // Collapse the visible items first
318 this.run_expansion(c);
322 // Run the final collapses now that the these are hidden hidden.
323 Y.all(finalcollapses).each(function(c) {
324 c.addClass(CSS.SECTIONCOLLAPSED);
325 c.all(SELECTORS.LOADEDTREES).addClass(CSS.SECTIONCOLLAPSED);
329 NS.update_collapsible_actions = function(ancestor) {
330 var foundmaximisedchildren = false,
331 // Grab the anchor for the collapseexpand all link.
332 togglelink = ancestor.one(SELECTORS.COLLAPSEEXPAND);
335 // We should always have a togglelink but ensure.
339 // Search for any visibly expanded children.
340 ancestor.all(SELECTORS.CATEGORYWITHMAXIMISEDLOADEDCHILDREN).each(function(n) {
341 // If we can find any collapsed ancestors, skip.
342 if (n.ancestor(SELECTORS.CATEGORYWITHCOLLAPSEDLOADEDCHILDREN)) {
345 foundmaximisedchildren = true;
349 if (foundmaximisedchildren) {
350 // At least one maximised child found. Show the collapseall.
351 togglelink.setHTML(M.util.get_string('collapseall', 'moodle'))
352 .addClass(CSS.COLLAPSEALL)
353 .removeClass(CSS.DISABLED);
355 // No maximised children found but there are collapsed children. Show the expandall.
356 togglelink.setHTML(M.util.get_string('expandall', 'moodle'))
357 .removeClass(CSS.COLLAPSEALL)
358 .removeClass(CSS.DISABLED);
363 * Process the data returned by Y.io.
364 * This includes appending it to the relevant part of the DOM, and applying our animations.
366 * @method process_results
368 * @param {String} tid The Transaction ID
369 * @param {Object} response The Reponse returned by Y.IO
370 * @param {Object} ioargs The additional arguments provided by Y.IO
372 NS.process_results = function(tid, response, args) {
376 data = Y.JSON.parse(response.responseText);
378 return new M.core.ajaxException(data);
381 return new M.core.exception(e);
384 // Insert the returned data into a new Node.
385 newnode = Y.Node.create(data);
387 // Append to the existing child location.
388 args.childnode.appendChild(newnode);
390 // Now that we have content, we can swap the classes on the toggled container.
392 .addClass(CSS.LOADED)
393 .removeClass(CSS.NOTLOADED);
395 // Toggle the open/close status of the node now that it's content has been loaded.
396 this.run_expansion(args.parentnode);
398 // Remove the spinner now that we've started to show the content.
400 args.spinner.hide().destroy();
405 * Add our animation to the Node.
407 * @method add_animation
409 * @param {Node} childnode
411 NS.add_animation = function(childnode) {
412 if (typeof childnode.fx !== "undefined") {
413 // The animation has already been plugged to this node.
417 childnode.plug(Y.Plugin.NodeFX, {
423 // This sets a dynamic height in case the node content changes.
424 height: function(node) {
425 // Get expanded height (offsetHeight may be zero).
426 return node.get('scrollHeight');