PageRenderTime 70ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/activiti-webapp-explorer2/src/main/webapp/libs/ext-2.0.2/adapter/prototype/prototype.js

https://bitbucket.org/ddabber/activiti
JavaScript | 4221 lines | 3653 code | 490 blank | 78 comment | 621 complexity | 85ef171f309a2e29242d85c105bb0223 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* Prototype JavaScript framework, version 1.6.0.2
  2. * (c) 2005-2008 Sam Stephenson
  3. *
  4. * Prototype is freely distributable under the terms of an MIT-style license.
  5. * For details, see the Prototype web site: http://www.prototypejs.org/
  6. *
  7. *--------------------------------------------------------------------------*/
  8. var Prototype = {
  9. Version: '1.6.0.2',
  10. Browser: {
  11. IE: !!(window.attachEvent && !window.opera),
  12. Opera: !!window.opera,
  13. WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
  14. Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
  15. MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
  16. },
  17. BrowserFeatures: {
  18. XPath: !!document.evaluate,
  19. ElementExtensions: !!window.HTMLElement,
  20. SpecificElementExtensions:
  21. document.createElement('div').__proto__ &&
  22. document.createElement('div').__proto__ !==
  23. document.createElement('form').__proto__
  24. },
  25. ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>',
  26. JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,
  27. emptyFunction: function() { },
  28. K: function(x) { return x }
  29. };
  30. if (Prototype.Browser.MobileSafari)
  31. Prototype.BrowserFeatures.SpecificElementExtensions = false;
  32. /* Based on Alex Arnell's inheritance implementation. */
  33. var Class = {
  34. create: function() {
  35. var parent = null, properties = $A(arguments);
  36. if (Object.isFunction(properties[0]))
  37. parent = properties.shift();
  38. function klass() {
  39. this.initialize.apply(this, arguments);
  40. }
  41. Object.extend(klass, Class.Methods);
  42. klass.superclass = parent;
  43. klass.subclasses = [];
  44. if (parent) {
  45. var subclass = function() { };
  46. subclass.prototype = parent.prototype;
  47. klass.prototype = new subclass;
  48. parent.subclasses.push(klass);
  49. }
  50. for (var i = 0; i < properties.length; i++)
  51. klass.addMethods(properties[i]);
  52. if (!klass.prototype.initialize)
  53. klass.prototype.initialize = Prototype.emptyFunction;
  54. klass.prototype.constructor = klass;
  55. return klass;
  56. }
  57. };
  58. Class.Methods = {
  59. addMethods: function(source) {
  60. var ancestor = this.superclass && this.superclass.prototype;
  61. var properties = Object.keys(source);
  62. if (!Object.keys({ toString: true }).length)
  63. properties.push("toString", "valueOf");
  64. for (var i = 0, length = properties.length; i < length; i++) {
  65. var property = properties[i], value = source[property];
  66. if (ancestor && Object.isFunction(value) &&
  67. value.argumentNames().first() == "$super") {
  68. var method = value, value = Object.extend((function(m) {
  69. return function() { return ancestor[m].apply(this, arguments) };
  70. })(property).wrap(method), {
  71. valueOf: function() { return method },
  72. toString: function() { return method.toString() }
  73. });
  74. }
  75. this.prototype[property] = value;
  76. }
  77. return this;
  78. }
  79. };
  80. var Abstract = { };
  81. Object.extend = function(destination, source) {
  82. for (var property in source)
  83. destination[property] = source[property];
  84. return destination;
  85. };
  86. Object.extend(Object, {
  87. inspect: function(object) {
  88. try {
  89. if (Object.isUndefined(object)) return 'undefined';
  90. if (object === null) return 'null';
  91. return object.inspect ? object.inspect() : String(object);
  92. } catch (e) {
  93. if (e instanceof RangeError) return '...';
  94. throw e;
  95. }
  96. },
  97. toJSON: function(object) {
  98. var type = typeof object;
  99. switch (type) {
  100. case 'undefined':
  101. case 'function':
  102. case 'unknown': return;
  103. case 'boolean': return object.toString();
  104. }
  105. if (object === null) return 'null';
  106. if (object.toJSON) return object.toJSON();
  107. if (Object.isElement(object)) return;
  108. var results = [];
  109. for (var property in object) {
  110. var value = Object.toJSON(object[property]);
  111. if (!Object.isUndefined(value))
  112. results.push(property.toJSON() + ': ' + value);
  113. }
  114. return '{' + results.join(', ') + '}';
  115. },
  116. toQueryString: function(object) {
  117. return $H(object).toQueryString();
  118. },
  119. toHTML: function(object) {
  120. return object && object.toHTML ? object.toHTML() : String.interpret(object);
  121. },
  122. keys: function(object) {
  123. var keys = [];
  124. for (var property in object)
  125. keys.push(property);
  126. return keys;
  127. },
  128. values: function(object) {
  129. var values = [];
  130. for (var property in object)
  131. values.push(object[property]);
  132. return values;
  133. },
  134. clone: function(object) {
  135. return Object.extend({ }, object);
  136. },
  137. isElement: function(object) {
  138. return object && object.nodeType == 1;
  139. },
  140. isArray: function(object) {
  141. return object != null && typeof object == "object" &&
  142. 'splice' in object && 'join' in object;
  143. },
  144. isHash: function(object) {
  145. return object instanceof Hash;
  146. },
  147. isFunction: function(object) {
  148. return typeof object == "function";
  149. },
  150. isString: function(object) {
  151. return typeof object == "string";
  152. },
  153. isNumber: function(object) {
  154. return typeof object == "number";
  155. },
  156. isUndefined: function(object) {
  157. return typeof object == "undefined";
  158. }
  159. });
  160. Object.extend(Function.prototype, {
  161. argumentNames: function() {
  162. var names = this.toString().match(/^[\s\(]*function[^(]*\((.*?)\)/)[1].split(",").invoke("strip");
  163. return names.length == 1 && !names[0] ? [] : names;
  164. },
  165. bind: function() {
  166. if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
  167. var __method = this, args = $A(arguments), object = args.shift();
  168. return function() {
  169. return __method.apply(object, args.concat($A(arguments)));
  170. }
  171. },
  172. bindAsEventListener: function() {
  173. var __method = this, args = $A(arguments), object = args.shift();
  174. return function(event) {
  175. return __method.apply(object, [event || window.event].concat(args));
  176. }
  177. },
  178. curry: function() {
  179. if (!arguments.length) return this;
  180. var __method = this, args = $A(arguments);
  181. return function() {
  182. return __method.apply(this, args.concat($A(arguments)));
  183. }
  184. },
  185. delay: function() {
  186. var __method = this, args = $A(arguments), timeout = args.shift() * 1000;
  187. return window.setTimeout(function() {
  188. return __method.apply(__method, args);
  189. }, timeout);
  190. },
  191. wrap: function(wrapper) {
  192. var __method = this;
  193. return function() {
  194. return wrapper.apply(this, [__method.bind(this)].concat($A(arguments)));
  195. }
  196. },
  197. methodize: function() {
  198. if (this._methodized) return this._methodized;
  199. var __method = this;
  200. return this._methodized = function() {
  201. return __method.apply(null, [this].concat($A(arguments)));
  202. };
  203. }
  204. });
  205. Function.prototype.defer = Function.prototype.delay.curry(0.01);
  206. Date.prototype.toJSON = function() {
  207. return '"' + this.getUTCFullYear() + '-' +
  208. (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
  209. this.getUTCDate().toPaddedString(2) + 'T' +
  210. this.getUTCHours().toPaddedString(2) + ':' +
  211. this.getUTCMinutes().toPaddedString(2) + ':' +
  212. this.getUTCSeconds().toPaddedString(2) + 'Z"';
  213. };
  214. var Try = {
  215. these: function() {
  216. var returnValue;
  217. for (var i = 0, length = arguments.length; i < length; i++) {
  218. var lambda = arguments[i];
  219. try {
  220. returnValue = lambda();
  221. break;
  222. } catch (e) { }
  223. }
  224. return returnValue;
  225. }
  226. };
  227. RegExp.prototype.match = RegExp.prototype.test;
  228. RegExp.escape = function(str) {
  229. return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  230. };
  231. /*--------------------------------------------------------------------------*/
  232. var PeriodicalExecuter = Class.create({
  233. initialize: function(callback, frequency) {
  234. this.callback = callback;
  235. this.frequency = frequency;
  236. this.currentlyExecuting = false;
  237. this.registerCallback();
  238. },
  239. registerCallback: function() {
  240. this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
  241. },
  242. execute: function() {
  243. this.callback(this);
  244. },
  245. stop: function() {
  246. if (!this.timer) return;
  247. clearInterval(this.timer);
  248. this.timer = null;
  249. },
  250. onTimerEvent: function() {
  251. if (!this.currentlyExecuting) {
  252. try {
  253. this.currentlyExecuting = true;
  254. this.execute();
  255. } finally {
  256. this.currentlyExecuting = false;
  257. }
  258. }
  259. }
  260. });
  261. Object.extend(String, {
  262. interpret: function(value) {
  263. return value == null ? '' : String(value);
  264. },
  265. specialChar: {
  266. '\b': '\\b',
  267. '\t': '\\t',
  268. '\n': '\\n',
  269. '\f': '\\f',
  270. '\r': '\\r',
  271. '\\': '\\\\'
  272. }
  273. });
  274. Object.extend(String.prototype, {
  275. gsub: function(pattern, replacement) {
  276. var result = '', source = this, match;
  277. replacement = arguments.callee.prepareReplacement(replacement);
  278. while (source.length > 0) {
  279. if (match = source.match(pattern)) {
  280. result += source.slice(0, match.index);
  281. result += String.interpret(replacement(match));
  282. source = source.slice(match.index + match[0].length);
  283. } else {
  284. result += source, source = '';
  285. }
  286. }
  287. return result;
  288. },
  289. sub: function(pattern, replacement, count) {
  290. replacement = this.gsub.prepareReplacement(replacement);
  291. count = Object.isUndefined(count) ? 1 : count;
  292. return this.gsub(pattern, function(match) {
  293. if (--count < 0) return match[0];
  294. return replacement(match);
  295. });
  296. },
  297. scan: function(pattern, iterator) {
  298. this.gsub(pattern, iterator);
  299. return String(this);
  300. },
  301. truncate: function(length, truncation) {
  302. length = length || 30;
  303. truncation = Object.isUndefined(truncation) ? '...' : truncation;
  304. return this.length > length ?
  305. this.slice(0, length - truncation.length) + truncation : String(this);
  306. },
  307. strip: function() {
  308. return this.replace(/^\s+/, '').replace(/\s+$/, '');
  309. },
  310. stripTags: function() {
  311. return this.replace(/<\/?[^>]+>/gi, '');
  312. },
  313. stripScripts: function() {
  314. return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
  315. },
  316. extractScripts: function() {
  317. var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
  318. var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
  319. return (this.match(matchAll) || []).map(function(scriptTag) {
  320. return (scriptTag.match(matchOne) || ['', ''])[1];
  321. });
  322. },
  323. evalScripts: function() {
  324. return this.extractScripts().map(function(script) { return eval(script) });
  325. },
  326. escapeHTML: function() {
  327. var self = arguments.callee;
  328. self.text.data = this;
  329. return self.div.innerHTML;
  330. },
  331. unescapeHTML: function() {
  332. var div = new Element('div');
  333. div.innerHTML = this.stripTags();
  334. return div.childNodes[0] ? (div.childNodes.length > 1 ?
  335. $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) :
  336. div.childNodes[0].nodeValue) : '';
  337. },
  338. toQueryParams: function(separator) {
  339. var match = this.strip().match(/([^?#]*)(#.*)?$/);
  340. if (!match) return { };
  341. return match[1].split(separator || '&').inject({ }, function(hash, pair) {
  342. if ((pair = pair.split('='))[0]) {
  343. var key = decodeURIComponent(pair.shift());
  344. var value = pair.length > 1 ? pair.join('=') : pair[0];
  345. if (value != undefined) value = decodeURIComponent(value);
  346. if (key in hash) {
  347. if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
  348. hash[key].push(value);
  349. }
  350. else hash[key] = value;
  351. }
  352. return hash;
  353. });
  354. },
  355. toArray: function() {
  356. return this.split('');
  357. },
  358. succ: function() {
  359. return this.slice(0, this.length - 1) +
  360. String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
  361. },
  362. times: function(count) {
  363. return count < 1 ? '' : new Array(count + 1).join(this);
  364. },
  365. camelize: function() {
  366. var parts = this.split('-'), len = parts.length;
  367. if (len == 1) return parts[0];
  368. var camelized = this.charAt(0) == '-'
  369. ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
  370. : parts[0];
  371. for (var i = 1; i < len; i++)
  372. camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
  373. return camelized;
  374. },
  375. capitalize: function() {
  376. return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
  377. },
  378. underscore: function() {
  379. return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
  380. },
  381. dasherize: function() {
  382. return this.gsub(/_/,'-');
  383. },
  384. inspect: function(useDoubleQuotes) {
  385. var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) {
  386. var character = String.specialChar[match[0]];
  387. return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16);
  388. });
  389. if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
  390. return "'" + escapedString.replace(/'/g, '\\\'') + "'";
  391. },
  392. toJSON: function() {
  393. return this.inspect(true);
  394. },
  395. unfilterJSON: function(filter) {
  396. return this.sub(filter || Prototype.JSONFilter, '#{1}');
  397. },
  398. isJSON: function() {
  399. var str = this;
  400. if (str.blank()) return false;
  401. str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
  402. return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
  403. },
  404. evalJSON: function(sanitize) {
  405. var json = this.unfilterJSON();
  406. try {
  407. if (!sanitize || json.isJSON()) return eval('(' + json + ')');
  408. } catch (e) { }
  409. throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
  410. },
  411. include: function(pattern) {
  412. return this.indexOf(pattern) > -1;
  413. },
  414. startsWith: function(pattern) {
  415. return this.indexOf(pattern) === 0;
  416. },
  417. endsWith: function(pattern) {
  418. var d = this.length - pattern.length;
  419. return d >= 0 && this.lastIndexOf(pattern) === d;
  420. },
  421. empty: function() {
  422. return this == '';
  423. },
  424. blank: function() {
  425. return /^\s*$/.test(this);
  426. },
  427. interpolate: function(object, pattern) {
  428. return new Template(this, pattern).evaluate(object);
  429. }
  430. });
  431. if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, {
  432. escapeHTML: function() {
  433. return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
  434. },
  435. unescapeHTML: function() {
  436. return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
  437. }
  438. });
  439. String.prototype.gsub.prepareReplacement = function(replacement) {
  440. if (Object.isFunction(replacement)) return replacement;
  441. var template = new Template(replacement);
  442. return function(match) { return template.evaluate(match) };
  443. };
  444. String.prototype.parseQuery = String.prototype.toQueryParams;
  445. Object.extend(String.prototype.escapeHTML, {
  446. div: document.createElement('div'),
  447. text: document.createTextNode('')
  448. });
  449. with (String.prototype.escapeHTML) div.appendChild(text);
  450. var Template = Class.create({
  451. initialize: function(template, pattern) {
  452. this.template = template.toString();
  453. this.pattern = pattern || Template.Pattern;
  454. },
  455. evaluate: function(object) {
  456. if (Object.isFunction(object.toTemplateReplacements))
  457. object = object.toTemplateReplacements();
  458. return this.template.gsub(this.pattern, function(match) {
  459. if (object == null) return '';
  460. var before = match[1] || '';
  461. if (before == '\\') return match[2];
  462. var ctx = object, expr = match[3];
  463. var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
  464. match = pattern.exec(expr);
  465. if (match == null) return before;
  466. while (match != null) {
  467. var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1];
  468. ctx = ctx[comp];
  469. if (null == ctx || '' == match[3]) break;
  470. expr = expr.substring('[' == match[3] ? match[1].length : match[0].length);
  471. match = pattern.exec(expr);
  472. }
  473. return before + String.interpret(ctx);
  474. });
  475. }
  476. });
  477. Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
  478. var $break = { };
  479. var Enumerable = {
  480. each: function(iterator, context) {
  481. var index = 0;
  482. iterator = iterator.bind(context);
  483. try {
  484. this._each(function(value) {
  485. iterator(value, index++);
  486. });
  487. } catch (e) {
  488. if (e != $break) throw e;
  489. }
  490. return this;
  491. },
  492. eachSlice: function(number, iterator, context) {
  493. iterator = iterator ? iterator.bind(context) : Prototype.K;
  494. var index = -number, slices = [], array = this.toArray();
  495. while ((index += number) < array.length)
  496. slices.push(array.slice(index, index+number));
  497. return slices.collect(iterator, context);
  498. },
  499. all: function(iterator, context) {
  500. iterator = iterator ? iterator.bind(context) : Prototype.K;
  501. var result = true;
  502. this.each(function(value, index) {
  503. result = result && !!iterator(value, index);
  504. if (!result) throw $break;
  505. });
  506. return result;
  507. },
  508. any: function(iterator, context) {
  509. iterator = iterator ? iterator.bind(context) : Prototype.K;
  510. var result = false;
  511. this.each(function(value, index) {
  512. if (result = !!iterator(value, index))
  513. throw $break;
  514. });
  515. return result;
  516. },
  517. collect: function(iterator, context) {
  518. iterator = iterator ? iterator.bind(context) : Prototype.K;
  519. var results = [];
  520. this.each(function(value, index) {
  521. results.push(iterator(value, index));
  522. });
  523. return results;
  524. },
  525. detect: function(iterator, context) {
  526. iterator = iterator.bind(context);
  527. var result;
  528. this.each(function(value, index) {
  529. if (iterator(value, index)) {
  530. result = value;
  531. throw $break;
  532. }
  533. });
  534. return result;
  535. },
  536. findAll: function(iterator, context) {
  537. iterator = iterator.bind(context);
  538. var results = [];
  539. this.each(function(value, index) {
  540. if (iterator(value, index))
  541. results.push(value);
  542. });
  543. return results;
  544. },
  545. grep: function(filter, iterator, context) {
  546. iterator = iterator ? iterator.bind(context) : Prototype.K;
  547. var results = [];
  548. if (Object.isString(filter))
  549. filter = new RegExp(filter);
  550. this.each(function(value, index) {
  551. if (filter.match(value))
  552. results.push(iterator(value, index));
  553. });
  554. return results;
  555. },
  556. include: function(object) {
  557. if (Object.isFunction(this.indexOf))
  558. if (this.indexOf(object) != -1) return true;
  559. var found = false;
  560. this.each(function(value) {
  561. if (value == object) {
  562. found = true;
  563. throw $break;
  564. }
  565. });
  566. return found;
  567. },
  568. inGroupsOf: function(number, fillWith) {
  569. fillWith = Object.isUndefined(fillWith) ? null : fillWith;
  570. return this.eachSlice(number, function(slice) {
  571. while(slice.length < number) slice.push(fillWith);
  572. return slice;
  573. });
  574. },
  575. inject: function(memo, iterator, context) {
  576. iterator = iterator.bind(context);
  577. this.each(function(value, index) {
  578. memo = iterator(memo, value, index);
  579. });
  580. return memo;
  581. },
  582. invoke: function(method) {
  583. var args = $A(arguments).slice(1);
  584. return this.map(function(value) {
  585. return value[method].apply(value, args);
  586. });
  587. },
  588. max: function(iterator, context) {
  589. iterator = iterator ? iterator.bind(context) : Prototype.K;
  590. var result;
  591. this.each(function(value, index) {
  592. value = iterator(value, index);
  593. if (result == null || value >= result)
  594. result = value;
  595. });
  596. return result;
  597. },
  598. min: function(iterator, context) {
  599. iterator = iterator ? iterator.bind(context) : Prototype.K;
  600. var result;
  601. this.each(function(value, index) {
  602. value = iterator(value, index);
  603. if (result == null || value < result)
  604. result = value;
  605. });
  606. return result;
  607. },
  608. partition: function(iterator, context) {
  609. iterator = iterator ? iterator.bind(context) : Prototype.K;
  610. var trues = [], falses = [];
  611. this.each(function(value, index) {
  612. (iterator(value, index) ?
  613. trues : falses).push(value);
  614. });
  615. return [trues, falses];
  616. },
  617. pluck: function(property) {
  618. var results = [];
  619. this.each(function(value) {
  620. results.push(value[property]);
  621. });
  622. return results;
  623. },
  624. reject: function(iterator, context) {
  625. iterator = iterator.bind(context);
  626. var results = [];
  627. this.each(function(value, index) {
  628. if (!iterator(value, index))
  629. results.push(value);
  630. });
  631. return results;
  632. },
  633. sortBy: function(iterator, context) {
  634. iterator = iterator.bind(context);
  635. return this.map(function(value, index) {
  636. return {value: value, criteria: iterator(value, index)};
  637. }).sort(function(left, right) {
  638. var a = left.criteria, b = right.criteria;
  639. return a < b ? -1 : a > b ? 1 : 0;
  640. }).pluck('value');
  641. },
  642. toArray: function() {
  643. return this.map();
  644. },
  645. zip: function() {
  646. var iterator = Prototype.K, args = $A(arguments);
  647. if (Object.isFunction(args.last()))
  648. iterator = args.pop();
  649. var collections = [this].concat(args).map($A);
  650. return this.map(function(value, index) {
  651. return iterator(collections.pluck(index));
  652. });
  653. },
  654. size: function() {
  655. return this.toArray().length;
  656. },
  657. inspect: function() {
  658. return '#<Enumerable:' + this.toArray().inspect() + '>';
  659. }
  660. };
  661. Object.extend(Enumerable, {
  662. map: Enumerable.collect,
  663. find: Enumerable.detect,
  664. select: Enumerable.findAll,
  665. filter: Enumerable.findAll,
  666. member: Enumerable.include,
  667. entries: Enumerable.toArray,
  668. every: Enumerable.all,
  669. some: Enumerable.any
  670. });
  671. function $A(iterable) {
  672. if (!iterable) return [];
  673. if (iterable.toArray) return iterable.toArray();
  674. var length = iterable.length || 0, results = new Array(length);
  675. while (length--) results[length] = iterable[length];
  676. return results;
  677. }
  678. if (Prototype.Browser.WebKit) {
  679. $A = function(iterable) {
  680. if (!iterable) return [];
  681. if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') &&
  682. iterable.toArray) return iterable.toArray();
  683. var length = iterable.length || 0, results = new Array(length);
  684. while (length--) results[length] = iterable[length];
  685. return results;
  686. };
  687. }
  688. Array.from = $A;
  689. Object.extend(Array.prototype, Enumerable);
  690. if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse;
  691. Object.extend(Array.prototype, {
  692. _each: function(iterator) {
  693. for (var i = 0, length = this.length; i < length; i++)
  694. iterator(this[i]);
  695. },
  696. clear: function() {
  697. this.length = 0;
  698. return this;
  699. },
  700. first: function() {
  701. return this[0];
  702. },
  703. last: function() {
  704. return this[this.length - 1];
  705. },
  706. compact: function() {
  707. return this.select(function(value) {
  708. return value != null;
  709. });
  710. },
  711. flatten: function() {
  712. return this.inject([], function(array, value) {
  713. return array.concat(Object.isArray(value) ?
  714. value.flatten() : [value]);
  715. });
  716. },
  717. without: function() {
  718. var values = $A(arguments);
  719. return this.select(function(value) {
  720. return !values.include(value);
  721. });
  722. },
  723. reverse: function(inline) {
  724. return (inline !== false ? this : this.toArray())._reverse();
  725. },
  726. reduce: function() {
  727. return this.length > 1 ? this : this[0];
  728. },
  729. uniq: function(sorted) {
  730. return this.inject([], function(array, value, index) {
  731. if (0 == index || (sorted ? array.last() != value : !array.include(value)))
  732. array.push(value);
  733. return array;
  734. });
  735. },
  736. intersect: function(array) {
  737. return this.uniq().findAll(function(item) {
  738. return array.detect(function(value) { return item === value });
  739. });
  740. },
  741. clone: function() {
  742. return [].concat(this);
  743. },
  744. size: function() {
  745. return this.length;
  746. },
  747. inspect: function() {
  748. return '[' + this.map(Object.inspect).join(', ') + ']';
  749. },
  750. toJSON: function() {
  751. var results = [];
  752. this.each(function(object) {
  753. var value = Object.toJSON(object);
  754. if (!Object.isUndefined(value)) results.push(value);
  755. });
  756. return '[' + results.join(', ') + ']';
  757. }
  758. });
  759. // use native browser JS 1.6 implementation if available
  760. if (Object.isFunction(Array.prototype.forEach))
  761. Array.prototype._each = Array.prototype.forEach;
  762. if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) {
  763. i || (i = 0);
  764. var length = this.length;
  765. if (i < 0) i = length + i;
  766. for (; i < length; i++)
  767. if (this[i] === item) return i;
  768. return -1;
  769. };
  770. if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) {
  771. i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1;
  772. var n = this.slice(0, i).reverse().indexOf(item);
  773. return (n < 0) ? n : i - n - 1;
  774. };
  775. Array.prototype.toArray = Array.prototype.clone;
  776. function $w(string) {
  777. if (!Object.isString(string)) return [];
  778. string = string.strip();
  779. return string ? string.split(/\s+/) : [];
  780. }
  781. if (Prototype.Browser.Opera){
  782. Array.prototype.concat = function() {
  783. var array = [];
  784. for (var i = 0, length = this.length; i < length; i++) array.push(this[i]);
  785. for (var i = 0, length = arguments.length; i < length; i++) {
  786. if (Object.isArray(arguments[i])) {
  787. for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++)
  788. array.push(arguments[i][j]);
  789. } else {
  790. array.push(arguments[i]);
  791. }
  792. }
  793. return array;
  794. };
  795. }
  796. Object.extend(Number.prototype, {
  797. toColorPart: function() {
  798. return this.toPaddedString(2, 16);
  799. },
  800. succ: function() {
  801. return this + 1;
  802. },
  803. times: function(iterator) {
  804. $R(0, this, true).each(iterator);
  805. return this;
  806. },
  807. toPaddedString: function(length, radix) {
  808. var string = this.toString(radix || 10);
  809. return '0'.times(length - string.length) + string;
  810. },
  811. toJSON: function() {
  812. return isFinite(this) ? this.toString() : 'null';
  813. }
  814. });
  815. $w('abs round ceil floor').each(function(method){
  816. Number.prototype[method] = Math[method].methodize();
  817. });
  818. function $H(object) {
  819. return new Hash(object);
  820. };
  821. var Hash = Class.create(Enumerable, (function() {
  822. function toQueryPair(key, value) {
  823. if (Object.isUndefined(value)) return key;
  824. return key + '=' + encodeURIComponent(String.interpret(value));
  825. }
  826. return {
  827. initialize: function(object) {
  828. this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
  829. },
  830. _each: function(iterator) {
  831. for (var key in this._object) {
  832. var value = this._object[key], pair = [key, value];
  833. pair.key = key;
  834. pair.value = value;
  835. iterator(pair);
  836. }
  837. },
  838. set: function(key, value) {
  839. return this._object[key] = value;
  840. },
  841. get: function(key) {
  842. return this._object[key];
  843. },
  844. unset: function(key) {
  845. var value = this._object[key];
  846. delete this._object[key];
  847. return value;
  848. },
  849. toObject: function() {
  850. return Object.clone(this._object);
  851. },
  852. keys: function() {
  853. return this.pluck('key');
  854. },
  855. values: function() {
  856. return this.pluck('value');
  857. },
  858. index: function(value) {
  859. var match = this.detect(function(pair) {
  860. return pair.value === value;
  861. });
  862. return match && match.key;
  863. },
  864. merge: function(object) {
  865. return this.clone().update(object);
  866. },
  867. update: function(object) {
  868. return new Hash(object).inject(this, function(result, pair) {
  869. result.set(pair.key, pair.value);
  870. return result;
  871. });
  872. },
  873. toQueryString: function() {
  874. return this.map(function(pair) {
  875. var key = encodeURIComponent(pair.key), values = pair.value;
  876. if (values && typeof values == 'object') {
  877. if (Object.isArray(values))
  878. return values.map(toQueryPair.curry(key)).join('&');
  879. }
  880. return toQueryPair(key, values);
  881. }).join('&');
  882. },
  883. inspect: function() {
  884. return '#<Hash:{' + this.map(function(pair) {
  885. return pair.map(Object.inspect).join(': ');
  886. }).join(', ') + '}>';
  887. },
  888. toJSON: function() {
  889. return Object.toJSON(this.toObject());
  890. },
  891. clone: function() {
  892. return new Hash(this);
  893. }
  894. }
  895. })());
  896. Hash.prototype.toTemplateReplacements = Hash.prototype.toObject;
  897. Hash.from = $H;
  898. var ObjectRange = Class.create(Enumerable, {
  899. initialize: function(start, end, exclusive) {
  900. this.start = start;
  901. this.end = end;
  902. this.exclusive = exclusive;
  903. },
  904. _each: function(iterator) {
  905. var value = this.start;
  906. while (this.include(value)) {
  907. iterator(value);
  908. value = value.succ();
  909. }
  910. },
  911. include: function(value) {
  912. if (value < this.start)
  913. return false;
  914. if (this.exclusive)
  915. return value < this.end;
  916. return value <= this.end;
  917. }
  918. });
  919. var $R = function(start, end, exclusive) {
  920. return new ObjectRange(start, end, exclusive);
  921. };
  922. var Ajax = {
  923. getTransport: function() {
  924. return Try.these(
  925. function() {return new XMLHttpRequest()},
  926. function() {return new ActiveXObject('Msxml2.XMLHTTP')},
  927. function() {return new ActiveXObject('Microsoft.XMLHTTP')}
  928. ) || false;
  929. },
  930. activeRequestCount: 0
  931. };
  932. Ajax.Responders = {
  933. responders: [],
  934. _each: function(iterator) {
  935. this.responders._each(iterator);
  936. },
  937. register: function(responder) {
  938. if (!this.include(responder))
  939. this.responders.push(responder);
  940. },
  941. unregister: function(responder) {
  942. this.responders = this.responders.without(responder);
  943. },
  944. dispatch: function(callback, request, transport, json) {
  945. this.each(function(responder) {
  946. if (Object.isFunction(responder[callback])) {
  947. try {
  948. responder[callback].apply(responder, [request, transport, json]);
  949. } catch (e) { }
  950. }
  951. });
  952. }
  953. };
  954. Object.extend(Ajax.Responders, Enumerable);
  955. Ajax.Responders.register({
  956. onCreate: function() { Ajax.activeRequestCount++ },
  957. onComplete: function() { Ajax.activeRequestCount-- }
  958. });
  959. Ajax.Base = Class.create({
  960. initialize: function(options) {
  961. this.options = {
  962. method: 'post',
  963. asynchronous: true,
  964. contentType: 'application/x-www-form-urlencoded',
  965. encoding: 'UTF-8',
  966. parameters: '',
  967. evalJSON: true,
  968. evalJS: true
  969. };
  970. Object.extend(this.options, options || { });
  971. this.options.method = this.options.method.toLowerCase();
  972. if (Object.isString(this.options.parameters))
  973. this.options.parameters = this.options.parameters.toQueryParams();
  974. else if (Object.isHash(this.options.parameters))
  975. this.options.parameters = this.options.parameters.toObject();
  976. }
  977. });
  978. Ajax.Request = Class.create(Ajax.Base, {
  979. _complete: false,
  980. initialize: function($super, url, options) {
  981. $super(options);
  982. this.transport = Ajax.getTransport();
  983. this.request(url);
  984. },
  985. request: function(url) {
  986. this.url = url;
  987. this.method = this.options.method;
  988. var params = Object.clone(this.options.parameters);
  989. if (!['get', 'post'].include(this.method)) {
  990. // simulate other verbs over post
  991. params['_method'] = this.method;
  992. this.method = 'post';
  993. }
  994. this.parameters = params;
  995. if (params = Object.toQueryString(params)) {
  996. // when GET, append parameters to URL
  997. if (this.method == 'get')
  998. this.url += (this.url.include('?') ? '&' : '?') + params;
  999. else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
  1000. params += '&_=';
  1001. }
  1002. try {
  1003. var response = new Ajax.Response(this);
  1004. if (this.options.onCreate) this.options.onCreate(response);
  1005. Ajax.Responders.dispatch('onCreate', this, response);
  1006. this.transport.open(this.method.toUpperCase(), this.url,
  1007. this.options.asynchronous);
  1008. if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
  1009. this.transport.onreadystatechange = this.onStateChange.bind(this);
  1010. this.setRequestHeaders();
  1011. this.body = this.method == 'post' ? (this.options.postBody || params) : null;
  1012. this.transport.send(this.body);
  1013. /* Force Firefox to handle ready state 4 for synchronous requests */
  1014. if (!this.options.asynchronous && this.transport.overrideMimeType)
  1015. this.onStateChange();
  1016. }
  1017. catch (e) {
  1018. this.dispatchException(e);
  1019. }
  1020. },
  1021. onStateChange: function() {
  1022. var readyState = this.transport.readyState;
  1023. if (readyState > 1 && !((readyState == 4) && this._complete))
  1024. this.respondToReadyState(this.transport.readyState);
  1025. },
  1026. setRequestHeaders: function() {
  1027. var headers = {
  1028. 'X-Requested-With': 'XMLHttpRequest',
  1029. 'X-Prototype-Version': Prototype.Version,
  1030. 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
  1031. };
  1032. if (this.method == 'post') {
  1033. headers['Content-type'] = this.options.contentType +
  1034. (this.options.encoding ? '; charset=' + this.options.encoding : '');
  1035. /* Force "Connection: close" for older Mozilla browsers to work
  1036. * around a bug where XMLHttpRequest sends an incorrect
  1037. * Content-length header. See Mozilla Bugzilla #246651.
  1038. */
  1039. if (this.transport.overrideMimeType &&
  1040. (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
  1041. headers['Connection'] = 'close';
  1042. }
  1043. // user-defined headers
  1044. if (typeof this.options.requestHeaders == 'object') {
  1045. var extras = this.options.requestHeaders;
  1046. if (Object.isFunction(extras.push))
  1047. for (var i = 0, length = extras.length; i < length; i += 2)
  1048. headers[extras[i]] = extras[i+1];
  1049. else
  1050. $H(extras).each(function(pair) { headers[pair.key] = pair.value });
  1051. }
  1052. for (var name in headers)
  1053. this.transport.setRequestHeader(name, headers[name]);
  1054. },
  1055. success: function() {
  1056. var status = this.getStatus();
  1057. return !status || (status >= 200 && status < 300);
  1058. },
  1059. getStatus: function() {
  1060. try {
  1061. return this.transport.status || 0;
  1062. } catch (e) { return 0 }
  1063. },
  1064. respondToReadyState: function(readyState) {
  1065. var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
  1066. if (state == 'Complete') {
  1067. try {
  1068. this._complete = true;
  1069. (this.options['on' + response.status]
  1070. || this.options['on' + (this.success() ? 'Success' : 'Failure')]
  1071. || Prototype.emptyFunction)(response, response.headerJSON);
  1072. } catch (e) {
  1073. this.dispatchException(e);
  1074. }
  1075. var contentType = response.getHeader('Content-type');
  1076. if (this.options.evalJS == 'force'
  1077. || (this.options.evalJS && this.isSameOrigin() && contentType
  1078. && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
  1079. this.evalResponse();
  1080. }
  1081. try {
  1082. (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
  1083. Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
  1084. } catch (e) {
  1085. this.dispatchException(e);
  1086. }
  1087. if (state == 'Complete') {
  1088. // avoid memory leak in MSIE: clean up
  1089. this.transport.onreadystatechange = Prototype.emptyFunction;
  1090. }
  1091. },
  1092. isSameOrigin: function() {
  1093. var m = this.url.match(/^\s*https?:\/\/[^\/]*/);
  1094. return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({
  1095. protocol: location.protocol,
  1096. domain: document.domain,
  1097. port: location.port ? ':' + location.port : ''
  1098. }));
  1099. },
  1100. getHeader: function(name) {
  1101. try {
  1102. return this.transport.getResponseHeader(name) || null;
  1103. } catch (e) { return null }
  1104. },
  1105. evalResponse: function() {
  1106. try {
  1107. return eval((this.transport.responseText || '').unfilterJSON());
  1108. } catch (e) {
  1109. this.dispatchException(e);
  1110. }
  1111. },
  1112. dispatchException: function(exception) {
  1113. (this.options.onException || Prototype.emptyFunction)(this, exception);
  1114. Ajax.Responders.dispatch('onException', this, exception);
  1115. }
  1116. });
  1117. Ajax.Request.Events =
  1118. ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
  1119. Ajax.Response = Class.create({
  1120. initialize: function(request){
  1121. this.request = request;
  1122. var transport = this.transport = request.transport,
  1123. readyState = this.readyState = transport.readyState;
  1124. if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) {
  1125. this.status = this.getStatus();
  1126. this.statusText = this.getStatusText();
  1127. this.responseText = String.interpret(transport.responseText);
  1128. this.headerJSON = this._getHeaderJSON();
  1129. }
  1130. if(readyState == 4) {
  1131. var xml = transport.responseXML;
  1132. this.responseXML = Object.isUndefined(xml) ? null : xml;
  1133. this.responseJSON = this._getResponseJSON();
  1134. }
  1135. },
  1136. status: 0,
  1137. statusText: '',
  1138. getStatus: Ajax.Request.prototype.getStatus,
  1139. getStatusText: function() {
  1140. try {
  1141. return this.transport.statusText || '';
  1142. } catch (e) { return '' }
  1143. },
  1144. getHeader: Ajax.Request.prototype.getHeader,
  1145. getAllHeaders: function() {
  1146. try {
  1147. return this.getAllResponseHeaders();
  1148. } catch (e) { return null }
  1149. },
  1150. getResponseHeader: function(name) {
  1151. return this.transport.getResponseHeader(name);
  1152. },
  1153. getAllResponseHeaders: function() {
  1154. return this.transport.getAllResponseHeaders();
  1155. },
  1156. _getHeaderJSON: function() {
  1157. var json = this.getHeader('X-JSON');
  1158. if (!json) return null;
  1159. json = decodeURIComponent(escape(json));
  1160. try {
  1161. return json.evalJSON(this.request.options.sanitizeJSON ||
  1162. !this.request.isSameOrigin());
  1163. } catch (e) {
  1164. this.request.dispatchException(e);
  1165. }
  1166. },
  1167. _getResponseJSON: function() {
  1168. var options = this.request.options;
  1169. if (!options.evalJSON || (options.evalJSON != 'force' &&
  1170. !(this.getHeader('Content-type') || '').include('application/json')) ||
  1171. this.responseText.blank())
  1172. return null;
  1173. try {
  1174. return this.responseText.evalJSON(options.sanitizeJSON ||
  1175. !this.request.isSameOrigin());
  1176. } catch (e) {
  1177. this.request.dispatchException(e);
  1178. }
  1179. }
  1180. });
  1181. Ajax.Updater = Class.create(Ajax.Request, {
  1182. initialize: function($super, container, url, options) {
  1183. this.container = {
  1184. success: (container.success || container),
  1185. failure: (container.failure || (container.success ? null : container))
  1186. };
  1187. options = Object.clone(options);
  1188. var onComplete = options.onComplete;
  1189. options.onComplete = (function(response, json) {
  1190. this.updateContent(response.responseText);
  1191. if (Object.isFunction(onComplete)) onComplete(response, json);
  1192. }).bind(this);
  1193. $super(url, options);
  1194. },
  1195. updateContent: function(responseText) {
  1196. var receiver = this.container[this.success() ? 'success' : 'failure'],
  1197. options = this.options;
  1198. if (!options.evalScripts) responseText = responseText.stripScripts();
  1199. if (receiver = $(receiver)) {
  1200. if (options.insertion) {
  1201. if (Object.isString(options.insertion)) {
  1202. var insertion = { }; insertion[options.insertion] = responseText;
  1203. receiver.insert(insertion);
  1204. }
  1205. else options.insertion(receiver, responseText);
  1206. }
  1207. else receiver.update(responseText);
  1208. }
  1209. }
  1210. });
  1211. Ajax.PeriodicalUpdater = Class.create(Ajax.Base, {
  1212. initialize: function($super, container, url, options) {
  1213. $super(options);
  1214. this.onComplete = this.options.onComplete;
  1215. this.frequency = (this.options.frequency || 2);
  1216. this.decay = (this.options.decay || 1);
  1217. this.updater = { };
  1218. this.container = container;
  1219. this.url = url;
  1220. this.start();
  1221. },
  1222. start: function() {
  1223. this.options.onComplete = this.updateComplete.bind(this);
  1224. this.onTimerEvent();
  1225. },
  1226. stop: function() {
  1227. this.updater.options.onComplete = undefined;
  1228. clearTimeout(this.timer);
  1229. (this.onComplete || Prototype.emptyFunction).apply(this, arguments);
  1230. },
  1231. updateComplete: function(response) {
  1232. if (this.options.decay) {
  1233. this.decay = (response.responseText == this.lastText ?
  1234. this.decay * this.options.decay : 1);
  1235. this.lastText = response.responseText;
  1236. }
  1237. this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency);
  1238. },
  1239. onTimerEvent: function() {
  1240. this.updater = new Ajax.Updater(this.container, this.url, this.options);
  1241. }
  1242. });
  1243. function $(element) {
  1244. if (arguments.length > 1) {
  1245. for (var i = 0, elements = [], length = arguments.length; i < length; i++)
  1246. elements.push($(arguments[i]));
  1247. return elements;
  1248. }
  1249. if (Object.isString(element))
  1250. element = document.getElementById(element);
  1251. return Element.extend(element);
  1252. }
  1253. if (Prototype.BrowserFeatures.XPath) {
  1254. document._getElementsByXPath = function(expression, parentElement) {
  1255. var results = [];
  1256. var query = document.evaluate(expression, $(parentElement) || document,
  1257. null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  1258. for (var i = 0, length = query.snapshotLength; i < length; i++)
  1259. results.push(Element.extend(query.snapshotItem(i)));
  1260. return results;
  1261. };
  1262. }
  1263. /*--------------------------------------------------------------------------*/
  1264. if (!window.Node) var Node = { };
  1265. if (!Node.ELEMENT_NODE) {
  1266. // DOM level 2 ECMAScript Language Binding
  1267. Object.extend(Node, {
  1268. ELEMENT_NODE: 1,
  1269. ATTRIBUTE_NODE: 2,
  1270. TEXT_NODE: 3,
  1271. CDATA_SECTION_NODE: 4,
  1272. ENTITY_REFERENCE_NODE: 5,
  1273. ENTITY_NODE: 6,
  1274. PROCESSING_INSTRUCTION_NODE: 7,
  1275. COMMENT_NODE: 8,
  1276. DOCUMENT_NODE: 9,
  1277. DOCUMENT_TYPE_NODE: 10,
  1278. DOCUMENT_FRAGMENT_NODE: 11,
  1279. NOTATION_NODE: 12
  1280. });
  1281. }
  1282. (function() {
  1283. var element = this.Element;
  1284. this.Element = function(tagName, attributes) {
  1285. attributes = attributes || { };
  1286. tagName = tagName.toLowerCase();
  1287. var cache = Element.cache;
  1288. if (Prototype.Browser.IE && attributes.name) {
  1289. tagName = '<' + tagName + ' name="' + attributes.name + '">';
  1290. delete attributes.name;
  1291. return Element.writeAttribute(document.createElement(tagName), attributes);
  1292. }
  1293. if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName));
  1294. return Element.writeAttribute(cache[tagName].cloneNode(false), attributes);
  1295. };
  1296. Object.extend(this.Element, element || { });
  1297. }).call(window);
  1298. Element.cache = { };
  1299. Element.Methods = {
  1300. visible: function(element) {
  1301. return $(element).style.display != 'none';
  1302. },
  1303. toggle: function(element) {
  1304. element = $(element);
  1305. Element[Element.visible(element) ? 'hide' : 'show'](element);
  1306. return element;
  1307. },
  1308. hide: function(element) {
  1309. $(element).style.display = 'none';
  1310. return element;
  1311. },
  1312. show: function(element) {
  1313. $(element).style.display = '';
  1314. return element;
  1315. },
  1316. remove: function(element) {
  1317. element = $(element);
  1318. element.parentNode.removeChild(element);
  1319. return element;
  1320. },
  1321. update: function(element, content) {
  1322. element = $(element);
  1323. if (content && content.toElement) content = content.toElement();
  1324. if (Object.isElement(content)) return element.update().insert(content);
  1325. content = Object.toHTML(content);
  1326. element.innerHTML = content.stripScripts();
  1327. content.evalScripts.bind(content).defer();
  1328. return element;
  1329. },
  1330. replace: function(element, content) {
  1331. element = $(element);
  1332. if (content && content.toElement) content = content.toElement();
  1333. else if (!Object.isElement(content)) {
  1334. content = Object.toHTML(content);
  1335. var range = element.ownerDocument.createRange();
  1336. range.selectNode(element);
  1337. content.evalScripts.bind(content).defer();
  1338. content = range.createContextualFragment(content.stripScripts());
  1339. }
  1340. element.parentNode.replaceChild(content, element);
  1341. return element;
  1342. },
  1343. insert: function(element, insertions) {
  1344. element = $(element);
  1345. if (Object.isString(insertions) || Object.isNumber(insertions) ||
  1346. Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML)))
  1347. insertions = {bottom:insertions};
  1348. var content, insert, tagName, childNodes;
  1349. for (var position in insertions) {
  1350. content = insertions[position];
  1351. position = position.toLowerCase();
  1352. insert = Element._insertionTranslations[position];
  1353. if (content && content.toElement) content = content.toElement();
  1354. if (Object.isElement(content)) {
  1355. insert(element, content);
  1356. continue;
  1357. }
  1358. content = Object.toHTML(content);
  1359. tagName = ((position == 'before' || position == 'after')
  1360. ? element.parentNode : element).tagName.toUpperCase();
  1361. childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
  1362. if (position == 'top' || position == 'after') childNodes.reverse();
  1363. childNodes.each(insert.curry(element));
  1364. content.evalScripts.bind(content).defer();
  1365. }
  1366. return element;
  1367. },
  1368. wrap: function(element, wrapper, attributes) {
  1369. element = $(element);
  1370. if (Object.isElement(wrapper))
  1371. $(wrapper).writeAttribute(attributes || { });
  1372. else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes);
  1373. else wrapper = new Element('div', wrapper);
  1374. if (element.parentNode)
  1375. element.parentNode.replaceChild(wrapper, element);
  1376. wrapper.appendChild(element);
  1377. return wrapper;
  1378. },
  1379. inspect: function(element) {
  1380. element = $(element);
  1381. var result = '<' + element.tagName.toLowerCase();
  1382. $H({'id': 'id', 'className': 'class'}).each(function(pair) {
  1383. var property = pair.first(), attribute = pair.last();
  1384. var value = (element[property] || '').toString();
  1385. if (value) result += ' ' + attribute + '=' + value.inspect(true);
  1386. });
  1387. return result + '>';
  1388. },
  1389. recursivelyCollect: function(element, property) {
  1390. element = $(element);
  1391. var elements = [];
  1392. while (element = element[property])
  1393. if (element.nodeType == 1)
  1394. elements.push(Element.extend(element));
  1395. return elements;
  1396. },
  1397. ancestors: function(element) {
  1398. return $(element).recursivelyCollect('parentNode');
  1399. },
  1400. descendants: function(element) {
  1401. return $(element).select("*");
  1402. },
  1403. firstDescendant: function(element) {
  1404. element = $(element).firstChild;
  1405. while (element && element.nodeType != 1) element = element.nextSibling;
  1406. return $(element);
  1407. },
  1408. immediateDescendants: function(element) {
  1409. if (!(element = $(element).firstChild)) return [];
  1410. while (element && element.nodeType != 1) element = element.nextSibling;
  1411. if (element) return [element].concat($(element).nextSiblings());
  1412. return [];
  1413. },
  1414. previousSiblings: function(element) {
  1415. return $(element).recursivelyCollect('previousSibling');
  1416. },
  1417. nextSiblings: function(element) {
  1418. return $(element).recursivelyCollect('nextSibling');
  1419. },
  1420. siblings: function(element) {
  1421. element = $(element);
  1422. return element.previousSiblings().reverse().concat(element.nextSiblings());
  1423. },
  1424. match: function(element, selector) {
  1425. if (Object.isString(selector))
  1426. selector = new Selector(selector);
  1427. return selector.match($(element));
  1428. },
  1429. up: function(element, expression, index) {
  1430. element = $(element);
  1431. if (arguments.length == 1) return $(element.parentNode);
  1432. var ancestors = element.ancestors();
  1433. return Object.isNumber(expression) ? ancestors[expression] :
  1434. Selector.findElement(ancestors, expression, index);
  1435. },
  1436. down: function(element, expression, index) {
  1437. element = $(element);
  1438. if (arguments.length == 1) return element.firstDescendant();
  1439. return Object.isNumber(expression) ? element.descendants()[expression] :
  1440. element.select(expression)[index || 0];
  1441. },
  1442. previous: function(element, expression, index) {
  1443. element = $(element);
  1444. if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element));
  1445. var previousSiblings = element.previousSiblings();
  1446. return Object.isNumber(expression) ? previousSiblings[expression] :
  1447. Selector.findElement(previousSiblings, expression, index);
  1448. },
  1449. next: function(element, expression, index) {
  1450. element = $(element);
  1451. if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element));
  1452. var nextSiblings = element.nextSiblings();
  1453. return Object.isNumber(expression)

Large files files are truncated, but you can click here to view the full file