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