PageRenderTime 91ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 2ms

/examples/AudioGraphOnRails/public/javascripts/all.js

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

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