MDL-63660 tool_dataprivacy: Increase expected export file size
[moodle.git] / mod / scorm / module.js
blobb1df94ff049807494b8d33547e000199406b2ef1
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.
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 window.scorm_current_node = null;
32 function underscore(str) {
33     str = String(str).replace(/.N/g,".");
34     return str.replace(/\./g,"__");
37 M.mod_scorm = {};
39 M.mod_scorm.init = function(Y, nav_display, navposition_left, navposition_top, hide_toc, collapsetocwinsize, toc_title, window_name, launch_sco, scoes_nav) {
40     var scorm_disable_toc = false;
41     var scorm_hide_nav = true;
42     var scorm_hide_toc = true;
43     if (hide_toc == 0) {
44         if (nav_display !== 0) {
45             scorm_hide_nav = false;
46         }
47         scorm_hide_toc = false;
48     } else if (hide_toc == 3) {
49         scorm_disable_toc = true;
50     }
52     scoes_nav = Y.JSON.parse(scoes_nav);
53     var scorm_buttons = [];
54     var scorm_bloody_labelclick = false;
55     var scorm_nav_panel;
57     Y.use('button', 'dd-plugin', 'panel', 'resize', 'gallery-sm-treeview', function(Y) {
59         Y.TreeView.prototype.getNodeByAttribute = function(attribute, value) {
60             var node = null,
61                 domnode = Y.one('a[' + attribute + '="' + value + '"]');
62             if (domnode !== null) {
63                 node = scorm_tree_node.getNodeById(domnode.ancestor('li').get('id'));
64             }
65             return node;
66         };
68         Y.TreeView.prototype.openAll = function () {
69             this.get('container').all('.yui3-treeview-can-have-children').each(function(target) {
70                 this.getNodeById(target.get('id')).open();
71             }, this);
72         };
74         Y.TreeView.prototype.closeAll = function () {
75             this.get('container').all('.yui3-treeview-can-have-children').each(function(target) {
76                 this.getNodeById(target.get('id')).close();
77             }, this);
78         }
80         var scorm_parse_toc_tree = function(srcNode) {
81             var SELECTORS = {
82                     child: '> li',
83                     label: '> li, > a',
84                     textlabel : '> li, > span',
85                     subtree: '> ul, > li'
86                 },
87                 children = [];
89             srcNode.all(SELECTORS.child).each(function(childNode) {
90                 var child = {},
91                     labelNode = childNode.one(SELECTORS.label),
92                     textNode = childNode.one(SELECTORS.textlabel),
93                     subTreeNode = childNode.one(SELECTORS.subtree);
95                 if (labelNode) {
96                     var title = labelNode.getAttribute('title');
97                     var scoid = labelNode.getData('scoid');
98                     child.label = labelNode.get('outerHTML');
99                     // Will be good to change to url instead of title.
100                     if (title && title !== '#') {
101                         child.title = title;
102                     }
103                     if (typeof scoid !== 'undefined') {
104                         child.scoid = scoid;
105                     }
106                 } else if (textNode) {
107                     // The selector did not find a label node with anchor.
108                     child.label = textNode.get('outerHTML');
109                 }
111                 if (subTreeNode) {
112                     child.children = scorm_parse_toc_tree(subTreeNode);
113                 }
115                 children.push(child);
116             });
118             return children;
119         };
121         mod_scorm_parse_toc_tree = scorm_parse_toc_tree;
123         var scorm_activate_item = function(node) {
124             if (!node) {
125                 return;
126             }
127             // Check if the item is already active, avoid recursive calls.
128             var content = Y.one('#scorm_content');
129             var old = Y.one('#scorm_object');
130             if (old) {
131                 var scorm_active_url = Y.one('#scorm_object').getAttribute('src');
132                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
133                 if (node_full_url === scorm_active_url) {
134                     return;
135                 }
136                 // Start to unload iframe here
137                 if(!window_name){
138                     content.removeChild(old);
139                     old = null;
140                 }
141             }
142             // End of - Avoid recursive calls.
144             scorm_current_node = node;
145             if (!scorm_current_node.state.selected) {
146                 scorm_current_node.select();
147             }
149             scorm_tree_node.closeAll();
150             var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
151             var el_old_api = document.getElementById('scormapi123');
152             if (el_old_api) {
153                 el_old_api.parentNode.removeChild(el_old_api);
154             }
156             var obj = document.createElement('iframe');
157             obj.setAttribute('id', 'scorm_object');
158             obj.setAttribute('type', 'text/html');
159             obj.setAttribute('allowfullscreen', 'allowfullscreen');
160             obj.setAttribute('webkitallowfullscreen', 'webkitallowfullscreen');
161             obj.setAttribute('mozallowfullscreen', 'mozallowfullscreen');
162             if (!window_name && node.title != null) {
163                 obj.setAttribute('src', url_prefix + node.title);
164             }
165             if (window_name) {
166                 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
167                 if(! mine) {
168                     alert(M.util.get_string('popupsblocked', 'scorm'));
169                 }
170                 mine.close();
171             }
173             if (old) {
174                 if(window_name) {
175                     var cwidth = scormplayerdata.cwidth;
176                     var cheight = scormplayerdata.cheight;
177                     var poptions = scormplayerdata.popupoptions;
178                     poptions = poptions + ',resizable=yes'; // Added for IE (MDL-32506).
179                     scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
180                 }
181             } else {
182                 content.prepend(obj);
183             }
185             if (scorm_hide_nav == false) {
186                 if (nav_display === 1 && navposition_left > 0 && navposition_top > 0) {
187                     Y.one('#scorm_object').addClass(cssclasses.scorm_nav_under_content);
188                 }
189                 scorm_fixnav();
190             }
191             scorm_tree_node.openAll();
192         };
194         mod_scorm_activate_item = scorm_activate_item;
196         /**
197          * Enables/disables navigation buttons as needed.
198          * @return void
199          */
200         var scorm_fixnav = function() {
201             var skipprevnode = scorm_skipprev(scorm_current_node);
202             var prevnode = scorm_prev(scorm_current_node);
203             var skipnextnode = scorm_skipnext(scorm_current_node);
204             var nextnode = scorm_next(scorm_current_node);
205             var upnode = scorm_up(scorm_current_node);
207             scorm_buttons[0].set('disabled', ((skipprevnode === null) ||
208                         (typeof(skipprevnode.scoid) === 'undefined') ||
209                         (scoes_nav[skipprevnode.scoid].isvisible === "false") ||
210                         (skipprevnode.title === null) ||
211                         (scoes_nav[launch_sco].hideprevious === 1)));
213             scorm_buttons[1].set('disabled', ((prevnode === null) ||
214                         (typeof(prevnode.scoid) === 'undefined') ||
215                         (scoes_nav[prevnode.scoid].isvisible === "false") ||
216                         (prevnode.title === null) ||
217                         (scoes_nav[launch_sco].hideprevious === 1)));
219             scorm_buttons[2].set('disabled', (upnode === null) ||
220                         (typeof(upnode.scoid) === 'undefined') ||
221                         (scoes_nav[upnode.scoid].isvisible === "false") ||
222                         (upnode.title === null));
224             scorm_buttons[3].set('disabled', ((nextnode === null) ||
225                         ((nextnode.title === null) && (scoes_nav[launch_sco].flow !== 1)) ||
226                         (typeof(nextnode.scoid) === 'undefined') ||
227                         (scoes_nav[nextnode.scoid].isvisible === "false") ||
228                         (scoes_nav[launch_sco].hidecontinue === 1)));
230             scorm_buttons[4].set('disabled', ((skipnextnode === null) ||
231                         (skipnextnode.title === null) ||
232                         (typeof(skipnextnode.scoid) === 'undefined') ||
233                         (scoes_nav[skipnextnode.scoid].isvisible === "false") ||
234                         scoes_nav[launch_sco].hidecontinue === 1));
235         };
237         var scorm_toggle_toc = function(windowresize) {
238             var toc = Y.one('#scorm_toc');
239             var scorm_content = Y.one('#scorm_content');
240             var scorm_toc_toggle_btn = Y.one('#scorm_toc_toggle_btn');
241             var toc_disabled = toc.hasClass('disabled');
242             var disabled_by = toc.getAttribute('disabled-by');
243             // Remove width element style from resize handle.
244             toc.setStyle('width', null);
245             scorm_content.setStyle('width', null);
246             if (windowresize === true) {
247                 if (disabled_by === 'user') {
248                     return;
249                 }
250                 var body = Y.one('body');
251                 if (body.get('winWidth') < collapsetocwinsize) {
252                     toc.addClass(cssclasses.disabled)
253                         .setAttribute('disabled-by', 'screen-size');
254                     scorm_toc_toggle_btn.setHTML('&gt;')
255                         .set('title', M.util.get_string('show', 'moodle'));
256                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
257                         .addClass(cssclasses.scorm_grid_content_toc_hidden);
258                 } else if (body.get('winWidth') > collapsetocwinsize) {
259                     toc.removeClass(cssclasses.disabled)
260                         .removeAttribute('disabled-by');
261                     scorm_toc_toggle_btn.setHTML('&lt;')
262                         .set('title', M.util.get_string('hide', 'moodle'));
263                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
264                         .addClass(cssclasses.scorm_grid_content_toc_visible);
265                 }
266                 return;
267             }
268             if (toc_disabled) {
269                 toc.removeClass(cssclasses.disabled)
270                     .removeAttribute('disabled-by');
271                 scorm_toc_toggle_btn.setHTML('&lt;')
272                     .set('title', M.util.get_string('hide', 'moodle'));
273                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
274                     .addClass(cssclasses.scorm_grid_content_toc_visible);
275             } else {
276                 toc.addClass(cssclasses.disabled)
277                     .setAttribute('disabled-by', 'user');
278                 scorm_toc_toggle_btn.setHTML('&gt;')
279                     .set('title', M.util.get_string('show', 'moodle'));
280                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
281                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
282             }
283         };
285         var scorm_resize_layout = function() {
286             if (window_name) {
287                 return;
288             }
290             // make sure that the max width of the TOC doesn't go to far
292             var scorm_toc_node = Y.one('#scorm_toc');
293             var maxwidth = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
294             scorm_toc_node.setStyle('maxWidth', (maxwidth - 200));
295             var cwidth = parseInt(scorm_toc_node.getComputedStyle('width'), 10);
296             if (cwidth > (maxwidth - 1)) {
297                 scorm_toc_node.setStyle('width', (maxwidth - 50));
298             }
300             // Calculate the rough new height from the viewport height.
301             var newheight = Y.one('body').get('winHeight') - 5
302                 - Y.one('#scorm_layout').getY()
303                 - window.pageYOffset;
304             if (newheight < 680 || isNaN(newheight)) {
305                 newheight = 680;
306             }
307             Y.one('#scorm_layout').setStyle('height', newheight);
309         };
311         // Handle AJAX Request
312         var scorm_ajax_request = function(url, datastring) {
313             var myRequest = NewHttpReq();
314             var result = DoRequest(myRequest, url + datastring);
315             return result;
316         };
318         var scorm_up = function(node, update_launch_sco) {
319             if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
320                 var parentscoid = scoes_nav[launch_sco].parentscoid;
321                 var parent = node.parent;
322                 if (parent.title !== scoes_nav[parentscoid].url) {
323                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
324                     if (parent === null) {
325                         parent = scorm_tree_node.rootNode.children[0];
326                         parent.title = scoes_nav[parentscoid].url;
327                     }
328                 }
329                 if (update_launch_sco) {
330                     launch_sco = parentscoid;
331                 }
332                 return parent;
333             }
334             return null;
335         };
337         var scorm_lastchild = function(node) {
338             if (node.children.length) {
339                 return scorm_lastchild(node.children[node.children.length - 1]);
340             } else {
341                 return node;
342             }
343         };
345         var scorm_prev = function(node, update_launch_sco) {
346             if (node.previous() && node.previous().children.length &&
347                     typeof scoes_nav[launch_sco].prevscoid !== 'undefined') {
348                 node = scorm_lastchild(node.previous());
349                 if (node) {
350                     var prevscoid = scoes_nav[launch_sco].prevscoid;
351                     if (node.title !== scoes_nav[prevscoid].url) {
352                         node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevscoid].url);
353                         if (node === null) {
354                             node = scorm_tree_node.rootNode.children[0];
355                             node.title = scoes_nav[prevscoid].url;
356                         }
357                     }
358                     if (update_launch_sco) {
359                         launch_sco = prevscoid;
360                     }
361                     return node;
362                 } else {
363                     return null;
364                 }
365             }
366             return scorm_skipprev(node, update_launch_sco);
367         };
369         var scorm_skipprev = function(node, update_launch_sco) {
370             if (node.previous() && typeof scoes_nav[launch_sco].prevsibling !== 'undefined') {
371                 var prevsibling = scoes_nav[launch_sco].prevsibling;
372                 var previous = node.previous();
373                 var prevscoid = scoes_nav[launch_sco].prevscoid;
374                 if (previous.title !== scoes_nav[prevscoid].url) {
375                     previous = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevsibling].url);
376                     if (previous === null) {
377                         previous = scorm_tree_node.rootNode.children[0];
378                         previous.title = scoes_nav[prevsibling].url;
379                     }
380                 }
381                 if (update_launch_sco) {
382                     launch_sco = prevsibling;
383                 }
384                 return previous;
385             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
386                 var parentscoid = scoes_nav[launch_sco].parentscoid;
387                 var parent = node.parent;
388                 if (parent.title !== scoes_nav[parentscoid].url) {
389                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
390                     if (parent === null) {
391                         parent = scorm_tree_node.rootNode.children[0];
392                         parent.title = scoes_nav[parentscoid].url;
393                     }
394                 }
395                 if (update_launch_sco) {
396                     launch_sco = parentscoid;
397                 }
398                 return parent;
399             }
400             return null;
401         };
403         var scorm_next = function(node, update_launch_sco) {
404             if (node === false) {
405                 return scorm_tree_node.children[0];
406             }
407             if (node.children.length && typeof scoes_nav[launch_sco].nextscoid != 'undefined') {
408                 node = node.children[0];
409                 var nextscoid = scoes_nav[launch_sco].nextscoid;
410                 if (node.title !== scoes_nav[nextscoid].url) {
411                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextscoid].url);
412                     if (node === null) {
413                         node = scorm_tree_node.rootNode.children[0];
414                         node.title = scoes_nav[nextscoid].url;
415                     }
416                 }
417                 if (update_launch_sco) {
418                     launch_sco = nextscoid;
419                 }
420                 return node;
421             }
422             return scorm_skipnext(node, update_launch_sco);
423         };
425         var scorm_skipnext = function(node, update_launch_sco) {
426             var next = node.next();
427             if (next && next.title && typeof scoes_nav[launch_sco] !== 'undefined' && typeof scoes_nav[launch_sco].nextsibling !== 'undefined') {
428                 var nextsibling = scoes_nav[launch_sco].nextsibling;
429                 if (next.title !== scoes_nav[nextsibling].url) {
430                     next = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextsibling].url);
431                     if (next === null) {
432                         next = scorm_tree_node.rootNode.children[0];
433                         next.title = scoes_nav[nextsibling].url;
434                     }
435                 }
436                 if (update_launch_sco) {
437                     launch_sco = nextsibling;
438                 }
439                 return next;
440             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
441                 var parentscoid = scoes_nav[launch_sco].parentscoid;
442                 var parent = node.parent;
443                 if (parent.title !== scoes_nav[parentscoid].url) {
444                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
445                     if (parent === null) {
446                         parent = scorm_tree_node.rootNode.children[0];
447                     }
448                 }
449                 if (update_launch_sco) {
450                     launch_sco = parentscoid;
451                 }
452                 return scorm_skipnext(parent, update_launch_sco);
453             }
454             return null;
455         };
457         // Launch prev sco
458         var scorm_launch_prev_sco = function() {
459             var result = null;
460             if (scoes_nav[launch_sco].flow === 1) {
461                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=backward';
462                 result = scorm_ajax_request(M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?', datastring);
463                 mod_scorm_seq = encodeURIComponent(result);
464                 result = Y.JSON.parse (result);
465                 if (typeof result.nextactivity.id != undefined) {
466                         var node = scorm_prev(scorm_tree_node.getSelectedNodes()[0]);
467                         if (node == null) {
468                             // Avoid use of TreeView for Navigation.
469                             node = scorm_tree_node.getSelectedNodes()[0];
470                         }
471                         if (node.title !== scoes_nav[result.nextactivity.id].url) {
472                             node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
473                             if (node === null) {
474                                 node = scorm_tree_node.rootNode.children[0];
475                                 node.title = scoes_nav[result.nextactivity.id].url;
476                             }
477                         }
478                         launch_sco = result.nextactivity.id;
479                         scorm_activate_item(node);
480                         scorm_fixnav();
481                 } else {
482                         scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
483                 }
484             } else {
485                 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
486             }
487         };
489         // Launch next sco
490         var scorm_launch_next_sco = function () {
491             var result = null;
492             if (scoes_nav[launch_sco].flow === 1) {
493                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=forward';
494                 result = scorm_ajax_request(M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?', datastring);
495                 mod_scorm_seq = encodeURIComponent(result);
496                 result = Y.JSON.parse (result);
497                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
498                     var node = scorm_next(scorm_tree_node.getSelectedNodes()[0]);
499                     if (node === null) {
500                         // Avoid use of TreeView for Navigation.
501                         node = scorm_tree_node.getSelectedNodes()[0];
502                     }
503                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
504                     if (node === null) {
505                         node = scorm_tree_node.rootNode.children[0];
506                         node.title = scoes_nav[result.nextactivity.id].url;
507                     }
508                     launch_sco = result.nextactivity.id;
509                     scorm_activate_item(node);
510                     scorm_fixnav();
511                 } else {
512                     scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
513                 }
514             } else {
515                 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
516             }
517         };
519         mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
520         mod_scorm_launch_next_sco = scorm_launch_next_sco;
522         var cssclasses = {
523                 // YUI grid class: use 100% of the available width to show only content, TOC hidden.
524                 scorm_grid_content_toc_hidden: 'yui3-u-1',
525                 // YUI grid class: use 1/5 of the available width to show TOC.
526                 scorm_grid_toc: 'yui3-u-1-5',
527                 // YUI grid class: use 1/24 of the available width to show TOC toggle button.
528                 scorm_grid_toggle: 'yui3-u-1-24',
529                 // YUI grid class: use 3/4 of the available width to show content, TOC visible.
530                 scorm_grid_content_toc_visible: 'yui3-u-3-4',
531                 // Reduce height of #scorm_object to accomodate nav buttons under content.
532                 scorm_nav_under_content: 'scorm_nav_under_content',
533                 disabled: 'disabled'
534             };
535         // layout
536         Y.one('#scorm_toc_title').setHTML(toc_title);
538         if (scorm_disable_toc) {
539             Y.one('#scorm_toc').addClass(cssclasses.disabled);
540             Y.one('#scorm_toc_toggle').addClass(cssclasses.disabled);
541             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_hidden);
542         } else {
543             Y.one('#scorm_toc').addClass(cssclasses.scorm_grid_toc);
544             Y.one('#scorm_toc_toggle').addClass(cssclasses.scorm_grid_toggle);
545             Y.one('#scorm_toc_toggle_btn')
546                 .setHTML('&lt;')
547                 .setAttribute('title', M.util.get_string('hide', 'moodle'));
548             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_visible);
549             scorm_toggle_toc(true);
550         }
552         // hide the TOC if that is the default
553         if (!scorm_disable_toc) {
554             if (scorm_hide_toc == true) {
555                 Y.one('#scorm_toc').addClass(cssclasses.disabled);
556                 Y.one('#scorm_toc_toggle_btn')
557                     .setHTML('&gt;')
558                     .setAttribute('title', M.util.get_string('show', 'moodle'));
559                 Y.one('#scorm_content')
560                     .removeClass(cssclasses.scorm_grid_content_toc_visible)
561                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
562             }
563         }
565         // Basic initialization completed, show the elements.
566         Y.one('#scorm_toc').removeClass('loading');
567         Y.one('#scorm_toc_toggle').removeClass('loading');
569         // TOC Resize handle.
570         var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
571         var scorm_resize_handle = new Y.Resize({
572             node: '#scorm_toc',
573             handles: 'r',
574             defMinWidth: 0.2 * layout_width
575         });
576         // TOC tree
577         var toc_source = Y.one('#scorm_tree > ul');
578         var toc = scorm_parse_toc_tree(toc_source);
579         // Empty container after parsing toc.
580         var el = document.getElementById('scorm_tree');
581         el.innerHTML = '';
582         var tree = new Y.TreeView({
583             container: '#scorm_tree',
584             nodes: toc,
585             multiSelect: false
586         });
587         scorm_tree_node = tree;
588         // Trigger after instead of on, avoid recursive calls.
589         tree.after('select', function(e) {
590             var node = e.node;
591             if (node.title == '' || node.title == null) {
592                 return; //this item has no navigation
593             }
595             // If item is already active, return; avoid recursive calls.
596             if (obj = Y.one('#scorm_object')) {
597                 var scorm_active_url = obj.getAttribute('src');
598                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
599                 if (node_full_url === scorm_active_url) {
600                     return;
601                 }
602             } else if(scorm_current_node == node){
603                 return;
604             }
606             // Update launch_sco.
607             if (typeof node.scoid !== 'undefined') {
608                 launch_sco = node.scoid;
609             }
610             scorm_activate_item(node);
611             if (node.children.length) {
612                 scorm_bloody_labelclick = true;
613             }
614         });
615         if (!scorm_disable_toc) {
616             tree.on('close', function(e) {
617                 if (scorm_bloody_labelclick) {
618                     scorm_bloody_labelclick = false;
619                     return false;
620                 }
621             });
622             tree.subscribe('open', function(e) {
623                 if (scorm_bloody_labelclick) {
624                     scorm_bloody_labelclick = false;
625                     return false;
626                 }
627             });
628         }
629         tree.render();
630         tree.openAll();
632         // On getting the window, always set the focus on the current item
633         Y.one(Y.config.win).on('focus', function (e) {
634             var current = scorm_tree_node.getSelectedNodes()[0];
635             var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
636             if (current.id && !toc_disabled) {
637                 Y.one('#' + current.id).focus();
638             }
639         });
641         // navigation
642         if (scorm_hide_nav == false) {
643             // TODO: make some better&accessible buttons.
644             var navbuttonshtml = '<span id="scorm_nav"><button id="nav_skipprev">&lt;&lt;</button>&nbsp;' +
645                                     '<button id="nav_prev">&lt;</button>&nbsp;<button id="nav_up">^</button>&nbsp;' +
646                                     '<button id="nav_next">&gt;</button>&nbsp;<button id="nav_skipnext">&gt;&gt;</button></span>';
647             if (nav_display === 1) {
648                 Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
649             } else {
650                 // Nav panel is floating type.
651                 var navposition = null;
652                 if (navposition_left < 0 && navposition_top < 0) {
653                     // Set default XY.
654                     navposition = Y.one('#scorm_toc').getXY();
655                     navposition[1] += 200;
656                 } else {
657                     // Set user defined XY.
658                     navposition = [];
659                     navposition[0] = parseInt(navposition_left, 10);
660                     navposition[1] = parseInt(navposition_top, 10);
661                 }
662                 scorm_nav_panel = new Y.Panel({
663                     fillHeight: "body",
664                     headerContent: M.util.get_string('navigation', 'scorm'),
665                     visible: true,
666                     xy: navposition,
667                     zIndex: 999
668                 });
669                 scorm_nav_panel.set('bodyContent', navbuttonshtml);
670                 scorm_nav_panel.removeButton('close');
671                 scorm_nav_panel.plug(Y.Plugin.Drag, {handles: ['.yui3-widget-hd']});
672                 scorm_nav_panel.render();
673             }
675             scorm_buttons[0] = new Y.Button({
676                 srcNode: '#nav_skipprev',
677                 render: true,
678                 on: {
679                         'click' : function(ev) {
680                             scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
681                         },
682                         'keydown' : function(ev) {
683                             if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
684                                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
685                             }
686                         }
687                     }
688             });
689             scorm_buttons[1] = new Y.Button({
690                 srcNode: '#nav_prev',
691                 render: true,
692                 on: {
693                     'click' : function(ev) {
694                         scorm_launch_prev_sco();
695                     },
696                     'keydown' : function(ev) {
697                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
698                             scorm_launch_prev_sco();
699                         }
700                     }
701                 }
702             });
703             scorm_buttons[2] = new Y.Button({
704                 srcNode: '#nav_up',
705                 render: true,
706                 on: {
707                     'click' : function(ev) {
708                         scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
709                     },
710                     'keydown' : function(ev) {
711                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
712                             scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
713                         }
714                     }
715                 }
716             });
717             scorm_buttons[3] = new Y.Button({
718                 srcNode: '#nav_next',
719                 render: true,
720                 on: {
721                     'click' : function(ev) {
722                         scorm_launch_next_sco();
723                     },
724                     'keydown' : function(ev) {
725                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
726                             scorm_launch_next_sco();
727                         }
728                     }
729                 }
730             });
731             scorm_buttons[4] = new Y.Button({
732                 srcNode: '#nav_skipnext',
733                 render: true,
734                 on: {
735                     'click' : function(ev) {
736                         scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
737                     },
738                     'keydown' : function(ev) {
739                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
740                             scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
741                         }
742                     }
743                 }
744             });
745         }
747         // finally activate the chosen item
748         var scorm_first_url = null;
749         if (typeof tree.rootNode.children[0] !== 'undefined') {
750             if (tree.rootNode.children[0].title !== scoes_nav[launch_sco].url) {
751                 var node = tree.getNodeByAttribute('title', scoes_nav[launch_sco].url);
752                 if (node !== null) {
753                     scorm_first_url = node;
754                 }
755             } else {
756                 scorm_first_url = tree.rootNode.children[0];
757             }
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     });
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             if (!pagecontent) {
815                 pagecontent = document.getElementById("content");
816             }
817             el_new_tree.setAttribute('id','scormtree123');
818             el_new_tree.innerHTML = o.responseText;
819             // Make sure it does not show.
820             el_new_tree.style.display = 'none';
821             pagecontent.appendChild(el_new_tree);
822             // Ignore the first level element as this is the title.
823             var startNode = el_new_tree.firstChild.firstChild;
824             if (startNode.tagName == 'LI') {
825                 // Go back to the beginning.
826                 startNode = el_new_tree;
827             }
828             var toc_source = Y.one('#scormtree123 > ul');
829             var toc = mod_scorm_parse_toc_tree(toc_source);
830             scorm_tree_node.appendNode(scorm_tree_node.rootNode, toc);
831             var el = document.getElementById('scormtree123');
832             el.parentNode.removeChild(el);
833             scorm_tree_node.render();
834             scorm_tree_node.openAll();
835             if (stitle !== null) {
836                 snode = scorm_tree_node.getNodeByAttribute('title', stitle);
837                 // Do not let destroyed node to be selected.
838                 if (snode && !snode.state.destroyed) {
839                     snode.select();
840                     var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
841                     if (!toc_disabled) {
842                         if (!snode.state.selected) {
843                             snode.select();
844                         }
845                     }
846                 }
847             }
848         }
849     },
851     failure: function(id, o) {
852         // TODO: do some sort of error handling.
853     }