MDL-39910 mod_scorm: fix enable/disable navigation buttons
[moodle.git] / mod / scorm / module.js
1 // This file is part of Moodle - http://moodle.org/
2 //
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16 /**
17  * Javascript helper function for SCORM module.
18  *
19  * @package   mod-scorm
20  * @copyright 2009 Petr Skoda (http://skodak.org)
21  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 mod_scorm_launch_next_sco = null;
25 mod_scorm_launch_prev_sco = null;
26 mod_scorm_activate_item = null;
27 mod_scorm_parse_toc_tree = null;
28 scorm_layout_widget = null;
30 M.mod_scorm = {};
32 M.mod_scorm.init = function(Y, nav_display, navposition_left, navposition_top, hide_toc, collapsetocwinsize, toc_title, window_name, launch_sco, scoes_nav) {
33     var scorm_disable_toc = false;
34     var scorm_hide_nav = true;
35     var scorm_hide_toc = true;
36     if (hide_toc == 0) {
37         if (nav_display !== 0) {
38             scorm_hide_nav = false;
39         }
40         scorm_hide_toc = false;
41     } else if (hide_toc == 3) {
42         scorm_disable_toc = true;
43     }
45     scoes_nav = Y.JSON.parse(scoes_nav);
46     var scorm_current_node;
47     var scorm_buttons = [];
48     var scorm_bloody_labelclick = false;
49     var scorm_nav_panel;
51     Y.use('button', 'dd-plugin', 'panel', 'resize', 'gallery-sm-treeview', function(Y) {
53         Y.TreeView.prototype.getNodeByAttribute = function(attribute, value) {
54             var node = null,
55                 domnode = Y.one('a[' + attribute + '="' + value + '"]');
56             if (domnode !== null) {
57                 node = scorm_tree_node.getNodeById(domnode.ancestor('li').get('id'));
58             }
59             return node;
60         };
62         Y.TreeView.prototype.openAll = function () {
63             var tree = this;
64             Y.all('.yui3-treeview-can-have-children').each(function() {
65                 var node = tree.getNodeById(this.get('id'));
66                 node.open();
67             });
68         };
70         // TODO: Remove next(), previous() prototype functions after YUI has been updated to 3.11.0 - MDL-41208.
71         Y.Tree.Node.prototype.next = function () {
72             if (this.parent) {
73                 return this.parent.children[this.index() + 1];
74             }
75         };
77         Y.Tree.Node.prototype.previous = function () {
78             if (this.parent) {
79                 return this.parent.children[this.index() - 1];
80             }
81         };
83         var scorm_parse_toc_tree = function(srcNode) {
84             var SELECTORS = {
85                     child: '> li',
86                     label: '> li, > a',
87                     textlabel : '> li, > span',
88                     subtree: '> ul, > li'
89                 },
90                 children = [];
92             srcNode.all(SELECTORS.child).each(function(childNode) {
93                 var child = {},
94                     labelNode = childNode.one(SELECTORS.label),
95                     textNode = childNode.one(SELECTORS.textlabel),
96                     subTreeNode = childNode.one(SELECTORS.subtree);
98                 if (labelNode) {
99                     var title = labelNode.getAttribute('title');
100                     var scoid = labelNode.getData('scoid');
101                     child.label = labelNode.get('outerHTML');
102                     // Will be good to change to url instead of title.
103                     if (title && title !== '#') {
104                         child.title = title;
105                     }
106                     if (typeof scoid !== 'undefined') {
107                         child.scoid = scoid;
108                     }
109                 } else if (textNode) {
110                     // The selector did not find a label node with anchor.
111                     child.label = textNode.get('outerHTML');
112                 }
114                 if (subTreeNode) {
115                     child.children = scorm_parse_toc_tree(subTreeNode);
116                 }
118                 children.push(child);
119             });
121             return children;
122         };
124         mod_scorm_parse_toc_tree = scorm_parse_toc_tree;
126         var scorm_activate_item = function(node) {
127             if (!node) {
128                 return;
129             }
130             // Check if the item is already active, avoid recursive calls.
131             if (Y.one('#scorm_object')) {
132                 var scorm_active_url = Y.one('#scorm_object').getAttribute('src');
133                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
134                 if (node_full_url === scorm_active_url) {
135                     return;
136                 }
137             }
138             scorm_current_node = node;
139             // Avoid recursive calls.
140             if (!scorm_current_node.state.selected) {
141                 scorm_current_node.select();
142             }
144             // remove any reference to the old API
145             if (window.API) {
146                 window.API = null;
147             }
148             if (window.API_1484_11) {
149                 window.API_1484_11 = null;
150             }
151             var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
152             var el_old_api = document.getElementById('scormapi123');
153             if (el_old_api) {
154                 el_old_api.parentNode.removeChild(el_old_api);
155             }
157             if (node.title) {
158                 var el_scorm_api = document.getElementById("external-scormapi");
159                 el_scorm_api.parentNode.removeChild(el_scorm_api);
160                 el_scorm_api = document.createElement('script');
161                 el_scorm_api.setAttribute('id','external-scormapi');
162                 el_scorm_api.setAttribute('type','text/javascript');
163                 var pel_scorm_api = document.getElementById('scormapi-parent');
164                 pel_scorm_api.appendChild(el_scorm_api);
165                 var api_url = M.cfg.wwwroot + '/mod/scorm/loaddatamodel.php?' + node.title;
166                 document.getElementById('external-scormapi').src = api_url;
167             }
169             var content = Y.one('#scorm_content');
170             var obj = document.createElement('iframe');
171             obj.setAttribute('id', 'scorm_object');
172             obj.setAttribute('type', 'text/html');
173             if (!window_name && node.title != null) {
174                 obj.setAttribute('src', url_prefix + node.title);
175             }
176             if (window_name) {
177                 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
178                 if(! mine) {
179                     alert(M.str.scorm.popupsblocked);
180                 }
181                 mine.close();
182             }
184             var old = Y.one('#scorm_object');
185             if (old) {
186                 if(window_name) {
187                     var cwidth = scormplayerdata.cwidth;
188                     var cheight = scormplayerdata.cheight;
189                     var poptions = scormplayerdata.popupoptions;
190                     scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
191                 } else {
192                     content.replaceChild(obj, old);
193                 }
194             } else {
195                 content.prepend(obj);
196             }
198             if (scorm_hide_nav == false) {
199                 if (nav_display === 1 && navposition_left > 0 && navposition_top > 0) {
200                     Y.one('#scorm_object').addClass(cssclasses.scorm_nav_under_content);
201                 }
202                 scorm_fixnav();
203             }
204         };
206         mod_scorm_activate_item = scorm_activate_item;
208         /**
209          * Enables/disables navigation buttons as needed.
210          * @return void
211          */
212         var scorm_fixnav = function() {
213             var skipprevnode = scorm_skipprev(scorm_current_node);
214             var prevnode = scorm_prev(scorm_current_node);
215             var skipnextnode = scorm_skipnext(scorm_current_node);
216             var nextnode = scorm_next(scorm_current_node);
217             var upnode = scorm_up(scorm_current_node);
219             scorm_buttons[0].set('disabled', ((skipprevnode === null) ||
220                         (typeof(skipprevnode.scoid) === 'undefined') ||
221                         (scoes_nav[skipprevnode.scoid].isvisible === "false") ||
222                         (skipprevnode.title === null) ||
223                         (scoes_nav[launch_sco].hideprevious === 1)));
225             scorm_buttons[1].set('disabled', ((prevnode === null) ||
226                         (typeof(prevnode.scoid) === 'undefined') ||
227                         (scoes_nav[prevnode.scoid].isvisible === "false") ||
228                         (prevnode.title === null) ||
229                         (scoes_nav[launch_sco].hideprevious === 1)));
231             scorm_buttons[2].set('disabled', (upnode === null) ||
232                         (typeof(upnode.scoid) === 'undefined') ||
233                         (scoes_nav[upnode.scoid].isvisible === "false") ||
234                         (upnode.title === null));
236             scorm_buttons[3].set('disabled', ((nextnode === null) ||
237                         ((nextnode.title === null) && (scoes_nav[launch_sco].flow !== 1)) ||
238                         (typeof(nextnode.scoid) === 'undefined') ||
239                         (scoes_nav[nextnode.scoid].isvisible === "false") ||
240                         (scoes_nav[launch_sco].hidecontinue === 1)));
242             scorm_buttons[4].set('disabled', ((skipnextnode === null) ||
243                         (skipnextnode.title === null) ||
244                         (typeof(skipnextnode.scoid) === 'undefined') ||
245                         (scoes_nav[skipnextnode.scoid].isvisible === "false") ||
246                         scoes_nav[launch_sco].hidecontinue === 1));
247         };
249         var scorm_toggle_toc = function(windowresize) {
250             var toc = Y.one('#scorm_toc');
251             var scorm_content = Y.one('#scorm_content');
252             var scorm_toc_toggle_btn = Y.one('#scorm_toc_toggle_btn');
253             var toc_disabled = toc.hasClass('disabled');
254             var disabled_by = toc.getAttribute('disabled-by');
255             // Remove width element style from resize handle.
256             toc.setStyle('width', null);
257             scorm_content.setStyle('width', null);
258             if (windowresize === true) {
259                 if (disabled_by === 'user') {
260                     return;
261                 }
262                 var body = Y.one('body');
263                 if (body.get('winWidth') < collapsetocwinsize) {
264                     toc.addClass(cssclasses.disabled)
265                         .setAttribute('disabled-by', 'screen-size');
266                     scorm_toc_toggle_btn.setHTML('&gt;')
267                         .set('title', M.util.get_string('show', 'moodle'));
268                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
269                         .addClass(cssclasses.scorm_grid_content_toc_hidden);
270                 } else if (body.get('winWidth') > collapsetocwinsize) {
271                     toc.removeClass(cssclasses.disabled)
272                         .removeAttribute('disabled-by');
273                     scorm_toc_toggle_btn.setHTML('&lt;')
274                         .set('title', M.util.get_string('hide', 'moodle'));
275                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
276                         .addClass(cssclasses.scorm_grid_content_toc_visible);
277                 }
278                 return;
279             }
280             if (toc_disabled) {
281                 toc.removeClass(cssclasses.disabled)
282                     .removeAttribute('disabled-by');
283                 scorm_toc_toggle_btn.setHTML('&lt;')
284                     .set('title', M.util.get_string('hide', 'moodle'));
285                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
286                     .addClass(cssclasses.scorm_grid_content_toc_visible);
287             } else {
288                 toc.addClass(cssclasses.disabled)
289                     .setAttribute('disabled-by', 'user');
290                 scorm_toc_toggle_btn.setHTML('&gt;')
291                     .set('title', M.util.get_string('show', 'moodle'));
292                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
293                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
294             }
295         };
297         var scorm_resize_layout = function() {
298             if (window_name) {
299                 return;
300             }
302             // make sure that the max width of the TOC doesn't go to far
304             var scorm_toc_node = Y.one('#scorm_toc');
305             var maxwidth = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
306             scorm_toc_node.setStyle('maxWidth', (maxwidth - 200));
307             var cwidth = parseInt(scorm_toc_node.getComputedStyle('width'), 10);
308             if (cwidth > (maxwidth - 1)) {
309                 scorm_toc_node.setStyle('width', (maxwidth - 50));
310             }
312             // Calculate the rough new height from the viewport height.
313             newheight = Y.one('body').get('winHeight') -5;
314             if (newheight < 600) {
315                 newheight = 600;
316             }
317             Y.one('#scorm_layout').setStyle('height', newheight);
319         };
321         // Handle AJAX Request
322         var scorm_ajax_request = function(url, datastring) {
323             var myRequest = NewHttpReq();
324             var result = DoRequest(myRequest, url + datastring);
325             return result;
326         };
328         var scorm_up = function(node, update_launch_sco) {
329             if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
330                 var parentscoid = scoes_nav[launch_sco].parentscoid;
331                 var parent = node.parent;
332                 if (parent.title !== scoes_nav[parentscoid].url) {
333                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
334                     if (parent === null) {
335                         parent = scorm_tree_node.rootNode.children[0];
336                         parent.title = scoes_nav[parentscoid].url;
337                     }
338                 }
339                 if (update_launch_sco) {
340                     launch_sco = parentscoid;
341                 }
342                 return parent;
343             }
344             return null;
345         };
347         var scorm_lastchild = function(node) {
348             if (node.children.length) {
349                 return scorm_lastchild(node.children[node.children.length-1]);
350             } else {
351                 return node;
352             }
353         };
355         var scorm_prev = function(node, update_launch_sco) {
356             if (node.previous() && node.previous().children.length &&
357                     typeof scoes_nav[launch_sco].prevscoid !== 'undefined') {
358                 node = scorm_lastchild(node.previous());
359                 if (node) {
360                     var prevscoid = scoes_nav[launch_sco].prevscoid;
361                     if (node.title !== scoes_nav[prevscoid].url) {
362                         node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevscoid].url);
363                         if (node === null) {
364                             node = scorm_tree_node.rootNode.children[0];
365                             node.title = scoes_nav[prevscoid].url;
366                         }
367                     }
368                     if (update_launch_sco) {
369                         launch_sco = prevscoid;
370                     }
371                     return node;
372                 } else {
373                     return null;
374                 }
375             }
376             return scorm_skipprev(node, update_launch_sco);
377         };
379         var scorm_skipprev = function(node, update_launch_sco) {
380             if (node.previous() && typeof scoes_nav[launch_sco].prevsibling !== 'undefined') {
381                 var prevsibling = scoes_nav[launch_sco].prevsibling;
382                 var previous = node.previous();
383                 var prevscoid = scoes_nav[launch_sco].prevscoid;
384                 if (previous.title !== scoes_nav[prevscoid].url) {
385                     previous = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevsibling].url);
386                     if (previous === null) {
387                         previous = scorm_tree_node.rootNode.children[0];
388                         previous.title = scoes_nav[prevsibling].url;
389                     }
390                 }
391                 if (update_launch_sco) {
392                     launch_sco = prevsibling;
393                 }
394                 return previous;
395             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
396                 var parentscoid = scoes_nav[launch_sco].parentscoid;
397                 var parent = node.parent;
398                 if (parent.title !== scoes_nav[parentscoid].url) {
399                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
400                     if (parent === null) {
401                         parent = scorm_tree_node.rootNode.children[0];
402                         parent.title = scoes_nav[parentscoid].url;
403                     }
404                 }
405                 if (update_launch_sco) {
406                     launch_sco = parentscoid;
407                 }
408                 return parent;
409             }
410             return null;
411         };
413         var scorm_next = function(node, update_launch_sco) {
414             if (node === false) {
415                 return scorm_tree_node.children[0];
416             }
417             if (node.children.length && typeof scoes_nav[launch_sco].nextscoid != 'undefined') {
418                 node = node.children[0];
419                 var nextscoid = scoes_nav[launch_sco].nextscoid;
420                 if (node.title !== scoes_nav[nextscoid].url) {
421                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextscoid].url);
422                     if (node === null) {
423                         node = scorm_tree_node.rootNode.children[0];
424                         node.title = scoes_nav[nextscoid].url;
425                     }
426                 }
427                 if (update_launch_sco) {
428                     launch_sco = nextscoid;
429                 }
430                 return node;
431             }
432             return scorm_skipnext(node, update_launch_sco);
433         };
435         var scorm_skipnext = function(node, update_launch_sco) {
436             var next = node.next();
437             if (next && next.title && typeof scoes_nav[launch_sco] !== 'undefined' && typeof scoes_nav[launch_sco].nextsibling !== 'undefined') {
438                 var nextsibling = scoes_nav[launch_sco].nextsibling;
439                 if (next.title !== scoes_nav[nextsibling].url) {
440                     next = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextsibling].url);
441                     if (next === null) {
442                         next = scorm_tree_node.rootNode.children[0];
443                         next.title = scoes_nav[nextsibling].url;
444                     }
445                 }
446                 if (update_launch_sco) {
447                     launch_sco = nextsibling;
448                 }
449                 return next;
450             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
451                 var parentscoid = scoes_nav[launch_sco].parentscoid;
452                 var parent = node.parent;
453                 if (parent.title !== scoes_nav[parentscoid].url) {
454                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
455                     if (parent === null) {
456                         parent = scorm_tree_node.rootNode.children[0];
457                     }
458                 }
459                 if (update_launch_sco) {
460                     launch_sco = parentscoid;
461                 }
462                 return scorm_skipnext(parent, update_launch_sco);
463             }
464             return null;
465         };
467         // Launch prev sco
468         var scorm_launch_prev_sco = function() {
469             var result = null;
470             if (scoes_nav[launch_sco].flow === 1) {
471                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=backward';
472                 result = scorm_ajax_request(M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?', datastring);
473                 mod_scorm_seq = encodeURIComponent(result);
474                 result = Y.JSON.parse (result);
475                 if (typeof result.nextactivity.id != undefined) {
476                         var node = scorm_prev(scorm_tree_node.getSelectedNodes()[0]);
477                         if (node == null) {
478                             // Avoid use of TreeView for Navigation.
479                             node = scorm_tree_node.getSelectedNodes()[0];
480                         }
481                         if (node.title !== scoes_nav[result.nextactivity.id].url) {
482                             node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
483                             if (node === null) {
484                                 node = scorm_tree_node.rootNode.children[0];
485                                 node.title = scoes_nav[result.nextactivity.id].url;
486                             }
487                         }
488                         launch_sco = result.nextactivity.id;
489                         scorm_activate_item(node);
490                         scorm_fixnav();
491                 } else {
492                         scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
493                 }
494             } else {
495                 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
496             }
497         };
499         // Launch next sco
500         var scorm_launch_next_sco = function () {
501             var result = null;
502             if (scoes_nav[launch_sco].flow === 1) {
503                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=forward';
504                 result = scorm_ajax_request(M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?', datastring);
505                 mod_scorm_seq = encodeURIComponent(result);
506                 result = Y.JSON.parse (result);
507                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
508                     var node = scorm_next(scorm_tree_node.getSelectedNodes()[0]);
509                     if (node === null) {
510                         // Avoid use of TreeView for Navigation.
511                         node = scorm_tree_node.getSelectedNodes()[0];
512                     }
513                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
514                     if (node === null) {
515                         node = scorm_tree_node.rootNode.children[0];
516                         node.title = scoes_nav[result.nextactivity.id].url;
517                     }
518                     launch_sco = result.nextactivity.id;
519                     scorm_activate_item(node);
520                     scorm_fixnav();
521                 } else {
522                     scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
523                 }
524             } else {
525                 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
526             }
527         };
529         mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
530         mod_scorm_launch_next_sco = scorm_launch_next_sco;
532         var cssclasses = {
533                 // YUI grid class: use 100% of the available width to show only content, TOC hidden.
534                 scorm_grid_content_toc_hidden: 'yui3-u-1',
535                 // YUI grid class: use 1/5 of the available width to show TOC.
536                 scorm_grid_toc: 'yui3-u-1-5',
537                 // YUI grid class: use 1/24 of the available width to show TOC toggle button.
538                 scorm_grid_toggle: 'yui3-u-1-24',
539                 // YUI grid class: use 3/4 of the available width to show content, TOC visible.
540                 scorm_grid_content_toc_visible: 'yui3-u-3-4',
541                 // Reduce height of #scorm_object to accomodate nav buttons under content.
542                 scorm_nav_under_content: 'scorm_nav_under_content',
543                 disabled: 'disabled'
544             };
545         // layout
546         Y.one('#scorm_toc_title').setHTML(toc_title);
548         if (scorm_disable_toc) {
549             Y.one('#scorm_toc').addClass(cssclasses.disabled);
550             Y.one('#scorm_toc_toggle').addClass(cssclasses.disabled);
551             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_hidden);
552         } else {
553             Y.one('#scorm_toc').addClass(cssclasses.scorm_grid_toc);
554             Y.one('#scorm_toc_toggle').addClass(cssclasses.scorm_grid_toggle);
555             Y.one('#scorm_toc_toggle_btn')
556                 .setHTML('&lt;')
557                 .setAttribute('title', M.util.get_string('hide', 'moodle'));
558             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_visible);
559             scorm_toggle_toc(true);
560         }
562         // hide the TOC if that is the default
563         if (!scorm_disable_toc) {
564             if (scorm_hide_toc == true) {
565                 Y.one('#scorm_toc').addClass(cssclasses.disabled);
566                 Y.one('#scorm_toc_toggle_btn')
567                     .setHTML('&gt;')
568                     .setAttribute('title', M.util.get_string('show', 'moodle'));
569                 Y.one('#scorm_content')
570                     .removeClass(cssclasses.scorm_grid_content_toc_visible)
571                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
572             }
573         }
575         // TOC Resize handle.
576         var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
577         var scorm_resize_handle = new Y.Resize({
578             node: '#scorm_toc',
579             handles: 'r',
580             defMinWidth: 0.2 * layout_width
581         });
582         // TOC tree
583         var toc_source = Y.one('#scorm_tree > ul');
584         var toc = scorm_parse_toc_tree(toc_source);
585         // Empty container after parsing toc.
586         var el = document.getElementById('scorm_tree');
587         el.innerHTML = '';
588         var tree = new Y.TreeView({
589             container: '#scorm_tree',
590             nodes: toc,
591             multiSelect: false
592         });
593         scorm_tree_node = tree;
594         // Trigger after instead of on, avoid recursive calls.
595         tree.after('select', function(e) {
596             var node = e.node;
597             if (node.title == '' || node.title == null) {
598                 return; //this item has no navigation
599             }
600             // If item is already active, return; avoid recursive calls.
601             if (Y.one('#scorm_data')) {
602                 var scorm_active_url = Y.one('#scorm_object').getAttribute('src');
603                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
604                 if (node_full_url === scorm_active_url) {
605                     return;
606                 }
607             }
608             // Update launch_sco.
609             if (typeof node.scoid !== 'undefined') {
610                 launch_sco = node.scoid;
611             }
612             scorm_activate_item(node);
613             if (node.children.length) {
614                 scorm_bloody_labelclick = true;
615             }
616         });
617         if (!scorm_disable_toc) {
618             tree.on('close', function(e) {
619                 if (scorm_bloody_labelclick) {
620                     scorm_bloody_labelclick = false;
621                     return false;
622                 }
623             });
624             tree.subscribe('open', function(e) {
625                 if (scorm_bloody_labelclick) {
626                     scorm_bloody_labelclick = false;
627                     return false;
628                 }
629             });
630         }
631         tree.render();
632         tree.openAll();
634         // On getting the window, always set the focus on the current item
635         Y.one(Y.config.win).on('focus', function (e) {
636             var current = scorm_tree_node.getSelectedNodes()[0];
637             var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
638             if (current.id && !toc_disabled) {
639                 Y.one('#' + current.id).focus();
640             }
641         });
643         // navigation
644         if (scorm_hide_nav == false) {
645             // TODO: make some better&accessible buttons.
646             var navbuttonshtml = '<span id="scorm_nav"><button id="nav_skipprev">&lt;&lt;</button>&nbsp;<button id="nav_prev">&lt;</button>'
647                     + '&nbsp;<button id="nav_up">^</button>&nbsp;<button id="nav_next">&gt;</button>'
648                     + '&nbsp;<button id="nav_skipnext">&gt;&gt;</button></span>';
649             if (nav_display === 1) {
650                 Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
651             } else {
652                 // Nav panel is floating type.
653                 var navposition = null;
654                 if (navposition_left < 0 && navposition_top < 0) {
655                     // Set default XY.
656                     navposition = Y.one('#scorm_toc').getXY();
657                     navposition[1] += 200;
658                 } else {
659                     // Set user defined XY.
660                     navposition = [];
661                     navposition[0] = parseInt(navposition_left, 10);
662                     navposition[1] = parseInt(navposition_top, 10);
663                 }
664                 scorm_nav_panel = new Y.Panel({
665                     fillHeight: "body",
666                     headerContent: M.util.get_string('navigation', 'scorm'),
667                     visible: true,
668                     xy: navposition,
669                     zIndex: 999
670                 });
671                 scorm_nav_panel.set('bodyContent', navbuttonshtml);
672                 scorm_nav_panel.removeButton('close');
673                 scorm_nav_panel.plug(Y.Plugin.Drag, {handles: ['.yui3-widget-hd']});
674                 scorm_nav_panel.render();
675             }
677             scorm_buttons[0] = new Y.Button({
678                 srcNode: '#nav_skipprev',
679                 render: true,
680                 on: {
681                         'click' : function(ev) {
682                             scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
683                         },
684                         'keydown' : function(ev) {
685                             if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
686                                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
687                             }
688                         }
689                     }
690             });
691             scorm_buttons[1] = new Y.Button({
692                 srcNode: '#nav_prev',
693                 render: true,
694                 on: {
695                     'click' : function(ev) {
696                         scorm_launch_prev_sco();
697                     },
698                     'keydown' : function(ev) {
699                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
700                             scorm_launch_prev_sco();
701                         }
702                     }
703                 }
704             });
705             scorm_buttons[2] = new Y.Button({
706                 srcNode: '#nav_up',
707                 render: true,
708                 on: {
709                     'click' : function(ev) {
710                         scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
711                     },
712                     'keydown' : function(ev) {
713                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
714                             scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
715                         }
716                     }
717                 }
718             });
719             scorm_buttons[3] = new Y.Button({
720                 srcNode: '#nav_next',
721                 render: true,
722                 on: {
723                     'click' : function(ev) {
724                         scorm_launch_next_sco();
725                     },
726                     'keydown' : function(ev) {
727                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
728                             scorm_launch_next_sco();
729                         }
730                     }
731                 }
732             });
733             scorm_buttons[4] = new Y.Button({
734                 srcNode: '#nav_skipnext',
735                 render: true,
736                 on: {
737                     'click' : function(ev) {
738                         scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
739                     },
740                     'keydown' : function(ev) {
741                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
742                             scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
743                         }
744                     }
745                 }
746             });
747         }
749         // finally activate the chosen item
750         var scorm_first_url = null;
751         if (tree.rootNode.children[0].title !== scoes_nav[launch_sco].url) {
752             var node = tree.getNodeByAttribute('title', scoes_nav[launch_sco].url);
753             if (node !== null) {
754                 scorm_first_url = node;
755             }
756         } else {
757             scorm_first_url = tree.rootNode.children[0];
758         }
760         if (scorm_first_url == null) { // This is probably a single sco with no children (AICC Direct uses this).
761             scorm_first_url = tree.rootNode;
762         }
763         scorm_first_url.title = scoes_nav[launch_sco].url;
764         scorm_activate_item(scorm_first_url);
766         // resizing
767         scorm_resize_layout();
769         // Collapse/expand TOC.
770         Y.one('#scorm_toc_toggle').on('click', scorm_toggle_toc);
771         Y.one('#scorm_toc_toggle').on('key', scorm_toggle_toc, 'down:enter,32');
772         // fix layout if window resized
773         Y.on("windowresize", function() {
774             scorm_resize_layout();
775             var toc_displayed = Y.one('#scorm_toc').getComputedStyle('display') !== 'none';
776             if ((!scorm_disable_toc && !scorm_hide_toc) || toc_displayed) {
777                 scorm_toggle_toc(true);
778             }
779             // Set 20% as minWidth constrain of TOC.
780             var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
781             scorm_resize_handle.set('defMinWidth', 0.2 * layout_width);
782         });
783         // On resize drag, change width of scorm_content.
784         scorm_resize_handle.on('resize:resize', function() {
785             var tocwidth = parseInt(Y.one('#scorm_toc').getComputedStyle('width'), 10);
786             var layoutwidth = parseInt(Y.one('#scorm_layout').getStyle('width'), 10);
787             Y.one('#scorm_content').setStyle('width', (layoutwidth - tocwidth - 60));
788         });
789     });
790 };
792 M.mod_scorm.connectPrereqCallback = {
794     success: function(id, o) {
795         if (o.responseText !== undefined) {
796             var snode = null,
797                 stitle = null;
798             if (scorm_tree_node && o.responseText) {
799                 snode = scorm_tree_node.getSelectedNodes()[0];
800                 stitle = null;
801                 if (snode) {
802                     stitle = snode.title;
803                 }
804                 // All gone with clear, add new root node.
805                 scorm_tree_node.clear(scorm_tree_node.createNode());
806             }
807             // Make sure the temporary tree element is not there.
808             var el_old_tree = document.getElementById('scormtree123');
809             if (el_old_tree) {
810                 el_old_tree.parentNode.removeChild(el_old_tree);
811             }
812             var el_new_tree = document.createElement('div');
813             var pagecontent = document.getElementById("page-content");
814             el_new_tree.setAttribute('id','scormtree123');
815             el_new_tree.innerHTML = o.responseText;
816             // Make sure it does not show.
817             el_new_tree.style.display = 'none';
818             pagecontent.appendChild(el_new_tree);
819             // Ignore the first level element as this is the title.
820             var startNode = el_new_tree.firstChild.firstChild;
821             if (startNode.tagName == 'LI') {
822                 // Go back to the beginning.
823                 startNode = el_new_tree;
824             }
825             var toc_source = Y.one('#scormtree123 > ul');
826             var toc = mod_scorm_parse_toc_tree(toc_source);
827             scorm_tree_node.appendNode(scorm_tree_node.rootNode, toc);
828             var el = document.getElementById('scormtree123');
829             el.parentNode.removeChild(el);
830             scorm_tree_node.render();
831             scorm_tree_node.openAll();
832             if (stitle !== null) {
833                 snode = scorm_tree_node.getNodeByAttribute('title', stitle);
834                 // Do not let destroyed node to be selected.
835                 if (snode && !snode.state.destroyed) {
836                     snode.select();
837                     var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
838                     if (!toc_disabled) {
839                         if (!snode.state.selected) {
840                             snode.select();
841                         }
842                     }
843                 }
844             }
845         }
846     },
848     failure: function(id, o) {
849         // TODO: do some sort of error handling.
850     }
852 };