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