Javascript
Javascript
Javascript
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 SCORM 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_launch_next_sco = null;
mod_scorm_launch_prev_sco = null;
mod_scorm_activate_item = null;
mod_scorm_parse_toc_tree = null;
scorm_layout_widget = null;
window.scorm_current_node = null;
function underscore(str) {
str = String(str).replace(/.N/g,".");
return str.replace(/\./g,"__");
}
M.mod_scorm = {};
scoes_nav = Y.JSON.parse(scoes_nav);
var scorm_buttons = [];
var scorm_bloody_labelclick = false;
var scorm_nav_panel;
Y.TreeView.prototype.openAll = function () {
this.get('container').all('.yui3-treeview-can-have-
children').each(function(target) {
this.getNodeById(target.get('id')).open();
}, this);
};
Y.TreeView.prototype.closeAll = function () {
this.get('container').all('.yui3-treeview-can-have-
children').each(function(target) {
this.getNodeById(target.get('id')).close();
}, this);
}
srcNode.all(SELECTORS.child).each(function(childNode) {
var child = {},
labelNode = childNode.one(SELECTORS.label),
textNode = childNode.one(SELECTORS.textlabel),
subTreeNode = childNode.one(SELECTORS.subtree);
if (labelNode) {
var title = labelNode.getAttribute('title');
var scoid = labelNode.getData('scoid');
child.label = labelNode.get('outerHTML');
// Will be good to change to url instead of title.
if (title && title !== '#') {
child.title = title;
}
if (typeof scoid !== 'undefined') {
child.scoid = scoid;
}
} else if (textNode) {
// The selector did not find a label node with anchor.
child.label = textNode.get('outerHTML');
}
if (subTreeNode) {
child.children = scorm_parse_toc_tree(subTreeNode);
}
children.push(child);
});
return children;
};
mod_scorm_parse_toc_tree = scorm_parse_toc_tree;
scorm_current_node = node;
if (!scorm_current_node.state.selected) {
scorm_current_node.select();
}
scorm_tree_node.closeAll();
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 (old) {
if(window_name) {
var cwidth = scormplayerdata.cwidth;
var cheight = scormplayerdata.cheight;
var poptions = scormplayerdata.popupoptions;
poptions = poptions + ',resizable=yes'; // Added for IE (MDL-
32506).
scorm_openpopup(M.cfg.wwwroot + "/mod/scorm/loadSCO.php?" +
node.title, window_name, poptions, cwidth, cheight);
}
} else {
content.prepend(obj);
}
if (scorm_hide_nav == false) {
if (nav_display === 1 && navposition_left > 0 && navposition_top >
0) {
Y.one('#scorm_object').addClass(cssclasses.scorm_nav_under_content);
}
scorm_fixnav();
}
scorm_tree_node.openAll();
};
mod_scorm_activate_item = scorm_activate_item;
/**
* Enables/disables navigation buttons as needed.
* @return void
*/
var scorm_fixnav = function() {
var skipprevnode = scorm_skipprev(scorm_current_node);
var prevnode = scorm_prev(scorm_current_node);
var skipnextnode = scorm_skipnext(scorm_current_node);
var nextnode = scorm_next(scorm_current_node);
var upnode = scorm_up(scorm_current_node);
scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
.addClass(cssclasses.scorm_grid_content_toc_hidden);
} else if (body.get('winWidth') > collapsetocwinsize) {
toc.removeClass(cssclasses.disabled)
.removeAttribute('disabled-by');
scorm_toc_toggle_btn.setHTML('<')
.set('title', M.util.get_string('hide', 'moodle'));
scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
.addClass(cssclasses.scorm_grid_content_toc_visible);
}
return;
}
if (toc_disabled) {
toc.removeClass(cssclasses.disabled)
.removeAttribute('disabled-by');
scorm_toc_toggle_btn.setHTML('<')
.set('title', M.util.get_string('hide', 'moodle'));
scorm_content.removeClass(cssclasses.scorm_grid_content_toc_hidden)
.addClass(cssclasses.scorm_grid_content_toc_visible);
} else {
toc.addClass(cssclasses.disabled)
.setAttribute('disabled-by', 'user');
scorm_toc_toggle_btn.setHTML('>')
.set('title', M.util.get_string('show', 'moodle'));
scorm_content.removeClass(cssclasses.scorm_grid_content_toc_visible)
.addClass(cssclasses.scorm_grid_content_toc_hidden);
}
};
// make sure that the max width of the TOC doesn't go to far
};
scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()[0], true));
}
} else {
scorm_activate_item(scorm_prev(scorm_tree_node.getSelectedNodes()
[0], true));
}
};
scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()[0], true));
}
} else {
scorm_activate_item(scorm_next(scorm_tree_node.getSelectedNodes()
[0], true));
}
};
mod_scorm_launch_prev_sco = scorm_launch_prev_sco;
mod_scorm_launch_next_sco = scorm_launch_next_sco;
var cssclasses = {
// YUI grid class: use 100% of the available width to show only
content, TOC hidden.
scorm_grid_content_toc_hidden: 'yui3-u-1',
// YUI grid class: use 1/5 of the available width to show TOC.
scorm_grid_toc: 'yui3-u-1-5',
// YUI grid class: use 1/24 of the available width to show TOC
toggle button.
scorm_grid_toggle: 'yui3-u-1-24',
// YUI grid class: use 3/4 of the available width to show content,
TOC visible.
scorm_grid_content_toc_visible: 'yui3-u-3-4',
// Reduce height of #scorm_object to accomodate nav buttons under
content.
scorm_nav_under_content: 'scorm_nav_under_content',
disabled: 'disabled'
};
// layout
Y.one('#scorm_toc_title').setHTML(toc_title);
if (scorm_disable_toc) {
Y.one('#scorm_toc').addClass(cssclasses.disabled);
Y.one('#scorm_toc_toggle').addClass(cssclasses.disabled);
Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_hidden);
} else {
Y.one('#scorm_toc').addClass(cssclasses.scorm_grid_toc);
Y.one('#scorm_toc_toggle').addClass(cssclasses.scorm_grid_toggle);
Y.one('#scorm_toc_toggle_btn')
.setHTML('<')
.setAttribute('title', M.util.get_string('hide', 'moodle'));
Y.one('#scorm_content').addClass(cssclasses.scorm_grid_content_toc_visible);
scorm_toggle_toc(true);
}
// Update launch_sco.
if (typeof node.scoid !== 'undefined') {
launch_sco = node.scoid;
}
scorm_activate_item(node);
if (node.children.length) {
scorm_bloody_labelclick = true;
}
});
if (!scorm_disable_toc) {
tree.on('close', function(e) {
if (scorm_bloody_labelclick) {
scorm_bloody_labelclick = false;
return false;
}
});
tree.subscribe('open', function(e) {
if (scorm_bloody_labelclick) {
scorm_bloody_labelclick = false;
return false;
}
});
}
tree.render();
tree.openAll();
// On getting the window, always set the focus on the current item
Y.one(Y.config.win).on('focus', function (e) {
var current = scorm_tree_node.getSelectedNodes()[0];
var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
if (current.id && !toc_disabled) {
Y.one('#' + current.id).focus();
}
});
// navigation
if (scorm_hide_nav == false) {
// TODO: make some better&accessible buttons.
var navbuttonshtml = '<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>';
if (nav_display === 1) {
Y.one('#scorm_navpanel').setHTML(navbuttonshtml);
} else {
// Nav panel is floating type.
var navposition = null;
if (navposition_left < 0 && navposition_top < 0) {
// Set default XY.
navposition = Y.one('#scorm_toc').getXY();
navposition[1] += 200;
} else {
// Set user defined XY.
navposition = [];
navposition[0] = parseInt(navposition_left, 10);
navposition[1] = parseInt(navposition_top, 10);
}
scorm_nav_panel = new Y.Panel({
fillHeight: "body",
headerContent: M.util.get_string('navigation', 'scorm'),
visible: true,
xy: navposition,
zIndex: 999
});
scorm_nav_panel.set('bodyContent', navbuttonshtml);
scorm_nav_panel.removeButton('close');
scorm_nav_panel.plug(Y.Plugin.Drag, {handles: ['.yui3-widget-
hd']});
scorm_nav_panel.render();
}
scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
},
'keydown' : function(ev) {
if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode
=== 32) {
scorm_activate_item(scorm_skipprev(scorm_tree_node.getSelectedNodes()[0], true));
}
}
}
});
scorm_buttons[1] = new Y.Button({
srcNode: '#nav_prev',
render: true,
on: {
'click' : function(ev) {
scorm_launch_prev_sco();
},
'keydown' : function(ev) {
if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode ===
32) {
scorm_launch_prev_sco();
}
}
}
});
scorm_buttons[2] = new Y.Button({
srcNode: '#nav_up',
render: true,
on: {
'click' : function(ev) {
scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
},
'keydown' : function(ev) {
if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode ===
32) {
scorm_activate_item(scorm_up(scorm_tree_node.getSelectedNodes()[0], true));
}
}
}
});
scorm_buttons[3] = new Y.Button({
srcNode: '#nav_next',
render: true,
on: {
'click' : function(ev) {
scorm_launch_next_sco();
},
'keydown' : function(ev) {
if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode ===
32) {
scorm_launch_next_sco();
}
}
}
});
scorm_buttons[4] = new Y.Button({
srcNode: '#nav_skipnext',
render: true,
on: {
'click' : function(ev) {
scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
},
'keydown' : function(ev) {
if (ev.domEvent.keyCode === 13 || ev.domEvent.keyCode ===
32) {
scorm_activate_item(scorm_skipnext(scorm_tree_node.getSelectedNodes()[0], true));
}
}
}
});
}
// resizing
scorm_resize_layout();
// Collapse/expand TOC.
Y.one('#scorm_toc_toggle').on('click', scorm_toggle_toc);
Y.one('#scorm_toc_toggle').on('key', scorm_toggle_toc, 'down:enter,32');
// fix layout if window resized
Y.on("windowresize", function() {
scorm_resize_layout();
var toc_displayed = Y.one('#scorm_toc').getComputedStyle('display') !==
'none';
if ((!scorm_disable_toc && !scorm_hide_toc) || toc_displayed) {
scorm_toggle_toc(true);
}
// Set 20% as minWidth constrain of TOC.
var layout_width =
parseInt(Y.one('#scorm_layout').getComputedStyle('width'), 10);
scorm_resize_handle.set('defMinWidth', 0.2 * layout_width);
});
// On resize drag, change width of scorm_content.
scorm_resize_handle.on('resize:resize', function() {
var tocwidth = parseInt(Y.one('#scorm_toc').getComputedStyle('width'),
10);
var layoutwidth = parseInt(Y.one('#scorm_layout').getStyle('width'),
10);
Y.one('#scorm_content').setStyle('width', (layoutwidth - tocwidth -
60));
});
});
};
M.mod_scorm.connectPrereqCallback = {
success: function(id, o) {
if (o.responseText !== undefined) {
var snode = null,
stitle = null;
if (scorm_tree_node && o.responseText) {
snode = scorm_tree_node.getSelectedNodes()[0];
stitle = null;
if (snode) {
stitle = snode.title;
}
// All gone with clear, add new root node.
scorm_tree_node.clear(scorm_tree_node.createNode());
}
// Make sure the temporary tree element is not there.
var el_old_tree = document.getElementById('scormtree123');
if (el_old_tree) {
el_old_tree.parentNode.removeChild(el_old_tree);
}
var el_new_tree = document.createElement('div');
var pagecontent = document.getElementById("page-content");
if (!pagecontent) {
pagecontent = document.getElementById("content");
}
el_new_tree.setAttribute('id','scormtree123');
el_new_tree.innerHTML = o.responseText;
// Make sure it does not show.
el_new_tree.style.display = 'none';
pagecontent.appendChild(el_new_tree);
// Ignore the first level element as this is the title.
var startNode = el_new_tree.firstChild.firstChild;
if (startNode.tagName == 'LI') {
// Go back to the beginning.
startNode = el_new_tree;
}
var toc_source = Y.one('#scormtree123 > ul');
var toc = mod_scorm_parse_toc_tree(toc_source);
scorm_tree_node.appendNode(scorm_tree_node.rootNode, toc);
var el = document.getElementById('scormtree123');
el.parentNode.removeChild(el);
scorm_tree_node.render();
scorm_tree_node.openAll();
if (stitle !== null) {
snode = scorm_tree_node.getNodeByAttribute('title', stitle);
// Do not let destroyed node to be selected.
if (snode && !snode.state.destroyed) {
snode.select();
var toc_disabled = Y.one('#scorm_toc').hasClass('disabled');
if (!toc_disabled) {
if (!snode.state.selected) {
snode.select();
}
}
}
}
}
},
failure: function(id, o) {
// TODO: do some sort of error handling.
}
};