Merge branch 'm20_MDL-26176_cleanup' of github.com:danmarsden/moodle
[moodle.git] / mod / scorm / module.js
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/>.
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) {
32     if (hide_nav == 0) {
33         scorm_hide_nav = false;
34     }
35     else {
36         scorm_hide_nav = true;
37     }
38     if (hide_toc == 0) {
39         scorm_hide_toc = false;
40     }
41     else {
42         scorm_hide_toc = true;
43     }
45     var scorm_layout_widget;
46     var scorm_current_node;
47     var scorm_buttons = [];
48     var scorm_bloody_labelclick = false;
49     var scorm_nav_panel;
52     Y.use('yui2-resize', 'yui2-dragdrop', 'yui2-container', 'yui2-button', 'yui2-layout', 'yui2-treeview', 'yui2-json', 'yui2-event', function(Y) {
55         var scorm_activate_item = function(node) {
56             if (!node) {
57                 return;
58             }
59             scorm_current_node = node;
60             scorm_current_node.highlight();
62             // remove any reference to the old API
63             if (window.API) {
64                 window.API = null;
65             }
66             if (window.API_1484_11) {
67                 window.API_1484_11 = null;
68             }
69             var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
70             var el_old_api = document.getElementById('scormapi123');
71             if (el_old_api) {
72                 el_old_api.parentNode.removeChild(el_old_api);
73             }
75             if (node.title) {
76                 var el_scorm_api = document.getElementById("external-scormapi");
77                 el_scorm_api.parentNode.removeChild(el_scorm_api);
78                 el_scorm_api = document.createElement('script');
79                 el_scorm_api.setAttribute('id','external-scormapi');
80                 el_scorm_api.setAttribute('type','text/javascript');
81                 var pel_scorm_api = document.getElementById('scormapi-parent');
82                 pel_scorm_api.appendChild(el_scorm_api);
83                 var api_url = M.cfg.wwwroot + '/mod/scorm/api.php?' + node.title;
84                 document.getElementById('external-scormapi').src = api_url;
85             }
87             var content = new YAHOO.util.Element('scorm_content');
88             try {
89                 // first try IE way - it can not set name attribute later
90                 // and also it has some restrictions on DOM access from object tag
91                 if (window_name || node.title == null) {
92                     var obj = document.createElement('<iframe id="scorm_object" src="">');
93                     if (window_name) {
94                         var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
95                         if(! mine) {
96                              alert(M.str.scorm.popupsblocked);
97                         }
98                         mine.close()
99                     }
100                 }
101                 else {
102                     var obj = document.createElement('<iframe id="scorm_object" src="'+url_prefix + node.title+'">');
103                 }
104                 // fudge IE7 to redraw the screen
105                 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
106                     obj.attachEvent("onload", scorm_resize_parent);
107                 }
108             } catch (e) {
109                 var obj = document.createElement('object');
110                 obj.setAttribute('id', 'scorm_object');
111                 obj.setAttribute('type', 'text/html');
112                 if (!window_name && node.title != null) {
113                     obj.setAttribute('data', url_prefix + node.title);
114                 }
115                 if (window_name) {
116                     var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
117                     if(! mine) {
118                          alert(M.str.scorm.popupsblocked);
119                     }
120                     mine.close()
121                 }
122             }
123             var old = YAHOO.util.Dom.get('scorm_object');
124             if (old) {
125                 if(window_name) { 
126                     var cwidth = scormplayerdata.cwidth;
127                     var cheight = scormplayerdata.cheight;
128                     var poptions = scormplayerdata.popupoptions;
129                     scorm_openpopup("loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
130                 }
131                 else { 
132                     content.replaceChild(obj, old); 
133                 }
134             } else {
135                 content.appendChild(obj);
136             }
138             scorm_resize_frame();
140             var left = scorm_layout_widget.getUnitByPosition('left');
141             if (left.expanded) {
142                 scorm_current_node.focus();
143             }
144             if (scorm_hide_nav == false) {
145                 scorm_fixnav();
146             }
147         };
149         mod_scorm_activate_item = scorm_activate_item;
151         /**
152          * Enables/disables navigation buttons as needed.
153          * @return void
154          */
155         var scorm_fixnav = function() {
156             scorm_buttons[0].set('disabled', (scorm_skipprev(scorm_current_node) == null));
157             scorm_buttons[1].set('disabled', (scorm_prev(scorm_current_node) == null));
158             scorm_buttons[2].set('disabled', (scorm_up(scorm_current_node) == null));
159             scorm_buttons[3].set('disabled', (scorm_next(scorm_current_node) == null));
160             scorm_buttons[4].set('disabled', (scorm_skipnext(scorm_current_node) == null));
161         };
163         var scorm_resize_parent = function() {
164             // fudge  IE7 to redraw the screen
165             parent.resizeBy(-10, -10);
166             parent.resizeBy(10, 10);
167             var ifr = YAHOO.util.Dom.get('scorm_object');
168             if (ifr) {
169                 ifr.detachEvent("onload", scorm_resize_parent);
170             }
171         };
173         var scorm_resize_layout = function(alsowidth) {
174             if (window_name) {
175                 return;
176             }
178             if (alsowidth) {
179                 scorm_layout_widget.setStyle('width', '');
180                 var newwidth = scorm_get_htmlelement_size('content', 'width');
181                 if (newwidth > 600) {
182                     scorm_layout_widget.setStyle('width', newwidth+'px');
183                 }
184                 else {
185                     scorm_layout_widget.setStyle('width', '600px');
186                 }
187             }
188             // make sure that the max width of the TOC doesn't go to far
190             var left = scorm_layout_widget.getUnitByPosition('left');
191             var maxwidth = parseInt(YAHOO.util.Dom.getStyle('scorm_layout', 'width'));
192             left.set('maxWidth', (maxwidth - 10));
193             var cwidth = left.get('width');
194             if (cwidth > (maxwidth - 10)) {
195                 left.set('width', (maxwidth - 10));
196             }
198             scorm_layout_widget.setStyle('height', '100%');
199             scorm_layout_widget.render();
200             scorm_resize_frame();
202             if (scorm_nav_panel) {
203                 scorm_nav_panel.align('bl', 'bl');
204             }
205         };
207         var scorm_get_htmlelement_size = function(el, prop) {
208             var val = YAHOO.util.Dom.getStyle(el, prop);
209             if (val == 'auto') {
210                 if (el.get) {
211                     el = el.get('element'); // get real HTMLElement from YUI element
212                 }
213                 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
214             }
215             return parseInt(val);
216         };
218         var scorm_resize_frame = function() {
219             var obj = YAHOO.util.Dom.get('scorm_object');
220             if (obj) {
221                 var content = scorm_layout_widget.getUnitByPosition('center').get('wrap');
222                 // basically trap IE6 and 7
223                 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
224                     if( obj.style.setAttribute ) {
225                         obj.style.setAttribute("cssText", 'width: ' +(content.offsetWidth - 6)+'px; height: ' + (content.offsetHeight - 10)+'px;');
226                     }
227                     else {
228                         obj.style.setAttribute('width', (content.offsetWidth - 6)+'px', 0);
229                         obj.style.setAttribute('height', (content.offsetHeight - 10)+'px', 0);
230                     }
231                 }
232                 else {
233                     obj.style.width = (content.offsetWidth - 6)+'px';
234                     obj.style.height = (content.offsetHeight - 10)+'px';
235                 }
236             }
237         };
240         var scorm_up = function(node) {
241             var node = scorm_tree_node.getHighlightedNode();
242             if (node.depth > 0) {
243                 return node.parent;
244             }
245             return null;
246         };
248         var scorm_lastchild = function(node) {
249             if (node.children.length) {
250                 return scorm_lastchild(node.children[node.children.length-1]);
251             } else {
252                 return node;
253             }
254         };
256         var scorm_prev = function(node) {
257             if (node.previousSibling && node.previousSibling.children.length) {
258                 return scorm_lastchild(node.previousSibling);
259             }
260             return scorm_skipprev(node);
261         };
263         var scorm_skipprev = function(node) {
264             if (node.previousSibling) {
265                 return node.previousSibling;
266             } else if (node.depth > 0) {
267                 return node.parent;
268             }
269             return null;
270         };
272         var scorm_next = function(node) {
273             if (node === false) {
274                 return scorm_tree_node.getRoot().children[0];
275             }
276             if (node.children.length) {
277                 return node.children[0];
278             }
279             return scorm_skipnext(node);
280         };
282         var scorm_skipnext = function(node) {
283             if (node.nextSibling) {
284                 return node.nextSibling;
285             } else if (node.depth > 0) {
286                 return scorm_skipnext(node.parent);
287             }
288             return null;
289         };
291         mod_scorm_next = scorm_next;
292         mod_scorm_prev = scorm_prev;
295         // layout
296         YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
297         YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
299         scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
300             minWidth: 255,
301             minHeight: 600,
302             units: [
303                 { position: 'left', body: 'scorm_toc', header: toc_title, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:250, maxWidth: 590},
304                 { position: 'center', body: '<div id="scorm_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
305             ]
306         });
307         scorm_layout_widget.render();
308         var left = scorm_layout_widget.getUnitByPosition('left');
309         left.on('collapse', function() {
310             scorm_resize_frame();
311         });
312         left.on('expand', function() {
313             scorm_resize_frame();
314         });
316         // ugly resizing hack that works around problems with resizing of iframes and objects
317         left._resize.on('startResize', function() {
318             var obj = YAHOO.util.Dom.get('scorm_object');
319             obj.style.display = 'none';
320         });
321         left._resize.on('endResize', function() {
322             var obj = YAHOO.util.Dom.get('scorm_object');
323             obj.style.display = 'block';
324             scorm_resize_frame();
325         });
327         // hide the TOC if that is the default
328         if (scorm_hide_toc == true) {
329             left.collapse();
330         }
332         // TOC tree
333         var tree = new YAHOO.widget.TreeView('scorm_tree');
334         scorm_tree_node = tree;
335         tree.singleNodeHighlight = true;
336         tree.subscribe('labelClick', function(node) {
337             scorm_activate_item(node);
338             if (node.children.length) {
339                 scorm_bloody_labelclick = true;
340             }
341         });
342         tree.subscribe('collapse', function(node) {
343             if (scorm_bloody_labelclick) {
344                 scorm_bloody_labelclick = false;
345                 return false;
346             }
347         });
348         tree.subscribe('expand', function(node) {
349             if (scorm_bloody_labelclick) {
350                 scorm_bloody_labelclick = false;
351                 return false;
352             }
353         });
354         tree.expandAll();
355         tree.render();
357         // navigation
358         if (scorm_hide_nav == false) {
359             scorm_nav_panel = new YAHOO.widget.Panel('scorm_navpanel', { visible:true, draggable:true, close:false, xy: [250, 450],
360                                                                     autofillheight: "body"} );
361             scorm_nav_panel.setHeader(M.str.scorm.navigation);
363             //TODO: make some better&accessible buttons
364             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>');
365             scorm_nav_panel.render();
366             scorm_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
367             scorm_buttons[1] = new YAHOO.widget.Button('nav_prev');
368             scorm_buttons[2] = new YAHOO.widget.Button('nav_up');
369             scorm_buttons[3] = new YAHOO.widget.Button('nav_next');
370             scorm_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
371             scorm_buttons[0].on('click', function(ev) {
372                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getHighlightedNode()));
373             });
374             scorm_buttons[1].on('click', function(ev) {
375                 scorm_activate_item(scorm_prev(scorm_tree_node.getHighlightedNode()));
376             });
377             scorm_buttons[2].on('click', function(ev) {
378                 scorm_activate_item(scorm_up(scorm_tree_node.getHighlightedNode()));
379             });
380             scorm_buttons[3].on('click', function(ev) {
381                 scorm_activate_item(scorm_next(scorm_tree_node.getHighlightedNode()));
382             });
383             scorm_buttons[4].on('click', function(ev) {
384                 scorm_activate_item(scorm_skipnext(scorm_tree_node.getHighlightedNode()));
385             });
386             scorm_nav_panel.render();
387         }
389         // finally activate the chosen item
390         var scorm_first_url = tree.getRoot().children[0];
391         var nxt = false;
392         while (nxt = scorm_next(nxt)) {
393             if (nxt.title) {
394                 expression = new RegExp('^.*?scoid=' + launch_sco + '.*?$');
395                 matches = nxt.title.match(expression);
396                 if (matches != null) {
397                     scorm_first_url = nxt;
398                     break;
399                 }
400             }
401         }
402         scorm_activate_item(scorm_first_url);
404         // resizing
405         scorm_resize_layout(false);
407         // fix layout if window resized
408         window.onresize = function() {
409             scorm_resize_layout(true);
410         };
411     });
412 };
415 function scorm_get_prev() {
416         scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
417     if (scorm_tree_node) {
418         var hnode = scorm_tree_node.getHighlightedNode();
419         var prev = mod_scorm_prev(hnode);
420         if (prev) {
421             mod_scorm_activate_item(prev);
422         }
423     }
426 function scorm_get_next() {
427         scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
428     if (scorm_tree_node) {
429         var hnode = scorm_tree_node.getHighlightedNode();
430         var next = mod_scorm_next(hnode);
431         if (next) {
432             mod_scorm_activate_item(next);
433         }
434     }