annotate js-lib/prototype.js @ 92:614dca52e323 laserkard

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