MDL-26482 SCORM add option to disable TOC completely - thanks to Clark Burns for...
[moodle.git] / mod / scorm / module.js
blob188b0bdf5bd4564e506b6aeffea000340aeba602
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 IMS Content Package 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_next = null;
25 mod_scorm_prev = null;
26 mod_scorm_activate_item = null;
28 M.mod_scorm = {};
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;
34     if (hide_nav == 0) {
35         scorm_hide_nav = false;
36     }
37     if (hide_toc == 0) {
38         scorm_hide_toc = false;
39     } else if (hide_toc == 3) {
40         scorm_disable_toc = true;
41     }
43     var scorm_layout_widget;
44     var scorm_current_node;
45     var scorm_buttons = [];
46     var scorm_bloody_labelclick = false;
47     var scorm_nav_panel;
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) {
54             if (!node) {
55                 return;
56             }
57             scorm_current_node = node;
58             scorm_current_node.highlight();
60             // remove any reference to the old API
61             if (window.API) {
62                 window.API = null;
63             }
64             if (window.API_1484_11) {
65                 window.API_1484_11 = null;
66             }
67             var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
68             var el_old_api = document.getElementById('scormapi123');
69             if (el_old_api) {
70                 el_old_api.parentNode.removeChild(el_old_api);
71             }
73             if (node.title) {
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;
83             }
85             var content = new YAHOO.util.Element('scorm_content');
86             try {
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="">');
91                     if (window_name) {
92                         var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
93                         if(! mine) {
94                              alert(M.str.scorm.popupsblocked);
95                         }
96                         mine.close()
97                     }
98                 }
99                 else {
100                     var obj = document.createElement('<iframe id="scorm_object" src="'+url_prefix + node.title+'">');
101                 }
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);
105                 }
106             } catch (e) {
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);
112                 }
113                 if (window_name) {
114                     var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
115                     if(! mine) {
116                          alert(M.str.scorm.popupsblocked);
117                     }
118                     mine.close()
119                 }
120             }
121             var old = YAHOO.util.Dom.get('scorm_object');
122             if (old) {
123                 if(window_name) { 
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);
128                 }
129                 else { 
130                     content.replaceChild(obj, old); 
131                 }
132             } else {
133                 content.appendChild(obj);
134             }
136             scorm_resize_frame();
138             var left = scorm_layout_widget.getUnitByPosition('left');
139             if (left.expanded) {
140                 scorm_current_node.focus();
141             }
142             if (scorm_hide_nav == false) {
143                 scorm_fixnav();
144             }
145         };
147         mod_scorm_activate_item = scorm_activate_item;
149         /**
150          * Enables/disables navigation buttons as needed.
151          * @return void
152          */
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));
159         };
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');
166             if (ifr) {
167                 ifr.detachEvent("onload", scorm_resize_parent);
168             }
169         };
171         var scorm_resize_layout = function(alsowidth) {
172             if (window_name) {
173                 return;
174             }
176             if (alsowidth) {
177                 scorm_layout_widget.setStyle('width', '');
178                 var newwidth = scorm_get_htmlelement_size('content', 'width');
179             }
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));
188             }
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');
196             }
197         };
199         var scorm_get_htmlelement_size = function(el, prop) {
200             var val = YAHOO.util.Dom.getStyle(el, prop);
201             if (val == 'auto') {
202                 if (el.get) {
203                     el = el.get('element'); // get real HTMLElement from YUI element
204                 }
205                 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
206             }
207             return parseInt(val);
208         };
210         var scorm_resize_frame = function() {
211             var obj = YAHOO.util.Dom.get('scorm_object');
212             if (obj) {
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;');
218                     }
219                     else {
220                         obj.style.setAttribute('width', (content.offsetWidth - 6)+'px', 0);
221                         obj.style.setAttribute('height', (content.offsetHeight - 10)+'px', 0);
222                     }
223                 }
224                 else {
225                     obj.style.width = (content.offsetWidth - 6)+'px';
226                     obj.style.height = (content.offsetHeight - 10)+'px';
227                 }
228             }
229         };
232         var scorm_up = function(node) {
233             var node = scorm_tree_node.getHighlightedNode();
234             if (node.depth > 0) {
235                 return node.parent;
236             }
237             return null;
238         };
240         var scorm_lastchild = function(node) {
241             if (node.children.length) {
242                 return scorm_lastchild(node.children[node.children.length-1]);
243             } else {
244                 return node;
245             }
246         };
248         var scorm_prev = function(node) {
249             if (node.previousSibling && node.previousSibling.children.length) {
250                 return scorm_lastchild(node.previousSibling);
251             }
252             return scorm_skipprev(node);
253         };
255         var scorm_skipprev = function(node) {
256             if (node.previousSibling) {
257                 return node.previousSibling;
258             } else if (node.depth > 0) {
259                 return node.parent;
260             }
261             return null;
262         };
264         var scorm_next = function(node) {
265             if (node === false) {
266                 return scorm_tree_node.getRoot().children[0];
267             }
268             if (node.children.length) {
269                 return node.children[0];
270             }
271             return scorm_skipnext(node);
272         };
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);
279             }
280             return null;
281         };
283         mod_scorm_next = scorm_next;
284         mod_scorm_prev = scorm_prev;
287         // layout
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', {
293                 minWidth: 255,
294                 minHeight: 600,
295                 units: [
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}
298                 ]
299             });
300         } else {
301             scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
302                 minWidth: 255,
303                 minHeight: 600,
304                 units: [
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}
307                 ]
308             });
309         }
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();
316             });
317             left.on('expand', function() {
318                 scorm_resize_frame();
319             });
320         }
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';
325         });
326         left._resize.on('endResize', function() {
327             var obj = YAHOO.util.Dom.get('scorm_object');
328             obj.style.display = 'block';
329             scorm_resize_frame();
330         });
332         // hide the TOC if that is the default
333         if (!scorm_disable_toc) {
334             if (scorm_hide_toc == true) {
335                left.collapse();
336             }
337         }
338         // TOC tree
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;
346             }
347         });
348         if (!scorm_disable_toc) {
349             tree.subscribe('collapse', function(node) {
350                 if (scorm_bloody_labelclick) {
351                     scorm_bloody_labelclick = false;
352                     return false;
353                 }
354             });
355             tree.subscribe('expand', function(node) {
356                 if (scorm_bloody_labelclick) {
357                     scorm_bloody_labelclick = false;
358                     return false;
359                 }
360             });
361         }
362         tree.expandAll();
363         tree.render();
365         // navigation
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">&lt;&lt;</button><button id="nav_prev">&lt;</button><button id="nav_up">^</button><button id="nav_next">&gt;</button><button id="nav_skipnext">&gt;&gt;</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()));
381             });
382             scorm_buttons[1].on('click', function(ev) {
383                 scorm_activate_item(scorm_prev(scorm_tree_node.getHighlightedNode()));
384             });
385             scorm_buttons[2].on('click', function(ev) {
386                 scorm_activate_item(scorm_up(scorm_tree_node.getHighlightedNode()));
387             });
388             scorm_buttons[3].on('click', function(ev) {
389                 scorm_activate_item(scorm_next(scorm_tree_node.getHighlightedNode()));
390             });
391             scorm_buttons[4].on('click', function(ev) {
392                 scorm_activate_item(scorm_skipnext(scorm_tree_node.getHighlightedNode()));
393             });
394             scorm_nav_panel.render();
395         }
397         // finally activate the chosen item
398         var scorm_first_url = tree.getRoot().children[0];
399         var nxt = false;
400         while (nxt = scorm_next(nxt)) {
401             if (nxt.title) {
402                 expression = new RegExp('^.*?scoid=' + launch_sco + '.*?$');
403                 matches = nxt.title.match(expression);
404                 if (matches != null) {
405                     scorm_first_url = nxt;
406                     break;
407                 }
408             }
409         }
410         scorm_activate_item(scorm_first_url);
412         // resizing
413         scorm_resize_layout(false);
415         // fix layout if window resized
416         window.onresize = function() {
417             scorm_resize_layout(true);
418         };
419     });
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);
428         if (prev) {
429             mod_scorm_activate_item(prev);
430         }
431     }
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);
439         if (next) {
440             mod_scorm_activate_item(next);
441         }
442     }