annotate awesome_js/prototype.js @ 106:5b0753c6f34d laserkard tip

updated to newest compojure and a simpler syntax
author Robert McIntyre <rlm@mit.edu>
date Mon, 27 Sep 2010 20:22:58 -0400
parents 021a9ab1ed5b
children
rev   line source
rlm@37 1 /* Prototype JavaScript framework, version 1.6.1
rlm@37 2 * (c) 2005-2009 Sam Stephenson
rlm@37 3 *
rlm@37 4 * Prototype is freely distributable under the terms of an MIT-style license.
rlm@37 5 * For details, see the Prototype web site: http://www.prototypejs.org/
rlm@37 6 *
rlm@37 7 *--------------------------------------------------------------------------*/
rlm@37 8
rlm@37 9 var Prototype = {
rlm@37 10 Version: '1.6.1',
rlm@37 11
rlm@37 12 Browser: (function(){
rlm@37 13 var ua = navigator.userAgent;
rlm@37 14 var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
rlm@37 15 return {
rlm@37 16 IE: !!window.attachEvent && !isOpera,
rlm@37 17 Opera: isOpera,
rlm@37 18 WebKit: ua.indexOf('AppleWebKit/') > -1,
rlm@37 19 Gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,
rlm@37 20 MobileSafari: /Apple.*Mobile.*Safari/.test(ua)
rlm@37 21 }
rlm@37 22 })(),
rlm@37 23
rlm@37 24 BrowserFeatures: {
rlm@37 25 XPath: !!document.evaluate,
rlm@37 26 SelectorsAPI: !!document.querySelector,
rlm@37 27 ElementExtensions: (function() {
rlm@37 28 var constructor = window.Element || window.HTMLElement;
rlm@37 29 return !!(constructor && constructor.prototype);
rlm@37 30 })(),
rlm@37 31 SpecificElementExtensions: (function() {
rlm@37 32 if (typeof window.HTMLDivElement !== 'undefined')
rlm@37 33 return true;
rlm@37 34
rlm@37 35 var div = document.createElement('div');
rlm@37 36 var form = document.createElement('form');
rlm@37 37 var isSupported = false;
rlm@37 38
rlm@37 39 if (div['__proto__'] && (div['__proto__'] !== form['__proto__'])) {
rlm@37 40 isSupported = true;
rlm@37 41 }
rlm@37 42
rlm@37 43 div = form = null;
rlm@37 44
rlm@37 45 return isSupported;
rlm@37 46 })()
rlm@37 47 },
rlm@37 48
rlm@37 49 ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>',
rlm@37 50 JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,
rlm@37 51
rlm@37 52 emptyFunction: function() { },
rlm@37 53 K: function(x) { return x }
rlm@37 54 };
rlm@37 55
rlm@37 56 if (Prototype.Browser.MobileSafari)
rlm@37 57 Prototype.BrowserFeatures.SpecificElementExtensions = false;
rlm@37 58
rlm@37 59
rlm@37 60 var Abstract = { };
rlm@37 61
rlm@37 62
rlm@37 63 var Try = {
rlm@37 64 these: function() {
rlm@37 65 var returnValue;
rlm@37 66
rlm@37 67 for (var i = 0, length = arguments.length; i < length; i++) {
rlm@37 68 var lambda = arguments[i];
rlm@37 69 try {
rlm@37 70 returnValue = lambda();
rlm@37 71 break;
rlm@37 72 } catch (e) { }
rlm@37 73 }
rlm@37 74
rlm@37 75 return returnValue;
rlm@37 76 }
rlm@37 77 };
rlm@37 78
rlm@37 79 /* Based on Alex Arnell's inheritance implementation. */
rlm@37 80
rlm@37 81 var Class = (function() {
rlm@37 82 function subclass() {};
rlm@37 83 function create() {
rlm@37 84 var parent = null, properties = $A(arguments);
rlm@37 85 if (Object.isFunction(properties[0]))
rlm@37 86 parent = properties.shift();
rlm@37 87
rlm@37 88 function klass() {
rlm@37 89 this.initialize.apply(this, arguments);
rlm@37 90 }
rlm@37 91
rlm@37 92 Object.extend(klass, Class.Methods);
rlm@37 93 klass.superclass = parent;
rlm@37 94 klass.subclasses = [];
rlm@37 95
rlm@37 96 if (parent) {
rlm@37 97 subclass.prototype = parent.prototype;
rlm@37 98 klass.prototype = new subclass;
rlm@37 99 parent.subclasses.push(klass);
rlm@37 100 }
rlm@37 101
rlm@37 102 for (var i = 0; i < properties.length; i++)
rlm@37 103 klass.addMethods(properties[i]);
rlm@37 104
rlm@37 105 if (!klass.prototype.initialize)
rlm@37 106 klass.prototype.initialize = Prototype.emptyFunction;
rlm@37 107
rlm@37 108 klass.prototype.constructor = klass;
rlm@37 109 return klass;
rlm@37 110 }
rlm@37 111
rlm@37 112 function addMethods(source) {
rlm@37 113 var ancestor = this.superclass && this.superclass.prototype;
rlm@37 114 var properties = Object.keys(source);
rlm@37 115
rlm@37 116 if (!Object.keys({ toString: true }).length) {
rlm@37 117 if (source.toString != Object.prototype.toString)
rlm@37 118 properties.push("toString");
rlm@37 119 if (source.valueOf != Object.prototype.valueOf)
rlm@37 120 properties.push("valueOf");
rlm@37 121 }
rlm@37 122
rlm@37 123 for (var i = 0, length = properties.length; i < length; i++) {
rlm@37 124 var property = properties[i], value = source[property];
rlm@37 125 if (ancestor && Object.isFunction(value) &&
rlm@37 126 value.argumentNames().first() == "$super") {
rlm@37 127 var method = value;
rlm@37 128 value = (function(m) {
rlm@37 129 return function() { return ancestor[m].apply(this, arguments); };
rlm@37 130 })(property).wrap(method);
rlm@37 131
rlm@37 132 value.valueOf = method.valueOf.bind(method);
rlm@37 133 value.toString = method.toString.bind(method);
rlm@37 134 }
rlm@37 135 this.prototype[property] = value;
rlm@37 136 }
rlm@37 137
rlm@37 138 return this;
rlm@37 139 }
rlm@37 140
rlm@37 141 return {
rlm@37 142 create: create,
rlm@37 143 Methods: {
rlm@37 144 addMethods: addMethods
rlm@37 145 }
rlm@37 146 };
rlm@37 147 })();
rlm@37 148 (function() {
rlm@37 149
rlm@37 150 var _toString = Object.prototype.toString;
rlm@37 151
rlm@37 152 function extend(destination, source) {
rlm@37 153 for (var property in source)
rlm@37 154 destination[property] = source[property];
rlm@37 155 return destination;
rlm@37 156 }
rlm@37 157
rlm@37 158 function inspect(object) {
rlm@37 159 try {
rlm@37 160 if (isUndefined(object)) return 'undefined';
rlm@37 161 if (object === null) return 'null';
rlm@37 162 return object.inspect ? object.inspect() : String(object);
rlm@37 163 } catch (e) {
rlm@37 164 if (e instanceof RangeError) return '...';
rlm@37 165 throw e;
rlm@37 166 }
rlm@37 167 }
rlm@37 168
rlm@37 169 function toJSON(object) {
rlm@37 170 var type = typeof object;
rlm@37 171 switch (type) {
rlm@37 172 case 'undefined':
rlm@37 173 case 'function':
rlm@37 174 case 'unknown': return;
rlm@37 175 case 'boolean': return object.toString();
rlm@37 176 }
rlm@37 177
rlm@37 178 if (object === null) return 'null';
rlm@37 179 if (object.toJSON) return object.toJSON();
rlm@37 180 if (isElement(object)) return;
rlm@37 181
rlm@37 182 var results = [];
rlm@37 183 for (var property in object) {
rlm@37 184 var value = toJSON(object[property]);
rlm@37 185 if (!isUndefined(value))
rlm@37 186 results.push(property.toJSON() + ': ' + value);
rlm@37 187 }
rlm@37 188
rlm@37 189 return '{' + results.join(', ') + '}';
rlm@37 190 }
rlm@37 191
rlm@37 192 function toQueryString(object) {
rlm@37 193 return $H(object).toQueryString();
rlm@37 194 }
rlm@37 195
rlm@37 196 function toHTML(object) {
rlm@37 197 return object && object.toHTML ? object.toHTML() : String.interpret(object);
rlm@37 198 }
rlm@37 199
rlm@37 200 function keys(object) {
rlm@37 201 var results = [];
rlm@37 202 for (var property in object)
rlm@37 203 results.push(property);
rlm@37 204 return results;
rlm@37 205 }
rlm@37 206
rlm@37 207 function values(object) {
rlm@37 208 var results = [];
rlm@37 209 for (var property in object)
rlm@37 210 results.push(object[property]);
rlm@37 211 return results;
rlm@37 212 }
rlm@37 213
rlm@37 214 function clone(object) {
rlm@37 215 return extend({ }, object);
rlm@37 216 }
rlm@37 217
rlm@37 218 function isElement(object) {
rlm@37 219 return !!(object && object.nodeType == 1);
rlm@37 220 }
rlm@37 221
rlm@37 222 function isArray(object) {
rlm@37 223 return _toString.call(object) == "[object Array]";
rlm@37 224 }
rlm@37 225
rlm@37 226
rlm@37 227 function isHash(object) {
rlm@37 228 return object instanceof Hash;
rlm@37 229 }
rlm@37 230
rlm@37 231 function isFunction(object) {
rlm@37 232 return typeof object === "function";
rlm@37 233 }
rlm@37 234
rlm@37 235 function isString(object) {
rlm@37 236 return _toString.call(object) == "[object String]";
rlm@37 237 }
rlm@37 238
rlm@37 239 function isNumber(object) {
rlm@37 240 return _toString.call(object) == "[object Number]";
rlm@37 241 }
rlm@37 242
rlm@37 243 function isUndefined(object) {
rlm@37 244 return typeof object === "undefined";
rlm@37 245 }
rlm@37 246
rlm@37 247 extend(Object, {
rlm@37 248 extend: extend,
rlm@37 249 inspect: inspect,
rlm@37 250 toJSON: toJSON,
rlm@37 251 toQueryString: toQueryString,
rlm@37 252 toHTML: toHTML,
rlm@37 253 keys: keys,
rlm@37 254 values: values,
rlm@37 255 clone: clone,
rlm@37 256 isElement: isElement,
rlm@37 257 isArray: isArray,
rlm@37 258 isHash: isHash,
rlm@37 259 isFunction: isFunction,
rlm@37 260 isString: isString,
rlm@37 261 isNumber: isNumber,
rlm@37 262 isUndefined: isUndefined
rlm@37 263 });
rlm@37 264 })();
rlm@37 265 Object.extend(Function.prototype, (function() {
rlm@37 266 var slice = Array.prototype.slice;
rlm@37 267
rlm@37 268 function update(array, args) {
rlm@37 269 var arrayLength = array.length, length = args.length;
rlm@37 270 while (length--) array[arrayLength + length] = args[length];
rlm@37 271 return array;
rlm@37 272 }
rlm@37 273
rlm@37 274 function merge(array, args) {
rlm@37 275 array = slice.call(array, 0);
rlm@37 276 return update(array, args);
rlm@37 277 }
rlm@37 278
rlm@37 279 function argumentNames() {
rlm@37 280 var names = this.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
rlm@37 281 .replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
rlm@37 282 .replace(/\s+/g, '').split(',');
rlm@37 283 return names.length == 1 && !names[0] ? [] : names;
rlm@37 284 }
rlm@37 285
rlm@37 286 function bind(context) {
rlm@37 287 if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
rlm@37 288 var __method = this, args = slice.call(arguments, 1);
rlm@37 289 return function() {
rlm@37 290 var a = merge(args, arguments);
rlm@37 291 return __method.apply(context, a);
rlm@37 292 }
rlm@37 293 }
rlm@37 294
rlm@37 295 function bindAsEventListener(context) {
rlm@37 296 var __method = this, args = slice.call(arguments, 1);
rlm@37 297 return function(event) {
rlm@37 298 var a = update([event || window.event], args);
rlm@37 299 return __method.apply(context, a);
rlm@37 300 }
rlm@37 301 }
rlm@37 302
rlm@37 303 function curry() {
rlm@37 304 if (!arguments.length) return this;
rlm@37 305 var __method = this, args = slice.call(arguments, 0);
rlm@37 306 return function() {
rlm@37 307 var a = merge(args, arguments);
rlm@37 308 return __method.apply(this, a);
rlm@37 309 }
rlm@37 310 }
rlm@37 311
rlm@37 312 function delay(timeout) {
rlm@37 313 var __method = this, args = slice.call(arguments, 1);
rlm@37 314 timeout = timeout * 1000
rlm@37 315 return window.setTimeout(function() {
rlm@37 316 return __method.apply(__method, args);
rlm@37 317 }, timeout);
rlm@37 318 }
rlm@37 319
rlm@37 320 function defer() {
rlm@37 321 var args = update([0.01], arguments);
rlm@37 322 return this.delay.apply(this, args);
rlm@37 323 }
rlm@37 324
rlm@37 325 function wrap(wrapper) {
rlm@37 326 var __method = this;
rlm@37 327 return function() {
rlm@37 328 var a = update([__method.bind(this)], arguments);
rlm@37 329 return wrapper.apply(this, a);
rlm@37 330 }
rlm@37 331 }
rlm@37 332
rlm@37 333 function methodize() {
rlm@37 334 if (this._methodized) return this._methodized;
rlm@37 335 var __method = this;
rlm@37 336 return this._methodized = function() {
rlm@37 337 var a = update([this], arguments);
rlm@37 338 return __method.apply(null, a);
rlm@37 339 };
rlm@37 340 }
rlm@37 341
rlm@37 342 return {
rlm@37 343 argumentNames: argumentNames,
rlm@37 344 bind: bind,
rlm@37 345 bindAsEventListener: bindAsEventListener,
rlm@37 346 curry: curry,
rlm@37 347 delay: delay,
rlm@37 348 defer: defer,
rlm@37 349 wrap: wrap,
rlm@37 350 methodize: methodize
rlm@37 351 }
rlm@37 352 })());
rlm@37 353
rlm@37 354
rlm@37 355 Date.prototype.toJSON = function() {
rlm@37 356 return '"' + this.getUTCFullYear() + '-' +
rlm@37 357 (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
rlm@37 358 this.getUTCDate().toPaddedString(2) + 'T' +
rlm@37 359 this.getUTCHours().toPaddedString(2) + ':' +
rlm@37 360 this.getUTCMinutes().toPaddedString(2) + ':' +
rlm@37 361 this.getUTCSeconds().toPaddedString(2) + 'Z"';
rlm@37 362 };
rlm@37 363
rlm@37 364
rlm@37 365 RegExp.prototype.match = RegExp.prototype.test;
rlm@37 366
rlm@37 367 RegExp.escape = function(str) {
rlm@37 368 return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
rlm@37 369 };
rlm@37 370 var PeriodicalExecuter = Class.create({
rlm@37 371 initialize: function(callback, frequency) {
rlm@37 372 this.callback = callback;
rlm@37 373 this.frequency = frequency;
rlm@37 374 this.currentlyExecuting = false;
rlm@37 375
rlm@37 376 this.registerCallback();
rlm@37 377 },
rlm@37 378
rlm@37 379 registerCallback: function() {
rlm@37 380 this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
rlm@37 381 },
rlm@37 382
rlm@37 383 execute: function() {
rlm@37 384 this.callback(this);
rlm@37 385 },
rlm@37 386
rlm@37 387 stop: function() {
rlm@37 388 if (!this.timer) return;
rlm@37 389 clearInterval(this.timer);
rlm@37 390 this.timer = null;
rlm@37 391 },
rlm@37 392
rlm@37 393 onTimerEvent: function() {
rlm@37 394 if (!this.currentlyExecuting) {
rlm@37 395 try {
rlm@37 396 this.currentlyExecuting = true;
rlm@37 397 this.execute();
rlm@37 398 this.currentlyExecuting = false;
rlm@37 399 } catch(e) {
rlm@37 400 this.currentlyExecuting = false;
rlm@37 401 throw e;
rlm@37 402 }
rlm@37 403 }
rlm@37 404 }
rlm@37 405 });
rlm@37 406 Object.extend(String, {
rlm@37 407 interpret: function(value) {
rlm@37 408 return value == null ? '' : String(value);
rlm@37 409 },
rlm@37 410 specialChar: {
rlm@37 411 '\b': '\\b',
rlm@37 412 '\t': '\\t',
rlm@37 413 '\n': '\\n',
rlm@37 414 '\f': '\\f',
rlm@37 415 '\r': '\\r',
rlm@37 416 '\\': '\\\\'
rlm@37 417 }
rlm@37 418 });
rlm@37 419
rlm@37 420 Object.extend(String.prototype, (function() {
rlm@37 421
rlm@37 422 function prepareReplacement(replacement) {
rlm@37 423 if (Object.isFunction(replacement)) return replacement;
rlm@37 424 var template = new Template(replacement);
rlm@37 425 return function(match) { return template.evaluate(match) };
rlm@37 426 }
rlm@37 427
rlm@37 428 function gsub(pattern, replacement) {
rlm@37 429 var result = '', source = this, match;
rlm@37 430 replacement = prepareReplacement(replacement);
rlm@37 431
rlm@37 432 if (Object.isString(pattern))
rlm@37 433 pattern = RegExp.escape(pattern);
rlm@37 434
rlm@37 435 if (!(pattern.length || pattern.source)) {
rlm@37 436 replacement = replacement('');
rlm@37 437 return replacement + source.split('').join(replacement) + replacement;
rlm@37 438 }
rlm@37 439
rlm@37 440 while (source.length > 0) {
rlm@37 441 if (match = source.match(pattern)) {
rlm@37 442 result += source.slice(0, match.index);
rlm@37 443 result += String.interpret(replacement(match));
rlm@37 444 source = source.slice(match.index + match[0].length);
rlm@37 445 } else {
rlm@37 446 result += source, source = '';
rlm@37 447 }
rlm@37 448 }
rlm@37 449 return result;
rlm@37 450 }
rlm@37 451
rlm@37 452 function sub(pattern, replacement, count) {
rlm@37 453 replacement = prepareReplacement(replacement);
rlm@37 454 count = Object.isUndefined(count) ? 1 : count;
rlm@37 455
rlm@37 456 return this.gsub(pattern, function(match) {
rlm@37 457 if (--count < 0) return match[0];
rlm@37 458 return replacement(match);
rlm@37 459 });
rlm@37 460 }
rlm@37 461
rlm@37 462 function scan(pattern, iterator) {
rlm@37 463 this.gsub(pattern, iterator);
rlm@37 464 return String(this);
rlm@37 465 }
rlm@37 466
rlm@37 467 function truncate(length, truncation) {
rlm@37 468 length = length || 30;
rlm@37 469 truncation = Object.isUndefined(truncation) ? '...' : truncation;
rlm@37 470 return this.length > length ?
rlm@37 471 this.slice(0, length - truncation.length) + truncation : String(this);
rlm@37 472 }
rlm@37 473
rlm@37 474 function strip() {
rlm@37 475 return this.replace(/^\s+/, '').replace(/\s+$/, '');
rlm@37 476 }
rlm@37 477
rlm@37 478 function stripTags() {
rlm@37 479 return this.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi, '');
rlm@37 480 }
rlm@37 481
rlm@37 482 function stripScripts() {
rlm@37 483 return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
rlm@37 484 }
rlm@37 485
rlm@37 486 function extractScripts() {
rlm@37 487 var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
rlm@37 488 var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
rlm@37 489 return (this.match(matchAll) || []).map(function(scriptTag) {
rlm@37 490 return (scriptTag.match(matchOne) || ['', ''])[1];
rlm@37 491 });
rlm@37 492 }
rlm@37 493
rlm@37 494 function evalScripts() {
rlm@37 495 return this.extractScripts().map(function(script) { return eval(script) });
rlm@37 496 }
rlm@37 497
rlm@37 498 function escapeHTML() {
rlm@37 499 return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
rlm@37 500 }
rlm@37 501
rlm@37 502 function unescapeHTML() {
rlm@37 503 return this.stripTags().replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
rlm@37 504 }
rlm@37 505
rlm@37 506
rlm@37 507 function toQueryParams(separator) {
rlm@37 508 var match = this.strip().match(/([^?#]*)(#.*)?$/);
rlm@37 509 if (!match) return { };
rlm@37 510
rlm@37 511 return match[1].split(separator || '&').inject({ }, function(hash, pair) {
rlm@37 512 if ((pair = pair.split('='))[0]) {
rlm@37 513 var key = decodeURIComponent(pair.shift());
rlm@37 514 var value = pair.length > 1 ? pair.join('=') : pair[0];
rlm@37 515 if (value != undefined) value = decodeURIComponent(value);
rlm@37 516
rlm@37 517 if (key in hash) {
rlm@37 518 if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
rlm@37 519 hash[key].push(value);
rlm@37 520 }
rlm@37 521 else hash[key] = value;
rlm@37 522 }
rlm@37 523 return hash;
rlm@37 524 });
rlm@37 525 }
rlm@37 526
rlm@37 527 function toArray() {
rlm@37 528 return this.split('');
rlm@37 529 }
rlm@37 530
rlm@37 531 function succ() {
rlm@37 532 return this.slice(0, this.length - 1) +
rlm@37 533 String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
rlm@37 534 }
rlm@37 535
rlm@37 536 function times(count) {
rlm@37 537 return count < 1 ? '' : new Array(count + 1).join(this);
rlm@37 538 }
rlm@37 539
rlm@37 540 function camelize() {
rlm@37 541 var parts = this.split('-'), len = parts.length;
rlm@37 542 if (len == 1) return parts[0];
rlm@37 543
rlm@37 544 var camelized = this.charAt(0) == '-'
rlm@37 545 ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
rlm@37 546 : parts[0];
rlm@37 547
rlm@37 548 for (var i = 1; i < len; i++)
rlm@37 549 camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
rlm@37 550
rlm@37 551 return camelized;
rlm@37 552 }
rlm@37 553
rlm@37 554 function capitalize() {
rlm@37 555 return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
rlm@37 556 }
rlm@37 557
rlm@37 558 function underscore() {
rlm@37 559 return this.replace(/::/g, '/')
rlm@37 560 .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
rlm@37 561 .replace(/([a-z\d])([A-Z])/g, '$1_$2')
rlm@37 562 .replace(/-/g, '_')
rlm@37 563 .toLowerCase();
rlm@37 564 }
rlm@37 565
rlm@37 566 function dasherize() {
rlm@37 567 return this.replace(/_/g, '-');
rlm@37 568 }
rlm@37 569
rlm@37 570 function inspect(useDoubleQuotes) {
rlm@37 571 var escapedString = this.replace(/[\x00-\x1f\\]/g, function(character) {
rlm@37 572 if (character in String.specialChar) {
rlm@37 573 return String.specialChar[character];
rlm@37 574 }
rlm@37 575 return '\\u00' + character.charCodeAt().toPaddedString(2, 16);
rlm@37 576 });
rlm@37 577 if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
rlm@37 578 return "'" + escapedString.replace(/'/g, '\\\'') + "'";
rlm@37 579 }
rlm@37 580
rlm@37 581 function toJSON() {
rlm@37 582 return this.inspect(true);
rlm@37 583 }
rlm@37 584
rlm@37 585 function unfilterJSON(filter) {
rlm@37 586 return this.replace(filter || Prototype.JSONFilter, '$1');
rlm@37 587 }
rlm@37 588
rlm@37 589 function isJSON() {
rlm@37 590 var str = this;
rlm@37 591 if (str.blank()) return false;
rlm@37 592 str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
rlm@37 593 return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
rlm@37 594 }
rlm@37 595
rlm@37 596 function evalJSON(sanitize) {
rlm@37 597 var json = this.unfilterJSON();
rlm@37 598 try {
rlm@37 599 if (!sanitize || json.isJSON()) return eval('(' + json + ')');
rlm@37 600 } catch (e) { }
rlm@37 601 throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
rlm@37 602 }
rlm@37 603
rlm@37 604 function include(pattern) {
rlm@37 605 return this.indexOf(pattern) > -1;
rlm@37 606 }
rlm@37 607
rlm@37 608 function startsWith(pattern) {
rlm@37 609 return this.indexOf(pattern) === 0;
rlm@37 610 }
rlm@37 611
rlm@37 612 function endsWith(pattern) {
rlm@37 613 var d = this.length - pattern.length;
rlm@37 614 return d >= 0 && this.lastIndexOf(pattern) === d;
rlm@37 615 }
rlm@37 616
rlm@37 617 function empty() {
rlm@37 618 return this == '';
rlm@37 619 }
rlm@37 620
rlm@37 621 function blank() {
rlm@37 622 return /^\s*$/.test(this);
rlm@37 623 }
rlm@37 624
rlm@37 625 function interpolate(object, pattern) {
rlm@37 626 return new Template(this, pattern).evaluate(object);
rlm@37 627 }
rlm@37 628
rlm@37 629 return {
rlm@37 630 gsub: gsub,
rlm@37 631 sub: sub,
rlm@37 632 scan: scan,
rlm@37 633 truncate: truncate,
rlm@37 634 strip: String.prototype.trim ? String.prototype.trim : strip,
rlm@37 635 stripTags: stripTags,
rlm@37 636 stripScripts: stripScripts,
rlm@37 637 extractScripts: extractScripts,
rlm@37 638 evalScripts: evalScripts,
rlm@37 639 escapeHTML: escapeHTML,
rlm@37 640 unescapeHTML: unescapeHTML,
rlm@37 641 toQueryParams: toQueryParams,
rlm@37 642 parseQuery: toQueryParams,
rlm@37 643 toArray: toArray,
rlm@37 644 succ: succ,
rlm@37 645 times: times,
rlm@37 646 camelize: camelize,
rlm@37 647 capitalize: capitalize,
rlm@37 648 underscore: underscore,
rlm@37 649 dasherize: dasherize,
rlm@37 650 inspect: inspect,
rlm@37 651 toJSON: toJSON,
rlm@37 652 unfilterJSON: unfilterJSON,
rlm@37 653 isJSON: isJSON,
rlm@37 654 evalJSON: evalJSON,
rlm@37 655 include: include,
rlm@37 656 startsWith: startsWith,
rlm@37 657 endsWith: endsWith,
rlm@37 658 empty: empty,
rlm@37 659 blank: blank,
rlm@37 660 interpolate: interpolate
rlm@37 661 };
rlm@37 662 })());
rlm@37 663
rlm@37 664 var Template = Class.create({
rlm@37 665 initialize: function(template, pattern) {
rlm@37 666 this.template = template.toString();
rlm@37 667 this.pattern = pattern || Template.Pattern;
rlm@37 668 },
rlm@37 669
rlm@37 670 evaluate: function(object) {
rlm@37 671 if (object && Object.isFunction(object.toTemplateReplacements))
rlm@37 672 object = object.toTemplateReplacements();
rlm@37 673
rlm@37 674 return this.template.gsub(this.pattern, function(match) {
rlm@37 675 if (object == null) return (match[1] + '');
rlm@37 676
rlm@37 677 var before = match[1] || '';
rlm@37 678 if (before == '\\') return match[2];
rlm@37 679
rlm@37 680 var ctx = object, expr = match[3];
rlm@37 681 var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
rlm@37 682 match = pattern.exec(expr);
rlm@37 683 if (match == null) return before;
rlm@37 684
rlm@37 685 while (match != null) {
rlm@37 686 var comp = match[1].startsWith('[') ? match[2].replace(/\\\\]/g, ']') : match[1];
rlm@37 687 ctx = ctx[comp];
rlm@37 688 if (null == ctx || '' == match[3]) break;
rlm@37 689 expr = expr.substring('[' == match[3] ? match[1].length : match[0].length);
rlm@37 690 match = pattern.exec(expr);
rlm@37 691 }
rlm@37 692
rlm@37 693 return before + String.interpret(ctx);
rlm@37 694 });
rlm@37 695 }
rlm@37 696 });
rlm@37 697 Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
rlm@37 698
rlm@37 699 var $break = { };
rlm@37 700
rlm@37 701 var Enumerable = (function() {
rlm@37 702 function each(iterator, context) {
rlm@37 703 var index = 0;
rlm@37 704 try {
rlm@37 705 this._each(function(value) {
rlm@37 706 iterator.call(context, value, index++);
rlm@37 707 });
rlm@37 708 } catch (e) {
rlm@37 709 if (e != $break) throw e;
rlm@37 710 }
rlm@37 711 return this;
rlm@37 712 }
rlm@37 713
rlm@37 714 function eachSlice(number, iterator, context) {
rlm@37 715 var index = -number, slices = [], array = this.toArray();
rlm@37 716 if (number < 1) return array;
rlm@37 717 while ((index += number) < array.length)
rlm@37 718 slices.push(array.slice(index, index+number));
rlm@37 719 return slices.collect(iterator, context);
rlm@37 720 }
rlm@37 721
rlm@37 722 function all(iterator, context) {
rlm@37 723 iterator = iterator || Prototype.K;
rlm@37 724 var result = true;
rlm@37 725 this.each(function(value, index) {
rlm@37 726 result = result && !!iterator.call(context, value, index);
rlm@37 727 if (!result) throw $break;
rlm@37 728 });
rlm@37 729 return result;
rlm@37 730 }
rlm@37 731
rlm@37 732 function any(iterator, context) {
rlm@37 733 iterator = iterator || Prototype.K;
rlm@37 734 var result = false;
rlm@37 735 this.each(function(value, index) {
rlm@37 736 if (result = !!iterator.call(context, value, index))
rlm@37 737 throw $break;
rlm@37 738 });
rlm@37 739 return result;
rlm@37 740 }
rlm@37 741
rlm@37 742 function collect(iterator, context) {
rlm@37 743 iterator = iterator || Prototype.K;
rlm@37 744 var results = [];
rlm@37 745 this.each(function(value, index) {
rlm@37 746 results.push(iterator.call(context, value, index));
rlm@37 747 });
rlm@37 748 return results;
rlm@37 749 }
rlm@37 750
rlm@37 751 function detect(iterator, context) {
rlm@37 752 var result;
rlm@37 753 this.each(function(value, index) {
rlm@37 754 if (iterator.call(context, value, index)) {
rlm@37 755 result = value;
rlm@37 756 throw $break;
rlm@37 757 }
rlm@37 758 });
rlm@37 759 return result;
rlm@37 760 }
rlm@37 761
rlm@37 762 function findAll(iterator, context) {
rlm@37 763 var results = [];
rlm@37 764 this.each(function(value, index) {
rlm@37 765 if (iterator.call(context, value, index))
rlm@37 766 results.push(value);
rlm@37 767 });
rlm@37 768 return results;
rlm@37 769 }
rlm@37 770
rlm@37 771 function grep(filter, iterator, context) {
rlm@37 772 iterator = iterator || Prototype.K;
rlm@37 773 var results = [];
rlm@37 774
rlm@37 775 if (Object.isString(filter))
rlm@37 776 filter = new RegExp(RegExp.escape(filter));
rlm@37 777
rlm@37 778 this.each(function(value, index) {
rlm@37 779 if (filter.match(value))
rlm@37 780 results.push(iterator.call(context, value, index));
rlm@37 781 });
rlm@37 782 return results;
rlm@37 783 }
rlm@37 784
rlm@37 785 function include(object) {
rlm@37 786 if (Object.isFunction(this.indexOf))
rlm@37 787 if (this.indexOf(object) != -1) return true;
rlm@37 788
rlm@37 789 var found = false;
rlm@37 790 this.each(function(value) {
rlm@37 791 if (value == object) {
rlm@37 792 found = true;
rlm@37 793 throw $break;
rlm@37 794 }
rlm@37 795 });
rlm@37 796 return found;
rlm@37 797 }
rlm@37 798
rlm@37 799 function inGroupsOf(number, fillWith) {
rlm@37 800 fillWith = Object.isUndefined(fillWith) ? null : fillWith;
rlm@37 801 return this.eachSlice(number, function(slice) {
rlm@37 802 while(slice.length < number) slice.push(fillWith);
rlm@37 803 return slice;
rlm@37 804 });
rlm@37 805 }
rlm@37 806
rlm@37 807 function inject(memo, iterator, context) {
rlm@37 808 this.each(function(value, index) {
rlm@37 809 memo = iterator.call(context, memo, value, index);
rlm@37 810 });
rlm@37 811 return memo;
rlm@37 812 }
rlm@37 813
rlm@37 814 function invoke(method) {
rlm@37 815 var args = $A(arguments).slice(1);
rlm@37 816 return this.map(function(value) {
rlm@37 817 return value[method].apply(value, args);
rlm@37 818 });
rlm@37 819 }
rlm@37 820
rlm@37 821 function max(iterator, context) {
rlm@37 822 iterator = iterator || Prototype.K;
rlm@37 823 var result;
rlm@37 824 this.each(function(value, index) {
rlm@37 825 value = iterator.call(context, value, index);
rlm@37 826 if (result == null || value >= result)
rlm@37 827 result = value;
rlm@37 828 });
rlm@37 829 return result;
rlm@37 830 }
rlm@37 831
rlm@37 832 function min(iterator, context) {
rlm@37 833 iterator = iterator || Prototype.K;
rlm@37 834 var result;
rlm@37 835 this.each(function(value, index) {
rlm@37 836 value = iterator.call(context, value, index);
rlm@37 837 if (result == null || value < result)
rlm@37 838 result = value;
rlm@37 839 });
rlm@37 840 return result;
rlm@37 841 }
rlm@37 842
rlm@37 843 function partition(iterator, context) {
rlm@37 844 iterator = iterator || Prototype.K;
rlm@37 845 var trues = [], falses = [];
rlm@37 846 this.each(function(value, index) {
rlm@37 847 (iterator.call(context, value, index) ?
rlm@37 848 trues : falses).push(value);
rlm@37 849 });
rlm@37 850 return [trues, falses];
rlm@37 851 }
rlm@37 852
rlm@37 853 function pluck(property) {
rlm@37 854 var results = [];
rlm@37 855 this.each(function(value) {
rlm@37 856 results.push(value[property]);
rlm@37 857 });
rlm@37 858 return results;
rlm@37 859 }
rlm@37 860
rlm@37 861 function reject(iterator, context) {
rlm@37 862 var results = [];
rlm@37 863 this.each(function(value, index) {
rlm@37 864 if (!iterator.call(context, value, index))
rlm@37 865 results.push(value);
rlm@37 866 });
rlm@37 867 return results;
rlm@37 868 }
rlm@37 869
rlm@37 870 function sortBy(iterator, context) {
rlm@37 871 return this.map(function(value, index) {
rlm@37 872 return {
rlm@37 873 value: value,
rlm@37 874 criteria: iterator.call(context, value, index)
rlm@37 875 };
rlm@37 876 }).sort(function(left, right) {
rlm@37 877 var a = left.criteria, b = right.criteria;
rlm@37 878 return a < b ? -1 : a > b ? 1 : 0;
rlm@37 879 }).pluck('value');
rlm@37 880 }
rlm@37 881
rlm@37 882 function toArray() {
rlm@37 883 return this.map();
rlm@37 884 }
rlm@37 885
rlm@37 886 function zip() {
rlm@37 887 var iterator = Prototype.K, args = $A(arguments);
rlm@37 888 if (Object.isFunction(args.last()))
rlm@37 889 iterator = args.pop();
rlm@37 890
rlm@37 891 var collections = [this].concat(args).map($A);
rlm@37 892 return this.map(function(value, index) {
rlm@37 893 return iterator(collections.pluck(index));
rlm@37 894 });
rlm@37 895 }
rlm@37 896
rlm@37 897 function size() {
rlm@37 898 return this.toArray().length;
rlm@37 899 }
rlm@37 900
rlm@37 901 function inspect() {
rlm@37 902 return '#<Enumerable:' + this.toArray().inspect() + '>';
rlm@37 903 }
rlm@37 904
rlm@37 905
rlm@37 906
rlm@37 907
rlm@37 908
rlm@37 909
rlm@37 910
rlm@37 911
rlm@37 912
rlm@37 913 return {
rlm@37 914 each: each,
rlm@37 915 eachSlice: eachSlice,
rlm@37 916 all: all,
rlm@37 917 every: all,
rlm@37 918 any: any,
rlm@37 919 some: any,
rlm@37 920 collect: collect,
rlm@37 921 map: collect,
rlm@37 922 detect: detect,
rlm@37 923 findAll: findAll,
rlm@37 924 select: findAll,
rlm@37 925 filter: findAll,
rlm@37 926 grep: grep,
rlm@37 927 include: include,
rlm@37 928 member: include,
rlm@37 929 inGroupsOf: inGroupsOf,
rlm@37 930 inject: inject,
rlm@37 931 invoke: invoke,
rlm@37 932 max: max,
rlm@37 933 min: min,
rlm@37 934 partition: partition,
rlm@37 935 pluck: pluck,
rlm@37 936 reject: reject,
rlm@37 937 sortBy: sortBy,
rlm@37 938 toArray: toArray,
rlm@37 939 entries: toArray,
rlm@37 940 zip: zip,
rlm@37 941 size: size,
rlm@37 942 inspect: inspect,
rlm@37 943 find: detect
rlm@37 944 };
rlm@37 945 })();
rlm@37 946 function $A(iterable) {
rlm@37 947 if (!iterable) return [];
rlm@37 948 if ('toArray' in Object(iterable)) return iterable.toArray();
rlm@37 949 var length = iterable.length || 0, results = new Array(length);
rlm@37 950 while (length--) results[length] = iterable[length];
rlm@37 951 return results;
rlm@37 952 }
rlm@37 953
rlm@37 954 function $w(string) {
rlm@37 955 if (!Object.isString(string)) return [];
rlm@37 956 string = string.strip();
rlm@37 957 return string ? string.split(/\s+/) : [];
rlm@37 958 }
rlm@37 959
rlm@37 960 Array.from = $A;
rlm@37 961
rlm@37 962
rlm@37 963 (function() {
rlm@37 964 var arrayProto = Array.prototype,
rlm@37 965 slice = arrayProto.slice,
rlm@37 966 _each = arrayProto.forEach; // use native browser JS 1.6 implementation if available
rlm@37 967
rlm@37 968 function each(iterator) {
rlm@37 969 for (var i = 0, length = this.length; i < length; i++)
rlm@37 970 iterator(this[i]);
rlm@37 971 }
rlm@37 972 if (!_each) _each = each;
rlm@37 973
rlm@37 974 function clear() {
rlm@37 975 this.length = 0;
rlm@37 976 return this;
rlm@37 977 }
rlm@37 978
rlm@37 979 function first() {
rlm@37 980 return this[0];
rlm@37 981 }
rlm@37 982
rlm@37 983 function last() {
rlm@37 984 return this[this.length - 1];
rlm@37 985 }
rlm@37 986
rlm@37 987 function compact() {
rlm@37 988 return this.select(function(value) {
rlm@37 989 return value != null;
rlm@37 990 });
rlm@37 991 }
rlm@37 992
rlm@37 993 function flatten() {
rlm@37 994 return this.inject([], function(array, value) {
rlm@37 995 if (Object.isArray(value))
rlm@37 996 return array.concat(value.flatten());
rlm@37 997 array.push(value);
rlm@37 998 return array;
rlm@37 999 });
rlm@37 1000 }
rlm@37 1001
rlm@37 1002 function without() {
rlm@37 1003 var values = slice.call(arguments, 0);
rlm@37 1004 return this.select(function(value) {
rlm@37 1005 return !values.include(value);
rlm@37 1006 });
rlm@37 1007 }
rlm@37 1008
rlm@37 1009 function reverse(inline) {
rlm@37 1010 return (inline !== false ? this : this.toArray())._reverse();
rlm@37 1011 }
rlm@37 1012
rlm@37 1013 function uniq(sorted) {
rlm@37 1014 return this.inject([], function(array, value, index) {
rlm@37 1015 if (0 == index || (sorted ? array.last() != value : !array.include(value)))
rlm@37 1016 array.push(value);
rlm@37 1017 return array;
rlm@37 1018 });
rlm@37 1019 }
rlm@37 1020
rlm@37 1021 function intersect(array) {
rlm@37 1022 return this.uniq().findAll(function(item) {
rlm@37 1023 return array.detect(function(value) { return item === value });
rlm@37 1024 });
rlm@37 1025 }
rlm@37 1026
rlm@37 1027
rlm@37 1028 function clone() {
rlm@37 1029 return slice.call(this, 0);
rlm@37 1030 }
rlm@37 1031
rlm@37 1032 function size() {
rlm@37 1033 return this.length;
rlm@37 1034 }
rlm@37 1035
rlm@37 1036 function inspect() {
rlm@37 1037 return '[' + this.map(Object.inspect).join(', ') + ']';
rlm@37 1038 }
rlm@37 1039
rlm@37 1040 function toJSON() {
rlm@37 1041 var results = [];
rlm@37 1042 this.each(function(object) {
rlm@37 1043 var value = Object.toJSON(object);
rlm@37 1044 if (!Object.isUndefined(value)) results.push(value);
rlm@37 1045 });
rlm@37 1046 return '[' + results.join(', ') + ']';
rlm@37 1047 }
rlm@37 1048
rlm@37 1049 function indexOf(item, i) {
rlm@37 1050 i || (i = 0);
rlm@37 1051 var length = this.length;
rlm@37 1052 if (i < 0) i = length + i;
rlm@37 1053 for (; i < length; i++)
rlm@37 1054 if (this[i] === item) return i;
rlm@37 1055 return -1;
rlm@37 1056 }
rlm@37 1057
rlm@37 1058 function lastIndexOf(item, i) {
rlm@37 1059 i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1;
rlm@37 1060 var n = this.slice(0, i).reverse().indexOf(item);
rlm@37 1061 return (n < 0) ? n : i - n - 1;
rlm@37 1062 }
rlm@37 1063
rlm@37 1064 function concat() {
rlm@37 1065 var array = slice.call(this, 0), item;
rlm@37 1066 for (var i = 0, length = arguments.length; i < length; i++) {
rlm@37 1067 item = arguments[i];
rlm@37 1068 if (Object.isArray(item) && !('callee' in item)) {
rlm@37 1069 for (var j = 0, arrayLength = item.length; j < arrayLength; j++)
rlm@37 1070 array.push(item[j]);
rlm@37 1071 } else {
rlm@37 1072 array.push(item);
rlm@37 1073 }
rlm@37 1074 }
rlm@37 1075 return array;
rlm@37 1076 }
rlm@37 1077
rlm@37 1078 Object.extend(arrayProto, Enumerable);
rlm@37 1079
rlm@37 1080 if (!arrayProto._reverse)
rlm@37 1081 arrayProto._reverse = arrayProto.reverse;
rlm@37 1082
rlm@37 1083 Object.extend(arrayProto, {
rlm@37 1084 _each: _each,
rlm@37 1085 clear: clear,
rlm@37 1086 first: first,
rlm@37 1087 last: last,
rlm@37 1088 compact: compact,
rlm@37 1089 flatten: flatten,
rlm@37 1090 without: without,
rlm@37 1091 reverse: reverse,
rlm@37 1092 uniq: uniq,
rlm@37 1093 intersect: intersect,
rlm@37 1094 clone: clone,
rlm@37 1095 toArray: clone,
rlm@37 1096 size: size,
rlm@37 1097 inspect: inspect,
rlm@37 1098 toJSON: toJSON
rlm@37 1099 });
rlm@37 1100
rlm@37 1101 var CONCAT_ARGUMENTS_BUGGY = (function() {
rlm@37 1102 return [].concat(arguments)[0][0] !== 1;
rlm@37 1103 })(1,2)
rlm@37 1104
rlm@37 1105 if (CONCAT_ARGUMENTS_BUGGY) arrayProto.concat = concat;
rlm@37 1106
rlm@37 1107 if (!arrayProto.indexOf) arrayProto.indexOf = indexOf;
rlm@37 1108 if (!arrayProto.lastIndexOf) arrayProto.lastIndexOf = lastIndexOf;
rlm@37 1109 })();
rlm@37 1110 function $H(object) {
rlm@37 1111 return new Hash(object);
rlm@37 1112 };
rlm@37 1113
rlm@37 1114 var Hash = Class.create(Enumerable, (function() {
rlm@37 1115 function initialize(object) {
rlm@37 1116 this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
rlm@37 1117 }
rlm@37 1118
rlm@37 1119 function _each(iterator) {
rlm@37 1120 for (var key in this._object) {
rlm@37 1121 var value = this._object[key], pair = [key, value];
rlm@37 1122 pair.key = key;
rlm@37 1123 pair.value = value;
rlm@37 1124 iterator(pair);
rlm@37 1125 }
rlm@37 1126 }
rlm@37 1127
rlm@37 1128 function set(key, value) {
rlm@37 1129 return this._object[key] = value;
rlm@37 1130 }
rlm@37 1131
rlm@37 1132 function get(key) {
rlm@37 1133 if (this._object[key] !== Object.prototype[key])
rlm@37 1134 return this._object[key];
rlm@37 1135 }
rlm@37 1136
rlm@37 1137 function unset(key) {
rlm@37 1138 var value = this._object[key];
rlm@37 1139 delete this._object[key];
rlm@37 1140 return value;
rlm@37 1141 }
rlm@37 1142
rlm@37 1143 function toObject() {
rlm@37 1144 return Object.clone(this._object);
rlm@37 1145 }
rlm@37 1146
rlm@37 1147 function keys() {
rlm@37 1148 return this.pluck('key');
rlm@37 1149 }
rlm@37 1150
rlm@37 1151 function values() {
rlm@37 1152 return this.pluck('value');
rlm@37 1153 }
rlm@37 1154
rlm@37 1155 function index(value) {
rlm@37 1156 var match = this.detect(function(pair) {
rlm@37 1157 return pair.value === value;
rlm@37 1158 });
rlm@37 1159 return match && match.key;
rlm@37 1160 }
rlm@37 1161
rlm@37 1162 function merge(object) {
rlm@37 1163 return this.clone().update(object);
rlm@37 1164 }
rlm@37 1165
rlm@37 1166 function update(object) {
rlm@37 1167 return new Hash(object).inject(this, function(result, pair) {
rlm@37 1168 result.set(pair.key, pair.value);
rlm@37 1169 return result;
rlm@37 1170 });
rlm@37 1171 }
rlm@37 1172
rlm@37 1173 function toQueryPair(key, value) {
rlm@37 1174 if (Object.isUndefined(value)) return key;
rlm@37 1175 return key + '=' + encodeURIComponent(String.interpret(value));
rlm@37 1176 }
rlm@37 1177
rlm@37 1178 function toQueryString() {
rlm@37 1179 return this.inject([], function(results, pair) {
rlm@37 1180 var key = encodeURIComponent(pair.key), values = pair.value;
rlm@37 1181
rlm@37 1182 if (values && typeof values == 'object') {
rlm@37 1183 if (Object.isArray(values))
rlm@37 1184 return results.concat(values.map(toQueryPair.curry(key)));
rlm@37 1185 } else results.push(toQueryPair(key, values));
rlm@37 1186 return results;
rlm@37 1187 }).join('&');
rlm@37 1188 }
rlm@37 1189
rlm@37 1190 function inspect() {
rlm@37 1191 return '#<Hash:{' + this.map(function(pair) {
rlm@37 1192 return pair.map(Object.inspect).join(': ');
rlm@37 1193 }).join(', ') + '}>';
rlm@37 1194 }
rlm@37 1195
rlm@37 1196 function toJSON() {
rlm@37 1197 return Object.toJSON(this.toObject());
rlm@37 1198 }
rlm@37 1199
rlm@37 1200 function clone() {
rlm@37 1201 return new Hash(this);
rlm@37 1202 }
rlm@37 1203
rlm@37 1204 return {
rlm@37 1205 initialize: initialize,
rlm@37 1206 _each: _each,
rlm@37 1207 set: set,
rlm@37 1208 get: get,
rlm@37 1209 unset: unset,
rlm@37 1210 toObject: toObject,
rlm@37 1211 toTemplateReplacements: toObject,
rlm@37 1212 keys: keys,
rlm@37 1213 values: values,
rlm@37 1214 index: index,
rlm@37 1215 merge: merge,
rlm@37 1216 update: update,
rlm@37 1217 toQueryString: toQueryString,
rlm@37 1218 inspect: inspect,
rlm@37 1219 toJSON: toJSON,
rlm@37 1220 clone: clone
rlm@37 1221 };
rlm@37 1222 })());
rlm@37 1223
rlm@37 1224 Hash.from = $H;
rlm@37 1225 Object.extend(Number.prototype, (function() {
rlm@37 1226 function toColorPart() {
rlm@37 1227 return this.toPaddedString(2, 16);
rlm@37 1228 }
rlm@37 1229
rlm@37 1230 function succ() {
rlm@37 1231 return this + 1;
rlm@37 1232 }
rlm@37 1233
rlm@37 1234 function times(iterator, context) {
rlm@37 1235 $R(0, this, true).each(iterator, context);
rlm@37 1236 return this;
rlm@37 1237 }
rlm@37 1238
rlm@37 1239 function toPaddedString(length, radix) {
rlm@37 1240 var string = this.toString(radix || 10);
rlm@37 1241 return '0'.times(length - string.length) + string;
rlm@37 1242 }
rlm@37 1243
rlm@37 1244 function toJSON() {
rlm@37 1245 return isFinite(this) ? this.toString() : 'null';
rlm@37 1246 }
rlm@37 1247
rlm@37 1248 function abs() {
rlm@37 1249 return Math.abs(this);
rlm@37 1250 }
rlm@37 1251
rlm@37 1252 function round() {
rlm@37 1253 return Math.round(this);
rlm@37 1254 }
rlm@37 1255
rlm@37 1256 function ceil() {
rlm@37 1257 return Math.ceil(this);
rlm@37 1258 }
rlm@37 1259
rlm@37 1260 function floor() {
rlm@37 1261 return Math.floor(this);
rlm@37 1262 }
rlm@37 1263
rlm@37 1264 return {
rlm@37 1265 toColorPart: toColorPart,
rlm@37 1266 succ: succ,
rlm@37 1267 times: times,
rlm@37 1268 toPaddedString: toPaddedString,
rlm@37 1269 toJSON: toJSON,
rlm@37 1270 abs: abs,
rlm@37 1271 round: round,
rlm@37 1272 ceil: ceil,
rlm@37 1273 floor: floor
rlm@37 1274 };
rlm@37 1275 })());
rlm@37 1276
rlm@37 1277 function $R(start, end, exclusive) {
rlm@37 1278 return new ObjectRange(start, end, exclusive);
rlm@37 1279 }
rlm@37 1280
rlm@37 1281 var ObjectRange = Class.create(Enumerable, (function() {
rlm@37 1282 function initialize(start, end, exclusive) {
rlm@37 1283 this.start = start;
rlm@37 1284 this.end = end;
rlm@37 1285 this.exclusive = exclusive;
rlm@37 1286 }
rlm@37 1287
rlm@37 1288 function _each(iterator) {
rlm@37 1289 var value = this.start;
rlm@37 1290 while (this.include(value)) {
rlm@37 1291 iterator(value);
rlm@37 1292 value = value.succ();
rlm@37 1293 }
rlm@37 1294 }
rlm@37 1295
rlm@37 1296 function include(value) {
rlm@37 1297 if (value < this.start)
rlm@37 1298 return false;
rlm@37 1299 if (this.exclusive)
rlm@37 1300 return value < this.end;
rlm@37 1301 return value <= this.end;
rlm@37 1302 }
rlm@37 1303
rlm@37 1304 return {
rlm@37 1305 initialize: initialize,
rlm@37 1306 _each: _each,
rlm@37 1307 include: include
rlm@37 1308 };
rlm@37 1309 })());
rlm@37 1310
rlm@37 1311
rlm@37 1312
rlm@37 1313 var Ajax = {
rlm@37 1314 getTransport: function() {
rlm@37 1315 return Try.these(
rlm@37 1316 function() {return new XMLHttpRequest()},
rlm@37 1317 function() {return new ActiveXObject('Msxml2.XMLHTTP')},
rlm@37 1318 function() {return new ActiveXObject('Microsoft.XMLHTTP')}
rlm@37 1319 ) || false;
rlm@37 1320 },
rlm@37 1321
rlm@37 1322 activeRequestCount: 0
rlm@37 1323 };
rlm@37 1324
rlm@37 1325 Ajax.Responders = {
rlm@37 1326 responders: [],
rlm@37 1327
rlm@37 1328 _each: function(iterator) {
rlm@37 1329 this.responders._each(iterator);
rlm@37 1330 },
rlm@37 1331
rlm@37 1332 register: function(responder) {
rlm@37 1333 if (!this.include(responder))
rlm@37 1334 this.responders.push(responder);
rlm@37 1335 },
rlm@37 1336
rlm@37 1337 unregister: function(responder) {
rlm@37 1338 this.responders = this.responders.without(responder);
rlm@37 1339 },
rlm@37 1340
rlm@37 1341 dispatch: function(callback, request, transport, json) {
rlm@37 1342 this.each(function(responder) {
rlm@37 1343 if (Object.isFunction(responder[callback])) {
rlm@37 1344 try {
rlm@37 1345 responder[callback].apply(responder, [request, transport, json]);
rlm@37 1346 } catch (e) { }
rlm@37 1347 }
rlm@37 1348 });
rlm@37 1349 }
rlm@37 1350 };
rlm@37 1351
rlm@37 1352 Object.extend(Ajax.Responders, Enumerable);
rlm@37 1353
rlm@37 1354 Ajax.Responders.register({
rlm@37 1355 onCreate: function() { Ajax.activeRequestCount++ },
rlm@37 1356 onComplete: function() { Ajax.activeRequestCount-- }
rlm@37 1357 });
rlm@37 1358 Ajax.Base = Class.create({
rlm@37 1359 initialize: function(options) {
rlm@37 1360 this.options = {
rlm@37 1361 method: 'post',
rlm@37 1362 asynchronous: true,
rlm@37 1363 contentType: 'application/x-www-form-urlencoded',
rlm@37 1364 encoding: 'UTF-8',
rlm@37 1365 parameters: '',
rlm@37 1366 evalJSON: true,
rlm@37 1367 evalJS: true
rlm@37 1368 };
rlm@37 1369 Object.extend(this.options, options || { });
rlm@37 1370
rlm@37 1371 this.options.method = this.options.method.toLowerCase();
rlm@37 1372
rlm@37 1373 if (Object.isString(this.options.parameters))
rlm@37 1374 this.options.parameters = this.options.parameters.toQueryParams();
rlm@37 1375 else if (Object.isHash(this.options.parameters))
rlm@37 1376 this.options.parameters = this.options.parameters.toObject();
rlm@37 1377 }
rlm@37 1378 });
rlm@37 1379 Ajax.Request = Class.create(Ajax.Base, {
rlm@37 1380 _complete: false,
rlm@37 1381
rlm@37 1382 initialize: function($super, url, options) {
rlm@37 1383 $super(options);
rlm@37 1384 this.transport = Ajax.getTransport();
rlm@37 1385 this.request(url);
rlm@37 1386 },
rlm@37 1387
rlm@37 1388 request: function(url) {
rlm@37 1389 this.url = url;
rlm@37 1390 this.method = this.options.method;
rlm@37 1391 var params = Object.clone(this.options.parameters);
rlm@37 1392
rlm@37 1393 if (!['get', 'post'].include(this.method)) {
rlm@37 1394 params['_method'] = this.method;
rlm@37 1395 this.method = 'post';
rlm@37 1396 }
rlm@37 1397
rlm@37 1398 this.parameters = params;
rlm@37 1399
rlm@37 1400 if (params = Object.toQueryString(params)) {
rlm@37 1401 if (this.method == 'get')
rlm@37 1402 this.url += (this.url.include('?') ? '&' : '?') + params;
rlm@37 1403 else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
rlm@37 1404 params += '&_=';
rlm@37 1405 }
rlm@37 1406
rlm@37 1407 try {
rlm@37 1408 var response = new Ajax.Response(this);
rlm@37 1409 if (this.options.onCreate) this.options.onCreate(response);
rlm@37 1410 Ajax.Responders.dispatch('onCreate', this, response);
rlm@37 1411
rlm@37 1412 this.transport.open(this.method.toUpperCase(), this.url,
rlm@37 1413 this.options.asynchronous);
rlm@37 1414
rlm@37 1415 if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
rlm@37 1416
rlm@37 1417 this.transport.onreadystatechange = this.onStateChange.bind(this);
rlm@37 1418 this.setRequestHeaders();
rlm@37 1419
rlm@37 1420 this.body = this.method == 'post' ? (this.options.postBody || params) : null;
rlm@37 1421 this.transport.send(this.body);
rlm@37 1422
rlm@37 1423 /* Force Firefox to handle ready state 4 for synchronous requests */
rlm@37 1424 if (!this.options.asynchronous && this.transport.overrideMimeType)
rlm@37 1425 this.onStateChange();
rlm@37 1426
rlm@37 1427 }
rlm@37 1428 catch (e) {
rlm@37 1429 this.dispatchException(e);
rlm@37 1430 }
rlm@37 1431 },
rlm@37 1432
rlm@37 1433 onStateChange: function() {
rlm@37 1434 var readyState = this.transport.readyState;
rlm@37 1435 if (readyState > 1 && !((readyState == 4) && this._complete))
rlm@37 1436 this.respondToReadyState(this.transport.readyState);
rlm@37 1437 },
rlm@37 1438
rlm@37 1439 setRequestHeaders: function() {
rlm@37 1440 var headers = {
rlm@37 1441 'X-Requested-With': 'XMLHttpRequest',
rlm@37 1442 'X-Prototype-Version': Prototype.Version,
rlm@37 1443 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
rlm@37 1444 };
rlm@37 1445
rlm@37 1446 if (this.method == 'post') {
rlm@37 1447 headers['Content-type'] = this.options.contentType +
rlm@37 1448 (this.options.encoding ? '; charset=' + this.options.encoding : '');
rlm@37 1449
rlm@37 1450 /* Force "Connection: close" for older Mozilla browsers to work
rlm@37 1451 * around a bug where XMLHttpRequest sends an incorrect
rlm@37 1452 * Content-length header. See Mozilla Bugzilla #246651.
rlm@37 1453 */
rlm@37 1454 if (this.transport.overrideMimeType &&
rlm@37 1455 (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
rlm@37 1456 headers['Connection'] = 'close';
rlm@37 1457 }
rlm@37 1458
rlm@37 1459 if (typeof this.options.requestHeaders == 'object') {
rlm@37 1460 var extras = this.options.requestHeaders;
rlm@37 1461
rlm@37 1462 if (Object.isFunction(extras.push))
rlm@37 1463 for (var i = 0, length = extras.length; i < length; i += 2)
rlm@37 1464 headers[extras[i]] = extras[i+1];
rlm@37 1465 else
rlm@37 1466 $H(extras).each(function(pair) { headers[pair.key] = pair.value });
rlm@37 1467 }
rlm@37 1468
rlm@37 1469 for (var name in headers)
rlm@37 1470 this.transport.setRequestHeader(name, headers[name]);
rlm@37 1471 },
rlm@37 1472
rlm@37 1473 success: function() {
rlm@37 1474 var status = this.getStatus();
rlm@37 1475 return !status || (status >= 200 && status < 300);
rlm@37 1476 },
rlm@37 1477
rlm@37 1478 getStatus: function() {
rlm@37 1479 try {
rlm@37 1480 return this.transport.status || 0;
rlm@37 1481 } catch (e) { return 0 }
rlm@37 1482 },
rlm@37 1483
rlm@37 1484 respondToReadyState: function(readyState) {
rlm@37 1485 var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
rlm@37 1486
rlm@37 1487 if (state == 'Complete') {
rlm@37 1488 try {
rlm@37 1489 this._complete = true;
rlm@37 1490 (this.options['on' + response.status]
rlm@37 1491 || this.options['on' + (this.success() ? 'Success' : 'Failure')]
rlm@37 1492 || Prototype.emptyFunction)(response, response.headerJSON);
rlm@37 1493 } catch (e) {
rlm@37 1494 this.dispatchException(e);
rlm@37 1495 }
rlm@37 1496
rlm@37 1497 var contentType = response.getHeader('Content-type');
rlm@37 1498 if (this.options.evalJS == 'force'
rlm@37 1499 || (this.options.evalJS && this.isSameOrigin() && contentType
rlm@37 1500 && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
rlm@37 1501 this.evalResponse();
rlm@37 1502 }
rlm@37 1503
rlm@37 1504 try {
rlm@37 1505 (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
rlm@37 1506 Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
rlm@37 1507 } catch (e) {
rlm@37 1508 this.dispatchException(e);
rlm@37 1509 }
rlm@37 1510
rlm@37 1511 if (state == 'Complete') {
rlm@37 1512 this.transport.onreadystatechange = Prototype.emptyFunction;
rlm@37 1513 }
rlm@37 1514 },
rlm@37 1515
rlm@37 1516 isSameOrigin: function() {
rlm@37 1517 var m = this.url.match(/^\s*https?:\/\/[^\/]*/);
rlm@37 1518 return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({
rlm@37 1519 protocol: location.protocol,
rlm@37 1520 domain: document.domain,
rlm@37 1521 port: location.port ? ':' + location.port : ''
rlm@37 1522 }));
rlm@37 1523 },
rlm@37 1524
rlm@37 1525 getHeader: function(name) {
rlm@37 1526 try {
rlm@37 1527 return this.transport.getResponseHeader(name) || null;
rlm@37 1528 } catch (e) { return null; }
rlm@37 1529 },
rlm@37 1530
rlm@37 1531 evalResponse: function() {
rlm@37 1532 try {
rlm@37 1533 return eval((this.transport.responseText || '').unfilterJSON());
rlm@37 1534 } catch (e) {
rlm@37 1535 this.dispatchException(e);
rlm@37 1536 }
rlm@37 1537 },
rlm@37 1538
rlm@37 1539 dispatchException: function(exception) {
rlm@37 1540 (this.options.onException || Prototype.emptyFunction)(this, exception);
rlm@37 1541 Ajax.Responders.dispatch('onException', this, exception);
rlm@37 1542 }
rlm@37 1543 });
rlm@37 1544
rlm@37 1545 Ajax.Request.Events =
rlm@37 1546 ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
rlm@37 1547
rlm@37 1548
rlm@37 1549
rlm@37 1550
rlm@37 1551
rlm@37 1552
rlm@37 1553
rlm@37 1554
rlm@37 1555 Ajax.Response = Class.create({
rlm@37 1556 initialize: function(request){
rlm@37 1557 this.request = request;
rlm@37 1558 var transport = this.transport = request.transport,
rlm@37 1559 readyState = this.readyState = transport.readyState;
rlm@37 1560
rlm@37 1561 if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) {
rlm@37 1562 this.status = this.getStatus();
rlm@37 1563 this.statusText = this.getStatusText();
rlm@37 1564 this.responseText = String.interpret(transport.responseText);
rlm@37 1565 this.headerJSON = this._getHeaderJSON();
rlm@37 1566 }
rlm@37 1567
rlm@37 1568 if(readyState == 4) {
rlm@37 1569 var xml = transport.responseXML;
rlm@37 1570 this.responseXML = Object.isUndefined(xml) ? null : xml;
rlm@37 1571 this.responseJSON = this._getResponseJSON();
rlm@37 1572 }
rlm@37 1573 },
rlm@37 1574
rlm@37 1575 status: 0,
rlm@37 1576
rlm@37 1577 statusText: '',
rlm@37 1578
rlm@37 1579 getStatus: Ajax.Request.prototype.getStatus,
rlm@37 1580
rlm@37 1581 getStatusText: function() {
rlm@37 1582 try {
rlm@37 1583 return this.transport.statusText || '';
rlm@37 1584 } catch (e) { return '' }
rlm@37 1585 },
rlm@37 1586
rlm@37 1587 getHeader: Ajax.Request.prototype.getHeader,
rlm@37 1588
rlm@37 1589 getAllHeaders: function() {
rlm@37 1590 try {
rlm@37 1591 return this.getAllResponseHeaders();
rlm@37 1592 } catch (e) { return null }
rlm@37 1593 },
rlm@37 1594
rlm@37 1595 getResponseHeader: function(name) {
rlm@37 1596 return this.transport.getResponseHeader(name);
rlm@37 1597 },
rlm@37 1598
rlm@37 1599 getAllResponseHeaders: function() {
rlm@37 1600 return this.transport.getAllResponseHeaders();
rlm@37 1601 },
rlm@37 1602
rlm@37 1603 _getHeaderJSON: function() {
rlm@37 1604 var json = this.getHeader('X-JSON');
rlm@37 1605 if (!json) return null;
rlm@37 1606 json = decodeURIComponent(escape(json));
rlm@37 1607 try {
rlm@37 1608 return json.evalJSON(this.request.options.sanitizeJSON ||
rlm@37 1609 !this.request.isSameOrigin());
rlm@37 1610 } catch (e) {
rlm@37 1611 this.request.dispatchException(e);
rlm@37 1612 }
rlm@37 1613 },
rlm@37 1614
rlm@37 1615 _getResponseJSON: function() {
rlm@37 1616 var options = this.request.options;
rlm@37 1617 if (!options.evalJSON || (options.evalJSON != 'force' &&
rlm@37 1618 !(this.getHeader('Content-type') || '').include('application/json')) ||
rlm@37 1619 this.responseText.blank())
rlm@37 1620 return null;
rlm@37 1621 try {
rlm@37 1622 return this.responseText.evalJSON(options.sanitizeJSON ||
rlm@37 1623 !this.request.isSameOrigin());
rlm@37 1624 } catch (e) {
rlm@37 1625 this.request.dispatchException(e);
rlm@37 1626 }
rlm@37 1627 }
rlm@37 1628 });
rlm@37 1629
rlm@37 1630 Ajax.Updater = Class.create(Ajax.Request, {
rlm@37 1631 initialize: function($super, container, url, options) {
rlm@37 1632 this.container = {
rlm@37 1633 success: (container.success || container),
rlm@37 1634 failure: (container.failure || (container.success ? null : container))
rlm@37 1635 };
rlm@37 1636
rlm@37 1637 options = Object.clone(options);
rlm@37 1638 var onComplete = options.onComplete;
rlm@37 1639 options.onComplete = (function(response, json) {
rlm@37 1640 this.updateContent(response.responseText);
rlm@37 1641 if (Object.isFunction(onComplete)) onComplete(response, json);
rlm@37 1642 }).bind(this);
rlm@37 1643
rlm@37 1644 $super(url, options);
rlm@37 1645 },
rlm@37 1646
rlm@37 1647 updateContent: function(responseText) {
rlm@37 1648 var receiver = this.container[this.success() ? 'success' : 'failure'],
rlm@37 1649 options = this.options;
rlm@37 1650
rlm@37 1651 if (!options.evalScripts) responseText = responseText.stripScripts();
rlm@37 1652
rlm@37 1653 if (receiver = $(receiver)) {
rlm@37 1654 if (options.insertion) {
rlm@37 1655 if (Object.isString(options.insertion)) {
rlm@37 1656 var insertion = { }; insertion[options.insertion] = responseText;
rlm@37 1657 receiver.insert(insertion);
rlm@37 1658 }
rlm@37 1659 else options.insertion(receiver, responseText);
rlm@37 1660 }
rlm@37 1661 else receiver.update(responseText);
rlm@37 1662 }
rlm@37 1663 }
rlm@37 1664 });
rlm@37 1665
rlm@37 1666 Ajax.PeriodicalUpdater = Class.create(Ajax.Base, {
rlm@37 1667 initialize: function($super, container, url, options) {
rlm@37 1668 $super(options);
rlm@37 1669 this.onComplete = this.options.onComplete;
rlm@37 1670
rlm@37 1671 this.frequency = (this.options.frequency || 2);
rlm@37 1672 this.decay = (this.options.decay || 1);
rlm@37 1673
rlm@37 1674 this.updater = { };
rlm@37 1675 this.container = container;
rlm@37 1676 this.url = url;
rlm@37 1677
rlm@37 1678 this.start();
rlm@37 1679 },
rlm@37 1680
rlm@37 1681 start: function() {
rlm@37 1682 this.options.onComplete = this.updateComplete.bind(this);
rlm@37 1683 this.onTimerEvent();
rlm@37 1684 },
rlm@37 1685
rlm@37 1686 stop: function() {
rlm@37 1687 this.updater.options.onComplete = undefined;
rlm@37 1688 clearTimeout(this.timer);
rlm@37 1689 (this.onComplete || Prototype.emptyFunction).apply(this, arguments);
rlm@37 1690 },
rlm@37 1691
rlm@37 1692 updateComplete: function(response) {
rlm@37 1693 if (this.options.decay) {
rlm@37 1694 this.decay = (response.responseText == this.lastText ?
rlm@37 1695 this.decay * this.options.decay : 1);
rlm@37 1696
rlm@37 1697 this.lastText = response.responseText;
rlm@37 1698 }
rlm@37 1699 this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency);
rlm@37 1700 },
rlm@37 1701
rlm@37 1702 onTimerEvent: function() {
rlm@37 1703 this.updater = new Ajax.Updater(this.container, this.url, this.options);
rlm@37 1704 }
rlm@37 1705 });
rlm@37 1706
rlm@37 1707
rlm@37 1708
rlm@37 1709 function $(element) {
rlm@37 1710 if (arguments.length > 1) {
rlm@37 1711 for (var i = 0, elements = [], length = arguments.length; i < length; i++)
rlm@37 1712 elements.push($(arguments[i]));
rlm@37 1713 return elements;
rlm@37 1714 }
rlm@37 1715 if (Object.isString(element))
rlm@37 1716 element = document.getElementById(element);
rlm@37 1717 return Element.extend(element);
rlm@37 1718 }
rlm@37 1719
rlm@37 1720 if (Prototype.BrowserFeatures.XPath) {
rlm@37 1721 document._getElementsByXPath = function(expression, parentElement) {
rlm@37 1722 var results = [];
rlm@37 1723 var query = document.evaluate(expression, $(parentElement) || document,
rlm@37 1724 null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
rlm@37 1725 for (var i = 0, length = query.snapshotLength; i < length; i++)
rlm@37 1726 results.push(Element.extend(query.snapshotItem(i)));
rlm@37 1727 return results;
rlm@37 1728 };
rlm@37 1729 }
rlm@37 1730
rlm@37 1731 /*--------------------------------------------------------------------------*/
rlm@37 1732
rlm@37 1733 if (!window.Node) var Node = { };
rlm@37 1734
rlm@37 1735 if (!Node.ELEMENT_NODE) {
rlm@37 1736 Object.extend(Node, {
rlm@37 1737 ELEMENT_NODE: 1,
rlm@37 1738 ATTRIBUTE_NODE: 2,
rlm@37 1739 TEXT_NODE: 3,
rlm@37 1740 CDATA_SECTION_NODE: 4,
rlm@37 1741 ENTITY_REFERENCE_NODE: 5,
rlm@37 1742 ENTITY_NODE: 6,
rlm@37 1743 PROCESSING_INSTRUCTION_NODE: 7,
rlm@37 1744 COMMENT_NODE: 8,
rlm@37 1745 DOCUMENT_NODE: 9,
rlm@37 1746 DOCUMENT_TYPE_NODE: 10,
rlm@37 1747 DOCUMENT_FRAGMENT_NODE: 11,
rlm@37 1748 NOTATION_NODE: 12
rlm@37 1749 });
rlm@37 1750 }
rlm@37 1751
rlm@37 1752
rlm@37 1753 (function(global) {
rlm@37 1754
rlm@37 1755 var SETATTRIBUTE_IGNORES_NAME = (function(){
rlm@37 1756 var elForm = document.createElement("form");
rlm@37 1757 var elInput = document.createElement("input");
rlm@37 1758 var root = document.documentElement;
rlm@37 1759 elInput.setAttribute("name", "test");
rlm@37 1760 elForm.appendChild(elInput);
rlm@37 1761 root.appendChild(elForm);
rlm@37 1762 var isBuggy = elForm.elements
rlm@37 1763 ? (typeof elForm.elements.test == "undefined")
rlm@37 1764 : null;
rlm@37 1765 root.removeChild(elForm);
rlm@37 1766 elForm = elInput = null;
rlm@37 1767 return isBuggy;
rlm@37 1768 })();
rlm@37 1769
rlm@37 1770 var element = global.Element;
rlm@37 1771 global.Element = function(tagName, attributes) {
rlm@37 1772 attributes = attributes || { };
rlm@37 1773 tagName = tagName.toLowerCase();
rlm@37 1774 var cache = Element.cache;
rlm@37 1775 if (SETATTRIBUTE_IGNORES_NAME && attributes.name) {
rlm@37 1776 tagName = '<' + tagName + ' name="' + attributes.name + '">';
rlm@37 1777 delete attributes.name;
rlm@37 1778 return Element.writeAttribute(document.createElement(tagName), attributes);
rlm@37 1779 }
rlm@37 1780 if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName));
rlm@37 1781 return Element.writeAttribute(cache[tagName].cloneNode(false), attributes);
rlm@37 1782 };
rlm@37 1783 Object.extend(global.Element, element || { });
rlm@37 1784 if (element) global.Element.prototype = element.prototype;
rlm@37 1785 })(this);
rlm@37 1786
rlm@37 1787 Element.cache = { };
rlm@37 1788 Element.idCounter = 1;
rlm@37 1789
rlm@37 1790 Element.Methods = {
rlm@37 1791 visible: function(element) {
rlm@37 1792 return $(element).style.display != 'none';
rlm@37 1793 },
rlm@37 1794
rlm@37 1795 toggle: function(element) {
rlm@37 1796 element = $(element);
rlm@37 1797 Element[Element.visible(element) ? 'hide' : 'show'](element);
rlm@37 1798 return element;
rlm@37 1799 },
rlm@37 1800
rlm@37 1801
rlm@37 1802 hide: function(element) {
rlm@37 1803 element = $(element);
rlm@37 1804 element.style.display = 'none';
rlm@37 1805 return element;
rlm@37 1806 },
rlm@37 1807
rlm@37 1808 show: function(element) {
rlm@37 1809 element = $(element);
rlm@37 1810 element.style.display = '';
rlm@37 1811 return element;
rlm@37 1812 },
rlm@37 1813
rlm@37 1814 remove: function(element) {
rlm@37 1815 element = $(element);
rlm@37 1816 element.parentNode.removeChild(element);
rlm@37 1817 return element;
rlm@37 1818 },
rlm@37 1819
rlm@37 1820 update: (function(){
rlm@37 1821
rlm@37 1822 var SELECT_ELEMENT_INNERHTML_BUGGY = (function(){
rlm@37 1823 var el = document.createElement("select"),
rlm@37 1824 isBuggy = true;
rlm@37 1825 el.innerHTML = "<option value=\"test\">test</option>";
rlm@37 1826 if (el.options && el.options[0]) {
rlm@37 1827 isBuggy = el.options[0].nodeName.toUpperCase() !== "OPTION";
rlm@37 1828 }
rlm@37 1829 el = null;
rlm@37 1830 return isBuggy;
rlm@37 1831 })();
rlm@37 1832
rlm@37 1833 var TABLE_ELEMENT_INNERHTML_BUGGY = (function(){
rlm@37 1834 try {
rlm@37 1835 var el = document.createElement("table");
rlm@37 1836 if (el && el.tBodies) {
rlm@37 1837 el.innerHTML = "<tbody><tr><td>test</td></tr></tbody>";
rlm@37 1838 var isBuggy = typeof el.tBodies[0] == "undefined";
rlm@37 1839 el = null;
rlm@37 1840 return isBuggy;
rlm@37 1841 }
rlm@37 1842 } catch (e) {
rlm@37 1843 return true;
rlm@37 1844 }
rlm@37 1845 })();
rlm@37 1846
rlm@37 1847 var SCRIPT_ELEMENT_REJECTS_TEXTNODE_APPENDING = (function () {
rlm@37 1848 var s = document.createElement("script"),
rlm@37 1849 isBuggy = false;
rlm@37 1850 try {
rlm@37 1851 s.appendChild(document.createTextNode(""));
rlm@37 1852 isBuggy = !s.firstChild ||
rlm@37 1853 s.firstChild && s.firstChild.nodeType !== 3;
rlm@37 1854 } catch (e) {
rlm@37 1855 isBuggy = true;
rlm@37 1856 }
rlm@37 1857 s = null;
rlm@37 1858 return isBuggy;
rlm@37 1859 })();
rlm@37 1860
rlm@37 1861 function update(element, content) {
rlm@37 1862 element = $(element);
rlm@37 1863
rlm@37 1864 if (content && content.toElement)
rlm@37 1865 content = content.toElement();
rlm@37 1866
rlm@37 1867 if (Object.isElement(content))
rlm@37 1868 return element.update().insert(content);
rlm@37 1869
rlm@37 1870 content = Object.toHTML(content);
rlm@37 1871
rlm@37 1872 var tagName = element.tagName.toUpperCase();
rlm@37 1873
rlm@37 1874 if (tagName === 'SCRIPT' && SCRIPT_ELEMENT_REJECTS_TEXTNODE_APPENDING) {
rlm@37 1875 element.text = content;
rlm@37 1876 return element;
rlm@37 1877 }
rlm@37 1878
rlm@37 1879 if (SELECT_ELEMENT_INNERHTML_BUGGY || TABLE_ELEMENT_INNERHTML_BUGGY) {
rlm@37 1880 if (tagName in Element._insertionTranslations.tags) {
rlm@37 1881 while (element.firstChild) {
rlm@37 1882 element.removeChild(element.firstChild);
rlm@37 1883 }
rlm@37 1884 Element._getContentFromAnonymousElement(tagName, content.stripScripts())
rlm@37 1885 .each(function(node) {
rlm@37 1886 element.appendChild(node)
rlm@37 1887 });
rlm@37 1888 }
rlm@37 1889 else {
rlm@37 1890 element.innerHTML = content.stripScripts();
rlm@37 1891 }
rlm@37 1892 }
rlm@37 1893 else {
rlm@37 1894 element.innerHTML = content.stripScripts();
rlm@37 1895 }
rlm@37 1896
rlm@37 1897 content.evalScripts.bind(content).defer();
rlm@37 1898 return element;
rlm@37 1899 }
rlm@37 1900
rlm@37 1901 return update;
rlm@37 1902 })(),
rlm@37 1903
rlm@37 1904 replace: function(element, content) {
rlm@37 1905 element = $(element);
rlm@37 1906 if (content && content.toElement) content = content.toElement();
rlm@37 1907 else if (!Object.isElement(content)) {
rlm@37 1908 content = Object.toHTML(content);
rlm@37 1909 var range = element.ownerDocument.createRange();
rlm@37 1910 range.selectNode(element);
rlm@37 1911 content.evalScripts.bind(content).defer();
rlm@37 1912 content = range.createContextualFragment(content.stripScripts());
rlm@37 1913 }
rlm@37 1914 element.parentNode.replaceChild(content, element);
rlm@37 1915 return element;
rlm@37 1916 },
rlm@37 1917
rlm@37 1918 insert: function(element, insertions) {
rlm@37 1919 element = $(element);
rlm@37 1920
rlm@37 1921 if (Object.isString(insertions) || Object.isNumber(insertions) ||
rlm@37 1922 Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML)))
rlm@37 1923 insertions = {bottom:insertions};
rlm@37 1924
rlm@37 1925 var content, insert, tagName, childNodes;
rlm@37 1926
rlm@37 1927 for (var position in insertions) {
rlm@37 1928 content = insertions[position];
rlm@37 1929 position = position.toLowerCase();
rlm@37 1930 insert = Element._insertionTranslations[position];
rlm@37 1931
rlm@37 1932 if (content && content.toElement) content = content.toElement();
rlm@37 1933 if (Object.isElement(content)) {
rlm@37 1934 insert(element, content);
rlm@37 1935 continue;
rlm@37 1936 }
rlm@37 1937
rlm@37 1938 content = Object.toHTML(content);
rlm@37 1939
rlm@37 1940 tagName = ((position == 'before' || position == 'after')
rlm@37 1941 ? element.parentNode : element).tagName.toUpperCase();
rlm@37 1942
rlm@37 1943 childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
rlm@37 1944
rlm@37 1945 if (position == 'top' || position == 'after') childNodes.reverse();
rlm@37 1946 childNodes.each(insert.curry(element));
rlm@37 1947
rlm@37 1948 content.evalScripts.bind(content).defer();
rlm@37 1949 }
rlm@37 1950
rlm@37 1951 return element;
rlm@37 1952 },
rlm@37 1953
rlm@37 1954 wrap: function(element, wrapper, attributes) {
rlm@37 1955 element = $(element);
rlm@37 1956 if (Object.isElement(wrapper))
rlm@37 1957 $(wrapper).writeAttribute(attributes || { });
rlm@37 1958 else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes);
rlm@37 1959 else wrapper = new Element('div', wrapper);
rlm@37 1960 if (element.parentNode)
rlm@37 1961 element.parentNode.replaceChild(wrapper, element);
rlm@37 1962 wrapper.appendChild(element);
rlm@37 1963 return wrapper;
rlm@37 1964 },
rlm@37 1965
rlm@37 1966 inspect: function(element) {
rlm@37 1967 element = $(element);
rlm@37 1968 var result = '<' + element.tagName.toLowerCase();
rlm@37 1969 $H({'id': 'id', 'className': 'class'}).each(function(pair) {
rlm@37 1970 var property = pair.first(), attribute = pair.last();
rlm@37 1971 var value = (element[property] || '').toString();
rlm@37 1972 if (value) result += ' ' + attribute + '=' + value.inspect(true);
rlm@37 1973 });
rlm@37 1974 return result + '>';
rlm@37 1975 },
rlm@37 1976
rlm@37 1977 recursivelyCollect: function(element, property) {
rlm@37 1978 element = $(element);
rlm@37 1979 var elements = [];
rlm@37 1980 while (element = element[property])
rlm@37 1981 if (element.nodeType == 1)
rlm@37 1982 elements.push(Element.extend(element));
rlm@37 1983 return elements;
rlm@37 1984 },
rlm@37 1985
rlm@37 1986 ancestors: function(element) {
rlm@37 1987 return Element.recursivelyCollect(element, 'parentNode');
rlm@37 1988 },
rlm@37 1989
rlm@37 1990 descendants: function(element) {
rlm@37 1991 return Element.select(element, "*");
rlm@37 1992 },
rlm@37 1993
rlm@37 1994 firstDescendant: function(element) {
rlm@37 1995 element = $(element).firstChild;
rlm@37 1996 while (element && element.nodeType != 1) element = element.nextSibling;
rlm@37 1997 return $(element);
rlm@37 1998 },
rlm@37 1999
rlm@37 2000 immediateDescendants: function(element) {
rlm@37 2001 if (!(element = $(element).firstChild)) return [];
rlm@37 2002 while (element && element.nodeType != 1) element = element.nextSibling;
rlm@37 2003 if (element) return [element].concat($(element).nextSiblings());
rlm@37 2004 return [];
rlm@37 2005 },
rlm@37 2006
rlm@37 2007 previousSiblings: function(element) {
rlm@37 2008 return Element.recursivelyCollect(element, 'previousSibling');
rlm@37 2009 },
rlm@37 2010
rlm@37 2011 nextSiblings: function(element) {
rlm@37 2012 return Element.recursivelyCollect(element, 'nextSibling');
rlm@37 2013 },
rlm@37 2014
rlm@37 2015 siblings: function(element) {
rlm@37 2016 element = $(element);
rlm@37 2017 return Element.previousSiblings(element).reverse()
rlm@37 2018 .concat(Element.nextSiblings(element));
rlm@37 2019 },
rlm@37 2020
rlm@37 2021 match: function(element, selector) {
rlm@37 2022 if (Object.isString(selector))
rlm@37 2023 selector = new Selector(selector);
rlm@37 2024 return selector.match($(element));
rlm@37 2025 },
rlm@37 2026
rlm@37 2027 up: function(element, expression, index) {
rlm@37 2028 element = $(element);
rlm@37 2029 if (arguments.length == 1) return $(element.parentNode);
rlm@37 2030 var ancestors = Element.ancestors(element);
rlm@37 2031 return Object.isNumber(expression) ? ancestors[expression] :
rlm@37 2032 Selector.findElement(ancestors, expression, index);
rlm@37 2033 },
rlm@37 2034
rlm@37 2035 down: function(element, expression, index) {
rlm@37 2036 element = $(element);
rlm@37 2037 if (arguments.length == 1) return Element.firstDescendant(element);
rlm@37 2038 return Object.isNumber(expression) ? Element.descendants(element)[expression] :
rlm@37 2039 Element.select(element, expression)[index || 0];
rlm@37 2040 },
rlm@37 2041
rlm@37 2042 previous: function(element, expression, index) {
rlm@37 2043 element = $(element);
rlm@37 2044 if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element));
rlm@37 2045 var previousSiblings = Element.previousSiblings(element);
rlm@37 2046 return Object.isNumber(expression) ? previousSiblings[expression] :
rlm@37 2047 Selector.findElement(previousSiblings, expression, index);
rlm@37 2048 },
rlm@37 2049
rlm@37 2050 next: function(element, expression, index) {
rlm@37 2051 element = $(element);
rlm@37 2052 if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element));
rlm@37 2053 var nextSiblings = Element.nextSiblings(element);
rlm@37 2054 return Object.isNumber(expression) ? nextSiblings[expression] :
rlm@37 2055 Selector.findElement(nextSiblings, expression, index);
rlm@37 2056 },
rlm@37 2057
rlm@37 2058
rlm@37 2059 select: function(element) {
rlm@37 2060 var args = Array.prototype.slice.call(arguments, 1);
rlm@37 2061 return Selector.findChildElements(element, args);
rlm@37 2062 },
rlm@37 2063
rlm@37 2064 adjacent: function(element) {
rlm@37 2065 var args = Array.prototype.slice.call(arguments, 1);
rlm@37 2066 return Selector.findChildElements(element.parentNode, args).without(element);
rlm@37 2067 },
rlm@37 2068
rlm@37 2069 identify: function(element) {
rlm@37 2070 element = $(element);
rlm@37 2071 var id = Element.readAttribute(element, 'id');
rlm@37 2072 if (id) return id;
rlm@37 2073 do { id = 'anonymous_element_' + Element.idCounter++ } while ($(id));
rlm@37 2074 Element.writeAttribute(element, 'id', id);
rlm@37 2075 return id;
rlm@37 2076 },
rlm@37 2077
rlm@37 2078 readAttribute: function(element, name) {
rlm@37 2079 element = $(element);
rlm@37 2080 if (Prototype.Browser.IE) {
rlm@37 2081 var t = Element._attributeTranslations.read;
rlm@37 2082 if (t.values[name]) return t.values[name](element, name);
rlm@37 2083 if (t.names[name]) name = t.names[name];
rlm@37 2084 if (name.include(':')) {
rlm@37 2085 return (!element.attributes || !element.attributes[name]) ? null :
rlm@37 2086 element.attributes[name].value;
rlm@37 2087 }
rlm@37 2088 }
rlm@37 2089 return element.getAttribute(name);
rlm@37 2090 },
rlm@37 2091
rlm@37 2092 writeAttribute: function(element, name, value) {
rlm@37 2093 element = $(element);
rlm@37 2094 var attributes = { }, t = Element._attributeTranslations.write;
rlm@37 2095
rlm@37 2096 if (typeof name == 'object') attributes = name;
rlm@37 2097 else attributes[name] = Object.isUndefined(value) ? true : value;
rlm@37 2098
rlm@37 2099 for (var attr in attributes) {
rlm@37 2100 name = t.names[attr] || attr;
rlm@37 2101 value = attributes[attr];
rlm@37 2102 if (t.values[attr]) name = t.values[attr](element, value);
rlm@37 2103 if (value === false || value === null)
rlm@37 2104 element.removeAttribute(name);
rlm@37 2105 else if (value === true)
rlm@37 2106 element.setAttribute(name, name);
rlm@37 2107 else element.setAttribute(name, value);
rlm@37 2108 }
rlm@37 2109 return element;
rlm@37 2110 },
rlm@37 2111
rlm@37 2112 getHeight: function(element) {
rlm@37 2113 return Element.getDimensions(element).height;
rlm@37 2114 },
rlm@37 2115
rlm@37 2116 getWidth: function(element) {
rlm@37 2117 return Element.getDimensions(element).width;
rlm@37 2118 },
rlm@37 2119
rlm@37 2120 classNames: function(element) {
rlm@37 2121 return new Element.ClassNames(element);
rlm@37 2122 },
rlm@37 2123
rlm@37 2124 hasClassName: function(element, className) {
rlm@37 2125 if (!(element = $(element))) return;
rlm@37 2126 var elementClassName = element.className;
rlm@37 2127 return (elementClassName.length > 0 && (elementClassName == className ||
rlm@37 2128 new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
rlm@37 2129 },
rlm@37 2130
rlm@37 2131 addClassName: function(element, className) {
rlm@37 2132 if (!(element = $(element))) return;
rlm@37 2133 if (!Element.hasClassName(element, className))
rlm@37 2134 element.className += (element.className ? ' ' : '') + className;
rlm@37 2135 return element;
rlm@37 2136 },
rlm@37 2137
rlm@37 2138 removeClassName: function(element, className) {
rlm@37 2139 if (!(element = $(element))) return;
rlm@37 2140 element.className = element.className.replace(
rlm@37 2141 new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip();
rlm@37 2142 return element;
rlm@37 2143 },
rlm@37 2144
rlm@37 2145 toggleClassName: function(element, className) {
rlm@37 2146 if (!(element = $(element))) return;
rlm@37 2147 return Element[Element.hasClassName(element, className) ?
rlm@37 2148 'removeClassName' : 'addClassName'](element, className);
rlm@37 2149 },
rlm@37 2150
rlm@37 2151 cleanWhitespace: function(element) {
rlm@37 2152 element = $(element);
rlm@37 2153 var node = element.firstChild;
rlm@37 2154 while (node) {
rlm@37 2155 var nextNode = node.nextSibling;
rlm@37 2156 if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
rlm@37 2157 element.removeChild(node);
rlm@37 2158 node = nextNode;
rlm@37 2159 }
rlm@37 2160 return element;
rlm@37 2161 },
rlm@37 2162
rlm@37 2163 empty: function(element) {
rlm@37 2164 return $(element).innerHTML.blank();
rlm@37 2165 },
rlm@37 2166
rlm@37 2167 descendantOf: function(element, ancestor) {
rlm@37 2168 element = $(element), ancestor = $(ancestor);
rlm@37 2169
rlm@37 2170 if (element.compareDocumentPosition)
rlm@37 2171 return (element.compareDocumentPosition(ancestor) & 8) === 8;
rlm@37 2172
rlm@37 2173 if (ancestor.contains)
rlm@37 2174 return ancestor.contains(element) && ancestor !== element;
rlm@37 2175
rlm@37 2176 while (element = element.parentNode)
rlm@37 2177 if (element == ancestor) return true;
rlm@37 2178
rlm@37 2179 return false;
rlm@37 2180 },
rlm@37 2181
rlm@37 2182 scrollTo: function(element) {
rlm@37 2183 element = $(element);
rlm@37 2184 var pos = Element.cumulativeOffset(element);
rlm@37 2185 window.scrollTo(pos[0], pos[1]);
rlm@37 2186 return element;
rlm@37 2187 },
rlm@37 2188
rlm@37 2189 getStyle: function(element, style) {
rlm@37 2190 element = $(element);
rlm@37 2191 style = style == 'float' ? 'cssFloat' : style.camelize();
rlm@37 2192 var value = element.style[style];
rlm@37 2193 if (!value || value == 'auto') {
rlm@37 2194 var css = document.defaultView.getComputedStyle(element, null);
rlm@37 2195 value = css ? css[style] : null;
rlm@37 2196 }
rlm@37 2197 if (style == 'opacity') return value ? parseFloat(value) : 1.0;
rlm@37 2198 return value == 'auto' ? null : value;
rlm@37 2199 },
rlm@37 2200
rlm@37 2201 getOpacity: function(element) {
rlm@37 2202 return $(element).getStyle('opacity');
rlm@37 2203 },
rlm@37 2204
rlm@37 2205 setStyle: function(element, styles) {
rlm@37 2206 element = $(element);
rlm@37 2207 var elementStyle = element.style, match;
rlm@37 2208 if (Object.isString(styles)) {
rlm@37 2209 element.style.cssText += ';' + styles;
rlm@37 2210 return styles.include('opacity') ?
rlm@37 2211 element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
rlm@37 2212 }
rlm@37 2213 for (var property in styles)
rlm@37 2214 if (property == 'opacity') element.setOpacity(styles[property]);
rlm@37 2215 else
rlm@37 2216 elementStyle[(property == 'float' || property == 'cssFloat') ?
rlm@37 2217 (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
rlm@37 2218 property] = styles[property];
rlm@37 2219
rlm@37 2220 return element;
rlm@37 2221 },
rlm@37 2222
rlm@37 2223 setOpacity: function(element, value) {
rlm@37 2224 element = $(element);
rlm@37 2225 element.style.opacity = (value == 1 || value === '') ? '' :
rlm@37 2226 (value < 0.00001) ? 0 : value;
rlm@37 2227 return element;
rlm@37 2228 },
rlm@37 2229
rlm@37 2230 getDimensions: function(element) {
rlm@37 2231 element = $(element);
rlm@37 2232 var display = Element.getStyle(element, 'display');
rlm@37 2233 if (display != 'none' && display != null) // Safari bug
rlm@37 2234 return {width: element.offsetWidth, height: element.offsetHeight};
rlm@37 2235
rlm@37 2236 var els = element.style;
rlm@37 2237 var originalVisibility = els.visibility;
rlm@37 2238 var originalPosition = els.position;
rlm@37 2239 var originalDisplay = els.display;
rlm@37 2240 els.visibility = 'hidden';
rlm@37 2241 if (originalPosition != 'fixed') // Switching fixed to absolute causes issues in Safari
rlm@37 2242 els.position = 'absolute';
rlm@37 2243 els.display = 'block';
rlm@37 2244 var originalWidth = element.clientWidth;
rlm@37 2245 var originalHeight = element.clientHeight;
rlm@37 2246 els.display = originalDisplay;
rlm@37 2247 els.position = originalPosition;
rlm@37 2248 els.visibility = originalVisibility;
rlm@37 2249 return {width: originalWidth, height: originalHeight};
rlm@37 2250 },
rlm@37 2251
rlm@37 2252 makePositioned: function(element) {
rlm@37 2253 element = $(element);
rlm@37 2254 var pos = Element.getStyle(element, 'position');
rlm@37 2255 if (pos == 'static' || !pos) {
rlm@37 2256 element._madePositioned = true;
rlm@37 2257 element.style.position = 'relative';
rlm@37 2258 if (Prototype.Browser.Opera) {
rlm@37 2259 element.style.top = 0;
rlm@37 2260 element.style.left = 0;
rlm@37 2261 }
rlm@37 2262 }
rlm@37 2263 return element;
rlm@37 2264 },
rlm@37 2265
rlm@37 2266 undoPositioned: function(element) {
rlm@37 2267 element = $(element);
rlm@37 2268 if (element._madePositioned) {
rlm@37 2269 element._madePositioned = undefined;
rlm@37 2270 element.style.position =
rlm@37 2271 element.style.top =
rlm@37 2272 element.style.left =
rlm@37 2273 element.style.bottom =
rlm@37 2274 element.style.right = '';
rlm@37 2275 }
rlm@37 2276 return element;
rlm@37 2277 },
rlm@37 2278
rlm@37 2279 makeClipping: function(element) {
rlm@37 2280 element = $(element);
rlm@37 2281 if (element._overflow) return element;
rlm@37 2282 element._overflow = Element.getStyle(element, 'overflow') || 'auto';
rlm@37 2283 if (element._overflow !== 'hidden')
rlm@37 2284 element.style.overflow = 'hidden';
rlm@37 2285 return element;
rlm@37 2286 },
rlm@37 2287
rlm@37 2288 undoClipping: function(element) {
rlm@37 2289 element = $(element);
rlm@37 2290 if (!element._overflow) return element;
rlm@37 2291 element.style.overflow = element._overflow == 'auto' ? '' : element._overflow;
rlm@37 2292 element._overflow = null;
rlm@37 2293 return element;
rlm@37 2294 },
rlm@37 2295
rlm@37 2296 cumulativeOffset: function(element) {
rlm@37 2297 var valueT = 0, valueL = 0;
rlm@37 2298 do {
rlm@37 2299 valueT += element.offsetTop || 0;
rlm@37 2300 valueL += element.offsetLeft || 0;
rlm@37 2301 element = element.offsetParent;
rlm@37 2302 } while (element);
rlm@37 2303 return Element._returnOffset(valueL, valueT);
rlm@37 2304 },
rlm@37 2305
rlm@37 2306 positionedOffset: function(element) {
rlm@37 2307 var valueT = 0, valueL = 0;
rlm@37 2308 do {
rlm@37 2309 valueT += element.offsetTop || 0;
rlm@37 2310 valueL += element.offsetLeft || 0;
rlm@37 2311 element = element.offsetParent;
rlm@37 2312 if (element) {
rlm@37 2313 if (element.tagName.toUpperCase() == 'BODY') break;
rlm@37 2314 var p = Element.getStyle(element, 'position');
rlm@37 2315 if (p !== 'static') break;
rlm@37 2316 }
rlm@37 2317 } while (element);
rlm@37 2318 return Element._returnOffset(valueL, valueT);
rlm@37 2319 },
rlm@37 2320
rlm@37 2321 absolutize: function(element) {
rlm@37 2322 element = $(element);
rlm@37 2323 if (Element.getStyle(element, 'position') == 'absolute') return element;
rlm@37 2324
rlm@37 2325 var offsets = Element.positionedOffset(element);
rlm@37 2326 var top = offsets[1];
rlm@37 2327 var left = offsets[0];
rlm@37 2328 var width = element.clientWidth;
rlm@37 2329 var height = element.clientHeight;
rlm@37 2330
rlm@37 2331 element._originalLeft = left - parseFloat(element.style.left || 0);
rlm@37 2332 element._originalTop = top - parseFloat(element.style.top || 0);
rlm@37 2333 element._originalWidth = element.style.width;
rlm@37 2334 element._originalHeight = element.style.height;
rlm@37 2335
rlm@37 2336 element.style.position = 'absolute';
rlm@37 2337 element.style.top = top + 'px';
rlm@37 2338 element.style.left = left + 'px';
rlm@37 2339 element.style.width = width + 'px';
rlm@37 2340 element.style.height = height + 'px';
rlm@37 2341 return element;
rlm@37 2342 },
rlm@37 2343
rlm@37 2344 relativize: function(element) {
rlm@37 2345 element = $(element);
rlm@37 2346 if (Element.getStyle(element, 'position') == 'relative') return element;
rlm@37 2347
rlm@37 2348 element.style.position = 'relative';
rlm@37 2349 var top = parseFloat(element.style.top || 0) - (element._originalTop || 0);
rlm@37 2350 var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0);
rlm@37 2351
rlm@37 2352 element.style.top = top + 'px';
rlm@37 2353 element.style.left = left + 'px';
rlm@37 2354 element.style.height = element._originalHeight;
rlm@37 2355 element.style.width = element._originalWidth;
rlm@37 2356 return element;
rlm@37 2357 },
rlm@37 2358
rlm@37 2359 cumulativeScrollOffset: function(element) {
rlm@37 2360 var valueT = 0, valueL = 0;
rlm@37 2361 do {
rlm@37 2362 valueT += element.scrollTop || 0;
rlm@37 2363 valueL += element.scrollLeft || 0;
rlm@37 2364 element = element.parentNode;
rlm@37 2365 } while (element);
rlm@37 2366 return Element._returnOffset(valueL, valueT);
rlm@37 2367 },
rlm@37 2368
rlm@37 2369 getOffsetParent: function(element) {
rlm@37 2370 if (element.offsetParent) return $(element.offsetParent);
rlm@37 2371 if (element == document.body) return $(element);
rlm@37 2372
rlm@37 2373 while ((element = element.parentNode) && element != document.body)
rlm@37 2374 if (Element.getStyle(element, 'position') != 'static')
rlm@37 2375 return $(element);
rlm@37 2376
rlm@37 2377 return $(document.body);
rlm@37 2378 },
rlm@37 2379
rlm@37 2380 viewportOffset: function(forElement) {
rlm@37 2381 var valueT = 0, valueL = 0;
rlm@37 2382
rlm@37 2383 var element = forElement;
rlm@37 2384 do {
rlm@37 2385 valueT += element.offsetTop || 0;
rlm@37 2386 valueL += element.offsetLeft || 0;
rlm@37 2387
rlm@37 2388 if (element.offsetParent == document.body &&
rlm@37 2389 Element.getStyle(element, 'position') == 'absolute') break;
rlm@37 2390
rlm@37 2391 } while (element = element.offsetParent);
rlm@37 2392
rlm@37 2393 element = forElement;
rlm@37 2394 do {
rlm@37 2395 if (!Prototype.Browser.Opera || (element.tagName && (element.tagName.toUpperCase() == 'BODY'))) {
rlm@37 2396 valueT -= element.scrollTop || 0;
rlm@37 2397 valueL -= element.scrollLeft || 0;
rlm@37 2398 }
rlm@37 2399 } while (element = element.parentNode);
rlm@37 2400
rlm@37 2401 return Element._returnOffset(valueL, valueT);
rlm@37 2402 },
rlm@37 2403
rlm@37 2404 clonePosition: function(element, source) {
rlm@37 2405 var options = Object.extend({
rlm@37 2406 setLeft: true,
rlm@37 2407 setTop: true,
rlm@37 2408 setWidth: true,
rlm@37 2409 setHeight: true,
rlm@37 2410 offsetTop: 0,
rlm@37 2411 offsetLeft: 0
rlm@37 2412 }, arguments[2] || { });
rlm@37 2413
rlm@37 2414 source = $(source);
rlm@37 2415 var p = Element.viewportOffset(source);
rlm@37 2416
rlm@37 2417 element = $(element);
rlm@37 2418 var delta = [0, 0];
rlm@37 2419 var parent = null;
rlm@37 2420 if (Element.getStyle(element, 'position') == 'absolute') {
rlm@37 2421 parent = Element.getOffsetParent(element);
rlm@37 2422 delta = Element.viewportOffset(parent);
rlm@37 2423 }
rlm@37 2424
rlm@37 2425 if (parent == document.body) {
rlm@37 2426 delta[0] -= document.body.offsetLeft;
rlm@37 2427 delta[1] -= document.body.offsetTop;
rlm@37 2428 }
rlm@37 2429
rlm@37 2430 if (options.setLeft) element.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px';
rlm@37 2431 if (options.setTop) element.style.top = (p[1] - delta[1] + options.offsetTop) + 'px';
rlm@37 2432 if (options.setWidth) element.style.width = source.offsetWidth + 'px';
rlm@37 2433 if (options.setHeight) element.style.height = source.offsetHeight + 'px';
rlm@37 2434 return element;
rlm@37 2435 }
rlm@37 2436 };
rlm@37 2437
rlm@37 2438 Object.extend(Element.Methods, {
rlm@37 2439 getElementsBySelector: Element.Methods.select,
rlm@37 2440
rlm@37 2441 childElements: Element.Methods.immediateDescendants
rlm@37 2442 });
rlm@37 2443
rlm@37 2444 Element._attributeTranslations = {
rlm@37 2445 write: {
rlm@37 2446 names: {
rlm@37 2447 className: 'class',
rlm@37 2448 htmlFor: 'for'
rlm@37 2449 },
rlm@37 2450 values: { }
rlm@37 2451 }
rlm@37 2452 };
rlm@37 2453
rlm@37 2454 if (Prototype.Browser.Opera) {
rlm@37 2455 Element.Methods.getStyle = Element.Methods.getStyle.wrap(
rlm@37 2456 function(proceed, element, style) {
rlm@37 2457 switch (style) {
rlm@37 2458 case 'left': case 'top': case 'right': case 'bottom':
rlm@37 2459 if (proceed(element, 'position') === 'static') return null;
rlm@37 2460 case 'height': case 'width':
rlm@37 2461 if (!Element.visible(element)) return null;
rlm@37 2462
rlm@37 2463 var dim = parseInt(proceed(element, style), 10);
rlm@37 2464
rlm@37 2465 if (dim !== element['offset' + style.capitalize()])
rlm@37 2466 return dim + 'px';
rlm@37 2467
rlm@37 2468 var properties;
rlm@37 2469 if (style === 'height') {
rlm@37 2470 properties = ['border-top-width', 'padding-top',
rlm@37 2471 'padding-bottom', 'border-bottom-width'];
rlm@37 2472 }
rlm@37 2473 else {
rlm@37 2474 properties = ['border-left-width', 'padding-left',
rlm@37 2475 'padding-right', 'border-right-width'];
rlm@37 2476 }
rlm@37 2477 return properties.inject(dim, function(memo, property) {
rlm@37 2478 var val = proceed(element, property);
rlm@37 2479 return val === null ? memo : memo - parseInt(val, 10);
rlm@37 2480 }) + 'px';
rlm@37 2481 default: return proceed(element, style);
rlm@37 2482 }
rlm@37 2483 }
rlm@37 2484 );
rlm@37 2485
rlm@37 2486 Element.Methods.readAttribute = Element.Methods.readAttribute.wrap(
rlm@37 2487 function(proceed, element, attribute) {
rlm@37 2488 if (attribute === 'title') return element.title;
rlm@37 2489 return proceed(element, attribute);
rlm@37 2490 }
rlm@37 2491 );
rlm@37 2492 }
rlm@37 2493
rlm@37 2494 else if (Prototype.Browser.IE) {
rlm@37 2495 Element.Methods.getOffsetParent = Element.Methods.getOffsetParent.wrap(
rlm@37 2496 function(proceed, element) {
rlm@37 2497 element = $(element);
rlm@37 2498 try { element.offsetParent }
rlm@37 2499 catch(e) { return $(document.body) }
rlm@37 2500 var position = element.getStyle('position');
rlm@37 2501 if (position !== 'static') return proceed(element);
rlm@37 2502 element.setStyle({ position: 'relative' });
rlm@37 2503 var value = proceed(element);
rlm@37 2504 element.setStyle({ position: position });
rlm@37 2505 return value;
rlm@37 2506 }
rlm@37 2507 );
rlm@37 2508
rlm@37 2509 $w('positionedOffset viewportOffset').each(function(method) {
rlm@37 2510 Element.Methods[method] = Element.Methods[method].wrap(
rlm@37 2511 function(proceed, element) {
rlm@37 2512 element = $(element);
rlm@37 2513 try { element.offsetParent }
rlm@37 2514 catch(e) { return Element._returnOffset(0,0) }
rlm@37 2515 var position = element.getStyle('position');
rlm@37 2516 if (position !== 'static') return proceed(element);
rlm@37 2517 var offsetParent = element.getOffsetParent();
rlm@37 2518 if (offsetParent && offsetParent.getStyle('position') === 'fixed')
rlm@37 2519 offsetParent.setStyle({ zoom: 1 });
rlm@37 2520 element.setStyle({ position: 'relative' });
rlm@37 2521 var value = proceed(element);
rlm@37 2522 element.setStyle({ position: position });
rlm@37 2523 return value;
rlm@37 2524 }
rlm@37 2525 );
rlm@37 2526 });
rlm@37 2527
rlm@37 2528 Element.Methods.cumulativeOffset = Element.Methods.cumulativeOffset.wrap(
rlm@37 2529 function(proceed, element) {
rlm@37 2530 try { element.offsetParent }
rlm@37 2531 catch(e) { return Element._returnOffset(0,0) }
rlm@37 2532 return proceed(element);
rlm@37 2533 }
rlm@37 2534 );
rlm@37 2535
rlm@37 2536 Element.Methods.getStyle = function(element, style) {
rlm@37 2537 element = $(element);
rlm@37 2538 style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize();
rlm@37 2539 var value = element.style[style];
rlm@37 2540 if (!value && element.currentStyle) value = element.currentStyle[style];
rlm@37 2541
rlm@37 2542 if (style == 'opacity') {
rlm@37 2543 if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/))
rlm@37 2544 if (value[1]) return parseFloat(value[1]) / 100;
rlm@37 2545 return 1.0;
rlm@37 2546 }
rlm@37 2547
rlm@37 2548 if (value == 'auto') {
rlm@37 2549 if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none'))
rlm@37 2550 return element['offset' + style.capitalize()] + 'px';
rlm@37 2551 return null;
rlm@37 2552 }
rlm@37 2553 return value;
rlm@37 2554 };
rlm@37 2555
rlm@37 2556 Element.Methods.setOpacity = function(element, value) {
rlm@37 2557 function stripAlpha(filter){
rlm@37 2558 return filter.replace(/alpha\([^\)]*\)/gi,'');
rlm@37 2559 }
rlm@37 2560 element = $(element);
rlm@37 2561 var currentStyle = element.currentStyle;
rlm@37 2562 if ((currentStyle && !currentStyle.hasLayout) ||
rlm@37 2563 (!currentStyle && element.style.zoom == 'normal'))
rlm@37 2564 element.style.zoom = 1;
rlm@37 2565
rlm@37 2566 var filter = element.getStyle('filter'), style = element.style;
rlm@37 2567 if (value == 1 || value === '') {
rlm@37 2568 (filter = stripAlpha(filter)) ?
rlm@37 2569 style.filter = filter : style.removeAttribute('filter');
rlm@37 2570 return element;
rlm@37 2571 } else if (value < 0.00001) value = 0;
rlm@37 2572 style.filter = stripAlpha(filter) +
rlm@37 2573 'alpha(opacity=' + (value * 100) + ')';
rlm@37 2574 return element;
rlm@37 2575 };
rlm@37 2576
rlm@37 2577 Element._attributeTranslations = (function(){
rlm@37 2578
rlm@37 2579 var classProp = 'className';
rlm@37 2580 var forProp = 'for';
rlm@37 2581
rlm@37 2582 var el = document.createElement('div');
rlm@37 2583
rlm@37 2584 el.setAttribute(classProp, 'x');
rlm@37 2585
rlm@37 2586 if (el.className !== 'x') {
rlm@37 2587 el.setAttribute('class', 'x');
rlm@37 2588 if (el.className === 'x') {
rlm@37 2589 classProp = 'class';
rlm@37 2590 }
rlm@37 2591 }
rlm@37 2592 el = null;
rlm@37 2593
rlm@37 2594 el = document.createElement('label');
rlm@37 2595 el.setAttribute(forProp, 'x');
rlm@37 2596 if (el.htmlFor !== 'x') {
rlm@37 2597 el.setAttribute('htmlFor', 'x');
rlm@37 2598 if (el.htmlFor === 'x') {
rlm@37 2599 forProp = 'htmlFor';
rlm@37 2600 }
rlm@37 2601 }
rlm@37 2602 el = null;
rlm@37 2603
rlm@37 2604 return {
rlm@37 2605 read: {
rlm@37 2606 names: {
rlm@37 2607 'class': classProp,
rlm@37 2608 'className': classProp,
rlm@37 2609 'for': forProp,
rlm@37 2610 'htmlFor': forProp
rlm@37 2611 },
rlm@37 2612 values: {
rlm@37 2613 _getAttr: function(element, attribute) {
rlm@37 2614 return element.getAttribute(attribute);
rlm@37 2615 },
rlm@37 2616 _getAttr2: function(element, attribute) {
rlm@37 2617 return element.getAttribute(attribute, 2);
rlm@37 2618 },
rlm@37 2619 _getAttrNode: function(element, attribute) {
rlm@37 2620 var node = element.getAttributeNode(attribute);
rlm@37 2621 return node ? node.value : "";
rlm@37 2622 },
rlm@37 2623 _getEv: (function(){
rlm@37 2624
rlm@37 2625 var el = document.createElement('div');
rlm@37 2626 el.onclick = Prototype.emptyFunction;
rlm@37 2627 var value = el.getAttribute('onclick');
rlm@37 2628 var f;
rlm@37 2629
rlm@37 2630 if (String(value).indexOf('{') > -1) {
rlm@37 2631 f = function(element, attribute) {
rlm@37 2632 attribute = element.getAttribute(attribute);
rlm@37 2633 if (!attribute) return null;
rlm@37 2634 attribute = attribute.toString();
rlm@37 2635 attribute = attribute.split('{')[1];
rlm@37 2636 attribute = attribute.split('}')[0];
rlm@37 2637 return attribute.strip();
rlm@37 2638 };
rlm@37 2639 }
rlm@37 2640 else if (value === '') {
rlm@37 2641 f = function(element, attribute) {
rlm@37 2642 attribute = element.getAttribute(attribute);
rlm@37 2643 if (!attribute) return null;
rlm@37 2644 return attribute.strip();
rlm@37 2645 };
rlm@37 2646 }
rlm@37 2647 el = null;
rlm@37 2648 return f;
rlm@37 2649 })(),
rlm@37 2650 _flag: function(element, attribute) {
rlm@37 2651 return $(element).hasAttribute(attribute) ? attribute : null;
rlm@37 2652 },
rlm@37 2653 style: function(element) {
rlm@37 2654 return element.style.cssText.toLowerCase();
rlm@37 2655 },
rlm@37 2656 title: function(element) {
rlm@37 2657 return element.title;
rlm@37 2658 }
rlm@37 2659 }
rlm@37 2660 }
rlm@37 2661 }
rlm@37 2662 })();
rlm@37 2663
rlm@37 2664 Element._attributeTranslations.write = {
rlm@37 2665 names: Object.extend({
rlm@37 2666 cellpadding: 'cellPadding',
rlm@37 2667 cellspacing: 'cellSpacing'
rlm@37 2668 }, Element._attributeTranslations.read.names),
rlm@37 2669 values: {
rlm@37 2670 checked: function(element, value) {
rlm@37 2671 element.checked = !!value;
rlm@37 2672 },
rlm@37 2673
rlm@37 2674 style: function(element, value) {
rlm@37 2675 element.style.cssText = value ? value : '';
rlm@37 2676 }
rlm@37 2677 }
rlm@37 2678 };
rlm@37 2679
rlm@37 2680 Element._attributeTranslations.has = {};
rlm@37 2681
rlm@37 2682 $w('colSpan rowSpan vAlign dateTime accessKey tabIndex ' +
rlm@37 2683 'encType maxLength readOnly longDesc frameBorder').each(function(attr) {
rlm@37 2684 Element._attributeTranslations.write.names[attr.toLowerCase()] = attr;
rlm@37 2685 Element._attributeTranslations.has[attr.toLowerCase()] = attr;
rlm@37 2686 });
rlm@37 2687
rlm@37 2688 (function(v) {
rlm@37 2689 Object.extend(v, {
rlm@37 2690 href: v._getAttr2,
rlm@37 2691 src: v._getAttr2,
rlm@37 2692 type: v._getAttr,
rlm@37 2693 action: v._getAttrNode,
rlm@37 2694 disabled: v._flag,
rlm@37 2695 checked: v._flag,
rlm@37 2696 readonly: v._flag,
rlm@37 2697 multiple: v._flag,
rlm@37 2698 onload: v._getEv,
rlm@37 2699 onunload: v._getEv,
rlm@37 2700 onclick: v._getEv,
rlm@37 2701 ondblclick: v._getEv,
rlm@37 2702 onmousedown: v._getEv,
rlm@37 2703 onmouseup: v._getEv,
rlm@37 2704 onmouseover: v._getEv,
rlm@37 2705 onmousemove: v._getEv,
rlm@37 2706 onmouseout: v._getEv,
rlm@37 2707 onfocus: v._getEv,
rlm@37 2708 onblur: v._getEv,
rlm@37 2709 onkeypress: v._getEv,
rlm@37 2710 onkeydown: v._getEv,
rlm@37 2711 onkeyup: v._getEv,
rlm@37 2712 onsubmit: v._getEv,
rlm@37 2713 onreset: v._getEv,
rlm@37 2714 onselect: v._getEv,
rlm@37 2715 onchange: v._getEv
rlm@37 2716 });
rlm@37 2717 })(Element._attributeTranslations.read.values);
rlm@37 2718
rlm@37 2719 if (Prototype.BrowserFeatures.ElementExtensions) {
rlm@37 2720 (function() {
rlm@37 2721 function _descendants(element) {
rlm@37 2722 var nodes = element.getElementsByTagName('*'), results = [];
rlm@37 2723 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 2724 if (node.tagName !== "!") // Filter out comment nodes.
rlm@37 2725 results.push(node);
rlm@37 2726 return results;
rlm@37 2727 }
rlm@37 2728
rlm@37 2729 Element.Methods.down = function(element, expression, index) {
rlm@37 2730 element = $(element);
rlm@37 2731 if (arguments.length == 1) return element.firstDescendant();
rlm@37 2732 return Object.isNumber(expression) ? _descendants(element)[expression] :
rlm@37 2733 Element.select(element, expression)[index || 0];
rlm@37 2734 }
rlm@37 2735 })();
rlm@37 2736 }
rlm@37 2737
rlm@37 2738 }
rlm@37 2739
rlm@37 2740 else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) {
rlm@37 2741 Element.Methods.setOpacity = function(element, value) {
rlm@37 2742 element = $(element);
rlm@37 2743 element.style.opacity = (value == 1) ? 0.999999 :
rlm@37 2744 (value === '') ? '' : (value < 0.00001) ? 0 : value;
rlm@37 2745 return element;
rlm@37 2746 };
rlm@37 2747 }
rlm@37 2748
rlm@37 2749 else if (Prototype.Browser.WebKit) {
rlm@37 2750 Element.Methods.setOpacity = function(element, value) {
rlm@37 2751 element = $(element);
rlm@37 2752 element.style.opacity = (value == 1 || value === '') ? '' :
rlm@37 2753 (value < 0.00001) ? 0 : value;
rlm@37 2754
rlm@37 2755 if (value == 1)
rlm@37 2756 if(element.tagName.toUpperCase() == 'IMG' && element.width) {
rlm@37 2757 element.width++; element.width--;
rlm@37 2758 } else try {
rlm@37 2759 var n = document.createTextNode(' ');
rlm@37 2760 element.appendChild(n);
rlm@37 2761 element.removeChild(n);
rlm@37 2762 } catch (e) { }
rlm@37 2763
rlm@37 2764 return element;
rlm@37 2765 };
rlm@37 2766
rlm@37 2767 Element.Methods.cumulativeOffset = function(element) {
rlm@37 2768 var valueT = 0, valueL = 0;
rlm@37 2769 do {
rlm@37 2770 valueT += element.offsetTop || 0;
rlm@37 2771 valueL += element.offsetLeft || 0;
rlm@37 2772 if (element.offsetParent == document.body)
rlm@37 2773 if (Element.getStyle(element, 'position') == 'absolute') break;
rlm@37 2774
rlm@37 2775 element = element.offsetParent;
rlm@37 2776 } while (element);
rlm@37 2777
rlm@37 2778 return Element._returnOffset(valueL, valueT);
rlm@37 2779 };
rlm@37 2780 }
rlm@37 2781
rlm@37 2782 if ('outerHTML' in document.documentElement) {
rlm@37 2783 Element.Methods.replace = function(element, content) {
rlm@37 2784 element = $(element);
rlm@37 2785
rlm@37 2786 if (content && content.toElement) content = content.toElement();
rlm@37 2787 if (Object.isElement(content)) {
rlm@37 2788 element.parentNode.replaceChild(content, element);
rlm@37 2789 return element;
rlm@37 2790 }
rlm@37 2791
rlm@37 2792 content = Object.toHTML(content);
rlm@37 2793 var parent = element.parentNode, tagName = parent.tagName.toUpperCase();
rlm@37 2794
rlm@37 2795 if (Element._insertionTranslations.tags[tagName]) {
rlm@37 2796 var nextSibling = element.next();
rlm@37 2797 var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
rlm@37 2798 parent.removeChild(element);
rlm@37 2799 if (nextSibling)
rlm@37 2800 fragments.each(function(node) { parent.insertBefore(node, nextSibling) });
rlm@37 2801 else
rlm@37 2802 fragments.each(function(node) { parent.appendChild(node) });
rlm@37 2803 }
rlm@37 2804 else element.outerHTML = content.stripScripts();
rlm@37 2805
rlm@37 2806 content.evalScripts.bind(content).defer();
rlm@37 2807 return element;
rlm@37 2808 };
rlm@37 2809 }
rlm@37 2810
rlm@37 2811 Element._returnOffset = function(l, t) {
rlm@37 2812 var result = [l, t];
rlm@37 2813 result.left = l;
rlm@37 2814 result.top = t;
rlm@37 2815 return result;
rlm@37 2816 };
rlm@37 2817
rlm@37 2818 Element._getContentFromAnonymousElement = function(tagName, html) {
rlm@37 2819 var div = new Element('div'), t = Element._insertionTranslations.tags[tagName];
rlm@37 2820 if (t) {
rlm@37 2821 div.innerHTML = t[0] + html + t[1];
rlm@37 2822 t[2].times(function() { div = div.firstChild });
rlm@37 2823 } else div.innerHTML = html;
rlm@37 2824 return $A(div.childNodes);
rlm@37 2825 };
rlm@37 2826
rlm@37 2827 Element._insertionTranslations = {
rlm@37 2828 before: function(element, node) {
rlm@37 2829 element.parentNode.insertBefore(node, element);
rlm@37 2830 },
rlm@37 2831 top: function(element, node) {
rlm@37 2832 element.insertBefore(node, element.firstChild);
rlm@37 2833 },
rlm@37 2834 bottom: function(element, node) {
rlm@37 2835 element.appendChild(node);
rlm@37 2836 },
rlm@37 2837 after: function(element, node) {
rlm@37 2838 element.parentNode.insertBefore(node, element.nextSibling);
rlm@37 2839 },
rlm@37 2840 tags: {
rlm@37 2841 TABLE: ['<table>', '</table>', 1],
rlm@37 2842 TBODY: ['<table><tbody>', '</tbody></table>', 2],
rlm@37 2843 TR: ['<table><tbody><tr>', '</tr></tbody></table>', 3],
rlm@37 2844 TD: ['<table><tbody><tr><td>', '</td></tr></tbody></table>', 4],
rlm@37 2845 SELECT: ['<select>', '</select>', 1]
rlm@37 2846 }
rlm@37 2847 };
rlm@37 2848
rlm@37 2849 (function() {
rlm@37 2850 var tags = Element._insertionTranslations.tags;
rlm@37 2851 Object.extend(tags, {
rlm@37 2852 THEAD: tags.TBODY,
rlm@37 2853 TFOOT: tags.TBODY,
rlm@37 2854 TH: tags.TD
rlm@37 2855 });
rlm@37 2856 })();
rlm@37 2857
rlm@37 2858 Element.Methods.Simulated = {
rlm@37 2859 hasAttribute: function(element, attribute) {
rlm@37 2860 attribute = Element._attributeTranslations.has[attribute] || attribute;
rlm@37 2861 var node = $(element).getAttributeNode(attribute);
rlm@37 2862 return !!(node && node.specified);
rlm@37 2863 }
rlm@37 2864 };
rlm@37 2865
rlm@37 2866 Element.Methods.ByTag = { };
rlm@37 2867
rlm@37 2868 Object.extend(Element, Element.Methods);
rlm@37 2869
rlm@37 2870 (function(div) {
rlm@37 2871
rlm@37 2872 if (!Prototype.BrowserFeatures.ElementExtensions && div['__proto__']) {
rlm@37 2873 window.HTMLElement = { };
rlm@37 2874 window.HTMLElement.prototype = div['__proto__'];
rlm@37 2875 Prototype.BrowserFeatures.ElementExtensions = true;
rlm@37 2876 }
rlm@37 2877
rlm@37 2878 div = null;
rlm@37 2879
rlm@37 2880 })(document.createElement('div'))
rlm@37 2881
rlm@37 2882 Element.extend = (function() {
rlm@37 2883
rlm@37 2884 function checkDeficiency(tagName) {
rlm@37 2885 if (typeof window.Element != 'undefined') {
rlm@37 2886 var proto = window.Element.prototype;
rlm@37 2887 if (proto) {
rlm@37 2888 var id = '_' + (Math.random()+'').slice(2);
rlm@37 2889 var el = document.createElement(tagName);
rlm@37 2890 proto[id] = 'x';
rlm@37 2891 var isBuggy = (el[id] !== 'x');
rlm@37 2892 delete proto[id];
rlm@37 2893 el = null;
rlm@37 2894 return isBuggy;
rlm@37 2895 }
rlm@37 2896 }
rlm@37 2897 return false;
rlm@37 2898 }
rlm@37 2899
rlm@37 2900 function extendElementWith(element, methods) {
rlm@37 2901 for (var property in methods) {
rlm@37 2902 var value = methods[property];
rlm@37 2903 if (Object.isFunction(value) && !(property in element))
rlm@37 2904 element[property] = value.methodize();
rlm@37 2905 }
rlm@37 2906 }
rlm@37 2907
rlm@37 2908 var HTMLOBJECTELEMENT_PROTOTYPE_BUGGY = checkDeficiency('object');
rlm@37 2909
rlm@37 2910 if (Prototype.BrowserFeatures.SpecificElementExtensions) {
rlm@37 2911 if (HTMLOBJECTELEMENT_PROTOTYPE_BUGGY) {
rlm@37 2912 return function(element) {
rlm@37 2913 if (element && typeof element._extendedByPrototype == 'undefined') {
rlm@37 2914 var t = element.tagName;
rlm@37 2915 if (t && (/^(?:object|applet|embed)$/i.test(t))) {
rlm@37 2916 extendElementWith(element, Element.Methods);
rlm@37 2917 extendElementWith(element, Element.Methods.Simulated);
rlm@37 2918 extendElementWith(element, Element.Methods.ByTag[t.toUpperCase()]);
rlm@37 2919 }
rlm@37 2920 }
rlm@37 2921 return element;
rlm@37 2922 }
rlm@37 2923 }
rlm@37 2924 return Prototype.K;
rlm@37 2925 }
rlm@37 2926
rlm@37 2927 var Methods = { }, ByTag = Element.Methods.ByTag;
rlm@37 2928
rlm@37 2929 var extend = Object.extend(function(element) {
rlm@37 2930 if (!element || typeof element._extendedByPrototype != 'undefined' ||
rlm@37 2931 element.nodeType != 1 || element == window) return element;
rlm@37 2932
rlm@37 2933 var methods = Object.clone(Methods),
rlm@37 2934 tagName = element.tagName.toUpperCase();
rlm@37 2935
rlm@37 2936 if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]);
rlm@37 2937
rlm@37 2938 extendElementWith(element, methods);
rlm@37 2939
rlm@37 2940 element._extendedByPrototype = Prototype.emptyFunction;
rlm@37 2941 return element;
rlm@37 2942
rlm@37 2943 }, {
rlm@37 2944 refresh: function() {
rlm@37 2945 if (!Prototype.BrowserFeatures.ElementExtensions) {
rlm@37 2946 Object.extend(Methods, Element.Methods);
rlm@37 2947 Object.extend(Methods, Element.Methods.Simulated);
rlm@37 2948 }
rlm@37 2949 }
rlm@37 2950 });
rlm@37 2951
rlm@37 2952 extend.refresh();
rlm@37 2953 return extend;
rlm@37 2954 })();
rlm@37 2955
rlm@37 2956 Element.hasAttribute = function(element, attribute) {
rlm@37 2957 if (element.hasAttribute) return element.hasAttribute(attribute);
rlm@37 2958 return Element.Methods.Simulated.hasAttribute(element, attribute);
rlm@37 2959 };
rlm@37 2960
rlm@37 2961 Element.addMethods = function(methods) {
rlm@37 2962 var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag;
rlm@37 2963
rlm@37 2964 if (!methods) {
rlm@37 2965 Object.extend(Form, Form.Methods);
rlm@37 2966 Object.extend(Form.Element, Form.Element.Methods);
rlm@37 2967 Object.extend(Element.Methods.ByTag, {
rlm@37 2968 "FORM": Object.clone(Form.Methods),
rlm@37 2969 "INPUT": Object.clone(Form.Element.Methods),
rlm@37 2970 "SELECT": Object.clone(Form.Element.Methods),
rlm@37 2971 "TEXTAREA": Object.clone(Form.Element.Methods)
rlm@37 2972 });
rlm@37 2973 }
rlm@37 2974
rlm@37 2975 if (arguments.length == 2) {
rlm@37 2976 var tagName = methods;
rlm@37 2977 methods = arguments[1];
rlm@37 2978 }
rlm@37 2979
rlm@37 2980 if (!tagName) Object.extend(Element.Methods, methods || { });
rlm@37 2981 else {
rlm@37 2982 if (Object.isArray(tagName)) tagName.each(extend);
rlm@37 2983 else extend(tagName);
rlm@37 2984 }
rlm@37 2985
rlm@37 2986 function extend(tagName) {
rlm@37 2987 tagName = tagName.toUpperCase();
rlm@37 2988 if (!Element.Methods.ByTag[tagName])
rlm@37 2989 Element.Methods.ByTag[tagName] = { };
rlm@37 2990 Object.extend(Element.Methods.ByTag[tagName], methods);
rlm@37 2991 }
rlm@37 2992
rlm@37 2993 function copy(methods, destination, onlyIfAbsent) {
rlm@37 2994 onlyIfAbsent = onlyIfAbsent || false;
rlm@37 2995 for (var property in methods) {
rlm@37 2996 var value = methods[property];
rlm@37 2997 if (!Object.isFunction(value)) continue;
rlm@37 2998 if (!onlyIfAbsent || !(property in destination))
rlm@37 2999 destination[property] = value.methodize();
rlm@37 3000 }
rlm@37 3001 }
rlm@37 3002
rlm@37 3003 function findDOMClass(tagName) {
rlm@37 3004 var klass;
rlm@37 3005 var trans = {
rlm@37 3006 "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph",
rlm@37 3007 "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList",
rlm@37 3008 "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading",
rlm@37 3009 "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote",
rlm@37 3010 "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION":
rlm@37 3011 "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD":
rlm@37 3012 "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR":
rlm@37 3013 "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET":
rlm@37 3014 "FrameSet", "IFRAME": "IFrame"
rlm@37 3015 };
rlm@37 3016 if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element';
rlm@37 3017 if (window[klass]) return window[klass];
rlm@37 3018 klass = 'HTML' + tagName + 'Element';
rlm@37 3019 if (window[klass]) return window[klass];
rlm@37 3020 klass = 'HTML' + tagName.capitalize() + 'Element';
rlm@37 3021 if (window[klass]) return window[klass];
rlm@37 3022
rlm@37 3023 var element = document.createElement(tagName);
rlm@37 3024 var proto = element['__proto__'] || element.constructor.prototype;
rlm@37 3025 element = null;
rlm@37 3026 return proto;
rlm@37 3027 }
rlm@37 3028
rlm@37 3029 var elementPrototype = window.HTMLElement ? HTMLElement.prototype :
rlm@37 3030 Element.prototype;
rlm@37 3031
rlm@37 3032 if (F.ElementExtensions) {
rlm@37 3033 copy(Element.Methods, elementPrototype);
rlm@37 3034 copy(Element.Methods.Simulated, elementPrototype, true);
rlm@37 3035 }
rlm@37 3036
rlm@37 3037 if (F.SpecificElementExtensions) {
rlm@37 3038 for (var tag in Element.Methods.ByTag) {
rlm@37 3039 var klass = findDOMClass(tag);
rlm@37 3040 if (Object.isUndefined(klass)) continue;
rlm@37 3041 copy(T[tag], klass.prototype);
rlm@37 3042 }
rlm@37 3043 }
rlm@37 3044
rlm@37 3045 Object.extend(Element, Element.Methods);
rlm@37 3046 delete Element.ByTag;
rlm@37 3047
rlm@37 3048 if (Element.extend.refresh) Element.extend.refresh();
rlm@37 3049 Element.cache = { };
rlm@37 3050 };
rlm@37 3051
rlm@37 3052
rlm@37 3053 document.viewport = {
rlm@37 3054
rlm@37 3055 getDimensions: function() {
rlm@37 3056 return { width: this.getWidth(), height: this.getHeight() };
rlm@37 3057 },
rlm@37 3058
rlm@37 3059 getScrollOffsets: function() {
rlm@37 3060 return Element._returnOffset(
rlm@37 3061 window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
rlm@37 3062 window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
rlm@37 3063 }
rlm@37 3064 };
rlm@37 3065
rlm@37 3066 (function(viewport) {
rlm@37 3067 var B = Prototype.Browser, doc = document, element, property = {};
rlm@37 3068
rlm@37 3069 function getRootElement() {
rlm@37 3070 if (B.WebKit && !doc.evaluate)
rlm@37 3071 return document;
rlm@37 3072
rlm@37 3073 if (B.Opera && window.parseFloat(window.opera.version()) < 9.5)
rlm@37 3074 return document.body;
rlm@37 3075
rlm@37 3076 return document.documentElement;
rlm@37 3077 }
rlm@37 3078
rlm@37 3079 function define(D) {
rlm@37 3080 if (!element) element = getRootElement();
rlm@37 3081
rlm@37 3082 property[D] = 'client' + D;
rlm@37 3083
rlm@37 3084 viewport['get' + D] = function() { return element[property[D]] };
rlm@37 3085 return viewport['get' + D]();
rlm@37 3086 }
rlm@37 3087
rlm@37 3088 viewport.getWidth = define.curry('Width');
rlm@37 3089
rlm@37 3090 viewport.getHeight = define.curry('Height');
rlm@37 3091 })(document.viewport);
rlm@37 3092
rlm@37 3093
rlm@37 3094 Element.Storage = {
rlm@37 3095 UID: 1
rlm@37 3096 };
rlm@37 3097
rlm@37 3098 Element.addMethods({
rlm@37 3099 getStorage: function(element) {
rlm@37 3100 if (!(element = $(element))) return;
rlm@37 3101
rlm@37 3102 var uid;
rlm@37 3103 if (element === window) {
rlm@37 3104 uid = 0;
rlm@37 3105 } else {
rlm@37 3106 if (typeof element._prototypeUID === "undefined")
rlm@37 3107 element._prototypeUID = [Element.Storage.UID++];
rlm@37 3108 uid = element._prototypeUID[0];
rlm@37 3109 }
rlm@37 3110
rlm@37 3111 if (!Element.Storage[uid])
rlm@37 3112 Element.Storage[uid] = $H();
rlm@37 3113
rlm@37 3114 return Element.Storage[uid];
rlm@37 3115 },
rlm@37 3116
rlm@37 3117 store: function(element, key, value) {
rlm@37 3118 if (!(element = $(element))) return;
rlm@37 3119
rlm@37 3120 if (arguments.length === 2) {
rlm@37 3121 Element.getStorage(element).update(key);
rlm@37 3122 } else {
rlm@37 3123 Element.getStorage(element).set(key, value);
rlm@37 3124 }
rlm@37 3125
rlm@37 3126 return element;
rlm@37 3127 },
rlm@37 3128
rlm@37 3129 retrieve: function(element, key, defaultValue) {
rlm@37 3130 if (!(element = $(element))) return;
rlm@37 3131 var hash = Element.getStorage(element), value = hash.get(key);
rlm@37 3132
rlm@37 3133 if (Object.isUndefined(value)) {
rlm@37 3134 hash.set(key, defaultValue);
rlm@37 3135 value = defaultValue;
rlm@37 3136 }
rlm@37 3137
rlm@37 3138 return value;
rlm@37 3139 },
rlm@37 3140
rlm@37 3141 clone: function(element, deep) {
rlm@37 3142 if (!(element = $(element))) return;
rlm@37 3143 var clone = element.cloneNode(deep);
rlm@37 3144 clone._prototypeUID = void 0;
rlm@37 3145 if (deep) {
rlm@37 3146 var descendants = Element.select(clone, '*'),
rlm@37 3147 i = descendants.length;
rlm@37 3148 while (i--) {
rlm@37 3149 descendants[i]._prototypeUID = void 0;
rlm@37 3150 }
rlm@37 3151 }
rlm@37 3152 return Element.extend(clone);
rlm@37 3153 }
rlm@37 3154 });
rlm@37 3155 /* Portions of the Selector class are derived from Jack Slocum's DomQuery,
rlm@37 3156 * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style
rlm@37 3157 * license. Please see http://www.yui-ext.com/ for more information. */
rlm@37 3158
rlm@37 3159 var Selector = Class.create({
rlm@37 3160 initialize: function(expression) {
rlm@37 3161 this.expression = expression.strip();
rlm@37 3162
rlm@37 3163 if (this.shouldUseSelectorsAPI()) {
rlm@37 3164 this.mode = 'selectorsAPI';
rlm@37 3165 } else if (this.shouldUseXPath()) {
rlm@37 3166 this.mode = 'xpath';
rlm@37 3167 this.compileXPathMatcher();
rlm@37 3168 } else {
rlm@37 3169 this.mode = "normal";
rlm@37 3170 this.compileMatcher();
rlm@37 3171 }
rlm@37 3172
rlm@37 3173 },
rlm@37 3174
rlm@37 3175 shouldUseXPath: (function() {
rlm@37 3176
rlm@37 3177 var IS_DESCENDANT_SELECTOR_BUGGY = (function(){
rlm@37 3178 var isBuggy = false;
rlm@37 3179 if (document.evaluate && window.XPathResult) {
rlm@37 3180 var el = document.createElement('div');
rlm@37 3181 el.innerHTML = '<ul><li></li></ul><div><ul><li></li></ul></div>';
rlm@37 3182
rlm@37 3183 var xpath = ".//*[local-name()='ul' or local-name()='UL']" +
rlm@37 3184 "//*[local-name()='li' or local-name()='LI']";
rlm@37 3185
rlm@37 3186 var result = document.evaluate(xpath, el, null,
rlm@37 3187 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
rlm@37 3188
rlm@37 3189 isBuggy = (result.snapshotLength !== 2);
rlm@37 3190 el = null;
rlm@37 3191 }
rlm@37 3192 return isBuggy;
rlm@37 3193 })();
rlm@37 3194
rlm@37 3195 return function() {
rlm@37 3196 if (!Prototype.BrowserFeatures.XPath) return false;
rlm@37 3197
rlm@37 3198 var e = this.expression;
rlm@37 3199
rlm@37 3200 if (Prototype.Browser.WebKit &&
rlm@37 3201 (e.include("-of-type") || e.include(":empty")))
rlm@37 3202 return false;
rlm@37 3203
rlm@37 3204 if ((/(\[[\w-]*?:|:checked)/).test(e))
rlm@37 3205 return false;
rlm@37 3206
rlm@37 3207 if (IS_DESCENDANT_SELECTOR_BUGGY) return false;
rlm@37 3208
rlm@37 3209 return true;
rlm@37 3210 }
rlm@37 3211
rlm@37 3212 })(),
rlm@37 3213
rlm@37 3214 shouldUseSelectorsAPI: function() {
rlm@37 3215 if (!Prototype.BrowserFeatures.SelectorsAPI) return false;
rlm@37 3216
rlm@37 3217 if (Selector.CASE_INSENSITIVE_CLASS_NAMES) return false;
rlm@37 3218
rlm@37 3219 if (!Selector._div) Selector._div = new Element('div');
rlm@37 3220
rlm@37 3221 try {
rlm@37 3222 Selector._div.querySelector(this.expression);
rlm@37 3223 } catch(e) {
rlm@37 3224 return false;
rlm@37 3225 }
rlm@37 3226
rlm@37 3227 return true;
rlm@37 3228 },
rlm@37 3229
rlm@37 3230 compileMatcher: function() {
rlm@37 3231 var e = this.expression, ps = Selector.patterns, h = Selector.handlers,
rlm@37 3232 c = Selector.criteria, le, p, m, len = ps.length, name;
rlm@37 3233
rlm@37 3234 if (Selector._cache[e]) {
rlm@37 3235 this.matcher = Selector._cache[e];
rlm@37 3236 return;
rlm@37 3237 }
rlm@37 3238
rlm@37 3239 this.matcher = ["this.matcher = function(root) {",
rlm@37 3240 "var r = root, h = Selector.handlers, c = false, n;"];
rlm@37 3241
rlm@37 3242 while (e && le != e && (/\S/).test(e)) {
rlm@37 3243 le = e;
rlm@37 3244 for (var i = 0; i<len; i++) {
rlm@37 3245 p = ps[i].re;
rlm@37 3246 name = ps[i].name;
rlm@37 3247 if (m = e.match(p)) {
rlm@37 3248 this.matcher.push(Object.isFunction(c[name]) ? c[name](m) :
rlm@37 3249 new Template(c[name]).evaluate(m));
rlm@37 3250 e = e.replace(m[0], '');
rlm@37 3251 break;
rlm@37 3252 }
rlm@37 3253 }
rlm@37 3254 }
rlm@37 3255
rlm@37 3256 this.matcher.push("return h.unique(n);\n}");
rlm@37 3257 eval(this.matcher.join('\n'));
rlm@37 3258 Selector._cache[this.expression] = this.matcher;
rlm@37 3259 },
rlm@37 3260
rlm@37 3261 compileXPathMatcher: function() {
rlm@37 3262 var e = this.expression, ps = Selector.patterns,
rlm@37 3263 x = Selector.xpath, le, m, len = ps.length, name;
rlm@37 3264
rlm@37 3265 if (Selector._cache[e]) {
rlm@37 3266 this.xpath = Selector._cache[e]; return;
rlm@37 3267 }
rlm@37 3268
rlm@37 3269 this.matcher = ['.//*'];
rlm@37 3270 while (e && le != e && (/\S/).test(e)) {
rlm@37 3271 le = e;
rlm@37 3272 for (var i = 0; i<len; i++) {
rlm@37 3273 name = ps[i].name;
rlm@37 3274 if (m = e.match(ps[i].re)) {
rlm@37 3275 this.matcher.push(Object.isFunction(x[name]) ? x[name](m) :
rlm@37 3276 new Template(x[name]).evaluate(m));
rlm@37 3277 e = e.replace(m[0], '');
rlm@37 3278 break;
rlm@37 3279 }
rlm@37 3280 }
rlm@37 3281 }
rlm@37 3282
rlm@37 3283 this.xpath = this.matcher.join('');
rlm@37 3284 Selector._cache[this.expression] = this.xpath;
rlm@37 3285 },
rlm@37 3286
rlm@37 3287 findElements: function(root) {
rlm@37 3288 root = root || document;
rlm@37 3289 var e = this.expression, results;
rlm@37 3290
rlm@37 3291 switch (this.mode) {
rlm@37 3292 case 'selectorsAPI':
rlm@37 3293 if (root !== document) {
rlm@37 3294 var oldId = root.id, id = $(root).identify();
rlm@37 3295 id = id.replace(/([\.:])/g, "\\$1");
rlm@37 3296 e = "#" + id + " " + e;
rlm@37 3297 }
rlm@37 3298
rlm@37 3299 results = $A(root.querySelectorAll(e)).map(Element.extend);
rlm@37 3300 root.id = oldId;
rlm@37 3301
rlm@37 3302 return results;
rlm@37 3303 case 'xpath':
rlm@37 3304 return document._getElementsByXPath(this.xpath, root);
rlm@37 3305 default:
rlm@37 3306 return this.matcher(root);
rlm@37 3307 }
rlm@37 3308 },
rlm@37 3309
rlm@37 3310 match: function(element) {
rlm@37 3311 this.tokens = [];
rlm@37 3312
rlm@37 3313 var e = this.expression, ps = Selector.patterns, as = Selector.assertions;
rlm@37 3314 var le, p, m, len = ps.length, name;
rlm@37 3315
rlm@37 3316 while (e && le !== e && (/\S/).test(e)) {
rlm@37 3317 le = e;
rlm@37 3318 for (var i = 0; i<len; i++) {
rlm@37 3319 p = ps[i].re;
rlm@37 3320 name = ps[i].name;
rlm@37 3321 if (m = e.match(p)) {
rlm@37 3322 if (as[name]) {
rlm@37 3323 this.tokens.push([name, Object.clone(m)]);
rlm@37 3324 e = e.replace(m[0], '');
rlm@37 3325 } else {
rlm@37 3326 return this.findElements(document).include(element);
rlm@37 3327 }
rlm@37 3328 }
rlm@37 3329 }
rlm@37 3330 }
rlm@37 3331
rlm@37 3332 var match = true, name, matches;
rlm@37 3333 for (var i = 0, token; token = this.tokens[i]; i++) {
rlm@37 3334 name = token[0], matches = token[1];
rlm@37 3335 if (!Selector.assertions[name](element, matches)) {
rlm@37 3336 match = false; break;
rlm@37 3337 }
rlm@37 3338 }
rlm@37 3339
rlm@37 3340 return match;
rlm@37 3341 },
rlm@37 3342
rlm@37 3343 toString: function() {
rlm@37 3344 return this.expression;
rlm@37 3345 },
rlm@37 3346
rlm@37 3347 inspect: function() {
rlm@37 3348 return "#<Selector:" + this.expression.inspect() + ">";
rlm@37 3349 }
rlm@37 3350 });
rlm@37 3351
rlm@37 3352 if (Prototype.BrowserFeatures.SelectorsAPI &&
rlm@37 3353 document.compatMode === 'BackCompat') {
rlm@37 3354 Selector.CASE_INSENSITIVE_CLASS_NAMES = (function(){
rlm@37 3355 var div = document.createElement('div'),
rlm@37 3356 span = document.createElement('span');
rlm@37 3357
rlm@37 3358 div.id = "prototype_test_id";
rlm@37 3359 span.className = 'Test';
rlm@37 3360 div.appendChild(span);
rlm@37 3361 var isIgnored = (div.querySelector('#prototype_test_id .test') !== null);
rlm@37 3362 div = span = null;
rlm@37 3363 return isIgnored;
rlm@37 3364 })();
rlm@37 3365 }
rlm@37 3366
rlm@37 3367 Object.extend(Selector, {
rlm@37 3368 _cache: { },
rlm@37 3369
rlm@37 3370 xpath: {
rlm@37 3371 descendant: "//*",
rlm@37 3372 child: "/*",
rlm@37 3373 adjacent: "/following-sibling::*[1]",
rlm@37 3374 laterSibling: '/following-sibling::*',
rlm@37 3375 tagName: function(m) {
rlm@37 3376 if (m[1] == '*') return '';
rlm@37 3377 return "[local-name()='" + m[1].toLowerCase() +
rlm@37 3378 "' or local-name()='" + m[1].toUpperCase() + "']";
rlm@37 3379 },
rlm@37 3380 className: "[contains(concat(' ', @class, ' '), ' #{1} ')]",
rlm@37 3381 id: "[@id='#{1}']",
rlm@37 3382 attrPresence: function(m) {
rlm@37 3383 m[1] = m[1].toLowerCase();
rlm@37 3384 return new Template("[@#{1}]").evaluate(m);
rlm@37 3385 },
rlm@37 3386 attr: function(m) {
rlm@37 3387 m[1] = m[1].toLowerCase();
rlm@37 3388 m[3] = m[5] || m[6];
rlm@37 3389 return new Template(Selector.xpath.operators[m[2]]).evaluate(m);
rlm@37 3390 },
rlm@37 3391 pseudo: function(m) {
rlm@37 3392 var h = Selector.xpath.pseudos[m[1]];
rlm@37 3393 if (!h) return '';
rlm@37 3394 if (Object.isFunction(h)) return h(m);
rlm@37 3395 return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m);
rlm@37 3396 },
rlm@37 3397 operators: {
rlm@37 3398 '=': "[@#{1}='#{3}']",
rlm@37 3399 '!=': "[@#{1}!='#{3}']",
rlm@37 3400 '^=': "[starts-with(@#{1}, '#{3}')]",
rlm@37 3401 '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']",
rlm@37 3402 '*=': "[contains(@#{1}, '#{3}')]",
rlm@37 3403 '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]",
rlm@37 3404 '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]"
rlm@37 3405 },
rlm@37 3406 pseudos: {
rlm@37 3407 'first-child': '[not(preceding-sibling::*)]',
rlm@37 3408 'last-child': '[not(following-sibling::*)]',
rlm@37 3409 'only-child': '[not(preceding-sibling::* or following-sibling::*)]',
rlm@37 3410 'empty': "[count(*) = 0 and (count(text()) = 0)]",
rlm@37 3411 'checked': "[@checked]",
rlm@37 3412 'disabled': "[(@disabled) and (@type!='hidden')]",
rlm@37 3413 'enabled': "[not(@disabled) and (@type!='hidden')]",
rlm@37 3414 'not': function(m) {
rlm@37 3415 var e = m[6], p = Selector.patterns,
rlm@37 3416 x = Selector.xpath, le, v, len = p.length, name;
rlm@37 3417
rlm@37 3418 var exclusion = [];
rlm@37 3419 while (e && le != e && (/\S/).test(e)) {
rlm@37 3420 le = e;
rlm@37 3421 for (var i = 0; i<len; i++) {
rlm@37 3422 name = p[i].name
rlm@37 3423 if (m = e.match(p[i].re)) {
rlm@37 3424 v = Object.isFunction(x[name]) ? x[name](m) : new Template(x[name]).evaluate(m);
rlm@37 3425 exclusion.push("(" + v.substring(1, v.length - 1) + ")");
rlm@37 3426 e = e.replace(m[0], '');
rlm@37 3427 break;
rlm@37 3428 }
rlm@37 3429 }
rlm@37 3430 }
rlm@37 3431 return "[not(" + exclusion.join(" and ") + ")]";
rlm@37 3432 },
rlm@37 3433 'nth-child': function(m) {
rlm@37 3434 return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m);
rlm@37 3435 },
rlm@37 3436 'nth-last-child': function(m) {
rlm@37 3437 return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m);
rlm@37 3438 },
rlm@37 3439 'nth-of-type': function(m) {
rlm@37 3440 return Selector.xpath.pseudos.nth("position() ", m);
rlm@37 3441 },
rlm@37 3442 'nth-last-of-type': function(m) {
rlm@37 3443 return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m);
rlm@37 3444 },
rlm@37 3445 'first-of-type': function(m) {
rlm@37 3446 m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m);
rlm@37 3447 },
rlm@37 3448 'last-of-type': function(m) {
rlm@37 3449 m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m);
rlm@37 3450 },
rlm@37 3451 'only-of-type': function(m) {
rlm@37 3452 var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m);
rlm@37 3453 },
rlm@37 3454 nth: function(fragment, m) {
rlm@37 3455 var mm, formula = m[6], predicate;
rlm@37 3456 if (formula == 'even') formula = '2n+0';
rlm@37 3457 if (formula == 'odd') formula = '2n+1';
rlm@37 3458 if (mm = formula.match(/^(\d+)$/)) // digit only
rlm@37 3459 return '[' + fragment + "= " + mm[1] + ']';
rlm@37 3460 if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
rlm@37 3461 if (mm[1] == "-") mm[1] = -1;
rlm@37 3462 var a = mm[1] ? Number(mm[1]) : 1;
rlm@37 3463 var b = mm[2] ? Number(mm[2]) : 0;
rlm@37 3464 predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " +
rlm@37 3465 "((#{fragment} - #{b}) div #{a} >= 0)]";
rlm@37 3466 return new Template(predicate).evaluate({
rlm@37 3467 fragment: fragment, a: a, b: b });
rlm@37 3468 }
rlm@37 3469 }
rlm@37 3470 }
rlm@37 3471 },
rlm@37 3472
rlm@37 3473 criteria: {
rlm@37 3474 tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;',
rlm@37 3475 className: 'n = h.className(n, r, "#{1}", c); c = false;',
rlm@37 3476 id: 'n = h.id(n, r, "#{1}", c); c = false;',
rlm@37 3477 attrPresence: 'n = h.attrPresence(n, r, "#{1}", c); c = false;',
rlm@37 3478 attr: function(m) {
rlm@37 3479 m[3] = (m[5] || m[6]);
rlm@37 3480 return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}", c); c = false;').evaluate(m);
rlm@37 3481 },
rlm@37 3482 pseudo: function(m) {
rlm@37 3483 if (m[6]) m[6] = m[6].replace(/"/g, '\\"');
rlm@37 3484 return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m);
rlm@37 3485 },
rlm@37 3486 descendant: 'c = "descendant";',
rlm@37 3487 child: 'c = "child";',
rlm@37 3488 adjacent: 'c = "adjacent";',
rlm@37 3489 laterSibling: 'c = "laterSibling";'
rlm@37 3490 },
rlm@37 3491
rlm@37 3492 patterns: [
rlm@37 3493 { name: 'laterSibling', re: /^\s*~\s*/ },
rlm@37 3494 { name: 'child', re: /^\s*>\s*/ },
rlm@37 3495 { name: 'adjacent', re: /^\s*\+\s*/ },
rlm@37 3496 { name: 'descendant', re: /^\s/ },
rlm@37 3497
rlm@37 3498 { name: 'tagName', re: /^\s*(\*|[\w\-]+)(\b|$)?/ },
rlm@37 3499 { name: 'id', re: /^#([\w\-\*]+)(\b|$)/ },
rlm@37 3500 { name: 'className', re: /^\.([\w\-\*]+)(\b|$)/ },
rlm@37 3501 { name: 'pseudo', re: /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/ },
rlm@37 3502 { name: 'attrPresence', re: /^\[((?:[\w-]+:)?[\w-]+)\]/ },
rlm@37 3503 { name: 'attr', re: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/ }
rlm@37 3504 ],
rlm@37 3505
rlm@37 3506 assertions: {
rlm@37 3507 tagName: function(element, matches) {
rlm@37 3508 return matches[1].toUpperCase() == element.tagName.toUpperCase();
rlm@37 3509 },
rlm@37 3510
rlm@37 3511 className: function(element, matches) {
rlm@37 3512 return Element.hasClassName(element, matches[1]);
rlm@37 3513 },
rlm@37 3514
rlm@37 3515 id: function(element, matches) {
rlm@37 3516 return element.id === matches[1];
rlm@37 3517 },
rlm@37 3518
rlm@37 3519 attrPresence: function(element, matches) {
rlm@37 3520 return Element.hasAttribute(element, matches[1]);
rlm@37 3521 },
rlm@37 3522
rlm@37 3523 attr: function(element, matches) {
rlm@37 3524 var nodeValue = Element.readAttribute(element, matches[1]);
rlm@37 3525 return nodeValue && Selector.operators[matches[2]](nodeValue, matches[5] || matches[6]);
rlm@37 3526 }
rlm@37 3527 },
rlm@37 3528
rlm@37 3529 handlers: {
rlm@37 3530 concat: function(a, b) {
rlm@37 3531 for (var i = 0, node; node = b[i]; i++)
rlm@37 3532 a.push(node);
rlm@37 3533 return a;
rlm@37 3534 },
rlm@37 3535
rlm@37 3536 mark: function(nodes) {
rlm@37 3537 var _true = Prototype.emptyFunction;
rlm@37 3538 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3539 node._countedByPrototype = _true;
rlm@37 3540 return nodes;
rlm@37 3541 },
rlm@37 3542
rlm@37 3543 unmark: (function(){
rlm@37 3544
rlm@37 3545 var PROPERTIES_ATTRIBUTES_MAP = (function(){
rlm@37 3546 var el = document.createElement('div'),
rlm@37 3547 isBuggy = false,
rlm@37 3548 propName = '_countedByPrototype',
rlm@37 3549 value = 'x'
rlm@37 3550 el[propName] = value;
rlm@37 3551 isBuggy = (el.getAttribute(propName) === value);
rlm@37 3552 el = null;
rlm@37 3553 return isBuggy;
rlm@37 3554 })();
rlm@37 3555
rlm@37 3556 return PROPERTIES_ATTRIBUTES_MAP ?
rlm@37 3557 function(nodes) {
rlm@37 3558 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3559 node.removeAttribute('_countedByPrototype');
rlm@37 3560 return nodes;
rlm@37 3561 } :
rlm@37 3562 function(nodes) {
rlm@37 3563 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3564 node._countedByPrototype = void 0;
rlm@37 3565 return nodes;
rlm@37 3566 }
rlm@37 3567 })(),
rlm@37 3568
rlm@37 3569 index: function(parentNode, reverse, ofType) {
rlm@37 3570 parentNode._countedByPrototype = Prototype.emptyFunction;
rlm@37 3571 if (reverse) {
rlm@37 3572 for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) {
rlm@37 3573 var node = nodes[i];
rlm@37 3574 if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++;
rlm@37 3575 }
rlm@37 3576 } else {
rlm@37 3577 for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++)
rlm@37 3578 if (node.nodeType == 1 && (!ofType || node._countedByPrototype)) node.nodeIndex = j++;
rlm@37 3579 }
rlm@37 3580 },
rlm@37 3581
rlm@37 3582 unique: function(nodes) {
rlm@37 3583 if (nodes.length == 0) return nodes;
rlm@37 3584 var results = [], n;
rlm@37 3585 for (var i = 0, l = nodes.length; i < l; i++)
rlm@37 3586 if (typeof (n = nodes[i])._countedByPrototype == 'undefined') {
rlm@37 3587 n._countedByPrototype = Prototype.emptyFunction;
rlm@37 3588 results.push(Element.extend(n));
rlm@37 3589 }
rlm@37 3590 return Selector.handlers.unmark(results);
rlm@37 3591 },
rlm@37 3592
rlm@37 3593 descendant: function(nodes) {
rlm@37 3594 var h = Selector.handlers;
rlm@37 3595 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3596 h.concat(results, node.getElementsByTagName('*'));
rlm@37 3597 return results;
rlm@37 3598 },
rlm@37 3599
rlm@37 3600 child: function(nodes) {
rlm@37 3601 var h = Selector.handlers;
rlm@37 3602 for (var i = 0, results = [], node; node = nodes[i]; i++) {
rlm@37 3603 for (var j = 0, child; child = node.childNodes[j]; j++)
rlm@37 3604 if (child.nodeType == 1 && child.tagName != '!') results.push(child);
rlm@37 3605 }
rlm@37 3606 return results;
rlm@37 3607 },
rlm@37 3608
rlm@37 3609 adjacent: function(nodes) {
rlm@37 3610 for (var i = 0, results = [], node; node = nodes[i]; i++) {
rlm@37 3611 var next = this.nextElementSibling(node);
rlm@37 3612 if (next) results.push(next);
rlm@37 3613 }
rlm@37 3614 return results;
rlm@37 3615 },
rlm@37 3616
rlm@37 3617 laterSibling: function(nodes) {
rlm@37 3618 var h = Selector.handlers;
rlm@37 3619 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3620 h.concat(results, Element.nextSiblings(node));
rlm@37 3621 return results;
rlm@37 3622 },
rlm@37 3623
rlm@37 3624 nextElementSibling: function(node) {
rlm@37 3625 while (node = node.nextSibling)
rlm@37 3626 if (node.nodeType == 1) return node;
rlm@37 3627 return null;
rlm@37 3628 },
rlm@37 3629
rlm@37 3630 previousElementSibling: function(node) {
rlm@37 3631 while (node = node.previousSibling)
rlm@37 3632 if (node.nodeType == 1) return node;
rlm@37 3633 return null;
rlm@37 3634 },
rlm@37 3635
rlm@37 3636 tagName: function(nodes, root, tagName, combinator) {
rlm@37 3637 var uTagName = tagName.toUpperCase();
rlm@37 3638 var results = [], h = Selector.handlers;
rlm@37 3639 if (nodes) {
rlm@37 3640 if (combinator) {
rlm@37 3641 if (combinator == "descendant") {
rlm@37 3642 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3643 h.concat(results, node.getElementsByTagName(tagName));
rlm@37 3644 return results;
rlm@37 3645 } else nodes = this[combinator](nodes);
rlm@37 3646 if (tagName == "*") return nodes;
rlm@37 3647 }
rlm@37 3648 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3649 if (node.tagName.toUpperCase() === uTagName) results.push(node);
rlm@37 3650 return results;
rlm@37 3651 } else return root.getElementsByTagName(tagName);
rlm@37 3652 },
rlm@37 3653
rlm@37 3654 id: function(nodes, root, id, combinator) {
rlm@37 3655 var targetNode = $(id), h = Selector.handlers;
rlm@37 3656
rlm@37 3657 if (root == document) {
rlm@37 3658 if (!targetNode) return [];
rlm@37 3659 if (!nodes) return [targetNode];
rlm@37 3660 } else {
rlm@37 3661 if (!root.sourceIndex || root.sourceIndex < 1) {
rlm@37 3662 var nodes = root.getElementsByTagName('*');
rlm@37 3663 for (var j = 0, node; node = nodes[j]; j++) {
rlm@37 3664 if (node.id === id) return [node];
rlm@37 3665 }
rlm@37 3666 }
rlm@37 3667 }
rlm@37 3668
rlm@37 3669 if (nodes) {
rlm@37 3670 if (combinator) {
rlm@37 3671 if (combinator == 'child') {
rlm@37 3672 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3673 if (targetNode.parentNode == node) return [targetNode];
rlm@37 3674 } else if (combinator == 'descendant') {
rlm@37 3675 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3676 if (Element.descendantOf(targetNode, node)) return [targetNode];
rlm@37 3677 } else if (combinator == 'adjacent') {
rlm@37 3678 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3679 if (Selector.handlers.previousElementSibling(targetNode) == node)
rlm@37 3680 return [targetNode];
rlm@37 3681 } else nodes = h[combinator](nodes);
rlm@37 3682 }
rlm@37 3683 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3684 if (node == targetNode) return [targetNode];
rlm@37 3685 return [];
rlm@37 3686 }
rlm@37 3687 return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : [];
rlm@37 3688 },
rlm@37 3689
rlm@37 3690 className: function(nodes, root, className, combinator) {
rlm@37 3691 if (nodes && combinator) nodes = this[combinator](nodes);
rlm@37 3692 return Selector.handlers.byClassName(nodes, root, className);
rlm@37 3693 },
rlm@37 3694
rlm@37 3695 byClassName: function(nodes, root, className) {
rlm@37 3696 if (!nodes) nodes = Selector.handlers.descendant([root]);
rlm@37 3697 var needle = ' ' + className + ' ';
rlm@37 3698 for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) {
rlm@37 3699 nodeClassName = node.className;
rlm@37 3700 if (nodeClassName.length == 0) continue;
rlm@37 3701 if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle))
rlm@37 3702 results.push(node);
rlm@37 3703 }
rlm@37 3704 return results;
rlm@37 3705 },
rlm@37 3706
rlm@37 3707 attrPresence: function(nodes, root, attr, combinator) {
rlm@37 3708 if (!nodes) nodes = root.getElementsByTagName("*");
rlm@37 3709 if (nodes && combinator) nodes = this[combinator](nodes);
rlm@37 3710 var results = [];
rlm@37 3711 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3712 if (Element.hasAttribute(node, attr)) results.push(node);
rlm@37 3713 return results;
rlm@37 3714 },
rlm@37 3715
rlm@37 3716 attr: function(nodes, root, attr, value, operator, combinator) {
rlm@37 3717 if (!nodes) nodes = root.getElementsByTagName("*");
rlm@37 3718 if (nodes && combinator) nodes = this[combinator](nodes);
rlm@37 3719 var handler = Selector.operators[operator], results = [];
rlm@37 3720 for (var i = 0, node; node = nodes[i]; i++) {
rlm@37 3721 var nodeValue = Element.readAttribute(node, attr);
rlm@37 3722 if (nodeValue === null) continue;
rlm@37 3723 if (handler(nodeValue, value)) results.push(node);
rlm@37 3724 }
rlm@37 3725 return results;
rlm@37 3726 },
rlm@37 3727
rlm@37 3728 pseudo: function(nodes, name, value, root, combinator) {
rlm@37 3729 if (nodes && combinator) nodes = this[combinator](nodes);
rlm@37 3730 if (!nodes) nodes = root.getElementsByTagName("*");
rlm@37 3731 return Selector.pseudos[name](nodes, value, root);
rlm@37 3732 }
rlm@37 3733 },
rlm@37 3734
rlm@37 3735 pseudos: {
rlm@37 3736 'first-child': function(nodes, value, root) {
rlm@37 3737 for (var i = 0, results = [], node; node = nodes[i]; i++) {
rlm@37 3738 if (Selector.handlers.previousElementSibling(node)) continue;
rlm@37 3739 results.push(node);
rlm@37 3740 }
rlm@37 3741 return results;
rlm@37 3742 },
rlm@37 3743 'last-child': function(nodes, value, root) {
rlm@37 3744 for (var i = 0, results = [], node; node = nodes[i]; i++) {
rlm@37 3745 if (Selector.handlers.nextElementSibling(node)) continue;
rlm@37 3746 results.push(node);
rlm@37 3747 }
rlm@37 3748 return results;
rlm@37 3749 },
rlm@37 3750 'only-child': function(nodes, value, root) {
rlm@37 3751 var h = Selector.handlers;
rlm@37 3752 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3753 if (!h.previousElementSibling(node) && !h.nextElementSibling(node))
rlm@37 3754 results.push(node);
rlm@37 3755 return results;
rlm@37 3756 },
rlm@37 3757 'nth-child': function(nodes, formula, root) {
rlm@37 3758 return Selector.pseudos.nth(nodes, formula, root);
rlm@37 3759 },
rlm@37 3760 'nth-last-child': function(nodes, formula, root) {
rlm@37 3761 return Selector.pseudos.nth(nodes, formula, root, true);
rlm@37 3762 },
rlm@37 3763 'nth-of-type': function(nodes, formula, root) {
rlm@37 3764 return Selector.pseudos.nth(nodes, formula, root, false, true);
rlm@37 3765 },
rlm@37 3766 'nth-last-of-type': function(nodes, formula, root) {
rlm@37 3767 return Selector.pseudos.nth(nodes, formula, root, true, true);
rlm@37 3768 },
rlm@37 3769 'first-of-type': function(nodes, formula, root) {
rlm@37 3770 return Selector.pseudos.nth(nodes, "1", root, false, true);
rlm@37 3771 },
rlm@37 3772 'last-of-type': function(nodes, formula, root) {
rlm@37 3773 return Selector.pseudos.nth(nodes, "1", root, true, true);
rlm@37 3774 },
rlm@37 3775 'only-of-type': function(nodes, formula, root) {
rlm@37 3776 var p = Selector.pseudos;
rlm@37 3777 return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root);
rlm@37 3778 },
rlm@37 3779
rlm@37 3780 getIndices: function(a, b, total) {
rlm@37 3781 if (a == 0) return b > 0 ? [b] : [];
rlm@37 3782 return $R(1, total).inject([], function(memo, i) {
rlm@37 3783 if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i);
rlm@37 3784 return memo;
rlm@37 3785 });
rlm@37 3786 },
rlm@37 3787
rlm@37 3788 nth: function(nodes, formula, root, reverse, ofType) {
rlm@37 3789 if (nodes.length == 0) return [];
rlm@37 3790 if (formula == 'even') formula = '2n+0';
rlm@37 3791 if (formula == 'odd') formula = '2n+1';
rlm@37 3792 var h = Selector.handlers, results = [], indexed = [], m;
rlm@37 3793 h.mark(nodes);
rlm@37 3794 for (var i = 0, node; node = nodes[i]; i++) {
rlm@37 3795 if (!node.parentNode._countedByPrototype) {
rlm@37 3796 h.index(node.parentNode, reverse, ofType);
rlm@37 3797 indexed.push(node.parentNode);
rlm@37 3798 }
rlm@37 3799 }
rlm@37 3800 if (formula.match(/^\d+$/)) { // just a number
rlm@37 3801 formula = Number(formula);
rlm@37 3802 for (var i = 0, node; node = nodes[i]; i++)
rlm@37 3803 if (node.nodeIndex == formula) results.push(node);
rlm@37 3804 } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
rlm@37 3805 if (m[1] == "-") m[1] = -1;
rlm@37 3806 var a = m[1] ? Number(m[1]) : 1;
rlm@37 3807 var b = m[2] ? Number(m[2]) : 0;
rlm@37 3808 var indices = Selector.pseudos.getIndices(a, b, nodes.length);
rlm@37 3809 for (var i = 0, node, l = indices.length; node = nodes[i]; i++) {
rlm@37 3810 for (var j = 0; j < l; j++)
rlm@37 3811 if (node.nodeIndex == indices[j]) results.push(node);
rlm@37 3812 }
rlm@37 3813 }
rlm@37 3814 h.unmark(nodes);
rlm@37 3815 h.unmark(indexed);
rlm@37 3816 return results;
rlm@37 3817 },
rlm@37 3818
rlm@37 3819 'empty': function(nodes, value, root) {
rlm@37 3820 for (var i = 0, results = [], node; node = nodes[i]; i++) {
rlm@37 3821 if (node.tagName == '!' || node.firstChild) continue;
rlm@37 3822 results.push(node);
rlm@37 3823 }
rlm@37 3824 return results;
rlm@37 3825 },
rlm@37 3826
rlm@37 3827 'not': function(nodes, selector, root) {
rlm@37 3828 var h = Selector.handlers, selectorType, m;
rlm@37 3829 var exclusions = new Selector(selector).findElements(root);
rlm@37 3830 h.mark(exclusions);
rlm@37 3831 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3832 if (!node._countedByPrototype) results.push(node);
rlm@37 3833 h.unmark(exclusions);
rlm@37 3834 return results;
rlm@37 3835 },
rlm@37 3836
rlm@37 3837 'enabled': function(nodes, value, root) {
rlm@37 3838 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3839 if (!node.disabled && (!node.type || node.type !== 'hidden'))
rlm@37 3840 results.push(node);
rlm@37 3841 return results;
rlm@37 3842 },
rlm@37 3843
rlm@37 3844 'disabled': function(nodes, value, root) {
rlm@37 3845 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3846 if (node.disabled) results.push(node);
rlm@37 3847 return results;
rlm@37 3848 },
rlm@37 3849
rlm@37 3850 'checked': function(nodes, value, root) {
rlm@37 3851 for (var i = 0, results = [], node; node = nodes[i]; i++)
rlm@37 3852 if (node.checked) results.push(node);
rlm@37 3853 return results;
rlm@37 3854 }
rlm@37 3855 },
rlm@37 3856
rlm@37 3857 operators: {
rlm@37 3858 '=': function(nv, v) { return nv == v; },
rlm@37 3859 '!=': function(nv, v) { return nv != v; },
rlm@37 3860 '^=': function(nv, v) { return nv == v || nv && nv.startsWith(v); },
rlm@37 3861 '$=': function(nv, v) { return nv == v || nv && nv.endsWith(v); },
rlm@37 3862 '*=': function(nv, v) { return nv == v || nv && nv.include(v); },
rlm@37 3863 '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); },
rlm@37 3864 '|=': function(nv, v) { return ('-' + (nv || "").toUpperCase() +
rlm@37 3865 '-').include('-' + (v || "").toUpperCase() + '-'); }
rlm@37 3866 },
rlm@37 3867
rlm@37 3868 split: function(expression) {
rlm@37 3869 var expressions = [];
rlm@37 3870 expression.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) {
rlm@37 3871 expressions.push(m[1].strip());
rlm@37 3872 });
rlm@37 3873 return expressions;
rlm@37 3874 },
rlm@37 3875
rlm@37 3876 matchElements: function(elements, expression) {
rlm@37 3877 var matches = $$(expression), h = Selector.handlers;
rlm@37 3878 h.mark(matches);
rlm@37 3879 for (var i = 0, results = [], element; element = elements[i]; i++)
rlm@37 3880 if (element._countedByPrototype) results.push(element);
rlm@37 3881 h.unmark(matches);
rlm@37 3882 return results;
rlm@37 3883 },
rlm@37 3884
rlm@37 3885 findElement: function(elements, expression, index) {
rlm@37 3886 if (Object.isNumber(expression)) {
rlm@37 3887 index = expression; expression = false;
rlm@37 3888 }
rlm@37 3889 return Selector.matchElements(elements, expression || '*')[index || 0];
rlm@37 3890 },
rlm@37 3891
rlm@37 3892 findChildElements: function(element, expressions) {
rlm@37 3893 expressions = Selector.split(expressions.join(','));
rlm@37 3894 var results = [], h = Selector.handlers;
rlm@37 3895 for (var i = 0, l = expressions.length, selector; i < l; i++) {
rlm@37 3896 selector = new Selector(expressions[i].strip());
rlm@37 3897 h.concat(results, selector.findElements(element));
rlm@37 3898 }
rlm@37 3899 return (l > 1) ? h.unique(results) : results;
rlm@37 3900 }
rlm@37 3901 });
rlm@37 3902
rlm@37 3903 if (Prototype.Browser.IE) {
rlm@37 3904 Object.extend(Selector.handlers, {
rlm@37 3905 concat: function(a, b) {
rlm@37 3906 for (var i = 0, node; node = b[i]; i++)
rlm@37 3907 if (node.tagName !== "!") a.push(node);
rlm@37 3908 return a;
rlm@37 3909 }
rlm@37 3910 });
rlm@37 3911 }
rlm@37 3912
rlm@37 3913 function $$() {
rlm@37 3914 return Selector.findChildElements(document, $A(arguments));
rlm@37 3915 }
rlm@37 3916
rlm@37 3917 var Form = {
rlm@37 3918 reset: function(form) {
rlm@37 3919 form = $(form);
rlm@37 3920 form.reset();
rlm@37 3921 return form;
rlm@37 3922 },
rlm@37 3923
rlm@37 3924 serializeElements: function(elements, options) {
rlm@37 3925 if (typeof options != 'object') options = { hash: !!options };
rlm@37 3926 else if (Object.isUndefined(options.hash)) options.hash = true;
rlm@37 3927 var key, value, submitted = false, submit = options.submit;
rlm@37 3928
rlm@37 3929 var data = elements.inject({ }, function(result, element) {
rlm@37 3930 if (!element.disabled && element.name) {
rlm@37 3931 key = element.name; value = $(element).getValue();
rlm@37 3932 if (value != null && element.type != 'file' && (element.type != 'submit' || (!submitted &&
rlm@37 3933 submit !== false && (!submit || key == submit) && (submitted = true)))) {
rlm@37 3934 if (key in result) {
rlm@37 3935 if (!Object.isArray(result[key])) result[key] = [result[key]];
rlm@37 3936 result[key].push(value);
rlm@37 3937 }
rlm@37 3938 else result[key] = value;
rlm@37 3939 }
rlm@37 3940 }
rlm@37 3941 return result;
rlm@37 3942 });
rlm@37 3943
rlm@37 3944 return options.hash ? data : Object.toQueryString(data);
rlm@37 3945 }
rlm@37 3946 };
rlm@37 3947
rlm@37 3948 Form.Methods = {
rlm@37 3949 serialize: function(form, options) {
rlm@37 3950 return Form.serializeElements(Form.getElements(form), options);
rlm@37 3951 },
rlm@37 3952
rlm@37 3953 getElements: function(form) {
rlm@37 3954 var elements = $(form).getElementsByTagName('*'),
rlm@37 3955 element,
rlm@37 3956 arr = [ ],
rlm@37 3957 serializers = Form.Element.Serializers;
rlm@37 3958 for (var i = 0; element = elements[i]; i++) {
rlm@37 3959 arr.push(element);
rlm@37 3960 }
rlm@37 3961 return arr.inject([], function(elements, child) {
rlm@37 3962 if (serializers[child.tagName.toLowerCase()])
rlm@37 3963 elements.push(Element.extend(child));
rlm@37 3964 return elements;
rlm@37 3965 })
rlm@37 3966 },
rlm@37 3967
rlm@37 3968 getInputs: function(form, typeName, name) {
rlm@37 3969 form = $(form);
rlm@37 3970 var inputs = form.getElementsByTagName('input');
rlm@37 3971
rlm@37 3972 if (!typeName && !name) return $A(inputs).map(Element.extend);
rlm@37 3973
rlm@37 3974 for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) {
rlm@37 3975 var input = inputs[i];
rlm@37 3976 if ((typeName && input.type != typeName) || (name && input.name != name))
rlm@37 3977 continue;
rlm@37 3978 matchingInputs.push(Element.extend(input));
rlm@37 3979 }
rlm@37 3980
rlm@37 3981 return matchingInputs;
rlm@37 3982 },
rlm@37 3983
rlm@37 3984 disable: function(form) {
rlm@37 3985 form = $(form);
rlm@37 3986 Form.getElements(form).invoke('disable');
rlm@37 3987 return form;
rlm@37 3988 },
rlm@37 3989
rlm@37 3990 enable: function(form) {
rlm@37 3991 form = $(form);
rlm@37 3992 Form.getElements(form).invoke('enable');
rlm@37 3993 return form;
rlm@37 3994 },
rlm@37 3995
rlm@37 3996 findFirstElement: function(form) {
rlm@37 3997 var elements = $(form).getElements().findAll(function(element) {
rlm@37 3998 return 'hidden' != element.type && !element.disabled;
rlm@37 3999 });
rlm@37 4000 var firstByIndex = elements.findAll(function(element) {
rlm@37 4001 return element.hasAttribute('tabIndex') && element.tabIndex >= 0;
rlm@37 4002 }).sortBy(function(element) { return element.tabIndex }).first();
rlm@37 4003
rlm@37 4004 return firstByIndex ? firstByIndex : elements.find(function(element) {
rlm@37 4005 return /^(?:input|select|textarea)$/i.test(element.tagName);
rlm@37 4006 });
rlm@37 4007 },
rlm@37 4008
rlm@37 4009 focusFirstElement: function(form) {
rlm@37 4010 form = $(form);
rlm@37 4011 form.findFirstElement().activate();
rlm@37 4012 return form;
rlm@37 4013 },
rlm@37 4014
rlm@37 4015 request: function(form, options) {
rlm@37 4016 form = $(form), options = Object.clone(options || { });
rlm@37 4017
rlm@37 4018 var params = options.parameters, action = form.readAttribute('action') || '';
rlm@37 4019 if (action.blank()) action = window.location.href;
rlm@37 4020 options.parameters = form.serialize(true);
rlm@37 4021
rlm@37 4022 if (params) {
rlm@37 4023 if (Object.isString(params)) params = params.toQueryParams();
rlm@37 4024 Object.extend(options.parameters, params);
rlm@37 4025 }
rlm@37 4026
rlm@37 4027 if (form.hasAttribute('method') && !options.method)
rlm@37 4028 options.method = form.method;
rlm@37 4029
rlm@37 4030 return new Ajax.Request(action, options);
rlm@37 4031 }
rlm@37 4032 };
rlm@37 4033
rlm@37 4034 /*--------------------------------------------------------------------------*/
rlm@37 4035
rlm@37 4036
rlm@37 4037 Form.Element = {
rlm@37 4038 focus: function(element) {
rlm@37 4039 $(element).focus();
rlm@37 4040 return element;
rlm@37 4041 },
rlm@37 4042
rlm@37 4043 select: function(element) {
rlm@37 4044 $(element).select();
rlm@37 4045 return element;
rlm@37 4046 }
rlm@37 4047 };
rlm@37 4048
rlm@37 4049 Form.Element.Methods = {
rlm@37 4050
rlm@37 4051 serialize: function(element) {
rlm@37 4052 element = $(element);
rlm@37 4053 if (!element.disabled && element.name) {
rlm@37 4054 var value = element.getValue();
rlm@37 4055 if (value != undefined) {
rlm@37 4056 var pair = { };
rlm@37 4057 pair[element.name] = value;
rlm@37 4058 return Object.toQueryString(pair);
rlm@37 4059 }
rlm@37 4060 }
rlm@37 4061 return '';
rlm@37 4062 },
rlm@37 4063
rlm@37 4064 getValue: function(element) {
rlm@37 4065 element = $(element);
rlm@37 4066 var method = element.tagName.toLowerCase();
rlm@37 4067 return Form.Element.Serializers[method](element);
rlm@37 4068 },
rlm@37 4069
rlm@37 4070 setValue: function(element, value) {
rlm@37 4071 element = $(element);
rlm@37 4072 var method = element.tagName.toLowerCase();
rlm@37 4073 Form.Element.Serializers[method](element, value);
rlm@37 4074 return element;
rlm@37 4075 },
rlm@37 4076
rlm@37 4077 clear: function(element) {
rlm@37 4078 $(element).value = '';
rlm@37 4079 return element;
rlm@37 4080 },
rlm@37 4081
rlm@37 4082 present: function(element) {
rlm@37 4083 return $(element).value != '';
rlm@37 4084 },
rlm@37 4085
rlm@37 4086 activate: function(element) {
rlm@37 4087 element = $(element);
rlm@37 4088 try {
rlm@37 4089 element.focus();
rlm@37 4090 if (element.select && (element.tagName.toLowerCase() != 'input' ||
rlm@37 4091 !(/^(?:button|reset|submit)$/i.test(element.type))))
rlm@37 4092 element.select();
rlm@37 4093 } catch (e) { }
rlm@37 4094 return element;
rlm@37 4095 },
rlm@37 4096
rlm@37 4097 disable: function(element) {
rlm@37 4098 element = $(element);
rlm@37 4099 element.disabled = true;
rlm@37 4100 return element;
rlm@37 4101 },
rlm@37 4102
rlm@37 4103 enable: function(element) {
rlm@37 4104 element = $(element);
rlm@37 4105 element.disabled = false;
rlm@37 4106 return element;
rlm@37 4107 }
rlm@37 4108 };
rlm@37 4109
rlm@37 4110 /*--------------------------------------------------------------------------*/
rlm@37 4111
rlm@37 4112 var Field = Form.Element;
rlm@37 4113
rlm@37 4114 var $F = Form.Element.Methods.getValue;
rlm@37 4115
rlm@37 4116 /*--------------------------------------------------------------------------*/
rlm@37 4117
rlm@37 4118 Form.Element.Serializers = {
rlm@37 4119 input: function(element, value) {
rlm@37 4120 switch (element.type.toLowerCase()) {
rlm@37 4121 case 'checkbox':
rlm@37 4122 case 'radio':
rlm@37 4123 return Form.Element.Serializers.inputSelector(element, value);
rlm@37 4124 default:
rlm@37 4125 return Form.Element.Serializers.textarea(element, value);
rlm@37 4126 }
rlm@37 4127 },
rlm@37 4128
rlm@37 4129 inputSelector: function(element, value) {
rlm@37 4130 if (Object.isUndefined(value)) return element.checked ? element.value : null;
rlm@37 4131 else element.checked = !!value;
rlm@37 4132 },
rlm@37 4133
rlm@37 4134 textarea: function(element, value) {
rlm@37 4135 if (Object.isUndefined(value)) return element.value;
rlm@37 4136 else element.value = value;
rlm@37 4137 },
rlm@37 4138
rlm@37 4139 select: function(element, value) {
rlm@37 4140 if (Object.isUndefined(value))
rlm@37 4141 return this[element.type == 'select-one' ?
rlm@37 4142 'selectOne' : 'selectMany'](element);
rlm@37 4143 else {
rlm@37 4144 var opt, currentValue, single = !Object.isArray(value);
rlm@37 4145 for (var i = 0, length = element.length; i < length; i++) {
rlm@37 4146 opt = element.options[i];
rlm@37 4147 currentValue = this.optionValue(opt);
rlm@37 4148 if (single) {
rlm@37 4149 if (currentValue == value) {
rlm@37 4150 opt.selected = true;
rlm@37 4151 return;
rlm@37 4152 }
rlm@37 4153 }
rlm@37 4154 else opt.selected = value.include(currentValue);
rlm@37 4155 }
rlm@37 4156 }
rlm@37 4157 },
rlm@37 4158
rlm@37 4159 selectOne: function(element) {
rlm@37 4160 var index = element.selectedIndex;
rlm@37 4161 return index >= 0 ? this.optionValue(element.options[index]) : null;
rlm@37 4162 },
rlm@37 4163
rlm@37 4164 selectMany: function(element) {
rlm@37 4165 var values, length = element.length;
rlm@37 4166 if (!length) return null;
rlm@37 4167
rlm@37 4168 for (var i = 0, values = []; i < length; i++) {
rlm@37 4169 var opt = element.options[i];
rlm@37 4170 if (opt.selected) values.push(this.optionValue(opt));
rlm@37 4171 }
rlm@37 4172 return values;
rlm@37 4173 },
rlm@37 4174
rlm@37 4175 optionValue: function(opt) {
rlm@37 4176 return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text;
rlm@37 4177 }
rlm@37 4178 };
rlm@37 4179
rlm@37 4180 /*--------------------------------------------------------------------------*/
rlm@37 4181
rlm@37 4182
rlm@37 4183 Abstract.TimedObserver = Class.create(PeriodicalExecuter, {
rlm@37 4184 initialize: function($super, element, frequency, callback) {
rlm@37 4185 $super(callback, frequency);
rlm@37 4186 this.element = $(element);
rlm@37 4187 this.lastValue = this.getValue();
rlm@37 4188 },
rlm@37 4189
rlm@37 4190 execute: function() {
rlm@37 4191 var value = this.getValue();
rlm@37 4192 if (Object.isString(this.lastValue) && Object.isString(value) ?
rlm@37 4193 this.lastValue != value : String(this.lastValue) != String(value)) {
rlm@37 4194 this.callback(this.element, value);
rlm@37 4195 this.lastValue = value;
rlm@37 4196 }
rlm@37 4197 }
rlm@37 4198 });
rlm@37 4199
rlm@37 4200 Form.Element.Observer = Class.create(Abstract.TimedObserver, {
rlm@37 4201 getValue: function() {
rlm@37 4202 return Form.Element.getValue(this.element);
rlm@37 4203 }
rlm@37 4204 });
rlm@37 4205
rlm@37 4206 Form.Observer = Class.create(Abstract.TimedObserver, {
rlm@37 4207 getValue: function() {
rlm@37 4208 return Form.serialize(this.element);
rlm@37 4209 }
rlm@37 4210 });
rlm@37 4211
rlm@37 4212 /*--------------------------------------------------------------------------*/
rlm@37 4213
rlm@37 4214 Abstract.EventObserver = Class.create({
rlm@37 4215 initialize: function(element, callback) {
rlm@37 4216 this.element = $(element);
rlm@37 4217 this.callback = callback;
rlm@37 4218
rlm@37 4219 this.lastValue = this.getValue();
rlm@37 4220 if (this.element.tagName.toLowerCase() == 'form')
rlm@37 4221 this.registerFormCallbacks();
rlm@37 4222 else
rlm@37 4223 this.registerCallback(this.element);
rlm@37 4224 },
rlm@37 4225
rlm@37 4226 onElementEvent: function() {
rlm@37 4227 var value = this.getValue();
rlm@37 4228 if (this.lastValue != value) {
rlm@37 4229 this.callback(this.element, value);
rlm@37 4230 this.lastValue = value;
rlm@37 4231 }
rlm@37 4232 },
rlm@37 4233
rlm@37 4234 registerFormCallbacks: function() {
rlm@37 4235 Form.getElements(this.element).each(this.registerCallback, this);
rlm@37 4236 },
rlm@37 4237
rlm@37 4238 registerCallback: function(element) {
rlm@37 4239 if (element.type) {
rlm@37 4240 switch (element.type.toLowerCase()) {
rlm@37 4241 case 'checkbox':
rlm@37 4242 case 'radio':
rlm@37 4243 Event.observe(element, 'click', this.onElementEvent.bind(this));
rlm@37 4244 break;
rlm@37 4245 default:
rlm@37 4246 Event.observe(element, 'change', this.onElementEvent.bind(this));
rlm@37 4247 break;
rlm@37 4248 }
rlm@37 4249 }
rlm@37 4250 }
rlm@37 4251 });
rlm@37 4252
rlm@37 4253 Form.Element.EventObserver = Class.create(Abstract.EventObserver, {
rlm@37 4254 getValue: function() {
rlm@37 4255 return Form.Element.getValue(this.element);
rlm@37 4256 }
rlm@37 4257 });
rlm@37 4258
rlm@37 4259 Form.EventObserver = Class.create(Abstract.EventObserver, {
rlm@37 4260 getValue: function() {
rlm@37 4261 return Form.serialize(this.element);
rlm@37 4262 }
rlm@37 4263 });
rlm@37 4264 (function() {
rlm@37 4265
rlm@37 4266 var Event = {
rlm@37 4267 KEY_BACKSPACE: 8,
rlm@37 4268 KEY_TAB: 9,
rlm@37 4269 KEY_RETURN: 13,
rlm@37 4270 KEY_ESC: 27,
rlm@37 4271 KEY_LEFT: 37,
rlm@37 4272 KEY_UP: 38,
rlm@37 4273 KEY_RIGHT: 39,
rlm@37 4274 KEY_DOWN: 40,
rlm@37 4275 KEY_DELETE: 46,
rlm@37 4276 KEY_HOME: 36,
rlm@37 4277 KEY_END: 35,
rlm@37 4278 KEY_PAGEUP: 33,
rlm@37 4279 KEY_PAGEDOWN: 34,
rlm@37 4280 KEY_INSERT: 45,
rlm@37 4281
rlm@37 4282 cache: {}
rlm@37 4283 };
rlm@37 4284
rlm@37 4285 var docEl = document.documentElement;
rlm@37 4286 var MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED = 'onmouseenter' in docEl
rlm@37 4287 && 'onmouseleave' in docEl;
rlm@37 4288
rlm@37 4289 var _isButton;
rlm@37 4290 if (Prototype.Browser.IE) {
rlm@37 4291 var buttonMap = { 0: 1, 1: 4, 2: 2 };
rlm@37 4292 _isButton = function(event, code) {
rlm@37 4293 return event.button === buttonMap[code];
rlm@37 4294 };
rlm@37 4295 } else if (Prototype.Browser.WebKit) {
rlm@37 4296 _isButton = function(event, code) {
rlm@37 4297 switch (code) {
rlm@37 4298 case 0: return event.which == 1 && !event.metaKey;
rlm@37 4299 case 1: return event.which == 1 && event.metaKey;
rlm@37 4300 default: return false;
rlm@37 4301 }
rlm@37 4302 };
rlm@37 4303 } else {
rlm@37 4304 _isButton = function(event, code) {
rlm@37 4305 return event.which ? (event.which === code + 1) : (event.button === code);
rlm@37 4306 };
rlm@37 4307 }
rlm@37 4308
rlm@37 4309 function isLeftClick(event) { return _isButton(event, 0) }
rlm@37 4310
rlm@37 4311 function isMiddleClick(event) { return _isButton(event, 1) }
rlm@37 4312
rlm@37 4313 function isRightClick(event) { return _isButton(event, 2) }
rlm@37 4314
rlm@37 4315 function element(event) {
rlm@37 4316 event = Event.extend(event);
rlm@37 4317
rlm@37 4318 var node = event.target, type = event.type,
rlm@37 4319 currentTarget = event.currentTarget;
rlm@37 4320
rlm@37 4321 if (currentTarget && currentTarget.tagName) {
rlm@37 4322 if (type === 'load' || type === 'error' ||
rlm@37 4323 (type === 'click' && currentTarget.tagName.toLowerCase() === 'input'
rlm@37 4324 && currentTarget.type === 'radio'))
rlm@37 4325 node = currentTarget;
rlm@37 4326 }
rlm@37 4327
rlm@37 4328 if (node.nodeType == Node.TEXT_NODE)
rlm@37 4329 node = node.parentNode;
rlm@37 4330
rlm@37 4331 return Element.extend(node);
rlm@37 4332 }
rlm@37 4333
rlm@37 4334 function findElement(event, expression) {
rlm@37 4335 var element = Event.element(event);
rlm@37 4336 if (!expression) return element;
rlm@37 4337 var elements = [element].concat(element.ancestors());
rlm@37 4338 return Selector.findElement(elements, expression, 0);
rlm@37 4339 }
rlm@37 4340
rlm@37 4341 function pointer(event) {
rlm@37 4342 return { x: pointerX(event), y: pointerY(event) };
rlm@37 4343 }
rlm@37 4344
rlm@37 4345 function pointerX(event) {
rlm@37 4346 var docElement = document.documentElement,
rlm@37 4347 body = document.body || { scrollLeft: 0 };
rlm@37 4348
rlm@37 4349 return event.pageX || (event.clientX +
rlm@37 4350 (docElement.scrollLeft || body.scrollLeft) -
rlm@37 4351 (docElement.clientLeft || 0));
rlm@37 4352 }
rlm@37 4353
rlm@37 4354 function pointerY(event) {
rlm@37 4355 var docElement = document.documentElement,
rlm@37 4356 body = document.body || { scrollTop: 0 };
rlm@37 4357
rlm@37 4358 return event.pageY || (event.clientY +
rlm@37 4359 (docElement.scrollTop || body.scrollTop) -
rlm@37 4360 (docElement.clientTop || 0));
rlm@37 4361 }
rlm@37 4362
rlm@37 4363
rlm@37 4364 function stop(event) {
rlm@37 4365 Event.extend(event);
rlm@37 4366 event.preventDefault();
rlm@37 4367 event.stopPropagation();
rlm@37 4368
rlm@37 4369 event.stopped = true;
rlm@37 4370 }
rlm@37 4371
rlm@37 4372 Event.Methods = {
rlm@37 4373 isLeftClick: isLeftClick,
rlm@37 4374 isMiddleClick: isMiddleClick,
rlm@37 4375 isRightClick: isRightClick,
rlm@37 4376
rlm@37 4377 element: element,
rlm@37 4378 findElement: findElement,
rlm@37 4379
rlm@37 4380 pointer: pointer,
rlm@37 4381 pointerX: pointerX,
rlm@37 4382 pointerY: pointerY,
rlm@37 4383
rlm@37 4384 stop: stop
rlm@37 4385 };
rlm@37 4386
rlm@37 4387
rlm@37 4388 var methods = Object.keys(Event.Methods).inject({ }, function(m, name) {
rlm@37 4389 m[name] = Event.Methods[name].methodize();
rlm@37 4390 return m;
rlm@37 4391 });
rlm@37 4392
rlm@37 4393 if (Prototype.Browser.IE) {
rlm@37 4394 function _relatedTarget(event) {
rlm@37 4395 var element;
rlm@37 4396 switch (event.type) {
rlm@37 4397 case 'mouseover': element = event.fromElement; break;
rlm@37 4398 case 'mouseout': element = event.toElement; break;
rlm@37 4399 default: return null;
rlm@37 4400 }
rlm@37 4401 return Element.extend(element);
rlm@37 4402 }
rlm@37 4403
rlm@37 4404 Object.extend(methods, {
rlm@37 4405 stopPropagation: function() { this.cancelBubble = true },
rlm@37 4406 preventDefault: function() { this.returnValue = false },
rlm@37 4407 inspect: function() { return '[object Event]' }
rlm@37 4408 });
rlm@37 4409
rlm@37 4410 Event.extend = function(event, element) {
rlm@37 4411 if (!event) return false;
rlm@37 4412 if (event._extendedByPrototype) return event;
rlm@37 4413
rlm@37 4414 event._extendedByPrototype = Prototype.emptyFunction;
rlm@37 4415 var pointer = Event.pointer(event);
rlm@37 4416
rlm@37 4417 Object.extend(event, {
rlm@37 4418 target: event.srcElement || element,
rlm@37 4419 relatedTarget: _relatedTarget(event),
rlm@37 4420 pageX: pointer.x,
rlm@37 4421 pageY: pointer.y
rlm@37 4422 });
rlm@37 4423
rlm@37 4424 return Object.extend(event, methods);
rlm@37 4425 };
rlm@37 4426 } else {
rlm@37 4427 Event.prototype = window.Event.prototype || document.createEvent('HTMLEvents').__proto__;
rlm@37 4428 Object.extend(Event.prototype, methods);
rlm@37 4429 Event.extend = Prototype.K;
rlm@37 4430 }
rlm@37 4431
rlm@37 4432 function _createResponder(element, eventName, handler) {
rlm@37 4433 var registry = Element.retrieve(element, 'prototype_event_registry');
rlm@37 4434
rlm@37 4435 if (Object.isUndefined(registry)) {
rlm@37 4436 CACHE.push(element);
rlm@37 4437 registry = Element.retrieve(element, 'prototype_event_registry', $H());
rlm@37 4438 }
rlm@37 4439
rlm@37 4440 var respondersForEvent = registry.get(eventName);
rlm@37 4441 if (Object.isUndefined(respondersForEvent)) {
rlm@37 4442 respondersForEvent = [];
rlm@37 4443 registry.set(eventName, respondersForEvent);
rlm@37 4444 }
rlm@37 4445
rlm@37 4446 if (respondersForEvent.pluck('handler').include(handler)) return false;
rlm@37 4447
rlm@37 4448 var responder;
rlm@37 4449 if (eventName.include(":")) {
rlm@37 4450 responder = function(event) {
rlm@37 4451 if (Object.isUndefined(event.eventName))
rlm@37 4452 return false;
rlm@37 4453
rlm@37 4454 if (event.eventName !== eventName)
rlm@37 4455 return false;
rlm@37 4456
rlm@37 4457 Event.extend(event, element);
rlm@37 4458 handler.call(element, event);
rlm@37 4459 };
rlm@37 4460 } else {
rlm@37 4461 if (!MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED &&
rlm@37 4462 (eventName === "mouseenter" || eventName === "mouseleave")) {
rlm@37 4463 if (eventName === "mouseenter" || eventName === "mouseleave") {
rlm@37 4464 responder = function(event) {
rlm@37 4465 Event.extend(event, element);
rlm@37 4466
rlm@37 4467 var parent = event.relatedTarget;
rlm@37 4468 while (parent && parent !== element) {
rlm@37 4469 try { parent = parent.parentNode; }
rlm@37 4470 catch(e) { parent = element; }
rlm@37 4471 }
rlm@37 4472
rlm@37 4473 if (parent === element) return;
rlm@37 4474
rlm@37 4475 handler.call(element, event);
rlm@37 4476 };
rlm@37 4477 }
rlm@37 4478 } else {
rlm@37 4479 responder = function(event) {
rlm@37 4480 Event.extend(event, element);
rlm@37 4481 handler.call(element, event);
rlm@37 4482 };
rlm@37 4483 }
rlm@37 4484 }
rlm@37 4485
rlm@37 4486 responder.handler = handler;
rlm@37 4487 respondersForEvent.push(responder);
rlm@37 4488 return responder;
rlm@37 4489 }
rlm@37 4490
rlm@37 4491 function _destroyCache() {
rlm@37 4492 for (var i = 0, length = CACHE.length; i < length; i++) {
rlm@37 4493 Event.stopObserving(CACHE[i]);
rlm@37 4494 CACHE[i] = null;
rlm@37 4495 }
rlm@37 4496 }
rlm@37 4497
rlm@37 4498 var CACHE = [];
rlm@37 4499
rlm@37 4500 if (Prototype.Browser.IE)
rlm@37 4501 window.attachEvent('onunload', _destroyCache);
rlm@37 4502
rlm@37 4503 if (Prototype.Browser.WebKit)
rlm@37 4504 window.addEventListener('unload', Prototype.emptyFunction, false);
rlm@37 4505
rlm@37 4506
rlm@37 4507 var _getDOMEventName = Prototype.K;
rlm@37 4508
rlm@37 4509 if (!MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED) {
rlm@37 4510 _getDOMEventName = function(eventName) {
rlm@37 4511 var translations = { mouseenter: "mouseover", mouseleave: "mouseout" };
rlm@37 4512 return eventName in translations ? translations[eventName] : eventName;
rlm@37 4513 };
rlm@37 4514 }
rlm@37 4515
rlm@37 4516 function observe(element, eventName, handler) {
rlm@37 4517 element = $(element);
rlm@37 4518
rlm@37 4519 var responder = _createResponder(element, eventName, handler);
rlm@37 4520
rlm@37 4521 if (!responder) return element;
rlm@37 4522
rlm@37 4523 if (eventName.include(':')) {
rlm@37 4524 if (element.addEventListener)
rlm@37 4525 element.addEventListener("dataavailable", responder, false);
rlm@37 4526 else {
rlm@37 4527 element.attachEvent("ondataavailable", responder);
rlm@37 4528 element.attachEvent("onfilterchange", responder);
rlm@37 4529 }
rlm@37 4530 } else {
rlm@37 4531 var actualEventName = _getDOMEventName(eventName);
rlm@37 4532
rlm@37 4533 if (element.addEventListener)
rlm@37 4534 element.addEventListener(actualEventName, responder, false);
rlm@37 4535 else
rlm@37 4536 element.attachEvent("on" + actualEventName, responder);
rlm@37 4537 }
rlm@37 4538
rlm@37 4539 return element;
rlm@37 4540 }
rlm@37 4541
rlm@37 4542 function stopObserving(element, eventName, handler) {
rlm@37 4543 element = $(element);
rlm@37 4544
rlm@37 4545 var registry = Element.retrieve(element, 'prototype_event_registry');
rlm@37 4546
rlm@37 4547 if (Object.isUndefined(registry)) return element;
rlm@37 4548
rlm@37 4549 if (eventName && !handler) {
rlm@37 4550 var responders = registry.get(eventName);
rlm@37 4551
rlm@37 4552 if (Object.isUndefined(responders)) return element;
rlm@37 4553
rlm@37 4554 responders.each( function(r) {
rlm@37 4555 Element.stopObserving(element, eventName, r.handler);
rlm@37 4556 });
rlm@37 4557 return element;
rlm@37 4558 } else if (!eventName) {
rlm@37 4559 registry.each( function(pair) {
rlm@37 4560 var eventName = pair.key, responders = pair.value;
rlm@37 4561
rlm@37 4562 responders.each( function(r) {
rlm@37 4563 Element.stopObserving(element, eventName, r.handler);
rlm@37 4564 });
rlm@37 4565 });
rlm@37 4566 return element;
rlm@37 4567 }
rlm@37 4568
rlm@37 4569 var responders = registry.get(eventName);
rlm@37 4570
rlm@37 4571 if (!responders) return;
rlm@37 4572
rlm@37 4573 var responder = responders.find( function(r) { return r.handler === handler; });
rlm@37 4574 if (!responder) return element;
rlm@37 4575
rlm@37 4576 var actualEventName = _getDOMEventName(eventName);
rlm@37 4577
rlm@37 4578 if (eventName.include(':')) {
rlm@37 4579 if (element.removeEventListener)
rlm@37 4580 element.removeEventListener("dataavailable", responder, false);
rlm@37 4581 else {
rlm@37 4582 element.detachEvent("ondataavailable", responder);
rlm@37 4583 element.detachEvent("onfilterchange", responder);
rlm@37 4584 }
rlm@37 4585 } else {
rlm@37 4586 if (element.removeEventListener)
rlm@37 4587 element.removeEventListener(actualEventName, responder, false);
rlm@37 4588 else
rlm@37 4589 element.detachEvent('on' + actualEventName, responder);
rlm@37 4590 }
rlm@37 4591
rlm@37 4592 registry.set(eventName, responders.without(responder));
rlm@37 4593
rlm@37 4594 return element;
rlm@37 4595 }
rlm@37 4596
rlm@37 4597 function fire(element, eventName, memo, bubble) {
rlm@37 4598 element = $(element);
rlm@37 4599
rlm@37 4600 if (Object.isUndefined(bubble))
rlm@37 4601 bubble = true;
rlm@37 4602
rlm@37 4603 if (element == document && document.createEvent && !element.dispatchEvent)
rlm@37 4604 element = document.documentElement;
rlm@37 4605
rlm@37 4606 var event;
rlm@37 4607 if (document.createEvent) {
rlm@37 4608 event = document.createEvent('HTMLEvents');
rlm@37 4609 event.initEvent('dataavailable', true, true);
rlm@37 4610 } else {
rlm@37 4611 event = document.createEventObject();
rlm@37 4612 event.eventType = bubble ? 'ondataavailable' : 'onfilterchange';
rlm@37 4613 }
rlm@37 4614
rlm@37 4615 event.eventName = eventName;
rlm@37 4616 event.memo = memo || { };
rlm@37 4617
rlm@37 4618 if (document.createEvent)
rlm@37 4619 element.dispatchEvent(event);
rlm@37 4620 else
rlm@37 4621 element.fireEvent(event.eventType, event);
rlm@37 4622
rlm@37 4623 return Event.extend(event);
rlm@37 4624 }
rlm@37 4625
rlm@37 4626
rlm@37 4627 Object.extend(Event, Event.Methods);
rlm@37 4628
rlm@37 4629 Object.extend(Event, {
rlm@37 4630 fire: fire,
rlm@37 4631 observe: observe,
rlm@37 4632 stopObserving: stopObserving
rlm@37 4633 });
rlm@37 4634
rlm@37 4635 Element.addMethods({
rlm@37 4636 fire: fire,
rlm@37 4637
rlm@37 4638 observe: observe,
rlm@37 4639
rlm@37 4640 stopObserving: stopObserving
rlm@37 4641 });
rlm@37 4642
rlm@37 4643 Object.extend(document, {
rlm@37 4644 fire: fire.methodize(),
rlm@37 4645
rlm@37 4646 observe: observe.methodize(),
rlm@37 4647
rlm@37 4648 stopObserving: stopObserving.methodize(),
rlm@37 4649
rlm@37 4650 loaded: false
rlm@37 4651 });
rlm@37 4652
rlm@37 4653 if (window.Event) Object.extend(window.Event, Event);
rlm@37 4654 else window.Event = Event;
rlm@37 4655 })();
rlm@37 4656
rlm@37 4657 (function() {
rlm@37 4658 /* Support for the DOMContentLoaded event is based on work by Dan Webb,
rlm@37 4659 Matthias Miller, Dean Edwards, John Resig, and Diego Perini. */
rlm@37 4660
rlm@37 4661 var timer;
rlm@37 4662
rlm@37 4663 function fireContentLoadedEvent() {
rlm@37 4664 if (document.loaded) return;
rlm@37 4665 if (timer) window.clearTimeout(timer);
rlm@37 4666 document.loaded = true;
rlm@37 4667 document.fire('dom:loaded');
rlm@37 4668 }
rlm@37 4669
rlm@37 4670 function checkReadyState() {
rlm@37 4671 if (document.readyState === 'complete') {
rlm@37 4672 document.stopObserving('readystatechange', checkReadyState);
rlm@37 4673 fireContentLoadedEvent();
rlm@37 4674 }
rlm@37 4675 }
rlm@37 4676
rlm@37 4677 function pollDoScroll() {
rlm@37 4678 try { document.documentElement.doScroll('left'); }
rlm@37 4679 catch(e) {
rlm@37 4680 timer = pollDoScroll.defer();
rlm@37 4681 return;
rlm@37 4682 }
rlm@37 4683 fireContentLoadedEvent();
rlm@37 4684 }
rlm@37 4685
rlm@37 4686 if (document.addEventListener) {
rlm@37 4687 document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false);
rlm@37 4688 } else {
rlm@37 4689 document.observe('readystatechange', checkReadyState);
rlm@37 4690 if (window == top)
rlm@37 4691 timer = pollDoScroll.defer();
rlm@37 4692 }
rlm@37 4693
rlm@37 4694 Event.observe(window, 'load', fireContentLoadedEvent);
rlm@37 4695 })();
rlm@37 4696
rlm@37 4697 Element.addMethods();
rlm@37 4698
rlm@37 4699 /*------------------------------- DEPRECATED -------------------------------*/
rlm@37 4700
rlm@37 4701 Hash.toQueryString = Object.toQueryString;
rlm@37 4702
rlm@37 4703 var Toggle = { display: Element.toggle };
rlm@37 4704
rlm@37 4705 Element.Methods.childOf = Element.Methods.descendantOf;
rlm@37 4706
rlm@37 4707 var Insertion = {
rlm@37 4708 Before: function(element, content) {
rlm@37 4709 return Element.insert(element, {before:content});
rlm@37 4710 },
rlm@37 4711
rlm@37 4712 Top: function(element, content) {
rlm@37 4713 return Element.insert(element, {top:content});
rlm@37 4714 },
rlm@37 4715
rlm@37 4716 Bottom: function(element, content) {
rlm@37 4717 return Element.insert(element, {bottom:content});
rlm@37 4718 },
rlm@37 4719
rlm@37 4720 After: function(element, content) {
rlm@37 4721 return Element.insert(element, {after:content});
rlm@37 4722 }
rlm@37 4723 };
rlm@37 4724
rlm@37 4725 var $continue = new Error('"throw $continue" is deprecated, use "return" instead');
rlm@37 4726
rlm@37 4727 var Position = {
rlm@37 4728 includeScrollOffsets: false,
rlm@37 4729
rlm@37 4730 prepare: function() {
rlm@37 4731 this.deltaX = window.pageXOffset
rlm@37 4732 || document.documentElement.scrollLeft
rlm@37 4733 || document.body.scrollLeft
rlm@37 4734 || 0;
rlm@37 4735 this.deltaY = window.pageYOffset
rlm@37 4736 || document.documentElement.scrollTop
rlm@37 4737 || document.body.scrollTop
rlm@37 4738 || 0;
rlm@37 4739 },
rlm@37 4740
rlm@37 4741 within: function(element, x, y) {
rlm@37 4742 if (this.includeScrollOffsets)
rlm@37 4743 return this.withinIncludingScrolloffsets(element, x, y);
rlm@37 4744 this.xcomp = x;
rlm@37 4745 this.ycomp = y;
rlm@37 4746 this.offset = Element.cumulativeOffset(element);
rlm@37 4747
rlm@37 4748 return (y >= this.offset[1] &&
rlm@37 4749 y < this.offset[1] + element.offsetHeight &&
rlm@37 4750 x >= this.offset[0] &&
rlm@37 4751 x < this.offset[0] + element.offsetWidth);
rlm@37 4752 },
rlm@37 4753
rlm@37 4754 withinIncludingScrolloffsets: function(element, x, y) {
rlm@37 4755 var offsetcache = Element.cumulativeScrollOffset(element);
rlm@37 4756
rlm@37 4757 this.xcomp = x + offsetcache[0] - this.deltaX;
rlm@37 4758 this.ycomp = y + offsetcache[1] - this.deltaY;
rlm@37 4759 this.offset = Element.cumulativeOffset(element);
rlm@37 4760
rlm@37 4761 return (this.ycomp >= this.offset[1] &&
rlm@37 4762 this.ycomp < this.offset[1] + element.offsetHeight &&
rlm@37 4763 this.xcomp >= this.offset[0] &&
rlm@37 4764 this.xcomp < this.offset[0] + element.offsetWidth);
rlm@37 4765 },
rlm@37 4766
rlm@37 4767 overlap: function(mode, element) {
rlm@37 4768 if (!mode) return 0;
rlm@37 4769 if (mode == 'vertical')
rlm@37 4770 return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
rlm@37 4771 element.offsetHeight;
rlm@37 4772 if (mode == 'horizontal')
rlm@37 4773 return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
rlm@37 4774 element.offsetWidth;
rlm@37 4775 },
rlm@37 4776
rlm@37 4777
rlm@37 4778 cumulativeOffset: Element.Methods.cumulativeOffset,
rlm@37 4779
rlm@37 4780 positionedOffset: Element.Methods.positionedOffset,
rlm@37 4781
rlm@37 4782 absolutize: function(element) {
rlm@37 4783 Position.prepare();
rlm@37 4784 return Element.absolutize(element);
rlm@37 4785 },
rlm@37 4786
rlm@37 4787 relativize: function(element) {
rlm@37 4788 Position.prepare();
rlm@37 4789 return Element.relativize(element);
rlm@37 4790 },
rlm@37 4791
rlm@37 4792 realOffset: Element.Methods.cumulativeScrollOffset,
rlm@37 4793
rlm@37 4794 offsetParent: Element.Methods.getOffsetParent,
rlm@37 4795
rlm@37 4796 page: Element.Methods.viewportOffset,
rlm@37 4797
rlm@37 4798 clone: function(source, target, options) {
rlm@37 4799 options = options || { };
rlm@37 4800 return Element.clonePosition(target, source, options);
rlm@37 4801 }
rlm@37 4802 };
rlm@37 4803
rlm@37 4804 /*--------------------------------------------------------------------------*/
rlm@37 4805
rlm@37 4806 if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){
rlm@37 4807 function iter(name) {
rlm@37 4808 return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]";
rlm@37 4809 }
rlm@37 4810
rlm@37 4811 instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ?
rlm@37 4812 function(element, className) {
rlm@37 4813 className = className.toString().strip();
rlm@37 4814 var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className);
rlm@37 4815 return cond ? document._getElementsByXPath('.//*' + cond, element) : [];
rlm@37 4816 } : function(element, className) {
rlm@37 4817 className = className.toString().strip();
rlm@37 4818 var elements = [], classNames = (/\s/.test(className) ? $w(className) : null);
rlm@37 4819 if (!classNames && !className) return elements;
rlm@37 4820
rlm@37 4821 var nodes = $(element).getElementsByTagName('*');
rlm@37 4822 className = ' ' + className + ' ';
rlm@37 4823
rlm@37 4824 for (var i = 0, child, cn; child = nodes[i]; i++) {
rlm@37 4825 if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) ||
rlm@37 4826 (classNames && classNames.all(function(name) {
rlm@37 4827 return !name.toString().blank() && cn.include(' ' + name + ' ');
rlm@37 4828 }))))
rlm@37 4829 elements.push(Element.extend(child));
rlm@37 4830 }
rlm@37 4831 return elements;
rlm@37 4832 };
rlm@37 4833
rlm@37 4834 return function(className, parentElement) {
rlm@37 4835 return $(parentElement || document.body).getElementsByClassName(className);
rlm@37 4836 };
rlm@37 4837 }(Element.Methods);
rlm@37 4838
rlm@37 4839 /*--------------------------------------------------------------------------*/
rlm@37 4840
rlm@37 4841 Element.ClassNames = Class.create();
rlm@37 4842 Element.ClassNames.prototype = {
rlm@37 4843 initialize: function(element) {
rlm@37 4844 this.element = $(element);
rlm@37 4845 },
rlm@37 4846
rlm@37 4847 _each: function(iterator) {
rlm@37 4848 this.element.className.split(/\s+/).select(function(name) {
rlm@37 4849 return name.length > 0;
rlm@37 4850 })._each(iterator);
rlm@37 4851 },
rlm@37 4852
rlm@37 4853 set: function(className) {
rlm@37 4854 this.element.className = className;
rlm@37 4855 },
rlm@37 4856
rlm@37 4857 add: function(classNameToAdd) {
rlm@37 4858 if (this.include(classNameToAdd)) return;
rlm@37 4859 this.set($A(this).concat(classNameToAdd).join(' '));
rlm@37 4860 },
rlm@37 4861
rlm@37 4862 remove: function(classNameToRemove) {
rlm@37 4863 if (!this.include(classNameToRemove)) return;
rlm@37 4864 this.set($A(this).without(classNameToRemove).join(' '));
rlm@37 4865 },
rlm@37 4866
rlm@37 4867 toString: function() {
rlm@37 4868 return $A(this).join(' ');
rlm@37 4869 }
rlm@37 4870 };
rlm@37 4871
rlm@37 4872 Object.extend(Element.ClassNames.prototype, Enumerable);
rlm@37 4873
rlm@37 4874 /*--------------------------------------------------------------------------*/