PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jruby-1.1.6RC1/lib/ruby/gems/1.8/gems/rspec-1.1.11/story_server/prototype/javascripts/prototype.js

https://bitbucket.org/nicksieger/advent-jruby
JavaScript | 4140 lines | 3592 code | 478 blank | 70 comment | 634 complexity | b387adf2c8f4629e8d50e8740dbea5e9 MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON

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

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

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