MDL-21400 imscp module converted to new JS coding style
[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 *
19 * @package mod-imscp
20 * @copyright 2009 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24M.mod_imscp = {};
25
26M.mod_imscp.init = function(Y) {
27
28 var imscp_layout_widget;
29 var imscp_current_node;
30 var imscp_buttons = [];
31 var imscp_bloody_labelclick = false;
32 var imscp_panel;
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
43 var content = new YAHOO.util.Element('imscp_content');
44 try {
45 // first try IE way - it can not set name attribute later
46 // and also it has some restrictions on DOM access from object tag
47 var obj = document.createElement('<iframe id="imscp_object" src="'+node.href+'">');
48 } catch (e) {
49 var obj = document.createElement('object');
50 obj.setAttribute('id', 'imscp_object');
51 obj.setAttribute('type', 'text/html');
52 obj.setAttribute('data', node.href);
53 }
54 var old = YAHOO.util.Dom.get('imscp_object');
55 if (old) {
56 content.replaceChild(obj, old);
57 } else {
58 content.appendChild(obj);
59 }
60 imscp_resize_frame();
61
62 imscp_current_node.focus();
63 imscp_fixnav();
64 };
65
66 /**
67 * Enables/disables navigation buttons as needed.
68 * @return void
69 */
70 var imscp_fixnav = function() {
71 imscp_buttons[0].set('disabled', (imscp_skipprev(imscp_current_node) == null));
72 imscp_buttons[1].set('disabled', (imscp_prev(imscp_current_node) == null));
73 imscp_buttons[2].set('disabled', (imscp_up(imscp_current_node) == null));
74 imscp_buttons[3].set('disabled', (imscp_next(imscp_current_node) == null));
75 imscp_buttons[4].set('disabled', (imscp_skipnext(imscp_current_node) == null));
76 };
77
78
79 var imscp_resize_layout = function(alsowidth) {
80 if (alsowidth) {
81 var layout = YAHOO.util.Dom.get('imscp_layout');
82 layout.style.width = '500px';
83 var newwidth = imscp_get_htmlelement_size('content', 'width');
84 if (newwidth > 500) {
85 layout.style.width = newwidth+'px';
86 }
87 }
88 var pageheight = imscp_get_htmlelement_size('page', 'height');
89 var layoutheight = imscp_get_htmlelement_size(imscp_layout_widget, 'height');
90 var newheight = layoutheight + parseInt(YAHOO.util.Dom.getViewportHeight()) - pageheight - 20;
91 if (newheight > 400) {
92 imscp_layout_widget.setStyle('height', newheight+'px');
93 }
94 imscp_layout_widget.render();
95 imscp_resize_frame();
96
97 imscp_panel.align('bl', 'bl');
98 };
99
100 var imscp_get_htmlelement_size = function(el, prop) {
101 var val = YAHOO.util.Dom.getStyle(el, prop);
102 if (val == 'auto') {
103 if (el.get) {
104 el = el.get('element'); // get real HTMLElement from YUI element
105 }
106 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
107 }
108 return parseInt(val);
109 };
110
111 var imscp_resize_frame = function() {
112 var obj = YAHOO.util.Dom.get('imscp_object');
113 if (obj) {
114 var content = imscp_layout_widget.getUnitByPosition('center').get('wrap');
115 obj.style.width = (content.offsetWidth - 6)+'px';
116 obj.style.height = (content.offsetHeight - 10)+'px';
117 }
118 };
119
120
121 var imscp_up = function(node) {
122 if (node.depth > 0) {
123 return node.parent;
124 }
125 return null;
126 };
127
128 var imscp_lastchild = function(node) {
129 if (node.children.length) {
130 return imscp_lastchild(node.children[node.children.length-1]);
131 } else {
132 return node;
133 }
134 };
135
136 var imscp_prev = function(node) {
137 if (node.previousSibling && node.previousSibling.children.length) {
138 return imscp_lastchild(node.previousSibling);
139 }
140 return imscp_skipprev(node);
141 };
142
143 var imscp_skipprev = function(node) {
144 if (node.previousSibling) {
145 return node.previousSibling;
146 } else if (node.depth > 0) {
147 return node.parent;
148 }
149 return null;
150 };
151
152 var imscp_next = function(node) {
153 if (node.children.length) {
154 return node.children[0];
155 }
156 return imscp_skipnext(node);
157 };
158
159 var imscp_skipnext = function(node) {
160 if (node.nextSibling) {
161 return node.nextSibling;
162 } else if (node.depth > 0) {
163 return imscp_skipnext(node.parent);
164 }
165 return null;
166 };
167
168
169 // layout
170 YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = mstr.moodle.hide;
171 YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = mstr.moodle.show;
172
173 imscp_layout_widget = new YAHOO.widget.Layout('imscp_layout', {
174 minWidth: 600,
175 minHeight: 400,
176 units: [
177 { position: 'left', body: 'imscp_toc', header: mstr.imscp.toc, width: 250, resize: true, gutter: '2px 5px 5px 2px', collapse: true, minWidth:150},
178 { position: 'center', body: '<div id="imscp_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
179 ]
180 });
181 imscp_layout_widget.render();
182 var left = imscp_layout_widget.getUnitByPosition('left');
183 left.on('collapse', function() {
184 imscp_resize_frame();
185 });
186 left.on('expand', function() {
187 imscp_resize_frame();
188 });
189
190 // ugly resizing hack that works around problems with resizing of iframes and objects
191 left._resize.on('startResize', function() {
192 var obj = YAHOO.util.Dom.get('imscp_object');
193 obj.style.display = 'none';
194 });
195 left._resize.on('endResize', function() {
196 var obj = YAHOO.util.Dom.get('imscp_object');
197 obj.style.display = 'block';
198 imscp_resize_frame();
199 });
200
201 // TOC tree
202 var tree = new YAHOO.widget.TreeView('imscp_tree');
203 tree.singleNodeHighlight = true;
204 tree.subscribe('labelClick', function(node) {
205 imscp_activate_item(node);
206 if (node.children.length) {
207 imscp_bloody_labelclick = true;
208 }
209 });
210 tree.subscribe('collapse', function(node) {
211 if (imscp_bloody_labelclick) {
212 imscp_bloody_labelclick = false;
213 return false;
214 }
215 });
216 tree.subscribe('expand', function(node) {
217 if (imscp_bloody_labelclick) {
218 imscp_bloody_labelclick = false;
219 return false;
220 }
221 });
222 tree.expandAll();
223 tree.render();
224
225 // navigation
226 imscp_panel = new YAHOO.widget.Panel('imscp_navpanel', { visible:true, draggable:true, close:false,
227 context: ['page', 'bl', 'bl', ["windowScroll", "textResize", "windowResize"]], constraintoviewport:true} );
228 imscp_panel.setHeader(mstr.imscp.navigation);
229
230 //TODO: make some better&accessible buttons
231 imscp_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>');
232 imscp_panel.render();
233 imscp_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
234 imscp_buttons[1] = new YAHOO.widget.Button('nav_prev');
235 imscp_buttons[2] = new YAHOO.widget.Button('nav_up');
236 imscp_buttons[3] = new YAHOO.widget.Button('nav_next');
237 imscp_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
238 imscp_buttons[0].on('click', function(ev) {
239 imscp_activate_item(imscp_skipprev(imscp_current_node));
240 });
241 imscp_buttons[1].on('click', function(ev) {
242 imscp_activate_item(imscp_prev(imscp_current_node));
243 });
244 imscp_buttons[2].on('click', function(ev) {
245 imscp_activate_item(imscp_up(imscp_current_node));
246 });
247 imscp_buttons[3].on('click', function(ev) {
248 imscp_activate_item(imscp_next(imscp_current_node));
249 });
250 imscp_buttons[4].on('click', function(ev) {
251 imscp_activate_item(imscp_skipnext(imscp_current_node));
252 });
253 imscp_panel.render();
254
255 // finally activate the first item
256 imscp_activate_item(tree.getRoot().children[0]);
257
258 // resizing
259 imscp_resize_layout(false);
260
261 // fix layout if window resized
262 window.onresize = function() {
263 imscp_resize_layout(true);
264 };
265 });
266};