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