1 // This file is part of Moodle - http://moodle.org/
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.
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/>.
17 * Javascript helper function for SCORM module.
20 * @copyright 2009 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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,"__");
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;
45 if (nav_display !== 0) {
46 scorm_hide_nav = false;
48 scorm_hide_toc = false;
49 } else if (hide_toc == 3) {
50 scorm_disable_toc = true;
53 scoes_nav = Y.JSON.parse(scoes_nav);
55 var scorm_update_siblings = function (scoesnav) {
56 for(var key in scoesnav ){
59 for (var mk in scoesnav) {
60 var val = scoesnav[mk];
61 if (typeof val !== "undefined" && typeof val.parentscoid !== 'undefined' && val.parentscoid === parentscoid) {
65 if (siblings.length > 1) {
66 scoesnav = scorm_get_siblings(scoesnav, siblings);
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];
77 if (index < siblings.length - 1 && typeof scoesnav[key] !== "undefined" &&
78 typeof scoesnav[key].nextsibling === "undefined") {
79 scoesnav[key].nextsibling = siblings[index + 1];
85 scoes_nav = scorm_update_siblings(scoes_nav);
86 var scorm_buttons = [];
87 var scorm_bloody_labelclick = false;
90 Y.use('button', 'dd-plugin', 'panel', 'resize', 'gallery-sm-treeview', function(Y) {
92 Y.TreeView.prototype.getNodeByAttribute = function(attribute, value) {
94 domnode = Y.one('a[' + attribute + '="' + value + '"]');
95 if (domnode !== null) {
96 node = scorm_tree_node.getNodeById(domnode.ancestor('li').get('id'));
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();
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();
113 var scorm_parse_toc_tree = function(srcNode) {
117 textlabel : '> li, > span',
118 subtree: '> ul, > li'
122 srcNode.all(SELECTORS.child).each(function(childNode) {
124 labelNode = childNode.one(SELECTORS.label),
125 textNode = childNode.one(SELECTORS.textlabel),
126 subTreeNode = childNode.one(SELECTORS.subtree);
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 !== '#') {
136 if (typeof scoid !== 'undefined') {
139 } else if (textNode) {
140 // The selector did not find a label node with anchor.
141 child.label = textNode.get('outerHTML');
145 child.children = scorm_parse_toc_tree(subTreeNode);
148 children.push(child);
154 mod_scorm_parse_toc_tree = scorm_parse_toc_tree;
156 var scorm_activate_item = function(node) {
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');
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) {
169 // Start to unload iframe here
171 content.removeChild(old);
175 // End of - Avoid recursive calls.
177 scorm_current_node = node;
178 if (!scorm_current_node.state.selected) {
179 scorm_current_node.select();
182 scorm_tree_node.closeAll();
183 var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
184 var el_old_api = document.getElementById('scormapi123');
186 el_old_api.parentNode.removeChild(el_old_api);
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);
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
201 if (typeof mod_scorm_monitorForBeaconRequirement !== 'undefined') {
202 mod_scorm_monitorForBeaconRequirement(obj);
205 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
207 alert(M.util.get_string('popupsblocked', 'scorm'));
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);
221 content.prepend(obj);
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);
230 scorm_tree_node.openAll();
233 mod_scorm_activate_item = scorm_activate_item;
236 * Enables/disables navigation buttons as needed.
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));
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') {
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('>')
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('<')
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);
309 toc.removeClass(cssclasses.disabled)
310 .removeAttribute('disabled-by');
311 scorm_toc_toggle_btn.setHTML('<')
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);
316 toc.addClass(cssclasses.disabled)
317 .setAttribute('disabled-by', 'user');
318 scorm_toc_toggle_btn.setHTML('>')
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);
325 var scorm_resize_layout = function() {
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));
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)) {
347 Y.one('#scorm_layout').setStyle('height', newheight);
352 * @deprecated as it is now unused.
353 * @param {string} url
354 * @param {string} datastring
355 * @returns {string|*|boolean}
357 var scorm_ajax_request = function(url, datastring) {
358 var myRequest = NewHttpReq();
359 var result = DoRequest(myRequest, url + datastring);
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;
374 if (update_launch_sco) {
375 launch_sco = parentscoid;
382 var scorm_lastchild = function(node) {
383 if (node.children.length) {
384 return scorm_lastchild(node.children[node.children.length - 1]);
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());
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);
399 node = scorm_tree_node.rootNode.children[0];
400 node.title = scoes_nav[prevscoid].url;
403 if (update_launch_sco) {
404 launch_sco = prevscoid;
411 return scorm_skipprev(node, update_launch_sco);
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;
426 if (update_launch_sco) {
427 launch_sco = prevsibling;
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;
440 if (update_launch_sco) {
441 launch_sco = parentscoid;
448 var scorm_next = function(node, update_launch_sco, test) {
449 if (node === false) {
450 return scorm_tree_node.children[0];
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);
458 node = scorm_tree_node.rootNode.children[0];
459 node.title = scoes_nav[nextscoid].url;
462 if (update_launch_sco) {
463 launch_sco_fix = nextscoid;
465 launch_sco = launch_sco_fix;
470 return scorm_skipnext(node, update_launch_sco, test);
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);
481 next = scorm_tree_node.rootNode.children[0];
482 next.title = scoes_nav[nextsibling].url;
485 if (update_launch_sco) {
486 launch_sco_fix = nextsibling;
488 launch_sco = launch_sco_fix;
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];
501 if (update_launch_sco) {
502 launch_sco_fix = parentscoid;
504 launch_sco = launch_sco_fix;
507 return scorm_skipnext(parent, update_launch_sco, test);
513 * Sends a request to the sequencing handler script on the server.
514 * @param {string} datastring
515 * @returns {string|boolean|*}
517 var scorm_dorequest_sequencing = function(datastring) {
518 var myRequest = NewHttpReq();
519 var result = DoRequest(
521 M.cfg.wwwroot + '/mod/scorm/datamodels/sequencinghandler.php?' + datastring,
529 var scorm_launch_prev_sco = function() {
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.
541 result = Y.JSON.parse(result);
544 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
545 var node = scorm_prev(scorm_tree_node.getSelectedNodes()[0]);
547 // Avoid use of TreeView for Navigation.
548 node = scorm_tree_node.getSelectedNodes()[0];
550 if (node.title !== scoes_nav[result.nextactivity.id].url) {
551 node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
553 node = scorm_tree_node.rootNode.children[0];
554 node.title = scoes_nav[result.nextactivity.id].url;
557 launch_sco = result.nextactivity.id;
558 scorm_activate_item(node);
561 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
564 scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
569 var scorm_launch_next_sco = function () {
570 launch_sco_fix = launch_sco;
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.
582 result = Y.JSON.parse(result);
585 if (typeof result.nextactivity !== 'undefined' && typeof result.nextactivity.id !== 'undefined') {
586 var node = scorm_next(scorm_tree_node.getSelectedNodes()[0]);
588 // Avoid use of TreeView for Navigation.
589 node = scorm_tree_node.getSelectedNodes()[0];
591 node = scorm_tree_node.getNodeByAttribute('title', scoes_nav[result.nextactivity.id].url);
593 node = scorm_tree_node.rootNode.children[0];
594 node.title = scoes_nav[result.nextactivity.id].url;
596 launch_sco = result.nextactivity.id;
597 launch_sco_fix = launch_sco;
598 scorm_activate_item(node);
601 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true, false));
604 scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true,false));
608 mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
609 mod_scorm_launch_next_sco = scorm_launch_next_sco;
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',
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);
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')
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);
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')
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);
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({
663 defMinWidth: 0.2 * layout_width
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');
671 var tree = new Y.TreeView({
672 container: '#scorm_tree',
677 scorm_tree_node = tree;
678 // Trigger after instead of on, avoid recursive calls.
679 tree.after('select', function(e) {
681 if (node.title == '' || node.title == null) {
682 return; //this item has no navigation
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) {
692 } else if(scorm_current_node == node){
696 // Update launch_sco.
697 if (typeof node.scoid !== 'undefined') {
698 launch_sco = node.scoid;
700 scorm_activate_item(node);
701 if (node.children.length) {
702 scorm_bloody_labelclick = true;
705 if (!scorm_disable_toc) {
706 tree.on('close', function(e) {
707 if (scorm_bloody_labelclick) {
708 scorm_bloody_labelclick = false;
712 tree.subscribe('open', function(e) {
713 if (scorm_bloody_labelclick) {
714 scorm_bloody_labelclick = false;
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();
732 if (scorm_hide_nav == false) {
733 // TODO: make some better&accessible buttons.
734 var navbuttonshtml = '<span id="scorm_nav"><button id="nav_skipprev"><<</button> ' +
735 '<button id="nav_prev"><</button> <button id="nav_up">^</button> ' +
736 '<button id="nav_next">></button> <button id="nav_skipnext">>></button></span>';
737 if (nav_display === 1) {
738 Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
740 // Nav panel is floating type.
741 var navposition = null;
742 if (navposition_left < 0 && navposition_top < 0) {
744 navposition = Y.one('#scorm_toc').getXY();
745 navposition[1] += 200;
747 // Set user defined XY.
749 navposition[0] = parseInt(navposition_left, 10);
750 navposition[1] = parseInt(navposition_top, 10);
752 scorm_nav_panel = new Y.Panel({
754 headerContent: M.util.get_string('navigation', 'scorm'),
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();
765 scorm_buttons[0] = new Y.Button({
766 srcNode: '#nav_skipprev',
769 'click' : function(ev) {
770 scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
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));
779 scorm_buttons[1] = new Y.Button({
780 srcNode: '#nav_prev',
783 'click' : function(ev) {
784 scorm_launch_prev_sco();
786 'keydown' : function(ev) {
787 if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
788 scorm_launch_prev_sco();
793 scorm_buttons[2] = new Y.Button({
797 'click' : function(ev) {
798 scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
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));
807 scorm_buttons[3] = new Y.Button({
808 srcNode: '#nav_next',
811 'click' : function(ev) {
812 scorm_launch_next_sco();
814 'keydown' : function(ev) {
815 if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode === 32) {
816 scorm_launch_next_sco();
821 scorm_buttons[4] = new Y.Button({
822 srcNode: '#nav_skipnext',
825 'click' : function(ev) {
826 launch_sco_fix = launch_sco;
827 scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true, false));
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));
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);
845 scorm_first_url = node;
848 scorm_first_url = tree.rootNode.children[0];
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;
855 scorm_first_url.title = scoes_nav[launch_sco].url;
856 scorm_activate_item(scorm_first_url);
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);
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);
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));
884 M.mod_scorm.connectPrereqCallback = {
886 success: function(id, o) {
887 if (o.responseText !== undefined) {
890 if (scorm_tree_node && o.responseText) {
891 snode = scorm_tree_node.getSelectedNodes()[0];
894 stitle = snode.title;
896 // All gone with clear, add new root node.
897 scorm_tree_node.clear(scorm_tree_node.createNode());
899 // Make sure the temporary tree element is not there.
900 var el_old_tree = document.getElementById('scormtree123');
902 el_old_tree.parentNode.removeChild(el_old_tree);
904 var el_new_tree = document.createElement('div');
905 var pagecontent = document.getElementById("page-content");
907 pagecontent = document.getElementById("content");
910 pagecontent = document.getElementById("scormpage");
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;
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) {
935 var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
937 if (!snode.state.selected) {
946 failure: function(id, o) {
947 // TODO: do some sort of error handling.