Merge branch 'MDL-73163-311' of https://github.com/lameze/moodle into MOODLE_311_STABLE
[moodle.git] / mod / scorm / module.js
blobc54a19f2595a6a09bda6b54ccf81667752a76003
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             // Attach unload observers to the iframe. The scorm package may be observing these unload events
166             // and trying to save progress when they occur. We need to ensure we use the Beacon API in those
167             // situations.
168             if (typeof mod_scorm_monitorForBeaconRequirement !== 'undefined') {
169                 mod_scorm_monitorForBeaconRequirement(obj);
170             }
171             if (window_name) {
172                 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
173                 if(! mine) {
174                     alert(M.util.get_string('popupsblocked', 'scorm'));
175                 }
176                 mine.close();
177             }
179             if (old) {
180                 if(window_name) {
181                     var cwidth = scormplayerdata.cwidth;
182                     var cheight = scormplayerdata.cheight;
183                     var poptions = scormplayerdata.popupoptions;
184                     poptions = poptions + ',resizable=yes'; // Added for IE (MDL-32506).
185                     scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
186                 }
187             } else {
188                 content.prepend(obj);
189             }
191             if (scorm_hide_nav == false) {
192                 if (nav_display === 1 && navposition_left > 0 && navposition_top > 0) {
193                     Y.one('#scorm_object').addClass(cssclasses.scorm_nav_under_content);
194                 }
195                 scorm_fixnav();
196             }
197             scorm_tree_node.openAll();
198         };
200         mod_scorm_activate_item = scorm_activate_item;
202         /**
203          * Enables/disables navigation buttons as needed.
204          * @return void
205          */
206         var scorm_fixnav = function() {
207             var skipprevnode = scorm_skipprev(scorm_current_node);
208             var prevnode = scorm_prev(scorm_current_node);
209             var skipnextnode = scorm_skipnext(scorm_current_node);
210             var nextnode = scorm_next(scorm_current_node);
211             var upnode = scorm_up(scorm_current_node);
213             scorm_buttons[0].set('disabled', ((skipprevnode === null) ||
214                         (typeof(skipprevnode.scoid) === 'undefined') ||
215                         (scoes_nav[skipprevnode.scoid].isvisible === "false") ||
216                         (skipprevnode.title === null) ||
217                         (scoes_nav[launch_sco].hideprevious === 1)));
219             scorm_buttons[1].set('disabled', ((prevnode === null) ||
220                         (typeof(prevnode.scoid) === 'undefined') ||
221                         (scoes_nav[prevnode.scoid].isvisible === "false") ||
222                         (prevnode.title === null) ||
223                         (scoes_nav[launch_sco].hideprevious === 1)));
225             scorm_buttons[2].set('disabled', (upnode === null) ||
226                         (typeof(upnode.scoid) === 'undefined') ||
227                         (scoes_nav[upnode.scoid].isvisible === "false") ||
228                         (upnode.title === null));
230             scorm_buttons[3].set('disabled', ((nextnode === null) ||
231                         ((nextnode.title === null) && (scoes_nav[launch_sco].flow !== 1)) ||
232                         (typeof(nextnode.scoid) === 'undefined') ||
233                         (scoes_nav[nextnode.scoid].isvisible === "false") ||
234                         (scoes_nav[launch_sco].hidecontinue === 1)));
236             scorm_buttons[4].set('disabled', ((skipnextnode === null) ||
237                         (skipnextnode.title === null) ||
238                         (typeof(skipnextnode.scoid) === 'undefined') ||
239                         (scoes_nav[skipnextnode.scoid].isvisible === "false") ||
240                         scoes_nav[launch_sco].hidecontinue === 1));
241         };
243         var scorm_toggle_toc = function(windowresize) {
244             var toc = Y.one('#scorm_toc');
245             var scorm_content = Y.one('#scorm_content');
246             var scorm_toc_toggle_btn = Y.one('#scorm_toc_toggle_btn');
247             var toc_disabled = toc.hasClass('disabled');
248             var disabled_by = toc.getAttribute('disabled-by');
249             // Remove width element style from resize handle.
250             toc.setStyle('width', null);
251             scorm_content.setStyle('width', null);
252             if (windowresize === true) {
253                 if (disabled_by === 'user') {
254                     return;
255                 }
256                 var body = Y.one('body');
257                 if (body.get('winWidth') < collapsetocwinsize) {
258                     toc.addClass(cssclasses.disabled)
259                         .setAttribute('disabled-by', 'screen-size');
260                     scorm_toc_toggle_btn.setHTML('&gt;')
261                         .set('title', M.util.get_string('show', 'moodle'));
262                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
263                         .addClass(cssclasses.scorm_grid_content_toc_hidden);
264                 } else if (body.get('winWidth') > collapsetocwinsize) {
265                     toc.removeClass(cssclasses.disabled)
266                         .removeAttribute('disabled-by');
267                     scorm_toc_toggle_btn.setHTML('&lt;')
268                         .set('title', M.util.get_string('hide', 'moodle'));
269                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
270                         .addClass(cssclasses.scorm_grid_content_toc_visible);
271                 }
272                 return;
273             }
274             if (toc_disabled) {
275                 toc.removeClass(cssclasses.disabled)
276                     .removeAttribute('disabled-by');
277                 scorm_toc_toggle_btn.setHTML('&lt;')
278                     .set('title', M.util.get_string('hide', 'moodle'));
279                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
280                     .addClass(cssclasses.scorm_grid_content_toc_visible);
281             } else {
282                 toc.addClass(cssclasses.disabled)
283                     .setAttribute('disabled-by', 'user');
284                 scorm_toc_toggle_btn.setHTML('&gt;')
285                     .set('title', M.util.get_string('show', 'moodle'));
286                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
287                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
288             }
289         };
291         var scorm_resize_layout = function() {
292             if (window_name) {
293                 return;
294             }
296             // make sure that the max width of the TOC doesn't go to far
298             var scorm_toc_node = Y.one('#scorm_toc');
299             var maxwidth = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
300             scorm_toc_node.setStyle('maxWidth', (maxwidth - 200));
301             var cwidth = parseInt(scorm_toc_node.getComputedStyle('width'), 10);
302             if (cwidth > (maxwidth - 1)) {
303                 scorm_toc_node.setStyle('width', (maxwidth - 50));
304             }
306             // Calculate the rough new height from the viewport height.
307             var newheight = Y.one('body').get('winHeight') - 5
308                 - Y.one('#scorm_layout').getY()
309                 - window.pageYOffset;
310             if (newheight < 680 || isNaN(newheight)) {
311                 newheight = 680;
312             }
313             Y.one('#scorm_layout').setStyle('height', newheight);
315         };
317         /**
318          * @deprecated as it is now unused.
319          * @param {string} url
320          * @param {string} datastring
321          * @returns {string|*|boolean}
322          */
323         var scorm_ajax_request = function(url, datastring) {
324             var myRequest = NewHttpReq();
325             var result = DoRequest(myRequest, url + datastring);
326             return result;
327         };
329         var scorm_up = function(node, update_launch_sco) {
330             if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
331                 var parentscoid = scoes_nav[launch_sco].parentscoid;
332                 var parent = node.parent;
333                 if (parent.title !== scoes_nav[parentscoid].url) {
334                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
335                     if (parent === null) {
336                         parent = scorm_tree_node.rootNode.children[0];
337                         parent.title = scoes_nav[parentscoid].url;
338                     }
339                 }
340                 if (update_launch_sco) {
341                     launch_sco = parentscoid;
342                 }
343                 return parent;
344             }
345             return null;
346         };
348         var scorm_lastchild = function(node) {
349             if (node.children.length) {
350                 return scorm_lastchild(node.children[node.children.length - 1]);
351             } else {
352                 return node;
353             }
354         };
356         var scorm_prev = function(node, update_launch_sco) {
357             if (node.previous() && node.previous().children.length &&
358                     typeof scoes_nav[launch_sco].prevscoid !== 'undefined') {
359                 node = scorm_lastchild(node.previous());
360                 if (node) {
361                     var prevscoid = scoes_nav[launch_sco].prevscoid;
362                     if (node.title !== scoes_nav[prevscoid].url) {
363                         node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevscoid].url);
364                         if (node === null) {
365                             node = scorm_tree_node.rootNode.children[0];
366                             node.title = scoes_nav[prevscoid].url;
367                         }
368                     }
369                     if (update_launch_sco) {
370                         launch_sco = prevscoid;
371                     }
372                     return node;
373                 } else {
374                     return null;
375                 }
376             }
377             return scorm_skipprev(node, update_launch_sco);
378         };
380         var scorm_skipprev = function(node, update_launch_sco) {
381             if (node.previous() && typeof scoes_nav[launch_sco].prevsibling !== 'undefined') {
382                 var prevsibling = scoes_nav[launch_sco].prevsibling;
383                 var previous = node.previous();
384                 var prevscoid = scoes_nav[launch_sco].prevscoid;
385                 if (previous.title !== scoes_nav[prevscoid].url) {
386                     previous = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevsibling].url);
387                     if (previous === null) {
388                         previous = scorm_tree_node.rootNode.children[0];
389                         previous.title = scoes_nav[prevsibling].url;
390                     }
391                 }
392                 if (update_launch_sco) {
393                     launch_sco = prevsibling;
394                 }
395                 return previous;
396             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
397                 var parentscoid = scoes_nav[launch_sco].parentscoid;
398                 var parent = node.parent;
399                 if (parent.title !== scoes_nav[parentscoid].url) {
400                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
401                     if (parent === null) {
402                         parent = scorm_tree_node.rootNode.children[0];
403                         parent.title = scoes_nav[parentscoid].url;
404                     }
405                 }
406                 if (update_launch_sco) {
407                     launch_sco = parentscoid;
408                 }
409                 return parent;
410             }
411             return null;
412         };
414         var scorm_next = function(node, update_launch_sco) {
415             if (node === false) {
416                 return scorm_tree_node.children[0];
417             }
418             if (node.children.length && typeof scoes_nav[launch_sco].nextscoid != 'undefined') {
419                 node = node.children[0];
420                 var nextscoid = scoes_nav[launch_sco].nextscoid;
421                 if (node.title !== scoes_nav[nextscoid].url) {
422                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextscoid].url);
423                     if (node === null) {
424                         node = scorm_tree_node.rootNode.children[0];
425                         node.title = scoes_nav[nextscoid].url;
426                     }
427                 }
428                 if (update_launch_sco) {
429                     launch_sco = nextscoid;
430                 }
431                 return node;
432             }
433             return scorm_skipnext(node, update_launch_sco);
434         };
436         var scorm_skipnext = function(node, update_launch_sco) {
437             var next = node.next();
438             if (next && next.title && typeof scoes_nav[launch_sco] !== 'undefined' && typeof scoes_nav[launch_sco].nextsibling !== 'undefined') {
439                 var nextsibling = scoes_nav[launch_sco].nextsibling;
440                 if (next.title !== scoes_nav[nextsibling].url) {
441                     next = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextsibling].url);
442                     if (next === null) {
443                         next = scorm_tree_node.rootNode.children[0];
444                         next.title = scoes_nav[nextsibling].url;
445                     }
446                 }
447                 if (update_launch_sco) {
448                     launch_sco = nextsibling;
449                 }
450                 return next;
451             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
452                 var parentscoid = scoes_nav[launch_sco].parentscoid;
453                 var parent = node.parent;
454                 if (parent.title !== scoes_nav[parentscoid].url) {
455                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
456                     if (parent === null) {
457                         parent = scorm_tree_node.rootNode.children[0];
458                     }
459                 }
460                 if (update_launch_sco) {
461                     launch_sco = parentscoid;
462                 }
463                 return scorm_skipnext(parent, update_launch_sco);
464             }
465             return null;
466         };
468         /**
469          * Sends a request to the sequencing handler script on the server.
470          * @param {string} datastring
471          * @returns {string|boolean|*}
472          */
473         var scorm_dorequest_sequencing = function(datastring) {
474             var myRequest = NewHttpReq();
475             var result = DoRequest(
476                 myRequest,
477                 M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?' + datastring,
478                 '',
479                 false
480             );
481             return result;
482         };
484         // Launch prev sco
485         var scorm_launch_prev_sco = function() {
486             var result = null;
487             if (scoes_nav[launch_sco].flow === 1) {
488                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=backward';
489                 result = scorm_dorequest_sequencing(datastring);
491                 // Check the scorm_ajax_result, it may be false.
492                 if (result === false) {
493                     // Either the outcome was a failure, or we are unloading and simply just don't know
494                     // what the outcome actually was.
495                     result = {};
496                 } else {
497                     result = Y.JSON.parse(result);
498                 }
500                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
501                         var node = scorm_prev(scorm_tree_node.getSelectedNodes()[0]);
502                         if (node == null) {
503                             // Avoid use of TreeView for Navigation.
504                             node = scorm_tree_node.getSelectedNodes()[0];
505                         }
506                         if (node.title !== scoes_nav[result.nextactivity.id].url) {
507                             node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
508                             if (node === null) {
509                                 node = scorm_tree_node.rootNode.children[0];
510                                 node.title = scoes_nav[result.nextactivity.id].url;
511                             }
512                         }
513                         launch_sco = result.nextactivity.id;
514                         scorm_activate_item(node);
515                         scorm_fixnav();
516                 } else {
517                         scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
518                 }
519             } else {
520                 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
521             }
522         };
524         // Launch next sco
525         var scorm_launch_next_sco = function () {
526             var result = null;
527             if (scoes_nav[launch_sco].flow === 1) {
528                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=forward';
529                 result = scorm_dorequest_sequencing(datastring);
531                 // Check the scorm_ajax_result, it may be false.
532                 if (result === false) {
533                     // Either the outcome was a failure, or we are unloading and simply just don't know
534                     // what the outcome actually was.
535                     result = {};
536                 } else {
537                     result = Y.JSON.parse(result);
538                 }
540                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
541                     var node = scorm_next(scorm_tree_node.getSelectedNodes()[0]);
542                     if (node === null) {
543                         // Avoid use of TreeView for Navigation.
544                         node = scorm_tree_node.getSelectedNodes()[0];
545                     }
546                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
547                     if (node === null) {
548                         node = scorm_tree_node.rootNode.children[0];
549                         node.title = scoes_nav[result.nextactivity.id].url;
550                     }
551                     launch_sco = result.nextactivity.id;
552                     scorm_activate_item(node);
553                     scorm_fixnav();
554                 } else {
555                     scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
556                 }
557             } else {
558                 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
559             }
560         };
562         mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
563         mod_scorm_launch_next_sco = scorm_launch_next_sco;
565         var cssclasses = {
566                 // YUI grid class: use 100% of the available width to show only content, TOC hidden.
567                 scorm_grid_content_toc_hidden: 'yui3-u-1',
568                 // YUI grid class: use 1/5 of the available width to show TOC.
569                 scorm_grid_toc: 'yui3-u-1-5',
570                 // YUI grid class: use 1/24 of the available width to show TOC toggle button.
571                 scorm_grid_toggle: 'yui3-u-1-24',
572                 // YUI grid class: use 3/4 of the available width to show content, TOC visible.
573                 scorm_grid_content_toc_visible: 'yui3-u-3-4',
574                 // Reduce height of #scorm_object to accomodate nav buttons under content.
575                 scorm_nav_under_content: 'scorm_nav_under_content',
576                 disabled: 'disabled'
577             };
578         // layout
579         Y.one('#scorm_toc_title').setHTML(toc_title);
581         if (scorm_disable_toc) {
582             Y.one('#scorm_toc').addClass(cssclasses.disabled);
583             Y.one('#scorm_toc_toggle').addClass(cssclasses.disabled);
584             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_hidden);
585         } else {
586             Y.one('#scorm_toc').addClass(cssclasses.scorm_grid_toc);
587             Y.one('#scorm_toc_toggle').addClass(cssclasses.scorm_grid_toggle);
588             Y.one('#scorm_toc_toggle_btn')
589                 .setHTML('&lt;')
590                 .setAttribute('title', M.util.get_string('hide', 'moodle'));
591             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_visible);
592             scorm_toggle_toc(true);
593         }
595         // hide the TOC if that is the default
596         if (!scorm_disable_toc) {
597             if (scorm_hide_toc == true) {
598                 Y.one('#scorm_toc').addClass(cssclasses.disabled);
599                 Y.one('#scorm_toc_toggle_btn')
600                     .setHTML('&gt;')
601                     .setAttribute('title', M.util.get_string('show', 'moodle'));
602                 Y.one('#scorm_content')
603                     .removeClass(cssclasses.scorm_grid_content_toc_visible)
604                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
605             }
606         }
608         // Basic initialization completed, show the elements.
609         Y.one('#scorm_toc').removeClass('loading');
610         Y.one('#scorm_toc_toggle').removeClass('loading');
612         // TOC Resize handle.
613         var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
614         var scorm_resize_handle = new Y.Resize({
615             node: '#scorm_toc',
616             handles: 'r',
617             defMinWidth: 0.2 * layout_width
618         });
619         // TOC tree
620         var toc_source = Y.one('#scorm_tree > ul');
621         var toc = scorm_parse_toc_tree(toc_source);
622         // Empty container after parsing toc.
623         var el = document.getElementById('scorm_tree');
624         el.innerHTML = '';
625         var tree = new Y.TreeView({
626             container: '#scorm_tree',
627             nodes: toc,
628             multiSelect: false
629         });
630         scorm_tree_node = tree;
631         // Trigger after instead of on, avoid recursive calls.
632         tree.after('select', function(e) {
633             var node = e.node;
634             if (node.title == '' || node.title == null) {
635                 return; //this item has no navigation
636             }
638             // If item is already active, return; avoid recursive calls.
639             if (obj = Y.one('#scorm_object')) {
640                 var scorm_active_url = obj.getAttribute('src');
641                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
642                 if (node_full_url === scorm_active_url) {
643                     return;
644                 }
645             } else if(scorm_current_node == node){
646                 return;
647             }
649             // Update launch_sco.
650             if (typeof node.scoid !== 'undefined') {
651                 launch_sco = node.scoid;
652             }
653             scorm_activate_item(node);
654             if (node.children.length) {
655                 scorm_bloody_labelclick = true;
656             }
657         });
658         if (!scorm_disable_toc) {
659             tree.on('close', function(e) {
660                 if (scorm_bloody_labelclick) {
661                     scorm_bloody_labelclick = false;
662                     return false;
663                 }
664             });
665             tree.subscribe('open', function(e) {
666                 if (scorm_bloody_labelclick) {
667                     scorm_bloody_labelclick = false;
668                     return false;
669                 }
670             });
671         }
672         tree.render();
673         tree.openAll();
675         // On getting the window, always set the focus on the current item
676         Y.one(Y.config.win).on('focus', function (e) {
677             var current = scorm_tree_node.getSelectedNodes()[0];
678             var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
679             if (current.id && !toc_disabled) {
680                 Y.one('#' + current.id).focus();
681             }
682         });
684         // navigation
685         if (scorm_hide_nav == false) {
686             // TODO: make some better&accessible buttons.
687             var navbuttonshtml = '<span id="scorm_nav"><button id="nav_skipprev">&lt;&lt;</button>&nbsp;' +
688                                     '<button id="nav_prev">&lt;</button>&nbsp;<button id="nav_up">^</button>&nbsp;' +
689                                     '<button id="nav_next">&gt;</button>&nbsp;<button id="nav_skipnext">&gt;&gt;</button></span>';
690             if (nav_display === 1) {
691                 Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
692             } else {
693                 // Nav panel is floating type.
694                 var navposition = null;
695                 if (navposition_left < 0 && navposition_top < 0) {
696                     // Set default XY.
697                     navposition = Y.one('#scorm_toc').getXY();
698                     navposition[1] += 200;
699                 } else {
700                     // Set user defined XY.
701                     navposition = [];
702                     navposition[0] = parseInt(navposition_left, 10);
703                     navposition[1] = parseInt(navposition_top, 10);
704                 }
705                 scorm_nav_panel = new Y.Panel({
706                     fillHeight: "body",
707                     headerContent: M.util.get_string('navigation', 'scorm'),
708                     visible: true,
709                     xy: navposition,
710                     zIndex: 999
711                 });
712                 scorm_nav_panel.set('bodyContent', navbuttonshtml);
713                 scorm_nav_panel.removeButton('close');
714                 scorm_nav_panel.plug(Y.Plugin.Drag, {handles: ['.yui3-widget-hd']});
715                 scorm_nav_panel.render();
716             }
718             scorm_buttons[0] = new Y.Button({
719                 srcNode: '#nav_skipprev',
720                 render: true,
721                 on: {
722                         'click' : function(ev) {
723                             scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
724                         },
725                         'keydown' : function(ev) {
726                             if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
727                                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
728                             }
729                         }
730                     }
731             });
732             scorm_buttons[1] = new Y.Button({
733                 srcNode: '#nav_prev',
734                 render: true,
735                 on: {
736                     'click' : function(ev) {
737                         scorm_launch_prev_sco();
738                     },
739                     'keydown' : function(ev) {
740                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
741                             scorm_launch_prev_sco();
742                         }
743                     }
744                 }
745             });
746             scorm_buttons[2] = new Y.Button({
747                 srcNode: '#nav_up',
748                 render: true,
749                 on: {
750                     'click' : function(ev) {
751                         scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
752                     },
753                     'keydown' : function(ev) {
754                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
755                             scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
756                         }
757                     }
758                 }
759             });
760             scorm_buttons[3] = new Y.Button({
761                 srcNode: '#nav_next',
762                 render: true,
763                 on: {
764                     'click' : function(ev) {
765                         scorm_launch_next_sco();
766                     },
767                     'keydown' : function(ev) {
768                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
769                             scorm_launch_next_sco();
770                         }
771                     }
772                 }
773             });
774             scorm_buttons[4] = new Y.Button({
775                 srcNode: '#nav_skipnext',
776                 render: true,
777                 on: {
778                     'click' : function(ev) {
779                         scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
780                     },
781                     'keydown' : function(ev) {
782                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
783                             scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
784                         }
785                     }
786                 }
787             });
788         }
790         // finally activate the chosen item
791         var scorm_first_url = null;
792         if (typeof tree.rootNode.children[0] !== 'undefined') {
793             if (tree.rootNode.children[0].title !== scoes_nav[launch_sco].url) {
794                 var node = tree.getNodeByAttribute('title', scoes_nav[launch_sco].url);
795                 if (node !== null) {
796                     scorm_first_url = node;
797                 }
798             } else {
799                 scorm_first_url = tree.rootNode.children[0];
800             }
801         }
803         if (scorm_first_url == null) { // This is probably a single sco with no children (AICC Direct uses this).
804             scorm_first_url = tree.rootNode;
805         }
806         scorm_first_url.title = scoes_nav[launch_sco].url;
807         scorm_activate_item(scorm_first_url);
809         // resizing
810         scorm_resize_layout();
812         // Collapse/expand TOC.
813         Y.one('#scorm_toc_toggle').on('click', scorm_toggle_toc);
814         Y.one('#scorm_toc_toggle').on('key', scorm_toggle_toc, 'down:enter,32');
815         // fix layout if window resized
816         Y.on("windowresize", function() {
817             scorm_resize_layout();
818             var toc_displayed = Y.one('#scorm_toc').getComputedStyle('display') !== 'none';
819             if ((!scorm_disable_toc && !scorm_hide_toc) || toc_displayed) {
820                 scorm_toggle_toc(true);
821             }
822             // Set 20% as minWidth constrain of TOC.
823             var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
824             scorm_resize_handle.set('defMinWidth', 0.2 * layout_width);
825         });
826         // On resize drag, change width of scorm_content.
827         scorm_resize_handle.on('resize:resize', function() {
828             var tocwidth = parseInt(Y.one('#scorm_toc').getComputedStyle('width'), 10);
829             var layoutwidth = parseInt(Y.one('#scorm_layout').getStyle('width'), 10);
830             Y.one('#scorm_content').setStyle('width', (layoutwidth - tocwidth - 60));
831         });
832     });
835 M.mod_scorm.connectPrereqCallback = {
837     success: function(id, o) {
838         if (o.responseText !== undefined) {
839             var snode = null,
840                 stitle = null;
841             if (scorm_tree_node && o.responseText) {
842                 snode = scorm_tree_node.getSelectedNodes()[0];
843                 stitle = null;
844                 if (snode) {
845                     stitle = snode.title;
846                 }
847                 // All gone with clear, add new root node.
848                 scorm_tree_node.clear(scorm_tree_node.createNode());
849             }
850             // Make sure the temporary tree element is not there.
851             var el_old_tree = document.getElementById('scormtree123');
852             if (el_old_tree) {
853                 el_old_tree.parentNode.removeChild(el_old_tree);
854             }
855             var el_new_tree = document.createElement('div');
856             var pagecontent = document.getElementById("page-content");
857             if (!pagecontent) {
858                 pagecontent = document.getElementById("content");
859             }
860             el_new_tree.setAttribute('id','scormtree123');
861             el_new_tree.innerHTML = o.responseText;
862             // Make sure it does not show.
863             el_new_tree.style.display = 'none';
864             pagecontent.appendChild(el_new_tree);
865             // Ignore the first level element as this is the title.
866             var startNode = el_new_tree.firstChild.firstChild;
867             if (startNode.tagName == 'LI') {
868                 // Go back to the beginning.
869                 startNode = el_new_tree;
870             }
871             var toc_source = Y.one('#scormtree123 > ul');
872             var toc = mod_scorm_parse_toc_tree(toc_source);
873             scorm_tree_node.appendNode(scorm_tree_node.rootNode, toc);
874             var el = document.getElementById('scormtree123');
875             el.parentNode.removeChild(el);
876             scorm_tree_node.render();
877             scorm_tree_node.openAll();
878             if (stitle !== null) {
879                 snode = scorm_tree_node.getNodeByAttribute('title', stitle);
880                 // Do not let destroyed node to be selected.
881                 if (snode && !snode.state.destroyed) {
882                     snode.select();
883                     var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
884                     if (!toc_disabled) {
885                         if (!snode.state.selected) {
886                             snode.select();
887                         }
888                     }
889                 }
890             }
891         }
892     },
894     failure: function(id, o) {
895         // TODO: do some sort of error handling.
896     }