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;
35 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;
50 Y.use('yui2-resize', 'yui2-dragdrop', 'yui2-container', 'yui2-button', 'yui2-layout', 'yui2-treeview', 'yui2-json', 'yui2-event', function(Y) {
53 var scorm_activate_item = function(node) {
57 scorm_current_node = node;
58 scorm_current_node.highlight();
60 // remove any reference to the old API
64 if (window.API_1484_11) {
65 window.API_1484_11 = null;
67 var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
68 var el_old_api = document.getElementById('scormapi123');
70 el_old_api.parentNode.removeChild(el_old_api);
74 var el_scorm_api = document.getElementById("external-scormapi");
75 el_scorm_api.parentNode.removeChild(el_scorm_api);
76 el_scorm_api = document.createElement('script');
77 el_scorm_api.setAttribute('id','external-scormapi');
78 el_scorm_api.setAttribute('type','text/javascript');
79 var pel_scorm_api = document.getElementById('scormapi-parent');
80 pel_scorm_api.appendChild(el_scorm_api);
81 var api_url = M.cfg.wwwroot + '/mod/scorm/api.php?' + node.title;
82 document.getElementById('external-scormapi').src = api_url;
85 var content = new YAHOO.util.Element('scorm_content');
87 // first try IE way - it can not set name attribute later
88 // and also it has some restrictions on DOM access from object tag
89 if (window_name || node.title == null) {
90 var obj = document.createElement('<iframe id="scorm_object" src="">');
92 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
94 alert(M.str.scorm.popupsblocked);
100 var obj = document.createElement('<iframe id="scorm_object" src="'+url_prefix + node.title+'">');
102 // fudge IE7 to redraw the screen
103 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
104 obj.attachEvent("onload", scorm_resize_parent);
107 var obj = document.createElement('object');
108 obj.setAttribute('id', 'scorm_object');
109 obj.setAttribute('type', 'text/html');
110 if (!window_name && node.title != null) {
111 obj.setAttribute('data', url_prefix + node.title);
114 var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
116 alert(M.str.scorm.popupsblocked);
121 var old = YAHOO.util.Dom.get('scorm_object');
124 var cwidth = scormplayerdata.cwidth;
125 var cheight = scormplayerdata.cheight;
126 var poptions = scormplayerdata.popupoptions;
127 scorm_openpopup("loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
130 content.replaceChild(obj, old);
133 content.appendChild(obj);
136 scorm_resize_frame();
138 var left = scorm_layout_widget.getUnitByPosition('left');
140 scorm_current_node.focus();
142 if (scorm_hide_nav == false) {
147 mod_scorm_activate_item = scorm_activate_item;
150 * Enables/disables navigation buttons as needed.
153 var scorm_fixnav = function() {
154 scorm_buttons[0].set('disabled', (scorm_skipprev(scorm_current_node) == null));
155 scorm_buttons[1].set('disabled', (scorm_prev(scorm_current_node) == null));
156 scorm_buttons[2].set('disabled', (scorm_up(scorm_current_node) == null));
157 scorm_buttons[3].set('disabled', (scorm_next(scorm_current_node) == null));
158 scorm_buttons[4].set('disabled', (scorm_skipnext(scorm_current_node) == null));
161 var scorm_resize_parent = function() {
162 // fudge IE7 to redraw the screen
163 parent.resizeBy(-10, -10);
164 parent.resizeBy(10, 10);
165 var ifr = YAHOO.util.Dom.get('scorm_object');
167 ifr.detachEvent("onload", scorm_resize_parent);
171 var scorm_resize_layout = function(alsowidth) {
177 scorm_layout_widget.setStyle('width', '');
178 var newwidth = scorm_get_htmlelement_size('content', 'width');
180 // make sure that the max width of the TOC doesn't go to far
182 var left = scorm_layout_widget.getUnitByPosition('left');
183 var maxwidth = parseInt(YAHOO.util.Dom.getStyle('scorm_layout', 'width'));
184 left.set('maxWidth', (maxwidth - 10));
185 var cwidth = left.get('width');
186 if (cwidth > (maxwidth - 10)) {
187 left.set('width', (maxwidth - 10));
190 scorm_layout_widget.setStyle('height', '100%');
191 scorm_layout_widget.render();
192 scorm_resize_frame();
194 if (scorm_nav_panel) {
195 scorm_nav_panel.align('bl', 'bl');
199 var scorm_get_htmlelement_size = function(el, prop) {
200 var val = YAHOO.util.Dom.getStyle(el, prop);
203 el = el.get('element'); // get real HTMLElement from YUI element
205 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
207 return parseInt(val);
210 var scorm_resize_frame = function() {
211 var obj = YAHOO.util.Dom.get('scorm_object');
213 var content = scorm_layout_widget.getUnitByPosition('center').get('wrap');
214 // basically trap IE6 and 7
215 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
216 if( obj.style.setAttribute ) {
217 obj.style.setAttribute("cssText", 'width: ' +(content.offsetWidth - 6)+'px; height: ' + (content.offsetHeight - 10)+'px;');
220 obj.style.setAttribute('width', (content.offsetWidth - 6)+'px', 0);
221 obj.style.setAttribute('height', (content.offsetHeight - 10)+'px', 0);
225 obj.style.width = (content.offsetWidth - 6)+'px';
226 obj.style.height = (content.offsetHeight - 10)+'px';
232 var scorm_up = function(node) {
233 var node = scorm_tree_node.getHighlightedNode();
234 if (node.depth > 0) {
240 var scorm_lastchild = function(node) {
241 if (node.children.length) {
242 return scorm_lastchild(node.children[node.children.length-1]);
248 var scorm_prev = function(node) {
249 if (node.previousSibling && node.previousSibling.children.length) {
250 return scorm_lastchild(node.previousSibling);
252 return scorm_skipprev(node);
255 var scorm_skipprev = function(node) {
256 if (node.previousSibling) {
257 return node.previousSibling;
258 } else if (node.depth > 0) {
264 var scorm_next = function(node) {
265 if (node === false) {
266 return scorm_tree_node.getRoot().children[0];
268 if (node.children.length) {
269 return node.children[0];
271 return scorm_skipnext(node);
274 var scorm_skipnext = function(node) {
275 if (node.nextSibling) {
276 return node.nextSibling;
277 } else if (node.depth > 0) {
278 return scorm_skipnext(node.parent);
283 mod_scorm_next = scorm_next;
284 mod_scorm_prev = scorm_prev;
288 YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
289 YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
291 if (scorm_disable_toc) {
292 scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
296 { position: 'left', body: 'scorm_toc', header: toc_title, width: 0, resize: true, gutter: '0px 0px 0px 0px', collapse: false},
297 { position: 'center', body: '<div id="scorm_content"></div>', gutter: '0px 0px 0px 0px', scroll: true}
301 scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
305 { position: 'left', body: 'scorm_toc', header: toc_title, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:250, maxWidth: 590},
306 { position: 'center', body: '<div id="scorm_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
311 scorm_layout_widget.render();
312 var left = scorm_layout_widget.getUnitByPosition('left');
313 if (!scorm_disable_toc) {
314 left.on('collapse', function() {
315 scorm_resize_frame();
317 left.on('expand', function() {
318 scorm_resize_frame();
321 // ugly resizing hack that works around problems with resizing of iframes and objects
322 left._resize.on('startResize', function() {
323 var obj = YAHOO.util.Dom.get('scorm_object');
324 obj.style.display = 'none';
326 left._resize.on('endResize', function() {
327 var obj = YAHOO.util.Dom.get('scorm_object');
328 obj.style.display = 'block';
329 scorm_resize_frame();
332 // hide the TOC if that is the default
333 if (!scorm_disable_toc) {
334 if (scorm_hide_toc == true) {
339 var tree = new YAHOO.widget.TreeView('scorm_tree');
340 scorm_tree_node = tree;
341 tree.singleNodeHighlight = true;
342 tree.subscribe('labelClick', function(node) {
343 scorm_activate_item(node);
344 if (node.children.length) {
345 scorm_bloody_labelclick = true;
348 if (!scorm_disable_toc) {
349 tree.subscribe('collapse', function(node) {
350 if (scorm_bloody_labelclick) {
351 scorm_bloody_labelclick = false;
355 tree.subscribe('expand', function(node) {
356 if (scorm_bloody_labelclick) {
357 scorm_bloody_labelclick = false;
366 if (scorm_hide_nav == false) {
367 scorm_nav_panel = new YAHOO.widget.Panel('scorm_navpanel', { visible:true, draggable:true, close:false, xy: [250, 450],
368 autofillheight: "body"} );
369 scorm_nav_panel.setHeader(M.str.scorm.navigation);
371 //TODO: make some better&accessible buttons
372 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>');
373 scorm_nav_panel.render();
374 scorm_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
375 scorm_buttons[1] = new YAHOO.widget.Button('nav_prev');
376 scorm_buttons[2] = new YAHOO.widget.Button('nav_up');
377 scorm_buttons[3] = new YAHOO.widget.Button('nav_next');
378 scorm_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
379 scorm_buttons[0].on('click', function(ev) {
380 scorm_activate_item(scorm_skipprev(scorm_tree_node.getHighlightedNode()));
382 scorm_buttons[1].on('click', function(ev) {
383 scorm_activate_item(scorm_prev(scorm_tree_node.getHighlightedNode()));
385 scorm_buttons[2].on('click', function(ev) {
386 scorm_activate_item(scorm_up(scorm_tree_node.getHighlightedNode()));
388 scorm_buttons[3].on('click', function(ev) {
389 scorm_activate_item(scorm_next(scorm_tree_node.getHighlightedNode()));
391 scorm_buttons[4].on('click', function(ev) {
392 scorm_activate_item(scorm_skipnext(scorm_tree_node.getHighlightedNode()));
394 scorm_nav_panel.render();
397 // finally activate the chosen item
398 var scorm_first_url = tree.getRoot().children[0];
400 while (nxt = scorm_next(nxt)) {
402 expression = new RegExp('^.*?scoid=' + launch_sco + '.*?$');
403 matches = nxt.title.match(expression);
404 if (matches != null) {
405 scorm_first_url = nxt;
410 scorm_activate_item(scorm_first_url);
413 scorm_resize_layout(false);
415 // fix layout if window resized
416 window.onresize = function() {
417 scorm_resize_layout(true);
423 function scorm_get_prev() {
424 scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
425 if (scorm_tree_node) {
426 var hnode = scorm_tree_node.getHighlightedNode();
427 var prev = mod_scorm_prev(hnode);
429 mod_scorm_activate_item(prev);
434 function scorm_get_next() {
435 scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
436 if (scorm_tree_node) {
437 var hnode = scorm_tree_node.getHighlightedNode();
438 var next = mod_scorm_next(hnode);
440 mod_scorm_activate_item(next);