Merge branch 'MDL-48255-27' of git://github.com/lameze/moodle into MOODLE_27_STABLE
[moodle.git] / mod / scorm / module.js
blobfb9eae510a5f2663b2d540c055b55e4d0bd08e1f
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 M.mod_scorm = {};
32 M.mod_scorm.init = function(Y, nav_display, navposition_left, navposition_top, hide_toc, collapsetocwinsize, toc_title, window_name, launch_sco, scoes_nav) {
33     var scorm_disable_toc = false;
34     var scorm_hide_nav = true;
35     var scorm_hide_toc = true;
36     if (hide_toc == 0) {
37         if (nav_display !== 0) {
38             scorm_hide_nav = false;
39         }
40         scorm_hide_toc = false;
41     } else if (hide_toc == 3) {
42         scorm_disable_toc = true;
43     }
45     scoes_nav = Y.JSON.parse(scoes_nav);
46     var scorm_current_node;
47     var scorm_buttons = [];
48     var scorm_bloody_labelclick = false;
49     var scorm_nav_panel;
51     Y.use('button', 'dd-plugin', 'panel', 'resize', 'gallery-sm-treeview', function(Y) {
53         Y.TreeView.prototype.getNodeByAttribute = function(attribute, value) {
54             var node = null,
55                 domnode = Y.one('a[' + attribute + '="' + value + '"]');
56             if (domnode !== null) {
57                 node = scorm_tree_node.getNodeById(domnode.ancestor('li').get('id'));
58             }
59             return node;
60         };
62         Y.TreeView.prototype.openAll = function () {
63             this.get('container').all('.yui3-treeview-can-have-children').each(function(target) {
64                 this.getNodeById(target.get('id')).open();
65             }, this);
66         };
68         Y.TreeView.prototype.closeAll = function () {
69             this.get('container').all('.yui3-treeview-can-have-children').each(function(target) {
70                 this.getNodeById(target.get('id')).close();
71             }, this);
72         }
74         var scorm_parse_toc_tree = function(srcNode) {
75             var SELECTORS = {
76                     child: '> li',
77                     label: '> li, > a',
78                     textlabel : '> li, > span',
79                     subtree: '> ul, > li'
80                 },
81                 children = [];
83             srcNode.all(SELECTORS.child).each(function(childNode) {
84                 var child = {},
85                     labelNode = childNode.one(SELECTORS.label),
86                     textNode = childNode.one(SELECTORS.textlabel),
87                     subTreeNode = childNode.one(SELECTORS.subtree);
89                 if (labelNode) {
90                     var title = labelNode.getAttribute('title');
91                     var scoid = labelNode.getData('scoid');
92                     child.label = labelNode.get('outerHTML');
93                     // Will be good to change to url instead of title.
94                     if (title && title !== '#') {
95                         child.title = title;
96                     }
97                     if (typeof scoid !== 'undefined') {
98                         child.scoid = scoid;
99                     }
100                 } else if (textNode) {
101                     // The selector did not find a label node with anchor.
102                     child.label = textNode.get('outerHTML');
103                 }
105                 if (subTreeNode) {
106                     child.children = scorm_parse_toc_tree(subTreeNode);
107                 }
109                 children.push(child);
110             });
112             return children;
113         };
115         mod_scorm_parse_toc_tree = scorm_parse_toc_tree;
117         var scorm_activate_item = function(node) {
118             if (!node) {
119                 return;
120             }
121             // Check if the item is already active, avoid recursive calls.
122             if (Y.one('#scorm_object')) {
123                 var scorm_active_url = Y.one('#scorm_object').getAttribute('src');
124                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
125                 if (node_full_url === scorm_active_url) {
126                     return;
127                 }
128             }
129             scorm_current_node = node;
130             // Avoid recursive calls.
131             if (!scorm_current_node.state.selected) {
132                 scorm_current_node.select();
133             }
135             scorm_tree_node.closeAll();
136             // remove any reference to the old API
137             if (window.API) {
138                 window.API = null;
139             }
140             if (window.API_1484_11) {
141                 window.API_1484_11 = null;
142             }
143             var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
144             var el_old_api = document.getElementById('scormapi123');
145             if (el_old_api) {
146                 el_old_api.parentNode.removeChild(el_old_api);
147             }
149             if (node.title) {
150                 var el_scorm_api = document.getElementById("external-scormapi");
151                 el_scorm_api.parentNode.removeChild(el_scorm_api);
152                 el_scorm_api = document.createElement('script');
153                 el_scorm_api.setAttribute('id','external-scormapi');
154                 el_scorm_api.setAttribute('type','text/javascript');
155                 var pel_scorm_api = document.getElementById('scormapi-parent');
156                 pel_scorm_api.appendChild(el_scorm_api);
157                 var api_url = M.cfg.wwwroot + '/mod/scorm/loaddatamodel.php?' + node.title;
158                 document.getElementById('external-scormapi').src = api_url;
159             }
161             var content = Y.one('#scorm_content');
162             var obj = document.createElement('iframe');
163             obj.setAttribute('id', 'scorm_object');
164             obj.setAttribute('type', 'text/html');
165             if (!window_name && node.title != null) {
166                 obj.setAttribute('src', url_prefix + node.title);
167             }
168             if (window_name) {
169                 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
170                 if(! mine) {
171                     alert(M.str.scorm.popupsblocked);
172                 }
173                 mine.close();
174             }
176             var old = Y.one('#scorm_object');
177             if (old) {
178                 if(window_name) {
179                     var cwidth = scormplayerdata.cwidth;
180                     var cheight = scormplayerdata.cheight;
181                     var poptions = scormplayerdata.popupoptions;
182                     poptions = poptions + ',resizable=yes'; // Added for IE (MDL-32506).
183                     scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
184                 } else {
185                     content.replaceChild(obj, old);
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             newheight = Y.one('body').get('winHeight') -5;
308             if (newheight < 600) {
309                 newheight = 600;
310             }
311             Y.one('#scorm_layout').setStyle('height', newheight);
313         };
315         // Handle AJAX Request
316         var scorm_ajax_request = function(url, datastring) {
317             var myRequest = NewHttpReq();
318             var result = DoRequest(myRequest, url + datastring);
319             return result;
320         };
322         var scorm_up = function(node, update_launch_sco) {
323             if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
324                 var parentscoid = scoes_nav[launch_sco].parentscoid;
325                 var parent = node.parent;
326                 if (parent.title !== scoes_nav[parentscoid].url) {
327                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
328                     if (parent === null) {
329                         parent = scorm_tree_node.rootNode.children[0];
330                         parent.title = scoes_nav[parentscoid].url;
331                     }
332                 }
333                 if (update_launch_sco) {
334                     launch_sco = parentscoid;
335                 }
336                 return parent;
337             }
338             return null;
339         };
341         var scorm_lastchild = function(node) {
342             if (node.children.length) {
343                 return scorm_lastchild(node.children[node.children.length-1]);
344             } else {
345                 return node;
346             }
347         };
349         var scorm_prev = function(node, update_launch_sco) {
350             if (node.previous() && node.previous().children.length &&
351                     typeof scoes_nav[launch_sco].prevscoid !== 'undefined') {
352                 node = scorm_lastchild(node.previous());
353                 if (node) {
354                     var prevscoid = scoes_nav[launch_sco].prevscoid;
355                     if (node.title !== scoes_nav[prevscoid].url) {
356                         node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevscoid].url);
357                         if (node === null) {
358                             node = scorm_tree_node.rootNode.children[0];
359                             node.title = scoes_nav[prevscoid].url;
360                         }
361                     }
362                     if (update_launch_sco) {
363                         launch_sco = prevscoid;
364                     }
365                     return node;
366                 } else {
367                     return null;
368                 }
369             }
370             return scorm_skipprev(node, update_launch_sco);
371         };
373         var scorm_skipprev = function(node, update_launch_sco) {
374             if (node.previous() && typeof scoes_nav[launch_sco].prevsibling !== 'undefined') {
375                 var prevsibling = scoes_nav[launch_sco].prevsibling;
376                 var previous = node.previous();
377                 var prevscoid = scoes_nav[launch_sco].prevscoid;
378                 if (previous.title !== scoes_nav[prevscoid].url) {
379                     previous = scorm_tree_node.getNodeByAttribute('title', scoes_nav[prevsibling].url);
380                     if (previous === null) {
381                         previous = scorm_tree_node.rootNode.children[0];
382                         previous.title = scoes_nav[prevsibling].url;
383                     }
384                 }
385                 if (update_launch_sco) {
386                     launch_sco = prevsibling;
387                 }
388                 return previous;
389             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
390                 var parentscoid = scoes_nav[launch_sco].parentscoid;
391                 var parent = node.parent;
392                 if (parent.title !== scoes_nav[parentscoid].url) {
393                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
394                     if (parent === null) {
395                         parent = scorm_tree_node.rootNode.children[0];
396                         parent.title = scoes_nav[parentscoid].url;
397                     }
398                 }
399                 if (update_launch_sco) {
400                     launch_sco = parentscoid;
401                 }
402                 return parent;
403             }
404             return null;
405         };
407         var scorm_next = function(node, update_launch_sco) {
408             if (node === false) {
409                 return scorm_tree_node.children[0];
410             }
411             if (node.children.length && typeof scoes_nav[launch_sco].nextscoid != 'undefined') {
412                 node = node.children[0];
413                 var nextscoid = scoes_nav[launch_sco].nextscoid;
414                 if (node.title !== scoes_nav[nextscoid].url) {
415                     node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextscoid].url);
416                     if (node === null) {
417                         node = scorm_tree_node.rootNode.children[0];
418                         node.title = scoes_nav[nextscoid].url;
419                     }
420                 }
421                 if (update_launch_sco) {
422                     launch_sco = nextscoid;
423                 }
424                 return node;
425             }
426             return scorm_skipnext(node, update_launch_sco);
427         };
429         var scorm_skipnext = function(node, update_launch_sco) {
430             var next = node.next();
431             if (next && next.title && typeof scoes_nav[launch_sco] !== 'undefined' && typeof scoes_nav[launch_sco].nextsibling !== 'undefined') {
432                 var nextsibling = scoes_nav[launch_sco].nextsibling;
433                 if (next.title !== scoes_nav[nextsibling].url) {
434                     next = scorm_tree_node.getNodeByAttribute('title', scoes_nav[nextsibling].url);
435                     if (next === null) {
436                         next = scorm_tree_node.rootNode.children[0];
437                         next.title = scoes_nav[nextsibling].url;
438                     }
439                 }
440                 if (update_launch_sco) {
441                     launch_sco = nextsibling;
442                 }
443                 return next;
444             } else if (node.parent && node.parent.parent && typeof scoes_nav[launch_sco].parentscoid !== 'undefined') {
445                 var parentscoid = scoes_nav[launch_sco].parentscoid;
446                 var parent = node.parent;
447                 if (parent.title !== scoes_nav[parentscoid].url) {
448                     parent = scorm_tree_node.getNodeByAttribute('title', scoes_nav[parentscoid].url);
449                     if (parent === null) {
450                         parent = scorm_tree_node.rootNode.children[0];
451                     }
452                 }
453                 if (update_launch_sco) {
454                     launch_sco = parentscoid;
455                 }
456                 return scorm_skipnext(parent, update_launch_sco);
457             }
458             return null;
459         };
461         // Launch prev sco
462         var scorm_launch_prev_sco = function() {
463             var result = null;
464             if (scoes_nav[launch_sco].flow === 1) {
465                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=backward';
466                 result = scorm_ajax_request(M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?', datastring);
467                 mod_scorm_seq = encodeURIComponent(result);
468                 result = Y.JSON.parse (result);
469                 if (typeof result.nextactivity.id != undefined) {
470                         var node = scorm_prev(scorm_tree_node.getSelectedNodes()[0]);
471                         if (node == null) {
472                             // Avoid use of TreeView for Navigation.
473                             node = scorm_tree_node.getSelectedNodes()[0];
474                         }
475                         if (node.title !== scoes_nav[result.nextactivity.id].url) {
476                             node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
477                             if (node === null) {
478                                 node = scorm_tree_node.rootNode.children[0];
479                                 node.title = scoes_nav[result.nextactivity.id].url;
480                             }
481                         }
482                         launch_sco = result.nextactivity.id;
483                         scorm_activate_item(node);
484                         scorm_fixnav();
485                 } else {
486                         scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
487                 }
488             } else {
489                 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
490             }
491         };
493         // Launch next sco
494         var scorm_launch_next_sco = function () {
495             var result = null;
496             if (scoes_nav[launch_sco].flow === 1) {
497                 var datastring = scoes_nav[launch_sco].url + '&function=scorm_seq_flow&request=forward';
498                 result = scorm_ajax_request(M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?', datastring);
499                 mod_scorm_seq = encodeURIComponent(result);
500                 result = Y.JSON.parse (result);
501                 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
502                     var node = scorm_next(scorm_tree_node.getSelectedNodes()[0]);
503                     if (node === null) {
504                         // Avoid use of TreeView for Navigation.
505                         node = scorm_tree_node.getSelectedNodes()[0];
506                     }
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                     launch_sco = result.nextactivity.id;
513                     scorm_activate_item(node);
514                     scorm_fixnav();
515                 } else {
516                     scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
517                 }
518             } else {
519                 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
520             }
521         };
523         mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
524         mod_scorm_launch_next_sco = scorm_launch_next_sco;
526         var cssclasses = {
527                 // YUI grid class: use 100% of the available width to show only content, TOC hidden.
528                 scorm_grid_content_toc_hidden: 'yui3-u-1',
529                 // YUI grid class: use 1/5 of the available width to show TOC.
530                 scorm_grid_toc: 'yui3-u-1-5',
531                 // YUI grid class: use 1/24 of the available width to show TOC toggle button.
532                 scorm_grid_toggle: 'yui3-u-1-24',
533                 // YUI grid class: use 3/4 of the available width to show content, TOC visible.
534                 scorm_grid_content_toc_visible: 'yui3-u-3-4',
535                 // Reduce height of #scorm_object to accomodate nav buttons under content.
536                 scorm_nav_under_content: 'scorm_nav_under_content',
537                 disabled: 'disabled'
538             };
539         // layout
540         Y.one('#scorm_toc_title').setHTML(toc_title);
542         if (scorm_disable_toc) {
543             Y.one('#scorm_toc').addClass(cssclasses.disabled);
544             Y.one('#scorm_toc_toggle').addClass(cssclasses.disabled);
545             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_hidden);
546         } else {
547             Y.one('#scorm_toc').addClass(cssclasses.scorm_grid_toc);
548             Y.one('#scorm_toc_toggle').addClass(cssclasses.scorm_grid_toggle);
549             Y.one('#scorm_toc_toggle_btn')
550                 .setHTML('&lt;')
551                 .setAttribute('title', M.util.get_string('hide', 'moodle'));
552             Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_visible);
553             scorm_toggle_toc(true);
554         }
556         // hide the TOC if that is the default
557         if (!scorm_disable_toc) {
558             if (scorm_hide_toc == true) {
559                 Y.one('#scorm_toc').addClass(cssclasses.disabled);
560                 Y.one('#scorm_toc_toggle_btn')
561                     .setHTML('&gt;')
562                     .setAttribute('title', M.util.get_string('show', 'moodle'));
563                 Y.one('#scorm_content')
564                     .removeClass(cssclasses.scorm_grid_content_toc_visible)
565                     .addClass(cssclasses.scorm_grid_content_toc_hidden);
566             }
567         }
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             }
594             // If item is already active, return; avoid recursive calls.
595             if (Y.one('#scorm_data')) {
596                 var scorm_active_url = Y.one('#scorm_object').getAttribute('src');
597                 var node_full_url = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?' + node.title;
598                 if (node_full_url === scorm_active_url) {
599                     return;
600                 }
601             }
602             // Update launch_sco.
603             if (typeof node.scoid !== 'undefined') {
604                 launch_sco = node.scoid;
605             }
606             scorm_activate_item(node);
607             if (node.children.length) {
608                 scorm_bloody_labelclick = true;
609             }
610         });
611         if (!scorm_disable_toc) {
612             tree.on('close', function(e) {
613                 if (scorm_bloody_labelclick) {
614                     scorm_bloody_labelclick = false;
615                     return false;
616                 }
617             });
618             tree.subscribe('open', function(e) {
619                 if (scorm_bloody_labelclick) {
620                     scorm_bloody_labelclick = false;
621                     return false;
622                 }
623             });
624         }
625         tree.render();
626         tree.openAll();
628         // On getting the window, always set the focus on the current item
629         Y.one(Y.config.win).on('focus', function (e) {
630             var current = scorm_tree_node.getSelectedNodes()[0];
631             var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
632             if (current.id && !toc_disabled) {
633                 Y.one('#' + current.id).focus();
634             }
635         });
637         // navigation
638         if (scorm_hide_nav == false) {
639             // TODO: make some better&accessible buttons.
640             var navbuttonshtml = '<span id="scorm_nav"><button id="nav_skipprev">&lt;&lt;</button>&nbsp;<button id="nav_prev">&lt;</button>'
641                     + '&nbsp;<button id="nav_up">^</button>&nbsp;<button id="nav_next">&gt;</button>'
642                     + '&nbsp;<button id="nav_skipnext">&gt;&gt;</button></span>';
643             if (nav_display === 1) {
644                 Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
645             } else {
646                 // Nav panel is floating type.
647                 var navposition = null;
648                 if (navposition_left < 0 && navposition_top < 0) {
649                     // Set default XY.
650                     navposition = Y.one('#scorm_toc').getXY();
651                     navposition[1] += 200;
652                 } else {
653                     // Set user defined XY.
654                     navposition = [];
655                     navposition[0] = parseInt(navposition_left, 10);
656                     navposition[1] = parseInt(navposition_top, 10);
657                 }
658                 scorm_nav_panel = new Y.Panel({
659                     fillHeight: "body",
660                     headerContent: M.util.get_string('navigation', 'scorm'),
661                     visible: true,
662                     xy: navposition,
663                     zIndex: 999
664                 });
665                 scorm_nav_panel.set('bodyContent', navbuttonshtml);
666                 scorm_nav_panel.removeButton('close');
667                 scorm_nav_panel.plug(Y.Plugin.Drag, {handles: ['.yui3-widget-hd']});
668                 scorm_nav_panel.render();
669             }
671             scorm_buttons[0] = new Y.Button({
672                 srcNode: '#nav_skipprev',
673                 render: true,
674                 on: {
675                         'click' : function(ev) {
676                             scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
677                         },
678                         'keydown' : function(ev) {
679                             if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
680                                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
681                             }
682                         }
683                     }
684             });
685             scorm_buttons[1] = new Y.Button({
686                 srcNode: '#nav_prev',
687                 render: true,
688                 on: {
689                     'click' : function(ev) {
690                         scorm_launch_prev_sco();
691                     },
692                     'keydown' : function(ev) {
693                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
694                             scorm_launch_prev_sco();
695                         }
696                     }
697                 }
698             });
699             scorm_buttons[2] = new Y.Button({
700                 srcNode: '#nav_up',
701                 render: true,
702                 on: {
703                     'click' : function(ev) {
704                         scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
705                     },
706                     'keydown' : function(ev) {
707                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
708                             scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
709                         }
710                     }
711                 }
712             });
713             scorm_buttons[3] = new Y.Button({
714                 srcNode: '#nav_next',
715                 render: true,
716                 on: {
717                     'click' : function(ev) {
718                         scorm_launch_next_sco();
719                     },
720                     'keydown' : function(ev) {
721                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
722                             scorm_launch_next_sco();
723                         }
724                     }
725                 }
726             });
727             scorm_buttons[4] = new Y.Button({
728                 srcNode: '#nav_skipnext',
729                 render: true,
730                 on: {
731                     'click' : function(ev) {
732                         scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
733                     },
734                     'keydown' : function(ev) {
735                         if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
736                             scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
737                         }
738                     }
739                 }
740             });
741         }
743         // finally activate the chosen item
744         var scorm_first_url = null;
745         if (typeof tree.rootNode.children[0] !== 'undefined') {
746             if (tree.rootNode.children[0].title !== scoes_nav[launch_sco].url) {
747                 var node = tree.getNodeByAttribute('title', scoes_nav[launch_sco].url);
748                 if (node !== null) {
749                     scorm_first_url = node;
750                 }
751             } else {
752                 scorm_first_url = tree.rootNode.children[0];
753             }
754         }
756         if (scorm_first_url == null) { // This is probably a single sco with no children (AICC Direct uses this).
757             scorm_first_url = tree.rootNode;
758         }
759         scorm_first_url.title = scoes_nav[launch_sco].url;
760         scorm_activate_item(scorm_first_url);
762         // resizing
763         scorm_resize_layout();
765         // Collapse/expand TOC.
766         Y.one('#scorm_toc_toggle').on('click', scorm_toggle_toc);
767         Y.one('#scorm_toc_toggle').on('key', scorm_toggle_toc, 'down:enter,32');
768         // fix layout if window resized
769         Y.on("windowresize", function() {
770             scorm_resize_layout();
771             var toc_displayed = Y.one('#scorm_toc').getComputedStyle('display') !== 'none';
772             if ((!scorm_disable_toc && !scorm_hide_toc) || toc_displayed) {
773                 scorm_toggle_toc(true);
774             }
775             // Set 20% as minWidth constrain of TOC.
776             var layout_width = parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
777             scorm_resize_handle.set('defMinWidth', 0.2 * layout_width);
778         });
779         // On resize drag, change width of scorm_content.
780         scorm_resize_handle.on('resize:resize', function() {
781             var tocwidth = parseInt(Y.one('#scorm_toc').getComputedStyle('width'), 10);
782             var layoutwidth = parseInt(Y.one('#scorm_layout').getStyle('width'), 10);
783             Y.one('#scorm_content').setStyle('width', (layoutwidth - tocwidth - 60));
784         });
785     });
788 M.mod_scorm.connectPrereqCallback = {
790     success: function(id, o) {
791         if (o.responseText !== undefined) {
792             var snode = null,
793                 stitle = null;
794             if (scorm_tree_node && o.responseText) {
795                 snode = scorm_tree_node.getSelectedNodes()[0];
796                 stitle = null;
797                 if (snode) {
798                     stitle = snode.title;
799                 }
800                 // All gone with clear, add new root node.
801                 scorm_tree_node.clear(scorm_tree_node.createNode());
802             }
803             // Make sure the temporary tree element is not there.
804             var el_old_tree = document.getElementById('scormtree123');
805             if (el_old_tree) {
806                 el_old_tree.parentNode.removeChild(el_old_tree);
807             }
808             var el_new_tree = document.createElement('div');
809             var pagecontent = document.getElementById("page-content");
810             if (!pagecontent) {
811                 pagecontent = document.getElementById("content");
812             }
813             el_new_tree.setAttribute('id','scormtree123');
814             el_new_tree.innerHTML = o.responseText;
815             // Make sure it does not show.
816             el_new_tree.style.display = 'none';
817             pagecontent.appendChild(el_new_tree);
818             // Ignore the first level element as this is the title.
819             var startNode = el_new_tree.firstChild.firstChild;
820             if (startNode.tagName == 'LI') {
821                 // Go back to the beginning.
822                 startNode = el_new_tree;
823             }
824             var toc_source = Y.one('#scormtree123 > ul');
825             var toc = mod_scorm_parse_toc_tree(toc_source);
826             scorm_tree_node.appendNode(scorm_tree_node.rootNode, toc);
827             var el = document.getElementById('scormtree123');
828             el.parentNode.removeChild(el);
829             scorm_tree_node.render();
830             scorm_tree_node.openAll();
831             if (stitle !== null) {
832                 snode = scorm_tree_node.getNodeByAttribute('title', stitle);
833                 // Do not let destroyed node to be selected.
834                 if (snode && !snode.state.destroyed) {
835                     snode.select();
836                     var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
837                     if (!toc_disabled) {
838                         if (!snode.state.selected) {
839                             snode.select();
840                         }
841                     }
842                 }
843             }
844         }
845     },
847     failure: function(id, o) {
848         // TODO: do some sort of error handling.
849     }