MDL-78251 tiny: Reduce use of UI for setting test expectations
[moodle.git] / mod / scorm / module.js
blobef0fff596582c73b2fb6a585db393160fdd9d2ac
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     var launch_sco_fix = launch_sco;
44     if (hide_toc == 0) {
45         if (nav_display !== 0) {
46             scorm_hide_nav = false;
47         }
48         scorm_hide_toc = false;
49     } else if (hide_toc == 3) {
50         scorm_disable_toc = true;
51     }
53     scoes_nav = Y.JSON.parse(scoes_nav);
55     var scorm_update_siblings = function (scoesnav) {
56         for(var key in scoesnav ){
57             var siblings = [],
58                 parentscoid = key;
59             for (var mk in scoesnav) {
60                 var val = scoesnav[mk];
61                 if (typeof val !== "undefined" && typeof val.parentscoid !== 'undefined' && val.parentscoid === parentscoid) {
62                     siblings.push(mk);
63                 }
64             }
65             if (siblings.length > 1) {
66                 scoesnav = scorm_get_siblings(scoesnav, siblings);
67             }
68         }
69         return scoesnav;
70     };
72     var scorm_get_siblings = function (scoesnav, siblings) {
73         siblings.forEach(function (key, index) {
74             if (index > 0 && typeof scoesnav[key] !== "undefined" && typeof scoesnav[key].prevsibling === "undefined") {
75                 scoesnav[key].prevsibling = siblings[index - 1];
76             }
77             if (index < siblings.length - 1 && typeof scoesnav[key] !== "undefined" &&
78                typeof scoesnav[key].nextsibling === "undefined") {
79                 scoesnav[key].nextsibling = siblings[index + 1];
80             }
81         });
82         return scoesnav;
83     };
85     scoes_nav = scorm_update_siblings(scoes_nav);
86     var scorm_buttons = [];
87     var scorm_bloody_labelclick = false;
88     var scorm_nav_panel;
90     Y.use('button', 'dd-plugin', 'panel', 'resize', 'gallery-sm-treeview', function(Y) {
92         Y.TreeView.prototype.getNodeByAttribute = function(attribute, value) {
93             var node = null,
94                 domnode = Y.one('a[' + attribute + '="' + value + '"]');
95             if (domnode !== null) {
96                 node = scorm_tree_node.getNodeById(domnode.ancestor('li').get('id'));
97             }
98             return node;
99         };
101         Y.TreeView.prototype.openAll = function () {
102             this.get('container').all('.yui3-treeview-can-have-children').each(function(target) {
103                 this.getNodeById(target.get('id')).open();
104             }, this);
105         };
107         Y.TreeView.prototype.closeAll = function () {
108             this.get('container').all('.yui3-treeview-can-have-children').each(function(target) {
109                 this.getNodeById(target.get('id')).close();
110             }, this);
111         }
113         var scorm_parse_toc_tree = function(srcNode) {
114             var SELECTORS = {
115                     child: '> li',
116                     label: '> li, > a',
117                     textlabel : '> li, > span',
118                     subtree: '> ul, > li'
119                 },
120                 children = [];
122             srcNode.all(SELECTORS.child).each(function(childNode) {
123                 var child = {},
124                     labelNode = childNode.one(SELECTORS.label),
125                     textNode = childNode.one(SELECTORS.textlabel),
126                     subTreeNode = childNode.one(SELECTORS.subtree);
128                 if (labelNode) {
129                     var title = labelNode.getAttribute('title');
130                     var scoid = labelNode.getData('scoid');
131                     child.label = labelNode.get('outerHTML');
132                     // Will be good to change to url instead of title.
133                     if (title && title !== '#') {
134                         child.title = title;
135                     }
136                     if (typeof scoid !== 'undefined') {
137                         child.scoid = scoid;
138                     }
139                 } else if (textNode) {
140                     // The selector did not find a label node with anchor.
141                     child.label = textNode.get('outerHTML');
142                 }
144                 if (subTreeNode) {
145                     child.children = scorm_parse_toc_tree(subTreeNode);
146                 }
148                 children.push(child);
149             });
151             return children;
152         };
154         mod_scorm_parse_toc_tree = scorm_parse_toc_tree;
156         var scorm_activate_item = function(node) {
157             if (!node) {
158                 return;
159             }
160             // Check if the item is already active, avoid recursive calls.
161             var content = Y.one('#scorm_content');
162             var old = Y.one('#scorm_object');
163             if (old) {
164                 var scorm_active_url = Y.one('#scorm_object').getAttribute('src');
165                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
166                 if (node_full_url === scorm_active_url) {
167                     return;
168                 }
169                 // Start to unload iframe here
170                 if(!window_name){
171                     content.removeChild(old);
172                     old = null;
173                 }
174             }
175             // End of - Avoid recursive calls.
177             scorm_current_node = node;
178             if (!scorm_current_node.state.selected) {
179                 scorm_current_node.select();
180             }
182             scorm_tree_node.closeAll();
183             var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
184             var el_old_api = document.getElementById('scormapi123');
185             if (el_old_api) {
186                 el_old_api.parentNode.removeChild(el_old_api);
187             }
189             var obj = document.createElement('iframe');
190             obj.setAttribute('id', 'scorm_object');
191             obj.setAttribute('type', 'text/html');
192             obj.setAttribute('allowfullscreen', 'allowfullscreen');
193             obj.setAttribute('webkitallowfullscreen', 'webkitallowfullscreen');
194             obj.setAttribute('mozallowfullscreen', 'mozallowfullscreen');
195             if (!window_name && node.title != null) {
196                 obj.setAttribute('src', url_prefix + node.title);
197             }
198             // Attach unload observers to the iframe. The scorm package may be observing these unload events
199             // and trying to save progress when they occur. We need to ensure we use the Beacon API in those
200             // situations.
201             if (typeof mod_scorm_monitorForBeaconRequirement !== 'undefined') {
202                 mod_scorm_monitorForBeaconRequirement(obj);
203             }
204             if (window_name) {
205                 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
206                 if(! mine) {
207                     alert(M.util.get_string('popupsblocked', 'scorm'));
208                 }
209                 mine.close();
210             }
212             if (old) {
213                 if(window_name) {
214                     var cwidth = scormplayerdata.cwidth;
215                     var cheight = scormplayerdata.cheight;
216                     var poptions = scormplayerdata.popupoptions;
217                     poptions = poptions + ',resizable=yes'; // Added for IE (MDL-32506).
218                     scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
219                 }
220             } else {
221                 content.prepend(obj);
222             }
224             if (scorm_hide_nav == false) {
225                 if (nav_display === 1 && navposition_left > 0 && navposition_top > 0) {
226                     Y.one('#scorm_object').addClass(cssclasses.scorm_nav_under_content);
227                 }
228                 scorm_fixnav();
229             }
230             scorm_tree_node.openAll();
231         };
233         mod_scorm_activate_item = scorm_activate_item;
235         /**
236          * Enables/disables navigation buttons as needed.
237          * @return void
238          */
239         var scorm_fixnav = function() {
240             launch_sco_fix = launch_sco;
241             var skipprevnode = scorm_skipprev(scorm_current_node);
242             var prevnode = scorm_prev(scorm_current_node);
243             var upnode = scorm_up(scorm_current_node);
244             var nextnode = scorm_next(scorm_current_node, true, true);
245             var skipnextnode = scorm_skipnext(scorm_current_node, true, true);
247             scorm_buttons[0].set('disabled', ((skipprevnode === null) ||
248                         (typeof(skipprevnode.scoid) === 'undefined') ||
249                         (scoes_nav[skipprevnode.scoid].isvisible === "false") ||
250                         (skipprevnode.title === null) ||
251                         (scoes_nav[launch_sco].hideprevious === 1)));
253             scorm_buttons[1].set('disabled', ((prevnode === null) ||
254                         (typeof(prevnode.scoid) === 'undefined') ||
255                         (scoes_nav[prevnode.scoid].isvisible === "false") ||
256                         (prevnode.title === null) ||
257                         (scoes_nav[launch_sco].hideprevious === 1)));
259             scorm_buttons[2].set('disabled', (upnode === null) ||
260                         (typeof(upnode.scoid) === 'undefined') ||
261                         (scoes_nav[upnode.scoid].isvisible === "false") ||
262                         (upnode.title === null));
264             scorm_buttons[3].set('disabled', ((nextnode === null) ||
265                         ((nextnode.title === null) && (scoes_nav[launch_sco].flow !== 1)) ||
266                         (typeof(nextnode.scoid) === 'undefined') ||
267                         (scoes_nav[nextnode.scoid].isvisible === "false") ||
268                         (scoes_nav[launch_sco].hidecontinue === 1)));
270             scorm_buttons[4].set('disabled', ((skipnextnode === null) ||
271                         (skipnextnode.title === null) ||
272                         (typeof(skipnextnode.scoid) === 'undefined') ||
273                         (scoes_nav[skipnextnode.scoid].isvisible === "false") ||
274                         scoes_nav[launch_sco].hidecontinue === 1));
275         };
277         var scorm_toggle_toc = function(windowresize) {
278             var toc = Y.one('#scorm_toc');
279             var scorm_content = Y.one('#scorm_content');
280             var scorm_toc_toggle_btn = Y.one('#scorm_toc_toggle_btn');
281             var toc_disabled = toc.hasClass('disabled');
282             var disabled_by = toc.getAttribute('disabled-by');
283             // Remove width element style from resize handle.
284             toc.setStyle('width', null);
285             scorm_content.setStyle('width', null);
286             if (windowresize === true) {
287                 if (disabled_by === 'user') {
288                     return;
289                 }
290                 var body = Y.one('body');
291                 if (body.get('winWidth') < collapsetocwinsize) {
292                     toc.addClass(cssclasses.disabled)
293                         .setAttribute('disabled-by', 'screen-size');
294                     scorm_toc_toggle_btn.setHTML('&gt;')
295                         .set('title', M.util.get_string('show', 'moodle'));
296                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
297                         .addClass(cssclasses.scorm_grid_content_toc_hidden);
298                 } else if (body.get('winWidth') > collapsetocwinsize) {
299                     toc.removeClass(cssclasses.disabled)
300                         .removeAttribute('disabled-by');
301                     scorm_toc_toggle_btn.setHTML('&lt;')
302                         .set('title', M.util.get_string('hide', 'moodle'));
303                     scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
304                         .addClass(cssclasses.scorm_grid_content_toc_visible);
305                 }
306                 return;
307             }
308             if (toc_disabled) {
309                 toc.removeClass(cssclasses.disabled)
310                     .removeAttribute('disabled-by');
311                 scorm_toc_toggle_btn.setHTML('&lt;')
312                     .set('title', M.util.get_string('hide', 'moodle'));
313                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
314                     .addClass(cssclasses.scorm_grid_content_toc_visible);
315             } else {
316                 toc.addClass(cssclasses.disabled)
317                     .setAttribute('disabled-by', 'user');
318                 scorm_toc_toggle_btn.setHTML('&gt;')
319                     .set('title', M.util.get_string('show', 'moodle'));
320                 scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
321                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
322             }
323         };
325         var scorm_resize_layout = function() {
326             if (window_name) {
327                 return;
328             }
330             // make sure that the max width of the TOC doesn't go to far
332             var scorm_toc_node = Y.one('#scorm_toc');
333             var maxwidth = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
334             scorm_toc_node.setStyle('maxWidth', (maxwidth - 200));
335             var cwidth = parseInt(scorm_toc_node.getComputedStyle('width'), 10);
336             if (cwidth > (maxwidth - 1)) {
337                 scorm_toc_node.setStyle('width', (maxwidth - 50));
338             }
340             // Calculate the rough new height from the viewport height.
341             var newheight = Y.one('body').get('winHeight') - 5
342                 - Y.one('#scorm_layout').getY()
343                 - window.pageYOffset;
344             if (newheight < 680 || isNaN(newheight)) {
345                 newheight = 680;
346             }
347             Y.one('#scorm_layout').setStyle('height', newheight);
349         };
351         /**
352          * @deprecated as it is now unused.
353          * @param {string} url
354          * @param {string} datastring
355          * @returns {string|*|boolean}
356          */
357         var scorm_ajax_request = function(url, datastring) {
358             var myRequest = NewHttpReq();
359             var result = DoRequest(myRequest, url + datastring);
360             return result;
361         };
363         var scorm_up = function(node, update_launch_sco) {
364             if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
365                 var parentscoid = scoes_nav[launch_sco].parentscoid;
366                 var parent = node.parent;
367                 if (parent.title !== scoes_nav[parentscoid].url) {
368                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
369                     if (parent === null) {
370                         parent = scorm_tree_node.rootNode.children[0];
371                         parent.title = scoes_nav[parentscoid].url;
372                     }
373                 }
374                 if (update_launch_sco) {
375                     launch_sco = parentscoid;
376                 }
377                 return parent;
378             }
379             return null;
380         };
382         var scorm_lastchild = function(node) {
383             if (node.children.length) {
384                 return scorm_lastchild(node.children[node.children.length - 1]);
385             } else {
386                 return node;
387             }
388         };
390         var scorm_prev = function(node, update_launch_sco) {
391             if (node.previous() && node.previous().children.length &&
392                     typeof scoes_nav[launch_sco].prevscoid !== 'undefined') {
393                 node = scorm_lastchild(node.previous());
394                 if (node) {
395                     var prevscoid = scoes_nav[launch_sco].prevscoid;
396                     if (node.title !== scoes_nav[prevscoid].url) {
397                         node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevscoid].url);
398                         if (node === null) {
399                             node = scorm_tree_node.rootNode.children[0];
400                             node.title = scoes_nav[prevscoid].url;
401                         }
402                     }
403                     if (update_launch_sco) {
404                         launch_sco = prevscoid;
405                     }
406                     return node;
407                 } else {
408                     return null;
409                 }
410             }
411             return scorm_skipprev(node, update_launch_sco);
412         };
414         var scorm_skipprev = function(node, update_launch_sco) {
415             if (node.previous() && typeof scoes_nav[launch_sco].prevsibling !== 'undefined') {
416                 var prevsibling = scoes_nav[launch_sco].prevsibling;
417                 var previous = node.previous();
418                 var prevscoid = scoes_nav[launch_sco].prevscoid;
419                 if (previous.title !== scoes_nav[prevscoid].url) {
420                     previous = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevsibling].url);
421                     if (previous === null) {
422                         previous = scorm_tree_node.rootNode.children[0];
423                         previous.title = scoes_nav[prevsibling].url;
424                     }
425                 }
426                 if (update_launch_sco) {
427                     launch_sco = prevsibling;
428                 }
429                 return previous;
430             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
431                 var parentscoid = scoes_nav[launch_sco].parentscoid;
432                 var parent = node.parent;
433                 if (parent.title !== scoes_nav[parentscoid].url) {
434                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
435                     if (parent === null) {
436                         parent = scorm_tree_node.rootNode.children[0];
437                         parent.title = scoes_nav[parentscoid].url;
438                     }
439                 }
440                 if (update_launch_sco) {
441                     launch_sco = parentscoid;
442                 }
443                 return parent;
444             }
445             return null;
446         };
448         var scorm_next = function(node, update_launch_sco, test) {
449             if (node === false) {
450                 return scorm_tree_node.children[0];
451             }
452             if (node.children.length && typeof scoes_nav[launch_sco_fix].nextscoid != 'undefined') {
453                 node = node.children[0];
454                 var nextscoid = scoes_nav[launch_sco_fix].nextscoid;
455                 if (node.title !== scoes_nav[nextscoid].url) {
456                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextscoid].url);
457                     if (node === null) {
458                         node = scorm_tree_node.rootNode.children[0];
459                         node.title = scoes_nav[nextscoid].url;
460                     }
461                 }
462                 if (update_launch_sco) {
463                     launch_sco_fix = nextscoid;
464                     if (!test) {
465                         launch_sco = launch_sco_fix;
466                     }
467                 }
468                 return node;
469             }
470             return scorm_skipnext(node, update_launch_sco, test);
471         };
473         var scorm_skipnext = function(node, update_launch_sco, test) {
474             var next = node.next();
475             if (next && next.title && typeof scoes_nav[launch_sco_fix] !== 'undefined' &&
476                         typeof scoes_nav[launch_sco_fix].nextsibling !== 'undefined') {
477                 var nextsibling = scoes_nav[launch_sco_fix].nextsibling;
478                 if (next.title !== scoes_nav[nextsibling].url) {
479                     next = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextsibling].url);
480                     if (next === null) {
481                         next = scorm_tree_node.rootNode.children[0];
482                         next.title = scoes_nav[nextsibling].url;
483                     }
484                 }
485                 if (update_launch_sco) {
486                     launch_sco_fix = nextsibling;
487                     if (!test) {
488                         launch_sco = launch_sco_fix;
489                     }
490                 }
491                 return next;
492             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco_fix].parentscoid !== 'undefined') {
493                 var parentscoid = scoes_nav[launch_sco_fix].parentscoid;
494                 var parent = node.parent;
495                 if (parent.title !== scoes_nav[parentscoid].url) {
496                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
497                     if (parent === null) {
498                         parent = scorm_tree_node.rootNode.children[0];
499                     }
500                 }
501                 if (update_launch_sco) {
502                     launch_sco_fix = parentscoid;
503                     if (!test) {
504                         launch_sco = launch_sco_fix;
505                     }
506                 }
507                 return scorm_skipnext(parent, update_launch_sco, test);
508             }
509             return null;
510         };
512         /**
513          * Sends a request to the sequencing handler script on the server.
514          * @param {string} datastring
515          * @returns {string|boolean|*}
516          */
517         var scorm_dorequest_sequencing = function(datastring) {
518             var myRequest = NewHttpReq();
519             var result = DoRequest(
520                 myRequest,
521                 M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?' + datastring,
522                 '',
523                 false
524             );
525             return result;
526         };
528         // Launch prev sco
529         var scorm_launch_prev_sco = function() {
530             var result = null;
531             if (scoes_nav[launch_sco].flow === 1) {
532                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=backward';
533                 result = scorm_dorequest_sequencing(datastring);
535                 // Check the scorm_ajax_result, it may be false.
536                 if (result === false) {
537                     // Either the outcome was a failure, or we are unloading and simply just don't know
538                     // what the outcome actually was.
539                     result = {};
540                 } else {
541                     result = Y.JSON.parse(result);
542                 }
544                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
545                         var node = scorm_prev(scorm_tree_node.getSelectedNodes()[0]);
546                         if (node == null) {
547                             // Avoid use of TreeView for Navigation.
548                             node = scorm_tree_node.getSelectedNodes()[0];
549                         }
550                         if (node.title !== scoes_nav[result.nextactivity.id].url) {
551                             node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
552                             if (node === null) {
553                                 node = scorm_tree_node.rootNode.children[0];
554                                 node.title = scoes_nav[result.nextactivity.id].url;
555                             }
556                         }
557                         launch_sco = result.nextactivity.id;
558                         scorm_activate_item(node);
559                         scorm_fixnav();
560                 } else {
561                         scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
562                 }
563             } else {
564                 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
565             }
566         };
568         // Launch next sco
569         var scorm_launch_next_sco = function () {
570             launch_sco_fix = launch_sco;
571             var result = null;
572             if (scoes_nav[launch_sco].flow === 1) {
573                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=forward';
574                 result = scorm_dorequest_sequencing(datastring);
576                 // Check the scorm_ajax_result, it may be false.
577                 if (result === false) {
578                     // Either the outcome was a failure, or we are unloading and simply just don't know
579                     // what the outcome actually was.
580                     result = {};
581                 } else {
582                     result = Y.JSON.parse(result);
583                 }
585                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
586                     var node = scorm_next(scorm_tree_node.getSelectedNodes()[0]);
587                     if (node === null) {
588                         // Avoid use of TreeView for Navigation.
589                         node = scorm_tree_node.getSelectedNodes()[0];
590                     }
591                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
592                     if (node === null) {
593                         node = scorm_tree_node.rootNode.children[0];
594                         node.title = scoes_nav[result.nextactivity.id].url;
595                     }
596                     launch_sco = result.nextactivity.id;
597                     launch_sco_fix = launch_sco;
598                     scorm_activate_item(node);
599                     scorm_fixnav();
600                 } else {
601                     scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true, false));
602                 }
603             } else {
604                 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true,false));
605             }
606         };
608         mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
609         mod_scorm_launch_next_sco = scorm_launch_next_sco;
611         var cssclasses = {
612                 // YUI grid class: use 100% of the available width to show only content, TOC hidden.
613                 scorm_grid_content_toc_hidden: 'yui3-u-1',
614                 // YUI grid class: use 1/5 of the available width to show TOC.
615                 scorm_grid_toc: 'yui3-u-1-5',
616                 // YUI grid class: use 1/24 of the available width to show TOC toggle button.
617                 scorm_grid_toggle: 'yui3-u-1-24',
618                 // YUI grid class: use 3/4 of the available width to show content, TOC visible.
619                 scorm_grid_content_toc_visible: 'yui3-u-3-4',
620                 // Reduce height of #scorm_object to accomodate nav buttons under content.
621                 scorm_nav_under_content: 'scorm_nav_under_content',
622                 disabled: 'disabled'
623             };
624         // layout
625         Y.one('#scorm_toc_title').setHTML(toc_title);
627         if (scorm_disable_toc) {
628             Y.one('#scorm_toc').addClass(cssclasses.disabled);
629             Y.one('#scorm_toc_toggle').addClass(cssclasses.disabled);
630             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_hidden);
631         } else {
632             Y.one('#scorm_toc').addClass(cssclasses.scorm_grid_toc);
633             Y.one('#scorm_toc_toggle').addClass(cssclasses.scorm_grid_toggle);
634             Y.one('#scorm_toc_toggle_btn')
635                 .setHTML('&lt;')
636                 .setAttribute('title', M.util.get_string('hide', 'moodle'));
637             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_visible);
638             scorm_toggle_toc(true);
639         }
641         // hide the TOC if that is the default
642         if (!scorm_disable_toc) {
643             if (scorm_hide_toc == true) {
644                 Y.one('#scorm_toc').addClass(cssclasses.disabled);
645                 Y.one('#scorm_toc_toggle_btn')
646                     .setHTML('&gt;')
647                     .setAttribute('title', M.util.get_string('show', 'moodle'));
648                 Y.one('#scorm_content')
649                     .removeClass(cssclasses.scorm_grid_content_toc_visible)
650                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
651             }
652         }
654         // Basic initialization completed, show the elements.
655         Y.one('#scorm_toc').removeClass('loading');
656         Y.one('#scorm_toc_toggle').removeClass('loading');
658         // TOC Resize handle.
659         var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
660         var scorm_resize_handle = new Y.Resize({
661             node: '#scorm_toc',
662             handles: 'r',
663             defMinWidth: 0.2 * layout_width
664         });
665         // TOC tree
666         var toc_source = Y.one('#scorm_tree > ul');
667         var toc = scorm_parse_toc_tree(toc_source);
668         // Empty container after parsing toc.
669         var el = document.getElementById('scorm_tree');
670         el.innerHTML = '';
671         var tree = new Y.TreeView({
672             container: '#scorm_tree',
673             nodes: toc,
674             multiSelect: false,
675             lazyRender: false
676         });
677         scorm_tree_node = tree;
678         // Trigger after instead of on, avoid recursive calls.
679         tree.after('select', function(e) {
680             var node = e.node;
681             if (node.title == '' || node.title == null) {
682                 return; //this item has no navigation
683             }
685             // If item is already active, return; avoid recursive calls.
686             if (obj = Y.one('#scorm_object')) {
687                 var scorm_active_url = obj.getAttribute('src');
688                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
689                 if (node_full_url === scorm_active_url) {
690                     return;
691                 }
692             } else if(scorm_current_node == node){
693                 return;
694             }
696             // Update launch_sco.
697             if (typeof node.scoid !== 'undefined') {
698                 launch_sco = node.scoid;
699             }
700             scorm_activate_item(node);
701             if (node.children.length) {
702                 scorm_bloody_labelclick = true;
703             }
704         });
705         if (!scorm_disable_toc) {
706             tree.on('close', function(e) {
707                 if (scorm_bloody_labelclick) {
708                     scorm_bloody_labelclick = false;
709                     return false;
710                 }
711             });
712             tree.subscribe('open', function(e) {
713                 if (scorm_bloody_labelclick) {
714                     scorm_bloody_labelclick = false;
715                     return false;
716                 }
717             });
718         }
719         tree.render();
720         tree.openAll();
722         // On getting the window, always set the focus on the current item
723         Y.one(Y.config.win).on('focus', function (e) {
724             var current = scorm_tree_node.getSelectedNodes()[0];
725             var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
726             if (current.id && !toc_disabled) {
727                 Y.one('#' + current.id).focus();
728             }
729         });
731         // navigation
732         if (scorm_hide_nav == false) {
733             // TODO: make some better&accessible buttons.
734             var navbuttonshtml = '<span id="scorm_nav"><button id="nav_skipprev">&lt;&lt;</button>&nbsp;' +
735                                     '<button id="nav_prev">&lt;</button>&nbsp;<button id="nav_up">^</button>&nbsp;' +
736                                     '<button id="nav_next">&gt;</button>&nbsp;<button id="nav_skipnext">&gt;&gt;</button></span>';
737             if (nav_display === 1) {
738                 Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
739             } else {
740                 // Nav panel is floating type.
741                 var navposition = null;
742                 if (navposition_left < 0 && navposition_top < 0) {
743                     // Set default XY.
744                     navposition = Y.one('#scorm_toc').getXY();
745                     navposition[1] += 200;
746                 } else {
747                     // Set user defined XY.
748                     navposition = [];
749                     navposition[0] = parseInt(navposition_left, 10);
750                     navposition[1] = parseInt(navposition_top, 10);
751                 }
752                 scorm_nav_panel = new Y.Panel({
753                     fillHeight: "body",
754                     headerContent: M.util.get_string('navigation', 'scorm'),
755                     visible: true,
756                     xy: navposition,
757                     zIndex: 999
758                 });
759                 scorm_nav_panel.set('bodyContent', navbuttonshtml);
760                 scorm_nav_panel.removeButton('close');
761                 scorm_nav_panel.plug(Y.Plugin.Drag, {handles: ['.yui3-widget-hd']});
762                 scorm_nav_panel.render();
763             }
765             scorm_buttons[0] = new Y.Button({
766                 srcNode: '#nav_skipprev',
767                 render: true,
768                 on: {
769                         'click' : function(ev) {
770                             scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
771                         },
772                         'keydown' : function(ev) {
773                             if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
774                                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
775                             }
776                         }
777                     }
778             });
779             scorm_buttons[1] = new Y.Button({
780                 srcNode: '#nav_prev',
781                 render: true,
782                 on: {
783                     'click' : function(ev) {
784                         scorm_launch_prev_sco();
785                     },
786                     'keydown' : function(ev) {
787                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
788                             scorm_launch_prev_sco();
789                         }
790                     }
791                 }
792             });
793             scorm_buttons[2] = new Y.Button({
794                 srcNode: '#nav_up',
795                 render: true,
796                 on: {
797                     'click' : function(ev) {
798                         scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
799                     },
800                     'keydown' : function(ev) {
801                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
802                             scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
803                         }
804                     }
805                 }
806             });
807             scorm_buttons[3] = new Y.Button({
808                 srcNode: '#nav_next',
809                 render: true,
810                 on: {
811                     'click' : function(ev) {
812                         scorm_launch_next_sco();
813                     },
814                     'keydown' : function(ev) {
815                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
816                             scorm_launch_next_sco();
817                         }
818                     }
819                 }
820             });
821             scorm_buttons[4] = new Y.Button({
822                 srcNode: '#nav_skipnext',
823                 render: true,
824                 on: {
825                     'click' : function(ev) {
826                         launch_sco_fix = launch_sco;
827                         scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true, false));
828                     },
829                     'keydown' : function(ev) {
830                         launch_sco_fix = launch_sco;
831                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
832                             scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true, false));
833                         }
834                     }
835                 }
836             });
837         }
839         // finally activate the chosen item
840         var scorm_first_url = null;
841         if (typeof tree.rootNode.children[0] !== 'undefined') {
842             if (tree.rootNode.children[0].title !== scoes_nav[launch_sco].url) {
843                 var node = tree.getNodeByAttribute('title', scoes_nav[launch_sco].url);
844                 if (node !== null) {
845                     scorm_first_url = node;
846                 }
847             } else {
848                 scorm_first_url = tree.rootNode.children[0];
849             }
850         }
852         if (scorm_first_url == null) { // This is probably a single sco with no children (AICC Direct uses this).
853             scorm_first_url = tree.rootNode;
854         }
855         scorm_first_url.title = scoes_nav[launch_sco].url;
856         scorm_activate_item(scorm_first_url);
858         // resizing
859         scorm_resize_layout();
861         // Collapse/expand TOC.
862         Y.one('#scorm_toc_toggle').on('click', scorm_toggle_toc);
863         Y.one('#scorm_toc_toggle').on('key', scorm_toggle_toc, 'down:enter,32');
864         // fix layout if window resized
865         Y.on("windowresize", function() {
866             scorm_resize_layout();
867             var toc_displayed = Y.one('#scorm_toc').getComputedStyle('display') !== 'none';
868             if ((!scorm_disable_toc && !scorm_hide_toc) || toc_displayed) {
869                 scorm_toggle_toc(true);
870             }
871             // Set 20% as minWidth constrain of TOC.
872             var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
873             scorm_resize_handle.set('defMinWidth', 0.2 * layout_width);
874         });
875         // On resize drag, change width of scorm_content.
876         scorm_resize_handle.on('resize:resize', function() {
877             var tocwidth = parseInt(Y.one('#scorm_toc').getComputedStyle('width'), 10);
878             var layoutwidth = parseInt(Y.one('#scorm_layout').getStyle('width'), 10);
879             Y.one('#scorm_content').setStyle('width', (layoutwidth - tocwidth - 60));
880         });
881     });
884 M.mod_scorm.connectPrereqCallback = {
886     success: function(id, o) {
887         if (o.responseText !== undefined) {
888             var snode = null,
889                 stitle = null;
890             if (scorm_tree_node && o.responseText) {
891                 snode = scorm_tree_node.getSelectedNodes()[0];
892                 stitle = null;
893                 if (snode) {
894                     stitle = snode.title;
895                 }
896                 // All gone with clear, add new root node.
897                 scorm_tree_node.clear(scorm_tree_node.createNode());
898             }
899             // Make sure the temporary tree element is not there.
900             var el_old_tree = document.getElementById('scormtree123');
901             if (el_old_tree) {
902                 el_old_tree.parentNode.removeChild(el_old_tree);
903             }
904             var el_new_tree = document.createElement('div');
905             var pagecontent = document.getElementById("page-content");
906             if (!pagecontent) {
907                 pagecontent = document.getElementById("content");
908             }
909             if (!pagecontent) {
910                 pagecontent = document.getElementById("scormpage");
911             }
912             el_new_tree.setAttribute('id','scormtree123');
913             el_new_tree.innerHTML = o.responseText;
914             // Make sure it does not show.
915             el_new_tree.style.display = 'none';
916             pagecontent.appendChild(el_new_tree);
917             // Ignore the first level element as this is the title.
918             var startNode = el_new_tree.firstChild.firstChild;
919             if (startNode.tagName == 'LI') {
920                 // Go back to the beginning.
921                 startNode = el_new_tree;
922             }
923             var toc_source = Y.one('#scormtree123 > ul');
924             var toc = mod_scorm_parse_toc_tree(toc_source);
925             scorm_tree_node.appendNode(scorm_tree_node.rootNode, toc);
926             var el = document.getElementById('scormtree123');
927             el.parentNode.removeChild(el);
928             scorm_tree_node.render();
929             scorm_tree_node.openAll();
930             if (stitle !== null) {
931                 snode = scorm_tree_node.getNodeByAttribute('title', stitle);
932                 // Do not let destroyed node to be selected.
933                 if (snode && !snode.state.destroyed) {
934                     snode.select();
935                     var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
936                     if (!toc_disabled) {
937                         if (!snode.state.selected) {
938                             snode.select();
939                         }
940                     }
941                 }
942             }
943         }
944     },
946     failure: function(id, o) {
947         // TODO: do some sort of error handling.
948     }