MDL-22951 - move the AJAX callback out into its own include, and tidy up
[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 M.mod_scorm = {};
26 M.mod_scorm.init = function(Y, hide_nav, hide_toc, toc_title, window_name, window_options, window_width, window_height, launch_sco) {
28     var popup_winHandle = false;
30     if (hide_nav == 0) {
31         scorm_hide_nav = false;
32     }
33     else {
34         scorm_hide_nav = true;
35     }
36     if (hide_toc == 0) {
37         scorm_hide_toc = false;
38     }
39     else {
40         scorm_hide_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             // make the popup work
86             if (window_name) {
87                 popup_winHandle = window.open('', window_name, window_options, window_width, window_height);
88                 popup_winHandle.location.href =  url_prefix + node.title;
89                 popup_winHandle.resizeTo(window_width, window_height);
90             }
91          
92             var content = new YAHOO.util.Element('scorm_content');
93             try {
94                 // first try IE way - it can not set name attribute later
95                 // and also it has some restrictions on DOM access from object tag
96                 if (window_name || node.title == null) {
97                     var obj = document.createElement('<iframe id="scorm_object" src="">');
98                 }
99                 else {
100                     var obj = document.createElement('<iframe id="scorm_object" src="'+url_prefix + node.title+'">');
101                 }
102             } catch (e) {
103                 var obj = document.createElement('object');
104                 obj.setAttribute('id', 'scorm_object');
105                 obj.setAttribute('type', 'text/html');
106                 if (!window_name && node.title != null) {
107                     obj.setAttribute('data', url_prefix + node.title);
108                 }
109             }
110             var old = YAHOO.util.Dom.get('scorm_object');
111             if (old) {
112                 content.replaceChild(obj, old);
113             } else {
114                 content.appendChild(obj);
115             }
116           
117             scorm_resize_frame();
119             var left = scorm_layout_widget.getUnitByPosition('left');
120             if (left.expanded) {
121                 scorm_current_node.focus();
122             }
123             if (scorm_hide_nav == false) {
124                 scorm_fixnav();
125             }
126         };
128         /**
129          * Enables/disables navigation buttons as needed.
130          * @return void
131          */
132         var scorm_fixnav = function() {
133             scorm_buttons[0].set('disabled', (scorm_skipprev(scorm_current_node) == null));
134             scorm_buttons[1].set('disabled', (scorm_prev(scorm_current_node) == null));
135             scorm_buttons[2].set('disabled', (scorm_up(scorm_current_node) == null));
136             scorm_buttons[3].set('disabled', (scorm_next(scorm_current_node) == null));
137             scorm_buttons[4].set('disabled', (scorm_skipnext(scorm_current_node) == null));
138         };
141         var scorm_resize_layout = function(alsowidth) {
142             if (window_name) {
143                 return;
144             }
146             if (alsowidth) {
147                 scorm_layout_widget.setStyle('width', '');
148                 var newwidth = scorm_get_htmlelement_size('content', 'width');
149                 if (newwidth > 600) {
150                     scorm_layout_widget.setStyle('width', newwidth+'px');
151                 }
152             }
153             // make sure that the max width of the TOC doesn't go to far
154             var left = scorm_layout_widget.getUnitByPosition('left');
155             var maxwidth = parseInt(YAHOO.util.Dom.getStyle('scorm_layout', 'width'));
156             left.set('maxWidth', (maxwidth - 10));
157             var cwidth = left.get('width');
158             if (cwidth > (maxwidth - 10)) {
159                 left.set('width', (maxwidth - 10));
160             }
162             var pageheight = scorm_get_htmlelement_size('page', 'height');
163             var layoutheight = scorm_get_htmlelement_size(scorm_layout_widget, 'height');
164             var newheight = layoutheight + parseInt(YAHOO.util.Dom.getViewportHeight()) - pageheight - 20;
165             if (newheight > 400) {
166                 if (newheight > 768) {
167                     scorm_layout_widget.setStyle('height', '768px');
168                 }
169                 else {
170                     scorm_layout_widget.setStyle('height', newheight+'px');
171                 }
172             }
174             scorm_layout_widget.render();
175             scorm_resize_frame();
177             if (scorm_nav_panel) {
178                 scorm_nav_panel.align('bl', 'bl');
179             }
180         };
182         var scorm_get_htmlelement_size = function(el, prop) {
183             var val = YAHOO.util.Dom.getStyle(el, prop);
184             if (val == 'auto') {
185                 if (el.get) {
186                     el = el.get('element'); // get real HTMLElement from YUI element
187                 }
188                 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
189             }
190             return parseInt(val);
191         };
193         var scorm_resize_frame = function() {
194             var obj = YAHOO.util.Dom.get('scorm_object');
195             if (obj) {
196                 var content = scorm_layout_widget.getUnitByPosition('center').get('wrap');
197                 // basically trap IE6 and 7
198                 if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
199                     if( obj.style.setAttribute ) {
200                         obj.style.setAttribute("cssText", 'width: ' +(content.offsetWidth - 6)+'px; height: ' + (content.offsetHeight - 10)+'px;');
201                     }
202                     else {
203                         obj.style.setAttribute('width', (content.offsetWidth - 6)+'px', 0);
204                         obj.style.setAttribute('height', (content.offsetHeight - 10)+'px', 0);
205                     }
206                 }
207                 else {
208                     obj.style.width = (content.offsetWidth - 6)+'px';
209                     obj.style.height = (content.offsetHeight - 10)+'px';
210                 }
211             }
212         };
215         var scorm_up = function(node) {
216             var node = scorm_tree_node.getHighlightedNode();
217             if (node.depth > 0) {
218                 return node.parent;
219             }
220             return null;
221         };
223         var scorm_lastchild = function(node) {
224             if (node.children.length) {
225                 return scorm_lastchild(node.children[node.children.length-1]);
226             } else {
227                 return node;
228             }
229         };
231         var scorm_prev = function(node) {
232             if (node.previousSibling && node.previousSibling.children.length) {
233                 return scorm_lastchild(node.previousSibling);
234             }
235             return scorm_skipprev(node);
236         };
238         var scorm_skipprev = function(node) {
239             if (node.previousSibling) {
240                 return node.previousSibling;
241             } else if (node.depth > 0) {
242                 return node.parent;
243             }
244             return null;
245         };
247         var scorm_next = function(node) {
248             if (node === false) {
249                 return scorm_tree_node.getRoot().children[0];
250             }
251             if (node.children.length) {
252                 return node.children[0];
253             }
254             return scorm_skipnext(node);
255         };
257         var scorm_skipnext = function(node) {
258             if (node.nextSibling) {
259                 return node.nextSibling;
260             } else if (node.depth > 0) {
261                 return scorm_skipnext(node.parent);
262             }
263             return null;
264         };
267         // layout
268         YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
269         YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
271         scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
272             minWidth: 255,
273             minHeight: 400,
274             units: [
275                 { position: 'left', body: 'scorm_toc', header: toc_title, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:250, maxWidth: 590},
276                 { position: 'center', body: '<div id="scorm_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
277             ]
278         });
279         scorm_layout_widget.render();
280         var left = scorm_layout_widget.getUnitByPosition('left');
281         left.on('collapse', function() {
282             scorm_resize_frame();
283         });
284         left.on('expand', function() {
285             scorm_resize_frame();
286         });
288         // ugly resizing hack that works around problems with resizing of iframes and objects
289         left._resize.on('startResize', function() {
290             var obj = YAHOO.util.Dom.get('scorm_object');
291             obj.style.display = 'none';
292         });
293         left._resize.on('endResize', function() {
294             var obj = YAHOO.util.Dom.get('scorm_object');
295             obj.style.display = 'block';
296             scorm_resize_frame();
297         });
299         // hide the TOC if that is the default
300         if (scorm_hide_toc == true) {
301             left.collapse();
302         }
304         // TOC tree
305         var tree = new YAHOO.widget.TreeView('scorm_tree');
306         scorm_tree_node = tree;
307         tree.singleNodeHighlight = true;
308         tree.subscribe('labelClick', function(node) {
309             scorm_activate_item(node);
310             if (node.children.length) {
311                 scorm_bloody_labelclick = true;
312             }
313         });
314         tree.subscribe('collapse', function(node) {
315             if (scorm_bloody_labelclick) {
316                 scorm_bloody_labelclick = false;
317                 return false;
318             }
319         });
320         tree.subscribe('expand', function(node) {
321             if (scorm_bloody_labelclick) {
322                 scorm_bloody_labelclick = false;
323                 return false;
324             }
325         });
326         tree.expandAll();
327         tree.render();
329         // navigation
330         if (scorm_hide_nav == false) {
331             scorm_nav_panel = new YAHOO.widget.Panel('scorm_navpanel', { visible:true, draggable:true, close:false, xy: [250, 450],
332                                                                     autofillheight: "body"} );
333             scorm_nav_panel.setHeader(M.str.scorm.navigation);
334     
335             //TODO: make some better&accessible buttons
336             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>');
337             scorm_nav_panel.render();
338             scorm_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
339             scorm_buttons[1] = new YAHOO.widget.Button('nav_prev');
340             scorm_buttons[2] = new YAHOO.widget.Button('nav_up');
341             scorm_buttons[3] = new YAHOO.widget.Button('nav_next');
342             scorm_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
343             scorm_buttons[0].on('click', function(ev) {
344                 scorm_activate_item(scorm_skipprev(scorm_tree_node.getHighlightedNode()));
345             });
346             scorm_buttons[1].on('click', function(ev) {
347                 scorm_activate_item(scorm_prev(scorm_tree_node.getHighlightedNode()));
348             });
349             scorm_buttons[2].on('click', function(ev) {
350                 scorm_activate_item(scorm_up(scorm_tree_node.getHighlightedNode()));
351             });
352             scorm_buttons[3].on('click', function(ev) {
353                 scorm_activate_item(scorm_next(scorm_tree_node.getHighlightedNode()));
354             });
355             scorm_buttons[4].on('click', function(ev) {
356                 scorm_activate_item(scorm_skipnext(scorm_tree_node.getHighlightedNode()));
357             });
358             scorm_nav_panel.render();
359         }
361         // finally activate the chosen item
362         var scorm_first_url = tree.getRoot().children[0];
363         var nxt = false;
364         while (nxt = scorm_next(nxt)) {
365             if (nxt.title) {
366                 expression = new RegExp('^.*?scoid=' + launch_sco + '.*?$');
367                 matches = nxt.title.match(expression);
368                 if (matches != null) {
369                     scorm_first_url = nxt;
370                     break;
371                 }
372             }
373         }
374         scorm_activate_item(scorm_first_url);
376         // resizing
377         scorm_resize_layout(false);
379         // fix layout if window resized
380         window.onresize = function() {
381             scorm_resize_layout(true);
382         };
383     });
384 };