|
1 cw = {}; |
|
2 |
|
3 jQuery.extend(cw, { |
|
4 log: function () { |
|
5 var args = []; |
|
6 for (var i = 0; i < arguments.length; i++) { |
|
7 args.push(arguments[i]); |
|
8 } |
|
9 if (typeof(window) != "undefined" && window.console && window.console.log) { |
|
10 window.console.log(args.join(' ')); |
|
11 } |
|
12 }, |
|
13 |
|
14 //removed: getElementsByTagAndClassName, replaceChildNodes, toggleElementClass |
|
15 // partial, merge, isNotEmpty, update, |
|
16 // String.in_, String.join, list, getattr, attrgetter, methodcaller, |
|
17 // min, max, dict, concat |
|
18 jqNode: function (node) { |
|
19 /** |
|
20 * .. function:: jqNode(node) |
|
21 * |
|
22 * safe version of jQuery('#nodeid') because we use ':' in nodeids |
|
23 * which messes with jQuery selection mechanism |
|
24 */ |
|
25 if (typeof(node) == 'string') { |
|
26 node = document.getElementById(node); |
|
27 } |
|
28 if (node) { |
|
29 return $(node); |
|
30 } |
|
31 return null; |
|
32 }, |
|
33 |
|
34 getNode: function (node) { |
|
35 if (typeof(node) == 'string') { |
|
36 return document.getElementById(node); |
|
37 } |
|
38 return node; |
|
39 }, |
|
40 |
|
41 evalJSON: function (json) { // trust source |
|
42 return eval("(" + json + ")"); |
|
43 }, |
|
44 |
|
45 urlEncode: function (str) { |
|
46 if (typeof(encodeURIComponent) != "undefined") { |
|
47 return encodeURIComponent(str).replace(/\'/g, '%27'); |
|
48 } else { |
|
49 return escape(str).replace(/\+/g, '%2B').replace(/\"/g, '%22'). |
|
50 rval.replace(/\'/g, '%27'); |
|
51 } |
|
52 }, |
|
53 |
|
54 swapDOM: function (dest, src) { |
|
55 dest = getNode(dest); |
|
56 var parent = dest.parentNode; |
|
57 if (src) { |
|
58 src = getNode(src); |
|
59 parent.replaceChild(src, dest); |
|
60 } else { |
|
61 parent.removeChild(dest); |
|
62 } |
|
63 return src; |
|
64 } |
|
65 }); |
|
66 |
|
67 |
|
68 cw.utils = { |
|
69 |
|
70 deprecatedFunction: function (msg, newfunc) { |
|
71 return function () { |
|
72 cw.log(msg); |
|
73 return newfunc.apply(this, arguments); |
|
74 }; |
|
75 }, |
|
76 |
|
77 movedToNamespace: function (funcnames, namespace) { |
|
78 for (var i = 0; i < funcnames.length; i++) { |
|
79 var funcname = funcnames[i]; |
|
80 var msg = '[3.9] ' + funcname + ' is deprecated, use cw.' + funcname + ' instead'; |
|
81 window[funcname] = cw.utils.deprecatedFunction(msg, namespace[funcname]); |
|
82 } |
|
83 }, |
|
84 |
|
85 createDomFunction: function (tag) { |
|
86 function builddom(params, children) { |
|
87 var node = document.createElement(tag); |
|
88 for (key in params) { |
|
89 var value = params[key]; |
|
90 if (key.substring(0, 2) == 'on') { |
|
91 // this is an event handler definition |
|
92 if (typeof value == 'string') { |
|
93 // litteral definition |
|
94 value = new Function(value); |
|
95 } |
|
96 node[key] = value; |
|
97 } else { // normal node attribute |
|
98 jQuery(node).attr(key, params[key]); |
|
99 } |
|
100 } |
|
101 if (children) { |
|
102 if (!cw.utils.isArrayLike(children)) { |
|
103 children = [children]; |
|
104 for (var i = 2; i < arguments.length; i++) { |
|
105 var arg = arguments[i]; |
|
106 if (cw.utils.isArray(arg)) { |
|
107 jQuery.merge(children, arg); |
|
108 } else { |
|
109 children.push(arg); |
|
110 } |
|
111 } |
|
112 } |
|
113 for (var i = 0; i < children.length; i++) { |
|
114 var child = children[i]; |
|
115 if (typeof child == "string" || typeof child == "number") { |
|
116 child = document.createTextNode(child); |
|
117 } |
|
118 node.appendChild(child); |
|
119 } |
|
120 } |
|
121 return node; |
|
122 } |
|
123 return builddom; |
|
124 }, |
|
125 |
|
126 /** |
|
127 * .. function:: toISOTimestamp(date) |
|
128 * |
|
129 */ |
|
130 toISOTimestamp: function (date) { |
|
131 if (typeof(date) == "undefined" || date === null) { |
|
132 return null; |
|
133 } |
|
134 |
|
135 function _padTwo(n) { |
|
136 return (n > 9) ? n : "0" + n; |
|
137 } |
|
138 var isoTime = [_padTwo(date.getHours()), _padTwo(date.getMinutes()), |
|
139 _padTwo(date.getSeconds())].join(':'); |
|
140 var isoDate = [date.getFullYear(), _padTwo(date.getMonth() + 1), |
|
141 _padTwo(date.getDate())].join("-"); |
|
142 return isoDate + " " + isoTime; |
|
143 }, |
|
144 |
|
145 /** |
|
146 * .. function:: nodeWalkDepthFirst(node, visitor) |
|
147 * |
|
148 * depth-first implementation of the nodeWalk function found |
|
149 * in `MochiKit.Base <http://mochikit.com/doc/html/MochiKit/Base.html#fn-nodewalk>`_ |
|
150 */ |
|
151 nodeWalkDepthFirst: function (node, visitor) { |
|
152 var children = visitor(node); |
|
153 if (children) { |
|
154 for (var i = 0; i < children.length; i++) { |
|
155 nodeWalkDepthFirst(children[i], visitor); |
|
156 } |
|
157 } |
|
158 }, |
|
159 |
|
160 isArray: function (it) { // taken from dojo |
|
161 return it && (it instanceof Array || typeof it == "array"); |
|
162 }, |
|
163 |
|
164 isString: function (it) { // taken from dojo |
|
165 return !!arguments.length && it != null && (typeof it == "string" || it instanceof String); |
|
166 }, |
|
167 |
|
168 isArrayLike: function (it) { // taken from dojo |
|
169 return (it && it !== undefined && |
|
170 // keep out built-in constructors (Number, String, ...) |
|
171 // which have length properties |
|
172 !cw.utils.isString(it) && !jQuery.isFunction(it) && |
|
173 !(it.tagName && it.tagName.toLowerCase() == 'form') && |
|
174 (cw.utils.isArray(it) || isFinite(it.length))); |
|
175 }, |
|
176 |
|
177 /** |
|
178 * .. function:: formContents(elem \/* = document.body *\/) |
|
179 * |
|
180 * this implementation comes from MochiKit |
|
181 */ |
|
182 formContents: function (elem /* = document.body */ ) { |
|
183 var names = []; |
|
184 var values = []; |
|
185 if (typeof(elem) == "undefined" || elem === null) { |
|
186 elem = document.body; |
|
187 } else { |
|
188 elem = getNode(elem); |
|
189 } |
|
190 cw.utils.nodeWalkDepthFirst(elem, function (elem) { |
|
191 var name = elem.name; |
|
192 if (name && name.length) { |
|
193 var tagName = elem.tagName.toUpperCase(); |
|
194 if (tagName === "INPUT" && (elem.type == "radio" || elem.type == "checkbox") && !elem.checked) { |
|
195 return null; |
|
196 } |
|
197 if (tagName === "SELECT") { |
|
198 if (elem.type == "select-one") { |
|
199 if (elem.selectedIndex >= 0) { |
|
200 var opt = elem.options[elem.selectedIndex]; |
|
201 var v = opt.value; |
|
202 if (!v) { |
|
203 var h = opt.outerHTML; |
|
204 // internet explorer sure does suck. |
|
205 if (h && !h.match(/^[^>]+\svalue\s*=/i)) { |
|
206 v = opt.text; |
|
207 } |
|
208 } |
|
209 names.push(name); |
|
210 values.push(v); |
|
211 return null; |
|
212 } |
|
213 // no form elements? |
|
214 names.push(name); |
|
215 values.push(""); |
|
216 return null; |
|
217 } else { |
|
218 var opts = elem.options; |
|
219 if (!opts.length) { |
|
220 names.push(name); |
|
221 values.push(""); |
|
222 return null; |
|
223 } |
|
224 for (var i = 0; i < opts.length; i++) { |
|
225 var opt = opts[i]; |
|
226 if (!opt.selected) { |
|
227 continue; |
|
228 } |
|
229 var v = opt.value; |
|
230 if (!v) { |
|
231 var h = opt.outerHTML; |
|
232 // internet explorer sure does suck. |
|
233 if (h && !h.match(/^[^>]+\svalue\s*=/i)) { |
|
234 v = opt.text; |
|
235 } |
|
236 } |
|
237 names.push(name); |
|
238 values.push(v); |
|
239 } |
|
240 return null; |
|
241 } |
|
242 } |
|
243 if (tagName === "FORM" || tagName === "P" || tagName === "SPAN" || tagName === "DIV") { |
|
244 return elem.childNodes; |
|
245 } |
|
246 names.push(name); |
|
247 values.push(elem.value || ''); |
|
248 return null; |
|
249 } |
|
250 return elem.childNodes; |
|
251 }); |
|
252 return [names, values]; |
|
253 }, |
|
254 |
|
255 /** |
|
256 * .. function:: sliceList(lst, start, stop, step) |
|
257 * |
|
258 * returns a subslice of `lst` using `start`/`stop`/`step` |
|
259 * start, stop might be negative |
|
260 * |
|
261 * >>> sliceList(['a', 'b', 'c', 'd', 'e', 'f'], 2) |
|
262 * ['c', 'd', 'e', 'f'] |
|
263 * >>> sliceList(['a', 'b', 'c', 'd', 'e', 'f'], 2, -2) |
|
264 * ['c', 'd'] |
|
265 * >>> sliceList(['a', 'b', 'c', 'd', 'e', 'f'], -3) |
|
266 * ['d', 'e', 'f'] |
|
267 */ |
|
268 sliceList: function (lst, start, stop, step) { |
|
269 start = start || 0; |
|
270 stop = stop || lst.length; |
|
271 step = step || 1; |
|
272 if (stop < 0) { |
|
273 stop = Math.max(lst.length + stop, 0); |
|
274 } |
|
275 if (start < 0) { |
|
276 start = Math.min(lst.length + start, lst.length); |
|
277 } |
|
278 var result = []; |
|
279 for (var i = start; i < stop; i += step) { |
|
280 result.push(lst[i]); |
|
281 } |
|
282 return result; |
|
283 } |
|
284 |
|
285 |
|
286 }; |
|
287 |
|
288 String.prototype.startsWith = cw.utils.deprecatedFunction('[3.9] str.startsWith() is deprecated, use str.startswith() instead', function (prefix) { |
|
289 return this.startswith(prefix); |
|
290 }); |
|
291 |
|
292 String.prototype.endsWith = cw.utils.deprecatedFunction('[3.9] str.endsWith() is deprecated, use str.endswith() instead', function (suffix) { |
|
293 return this.endswith(prefix); |
|
294 }); |
|
295 |
|
296 /** DOM factories ************************************************************/ |
|
297 A = cw.utils.createDomFunction('a'); |
|
298 BUTTON = cw.utils.createDomFunction('button'); |
|
299 BR = cw.utils.createDomFunction('br'); |
|
300 CANVAS = cw.utils.createDomFunction('canvas'); |
|
301 DD = cw.utils.createDomFunction('dd'); |
|
302 DIV = cw.utils.createDomFunction('div'); |
|
303 DL = cw.utils.createDomFunction('dl'); |
|
304 DT = cw.utils.createDomFunction('dt'); |
|
305 FIELDSET = cw.utils.createDomFunction('fieldset'); |
|
306 FORM = cw.utils.createDomFunction('form'); |
|
307 H1 = cw.utils.createDomFunction('H1'); |
|
308 H2 = cw.utils.createDomFunction('H2'); |
|
309 H3 = cw.utils.createDomFunction('H3'); |
|
310 H4 = cw.utils.createDomFunction('H4'); |
|
311 H5 = cw.utils.createDomFunction('H5'); |
|
312 H6 = cw.utils.createDomFunction('H6'); |
|
313 HR = cw.utils.createDomFunction('hr'); |
|
314 IMG = cw.utils.createDomFunction('img'); |
|
315 INPUT = cw.utils.createDomFunction('input'); |
|
316 LABEL = cw.utils.createDomFunction('label'); |
|
317 LEGEND = cw.utils.createDomFunction('legend'); |
|
318 LI = cw.utils.createDomFunction('li'); |
|
319 OL = cw.utils.createDomFunction('ol'); |
|
320 OPTGROUP = cw.utils.createDomFunction('optgroup'); |
|
321 OPTION = cw.utils.createDomFunction('option'); |
|
322 P = cw.utils.createDomFunction('p'); |
|
323 PRE = cw.utils.createDomFunction('pre'); |
|
324 SELECT = cw.utils.createDomFunction('select'); |
|
325 SPAN = cw.utils.createDomFunction('span'); |
|
326 STRONG = cw.utils.createDomFunction('strong'); |
|
327 TABLE = cw.utils.createDomFunction('table'); |
|
328 TBODY = cw.utils.createDomFunction('tbody'); |
|
329 TD = cw.utils.createDomFunction('td'); |
|
330 TEXTAREA = cw.utils.createDomFunction('textarea'); |
|
331 TFOOT = cw.utils.createDomFunction('tfoot'); |
|
332 TH = cw.utils.createDomFunction('th'); |
|
333 THEAD = cw.utils.createDomFunction('thead'); |
|
334 TR = cw.utils.createDomFunction('tr'); |
|
335 TT = cw.utils.createDomFunction('tt'); |
|
336 UL = cw.utils.createDomFunction('ul'); |
|
337 |
|
338 // cubicweb specific |
|
339 //IFRAME = cw.utils.createDomFunction('iframe'); |
|
340 |
|
341 |
|
342 function IFRAME(params) { |
|
343 if ('name' in params) { |
|
344 try { |
|
345 var node = document.createElement('<iframe name="' + params['name'] + '">'); |
|
346 } catch (ex) { |
|
347 var node = document.createElement('iframe'); |
|
348 node.id = node.name = params.name; |
|
349 } |
|
350 } |
|
351 else { |
|
352 var node = document.createElement('iframe'); |
|
353 } |
|
354 for (key in params) { |
|
355 if (key != 'name') { |
|
356 var value = params[key]; |
|
357 if (key.substring(0, 2) == 'on') { |
|
358 // this is an event handler definition |
|
359 if (typeof value == 'string') { |
|
360 // litteral definition |
|
361 value = new Function(value); |
|
362 } |
|
363 node[key] = value; |
|
364 } else { // normal node attribute |
|
365 node.setAttribute(key, params[key]); |
|
366 } |
|
367 } |
|
368 } |
|
369 return node; |
|
370 } |