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