MDL-46116 mod_imscp: Fix codechecker issues, some moodlecheck issues.
[moodle.git] / mod / imscp / module.js
CommitLineData
57d9e586
PS
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/>.
15
16/**
17 * Javascript helper function for IMS Content Package module.
18 *
3b43615f
PS
19 * @package mod
20 * @subpackage imscp
21 * @copyright 2009 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
57d9e586
PS
23 */
24
25M.mod_imscp = {};
26
27M.mod_imscp.init = function(Y) {
28
29 var imscp_layout_widget;
30 var imscp_current_node;
31 var imscp_buttons = [];
32 var imscp_bloody_labelclick = false;
57d9e586
PS
33
34 Y.use('yui2-resize', 'yui2-dragdrop', 'yui2-container', 'yui2-button', 'yui2-layout', 'yui2-treeview', 'yui2-json', 'yui2-event', function(Y) {
35
581e247b
DB
36 var imscp_activate_item_by_index = function(index) {
37 imscp_activate_item(Y.YUI2.widget.TreeView.getNode('imscp_tree', index));
38 }
39
57d9e586
PS
40 var imscp_activate_item = function(node) {
41 if (!node) {
42 return;
43 }
44 imscp_current_node = node;
45 imscp_current_node.highlight();
46
dbbc5e83 47 var content = new Y.YUI2.util.Element('imscp_content');
949c66ab
DB
48 if (node.href) {
49 try {
50 // First try IE way - it can not set name attribute later
51 // and also it has some restrictions on DOM access from object tag.
52 var obj = document.createElement('<iframe id="imscp_object" src="' + node.href + '">');
53 } catch (e) {
54 var obj = document.createElement('object');
55 obj.setAttribute('id', 'imscp_object');
56 obj.setAttribute('type', 'text/html');
57 obj.setAttribute('data', node.href);
58 }
d5ba9f03
DB
59 } else {
60 // No href, so create links to children.
61 var obj = document.createElement('div');
62 obj.setAttribute('id', 'imscp_child_list');
63
64 var title = document.createElement('h2');
65 title.appendChild(document.createTextNode(node.label));
66 title.setAttribute('class', 'sectionname');
67 obj.appendChild(title);
68
69 var ul = document.createElement('ul');
70 obj.appendChild(ul);
71 for (var i = 0; i < node.children.length; i++) {
72 var childnode = node.children[i];
73 var li = document.createElement('li');
74 var a = document.createElement('a');
75 a.appendChild(document.createTextNode(childnode.label));
76 a.setAttribute('id', 'ref_' + childnode.index);
77 Y.YUI2.util.Event.addListener(a, "click", function () {
78 imscp_activate_item_by_index(this.id.substr(4));
79 });
80 ul.appendChild(li);
81 li.appendChild(a);
82 }
57d9e586 83 }
dbbc5e83 84 var old = Y.YUI2.util.Dom.get('imscp_object');
57d9e586
PS
85 if (old) {
86 content.replaceChild(obj, old);
581e247b
DB
87 } else {
88 old = Y.YUI2.util.Dom.get('imscp_child_list');
89 if (old) {
90 content.replaceChild(obj, old);
91 } else {
92 content.appendChild(obj);
93 }
57d9e586
PS
94 }
95 imscp_resize_frame();
96
97 imscp_current_node.focus();
98 imscp_fixnav();
99 };
100
101 /**
102 * Enables/disables navigation buttons as needed.
103 * @return void
104 */
105 var imscp_fixnav = function() {
106 imscp_buttons[0].set('disabled', (imscp_skipprev(imscp_current_node) == null));
107 imscp_buttons[1].set('disabled', (imscp_prev(imscp_current_node) == null));
108 imscp_buttons[2].set('disabled', (imscp_up(imscp_current_node) == null));
109 imscp_buttons[3].set('disabled', (imscp_next(imscp_current_node) == null));
110 imscp_buttons[4].set('disabled', (imscp_skipnext(imscp_current_node) == null));
111 };
112
57d9e586
PS
113 var imscp_resize_layout = function(alsowidth) {
114 if (alsowidth) {
dbbc5e83 115 var layout = Y.YUI2.util.Dom.get('imscp_layout');
1d6e7ae4
PS
116 var newwidth = imscp_get_htmlelement_size('maincontent', 'width');
117 layout.style.width = '600px';
118 if (newwidth > 600) {
218ab060 119 layout.style.width = newwidth + 'px';
57d9e586
PS
120 }
121 }
218ab060 122 // Make sure that the max width of the TOC doesn't go to far.
1d6e7ae4
PS
123
124 var left = imscp_layout_widget.getUnitByPosition('left');
dbbc5e83 125 var maxwidth = parseInt(Y.YUI2.util.Dom.getStyle('imscp_layout', 'width'));
1d6e7ae4
PS
126 left.set('maxWidth', (maxwidth - 10));
127 var cwidth = left.get('width');
128 if (cwidth > (maxwidth - 10)) {
129 left.set('width', (maxwidth - 10));
130 }
131
ead94e3d
PS
132 var headerheight = imscp_get_htmlelement_size('page-header', 'height');
133 var footerheight = imscp_get_htmlelement_size('page-footer', 'height');
dbbc5e83 134 var newheight = parseInt(Y.YUI2.util.Dom.getViewportHeight()) - footerheight - headerheight - 20;
ead94e3d
PS
135 if (newheight < 400) {
136 newheight = 400;
57d9e586 137 }
218ab060 138 imscp_layout_widget.setStyle('height', newheight + 'px');
1d6e7ae4 139
57d9e586
PS
140 imscp_layout_widget.render();
141 imscp_resize_frame();
57d9e586
PS
142 };
143
144 var imscp_get_htmlelement_size = function(el, prop) {
dbbc5e83 145 var val = Y.YUI2.util.Dom.getStyle(el, prop);
57d9e586
PS
146 if (val == 'auto') {
147 if (el.get) {
218ab060 148 el = el.get('element'); // Get real HTMLElement from YUI element.
57d9e586 149 }
dbbc5e83 150 val = Y.YUI2.util.Dom.getComputedStyle(Y.YUI2.util.Dom.get(el), prop);
57d9e586
PS
151 }
152 return parseInt(val);
153 };
154
155 var imscp_resize_frame = function() {
dbbc5e83 156 var obj = Y.YUI2.util.Dom.get('imscp_object');
57d9e586
PS
157 if (obj) {
158 var content = imscp_layout_widget.getUnitByPosition('center').get('wrap');
218ab060 159 // Basically trap IE6 and 7.
dbbc5e83 160 if (Y.YUI2.env.ua.ie > 5 && Y.YUI2.env.ua.ie < 8) {
1d6e7ae4 161 if( obj.style.setAttribute ) {
218ab060 162 obj.style.setAttribute("cssText", 'width: ' + (content.offsetWidth - 6) + 'px; height: ' + (content.offsetHeight - 10) + 'px;');
1d6e7ae4
PS
163 }
164 else {
218ab060
DB
165 obj.style.setAttribute('width', (content.offsetWidth - 6) + 'px', 0);
166 obj.style.setAttribute('height', (content.offsetHeight - 10) + 'px', 0);
1d6e7ae4
PS
167 }
168 }
169 else {
218ab060
DB
170 obj.style.width = (content.offsetWidth - 6) + 'px';
171 obj.style.height = (content.offsetHeight - 10) + 'px';
1d6e7ae4 172 }
57d9e586
PS
173 }
174 };
175
581e247b 176 var imscp_firstlinked = function(node) {
218ab060 177 // Return first item with an href.
581e247b
DB
178 if (node.href) {
179 return node;
180 } else if (node.children) {
181 return imscp_firstlinked(node.children[0]);
182 } else {
183 return null
184 }
185 };
186
57d9e586
PS
187 var imscp_up = function(node) {
188 if (node.depth > 0) {
189 return node.parent;
190 }
191 return null;
192 };
193
194 var imscp_lastchild = function(node) {
195 if (node.children.length) {
218ab060 196 return imscp_lastchild(node.children[node.children.length - 1]);
57d9e586
PS
197 } else {
198 return node;
199 }
200 };
201
202 var imscp_prev = function(node) {
203 if (node.previousSibling && node.previousSibling.children.length) {
204 return imscp_lastchild(node.previousSibling);
205 }
206 return imscp_skipprev(node);
207 };
208
209 var imscp_skipprev = function(node) {
210 if (node.previousSibling) {
211 return node.previousSibling;
212 } else if (node.depth > 0) {
213 return node.parent;
214 }
215 return null;
216 };
217
218 var imscp_next = function(node) {
219 if (node.children.length) {
220 return node.children[0];
221 }
222 return imscp_skipnext(node);
223 };
224
225 var imscp_skipnext = function(node) {
226 if (node.nextSibling) {
227 return node.nextSibling;
228 } else if (node.depth > 0) {
229 return imscp_skipnext(node.parent);
230 }
231 return null;
232 };
233
218ab060 234 // Layout.
dbbc5e83
PS
235 Y.YUI2.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
236 Y.YUI2.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
57d9e586 237
dbbc5e83 238 imscp_layout_widget = new Y.YUI2.widget.Layout('imscp_layout', {
57d9e586
PS
239 minWidth: 600,
240 minHeight: 400,
241 units: [
2b728cb5 242 { position: 'left', body: 'imscp_toc', header: M.str.imscp.toc, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:150},
57d9e586
PS
243 { position: 'center', body: '<div id="imscp_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
244 ]
245 });
246 imscp_layout_widget.render();
247 var left = imscp_layout_widget.getUnitByPosition('left');
248 left.on('collapse', function() {
249 imscp_resize_frame();
250 });
251 left.on('expand', function() {
252 imscp_resize_frame();
253 });
254
218ab060 255 // Ugly resizing hack that works around problems with resizing of iframes and objects.
57d9e586 256 left._resize.on('startResize', function() {
dbbc5e83 257 var obj = Y.YUI2.util.Dom.get('imscp_object');
57d9e586
PS
258 obj.style.display = 'none';
259 });
260 left._resize.on('endResize', function() {
dbbc5e83 261 var obj = Y.YUI2.util.Dom.get('imscp_object');
57d9e586
PS
262 obj.style.display = 'block';
263 imscp_resize_frame();
264 });
265
218ab060 266 // TOC tree.
dbbc5e83 267 var tree = new Y.YUI2.widget.TreeView('imscp_tree');
57d9e586 268 tree.singleNodeHighlight = true;
165dbc81
PS
269 tree.subscribe('clickEvent', function(oArgs) {
270 imscp_activate_item(oArgs.node);
271 if (oArgs.node.children.length) {
57d9e586
PS
272 imscp_bloody_labelclick = true;
273 }
dbbc5e83 274 Y.YUI2.util.Event.preventDefault(oArgs.event);
165dbc81 275 return false;
57d9e586
PS
276 });
277 tree.subscribe('collapse', function(node) {
278 if (imscp_bloody_labelclick) {
279 imscp_bloody_labelclick = false;
280 return false;
281 }
282 });
283 tree.subscribe('expand', function(node) {
284 if (imscp_bloody_labelclick) {
285 imscp_bloody_labelclick = false;
286 return false;
287 }
288 });
289 tree.expandAll();
290 tree.render();
291
dbbc5e83 292 var navbar = Y.YUI2.util.Dom.get('imscp_nav');
ead94e3d 293 navbar.style.display = 'block';
57d9e586 294
218ab060 295 // Navigation.
dbbc5e83
PS
296 imscp_buttons[0] = new Y.YUI2.widget.Button('nav_skipprev');
297 imscp_buttons[1] = new Y.YUI2.widget.Button('nav_prev');
298 imscp_buttons[2] = new Y.YUI2.widget.Button('nav_up');
299 imscp_buttons[3] = new Y.YUI2.widget.Button('nav_next');
300 imscp_buttons[4] = new Y.YUI2.widget.Button('nav_skipnext');
57d9e586
PS
301 imscp_buttons[0].on('click', function(ev) {
302 imscp_activate_item(imscp_skipprev(imscp_current_node));
303 });
304 imscp_buttons[1].on('click', function(ev) {
305 imscp_activate_item(imscp_prev(imscp_current_node));
306 });
307 imscp_buttons[2].on('click', function(ev) {
308 imscp_activate_item(imscp_up(imscp_current_node));
309 });
310 imscp_buttons[3].on('click', function(ev) {
311 imscp_activate_item(imscp_next(imscp_current_node));
312 });
313 imscp_buttons[4].on('click', function(ev) {
314 imscp_activate_item(imscp_skipnext(imscp_current_node));
315 });
57d9e586 316
218ab060 317 // Finally activate the first item.
581e247b 318 imscp_activate_item(imscp_firstlinked(tree.getRoot().children[0]));
57d9e586 319
218ab060 320 // Resizing.
57d9e586
PS
321 imscp_resize_layout(false);
322
218ab060 323 // Fix layout if window resized.
57d9e586
PS
324 window.onresize = function() {
325 imscp_resize_layout(true);
326 };
327 });
328};