mirrored from git://git.moodle.org/moodle.git
/
module.js
450 lines (403 loc) · 17 KB
/
module.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Javascript helper function for IMS Content Package module.
*
* @package mod-scorm
* @copyright 2009 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
mod_scorm_next = null;
mod_scorm_prev = null;
mod_scorm_activate_item = null;
M.mod_scorm = {};
M.mod_scorm.init = function(Y, hide_nav, hide_toc, toc_title, window_name, launch_sco) {
var scorm_disable_toc = false;
var scorm_hide_nav = true;
var scorm_hide_toc = true;
if (hide_toc == 0) {
scorm_hide_nav = false;
scorm_hide_toc = false;
} else if (hide_toc == 3) {
scorm_disable_toc = true;
}
var scorm_layout_widget;
var scorm_current_node;
var scorm_buttons = [];
var scorm_bloody_labelclick = false;
var scorm_nav_panel;
Y.use('yui2-resize', 'yui2-dragdrop', 'yui2-container', 'yui2-button', 'yui2-layout', 'yui2-treeview', 'yui2-json', 'yui2-event', function(Y) {
var scorm_activate_item = function(node) {
if (!node) {
return;
}
scorm_current_node = node;
scorm_current_node.highlight();
// remove any reference to the old API
if (window.API) {
window.API = null;
}
if (window.API_1484_11) {
window.API_1484_11 = null;
}
var url_prefix = M.cfg.wwwroot + '/mod/scorm/loadSCO.php?';
var el_old_api = document.getElementById('scormapi123');
if (el_old_api) {
el_old_api.parentNode.removeChild(el_old_api);
}
if (node.title) {
var el_scorm_api = document.getElementById("external-scormapi");
el_scorm_api.parentNode.removeChild(el_scorm_api);
el_scorm_api = document.createElement('script');
el_scorm_api.setAttribute('id','external-scormapi');
el_scorm_api.setAttribute('type','text/javascript');
var pel_scorm_api = document.getElementById('scormapi-parent');
pel_scorm_api.appendChild(el_scorm_api);
var api_url = M.cfg.wwwroot + '/mod/scorm/api.php?' + node.title;
document.getElementById('external-scormapi').src = api_url;
}
var content = new YAHOO.util.Element('scorm_content');
try {
// first try IE way - it can not set name attribute later
// and also it has some restrictions on DOM access from object tag
if (window_name || node.title == null) {
var obj = document.createElement('<iframe id="scorm_object" src="">');
if (window_name) {
var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
if(! mine) {
alert(M.str.scorm.popupsblocked);
}
mine.close()
}
}
else {
var obj = document.createElement('<iframe id="scorm_object" src="'+url_prefix + node.title+'">');
}
// fudge IE7 to redraw the screen
if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
obj.attachEvent("onload", scorm_resize_parent);
}
} catch (e) {
var obj = document.createElement('object');
obj.setAttribute('id', 'scorm_object');
obj.setAttribute('type', 'text/html');
if (!window_name && node.title != null) {
obj.setAttribute('data', url_prefix + node.title);
}
if (window_name) {
var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
if(! mine) {
alert(M.str.scorm.popupsblocked);
}
mine.close()
}
}
var old = YAHOO.util.Dom.get('scorm_object');
if (old) {
if(window_name) {
var cwidth = scormplayerdata.cwidth;
var cheight = scormplayerdata.cheight;
var poptions = scormplayerdata.popupoptions;
scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" + node.title, window_name, poptions, cwidth, cheight);
} else {
content.replaceChild(obj, old);
}
} else {
content.appendChild(obj);
}
scorm_resize_frame();
var left = scorm_layout_widget.getUnitByPosition('left');
if (left.expanded) {
scorm_current_node.focus();
}
if (scorm_hide_nav == false) {
scorm_fixnav();
}
};
mod_scorm_activate_item = scorm_activate_item;
/**
* Enables/disables navigation buttons as needed.
* @return void
*/
var scorm_fixnav = function() {
scorm_buttons[0].set('disabled', (scorm_skipprev(scorm_current_node) == null));
scorm_buttons[1].set('disabled', (scorm_prev(scorm_current_node) == null));
scorm_buttons[2].set('disabled', (scorm_up(scorm_current_node) == null));
scorm_buttons[3].set('disabled', (scorm_next(scorm_current_node) == null));
scorm_buttons[4].set('disabled', (scorm_skipnext(scorm_current_node) == null));
};
var scorm_resize_parent = function() {
// fudge IE7 to redraw the screen
parent.resizeBy(-10, -10);
parent.resizeBy(10, 10);
var ifr = YAHOO.util.Dom.get('scorm_object');
if (ifr) {
ifr.detachEvent("onload", scorm_resize_parent);
}
};
var scorm_resize_layout = function(alsowidth) {
if (window_name) {
return;
}
if (alsowidth) {
scorm_layout_widget.setStyle('width', '');
var newwidth = scorm_get_htmlelement_size('content', 'width');
}
// make sure that the max width of the TOC doesn't go to far
var left = scorm_layout_widget.getUnitByPosition('left');
var maxwidth = parseInt(YAHOO.util.Dom.getStyle('scorm_layout', 'width'));
left.set('maxWidth', (maxwidth - 10));
var cwidth = left.get('width');
if (cwidth > (maxwidth - 10)) {
left.set('width', (maxwidth - 10));
}
scorm_layout_widget.setStyle('height', '100%');
var center = scorm_layout_widget.getUnitByPosition('center');
center.setStyle('height', '100%');
// calculate the rough new height
newheight = YAHOO.util.Dom.getViewportHeight() *.82;
if (newheight < 600) {
newheight = 600;
}
scorm_layout_widget.set('height', newheight);
scorm_layout_widget.render();
scorm_resize_frame();
if (scorm_nav_panel) {
scorm_nav_panel.align('bl', 'bl');
}
};
var scorm_get_htmlelement_size = function(el, prop) {
var val = YAHOO.util.Dom.getStyle(el, prop);
if (val == 'auto') {
if (el.get) {
el = el.get('element'); // get real HTMLElement from YUI element
}
val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
}
return parseInt(val);
};
var scorm_resize_frame = function() {
var obj = YAHOO.util.Dom.get('scorm_object');
if (obj) {
var content = scorm_layout_widget.getUnitByPosition('center').get('wrap');
// basically trap IE6 and 7
if (YAHOO.env.ua.ie > 5 && YAHOO.env.ua.ie < 8) {
if( obj.style.setAttribute ) {
obj.style.setAttribute("cssText", 'width: ' +(content.offsetWidth - 6)+'px; height: ' + (content.offsetHeight - 10)+'px;');
}
else {
obj.style.setAttribute('width', (content.offsetWidth - 6)+'px', 0);
obj.style.setAttribute('height', (content.offsetHeight - 10)+'px', 0);
}
}
else {
obj.style.width = (content.offsetWidth - 6)+'px';
obj.style.height = (content.offsetHeight - 10)+'px';
}
}
};
var scorm_up = function(node) {
var node = scorm_tree_node.getHighlightedNode();
if (node.depth > 0) {
return node.parent;
}
return null;
};
var scorm_lastchild = function(node) {
if (node.children.length) {
return scorm_lastchild(node.children[node.children.length-1]);
} else {
return node;
}
};
var scorm_prev = function(node) {
if (node.previousSibling && node.previousSibling.children.length) {
return scorm_lastchild(node.previousSibling);
}
return scorm_skipprev(node);
};
var scorm_skipprev = function(node) {
if (node.previousSibling) {
return node.previousSibling;
} else if (node.depth > 0) {
return node.parent;
}
return null;
};
var scorm_next = function(node) {
if (node === false) {
return scorm_tree_node.getRoot().children[0];
}
if (node.children.length) {
return node.children[0];
}
return scorm_skipnext(node);
};
var scorm_skipnext = function(node) {
if (node.nextSibling) {
return node.nextSibling;
} else if (node.depth > 0) {
return scorm_skipnext(node.parent);
}
return null;
};
mod_scorm_next = scorm_next;
mod_scorm_prev = scorm_prev;
// layout
YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
if (scorm_disable_toc) {
scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
minWidth: 255,
minHeight: 600,
units: [
{ position: 'left', body: 'scorm_toc', header: toc_title, width: 0, resize: true, gutter: '0px 0px 0px 0px', collapse: false},
{ position: 'center', body: '<div id="scorm_content"></div>', gutter: '0px 0px 0px 0px', scroll: true}
]
});
} else {
scorm_layout_widget = new YAHOO.widget.Layout('scorm_layout', {
minWidth: 255,
minHeight: 600,
units: [
{ position: 'left', body: 'scorm_toc', header: toc_title, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:250, maxWidth: 590},
{ position: 'center', body: '<div id="scorm_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
]
});
}
scorm_layout_widget.render();
var left = scorm_layout_widget.getUnitByPosition('left');
if (!scorm_disable_toc) {
left.on('collapse', function() {
scorm_resize_frame();
});
left.on('expand', function() {
scorm_resize_frame();
});
}
// ugly resizing hack that works around problems with resizing of iframes and objects
left._resize.on('startResize', function() {
var obj = YAHOO.util.Dom.get('scorm_object');
obj.style.display = 'none';
});
left._resize.on('endResize', function() {
var obj = YAHOO.util.Dom.get('scorm_object');
obj.style.display = 'block';
scorm_resize_frame();
});
// hide the TOC if that is the default
if (!scorm_disable_toc) {
if (scorm_hide_toc == true) {
left.collapse();
}
}
// TOC tree
var tree = new YAHOO.widget.TreeView('scorm_tree');
scorm_tree_node = tree;
tree.singleNodeHighlight = true;
tree.subscribe('labelClick', function(node) {
scorm_activate_item(node);
if (node.children.length) {
scorm_bloody_labelclick = true;
}
});
if (!scorm_disable_toc) {
tree.subscribe('collapse', function(node) {
if (scorm_bloody_labelclick) {
scorm_bloody_labelclick = false;
return false;
}
});
tree.subscribe('expand', function(node) {
if (scorm_bloody_labelclick) {
scorm_bloody_labelclick = false;
return false;
}
});
}
tree.expandAll();
tree.render();
// navigation
if (scorm_hide_nav == false) {
scorm_nav_panel = new YAHOO.widget.Panel('scorm_navpanel', { visible:true, draggable:true, close:false, xy: [250, 450],
autofillheight: "body"} );
scorm_nav_panel.setHeader(M.str.scorm.navigation);
//TODO: make some better&accessible buttons
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>');
scorm_nav_panel.render();
scorm_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
scorm_buttons[1] = new YAHOO.widget.Button('nav_prev');
scorm_buttons[2] = new YAHOO.widget.Button('nav_up');
scorm_buttons[3] = new YAHOO.widget.Button('nav_next');
scorm_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
scorm_buttons[0].on('click', function(ev) {
scorm_activate_item(scorm_skipprev(scorm_tree_node.getHighlightedNode()));
});
scorm_buttons[1].on('click', function(ev) {
scorm_activate_item(scorm_prev(scorm_tree_node.getHighlightedNode()));
});
scorm_buttons[2].on('click', function(ev) {
scorm_activate_item(scorm_up(scorm_tree_node.getHighlightedNode()));
});
scorm_buttons[3].on('click', function(ev) {
scorm_activate_item(scorm_next(scorm_tree_node.getHighlightedNode()));
});
scorm_buttons[4].on('click', function(ev) {
scorm_activate_item(scorm_skipnext(scorm_tree_node.getHighlightedNode()));
});
scorm_nav_panel.render();
}
// finally activate the chosen item
var scorm_first_url = tree.getRoot().children[0];
var nxt = false;
while (nxt = scorm_next(nxt)) {
if (nxt.title) {
expression = new RegExp('^.*?scoid=' + launch_sco + '.*?$');
matches = nxt.title.match(expression);
if (matches != null) {
scorm_first_url = nxt;
break;
}
}
}
scorm_activate_item(scorm_first_url);
// resizing
scorm_resize_layout(false);
// fix layout if window resized
window.onresize = function() {
scorm_resize_layout(true);
};
});
};
function scorm_get_prev() {
scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
if (scorm_tree_node) {
var hnode = scorm_tree_node.getHighlightedNode();
var prev = mod_scorm_prev(hnode);
if (prev) {
mod_scorm_activate_item(prev);
}
}
}
function scorm_get_next() {
scorm_tree_node = YAHOO.widget.TreeView.getTree('scorm_tree');
if (scorm_tree_node) {
var hnode = scorm_tree_node.getHighlightedNode();
var next = mod_scorm_next(hnode);
if (next) {
mod_scorm_activate_item(next);
}
}
}