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 IMS Content Package 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_next = null;
25 mod_scorm_prev = null;
26 mod_scorm_activate_item = null;
30 M.mod_scorm.init = function(Y, hide_nav, hide_toc, toc_title, window_name, launch_sco) {
31 var scorm_disable_toc = false;
32 var scorm_hide_nav = true;
33 var scorm_hide_toc = true;
36 scorm_hide_nav = false;
38 scorm_hide_toc = false;
39 } else if (hide_toc == 3) {
40 scorm_disable_toc = true;
43 var scorm_layout_widget;
44 var scorm_current_node;
45 var scorm_buttons = [];
46 var scorm_bloody_labelclick = false;
49 Y.use('yui2-resize', 'yui2-dragdrop', 'yui2-container', 'yui2-button', 'yui2-layout', 'yui2-treeview', 'yui2-json', 'yui2-event', function(Y) {
51 YAHOO.widget.TextNode.prototype.getContentHtml = function() {
53 sb[sb.length] = this.href ? '<a' : '<span';
54 sb[sb.length] = ' id="' + YAHOO.lang.escapeHTML(this.labelElId) + '"';
55 sb[sb.length] = ' class="' + YAHOO.lang.escapeHTML(this.labelStyle) + '"';
57 sb[sb.length] = ' href="' + YAHOO.lang.escapeHTML(this.href) + '"';
58 sb[sb.length] = ' target="' + YAHOO.lang.escapeHTML(this.target) + '"';
61 sb[sb.length] = ' title="' + YAHOO.lang.escapeHTML(this.title) + '"';
64 sb[sb.length] = this.label;
65 sb[sb.length] = this.href?'</a>':'</span>';
69 var scorm_activate_item = function(node) {
73 scorm_current_node = node;
74 scorm_current_node.highlight();
76 // remove any reference to the old API
80 if (window.API_1484_11) {
81 window.API_1484_11 = null;
83 var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
84 var el_old_api = document.getElementById('scormapi123');
86 el_old_api.parentNode.removeChild(el_old_api);
90 var el_scorm_api = document.getElementById("external-scormapi");
91 el_scorm_api.parentNode.removeChild(el_scorm_api);
92 el_scorm_api = document.createElement('script');
93 el_scorm_api.setAttribute('id','external-scormapi');
94 el_scorm_api.setAttribute('type','text/javascript');
95 var pel_scorm_api = document.getElementById('scormapi-parent');
96 pel_scorm_api.appendChild(el_scorm_api);
97 var api_url = M.cfg.wwwroot + '/mod/scorm/api.php?' + node.title;
98 document.getElementById('external-scormapi').src = api_url;
101 var content = new YAHOO.util.Element('scorm_content');
103 // first try IE way - it can not set name attribute later
104 // and also it has some restrictions on DOM access from object tag
105 if (window_name || node.title == null) {
106 var obj = document.createElement('<iframe id="scorm_object" src="">');
108 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
110 alert(M.str.scorm.popupsblocked);
116 var obj = document.createElement('<iframe id="scorm_object" src="'+url_prefix + node.title+'">');
118 // fudge IE7 to redraw the screen
119 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
120 obj.attachEvent("onload", scorm_resize_parent);
123 var obj = document.createElement('object');
124 obj.setAttribute('id', 'scorm_object');
125 obj.setAttribute('type', 'text/html');
126 if (!window_name && node.title != null) {
127 obj.setAttribute('data', url_prefix + node.title);
130 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
132 alert(M.str.scorm.popupsblocked);
137 var old = YAHOO.util.Dom.get('scorm_object');
140 var cwidth = scormplayerdata.cwidth;
141 var cheight = scormplayerdata.cheight;
142 var poptions = scormplayerdata.popupoptions;
143 scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
145 content.replaceChild(obj, old);
148 content.appendChild(obj);
151 scorm_resize_frame();
153 var left = scorm_layout_widget.getUnitByPosition('left');
155 scorm_current_node.focus();
157 if (scorm_hide_nav == false) {
162 mod_scorm_activate_item = scorm_activate_item;
165 * Enables/disables navigation buttons as needed.
168 var scorm_fixnav = function() {
169 scorm_buttons[0].set('disabled', (scorm_skipprev(scorm_current_node) == null || scorm_skipprev(scorm_current_node).title == null));
170 scorm_buttons[1].set('disabled', (scorm_prev(scorm_current_node) == null || scorm_prev(scorm_current_node).title == null));
171 scorm_buttons[2].set('disabled', (scorm_up(scorm_current_node) == null) || scorm_up(scorm_current_node).title == null);
172 scorm_buttons[3].set('disabled', (scorm_next(scorm_current_node) == null) || scorm_next(scorm_current_node).title == null);
173 scorm_buttons[4].set('disabled', (scorm_skipnext(scorm_current_node) == null || scorm_skipnext(scorm_current_node).title == null));
176 var scorm_resize_parent = function() {
177 // fudge IE7 to redraw the screen
178 parent.resizeBy(-10, -10);
179 parent.resizeBy(10, 10);
180 var ifr = YAHOO.util.Dom.get('scorm_object');
182 ifr.detachEvent("onload", scorm_resize_parent);
186 var scorm_resize_layout = function(alsowidth) {
192 scorm_layout_widget.setStyle('width', '');
193 var newwidth = scorm_get_htmlelement_size('content', 'width');
195 // make sure that the max width of the TOC doesn't go to far
197 var left = scorm_layout_widget.getUnitByPosition('left');
198 var maxwidth = parseInt(YAHOO.util.Dom.getStyle('scorm_layout', 'width'));
199 left.set('maxWidth', (maxwidth - 50));
200 var cwidth = left.get('width');
201 if (cwidth > (maxwidth - 1)) {
202 left.set('width', (maxwidth - 50));
205 scorm_layout_widget.setStyle('height', '100%');
206 var center = scorm_layout_widget.getUnitByPosition('center');
207 center.setStyle('height', '100%');
209 // calculate the rough new height
210 newheight = YAHOO.util.Dom.getViewportHeight() -5;
211 if (newheight < 600) {
214 scorm_layout_widget.set('height', newheight);
216 scorm_layout_widget.render();
217 scorm_resize_frame();
219 if (scorm_nav_panel) {
220 scorm_nav_panel.align('bl', 'bl');
224 var scorm_get_htmlelement_size = function(el, prop) {
225 var val = YAHOO.util.Dom.getStyle(el, prop);
228 el = el.get('element'); // get real HTMLElement from YUI element
230 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
232 return parseInt(val);
235 var scorm_resize_frame = function() {
236 var obj = YAHOO.util.Dom.get('scorm_object');
238 var content = scorm_layout_widget.getUnitByPosition('center').get('wrap');
239 // basically trap IE6 and 7
240 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
241 if( obj.style.setAttribute ) {
242 obj.style.setAttribute("cssText", 'width: ' +(content.offsetWidth - 6)+'px; height: ' + (content.offsetHeight - 10)+'px;');
245 obj.style.setAttribute('width', (content.offsetWidth - 6)+'px', 0);
246 obj.style.setAttribute('height', (content.offsetHeight - 10)+'px', 0);
250 obj.style.width = (content.offsetWidth)+'px';
251 obj.style.height = (content.offsetHeight - 10)+'px';
256 var scorm_up = function(node) {
257 var node = scorm_tree_node.getHighlightedNode();
258 if (node.depth > 0) {
264 var scorm_lastchild = function(node) {
265 if (node.children.length) {
266 return scorm_lastchild(node.children[node.children.length-1]);
272 var scorm_prev = function(node) {
273 if (node.previousSibling && node.previousSibling.children.length) {
274 return scorm_lastchild(node.previousSibling);
276 return scorm_skipprev(node);
279 var scorm_skipprev = function(node) {
280 if (node.previousSibling) {
281 return node.previousSibling;
282 } else if (node.depth > 0) {
288 var scorm_next = function(node) {
289 if (node === false) {
290 return scorm_tree_node.getRoot().children[0];
292 if (node.children.length) {
293 return node.children[0];
295 return scorm_skipnext(node);
298 var scorm_skipnext = function(node) {
299 if (node.nextSibling) {
300 return node.nextSibling;
301 } else if (node.depth > 0) {
302 return scorm_skipnext(node.parent);
307 mod_scorm_next = scorm_next;
308 mod_scorm_prev = scorm_prev;
311 YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
312 YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
314 if (scorm_disable_toc) {
315 scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
319 { position: 'left', body: 'scorm_toc', header: toc_title, width: 0, resize: true, gutter: '0px 0px 0px 0px', collapse: false},
320 { position: 'center', body: '<div id="scorm_content"></div>', gutter: '0px 0px 0px 0px', scroll: true}
324 scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
328 { position: 'left', body: 'scorm_toc', header: toc_title, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:250, maxWidth: 590},
329 { position: 'center', body: '<div id="scorm_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
334 scorm_layout_widget.render();
335 var left = scorm_layout_widget.getUnitByPosition('left');
336 if (!scorm_disable_toc) {
337 left.on('collapse', function() {
338 scorm_resize_frame();
340 left.on('expand', function() {
341 scorm_resize_frame();
344 // ugly resizing hack that works around problems with resizing of iframes and objects
345 left._resize.on('startResize', function() {
346 var obj = YAHOO.util.Dom.get('scorm_object');
347 obj.style.display = 'none';
349 left._resize.on('endResize', function() {
350 var obj = YAHOO.util.Dom.get('scorm_object');
351 obj.style.display = 'block';
352 scorm_resize_frame();
355 // hide the TOC if that is the default
356 if (!scorm_disable_toc) {
357 if (scorm_hide_toc == true) {
362 var tree = new YAHOO.widget.TreeView('scorm_tree');
363 scorm_tree_node = tree;
364 tree.singleNodeHighlight = true;
365 tree.subscribe('labelClick', function(node) {
366 if (node.title == '' || node.title == null) {
367 return; //this item has no navigation
369 scorm_activate_item(node);
370 if (node.children.length) {
371 scorm_bloody_labelclick = true;
374 if (!scorm_disable_toc) {
375 tree.subscribe('collapse', function(node) {
376 if (scorm_bloody_labelclick) {
377 scorm_bloody_labelclick = false;
381 tree.subscribe('expand', function(node) {
382 if (scorm_bloody_labelclick) {
383 scorm_bloody_labelclick = false;
392 if (scorm_hide_nav == false) {
393 scorm_nav_panel = new YAHOO.widget.Panel('scorm_navpanel', { visible:true, draggable:true, close:false, xy: [250, 450],
394 autofillheight: "body"} );
395 scorm_nav_panel.setHeader(M.str.scorm.navigation);
397 //TODO: make some better&accessible buttons
398 scorm_nav_panel.setBody('<span id="scorm_nav"><button id="nav_skipprev"><<</button><button id="nav_prev"><</button><button id="nav_up">^</button><button id="nav_next">></button><button id="nav_skipnext">>></button></span>');
399 scorm_nav_panel.render();
400 scorm_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
401 scorm_buttons[1] = new YAHOO.widget.Button('nav_prev');
402 scorm_buttons[2] = new YAHOO.widget.Button('nav_up');
403 scorm_buttons[3] = new YAHOO.widget.Button('nav_next');
404 scorm_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
405 scorm_buttons[0].on('click', function(ev) {
406 scorm_activate_item(scorm_skipprev(scorm_tree_node.getHighlightedNode()));
408 scorm_buttons[1].on('click', function(ev) {
409 scorm_activate_item(scorm_prev(scorm_tree_node.getHighlightedNode()));
411 scorm_buttons[2].on('click', function(ev) {
412 scorm_activate_item(scorm_up(scorm_tree_node.getHighlightedNode()));
414 scorm_buttons[3].on('click', function(ev) {
415 scorm_activate_item(scorm_next(scorm_tree_node.getHighlightedNode()));
417 scorm_buttons[4].on('click', function(ev) {
418 scorm_activate_item(scorm_skipnext(scorm_tree_node.getHighlightedNode()));
420 scorm_nav_panel.render();
423 // finally activate the chosen item
424 var scorm_first_url = tree.getRoot().children[0];
426 while (nxt = scorm_next(nxt)) {
428 expression = new RegExp('^.*?scoid=' + launch_sco + '.*?$');
429 matches = nxt.title.match(expression);
430 if (matches != null) {
431 scorm_first_url = nxt;
436 scorm_activate_item(scorm_first_url);
439 scorm_resize_layout(false);
441 // fix layout if window resized
442 window.onresize = function() {
443 scorm_resize_layout(true);
449 function scorm_get_prev() {
450 scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
451 if (scorm_tree_node) {
452 var hnode = scorm_tree_node.getHighlightedNode();
453 var prev = mod_scorm_prev(hnode);
455 mod_scorm_activate_item(prev);
460 function scorm_get_next() {
461 scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
462 if (scorm_tree_node) {
463 var hnode = scorm_tree_node.getHighlightedNode();
464 var next = mod_scorm_next(hnode);
466 mod_scorm_activate_item(next);