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