message MDL-24548 changed message processor defaults
[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;
33 var imscp_panel;
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');
83 layout.style.width = '500px';
84 var newwidth = imscp_get_htmlelement_size('content', 'width');
85 if (newwidth > 500) {
86 layout.style.width = newwidth+'px';
87 }
88 }
89 var pageheight = imscp_get_htmlelement_size('page', 'height');
90 var layoutheight = imscp_get_htmlelement_size(imscp_layout_widget, 'height');
91 var newheight = layoutheight + parseInt(YAHOO.util.Dom.getViewportHeight()) - pageheight - 20;
92 if (newheight > 400) {
93 imscp_layout_widget.setStyle('height', newheight+'px');
94 }
95 imscp_layout_widget.render();
96 imscp_resize_frame();
97
98 imscp_panel.align('bl', 'bl');
99 };
100
101 var imscp_get_htmlelement_size = function(el, prop) {
102 var val = YAHOO.util.Dom.getStyle(el, prop);
103 if (val == 'auto') {
104 if (el.get) {
105 el = el.get('element'); // get real HTMLElement from YUI element
106 }
107 val = YAHOO.util.Dom.getComputedStyle(YAHOO.util.Dom.get(el), prop);
108 }
109 return parseInt(val);
110 };
111
112 var imscp_resize_frame = function() {
113 var obj = YAHOO.util.Dom.get('imscp_object');
114 if (obj) {
115 var content = imscp_layout_widget.getUnitByPosition('center').get('wrap');
116 obj.style.width = (content.offsetWidth - 6)+'px';
117 obj.style.height = (content.offsetHeight - 10)+'px';
118 }
119 };
120
121
122 var imscp_up = function(node) {
123 if (node.depth > 0) {
124 return node.parent;
125 }
126 return null;
127 };
128
129 var imscp_lastchild = function(node) {
130 if (node.children.length) {
131 return imscp_lastchild(node.children[node.children.length-1]);
132 } else {
133 return node;
134 }
135 };
136
137 var imscp_prev = function(node) {
138 if (node.previousSibling && node.previousSibling.children.length) {
139 return imscp_lastchild(node.previousSibling);
140 }
141 return imscp_skipprev(node);
142 };
143
144 var imscp_skipprev = function(node) {
145 if (node.previousSibling) {
146 return node.previousSibling;
147 } else if (node.depth > 0) {
148 return node.parent;
149 }
150 return null;
151 };
152
153 var imscp_next = function(node) {
154 if (node.children.length) {
155 return node.children[0];
156 }
157 return imscp_skipnext(node);
158 };
159
160 var imscp_skipnext = function(node) {
161 if (node.nextSibling) {
162 return node.nextSibling;
163 } else if (node.depth > 0) {
164 return imscp_skipnext(node.parent);
165 }
166 return null;
167 };
168
169
170 // layout
2b728cb5
PS
171 YAHOO.widget.LayoutUnit.prototype.STR_COLLAPSE = M.str.moodle.hide;
172 YAHOO.widget.LayoutUnit.prototype.STR_EXPAND = M.str.moodle.show;
57d9e586
PS
173
174 imscp_layout_widget = new YAHOO.widget.Layout('imscp_layout', {
175 minWidth: 600,
176 minHeight: 400,
177 units: [
2b728cb5 178 { 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
179 { position: 'center', body: '<div id="imscp_content"></div>', gutter: '2px 5px 5px 2px', scroll: true}
180 ]
181 });
182 imscp_layout_widget.render();
183 var left = imscp_layout_widget.getUnitByPosition('left');
184 left.on('collapse', function() {
185 imscp_resize_frame();
186 });
187 left.on('expand', function() {
188 imscp_resize_frame();
189 });
190
191 // ugly resizing hack that works around problems with resizing of iframes and objects
192 left._resize.on('startResize', function() {
193 var obj = YAHOO.util.Dom.get('imscp_object');
194 obj.style.display = 'none';
195 });
196 left._resize.on('endResize', function() {
197 var obj = YAHOO.util.Dom.get('imscp_object');
198 obj.style.display = 'block';
199 imscp_resize_frame();
200 });
201
202 // TOC tree
203 var tree = new YAHOO.widget.TreeView('imscp_tree');
204 tree.singleNodeHighlight = true;
165dbc81
PS
205 tree.subscribe('clickEvent', function(oArgs) {
206 imscp_activate_item(oArgs.node);
207 if (oArgs.node.children.length) {
57d9e586
PS
208 imscp_bloody_labelclick = true;
209 }
165dbc81
PS
210 YAHOO.util.Event.preventDefault(oArgs.event);
211 return false;
57d9e586
PS
212 });
213 tree.subscribe('collapse', function(node) {
214 if (imscp_bloody_labelclick) {
215 imscp_bloody_labelclick = false;
216 return false;
217 }
218 });
219 tree.subscribe('expand', function(node) {
220 if (imscp_bloody_labelclick) {
221 imscp_bloody_labelclick = false;
222 return false;
223 }
224 });
225 tree.expandAll();
226 tree.render();
227
228 // navigation
229 imscp_panel = new YAHOO.widget.Panel('imscp_navpanel', { visible:true, draggable:true, close:false,
230 context: ['page', 'bl', 'bl', ["windowScroll", "textResize", "windowResize"]], constraintoviewport:true} );
2b728cb5 231 imscp_panel.setHeader(M.str.imscp.navigation);
57d9e586
PS
232
233 //TODO: make some better&accessible buttons
234 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>');
235 imscp_panel.render();
236 imscp_buttons[0] = new YAHOO.widget.Button('nav_skipprev');
237 imscp_buttons[1] = new YAHOO.widget.Button('nav_prev');
238 imscp_buttons[2] = new YAHOO.widget.Button('nav_up');
239 imscp_buttons[3] = new YAHOO.widget.Button('nav_next');
240 imscp_buttons[4] = new YAHOO.widget.Button('nav_skipnext');
241 imscp_buttons[0].on('click', function(ev) {
242 imscp_activate_item(imscp_skipprev(imscp_current_node));
243 });
244 imscp_buttons[1].on('click', function(ev) {
245 imscp_activate_item(imscp_prev(imscp_current_node));
246 });
247 imscp_buttons[2].on('click', function(ev) {
248 imscp_activate_item(imscp_up(imscp_current_node));
249 });
250 imscp_buttons[3].on('click', function(ev) {
251 imscp_activate_item(imscp_next(imscp_current_node));
252 });
253 imscp_buttons[4].on('click', function(ev) {
254 imscp_activate_item(imscp_skipnext(imscp_current_node));
255 });
256 imscp_panel.render();
257
258 // finally activate the first item
259 imscp_activate_item(tree.getRoot().children[0]);
260
261 // resizing
262 imscp_resize_layout(false);
263
264 // fix layout if window resized
265 window.onresize = function() {
266 imscp_resize_layout(true);
267 };
268 });
269};