|
1 /* Prototype JavaScript framework, version 1.5.0_rc1 |
|
2 * (c) 2005 Sam Stephenson <sam@conio.net> |
|
3 * |
|
4 * Prototype is freely distributable under the terms of an MIT-style license. |
|
5 * For details, see the Prototype web site: http://prototype.conio.net/ |
|
6 * |
|
7 /*--------------------------------------------------------------------------*/ |
|
8 |
|
9 var Prototype = { |
|
10 Version: '1.5.0_rc1', |
|
11 ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', |
|
12 |
|
13 emptyFunction: function() {}, |
|
14 K: function(x) {return x} |
|
15 } |
|
16 |
|
17 var Class = { |
|
18 create: function() { |
|
19 return function() { |
|
20 this.initialize.apply(this, arguments); |
|
21 } |
|
22 } |
|
23 } |
|
24 |
|
25 var Abstract = new Object(); |
|
26 |
|
27 Object.extend = function(destination, source) { |
|
28 for (var property in source) { |
|
29 destination[property] = source[property]; |
|
30 } |
|
31 return destination; |
|
32 } |
|
33 |
|
34 Object.extend(Object, { |
|
35 inspect: function(object) { |
|
36 try { |
|
37 if (object == undefined) return 'undefined'; |
|
38 if (object == null) return 'null'; |
|
39 return object.inspect ? object.inspect() : object.toString(); |
|
40 } catch (e) { |
|
41 if (e instanceof RangeError) return '...'; |
|
42 throw e; |
|
43 } |
|
44 }, |
|
45 |
|
46 keys: function(object) { |
|
47 var keys = []; |
|
48 for (var property in object) |
|
49 keys.push(property); |
|
50 return keys; |
|
51 }, |
|
52 |
|
53 values: function(object) { |
|
54 var values = []; |
|
55 for (var property in object) |
|
56 values.push(object[property]); |
|
57 return values; |
|
58 }, |
|
59 |
|
60 clone: function(object) { |
|
61 return Object.extend({}, object); |
|
62 } |
|
63 }); |
|
64 |
|
65 Function.prototype.bind = function() { |
|
66 var __method = this, args = $A(arguments), object = args.shift(); |
|
67 return function() { |
|
68 return __method.apply(object, args.concat($A(arguments))); |
|
69 } |
|
70 } |
|
71 |
|
72 Function.prototype.bindAsEventListener = function(object) { |
|
73 var __method = this, args = $A(arguments), object = args.shift(); |
|
74 return function(event) { |
|
75 return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments))); |
|
76 } |
|
77 } |
|
78 |
|
79 Object.extend(Number.prototype, { |
|
80 toColorPart: function() { |
|
81 var digits = this.toString(16); |
|
82 if (this < 16) return '0' + digits; |
|
83 return digits; |
|
84 }, |
|
85 |
|
86 succ: function() { |
|
87 return this + 1; |
|
88 }, |
|
89 |
|
90 times: function(iterator) { |
|
91 $R(0, this, true).each(iterator); |
|
92 return this; |
|
93 } |
|
94 }); |
|
95 |
|
96 var Try = { |
|
97 these: function() { |
|
98 var returnValue; |
|
99 |
|
100 for (var i = 0; i < arguments.length; i++) { |
|
101 var lambda = arguments[i]; |
|
102 try { |
|
103 returnValue = lambda(); |
|
104 break; |
|
105 } catch (e) {} |
|
106 } |
|
107 |
|
108 return returnValue; |
|
109 } |
|
110 } |
|
111 |
|
112 /*--------------------------------------------------------------------------*/ |
|
113 |
|
114 var PeriodicalExecuter = Class.create(); |
|
115 PeriodicalExecuter.prototype = { |
|
116 initialize: function(callback, frequency) { |
|
117 this.callback = callback; |
|
118 this.frequency = frequency; |
|
119 this.currentlyExecuting = false; |
|
120 |
|
121 this.registerCallback(); |
|
122 }, |
|
123 |
|
124 registerCallback: function() { |
|
125 this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); |
|
126 }, |
|
127 |
|
128 stop: function() { |
|
129 if (!this.timer) return; |
|
130 clearInterval(this.timer); |
|
131 this.timer = null; |
|
132 }, |
|
133 |
|
134 onTimerEvent: function() { |
|
135 if (!this.currentlyExecuting) { |
|
136 try { |
|
137 this.currentlyExecuting = true; |
|
138 this.callback(this); |
|
139 } finally { |
|
140 this.currentlyExecuting = false; |
|
141 } |
|
142 } |
|
143 } |
|
144 } |
|
145 Object.extend(String.prototype, { |
|
146 gsub: function(pattern, replacement) { |
|
147 var result = '', source = this, match; |
|
148 replacement = arguments.callee.prepareReplacement(replacement); |
|
149 |
|
150 while (source.length > 0) { |
|
151 if (match = source.match(pattern)) { |
|
152 result += source.slice(0, match.index); |
|
153 result += (replacement(match) || '').toString(); |
|
154 source = source.slice(match.index + match[0].length); |
|
155 } else { |
|
156 result += source, source = ''; |
|
157 } |
|
158 } |
|
159 return result; |
|
160 }, |
|
161 |
|
162 sub: function(pattern, replacement, count) { |
|
163 replacement = this.gsub.prepareReplacement(replacement); |
|
164 count = count === undefined ? 1 : count; |
|
165 |
|
166 return this.gsub(pattern, function(match) { |
|
167 if (--count < 0) return match[0]; |
|
168 return replacement(match); |
|
169 }); |
|
170 }, |
|
171 |
|
172 scan: function(pattern, iterator) { |
|
173 this.gsub(pattern, iterator); |
|
174 return this; |
|
175 }, |
|
176 |
|
177 truncate: function(length, truncation) { |
|
178 length = length || 30; |
|
179 truncation = truncation === undefined ? '...' : truncation; |
|
180 return this.length > length ? |
|
181 this.slice(0, length - truncation.length) + truncation : this; |
|
182 }, |
|
183 |
|
184 strip: function() { |
|
185 return this.replace(/^\s+/, '').replace(/\s+$/, ''); |
|
186 }, |
|
187 |
|
188 stripTags: function() { |
|
189 return this.replace(/<\/?[^>]+>/gi, ''); |
|
190 }, |
|
191 |
|
192 stripScripts: function() { |
|
193 return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); |
|
194 }, |
|
195 |
|
196 extractScripts: function() { |
|
197 var matchAll = new RegExp(Prototype.ScriptFragment, 'img'); |
|
198 var matchOne = new RegExp(Prototype.ScriptFragment, 'im'); |
|
199 return (this.match(matchAll) || []).map(function(scriptTag) { |
|
200 return (scriptTag.match(matchOne) || ['', ''])[1]; |
|
201 }); |
|
202 }, |
|
203 |
|
204 evalScripts: function() { |
|
205 return this.extractScripts().map(function(script) { return eval(script) }); |
|
206 }, |
|
207 |
|
208 escapeHTML: function() { |
|
209 var div = document.createElement('div'); |
|
210 var text = document.createTextNode(this); |
|
211 div.appendChild(text); |
|
212 return div.innerHTML; |
|
213 }, |
|
214 |
|
215 unescapeHTML: function() { |
|
216 var div = document.createElement('div'); |
|
217 div.innerHTML = this.stripTags(); |
|
218 return div.childNodes[0] ? div.childNodes[0].nodeValue : ''; |
|
219 }, |
|
220 |
|
221 toQueryParams: function() { |
|
222 var pairs = this.match(/^\??(.*)$/)[1].split('&'); |
|
223 return pairs.inject({}, function(params, pairString) { |
|
224 var pair = pairString.split('='); |
|
225 var value = pair[1] ? decodeURIComponent(pair[1]) : undefined; |
|
226 params[decodeURIComponent(pair[0])] = value; |
|
227 return params; |
|
228 }); |
|
229 }, |
|
230 |
|
231 toArray: function() { |
|
232 return this.split(''); |
|
233 }, |
|
234 |
|
235 camelize: function() { |
|
236 var oStringList = this.split('-'); |
|
237 if (oStringList.length == 1) return oStringList[0]; |
|
238 |
|
239 var camelizedString = this.indexOf('-') == 0 |
|
240 ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) |
|
241 : oStringList[0]; |
|
242 |
|
243 for (var i = 1, len = oStringList.length; i < len; i++) { |
|
244 var s = oStringList[i]; |
|
245 camelizedString += s.charAt(0).toUpperCase() + s.substring(1); |
|
246 } |
|
247 |
|
248 return camelizedString; |
|
249 }, |
|
250 |
|
251 inspect: function(useDoubleQuotes) { |
|
252 var escapedString = this.replace(/\\/g, '\\\\'); |
|
253 if (useDoubleQuotes) |
|
254 return '"' + escapedString.replace(/"/g, '\\"') + '"'; |
|
255 else |
|
256 return "'" + escapedString.replace(/'/g, '\\\'') + "'"; |
|
257 } |
|
258 }); |
|
259 |
|
260 String.prototype.gsub.prepareReplacement = function(replacement) { |
|
261 if (typeof replacement == 'function') return replacement; |
|
262 var template = new Template(replacement); |
|
263 return function(match) { return template.evaluate(match) }; |
|
264 } |
|
265 |
|
266 String.prototype.parseQuery = String.prototype.toQueryParams; |
|
267 |
|
268 var Template = Class.create(); |
|
269 Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; |
|
270 Template.prototype = { |
|
271 initialize: function(template, pattern) { |
|
272 this.template = template.toString(); |
|
273 this.pattern = pattern || Template.Pattern; |
|
274 }, |
|
275 |
|
276 evaluate: function(object) { |
|
277 return this.template.gsub(this.pattern, function(match) { |
|
278 var before = match[1]; |
|
279 if (before == '\\') return match[2]; |
|
280 return before + (object[match[3]] || '').toString(); |
|
281 }); |
|
282 } |
|
283 } |
|
284 |
|
285 var $break = new Object(); |
|
286 var $continue = new Object(); |
|
287 |
|
288 var Enumerable = { |
|
289 each: function(iterator) { |
|
290 var index = 0; |
|
291 try { |
|
292 this._each(function(value) { |
|
293 try { |
|
294 iterator(value, index++); |
|
295 } catch (e) { |
|
296 if (e != $continue) throw e; |
|
297 } |
|
298 }); |
|
299 } catch (e) { |
|
300 if (e != $break) throw e; |
|
301 } |
|
302 }, |
|
303 |
|
304 all: function(iterator) { |
|
305 var result = true; |
|
306 this.each(function(value, index) { |
|
307 result = result && !!(iterator || Prototype.K)(value, index); |
|
308 if (!result) throw $break; |
|
309 }); |
|
310 return result; |
|
311 }, |
|
312 |
|
313 any: function(iterator) { |
|
314 var result = false; |
|
315 this.each(function(value, index) { |
|
316 if (result = !!(iterator || Prototype.K)(value, index)) |
|
317 throw $break; |
|
318 }); |
|
319 return result; |
|
320 }, |
|
321 |
|
322 collect: function(iterator) { |
|
323 var results = []; |
|
324 this.each(function(value, index) { |
|
325 results.push(iterator(value, index)); |
|
326 }); |
|
327 return results; |
|
328 }, |
|
329 |
|
330 detect: function (iterator) { |
|
331 var result; |
|
332 this.each(function(value, index) { |
|
333 if (iterator(value, index)) { |
|
334 result = value; |
|
335 throw $break; |
|
336 } |
|
337 }); |
|
338 return result; |
|
339 }, |
|
340 |
|
341 findAll: function(iterator) { |
|
342 var results = []; |
|
343 this.each(function(value, index) { |
|
344 if (iterator(value, index)) |
|
345 results.push(value); |
|
346 }); |
|
347 return results; |
|
348 }, |
|
349 |
|
350 grep: function(pattern, iterator) { |
|
351 var results = []; |
|
352 this.each(function(value, index) { |
|
353 var stringValue = value.toString(); |
|
354 if (stringValue.match(pattern)) |
|
355 results.push((iterator || Prototype.K)(value, index)); |
|
356 }) |
|
357 return results; |
|
358 }, |
|
359 |
|
360 include: function(object) { |
|
361 var found = false; |
|
362 this.each(function(value) { |
|
363 if (value == object) { |
|
364 found = true; |
|
365 throw $break; |
|
366 } |
|
367 }); |
|
368 return found; |
|
369 }, |
|
370 |
|
371 inject: function(memo, iterator) { |
|
372 this.each(function(value, index) { |
|
373 memo = iterator(memo, value, index); |
|
374 }); |
|
375 return memo; |
|
376 }, |
|
377 |
|
378 invoke: function(method) { |
|
379 var args = $A(arguments).slice(1); |
|
380 return this.collect(function(value) { |
|
381 return value[method].apply(value, args); |
|
382 }); |
|
383 }, |
|
384 |
|
385 max: function(iterator) { |
|
386 var result; |
|
387 this.each(function(value, index) { |
|
388 value = (iterator || Prototype.K)(value, index); |
|
389 if (result == undefined || value >= result) |
|
390 result = value; |
|
391 }); |
|
392 return result; |
|
393 }, |
|
394 |
|
395 min: function(iterator) { |
|
396 var result; |
|
397 this.each(function(value, index) { |
|
398 value = (iterator || Prototype.K)(value, index); |
|
399 if (result == undefined || value < result) |
|
400 result = value; |
|
401 }); |
|
402 return result; |
|
403 }, |
|
404 |
|
405 partition: function(iterator) { |
|
406 var trues = [], falses = []; |
|
407 this.each(function(value, index) { |
|
408 ((iterator || Prototype.K)(value, index) ? |
|
409 trues : falses).push(value); |
|
410 }); |
|
411 return [trues, falses]; |
|
412 }, |
|
413 |
|
414 pluck: function(property) { |
|
415 var results = []; |
|
416 this.each(function(value, index) { |
|
417 results.push(value[property]); |
|
418 }); |
|
419 return results; |
|
420 }, |
|
421 |
|
422 reject: function(iterator) { |
|
423 var results = []; |
|
424 this.each(function(value, index) { |
|
425 if (!iterator(value, index)) |
|
426 results.push(value); |
|
427 }); |
|
428 return results; |
|
429 }, |
|
430 |
|
431 sortBy: function(iterator) { |
|
432 return this.collect(function(value, index) { |
|
433 return {value: value, criteria: iterator(value, index)}; |
|
434 }).sort(function(left, right) { |
|
435 var a = left.criteria, b = right.criteria; |
|
436 return a < b ? -1 : a > b ? 1 : 0; |
|
437 }).pluck('value'); |
|
438 }, |
|
439 |
|
440 toArray: function() { |
|
441 return this.collect(Prototype.K); |
|
442 }, |
|
443 |
|
444 zip: function() { |
|
445 var iterator = Prototype.K, args = $A(arguments); |
|
446 if (typeof args.last() == 'function') |
|
447 iterator = args.pop(); |
|
448 |
|
449 var collections = [this].concat(args).map($A); |
|
450 return this.map(function(value, index) { |
|
451 return iterator(collections.pluck(index)); |
|
452 }); |
|
453 }, |
|
454 |
|
455 inspect: function() { |
|
456 return '#<Enumerable:' + this.toArray().inspect() + '>'; |
|
457 } |
|
458 } |
|
459 |
|
460 Object.extend(Enumerable, { |
|
461 map: Enumerable.collect, |
|
462 find: Enumerable.detect, |
|
463 select: Enumerable.findAll, |
|
464 member: Enumerable.include, |
|
465 entries: Enumerable.toArray |
|
466 }); |
|
467 var $A = Array.from = function(iterable) { |
|
468 if (!iterable) return []; |
|
469 if (iterable.toArray) { |
|
470 return iterable.toArray(); |
|
471 } else { |
|
472 var results = []; |
|
473 for (var i = 0; i < iterable.length; i++) |
|
474 results.push(iterable[i]); |
|
475 return results; |
|
476 } |
|
477 } |
|
478 |
|
479 Object.extend(Array.prototype, Enumerable); |
|
480 |
|
481 if (!Array.prototype._reverse) |
|
482 Array.prototype._reverse = Array.prototype.reverse; |
|
483 |
|
484 Object.extend(Array.prototype, { |
|
485 _each: function(iterator) { |
|
486 for (var i = 0; i < this.length; i++) |
|
487 iterator(this[i]); |
|
488 }, |
|
489 |
|
490 clear: function() { |
|
491 this.length = 0; |
|
492 return this; |
|
493 }, |
|
494 |
|
495 first: function() { |
|
496 return this[0]; |
|
497 }, |
|
498 |
|
499 last: function() { |
|
500 return this[this.length - 1]; |
|
501 }, |
|
502 |
|
503 compact: function() { |
|
504 return this.select(function(value) { |
|
505 return value != undefined || value != null; |
|
506 }); |
|
507 }, |
|
508 |
|
509 flatten: function() { |
|
510 return this.inject([], function(array, value) { |
|
511 return array.concat(value && value.constructor == Array ? |
|
512 value.flatten() : [value]); |
|
513 }); |
|
514 }, |
|
515 |
|
516 without: function() { |
|
517 var values = $A(arguments); |
|
518 return this.select(function(value) { |
|
519 return !values.include(value); |
|
520 }); |
|
521 }, |
|
522 |
|
523 indexOf: function(object) { |
|
524 for (var i = 0; i < this.length; i++) |
|
525 if (this[i] == object) return i; |
|
526 return -1; |
|
527 }, |
|
528 |
|
529 reverse: function(inline) { |
|
530 return (inline !== false ? this : this.toArray())._reverse(); |
|
531 }, |
|
532 |
|
533 reduce: function() { |
|
534 return this.length > 1 ? this : this[0]; |
|
535 }, |
|
536 |
|
537 uniq: function() { |
|
538 return this.inject([], function(array, value) { |
|
539 return array.include(value) ? array : array.concat([value]); |
|
540 }); |
|
541 }, |
|
542 |
|
543 inspect: function() { |
|
544 return '[' + this.map(Object.inspect).join(', ') + ']'; |
|
545 } |
|
546 }); |
|
547 var Hash = { |
|
548 _each: function(iterator) { |
|
549 for (var key in this) { |
|
550 var value = this[key]; |
|
551 if (typeof value == 'function') continue; |
|
552 |
|
553 var pair = [key, value]; |
|
554 pair.key = key; |
|
555 pair.value = value; |
|
556 iterator(pair); |
|
557 } |
|
558 }, |
|
559 |
|
560 keys: function() { |
|
561 return this.pluck('key'); |
|
562 }, |
|
563 |
|
564 values: function() { |
|
565 return this.pluck('value'); |
|
566 }, |
|
567 |
|
568 merge: function(hash) { |
|
569 return $H(hash).inject($H(this), function(mergedHash, pair) { |
|
570 mergedHash[pair.key] = pair.value; |
|
571 return mergedHash; |
|
572 }); |
|
573 }, |
|
574 |
|
575 toQueryString: function() { |
|
576 return this.map(function(pair) { |
|
577 return pair.map(encodeURIComponent).join('='); |
|
578 }).join('&'); |
|
579 }, |
|
580 |
|
581 inspect: function() { |
|
582 return '#<Hash:{' + this.map(function(pair) { |
|
583 return pair.map(Object.inspect).join(': '); |
|
584 }).join(', ') + '}>'; |
|
585 } |
|
586 } |
|
587 |
|
588 function $H(object) { |
|
589 var hash = Object.extend({}, object || {}); |
|
590 Object.extend(hash, Enumerable); |
|
591 Object.extend(hash, Hash); |
|
592 return hash; |
|
593 } |
|
594 ObjectRange = Class.create(); |
|
595 Object.extend(ObjectRange.prototype, Enumerable); |
|
596 Object.extend(ObjectRange.prototype, { |
|
597 initialize: function(start, end, exclusive) { |
|
598 this.start = start; |
|
599 this.end = end; |
|
600 this.exclusive = exclusive; |
|
601 }, |
|
602 |
|
603 _each: function(iterator) { |
|
604 var value = this.start; |
|
605 while (this.include(value)) { |
|
606 iterator(value); |
|
607 value = value.succ(); |
|
608 } |
|
609 }, |
|
610 |
|
611 include: function(value) { |
|
612 if (value < this.start) |
|
613 return false; |
|
614 if (this.exclusive) |
|
615 return value < this.end; |
|
616 return value <= this.end; |
|
617 } |
|
618 }); |
|
619 |
|
620 var $R = function(start, end, exclusive) { |
|
621 return new ObjectRange(start, end, exclusive); |
|
622 } |
|
623 |
|
624 var Ajax = { |
|
625 getTransport: function() { |
|
626 return Try.these( |
|
627 function() {return new XMLHttpRequest()}, |
|
628 function() {return new ActiveXObject('Msxml2.XMLHTTP')}, |
|
629 function() {return new ActiveXObject('Microsoft.XMLHTTP')} |
|
630 ) || false; |
|
631 }, |
|
632 |
|
633 activeRequestCount: 0 |
|
634 } |
|
635 |
|
636 Ajax.Responders = { |
|
637 responders: [], |
|
638 |
|
639 _each: function(iterator) { |
|
640 this.responders._each(iterator); |
|
641 }, |
|
642 |
|
643 register: function(responderToAdd) { |
|
644 if (!this.include(responderToAdd)) |
|
645 this.responders.push(responderToAdd); |
|
646 }, |
|
647 |
|
648 unregister: function(responderToRemove) { |
|
649 this.responders = this.responders.without(responderToRemove); |
|
650 }, |
|
651 |
|
652 dispatch: function(callback, request, transport, json) { |
|
653 this.each(function(responder) { |
|
654 if (responder[callback] && typeof responder[callback] == 'function') { |
|
655 try { |
|
656 responder[callback].apply(responder, [request, transport, json]); |
|
657 } catch (e) {} |
|
658 } |
|
659 }); |
|
660 } |
|
661 }; |
|
662 |
|
663 Object.extend(Ajax.Responders, Enumerable); |
|
664 |
|
665 Ajax.Responders.register({ |
|
666 onCreate: function() { |
|
667 Ajax.activeRequestCount++; |
|
668 }, |
|
669 |
|
670 onComplete: function() { |
|
671 Ajax.activeRequestCount--; |
|
672 } |
|
673 }); |
|
674 |
|
675 Ajax.Base = function() {}; |
|
676 Ajax.Base.prototype = { |
|
677 setOptions: function(options) { |
|
678 this.options = { |
|
679 method: 'post', |
|
680 asynchronous: true, |
|
681 contentType: 'application/x-www-form-urlencoded', |
|
682 parameters: '' |
|
683 } |
|
684 Object.extend(this.options, options || {}); |
|
685 }, |
|
686 |
|
687 responseIsSuccess: function() { |
|
688 return this.transport.status == undefined |
|
689 || this.transport.status == 0 |
|
690 || (this.transport.status >= 200 && this.transport.status < 300); |
|
691 }, |
|
692 |
|
693 responseIsFailure: function() { |
|
694 return !this.responseIsSuccess(); |
|
695 } |
|
696 } |
|
697 |
|
698 Ajax.Request = Class.create(); |
|
699 Ajax.Request.Events = |
|
700 ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; |
|
701 |
|
702 Ajax.Request.prototype = Object.extend(new Ajax.Base(), { |
|
703 initialize: function(url, options) { |
|
704 this.transport = Ajax.getTransport(); |
|
705 this.setOptions(options); |
|
706 this.request(url); |
|
707 }, |
|
708 |
|
709 request: function(url) { |
|
710 var parameters = this.options.parameters || ''; |
|
711 if (parameters.length > 0) parameters += '&_='; |
|
712 |
|
713 /* Simulate other verbs over post */ |
|
714 if (this.options.method != 'get' && this.options.method != 'post') { |
|
715 parameters += (parameters.length > 0 ? '&' : '') + '_method=' + this.options.method; |
|
716 this.options.method = 'post'; |
|
717 } |
|
718 |
|
719 try { |
|
720 this.url = url; |
|
721 if (this.options.method == 'get' && parameters.length > 0) |
|
722 this.url += (this.url.match(/\?/) ? '&' : '?') + parameters; |
|
723 |
|
724 Ajax.Responders.dispatch('onCreate', this, this.transport); |
|
725 |
|
726 this.transport.open(this.options.method, this.url, |
|
727 this.options.asynchronous); |
|
728 |
|
729 if (this.options.asynchronous) |
|
730 setTimeout(function() { this.respondToReadyState(1) }.bind(this), 10); |
|
731 |
|
732 this.transport.onreadystatechange = this.onStateChange.bind(this); |
|
733 this.setRequestHeaders(); |
|
734 |
|
735 var body = this.options.postBody ? this.options.postBody : parameters; |
|
736 this.transport.send(this.options.method == 'post' ? body : null); |
|
737 |
|
738 /* Force Firefox to handle ready state 4 for synchronous requests */ |
|
739 if (!this.options.asynchronous && this.transport.overrideMimeType) |
|
740 this.onStateChange(); |
|
741 |
|
742 } catch (e) { |
|
743 this.dispatchException(e); |
|
744 } |
|
745 }, |
|
746 |
|
747 setRequestHeaders: function() { |
|
748 var requestHeaders = |
|
749 ['X-Requested-With', 'XMLHttpRequest', |
|
750 'X-Prototype-Version', Prototype.Version, |
|
751 'Accept', 'text/javascript, text/html, application/xml, text/xml, */*']; |
|
752 |
|
753 if (this.options.method == 'post') { |
|
754 requestHeaders.push('Content-type', this.options.contentType); |
|
755 |
|
756 /* Force "Connection: close" for Mozilla browsers to work around |
|
757 * a bug where XMLHttpReqeuest sends an incorrect Content-length |
|
758 * header. See Mozilla Bugzilla #246651. |
|
759 */ |
|
760 if (this.transport.overrideMimeType) |
|
761 requestHeaders.push('Connection', 'close'); |
|
762 } |
|
763 |
|
764 if (this.options.requestHeaders) |
|
765 requestHeaders.push.apply(requestHeaders, this.options.requestHeaders); |
|
766 |
|
767 for (var i = 0; i < requestHeaders.length; i += 2) |
|
768 this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]); |
|
769 }, |
|
770 |
|
771 onStateChange: function() { |
|
772 var readyState = this.transport.readyState; |
|
773 if (readyState != 1) |
|
774 this.respondToReadyState(this.transport.readyState); |
|
775 }, |
|
776 |
|
777 header: function(name) { |
|
778 try { |
|
779 return this.transport.getResponseHeader(name); |
|
780 } catch (e) {} |
|
781 }, |
|
782 |
|
783 evalJSON: function() { |
|
784 try { |
|
785 return eval('(' + this.header('X-JSON') + ')'); |
|
786 } catch (e) {} |
|
787 }, |
|
788 |
|
789 evalResponse: function() { |
|
790 try { |
|
791 return eval(this.transport.responseText); |
|
792 } catch (e) { |
|
793 this.dispatchException(e); |
|
794 } |
|
795 }, |
|
796 |
|
797 respondToReadyState: function(readyState) { |
|
798 var event = Ajax.Request.Events[readyState]; |
|
799 var transport = this.transport, json = this.evalJSON(); |
|
800 |
|
801 if (event == 'Complete') { |
|
802 try { |
|
803 (this.options['on' + this.transport.status] |
|
804 || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')] |
|
805 || Prototype.emptyFunction)(transport, json); |
|
806 } catch (e) { |
|
807 this.dispatchException(e); |
|
808 } |
|
809 |
|
810 if ((this.header('Content-type') || '').match(/^text\/javascript/i)) |
|
811 this.evalResponse(); |
|
812 } |
|
813 |
|
814 try { |
|
815 (this.options['on' + event] || Prototype.emptyFunction)(transport, json); |
|
816 Ajax.Responders.dispatch('on' + event, this, transport, json); |
|
817 } catch (e) { |
|
818 this.dispatchException(e); |
|
819 } |
|
820 |
|
821 /* Avoid memory leak in MSIE: clean up the oncomplete event handler */ |
|
822 if (event == 'Complete') |
|
823 this.transport.onreadystatechange = Prototype.emptyFunction; |
|
824 }, |
|
825 |
|
826 dispatchException: function(exception) { |
|
827 (this.options.onException || Prototype.emptyFunction)(this, exception); |
|
828 Ajax.Responders.dispatch('onException', this, exception); |
|
829 } |
|
830 }); |
|
831 |
|
832 Ajax.Updater = Class.create(); |
|
833 |
|
834 Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), { |
|
835 initialize: function(container, url, options) { |
|
836 this.containers = { |
|
837 success: container.success ? $(container.success) : $(container), |
|
838 failure: container.failure ? $(container.failure) : |
|
839 (container.success ? null : $(container)) |
|
840 } |
|
841 |
|
842 this.transport = Ajax.getTransport(); |
|
843 this.setOptions(options); |
|
844 |
|
845 var onComplete = this.options.onComplete || Prototype.emptyFunction; |
|
846 this.options.onComplete = (function(transport, object) { |
|
847 this.updateContent(); |
|
848 onComplete(transport, object); |
|
849 }).bind(this); |
|
850 |
|
851 this.request(url); |
|
852 }, |
|
853 |
|
854 updateContent: function() { |
|
855 var receiver = this.responseIsSuccess() ? |
|
856 this.containers.success : this.containers.failure; |
|
857 var response = this.transport.responseText; |
|
858 |
|
859 if (!this.options.evalScripts) |
|
860 response = response.stripScripts(); |
|
861 |
|
862 if (receiver) { |
|
863 if (this.options.insertion) { |
|
864 new this.options.insertion(receiver, response); |
|
865 } else { |
|
866 Element.update(receiver, response); |
|
867 } |
|
868 } |
|
869 |
|
870 if (this.responseIsSuccess()) { |
|
871 if (this.onComplete) |
|
872 setTimeout(this.onComplete.bind(this), 10); |
|
873 } |
|
874 } |
|
875 }); |
|
876 |
|
877 Ajax.PeriodicalUpdater = Class.create(); |
|
878 Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), { |
|
879 initialize: function(container, url, options) { |
|
880 this.setOptions(options); |
|
881 this.onComplete = this.options.onComplete; |
|
882 |
|
883 this.frequency = (this.options.frequency || 2); |
|
884 this.decay = (this.options.decay || 1); |
|
885 |
|
886 this.updater = {}; |
|
887 this.container = container; |
|
888 this.url = url; |
|
889 |
|
890 this.start(); |
|
891 }, |
|
892 |
|
893 start: function() { |
|
894 this.options.onComplete = this.updateComplete.bind(this); |
|
895 this.onTimerEvent(); |
|
896 }, |
|
897 |
|
898 stop: function() { |
|
899 this.updater.options.onComplete = undefined; |
|
900 clearTimeout(this.timer); |
|
901 (this.onComplete || Prototype.emptyFunction).apply(this, arguments); |
|
902 }, |
|
903 |
|
904 updateComplete: function(request) { |
|
905 if (this.options.decay) { |
|
906 this.decay = (request.responseText == this.lastText ? |
|
907 this.decay * this.options.decay : 1); |
|
908 |
|
909 this.lastText = request.responseText; |
|
910 } |
|
911 this.timer = setTimeout(this.onTimerEvent.bind(this), |
|
912 this.decay * this.frequency * 1000); |
|
913 }, |
|
914 |
|
915 onTimerEvent: function() { |
|
916 this.updater = new Ajax.Updater(this.container, this.url, this.options); |
|
917 } |
|
918 }); |
|
919 function $() { |
|
920 var results = [], element; |
|
921 for (var i = 0; i < arguments.length; i++) { |
|
922 element = arguments[i]; |
|
923 if (typeof element == 'string') |
|
924 element = document.getElementById(element); |
|
925 results.push(Element.extend(element)); |
|
926 } |
|
927 return results.reduce(); |
|
928 } |
|
929 |
|
930 document.getElementsByClassName = function(className, parentElement) { |
|
931 var children = ($(parentElement) || document.body).getElementsByTagName('*'); |
|
932 return $A(children).inject([], function(elements, child) { |
|
933 if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) |
|
934 elements.push(Element.extend(child)); |
|
935 return elements; |
|
936 }); |
|
937 } |
|
938 |
|
939 /*--------------------------------------------------------------------------*/ |
|
940 |
|
941 if (!window.Element) |
|
942 var Element = new Object(); |
|
943 |
|
944 Element.extend = function(element) { |
|
945 if (!element) return; |
|
946 if (_nativeExtensions || element.nodeType == 3) return element; |
|
947 |
|
948 if (!element._extended && element.tagName && element != window) { |
|
949 var methods = Object.clone(Element.Methods), cache = Element.extend.cache; |
|
950 |
|
951 if (element.tagName == 'FORM') |
|
952 Object.extend(methods, Form.Methods); |
|
953 if (['INPUT', 'TEXTAREA', 'SELECT'].include(element.tagName)) |
|
954 Object.extend(methods, Form.Element.Methods); |
|
955 |
|
956 for (var property in methods) { |
|
957 var value = methods[property]; |
|
958 if (typeof value == 'function') |
|
959 element[property] = cache.findOrStore(value); |
|
960 } |
|
961 } |
|
962 |
|
963 element._extended = true; |
|
964 return element; |
|
965 } |
|
966 |
|
967 Element.extend.cache = { |
|
968 findOrStore: function(value) { |
|
969 return this[value] = this[value] || function() { |
|
970 return value.apply(null, [this].concat($A(arguments))); |
|
971 } |
|
972 } |
|
973 } |
|
974 |
|
975 Element.Methods = { |
|
976 visible: function(element) { |
|
977 return $(element).style.display != 'none'; |
|
978 }, |
|
979 |
|
980 toggle: function(element) { |
|
981 element = $(element); |
|
982 Element[Element.visible(element) ? 'hide' : 'show'](element); |
|
983 return element; |
|
984 }, |
|
985 |
|
986 hide: function(element) { |
|
987 $(element).style.display = 'none'; |
|
988 return element; |
|
989 }, |
|
990 |
|
991 show: function(element) { |
|
992 $(element).style.display = ''; |
|
993 return element; |
|
994 }, |
|
995 |
|
996 remove: function(element) { |
|
997 element = $(element); |
|
998 element.parentNode.removeChild(element); |
|
999 return element; |
|
1000 }, |
|
1001 |
|
1002 update: function(element, html) { |
|
1003 $(element).innerHTML = html.stripScripts(); |
|
1004 setTimeout(function() {html.evalScripts()}, 10); |
|
1005 return element; |
|
1006 }, |
|
1007 |
|
1008 replace: function(element, html) { |
|
1009 element = $(element); |
|
1010 if (element.outerHTML) { |
|
1011 element.outerHTML = html.stripScripts(); |
|
1012 } else { |
|
1013 var range = element.ownerDocument.createRange(); |
|
1014 range.selectNodeContents(element); |
|
1015 element.parentNode.replaceChild( |
|
1016 range.createContextualFragment(html.stripScripts()), element); |
|
1017 } |
|
1018 setTimeout(function() {html.evalScripts()}, 10); |
|
1019 return element; |
|
1020 }, |
|
1021 |
|
1022 inspect: function(element) { |
|
1023 element = $(element); |
|
1024 var result = '<' + element.tagName.toLowerCase(); |
|
1025 $H({'id': 'id', 'className': 'class'}).each(function(pair) { |
|
1026 var property = pair.first(), attribute = pair.last(); |
|
1027 var value = (element[property] || '').toString(); |
|
1028 if (value) result += ' ' + attribute + '=' + value.inspect(true); |
|
1029 }); |
|
1030 return result + '>'; |
|
1031 }, |
|
1032 |
|
1033 recursivelyCollect: function(element, property) { |
|
1034 element = $(element); |
|
1035 var elements = []; |
|
1036 while (element = element[property]) |
|
1037 if (element.nodeType == 1) |
|
1038 elements.push(Element.extend(element)); |
|
1039 return elements; |
|
1040 }, |
|
1041 |
|
1042 ancestors: function(element) { |
|
1043 return $(element).recursivelyCollect('parentNode'); |
|
1044 }, |
|
1045 |
|
1046 descendants: function(element) { |
|
1047 element = $(element); |
|
1048 return $A(element.getElementsByTagName('*')); |
|
1049 }, |
|
1050 |
|
1051 previousSiblings: function(element) { |
|
1052 return $(element).recursivelyCollect('previousSibling'); |
|
1053 }, |
|
1054 |
|
1055 nextSiblings: function(element) { |
|
1056 return $(element).recursivelyCollect('nextSibling'); |
|
1057 }, |
|
1058 |
|
1059 siblings: function(element) { |
|
1060 element = $(element); |
|
1061 return element.previousSiblings().reverse().concat(element.nextSiblings()); |
|
1062 }, |
|
1063 |
|
1064 match: function(element, selector) { |
|
1065 element = $(element); |
|
1066 if (typeof selector == 'string') |
|
1067 selector = new Selector(selector); |
|
1068 return selector.match(element); |
|
1069 }, |
|
1070 |
|
1071 up: function(element, expression, index) { |
|
1072 return Selector.findElement($(element).ancestors(), expression, index); |
|
1073 }, |
|
1074 |
|
1075 down: function(element, expression, index) { |
|
1076 return Selector.findElement($(element).descendants(), expression, index); |
|
1077 }, |
|
1078 |
|
1079 previous: function(element, expression, index) { |
|
1080 return Selector.findElement($(element).previousSiblings(), expression, index); |
|
1081 }, |
|
1082 |
|
1083 next: function(element, expression, index) { |
|
1084 return Selector.findElement($(element).nextSiblings(), expression, index); |
|
1085 }, |
|
1086 |
|
1087 getElementsBySelector: function() { |
|
1088 var args = $A(arguments), element = $(args.shift()); |
|
1089 return Selector.findChildElements(element, args); |
|
1090 }, |
|
1091 |
|
1092 getElementsByClassName: function(element, className) { |
|
1093 element = $(element); |
|
1094 return document.getElementsByClassName(className, element); |
|
1095 }, |
|
1096 |
|
1097 getHeight: function(element) { |
|
1098 element = $(element); |
|
1099 return element.offsetHeight; |
|
1100 }, |
|
1101 |
|
1102 classNames: function(element) { |
|
1103 return new Element.ClassNames(element); |
|
1104 }, |
|
1105 |
|
1106 hasClassName: function(element, className) { |
|
1107 if (!(element = $(element))) return; |
|
1108 return Element.classNames(element).include(className); |
|
1109 }, |
|
1110 |
|
1111 addClassName: function(element, className) { |
|
1112 if (!(element = $(element))) return; |
|
1113 Element.classNames(element).add(className); |
|
1114 return element; |
|
1115 }, |
|
1116 |
|
1117 removeClassName: function(element, className) { |
|
1118 if (!(element = $(element))) return; |
|
1119 Element.classNames(element).remove(className); |
|
1120 return element; |
|
1121 }, |
|
1122 |
|
1123 observe: function() { |
|
1124 Event.observe.apply(Event, arguments); |
|
1125 return $A(arguments).first(); |
|
1126 }, |
|
1127 |
|
1128 stopObserving: function() { |
|
1129 Event.stopObserving.apply(Event, arguments); |
|
1130 return $A(arguments).first(); |
|
1131 }, |
|
1132 |
|
1133 // removes whitespace-only text node children |
|
1134 cleanWhitespace: function(element) { |
|
1135 element = $(element); |
|
1136 var node = element.firstChild; |
|
1137 while (node) { |
|
1138 var nextNode = node.nextSibling; |
|
1139 if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) |
|
1140 element.removeChild(node); |
|
1141 node = nextNode; |
|
1142 } |
|
1143 return element; |
|
1144 }, |
|
1145 |
|
1146 empty: function(element) { |
|
1147 return $(element).innerHTML.match(/^\s*$/); |
|
1148 }, |
|
1149 |
|
1150 childOf: function(element, ancestor) { |
|
1151 element = $(element), ancestor = $(ancestor); |
|
1152 while (element = element.parentNode) |
|
1153 if (element == ancestor) return true; |
|
1154 return false; |
|
1155 }, |
|
1156 |
|
1157 scrollTo: function(element) { |
|
1158 element = $(element); |
|
1159 var x = element.x ? element.x : element.offsetLeft, |
|
1160 y = element.y ? element.y : element.offsetTop; |
|
1161 window.scrollTo(x, y); |
|
1162 return element; |
|
1163 }, |
|
1164 |
|
1165 getStyle: function(element, style) { |
|
1166 element = $(element); |
|
1167 var value = element.style[style.camelize()]; |
|
1168 if (!value) { |
|
1169 if (document.defaultView && document.defaultView.getComputedStyle) { |
|
1170 var css = document.defaultView.getComputedStyle(element, null); |
|
1171 value = css ? css.getPropertyValue(style) : null; |
|
1172 } else if (element.currentStyle) { |
|
1173 value = element.currentStyle[style.camelize()]; |
|
1174 } |
|
1175 } |
|
1176 |
|
1177 if (window.opera && ['left', 'top', 'right', 'bottom'].include(style)) |
|
1178 if (Element.getStyle(element, 'position') == 'static') value = 'auto'; |
|
1179 |
|
1180 return value == 'auto' ? null : value; |
|
1181 }, |
|
1182 |
|
1183 setStyle: function(element, style) { |
|
1184 element = $(element); |
|
1185 for (var name in style) |
|
1186 element.style[name.camelize()] = style[name]; |
|
1187 return element; |
|
1188 }, |
|
1189 |
|
1190 getDimensions: function(element) { |
|
1191 element = $(element); |
|
1192 if (Element.getStyle(element, 'display') != 'none') |
|
1193 return {width: element.offsetWidth, height: element.offsetHeight}; |
|
1194 |
|
1195 // All *Width and *Height properties give 0 on elements with display none, |
|
1196 // so enable the element temporarily |
|
1197 var els = element.style; |
|
1198 var originalVisibility = els.visibility; |
|
1199 var originalPosition = els.position; |
|
1200 els.visibility = 'hidden'; |
|
1201 els.position = 'absolute'; |
|
1202 els.display = ''; |
|
1203 var originalWidth = element.clientWidth; |
|
1204 var originalHeight = element.clientHeight; |
|
1205 els.display = 'none'; |
|
1206 els.position = originalPosition; |
|
1207 els.visibility = originalVisibility; |
|
1208 return {width: originalWidth, height: originalHeight}; |
|
1209 }, |
|
1210 |
|
1211 makePositioned: function(element) { |
|
1212 element = $(element); |
|
1213 var pos = Element.getStyle(element, 'position'); |
|
1214 if (pos == 'static' || !pos) { |
|
1215 element._madePositioned = true; |
|
1216 element.style.position = 'relative'; |
|
1217 // Opera returns the offset relative to the positioning context, when an |
|
1218 // element is position relative but top and left have not been defined |
|
1219 if (window.opera) { |
|
1220 element.style.top = 0; |
|
1221 element.style.left = 0; |
|
1222 } |
|
1223 } |
|
1224 return element; |
|
1225 }, |
|
1226 |
|
1227 undoPositioned: function(element) { |
|
1228 element = $(element); |
|
1229 if (element._madePositioned) { |
|
1230 element._madePositioned = undefined; |
|
1231 element.style.position = |
|
1232 element.style.top = |
|
1233 element.style.left = |
|
1234 element.style.bottom = |
|
1235 element.style.right = ''; |
|
1236 } |
|
1237 return element; |
|
1238 }, |
|
1239 |
|
1240 makeClipping: function(element) { |
|
1241 element = $(element); |
|
1242 if (element._overflow) return; |
|
1243 element._overflow = element.style.overflow || 'auto'; |
|
1244 if ((Element.getStyle(element, 'overflow') || 'visible') != 'hidden') |
|
1245 element.style.overflow = 'hidden'; |
|
1246 return element; |
|
1247 }, |
|
1248 |
|
1249 undoClipping: function(element) { |
|
1250 element = $(element); |
|
1251 if (!element._overflow) return; |
|
1252 element.style.overflow = element._overflow == 'auto' ? '' : element._overflow; |
|
1253 element._overflow = null; |
|
1254 return element; |
|
1255 } |
|
1256 } |
|
1257 |
|
1258 // IE is missing .innerHTML support for TABLE-related elements |
|
1259 if(document.all){ |
|
1260 Element.Methods.update = function(element, html) { |
|
1261 element = $(element); |
|
1262 var tagName = element.tagName.toUpperCase(); |
|
1263 if (['THEAD','TBODY','TR','TD'].indexOf(tagName) > -1) { |
|
1264 var div = document.createElement('div'); |
|
1265 switch (tagName) { |
|
1266 case 'THEAD': |
|
1267 case 'TBODY': |
|
1268 div.innerHTML = '<table><tbody>' + html.stripScripts() + '</tbody></table>'; |
|
1269 depth = 2; |
|
1270 break; |
|
1271 case 'TR': |
|
1272 div.innerHTML = '<table><tbody><tr>' + html.stripScripts() + '</tr></tbody></table>'; |
|
1273 depth = 3; |
|
1274 break; |
|
1275 case 'TD': |
|
1276 div.innerHTML = '<table><tbody><tr><td>' + html.stripScripts() + '</td></tr></tbody></table>'; |
|
1277 depth = 4; |
|
1278 } |
|
1279 $A(element.childNodes).each(function(node){ |
|
1280 element.removeChild(node) |
|
1281 }); |
|
1282 depth.times(function(){ div = div.firstChild }); |
|
1283 |
|
1284 $A(div.childNodes).each( |
|
1285 function(node){ element.appendChild(node) }); |
|
1286 } else { |
|
1287 element.innerHTML = html.stripScripts(); |
|
1288 } |
|
1289 setTimeout(function() {html.evalScripts()}, 10); |
|
1290 return element; |
|
1291 } |
|
1292 } |
|
1293 |
|
1294 Object.extend(Element, Element.Methods); |
|
1295 |
|
1296 var _nativeExtensions = false; |
|
1297 |
|
1298 if (!window.HTMLElement && /Konqueror|Safari|KHTML/.test(navigator.userAgent)) { |
|
1299 /* Emulate HTMLElement, HTMLFormElement, HTMLInputElement, HTMLTextAreaElement, |
|
1300 and HTMLSelectElement in Safari */ |
|
1301 ['', 'Form', 'Input', 'TextArea', 'Select'].each(function(tag) { |
|
1302 var klass = window['HTML' + tag + 'Element'] = {}; |
|
1303 klass.prototype = document.createElement(tag ? tag.toLowerCase() : 'div').__proto__; |
|
1304 }); |
|
1305 } |
|
1306 |
|
1307 Element.addMethods = function(methods) { |
|
1308 Object.extend(Element.Methods, methods || {}); |
|
1309 |
|
1310 function copy(methods, destination) { |
|
1311 var cache = Element.extend.cache; |
|
1312 for (var property in methods) { |
|
1313 var value = methods[property]; |
|
1314 destination[property] = cache.findOrStore(value); |
|
1315 } |
|
1316 } |
|
1317 |
|
1318 if (typeof HTMLElement != 'undefined') { |
|
1319 copy(Element.Methods, HTMLElement.prototype); |
|
1320 copy(Form.Methods, HTMLFormElement.prototype); |
|
1321 [HTMLInputElement, HTMLTextAreaElement, HTMLSelectElement].each(function(klass) { |
|
1322 copy(Form.Element.Methods, klass.prototype); |
|
1323 }); |
|
1324 _nativeExtensions = true; |
|
1325 } |
|
1326 } |
|
1327 |
|
1328 var Toggle = new Object(); |
|
1329 Toggle.display = Element.toggle; |
|
1330 |
|
1331 /*--------------------------------------------------------------------------*/ |
|
1332 |
|
1333 Abstract.Insertion = function(adjacency) { |
|
1334 this.adjacency = adjacency; |
|
1335 } |
|
1336 |
|
1337 Abstract.Insertion.prototype = { |
|
1338 initialize: function(element, content) { |
|
1339 this.element = $(element); |
|
1340 this.content = content.stripScripts(); |
|
1341 |
|
1342 if (this.adjacency && this.element.insertAdjacentHTML) { |
|
1343 try { |
|
1344 this.element.insertAdjacentHTML(this.adjacency, this.content); |
|
1345 } catch (e) { |
|
1346 var tagName = this.element.tagName.toLowerCase(); |
|
1347 if (tagName == 'tbody' || tagName == 'tr') { |
|
1348 this.insertContent(this.contentFromAnonymousTable()); |
|
1349 } else { |
|
1350 throw e; |
|
1351 } |
|
1352 } |
|
1353 } else { |
|
1354 this.range = this.element.ownerDocument.createRange(); |
|
1355 if (this.initializeRange) this.initializeRange(); |
|
1356 this.insertContent([this.range.createContextualFragment(this.content)]); |
|
1357 } |
|
1358 |
|
1359 setTimeout(function() {content.evalScripts()}, 10); |
|
1360 }, |
|
1361 |
|
1362 contentFromAnonymousTable: function() { |
|
1363 var div = document.createElement('div'); |
|
1364 div.innerHTML = '<table><tbody>' + this.content + '</tbody></table>'; |
|
1365 return $A(div.childNodes[0].childNodes[0].childNodes); |
|
1366 } |
|
1367 } |
|
1368 |
|
1369 var Insertion = new Object(); |
|
1370 |
|
1371 Insertion.Before = Class.create(); |
|
1372 Insertion.Before.prototype = Object.extend(new Abstract.Insertion('beforeBegin'), { |
|
1373 initializeRange: function() { |
|
1374 this.range.setStartBefore(this.element); |
|
1375 }, |
|
1376 |
|
1377 insertContent: function(fragments) { |
|
1378 fragments.each((function(fragment) { |
|
1379 this.element.parentNode.insertBefore(fragment, this.element); |
|
1380 }).bind(this)); |
|
1381 } |
|
1382 }); |
|
1383 |
|
1384 Insertion.Top = Class.create(); |
|
1385 Insertion.Top.prototype = Object.extend(new Abstract.Insertion('afterBegin'), { |
|
1386 initializeRange: function() { |
|
1387 this.range.selectNodeContents(this.element); |
|
1388 this.range.collapse(true); |
|
1389 }, |
|
1390 |
|
1391 insertContent: function(fragments) { |
|
1392 fragments.reverse(false).each((function(fragment) { |
|
1393 this.element.insertBefore(fragment, this.element.firstChild); |
|
1394 }).bind(this)); |
|
1395 } |
|
1396 }); |
|
1397 |
|
1398 Insertion.Bottom = Class.create(); |
|
1399 Insertion.Bottom.prototype = Object.extend(new Abstract.Insertion('beforeEnd'), { |
|
1400 initializeRange: function() { |
|
1401 this.range.selectNodeContents(this.element); |
|
1402 this.range.collapse(this.element); |
|
1403 }, |
|
1404 |
|
1405 insertContent: function(fragments) { |
|
1406 fragments.each((function(fragment) { |
|
1407 this.element.appendChild(fragment); |
|
1408 }).bind(this)); |
|
1409 } |
|
1410 }); |
|
1411 |
|
1412 Insertion.After = Class.create(); |
|
1413 Insertion.After.prototype = Object.extend(new Abstract.Insertion('afterEnd'), { |
|
1414 initializeRange: function() { |
|
1415 this.range.setStartAfter(this.element); |
|
1416 }, |
|
1417 |
|
1418 insertContent: function(fragments) { |
|
1419 fragments.each((function(fragment) { |
|
1420 this.element.parentNode.insertBefore(fragment, |
|
1421 this.element.nextSibling); |
|
1422 }).bind(this)); |
|
1423 } |
|
1424 }); |
|
1425 |
|
1426 /*--------------------------------------------------------------------------*/ |
|
1427 |
|
1428 Element.ClassNames = Class.create(); |
|
1429 Element.ClassNames.prototype = { |
|
1430 initialize: function(element) { |
|
1431 this.element = $(element); |
|
1432 }, |
|
1433 |
|
1434 _each: function(iterator) { |
|
1435 this.element.className.split(/\s+/).select(function(name) { |
|
1436 return name.length > 0; |
|
1437 })._each(iterator); |
|
1438 }, |
|
1439 |
|
1440 set: function(className) { |
|
1441 this.element.className = className; |
|
1442 }, |
|
1443 |
|
1444 add: function(classNameToAdd) { |
|
1445 if (this.include(classNameToAdd)) return; |
|
1446 this.set(this.toArray().concat(classNameToAdd).join(' ')); |
|
1447 }, |
|
1448 |
|
1449 remove: function(classNameToRemove) { |
|
1450 if (!this.include(classNameToRemove)) return; |
|
1451 this.set(this.select(function(className) { |
|
1452 return className != classNameToRemove; |
|
1453 }).join(' ')); |
|
1454 }, |
|
1455 |
|
1456 toString: function() { |
|
1457 return this.toArray().join(' '); |
|
1458 } |
|
1459 } |
|
1460 |
|
1461 Object.extend(Element.ClassNames.prototype, Enumerable); |
|
1462 var Selector = Class.create(); |
|
1463 Selector.prototype = { |
|
1464 initialize: function(expression) { |
|
1465 this.params = {classNames: []}; |
|
1466 this.expression = expression.toString().strip(); |
|
1467 this.parseExpression(); |
|
1468 this.compileMatcher(); |
|
1469 }, |
|
1470 |
|
1471 parseExpression: function() { |
|
1472 function abort(message) { throw 'Parse error in selector: ' + message; } |
|
1473 |
|
1474 if (this.expression == '') abort('empty expression'); |
|
1475 |
|
1476 var params = this.params, expr = this.expression, match, modifier, clause, rest; |
|
1477 while (match = expr.match(/^(.*)\[([a-z0-9_:-]+?)(?:([~\|!]?=)(?:"([^"]*)"|([^\]\s]*)))?\]$/i)) { |
|
1478 params.attributes = params.attributes || []; |
|
1479 params.attributes.push({name: match[2], operator: match[3], value: match[4] || match[5] || ''}); |
|
1480 expr = match[1]; |
|
1481 } |
|
1482 |
|
1483 if (expr == '*') return this.params.wildcard = true; |
|
1484 |
|
1485 while (match = expr.match(/^([^a-z0-9_-])?([a-z0-9_-]+)(.*)/i)) { |
|
1486 modifier = match[1], clause = match[2], rest = match[3]; |
|
1487 switch (modifier) { |
|
1488 case '#': params.id = clause; break; |
|
1489 case '.': params.classNames.push(clause); break; |
|
1490 case '': |
|
1491 case undefined: params.tagName = clause.toUpperCase(); break; |
|
1492 default: abort(expr.inspect()); |
|
1493 } |
|
1494 expr = rest; |
|
1495 } |
|
1496 |
|
1497 if (expr.length > 0) abort(expr.inspect()); |
|
1498 }, |
|
1499 |
|
1500 buildMatchExpression: function() { |
|
1501 var params = this.params, conditions = [], clause; |
|
1502 |
|
1503 if (params.wildcard) |
|
1504 conditions.push('true'); |
|
1505 if (clause = params.id) |
|
1506 conditions.push('element.id == ' + clause.inspect()); |
|
1507 if (clause = params.tagName) |
|
1508 conditions.push('element.tagName.toUpperCase() == ' + clause.inspect()); |
|
1509 if ((clause = params.classNames).length > 0) |
|
1510 for (var i = 0; i < clause.length; i++) |
|
1511 conditions.push('Element.hasClassName(element, ' + clause[i].inspect() + ')'); |
|
1512 if (clause = params.attributes) { |
|
1513 clause.each(function(attribute) { |
|
1514 var value = 'element.getAttribute(' + attribute.name.inspect() + ')'; |
|
1515 var splitValueBy = function(delimiter) { |
|
1516 return value + ' && ' + value + '.split(' + delimiter.inspect() + ')'; |
|
1517 } |
|
1518 |
|
1519 switch (attribute.operator) { |
|
1520 case '=': conditions.push(value + ' == ' + attribute.value.inspect()); break; |
|
1521 case '~=': conditions.push(splitValueBy(' ') + '.include(' + attribute.value.inspect() + ')'); break; |
|
1522 case '|=': conditions.push( |
|
1523 splitValueBy('-') + '.first().toUpperCase() == ' + attribute.value.toUpperCase().inspect() |
|
1524 ); break; |
|
1525 case '!=': conditions.push(value + ' != ' + attribute.value.inspect()); break; |
|
1526 case '': |
|
1527 case undefined: conditions.push(value + ' != null'); break; |
|
1528 default: throw 'Unknown operator ' + attribute.operator + ' in selector'; |
|
1529 } |
|
1530 }); |
|
1531 } |
|
1532 |
|
1533 return conditions.join(' && '); |
|
1534 }, |
|
1535 |
|
1536 compileMatcher: function() { |
|
1537 this.match = new Function('element', 'if (!element.tagName) return false; \ |
|
1538 return ' + this.buildMatchExpression()); |
|
1539 }, |
|
1540 |
|
1541 findElements: function(scope) { |
|
1542 var element; |
|
1543 |
|
1544 if (element = $(this.params.id)) |
|
1545 if (this.match(element)) |
|
1546 if (!scope || Element.childOf(element, scope)) |
|
1547 return [element]; |
|
1548 |
|
1549 scope = (scope || document).getElementsByTagName(this.params.tagName || '*'); |
|
1550 |
|
1551 var results = []; |
|
1552 for (var i = 0; i < scope.length; i++) |
|
1553 if (this.match(element = scope[i])) |
|
1554 results.push(Element.extend(element)); |
|
1555 |
|
1556 return results; |
|
1557 }, |
|
1558 |
|
1559 toString: function() { |
|
1560 return this.expression; |
|
1561 } |
|
1562 } |
|
1563 |
|
1564 Object.extend(Selector, { |
|
1565 matchElements: function(elements, expression) { |
|
1566 var selector = new Selector(expression); |
|
1567 return elements.select(selector.match.bind(selector)); |
|
1568 }, |
|
1569 |
|
1570 findElement: function(elements, expression, index) { |
|
1571 if (typeof expression == 'number') index = expression, expression = false; |
|
1572 return Selector.matchElements(elements, expression || '*')[index || 0]; |
|
1573 }, |
|
1574 |
|
1575 findChildElements: function(element, expressions) { |
|
1576 return expressions.map(function(expression) { |
|
1577 return expression.strip().split(/\s+/).inject([null], function(results, expr) { |
|
1578 var selector = new Selector(expr); |
|
1579 return results.inject([], function(elements, result) { |
|
1580 return elements.concat(selector.findElements(result || element)); |
|
1581 }); |
|
1582 }); |
|
1583 }).flatten(); |
|
1584 } |
|
1585 }); |
|
1586 |
|
1587 function $$() { |
|
1588 return Selector.findChildElements(document, $A(arguments)); |
|
1589 } |
|
1590 var Form = { |
|
1591 reset: function(form) { |
|
1592 $(form).reset(); |
|
1593 return form; |
|
1594 } |
|
1595 }; |
|
1596 |
|
1597 Form.Methods = { |
|
1598 serialize: function(form) { |
|
1599 var elements = Form.getElements($(form)); |
|
1600 var queryComponents = new Array(); |
|
1601 |
|
1602 for (var i = 0; i < elements.length; i++) { |
|
1603 var queryComponent = Form.Element.serialize(elements[i]); |
|
1604 if (queryComponent) |
|
1605 queryComponents.push(queryComponent); |
|
1606 } |
|
1607 |
|
1608 return queryComponents.join('&'); |
|
1609 }, |
|
1610 |
|
1611 getElements: function(form) { |
|
1612 form = $(form); |
|
1613 var elements = new Array(); |
|
1614 |
|
1615 for (var tagName in Form.Element.Serializers) { |
|
1616 var tagElements = form.getElementsByTagName(tagName); |
|
1617 for (var j = 0; j < tagElements.length; j++) |
|
1618 elements.push(tagElements[j]); |
|
1619 } |
|
1620 return elements; |
|
1621 }, |
|
1622 |
|
1623 getInputs: function(form, typeName, name) { |
|
1624 form = $(form); |
|
1625 var inputs = form.getElementsByTagName('input'); |
|
1626 |
|
1627 if (!typeName && !name) |
|
1628 return inputs; |
|
1629 |
|
1630 var matchingInputs = new Array(); |
|
1631 for (var i = 0; i < inputs.length; i++) { |
|
1632 var input = inputs[i]; |
|
1633 if ((typeName && input.type != typeName) || |
|
1634 (name && input.name != name)) |
|
1635 continue; |
|
1636 matchingInputs.push(input); |
|
1637 } |
|
1638 |
|
1639 return matchingInputs; |
|
1640 }, |
|
1641 |
|
1642 disable: function(form) { |
|
1643 form = $(form); |
|
1644 var elements = Form.getElements(form); |
|
1645 for (var i = 0; i < elements.length; i++) { |
|
1646 var element = elements[i]; |
|
1647 element.blur(); |
|
1648 element.disabled = 'true'; |
|
1649 } |
|
1650 return form; |
|
1651 }, |
|
1652 |
|
1653 enable: function(form) { |
|
1654 form = $(form); |
|
1655 var elements = Form.getElements(form); |
|
1656 for (var i = 0; i < elements.length; i++) { |
|
1657 var element = elements[i]; |
|
1658 element.disabled = ''; |
|
1659 } |
|
1660 return form; |
|
1661 }, |
|
1662 |
|
1663 findFirstElement: function(form) { |
|
1664 return Form.getElements(form).find(function(element) { |
|
1665 return element.type != 'hidden' && !element.disabled && |
|
1666 ['input', 'select', 'textarea'].include(element.tagName.toLowerCase()); |
|
1667 }); |
|
1668 }, |
|
1669 |
|
1670 focusFirstElement: function(form) { |
|
1671 form = $(form); |
|
1672 Field.activate(Form.findFirstElement(form)); |
|
1673 return form; |
|
1674 } |
|
1675 } |
|
1676 |
|
1677 Object.extend(Form, Form.Methods); |
|
1678 |
|
1679 /*--------------------------------------------------------------------------*/ |
|
1680 |
|
1681 Form.Element = { |
|
1682 focus: function(element) { |
|
1683 $(element).focus(); |
|
1684 return element; |
|
1685 }, |
|
1686 |
|
1687 select: function(element) { |
|
1688 $(element).select(); |
|
1689 return element; |
|
1690 } |
|
1691 } |
|
1692 |
|
1693 Form.Element.Methods = { |
|
1694 serialize: function(element) { |
|
1695 element = $(element); |
|
1696 var method = element.tagName.toLowerCase(); |
|
1697 var parameter = Form.Element.Serializers[method](element); |
|
1698 |
|
1699 if (parameter) { |
|
1700 var key = encodeURIComponent(parameter[0]); |
|
1701 if (key.length == 0) return; |
|
1702 |
|
1703 if (parameter[1].constructor != Array) |
|
1704 parameter[1] = [parameter[1]]; |
|
1705 |
|
1706 return parameter[1].map(function(value) { |
|
1707 return key + '=' + encodeURIComponent(value); |
|
1708 }).join('&'); |
|
1709 } |
|
1710 }, |
|
1711 |
|
1712 getValue: function(element) { |
|
1713 element = $(element); |
|
1714 var method = element.tagName.toLowerCase(); |
|
1715 var parameter = Form.Element.Serializers[method](element); |
|
1716 |
|
1717 if (parameter) |
|
1718 return parameter[1]; |
|
1719 }, |
|
1720 |
|
1721 clear: function(element) { |
|
1722 $(element).value = ''; |
|
1723 return element; |
|
1724 }, |
|
1725 |
|
1726 present: function(element) { |
|
1727 return $(element).value != ''; |
|
1728 }, |
|
1729 |
|
1730 activate: function(element) { |
|
1731 element = $(element); |
|
1732 element.focus(); |
|
1733 if (element.select) |
|
1734 element.select(); |
|
1735 return element; |
|
1736 }, |
|
1737 |
|
1738 disable: function(element) { |
|
1739 element = $(element); |
|
1740 element.disabled = ''; |
|
1741 return element; |
|
1742 }, |
|
1743 |
|
1744 enable: function(element) { |
|
1745 element = $(element); |
|
1746 element.blur(); |
|
1747 element.disabled = 'true'; |
|
1748 return element; |
|
1749 } |
|
1750 } |
|
1751 |
|
1752 Object.extend(Form.Element, Form.Element.Methods); |
|
1753 var Field = Form.Element; |
|
1754 |
|
1755 /*--------------------------------------------------------------------------*/ |
|
1756 |
|
1757 Form.Element.Serializers = { |
|
1758 input: function(element) { |
|
1759 switch (element.type.toLowerCase()) { |
|
1760 case 'checkbox': |
|
1761 case 'radio': |
|
1762 return Form.Element.Serializers.inputSelector(element); |
|
1763 default: |
|
1764 return Form.Element.Serializers.textarea(element); |
|
1765 } |
|
1766 return false; |
|
1767 }, |
|
1768 |
|
1769 inputSelector: function(element) { |
|
1770 if (element.checked) |
|
1771 return [element.name, element.value]; |
|
1772 }, |
|
1773 |
|
1774 textarea: function(element) { |
|
1775 return [element.name, element.value]; |
|
1776 }, |
|
1777 |
|
1778 select: function(element) { |
|
1779 return Form.Element.Serializers[element.type == 'select-one' ? |
|
1780 'selectOne' : 'selectMany'](element); |
|
1781 }, |
|
1782 |
|
1783 selectOne: function(element) { |
|
1784 var value = '', opt, index = element.selectedIndex; |
|
1785 if (index >= 0) { |
|
1786 opt = element.options[index]; |
|
1787 value = opt.value || opt.text; |
|
1788 } |
|
1789 return [element.name, value]; |
|
1790 }, |
|
1791 |
|
1792 selectMany: function(element) { |
|
1793 var value = []; |
|
1794 for (var i = 0; i < element.length; i++) { |
|
1795 var opt = element.options[i]; |
|
1796 if (opt.selected) |
|
1797 value.push(opt.value || opt.text); |
|
1798 } |
|
1799 return [element.name, value]; |
|
1800 } |
|
1801 } |
|
1802 |
|
1803 /*--------------------------------------------------------------------------*/ |
|
1804 |
|
1805 var $F = Form.Element.getValue; |
|
1806 |
|
1807 /*--------------------------------------------------------------------------*/ |
|
1808 |
|
1809 Abstract.TimedObserver = function() {} |
|
1810 Abstract.TimedObserver.prototype = { |
|
1811 initialize: function(element, frequency, callback) { |
|
1812 this.frequency = frequency; |
|
1813 this.element = $(element); |
|
1814 this.callback = callback; |
|
1815 |
|
1816 this.lastValue = this.getValue(); |
|
1817 this.registerCallback(); |
|
1818 }, |
|
1819 |
|
1820 registerCallback: function() { |
|
1821 setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); |
|
1822 }, |
|
1823 |
|
1824 onTimerEvent: function() { |
|
1825 var value = this.getValue(); |
|
1826 if (this.lastValue != value) { |
|
1827 this.callback(this.element, value); |
|
1828 this.lastValue = value; |
|
1829 } |
|
1830 } |
|
1831 } |
|
1832 |
|
1833 Form.Element.Observer = Class.create(); |
|
1834 Form.Element.Observer.prototype = Object.extend(new Abstract.TimedObserver(), { |
|
1835 getValue: function() { |
|
1836 return Form.Element.getValue(this.element); |
|
1837 } |
|
1838 }); |
|
1839 |
|
1840 Form.Observer = Class.create(); |
|
1841 Form.Observer.prototype = Object.extend(new Abstract.TimedObserver(), { |
|
1842 getValue: function() { |
|
1843 return Form.serialize(this.element); |
|
1844 } |
|
1845 }); |
|
1846 |
|
1847 /*--------------------------------------------------------------------------*/ |
|
1848 |
|
1849 Abstract.EventObserver = function() {} |
|
1850 Abstract.EventObserver.prototype = { |
|
1851 initialize: function(element, callback) { |
|
1852 this.element = $(element); |
|
1853 this.callback = callback; |
|
1854 |
|
1855 this.lastValue = this.getValue(); |
|
1856 if (this.element.tagName.toLowerCase() == 'form') |
|
1857 this.registerFormCallbacks(); |
|
1858 else |
|
1859 this.registerCallback(this.element); |
|
1860 }, |
|
1861 |
|
1862 onElementEvent: function() { |
|
1863 var value = this.getValue(); |
|
1864 if (this.lastValue != value) { |
|
1865 this.callback(this.element, value); |
|
1866 this.lastValue = value; |
|
1867 } |
|
1868 }, |
|
1869 |
|
1870 registerFormCallbacks: function() { |
|
1871 var elements = Form.getElements(this.element); |
|
1872 for (var i = 0; i < elements.length; i++) |
|
1873 this.registerCallback(elements[i]); |
|
1874 }, |
|
1875 |
|
1876 registerCallback: function(element) { |
|
1877 if (element.type) { |
|
1878 switch (element.type.toLowerCase()) { |
|
1879 case 'checkbox': |
|
1880 case 'radio': |
|
1881 Event.observe(element, 'click', this.onElementEvent.bind(this)); |
|
1882 break; |
|
1883 default: |
|
1884 Event.observe(element, 'change', this.onElementEvent.bind(this)); |
|
1885 break; |
|
1886 } |
|
1887 } |
|
1888 } |
|
1889 } |
|
1890 |
|
1891 Form.Element.EventObserver = Class.create(); |
|
1892 Form.Element.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), { |
|
1893 getValue: function() { |
|
1894 return Form.Element.getValue(this.element); |
|
1895 } |
|
1896 }); |
|
1897 |
|
1898 Form.EventObserver = Class.create(); |
|
1899 Form.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), { |
|
1900 getValue: function() { |
|
1901 return Form.serialize(this.element); |
|
1902 } |
|
1903 }); |
|
1904 if (!window.Event) { |
|
1905 var Event = new Object(); |
|
1906 } |
|
1907 |
|
1908 Object.extend(Event, { |
|
1909 KEY_BACKSPACE: 8, |
|
1910 KEY_TAB: 9, |
|
1911 KEY_RETURN: 13, |
|
1912 KEY_ESC: 27, |
|
1913 KEY_LEFT: 37, |
|
1914 KEY_UP: 38, |
|
1915 KEY_RIGHT: 39, |
|
1916 KEY_DOWN: 40, |
|
1917 KEY_DELETE: 46, |
|
1918 KEY_HOME: 36, |
|
1919 KEY_END: 35, |
|
1920 KEY_PAGEUP: 33, |
|
1921 KEY_PAGEDOWN: 34, |
|
1922 |
|
1923 element: function(event) { |
|
1924 return event.target || event.srcElement; |
|
1925 }, |
|
1926 |
|
1927 isLeftClick: function(event) { |
|
1928 return (((event.which) && (event.which == 1)) || |
|
1929 ((event.button) && (event.button == 1))); |
|
1930 }, |
|
1931 |
|
1932 pointerX: function(event) { |
|
1933 return event.pageX || (event.clientX + |
|
1934 (document.documentElement.scrollLeft || document.body.scrollLeft)); |
|
1935 }, |
|
1936 |
|
1937 pointerY: function(event) { |
|
1938 return event.pageY || (event.clientY + |
|
1939 (document.documentElement.scrollTop || document.body.scrollTop)); |
|
1940 }, |
|
1941 |
|
1942 stop: function(event) { |
|
1943 if (event.preventDefault) { |
|
1944 event.preventDefault(); |
|
1945 event.stopPropagation(); |
|
1946 } else { |
|
1947 event.returnValue = false; |
|
1948 event.cancelBubble = true; |
|
1949 } |
|
1950 }, |
|
1951 |
|
1952 // find the first node with the given tagName, starting from the |
|
1953 // node the event was triggered on; traverses the DOM upwards |
|
1954 findElement: function(event, tagName) { |
|
1955 var element = Event.element(event); |
|
1956 while (element.parentNode && (!element.tagName || |
|
1957 (element.tagName.toUpperCase() != tagName.toUpperCase()))) |
|
1958 element = element.parentNode; |
|
1959 return element; |
|
1960 }, |
|
1961 |
|
1962 observers: false, |
|
1963 |
|
1964 _observeAndCache: function(element, name, observer, useCapture) { |
|
1965 if (!this.observers) this.observers = []; |
|
1966 if (element.addEventListener) { |
|
1967 this.observers.push([element, name, observer, useCapture]); |
|
1968 element.addEventListener(name, observer, useCapture); |
|
1969 } else if (element.attachEvent) { |
|
1970 this.observers.push([element, name, observer, useCapture]); |
|
1971 element.attachEvent('on' + name, observer); |
|
1972 } |
|
1973 }, |
|
1974 |
|
1975 unloadCache: function() { |
|
1976 if (!Event.observers) return; |
|
1977 for (var i = 0; i < Event.observers.length; i++) { |
|
1978 Event.stopObserving.apply(this, Event.observers[i]); |
|
1979 Event.observers[i][0] = null; |
|
1980 } |
|
1981 Event.observers = false; |
|
1982 }, |
|
1983 |
|
1984 observe: function(element, name, observer, useCapture) { |
|
1985 element = $(element); |
|
1986 useCapture = useCapture || false; |
|
1987 |
|
1988 if (name == 'keypress' && |
|
1989 (navigator.appVersion.match(/Konqueror|Safari|KHTML/) |
|
1990 || element.attachEvent)) |
|
1991 name = 'keydown'; |
|
1992 |
|
1993 Event._observeAndCache(element, name, observer, useCapture); |
|
1994 }, |
|
1995 |
|
1996 stopObserving: function(element, name, observer, useCapture) { |
|
1997 element = $(element); |
|
1998 useCapture = useCapture || false; |
|
1999 |
|
2000 if (name == 'keypress' && |
|
2001 (navigator.appVersion.match(/Konqueror|Safari|KHTML/) |
|
2002 || element.detachEvent)) |
|
2003 name = 'keydown'; |
|
2004 |
|
2005 if (element.removeEventListener) { |
|
2006 element.removeEventListener(name, observer, useCapture); |
|
2007 } else if (element.detachEvent) { |
|
2008 try { |
|
2009 element.detachEvent('on' + name, observer); |
|
2010 } catch (e) {} |
|
2011 } |
|
2012 } |
|
2013 }); |
|
2014 |
|
2015 /* prevent memory leaks in IE */ |
|
2016 if (navigator.appVersion.match(/\bMSIE\b/)) |
|
2017 Event.observe(window, 'unload', Event.unloadCache, false); |
|
2018 var Position = { |
|
2019 // set to true if needed, warning: firefox performance problems |
|
2020 // NOT neeeded for page scrolling, only if draggable contained in |
|
2021 // scrollable elements |
|
2022 includeScrollOffsets: false, |
|
2023 |
|
2024 // must be called before calling withinIncludingScrolloffset, every time the |
|
2025 // page is scrolled |
|
2026 prepare: function() { |
|
2027 this.deltaX = window.pageXOffset |
|
2028 || document.documentElement.scrollLeft |
|
2029 || document.body.scrollLeft |
|
2030 || 0; |
|
2031 this.deltaY = window.pageYOffset |
|
2032 || document.documentElement.scrollTop |
|
2033 || document.body.scrollTop |
|
2034 || 0; |
|
2035 }, |
|
2036 |
|
2037 realOffset: function(element) { |
|
2038 var valueT = 0, valueL = 0; |
|
2039 do { |
|
2040 valueT += element.scrollTop || 0; |
|
2041 valueL += element.scrollLeft || 0; |
|
2042 element = element.parentNode; |
|
2043 } while (element); |
|
2044 return [valueL, valueT]; |
|
2045 }, |
|
2046 |
|
2047 cumulativeOffset: function(element) { |
|
2048 var valueT = 0, valueL = 0; |
|
2049 do { |
|
2050 valueT += element.offsetTop || 0; |
|
2051 valueL += element.offsetLeft || 0; |
|
2052 element = element.offsetParent; |
|
2053 } while (element); |
|
2054 return [valueL, valueT]; |
|
2055 }, |
|
2056 |
|
2057 positionedOffset: function(element) { |
|
2058 var valueT = 0, valueL = 0; |
|
2059 do { |
|
2060 valueT += element.offsetTop || 0; |
|
2061 valueL += element.offsetLeft || 0; |
|
2062 element = element.offsetParent; |
|
2063 if (element) { |
|
2064 p = Element.getStyle(element, 'position'); |
|
2065 if (p == 'relative' || p == 'absolute') break; |
|
2066 } |
|
2067 } while (element); |
|
2068 return [valueL, valueT]; |
|
2069 }, |
|
2070 |
|
2071 offsetParent: function(element) { |
|
2072 if (element.offsetParent) return element.offsetParent; |
|
2073 if (element == document.body) return element; |
|
2074 |
|
2075 while ((element = element.parentNode) && element != document.body) |
|
2076 if (Element.getStyle(element, 'position') != 'static') |
|
2077 return element; |
|
2078 |
|
2079 return document.body; |
|
2080 }, |
|
2081 |
|
2082 // caches x/y coordinate pair to use with overlap |
|
2083 within: function(element, x, y) { |
|
2084 if (this.includeScrollOffsets) |
|
2085 return this.withinIncludingScrolloffsets(element, x, y); |
|
2086 this.xcomp = x; |
|
2087 this.ycomp = y; |
|
2088 this.offset = this.cumulativeOffset(element); |
|
2089 |
|
2090 return (y >= this.offset[1] && |
|
2091 y < this.offset[1] + element.offsetHeight && |
|
2092 x >= this.offset[0] && |
|
2093 x < this.offset[0] + element.offsetWidth); |
|
2094 }, |
|
2095 |
|
2096 withinIncludingScrolloffsets: function(element, x, y) { |
|
2097 var offsetcache = this.realOffset(element); |
|
2098 |
|
2099 this.xcomp = x + offsetcache[0] - this.deltaX; |
|
2100 this.ycomp = y + offsetcache[1] - this.deltaY; |
|
2101 this.offset = this.cumulativeOffset(element); |
|
2102 |
|
2103 return (this.ycomp >= this.offset[1] && |
|
2104 this.ycomp < this.offset[1] + element.offsetHeight && |
|
2105 this.xcomp >= this.offset[0] && |
|
2106 this.xcomp < this.offset[0] + element.offsetWidth); |
|
2107 }, |
|
2108 |
|
2109 // within must be called directly before |
|
2110 overlap: function(mode, element) { |
|
2111 if (!mode) return 0; |
|
2112 if (mode == 'vertical') |
|
2113 return ((this.offset[1] + element.offsetHeight) - this.ycomp) / |
|
2114 element.offsetHeight; |
|
2115 if (mode == 'horizontal') |
|
2116 return ((this.offset[0] + element.offsetWidth) - this.xcomp) / |
|
2117 element.offsetWidth; |
|
2118 }, |
|
2119 |
|
2120 page: function(forElement) { |
|
2121 var valueT = 0, valueL = 0; |
|
2122 |
|
2123 var element = forElement; |
|
2124 do { |
|
2125 valueT += element.offsetTop || 0; |
|
2126 valueL += element.offsetLeft || 0; |
|
2127 |
|
2128 // Safari fix |
|
2129 if (element.offsetParent==document.body) |
|
2130 if (Element.getStyle(element,'position')=='absolute') break; |
|
2131 |
|
2132 } while (element = element.offsetParent); |
|
2133 |
|
2134 element = forElement; |
|
2135 do { |
|
2136 if (!window.opera || element.tagName=='BODY') { |
|
2137 valueT -= element.scrollTop || 0; |
|
2138 valueL -= element.scrollLeft || 0; |
|
2139 } |
|
2140 } while (element = element.parentNode); |
|
2141 |
|
2142 return [valueL, valueT]; |
|
2143 }, |
|
2144 |
|
2145 clone: function(source, target) { |
|
2146 var options = Object.extend({ |
|
2147 setLeft: true, |
|
2148 setTop: true, |
|
2149 setWidth: true, |
|
2150 setHeight: true, |
|
2151 offsetTop: 0, |
|
2152 offsetLeft: 0 |
|
2153 }, arguments[2] || {}) |
|
2154 |
|
2155 // find page position of source |
|
2156 source = $(source); |
|
2157 var p = Position.page(source); |
|
2158 |
|
2159 // find coordinate system to use |
|
2160 target = $(target); |
|
2161 var delta = [0, 0]; |
|
2162 var parent = null; |
|
2163 // delta [0,0] will do fine with position: fixed elements, |
|
2164 // position:absolute needs offsetParent deltas |
|
2165 if (Element.getStyle(target,'position') == 'absolute') { |
|
2166 parent = Position.offsetParent(target); |
|
2167 delta = Position.page(parent); |
|
2168 } |
|
2169 |
|
2170 // correct by body offsets (fixes Safari) |
|
2171 if (parent == document.body) { |
|
2172 delta[0] -= document.body.offsetLeft; |
|
2173 delta[1] -= document.body.offsetTop; |
|
2174 } |
|
2175 |
|
2176 // set position |
|
2177 if(options.setLeft) target.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px'; |
|
2178 if(options.setTop) target.style.top = (p[1] - delta[1] + options.offsetTop) + 'px'; |
|
2179 if(options.setWidth) target.style.width = source.offsetWidth + 'px'; |
|
2180 if(options.setHeight) target.style.height = source.offsetHeight + 'px'; |
|
2181 }, |
|
2182 |
|
2183 absolutize: function(element) { |
|
2184 element = $(element); |
|
2185 if (element.style.position == 'absolute') return; |
|
2186 Position.prepare(); |
|
2187 |
|
2188 var offsets = Position.positionedOffset(element); |
|
2189 var top = offsets[1]; |
|
2190 var left = offsets[0]; |
|
2191 var width = element.clientWidth; |
|
2192 var height = element.clientHeight; |
|
2193 |
|
2194 element._originalLeft = left - parseFloat(element.style.left || 0); |
|
2195 element._originalTop = top - parseFloat(element.style.top || 0); |
|
2196 element._originalWidth = element.style.width; |
|
2197 element._originalHeight = element.style.height; |
|
2198 |
|
2199 element.style.position = 'absolute'; |
|
2200 element.style.top = top + 'px';; |
|
2201 element.style.left = left + 'px';; |
|
2202 element.style.width = width + 'px';; |
|
2203 element.style.height = height + 'px';; |
|
2204 }, |
|
2205 |
|
2206 relativize: function(element) { |
|
2207 element = $(element); |
|
2208 if (element.style.position == 'relative') return; |
|
2209 Position.prepare(); |
|
2210 |
|
2211 element.style.position = 'relative'; |
|
2212 var top = parseFloat(element.style.top || 0) - (element._originalTop || 0); |
|
2213 var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0); |
|
2214 |
|
2215 element.style.top = top + 'px'; |
|
2216 element.style.left = left + 'px'; |
|
2217 element.style.height = element._originalHeight; |
|
2218 element.style.width = element._originalWidth; |
|
2219 } |
|
2220 } |
|
2221 |
|
2222 // Safari returns margins on body which is incorrect if the child is absolutely |
|
2223 // positioned. For performance reasons, redefine Position.cumulativeOffset for |
|
2224 // KHTML/WebKit only. |
|
2225 if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) { |
|
2226 Position.cumulativeOffset = function(element) { |
|
2227 var valueT = 0, valueL = 0; |
|
2228 do { |
|
2229 valueT += element.offsetTop || 0; |
|
2230 valueL += element.offsetLeft || 0; |
|
2231 if (element.offsetParent == document.body) |
|
2232 if (Element.getStyle(element, 'position') == 'absolute') break; |
|
2233 |
|
2234 element = element.offsetParent; |
|
2235 } while (element); |
|
2236 |
|
2237 return [valueL, valueT]; |
|
2238 } |
|
2239 } |
|
2240 |
|
2241 Element.addMethods(); |