annotate onlypaths/js/prototype.js @ 70:751fb9fd445b laserkard

finally got a lisp server yay
author Robert McIntyre <rlm@mit.edu>
date Tue, 22 Jun 2010 20:15:19 -0400
parents 26c2b3ad21c7
children
rev   line source
rlm@46 1 /* Prototype JavaScript framework, version 1.4.0
rlm@46 2 * (c) 2005 Sam Stephenson <sam@conio.net>
rlm@46 3 *
rlm@46 4 * THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff
rlm@46 5 * against the source tree, available from the Prototype darcs repository.
rlm@46 6 *
rlm@46 7 * Prototype is freely distributable under the terms of an MIT-style license.
rlm@46 8 *
rlm@46 9 * For details, see the Prototype web site: http://prototype.conio.net/
rlm@46 10 *
rlm@46 11 /*--------------------------------------------------------------------------*/
rlm@46 12
rlm@46 13 var Prototype = {
rlm@46 14 Version: '1.4.0',
rlm@46 15 ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
rlm@46 16
rlm@46 17 emptyFunction: function() {},
rlm@46 18 K: function(x) {return x}
rlm@46 19 }
rlm@46 20
rlm@46 21 var Class = {
rlm@46 22 create: function() {
rlm@46 23 return function() {
rlm@46 24 this.initialize.apply(this, arguments);
rlm@46 25 }
rlm@46 26 }
rlm@46 27 }
rlm@46 28
rlm@46 29 var Abstract = new Object();
rlm@46 30
rlm@46 31 Object.extend = function(destination, source) {
rlm@46 32 for (property in source) {
rlm@46 33 destination[property] = source[property];
rlm@46 34 }
rlm@46 35 return destination;
rlm@46 36 }
rlm@46 37
rlm@46 38 Object.inspect = function(object) {
rlm@46 39 try {
rlm@46 40 if (object == undefined) return 'undefined';
rlm@46 41 if (object == null) return 'null';
rlm@46 42 return object.inspect ? object.inspect() : object.toString();
rlm@46 43 } catch (e) {
rlm@46 44 if (e instanceof RangeError) return '...';
rlm@46 45 throw e;
rlm@46 46 }
rlm@46 47 }
rlm@46 48
rlm@46 49 Function.prototype.bind = function() {
rlm@46 50 var __method = this, args = $A(arguments), object = args.shift();
rlm@46 51 return function() {
rlm@46 52 return __method.apply(object, args.concat($A(arguments)));
rlm@46 53 }
rlm@46 54 }
rlm@46 55
rlm@46 56 Function.prototype.bindAsEventListener = function(object) {
rlm@46 57 var __method = this;
rlm@46 58 return function(event) {
rlm@46 59 return __method.call(object, event || window.event);
rlm@46 60 }
rlm@46 61 }
rlm@46 62
rlm@46 63 Object.extend(Number.prototype, {
rlm@46 64 toColorPart: function() {
rlm@46 65 var digits = this.toString(16);
rlm@46 66 if (this < 16) return '0' + digits;
rlm@46 67 return digits;
rlm@46 68 },
rlm@46 69
rlm@46 70 succ: function() {
rlm@46 71 return this + 1;
rlm@46 72 },
rlm@46 73
rlm@46 74 times: function(iterator) {
rlm@46 75 $R(0, this, true).each(iterator);
rlm@46 76 return this;
rlm@46 77 }
rlm@46 78 });
rlm@46 79
rlm@46 80 var Try = {
rlm@46 81 these: function() {
rlm@46 82 var returnValue;
rlm@46 83
rlm@46 84 for (var i = 0; i < arguments.length; i++) {
rlm@46 85 var lambda = arguments[i];
rlm@46 86 try {
rlm@46 87 returnValue = lambda();
rlm@46 88 break;
rlm@46 89 } catch (e) {}
rlm@46 90 }
rlm@46 91
rlm@46 92 return returnValue;
rlm@46 93 }
rlm@46 94 }
rlm@46 95
rlm@46 96 /*--------------------------------------------------------------------------*/
rlm@46 97
rlm@46 98 var PeriodicalExecuter = Class.create();
rlm@46 99 PeriodicalExecuter.prototype = {
rlm@46 100 initialize: function(callback, frequency) {
rlm@46 101 this.callback = callback;
rlm@46 102 this.frequency = frequency;
rlm@46 103 this.currentlyExecuting = false;
rlm@46 104
rlm@46 105 this.registerCallback();
rlm@46 106 },
rlm@46 107
rlm@46 108 registerCallback: function() {
rlm@46 109 setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
rlm@46 110 },
rlm@46 111
rlm@46 112 onTimerEvent: function() {
rlm@46 113 if (!this.currentlyExecuting) {
rlm@46 114 try {
rlm@46 115 this.currentlyExecuting = true;
rlm@46 116 this.callback();
rlm@46 117 } finally {
rlm@46 118 this.currentlyExecuting = false;
rlm@46 119 }
rlm@46 120 }
rlm@46 121 }
rlm@46 122 }
rlm@46 123
rlm@46 124 /*--------------------------------------------------------------------------*/
rlm@46 125
rlm@46 126 function $() {
rlm@46 127 var elements = new Array();
rlm@46 128
rlm@46 129 for (var i = 0; i < arguments.length; i++) {
rlm@46 130 var element = arguments[i];
rlm@46 131 if (typeof element == 'string')
rlm@46 132 element = document.getElementById(element);
rlm@46 133
rlm@46 134 if (arguments.length == 1)
rlm@46 135 return element;
rlm@46 136
rlm@46 137 elements.push(element);
rlm@46 138 }
rlm@46 139
rlm@46 140 return elements;
rlm@46 141 }
rlm@46 142 Object.extend(String.prototype, {
rlm@46 143 stripTags: function() {
rlm@46 144 return this.replace(/<\/?[^>]+>/gi, '');
rlm@46 145 },
rlm@46 146
rlm@46 147 stripScripts: function() {
rlm@46 148 return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
rlm@46 149 },
rlm@46 150
rlm@46 151 extractScripts: function() {
rlm@46 152 var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
rlm@46 153 var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
rlm@46 154 return (this.match(matchAll) || []).map(function(scriptTag) {
rlm@46 155 return (scriptTag.match(matchOne) || ['', ''])[1];
rlm@46 156 });
rlm@46 157 },
rlm@46 158
rlm@46 159 evalScripts: function() {
rlm@46 160 return this.extractScripts().map(eval);
rlm@46 161 },
rlm@46 162
rlm@46 163 escapeHTML: function() {
rlm@46 164 var div = document.createElement('div');
rlm@46 165 var text = document.createTextNode(this);
rlm@46 166 div.appendChild(text);
rlm@46 167 return div.innerHTML;
rlm@46 168 },
rlm@46 169
rlm@46 170 unescapeHTML: function() {
rlm@46 171 var div = document.createElement('div');
rlm@46 172 div.innerHTML = this.stripTags();
rlm@46 173 return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
rlm@46 174 },
rlm@46 175
rlm@46 176 toQueryParams: function() {
rlm@46 177 var pairs = this.match(/^\??(.*)$/)[1].split('&');
rlm@46 178 return pairs.inject({}, function(params, pairString) {
rlm@46 179 var pair = pairString.split('=');
rlm@46 180 params[pair[0]] = pair[1];
rlm@46 181 return params;
rlm@46 182 });
rlm@46 183 },
rlm@46 184
rlm@46 185 toArray: function() {
rlm@46 186 return this.split('');
rlm@46 187 },
rlm@46 188
rlm@46 189 camelize: function() {
rlm@46 190 var oStringList = this.split('-');
rlm@46 191 if (oStringList.length == 1) return oStringList[0];
rlm@46 192
rlm@46 193 var camelizedString = this.indexOf('-') == 0
rlm@46 194 ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1)
rlm@46 195 : oStringList[0];
rlm@46 196
rlm@46 197 for (var i = 1, len = oStringList.length; i < len; i++) {
rlm@46 198 var s = oStringList[i];
rlm@46 199 camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
rlm@46 200 }
rlm@46 201
rlm@46 202 return camelizedString;
rlm@46 203 },
rlm@46 204
rlm@46 205 inspect: function() {
rlm@46 206 return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'";
rlm@46 207 }
rlm@46 208 });
rlm@46 209
rlm@46 210 String.prototype.parseQuery = String.prototype.toQueryParams;
rlm@46 211
rlm@46 212 var $break = new Object();
rlm@46 213 var $continue = new Object();
rlm@46 214
rlm@46 215 var Enumerable = {
rlm@46 216 each: function(iterator) {
rlm@46 217 var index = 0;
rlm@46 218 try {
rlm@46 219 this._each(function(value) {
rlm@46 220 try {
rlm@46 221 iterator(value, index++);
rlm@46 222 } catch (e) {
rlm@46 223 if (e != $continue) throw e;
rlm@46 224 }
rlm@46 225 });
rlm@46 226 } catch (e) {
rlm@46 227 if (e != $break) throw e;
rlm@46 228 }
rlm@46 229 },
rlm@46 230
rlm@46 231 all: function(iterator) {
rlm@46 232 var result = true;
rlm@46 233 this.each(function(value, index) {
rlm@46 234 result = result && !!(iterator || Prototype.K)(value, index);
rlm@46 235 if (!result) throw $break;
rlm@46 236 });
rlm@46 237 return result;
rlm@46 238 },
rlm@46 239
rlm@46 240 any: function(iterator) {
rlm@46 241 var result = true;
rlm@46 242 this.each(function(value, index) {
rlm@46 243 if (result = !!(iterator || Prototype.K)(value, index))
rlm@46 244 throw $break;
rlm@46 245 });
rlm@46 246 return result;
rlm@46 247 },
rlm@46 248
rlm@46 249 collect: function(iterator) {
rlm@46 250 var results = [];
rlm@46 251 this.each(function(value, index) {
rlm@46 252 results.push(iterator(value, index));
rlm@46 253 });
rlm@46 254 return results;
rlm@46 255 },
rlm@46 256
rlm@46 257 detect: function (iterator) {
rlm@46 258 var result;
rlm@46 259 this.each(function(value, index) {
rlm@46 260 if (iterator(value, index)) {
rlm@46 261 result = value;
rlm@46 262 throw $break;
rlm@46 263 }
rlm@46 264 });
rlm@46 265 return result;
rlm@46 266 },
rlm@46 267
rlm@46 268 findAll: function(iterator) {
rlm@46 269 var results = [];
rlm@46 270 this.each(function(value, index) {
rlm@46 271 if (iterator(value, index))
rlm@46 272 results.push(value);
rlm@46 273 });
rlm@46 274 return results;
rlm@46 275 },
rlm@46 276
rlm@46 277 grep: function(pattern, iterator) {
rlm@46 278 var results = [];
rlm@46 279 this.each(function(value, index) {
rlm@46 280 var stringValue = value.toString();
rlm@46 281 if (stringValue.match(pattern))
rlm@46 282 results.push((iterator || Prototype.K)(value, index));
rlm@46 283 })
rlm@46 284 return results;
rlm@46 285 },
rlm@46 286
rlm@46 287 include: function(object) {
rlm@46 288 var found = false;
rlm@46 289 this.each(function(value) {
rlm@46 290 if (value == object) {
rlm@46 291 found = true;
rlm@46 292 throw $break;
rlm@46 293 }
rlm@46 294 });
rlm@46 295 return found;
rlm@46 296 },
rlm@46 297
rlm@46 298 inject: function(memo, iterator) {
rlm@46 299 this.each(function(value, index) {
rlm@46 300 memo = iterator(memo, value, index);
rlm@46 301 });
rlm@46 302 return memo;
rlm@46 303 },
rlm@46 304
rlm@46 305 invoke: function(method) {
rlm@46 306 var args = $A(arguments).slice(1);
rlm@46 307 return this.collect(function(value) {
rlm@46 308 return value[method].apply(value, args);
rlm@46 309 });
rlm@46 310 },
rlm@46 311
rlm@46 312 max: function(iterator) {
rlm@46 313 var result;
rlm@46 314 this.each(function(value, index) {
rlm@46 315 value = (iterator || Prototype.K)(value, index);
rlm@46 316 if (value >= (result || value))
rlm@46 317 result = value;
rlm@46 318 });
rlm@46 319 return result;
rlm@46 320 },
rlm@46 321
rlm@46 322 min: function(iterator) {
rlm@46 323 var result;
rlm@46 324 this.each(function(value, index) {
rlm@46 325 value = (iterator || Prototype.K)(value, index);
rlm@46 326 if (value <= (result || value))
rlm@46 327 result = value;
rlm@46 328 });
rlm@46 329 return result;
rlm@46 330 },
rlm@46 331
rlm@46 332 partition: function(iterator) {
rlm@46 333 var trues = [], falses = [];
rlm@46 334 this.each(function(value, index) {
rlm@46 335 ((iterator || Prototype.K)(value, index) ?
rlm@46 336 trues : falses).push(value);
rlm@46 337 });
rlm@46 338 return [trues, falses];
rlm@46 339 },
rlm@46 340
rlm@46 341 pluck: function(property) {
rlm@46 342 var results = [];
rlm@46 343 this.each(function(value, index) {
rlm@46 344 results.push(value[property]);
rlm@46 345 });
rlm@46 346 return results;
rlm@46 347 },
rlm@46 348
rlm@46 349 reject: function(iterator) {
rlm@46 350 var results = [];
rlm@46 351 this.each(function(value, index) {
rlm@46 352 if (!iterator(value, index))
rlm@46 353 results.push(value);
rlm@46 354 });
rlm@46 355 return results;
rlm@46 356 },
rlm@46 357
rlm@46 358 sortBy: function(iterator) {
rlm@46 359 return this.collect(function(value, index) {
rlm@46 360 return {value: value, criteria: iterator(value, index)};
rlm@46 361 }).sort(function(left, right) {
rlm@46 362 var a = left.criteria, b = right.criteria;
rlm@46 363 return a < b ? -1 : a > b ? 1 : 0;
rlm@46 364 }).pluck('value');
rlm@46 365 },
rlm@46 366
rlm@46 367 toArray: function() {
rlm@46 368 return this.collect(Prototype.K);
rlm@46 369 },
rlm@46 370
rlm@46 371 zip: function() {
rlm@46 372 var iterator = Prototype.K, args = $A(arguments);
rlm@46 373 if (typeof args.last() == 'function')
rlm@46 374 iterator = args.pop();
rlm@46 375
rlm@46 376 var collections = [this].concat(args).map($A);
rlm@46 377 return this.map(function(value, index) {
rlm@46 378 iterator(value = collections.pluck(index));
rlm@46 379 return value;
rlm@46 380 });
rlm@46 381 },
rlm@46 382
rlm@46 383 inspect: function() {
rlm@46 384 return '#<Enumerable:' + this.toArray().inspect() + '>';
rlm@46 385 }
rlm@46 386 }
rlm@46 387
rlm@46 388 Object.extend(Enumerable, {
rlm@46 389 map: Enumerable.collect,
rlm@46 390 find: Enumerable.detect,
rlm@46 391 select: Enumerable.findAll,
rlm@46 392 member: Enumerable.include,
rlm@46 393 entries: Enumerable.toArray
rlm@46 394 });
rlm@46 395 var $A = Array.from = function(iterable) {
rlm@46 396 if (!iterable) return [];
rlm@46 397 if (iterable.toArray) {
rlm@46 398 return iterable.toArray();
rlm@46 399 } else {
rlm@46 400 var results = [];
rlm@46 401 for (var i = 0; i < iterable.length; i++)
rlm@46 402 results.push(iterable[i]);
rlm@46 403 return results;
rlm@46 404 }
rlm@46 405 }
rlm@46 406
rlm@46 407 Object.extend(Array.prototype, Enumerable);
rlm@46 408
rlm@46 409 Array.prototype._reverse = Array.prototype.reverse;
rlm@46 410
rlm@46 411 Object.extend(Array.prototype, {
rlm@46 412 _each: function(iterator) {
rlm@46 413 for (var i = 0; i < this.length; i++)
rlm@46 414 iterator(this[i]);
rlm@46 415 },
rlm@46 416
rlm@46 417 clear: function() {
rlm@46 418 this.length = 0;
rlm@46 419 return this;
rlm@46 420 },
rlm@46 421
rlm@46 422 first: function() {
rlm@46 423 return this[0];
rlm@46 424 },
rlm@46 425
rlm@46 426 last: function() {
rlm@46 427 return this[this.length - 1];
rlm@46 428 },
rlm@46 429
rlm@46 430 compact: function() {
rlm@46 431 return this.select(function(value) {
rlm@46 432 return value != undefined || value != null;
rlm@46 433 });
rlm@46 434 },
rlm@46 435
rlm@46 436 flatten: function() {
rlm@46 437 return this.inject([], function(array, value) {
rlm@46 438 return array.concat(value.constructor == Array ?
rlm@46 439 value.flatten() : [value]);
rlm@46 440 });
rlm@46 441 },
rlm@46 442
rlm@46 443 without: function() {
rlm@46 444 var values = $A(arguments);
rlm@46 445 return this.select(function(value) {
rlm@46 446 return !values.include(value);
rlm@46 447 });
rlm@46 448 },
rlm@46 449
rlm@46 450 indexOf: function(object) {
rlm@46 451 for (var i = 0; i < this.length; i++)
rlm@46 452 if (this[i] == object) return i;
rlm@46 453 return -1;
rlm@46 454 },
rlm@46 455
rlm@46 456 reverse: function(inline) {
rlm@46 457 return (inline !== false ? this : this.toArray())._reverse();
rlm@46 458 },
rlm@46 459
rlm@46 460 shift: function() {
rlm@46 461 var result = this[0];
rlm@46 462 for (var i = 0; i < this.length - 1; i++)
rlm@46 463 this[i] = this[i + 1];
rlm@46 464 this.length--;
rlm@46 465 return result;
rlm@46 466 },
rlm@46 467
rlm@46 468 inspect: function() {
rlm@46 469 return '[' + this.map(Object.inspect).join(', ') + ']';
rlm@46 470 }
rlm@46 471 });
rlm@46 472 var Hash = {
rlm@46 473 _each: function(iterator) {
rlm@46 474 for (key in this) {
rlm@46 475 var value = this[key];
rlm@46 476 if (typeof value == 'function') continue;
rlm@46 477
rlm@46 478 var pair = [key, value];
rlm@46 479 pair.key = key;
rlm@46 480 pair.value = value;
rlm@46 481 iterator(pair);
rlm@46 482 }
rlm@46 483 },
rlm@46 484
rlm@46 485 keys: function() {
rlm@46 486 return this.pluck('key');
rlm@46 487 },
rlm@46 488
rlm@46 489 values: function() {
rlm@46 490 return this.pluck('value');
rlm@46 491 },
rlm@46 492
rlm@46 493 merge: function(hash) {
rlm@46 494 return $H(hash).inject($H(this), function(mergedHash, pair) {
rlm@46 495 mergedHash[pair.key] = pair.value;
rlm@46 496 return mergedHash;
rlm@46 497 });
rlm@46 498 },
rlm@46 499
rlm@46 500 toQueryString: function() {
rlm@46 501 return this.map(function(pair) {
rlm@46 502 return pair.map(encodeURIComponent).join('=');
rlm@46 503 }).join('&');
rlm@46 504 },
rlm@46 505
rlm@46 506 inspect: function() {
rlm@46 507 return '#<Hash:{' + this.map(function(pair) {
rlm@46 508 return pair.map(Object.inspect).join(': ');
rlm@46 509 }).join(', ') + '}>';
rlm@46 510 }
rlm@46 511 }
rlm@46 512
rlm@46 513 function $H(object) {
rlm@46 514 var hash = Object.extend({}, object || {});
rlm@46 515 Object.extend(hash, Enumerable);
rlm@46 516 Object.extend(hash, Hash);
rlm@46 517 return hash;
rlm@46 518 }
rlm@46 519 ObjectRange = Class.create();
rlm@46 520 Object.extend(ObjectRange.prototype, Enumerable);
rlm@46 521 Object.extend(ObjectRange.prototype, {
rlm@46 522 initialize: function(start, end, exclusive) {
rlm@46 523 this.start = start;
rlm@46 524 this.end = end;
rlm@46 525 this.exclusive = exclusive;
rlm@46 526 },
rlm@46 527
rlm@46 528 _each: function(iterator) {
rlm@46 529 var value = this.start;
rlm@46 530 do {
rlm@46 531 iterator(value);
rlm@46 532 value = value.succ();
rlm@46 533 } while (this.include(value));
rlm@46 534 },
rlm@46 535
rlm@46 536 include: function(value) {
rlm@46 537 if (value < this.start)
rlm@46 538 return false;
rlm@46 539 if (this.exclusive)
rlm@46 540 return value < this.end;
rlm@46 541 return value <= this.end;
rlm@46 542 }
rlm@46 543 });
rlm@46 544
rlm@46 545 var $R = function(start, end, exclusive) {
rlm@46 546 return new ObjectRange(start, end, exclusive);
rlm@46 547 }
rlm@46 548
rlm@46 549 var Ajax = {
rlm@46 550 getTransport: function() {
rlm@46 551 return Try.these(
rlm@46 552 function() {return new ActiveXObject('Msxml2.XMLHTTP')},
rlm@46 553 function() {return new ActiveXObject('Microsoft.XMLHTTP')},
rlm@46 554 function() {return new XMLHttpRequest()}
rlm@46 555 ) || false;
rlm@46 556 },
rlm@46 557
rlm@46 558 activeRequestCount: 0
rlm@46 559 }
rlm@46 560
rlm@46 561 Ajax.Responders = {
rlm@46 562 responders: [],
rlm@46 563
rlm@46 564 _each: function(iterator) {
rlm@46 565 this.responders._each(iterator);
rlm@46 566 },
rlm@46 567
rlm@46 568 register: function(responderToAdd) {
rlm@46 569 if (!this.include(responderToAdd))
rlm@46 570 this.responders.push(responderToAdd);
rlm@46 571 },
rlm@46 572
rlm@46 573 unregister: function(responderToRemove) {
rlm@46 574 this.responders = this.responders.without(responderToRemove);
rlm@46 575 },
rlm@46 576
rlm@46 577 dispatch: function(callback, request, transport, json) {
rlm@46 578 this.each(function(responder) {
rlm@46 579 if (responder[callback] && typeof responder[callback] == 'function') {
rlm@46 580 try {
rlm@46 581 responder[callback].apply(responder, [request, transport, json]);
rlm@46 582 } catch (e) {}
rlm@46 583 }
rlm@46 584 });
rlm@46 585 }
rlm@46 586 };
rlm@46 587
rlm@46 588 Object.extend(Ajax.Responders, Enumerable);
rlm@46 589
rlm@46 590 Ajax.Responders.register({
rlm@46 591 onCreate: function() {
rlm@46 592 Ajax.activeRequestCount++;
rlm@46 593 },
rlm@46 594
rlm@46 595 onComplete: function() {
rlm@46 596 Ajax.activeRequestCount--;
rlm@46 597 }
rlm@46 598 });
rlm@46 599
rlm@46 600 Ajax.Base = function() {};
rlm@46 601 Ajax.Base.prototype = {
rlm@46 602 setOptions: function(options) {
rlm@46 603 this.options = {
rlm@46 604 method: 'post',
rlm@46 605 asynchronous: true,
rlm@46 606 parameters: ''
rlm@46 607 }
rlm@46 608 Object.extend(this.options, options || {});
rlm@46 609 },
rlm@46 610
rlm@46 611 responseIsSuccess: function() {
rlm@46 612 return this.transport.status == undefined
rlm@46 613 || this.transport.status == 0
rlm@46 614 || (this.transport.status >= 200 && this.transport.status < 300);
rlm@46 615 },
rlm@46 616
rlm@46 617 responseIsFailure: function() {
rlm@46 618 return !this.responseIsSuccess();
rlm@46 619 }
rlm@46 620 }
rlm@46 621
rlm@46 622 Ajax.Request = Class.create();
rlm@46 623 Ajax.Request.Events =
rlm@46 624 ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
rlm@46 625
rlm@46 626 Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
rlm@46 627 initialize: function(url, options) {
rlm@46 628 this.transport = Ajax.getTransport();
rlm@46 629 this.setOptions(options);
rlm@46 630 this.request(url);
rlm@46 631 },
rlm@46 632
rlm@46 633 request: function(url) {
rlm@46 634 var parameters = this.options.parameters || '';
rlm@46 635 if (parameters.length > 0) parameters += '&_=';
rlm@46 636
rlm@46 637 try {
rlm@46 638 this.url = url;
rlm@46 639 if (this.options.method == 'get' && parameters.length > 0)
rlm@46 640 this.url += (this.url.match(/\?/) ? '&' : '?') + parameters;
rlm@46 641
rlm@46 642 Ajax.Responders.dispatch('onCreate', this, this.transport);
rlm@46 643
rlm@46 644 this.transport.open(this.options.method, this.url,
rlm@46 645 this.options.asynchronous);
rlm@46 646
rlm@46 647 if (this.options.asynchronous) {
rlm@46 648 this.transport.onreadystatechange = this.onStateChange.bind(this);
rlm@46 649 setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
rlm@46 650 }
rlm@46 651
rlm@46 652 this.setRequestHeaders();
rlm@46 653
rlm@46 654 var body = this.options.postBody ? this.options.postBody : parameters;
rlm@46 655 this.transport.send(this.options.method == 'post' ? body : null);
rlm@46 656
rlm@46 657 } catch (e) {
rlm@46 658 this.dispatchException(e);
rlm@46 659 }
rlm@46 660 },
rlm@46 661
rlm@46 662 setRequestHeaders: function() {
rlm@46 663 var requestHeaders =
rlm@46 664 ['X-Requested-With', 'XMLHttpRequest',
rlm@46 665 'X-Prototype-Version', Prototype.Version];
rlm@46 666
rlm@46 667 if (this.options.method == 'post') {
rlm@46 668 requestHeaders.push('Content-type',
rlm@46 669 'application/x-www-form-urlencoded');
rlm@46 670
rlm@46 671 /* Force "Connection: close" for Mozilla browsers to work around
rlm@46 672 * a bug where XMLHttpReqeuest sends an incorrect Content-length
rlm@46 673 * header. See Mozilla Bugzilla #246651.
rlm@46 674 */
rlm@46 675 if (this.transport.overrideMimeType)
rlm@46 676 requestHeaders.push('Connection', 'close');
rlm@46 677 }
rlm@46 678
rlm@46 679 if (this.options.requestHeaders)
rlm@46 680 requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
rlm@46 681
rlm@46 682 for (var i = 0; i < requestHeaders.length; i += 2)
rlm@46 683 this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
rlm@46 684 },
rlm@46 685
rlm@46 686 onStateChange: function() {
rlm@46 687 var readyState = this.transport.readyState;
rlm@46 688 if (readyState != 1)
rlm@46 689 this.respondToReadyState(this.transport.readyState);
rlm@46 690 },
rlm@46 691
rlm@46 692 header: function(name) {
rlm@46 693 try {
rlm@46 694 return this.transport.getResponseHeader(name);
rlm@46 695 } catch (e) {}
rlm@46 696 },
rlm@46 697
rlm@46 698 evalJSON: function() {
rlm@46 699 try {
rlm@46 700 return eval(this.header('X-JSON'));
rlm@46 701 } catch (e) {}
rlm@46 702 },
rlm@46 703
rlm@46 704 evalResponse: function() {
rlm@46 705 try {
rlm@46 706 return eval(this.transport.responseText);
rlm@46 707 } catch (e) {
rlm@46 708 this.dispatchException(e);
rlm@46 709 }
rlm@46 710 },
rlm@46 711
rlm@46 712 respondToReadyState: function(readyState) {
rlm@46 713 var event = Ajax.Request.Events[readyState];
rlm@46 714 var transport = this.transport, json = this.evalJSON();
rlm@46 715
rlm@46 716 if (event == 'Complete') {
rlm@46 717 try {
rlm@46 718 (this.options['on' + this.transport.status]
rlm@46 719 || this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
rlm@46 720 || Prototype.emptyFunction)(transport, json);
rlm@46 721 } catch (e) {
rlm@46 722 this.dispatchException(e);
rlm@46 723 }
rlm@46 724
rlm@46 725 if ((this.header('Content-type') || '').match(/^text\/javascript/i))
rlm@46 726 this.evalResponse();
rlm@46 727 }
rlm@46 728
rlm@46 729 try {
rlm@46 730 (this.options['on' + event] || Prototype.emptyFunction)(transport, json);
rlm@46 731 Ajax.Responders.dispatch('on' + event, this, transport, json);
rlm@46 732 } catch (e) {
rlm@46 733 this.dispatchException(e);
rlm@46 734 }
rlm@46 735
rlm@46 736 /* Avoid memory leak in MSIE: clean up the oncomplete event handler */
rlm@46 737 if (event == 'Complete')
rlm@46 738 this.transport.onreadystatechange = Prototype.emptyFunction;
rlm@46 739 },
rlm@46 740
rlm@46 741 dispatchException: function(exception) {
rlm@46 742 (this.options.onException || Prototype.emptyFunction)(this, exception);
rlm@46 743 Ajax.Responders.dispatch('onException', this, exception);
rlm@46 744 }
rlm@46 745 });
rlm@46 746
rlm@46 747 Ajax.Updater = Class.create();
rlm@46 748
rlm@46 749 Object.extend(Object.extend(Ajax.Updater.prototype, Ajax.Request.prototype), {
rlm@46 750 initialize: function(container, url, options) {
rlm@46 751 this.containers = {
rlm@46 752 success: container.success ? $(container.success) : $(container),
rlm@46 753 failure: container.failure ? $(container.failure) :
rlm@46 754 (container.success ? null : $(container))
rlm@46 755 }
rlm@46 756
rlm@46 757 this.transport = Ajax.getTransport();
rlm@46 758 this.setOptions(options);
rlm@46 759
rlm@46 760 var onComplete = this.options.onComplete || Prototype.emptyFunction;
rlm@46 761 this.options.onComplete = (function(transport, object) {
rlm@46 762 this.updateContent();
rlm@46 763 onComplete(transport, object);
rlm@46 764 }).bind(this);
rlm@46 765
rlm@46 766 this.request(url);
rlm@46 767 },
rlm@46 768
rlm@46 769 updateContent: function() {
rlm@46 770 var receiver = this.responseIsSuccess() ?
rlm@46 771 this.containers.success : this.containers.failure;
rlm@46 772 var response = this.transport.responseText;
rlm@46 773
rlm@46 774 if (!this.options.evalScripts)
rlm@46 775 response = response.stripScripts();
rlm@46 776
rlm@46 777 if (receiver) {
rlm@46 778 if (this.options.insertion) {
rlm@46 779 new this.options.insertion(receiver, response);
rlm@46 780 } else {
rlm@46 781 Element.update(receiver, response);
rlm@46 782 }
rlm@46 783 }
rlm@46 784
rlm@46 785 if (this.responseIsSuccess()) {
rlm@46 786 if (this.onComplete)
rlm@46 787 setTimeout(this.onComplete.bind(this), 10);
rlm@46 788 }
rlm@46 789 }
rlm@46 790 });
rlm@46 791
rlm@46 792 Ajax.PeriodicalUpdater = Class.create();
rlm@46 793 Ajax.PeriodicalUpdater.prototype = Object.extend(new Ajax.Base(), {
rlm@46 794 initialize: function(container, url, options) {
rlm@46 795 this.setOptions(options);
rlm@46 796 this.onComplete = this.options.onComplete;
rlm@46 797
rlm@46 798 this.frequency = (this.options.frequency || 2);
rlm@46 799 this.decay = (this.options.decay || 1);
rlm@46 800
rlm@46 801 this.updater = {};
rlm@46 802 this.container = container;
rlm@46 803 this.url = url;
rlm@46 804
rlm@46 805 this.start();
rlm@46 806 },
rlm@46 807
rlm@46 808 start: function() {
rlm@46 809 this.options.onComplete = this.updateComplete.bind(this);
rlm@46 810 this.onTimerEvent();
rlm@46 811 },
rlm@46 812
rlm@46 813 stop: function() {
rlm@46 814 this.updater.onComplete = undefined;
rlm@46 815 clearTimeout(this.timer);
rlm@46 816 (this.onComplete || Prototype.emptyFunction).apply(this, arguments);
rlm@46 817 },
rlm@46 818
rlm@46 819 updateComplete: function(request) {
rlm@46 820 if (this.options.decay) {
rlm@46 821 this.decay = (request.responseText == this.lastText ?
rlm@46 822 this.decay * this.options.decay : 1);
rlm@46 823
rlm@46 824 this.lastText = request.responseText;
rlm@46 825 }
rlm@46 826 this.timer = setTimeout(this.onTimerEvent.bind(this),
rlm@46 827 this.decay * this.frequency * 1000);
rlm@46 828 },
rlm@46 829
rlm@46 830 onTimerEvent: function() {
rlm@46 831 this.updater = new Ajax.Updater(this.container, this.url, this.options);
rlm@46 832 }
rlm@46 833 });
rlm@46 834 document.getElementsByClassName = function(className, parentElement) {
rlm@46 835 var children = ($(parentElement) || document.body).getElementsByTagName('*');
rlm@46 836 return $A(children).inject([], function(elements, child) {
rlm@46 837 if (child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
rlm@46 838 elements.push(child);
rlm@46 839 return elements;
rlm@46 840 });
rlm@46 841 }
rlm@46 842
rlm@46 843 /*--------------------------------------------------------------------------*/
rlm@46 844
rlm@46 845 if (!window.Element) {
rlm@46 846 var Element = new Object();
rlm@46 847 }
rlm@46 848
rlm@46 849 Object.extend(Element, {
rlm@46 850 visible: function(element) {
rlm@46 851 return $(element).style.display != 'none';
rlm@46 852 },
rlm@46 853
rlm@46 854 toggle: function() {
rlm@46 855 for (var i = 0; i < arguments.length; i++) {
rlm@46 856 var element = $(arguments[i]);
rlm@46 857 Element[Element.visible(element) ? 'hide' : 'show'](element);
rlm@46 858 }
rlm@46 859 },
rlm@46 860
rlm@46 861 hide: function() {
rlm@46 862 for (var i = 0; i < arguments.length; i++) {
rlm@46 863 var element = $(arguments[i]);
rlm@46 864 element.style.display = 'none';
rlm@46 865 }
rlm@46 866 },
rlm@46 867
rlm@46 868 show: function() {
rlm@46 869 for (var i = 0; i < arguments.length; i++) {
rlm@46 870 var element = $(arguments[i]);
rlm@46 871 element.style.display = '';
rlm@46 872 }
rlm@46 873 },
rlm@46 874
rlm@46 875 remove: function(element) {
rlm@46 876 element = $(element);
rlm@46 877 element.parentNode.removeChild(element);
rlm@46 878 },
rlm@46 879
rlm@46 880 update: function(element, html) {
rlm@46 881 $(element).innerHTML = html.stripScripts();
rlm@46 882 setTimeout(function() {html.evalScripts()}, 10);
rlm@46 883 },
rlm@46 884
rlm@46 885 getHeight: function(element) {
rlm@46 886 element = $(element);
rlm@46 887 return element.offsetHeight;
rlm@46 888 },
rlm@46 889
rlm@46 890 classNames: function(element) {
rlm@46 891 return new Element.ClassNames(element);
rlm@46 892 },
rlm@46 893
rlm@46 894 hasClassName: function(element, className) {
rlm@46 895 if (!(element = $(element))) return;
rlm@46 896 return Element.classNames(element).include(className);
rlm@46 897 },
rlm@46 898
rlm@46 899 addClassName: function(element, className) {
rlm@46 900 if (!(element = $(element))) return;
rlm@46 901 return Element.classNames(element).add(className);
rlm@46 902 },
rlm@46 903
rlm@46 904 removeClassName: function(element, className) {
rlm@46 905 if (!(element = $(element))) return;
rlm@46 906 return Element.classNames(element).remove(className);
rlm@46 907 },
rlm@46 908
rlm@46 909 // removes whitespace-only text node children
rlm@46 910 cleanWhitespace: function(element) {
rlm@46 911 element = $(element);
rlm@46 912 for (var i = 0; i < element.childNodes.length; i++) {
rlm@46 913 var node = element.childNodes[i];
rlm@46 914 if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
rlm@46 915 Element.remove(node);
rlm@46 916 }
rlm@46 917 },
rlm@46 918
rlm@46 919 empty: function(element) {
rlm@46 920 return $(element).innerHTML.match(/^\s*$/);
rlm@46 921 },
rlm@46 922
rlm@46 923 scrollTo: function(element) {
rlm@46 924 element = $(element);
rlm@46 925 var x = element.x ? element.x : element.offsetLeft,
rlm@46 926 y = element.y ? element.y : element.offsetTop;
rlm@46 927 window.scrollTo(x, y);
rlm@46 928 },
rlm@46 929
rlm@46 930 getStyle: function(element, style) {
rlm@46 931 element = $(element);
rlm@46 932 var value = element.style[style.camelize()];
rlm@46 933 if (!value) {
rlm@46 934 if (document.defaultView && document.defaultView.getComputedStyle) {
rlm@46 935 var css = document.defaultView.getComputedStyle(element, null);
rlm@46 936 value = css ? css.getPropertyValue(style) : null;
rlm@46 937 } else if (element.currentStyle) {
rlm@46 938 value = element.currentStyle[style.camelize()];
rlm@46 939 }
rlm@46 940 }
rlm@46 941
rlm@46 942 if (window.opera && ['left', 'top', 'right', 'bottom'].include(style))
rlm@46 943 if (Element.getStyle(element, 'position') == 'static') value = 'auto';
rlm@46 944
rlm@46 945 return value == 'auto' ? null : value;
rlm@46 946 },
rlm@46 947
rlm@46 948 setStyle: function(element, style) {
rlm@46 949 element = $(element);
rlm@46 950 for (name in style)
rlm@46 951 element.style[name.camelize()] = style[name];
rlm@46 952 },
rlm@46 953
rlm@46 954 getDimensions: function(element) {
rlm@46 955 element = $(element);
rlm@46 956 if (Element.getStyle(element, 'display') != 'none')
rlm@46 957 return {width: element.offsetWidth, height: element.offsetHeight};
rlm@46 958
rlm@46 959 // All *Width and *Height properties give 0 on elements with display none,
rlm@46 960 // so enable the element temporarily
rlm@46 961 var els = element.style;
rlm@46 962 var originalVisibility = els.visibility;
rlm@46 963 var originalPosition = els.position;
rlm@46 964 els.visibility = 'hidden';
rlm@46 965 els.position = 'absolute';
rlm@46 966 els.display = '';
rlm@46 967 var originalWidth = element.clientWidth;
rlm@46 968 var originalHeight = element.clientHeight;
rlm@46 969 els.display = 'none';
rlm@46 970 els.position = originalPosition;
rlm@46 971 els.visibility = originalVisibility;
rlm@46 972 return {width: originalWidth, height: originalHeight};
rlm@46 973 },
rlm@46 974
rlm@46 975 makePositioned: function(element) {
rlm@46 976 element = $(element);
rlm@46 977 var pos = Element.getStyle(element, 'position');
rlm@46 978 if (pos == 'static' || !pos) {
rlm@46 979 element._madePositioned = true;
rlm@46 980 element.style.position = 'relative';
rlm@46 981 // Opera returns the offset relative to the positioning context, when an
rlm@46 982 // element is position relative but top and left have not been defined
rlm@46 983 if (window.opera) {
rlm@46 984 element.style.top = 0;
rlm@46 985 element.style.left = 0;
rlm@46 986 }
rlm@46 987 }
rlm@46 988 },
rlm@46 989
rlm@46 990 undoPositioned: function(element) {
rlm@46 991 element = $(element);
rlm@46 992 if (element._madePositioned) {
rlm@46 993 element._madePositioned = undefined;
rlm@46 994 element.style.position =
rlm@46 995 element.style.top =
rlm@46 996 element.style.left =
rlm@46 997 element.style.bottom =
rlm@46 998 element.style.right = '';
rlm@46 999 }
rlm@46 1000 },
rlm@46 1001
rlm@46 1002 makeClipping: function(element) {
rlm@46 1003 element = $(element);
rlm@46 1004 if (element._overflow) return;
rlm@46 1005 element._overflow = element.style.overflow;
rlm@46 1006 if ((Element.getStyle(element, 'overflow') || 'visible') != 'hidden')
rlm@46 1007 element.style.overflow = 'hidden';
rlm@46 1008 },
rlm@46 1009
rlm@46 1010 undoClipping: function(element) {
rlm@46 1011 element = $(element);
rlm@46 1012 if (element._overflow) return;
rlm@46 1013 element.style.overflow = element._overflow;
rlm@46 1014 element._overflow = undefined;
rlm@46 1015 }
rlm@46 1016 });
rlm@46 1017
rlm@46 1018 var Toggle = new Object();
rlm@46 1019 Toggle.display = Element.toggle;
rlm@46 1020
rlm@46 1021 /*--------------------------------------------------------------------------*/
rlm@46 1022
rlm@46 1023 Abstract.Insertion = function(adjacency) {
rlm@46 1024 this.adjacency = adjacency;
rlm@46 1025 }
rlm@46 1026
rlm@46 1027 Abstract.Insertion.prototype = {
rlm@46 1028 initialize: function(element, content) {
rlm@46 1029 this.element = $(element);
rlm@46 1030 this.content = content.stripScripts();
rlm@46 1031
rlm@46 1032 if (this.adjacency && this.element.insertAdjacentHTML) {
rlm@46 1033 try {
rlm@46 1034 this.element.insertAdjacentHTML(this.adjacency, this.content);
rlm@46 1035 } catch (e) {
rlm@46 1036 if (this.element.tagName.toLowerCase() == 'tbody') {
rlm@46 1037 this.insertContent(this.contentFromAnonymousTable());
rlm@46 1038 } else {
rlm@46 1039 throw e;
rlm@46 1040 }
rlm@46 1041 }
rlm@46 1042 } else {
rlm@46 1043 this.range = this.element.ownerDocument.createRange();
rlm@46 1044 if (this.initializeRange) this.initializeRange();
rlm@46 1045 this.insertContent([this.range.createContextualFragment(this.content)]);
rlm@46 1046 }
rlm@46 1047
rlm@46 1048 setTimeout(function() {content.evalScripts()}, 10);
rlm@46 1049 },
rlm@46 1050
rlm@46 1051 contentFromAnonymousTable: function() {
rlm@46 1052 var div = document.createElement('div');
rlm@46 1053 div.innerHTML = '<table><tbody>' + this.content + '</tbody></table>';
rlm@46 1054 return $A(div.childNodes[0].childNodes[0].childNodes);
rlm@46 1055 }
rlm@46 1056 }
rlm@46 1057
rlm@46 1058 var Insertion = new Object();
rlm@46 1059
rlm@46 1060 Insertion.Before = Class.create();
rlm@46 1061 Insertion.Before.prototype = Object.extend(new Abstract.Insertion('beforeBegin'), {
rlm@46 1062 initializeRange: function() {
rlm@46 1063 this.range.setStartBefore(this.element);
rlm@46 1064 },
rlm@46 1065
rlm@46 1066 insertContent: function(fragments) {
rlm@46 1067 fragments.each((function(fragment) {
rlm@46 1068 this.element.parentNode.insertBefore(fragment, this.element);
rlm@46 1069 }).bind(this));
rlm@46 1070 }
rlm@46 1071 });
rlm@46 1072
rlm@46 1073 Insertion.Top = Class.create();
rlm@46 1074 Insertion.Top.prototype = Object.extend(new Abstract.Insertion('afterBegin'), {
rlm@46 1075 initializeRange: function() {
rlm@46 1076 this.range.selectNodeContents(this.element);
rlm@46 1077 this.range.collapse(true);
rlm@46 1078 },
rlm@46 1079
rlm@46 1080 insertContent: function(fragments) {
rlm@46 1081 fragments.reverse(false).each((function(fragment) {
rlm@46 1082 this.element.insertBefore(fragment, this.element.firstChild);
rlm@46 1083 }).bind(this));
rlm@46 1084 }
rlm@46 1085 });
rlm@46 1086
rlm@46 1087 Insertion.Bottom = Class.create();
rlm@46 1088 Insertion.Bottom.prototype = Object.extend(new Abstract.Insertion('beforeEnd'), {
rlm@46 1089 initializeRange: function() {
rlm@46 1090 this.range.selectNodeContents(this.element);
rlm@46 1091 this.range.collapse(this.element);
rlm@46 1092 },
rlm@46 1093
rlm@46 1094 insertContent: function(fragments) {
rlm@46 1095 fragments.each((function(fragment) {
rlm@46 1096 this.element.appendChild(fragment);
rlm@46 1097 }).bind(this));
rlm@46 1098 }
rlm@46 1099 });
rlm@46 1100
rlm@46 1101 Insertion.After = Class.create();
rlm@46 1102 Insertion.After.prototype = Object.extend(new Abstract.Insertion('afterEnd'), {
rlm@46 1103 initializeRange: function() {
rlm@46 1104 this.range.setStartAfter(this.element);
rlm@46 1105 },
rlm@46 1106
rlm@46 1107 insertContent: function(fragments) {
rlm@46 1108 fragments.each((function(fragment) {
rlm@46 1109 this.element.parentNode.insertBefore(fragment,
rlm@46 1110 this.element.nextSibling);
rlm@46 1111 }).bind(this));
rlm@46 1112 }
rlm@46 1113 });
rlm@46 1114
rlm@46 1115 /*--------------------------------------------------------------------------*/
rlm@46 1116
rlm@46 1117 Element.ClassNames = Class.create();
rlm@46 1118 Element.ClassNames.prototype = {
rlm@46 1119 initialize: function(element) {
rlm@46 1120 this.element = $(element);
rlm@46 1121 },
rlm@46 1122
rlm@46 1123 _each: function(iterator) {
rlm@46 1124 this.element.className.split(/\s+/).select(function(name) {
rlm@46 1125 return name.length > 0;
rlm@46 1126 })._each(iterator);
rlm@46 1127 },
rlm@46 1128
rlm@46 1129 set: function(className) {
rlm@46 1130 this.element.className = className;
rlm@46 1131 },
rlm@46 1132
rlm@46 1133 add: function(classNameToAdd) {
rlm@46 1134 if (this.include(classNameToAdd)) return;
rlm@46 1135 this.set(this.toArray().concat(classNameToAdd).join(' '));
rlm@46 1136 },
rlm@46 1137
rlm@46 1138 remove: function(classNameToRemove) {
rlm@46 1139 if (!this.include(classNameToRemove)) return;
rlm@46 1140 this.set(this.select(function(className) {
rlm@46 1141 return className != classNameToRemove;
rlm@46 1142 }).join(' '));
rlm@46 1143 },
rlm@46 1144
rlm@46 1145 toString: function() {
rlm@46 1146 return this.toArray().join(' ');
rlm@46 1147 }
rlm@46 1148 }
rlm@46 1149
rlm@46 1150 Object.extend(Element.ClassNames.prototype, Enumerable);
rlm@46 1151 var Field = {
rlm@46 1152 clear: function() {
rlm@46 1153 for (var i = 0; i < arguments.length; i++)
rlm@46 1154 $(arguments[i]).value = '';
rlm@46 1155 },
rlm@46 1156
rlm@46 1157 focus: function(element) {
rlm@46 1158 $(element).focus();
rlm@46 1159 },
rlm@46 1160
rlm@46 1161 present: function() {
rlm@46 1162 for (var i = 0; i < arguments.length; i++)
rlm@46 1163 if ($(arguments[i]).value == '') return false;
rlm@46 1164 return true;
rlm@46 1165 },
rlm@46 1166
rlm@46 1167 select: function(element) {
rlm@46 1168 $(element).select();
rlm@46 1169 },
rlm@46 1170
rlm@46 1171 activate: function(element) {
rlm@46 1172 element = $(element);
rlm@46 1173 element.focus();
rlm@46 1174 if (element.select)
rlm@46 1175 element.select();
rlm@46 1176 }
rlm@46 1177 }
rlm@46 1178
rlm@46 1179 /*--------------------------------------------------------------------------*/
rlm@46 1180
rlm@46 1181 var Form = {
rlm@46 1182 serialize: function(form) {
rlm@46 1183 var elements = Form.getElements($(form));
rlm@46 1184 var queryComponents = new Array();
rlm@46 1185
rlm@46 1186 for (var i = 0; i < elements.length; i++) {
rlm@46 1187 var queryComponent = Form.Element.serialize(elements[i]);
rlm@46 1188 if (queryComponent)
rlm@46 1189 queryComponents.push(queryComponent);
rlm@46 1190 }
rlm@46 1191
rlm@46 1192 return queryComponents.join('&');
rlm@46 1193 },
rlm@46 1194
rlm@46 1195 getElements: function(form) {
rlm@46 1196 form = $(form);
rlm@46 1197 var elements = new Array();
rlm@46 1198
rlm@46 1199 for (tagName in Form.Element.Serializers) {
rlm@46 1200 var tagElements = form.getElementsByTagName(tagName);
rlm@46 1201 for (var j = 0; j < tagElements.length; j++)
rlm@46 1202 elements.push(tagElements[j]);
rlm@46 1203 }
rlm@46 1204 return elements;
rlm@46 1205 },
rlm@46 1206
rlm@46 1207 getInputs: function(form, typeName, name) {
rlm@46 1208 form = $(form);
rlm@46 1209 var inputs = form.getElementsByTagName('input');
rlm@46 1210
rlm@46 1211 if (!typeName && !name)
rlm@46 1212 return inputs;
rlm@46 1213
rlm@46 1214 var matchingInputs = new Array();
rlm@46 1215 for (var i = 0; i < inputs.length; i++) {
rlm@46 1216 var input = inputs[i];
rlm@46 1217 if ((typeName && input.type != typeName) ||
rlm@46 1218 (name && input.name != name))
rlm@46 1219 continue;
rlm@46 1220 matchingInputs.push(input);
rlm@46 1221 }
rlm@46 1222
rlm@46 1223 return matchingInputs;
rlm@46 1224 },
rlm@46 1225
rlm@46 1226 disable: function(form) {
rlm@46 1227 var elements = Form.getElements(form);
rlm@46 1228 for (var i = 0; i < elements.length; i++) {
rlm@46 1229 var element = elements[i];
rlm@46 1230 element.blur();
rlm@46 1231 element.disabled = 'true';
rlm@46 1232 }
rlm@46 1233 },
rlm@46 1234
rlm@46 1235 enable: function(form) {
rlm@46 1236 var elements = Form.getElements(form);
rlm@46 1237 for (var i = 0; i < elements.length; i++) {
rlm@46 1238 var element = elements[i];
rlm@46 1239 element.disabled = '';
rlm@46 1240 }
rlm@46 1241 },
rlm@46 1242
rlm@46 1243 findFirstElement: function(form) {
rlm@46 1244 return Form.getElements(form).find(function(element) {
rlm@46 1245 return element.type != 'hidden' && !element.disabled &&
rlm@46 1246 ['input', 'select', 'textarea'].include(element.tagName.toLowerCase());
rlm@46 1247 });
rlm@46 1248 },
rlm@46 1249
rlm@46 1250 focusFirstElement: function(form) {
rlm@46 1251 Field.activate(Form.findFirstElement(form));
rlm@46 1252 },
rlm@46 1253
rlm@46 1254 reset: function(form) {
rlm@46 1255 $(form).reset();
rlm@46 1256 }
rlm@46 1257 }
rlm@46 1258
rlm@46 1259 Form.Element = {
rlm@46 1260 serialize: function(element) {
rlm@46 1261 element = $(element);
rlm@46 1262 var method = element.tagName.toLowerCase();
rlm@46 1263 var parameter = Form.Element.Serializers[method](element);
rlm@46 1264
rlm@46 1265 if (parameter) {
rlm@46 1266 var key = encodeURIComponent(parameter[0]);
rlm@46 1267 if (key.length == 0) return;
rlm@46 1268
rlm@46 1269 if (parameter[1].constructor != Array)
rlm@46 1270 parameter[1] = [parameter[1]];
rlm@46 1271
rlm@46 1272 return parameter[1].map(function(value) {
rlm@46 1273 return key + '=' + encodeURIComponent(value);
rlm@46 1274 }).join('&');
rlm@46 1275 }
rlm@46 1276 },
rlm@46 1277
rlm@46 1278 getValue: function(element) {
rlm@46 1279 element = $(element);
rlm@46 1280 var method = element.tagName.toLowerCase();
rlm@46 1281 var parameter = Form.Element.Serializers[method](element);
rlm@46 1282
rlm@46 1283 if (parameter)
rlm@46 1284 return parameter[1];
rlm@46 1285 }
rlm@46 1286 }
rlm@46 1287
rlm@46 1288 Form.Element.Serializers = {
rlm@46 1289 input: function(element) {
rlm@46 1290 switch (element.type.toLowerCase()) {
rlm@46 1291 case 'submit':
rlm@46 1292 case 'hidden':
rlm@46 1293 case 'password':
rlm@46 1294 case 'text':
rlm@46 1295 return Form.Element.Serializers.textarea(element);
rlm@46 1296 case 'checkbox':
rlm@46 1297 case 'radio':
rlm@46 1298 return Form.Element.Serializers.inputSelector(element);
rlm@46 1299 }
rlm@46 1300 return false;
rlm@46 1301 },
rlm@46 1302
rlm@46 1303 inputSelector: function(element) {
rlm@46 1304 if (element.checked)
rlm@46 1305 return [element.name, element.value];
rlm@46 1306 },
rlm@46 1307
rlm@46 1308 textarea: function(element) {
rlm@46 1309 return [element.name, element.value];
rlm@46 1310 },
rlm@46 1311
rlm@46 1312 select: function(element) {
rlm@46 1313 return Form.Element.Serializers[element.type == 'select-one' ?
rlm@46 1314 'selectOne' : 'selectMany'](element);
rlm@46 1315 },
rlm@46 1316
rlm@46 1317 selectOne: function(element) {
rlm@46 1318 var value = '', opt, index = element.selectedIndex;
rlm@46 1319 if (index >= 0) {
rlm@46 1320 opt = element.options[index];
rlm@46 1321 value = opt.value;
rlm@46 1322 if (!value && !('value' in opt))
rlm@46 1323 value = opt.text;
rlm@46 1324 }
rlm@46 1325 return [element.name, value];
rlm@46 1326 },
rlm@46 1327
rlm@46 1328 selectMany: function(element) {
rlm@46 1329 var value = new Array();
rlm@46 1330 for (var i = 0; i < element.length; i++) {
rlm@46 1331 var opt = element.options[i];
rlm@46 1332 if (opt.selected) {
rlm@46 1333 var optValue = opt.value;
rlm@46 1334 if (!optValue && !('value' in opt))
rlm@46 1335 optValue = opt.text;
rlm@46 1336 value.push(optValue);
rlm@46 1337 }
rlm@46 1338 }
rlm@46 1339 return [element.name, value];
rlm@46 1340 }
rlm@46 1341 }
rlm@46 1342
rlm@46 1343 /*--------------------------------------------------------------------------*/
rlm@46 1344
rlm@46 1345 var $F = Form.Element.getValue;
rlm@46 1346
rlm@46 1347 /*--------------------------------------------------------------------------*/
rlm@46 1348
rlm@46 1349 Abstract.TimedObserver = function() {}
rlm@46 1350 Abstract.TimedObserver.prototype = {
rlm@46 1351 initialize: function(element, frequency, callback) {
rlm@46 1352 this.frequency = frequency;
rlm@46 1353 this.element = $(element);
rlm@46 1354 this.callback = callback;
rlm@46 1355
rlm@46 1356 this.lastValue = this.getValue();
rlm@46 1357 this.registerCallback();
rlm@46 1358 },
rlm@46 1359
rlm@46 1360 registerCallback: function() {
rlm@46 1361 setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
rlm@46 1362 },
rlm@46 1363
rlm@46 1364 onTimerEvent: function() {
rlm@46 1365 var value = this.getValue();
rlm@46 1366 if (this.lastValue != value) {
rlm@46 1367 this.callback(this.element, value);
rlm@46 1368 this.lastValue = value;
rlm@46 1369 }
rlm@46 1370 }
rlm@46 1371 }
rlm@46 1372
rlm@46 1373 Form.Element.Observer = Class.create();
rlm@46 1374 Form.Element.Observer.prototype = Object.extend(new Abstract.TimedObserver(), {
rlm@46 1375 getValue: function() {
rlm@46 1376 return Form.Element.getValue(this.element);
rlm@46 1377 }
rlm@46 1378 });
rlm@46 1379
rlm@46 1380 Form.Observer = Class.create();
rlm@46 1381 Form.Observer.prototype = Object.extend(new Abstract.TimedObserver(), {
rlm@46 1382 getValue: function() {
rlm@46 1383 return Form.serialize(this.element);
rlm@46 1384 }
rlm@46 1385 });
rlm@46 1386
rlm@46 1387 /*--------------------------------------------------------------------------*/
rlm@46 1388
rlm@46 1389 Abstract.EventObserver = function() {}
rlm@46 1390 Abstract.EventObserver.prototype = {
rlm@46 1391 initialize: function(element, callback) {
rlm@46 1392 this.element = $(element);
rlm@46 1393 this.callback = callback;
rlm@46 1394
rlm@46 1395 this.lastValue = this.getValue();
rlm@46 1396 if (this.element.tagName.toLowerCase() == 'form')
rlm@46 1397 this.registerFormCallbacks();
rlm@46 1398 else
rlm@46 1399 this.registerCallback(this.element);
rlm@46 1400 },
rlm@46 1401
rlm@46 1402 onElementEvent: function() {
rlm@46 1403 var value = this.getValue();
rlm@46 1404 if (this.lastValue != value) {
rlm@46 1405 this.callback(this.element, value);
rlm@46 1406 this.lastValue = value;
rlm@46 1407 }
rlm@46 1408 },
rlm@46 1409
rlm@46 1410 registerFormCallbacks: function() {
rlm@46 1411 var elements = Form.getElements(this.element);
rlm@46 1412 for (var i = 0; i < elements.length; i++)
rlm@46 1413 this.registerCallback(elements[i]);
rlm@46 1414 },
rlm@46 1415
rlm@46 1416 registerCallback: function(element) {
rlm@46 1417 if (element.type) {
rlm@46 1418 switch (element.type.toLowerCase()) {
rlm@46 1419 case 'checkbox':
rlm@46 1420 case 'radio':
rlm@46 1421 Event.observe(element, 'click', this.onElementEvent.bind(this));
rlm@46 1422 break;
rlm@46 1423 case 'password':
rlm@46 1424 case 'text':
rlm@46 1425 case 'textarea':
rlm@46 1426 case 'select-one':
rlm@46 1427 case 'select-multiple':
rlm@46 1428 Event.observe(element, 'change', this.onElementEvent.bind(this));
rlm@46 1429 break;
rlm@46 1430 }
rlm@46 1431 }
rlm@46 1432 }
rlm@46 1433 }
rlm@46 1434
rlm@46 1435 Form.Element.EventObserver = Class.create();
rlm@46 1436 Form.Element.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), {
rlm@46 1437 getValue: function() {
rlm@46 1438 return Form.Element.getValue(this.element);
rlm@46 1439 }
rlm@46 1440 });
rlm@46 1441
rlm@46 1442 Form.EventObserver = Class.create();
rlm@46 1443 Form.EventObserver.prototype = Object.extend(new Abstract.EventObserver(), {
rlm@46 1444 getValue: function() {
rlm@46 1445 return Form.serialize(this.element);
rlm@46 1446 }
rlm@46 1447 });
rlm@46 1448 if (!window.Event) {
rlm@46 1449 var Event = new Object();
rlm@46 1450 }
rlm@46 1451
rlm@46 1452 Object.extend(Event, {
rlm@46 1453 KEY_BACKSPACE: 8,
rlm@46 1454 KEY_TAB: 9,
rlm@46 1455 KEY_RETURN: 13,
rlm@46 1456 KEY_ESC: 27,
rlm@46 1457 KEY_LEFT: 37,
rlm@46 1458 KEY_UP: 38,
rlm@46 1459 KEY_RIGHT: 39,
rlm@46 1460 KEY_DOWN: 40,
rlm@46 1461 KEY_DELETE: 46,
rlm@46 1462
rlm@46 1463 element: function(event) {
rlm@46 1464 return event.target || event.srcElement;
rlm@46 1465 },
rlm@46 1466
rlm@46 1467 isLeftClick: function(event) {
rlm@46 1468 return (((event.which) && (event.which == 1)) ||
rlm@46 1469 ((event.button) && (event.button == 1)));
rlm@46 1470 },
rlm@46 1471
rlm@46 1472 pointerX: function(event) {
rlm@46 1473 return event.pageX || (event.clientX +
rlm@46 1474 (document.documentElement.scrollLeft || document.body.scrollLeft));
rlm@46 1475 },
rlm@46 1476
rlm@46 1477 pointerY: function(event) {
rlm@46 1478 return event.pageY || (event.clientY +
rlm@46 1479 (document.documentElement.scrollTop || document.body.scrollTop));
rlm@46 1480 },
rlm@46 1481
rlm@46 1482 stop: function(event) {
rlm@46 1483 if (event.preventDefault) {
rlm@46 1484 event.preventDefault();
rlm@46 1485 event.stopPropagation();
rlm@46 1486 } else {
rlm@46 1487 event.returnValue = false;
rlm@46 1488 event.cancelBubble = true;
rlm@46 1489 }
rlm@46 1490 },
rlm@46 1491
rlm@46 1492 // find the first node with the given tagName, starting from the
rlm@46 1493 // node the event was triggered on; traverses the DOM upwards
rlm@46 1494 findElement: function(event, tagName) {
rlm@46 1495 var element = Event.element(event);
rlm@46 1496 while (element.parentNode && (!element.tagName ||
rlm@46 1497 (element.tagName.toUpperCase() != tagName.toUpperCase())))
rlm@46 1498 element = element.parentNode;
rlm@46 1499 return element;
rlm@46 1500 },
rlm@46 1501
rlm@46 1502 observers: false,
rlm@46 1503
rlm@46 1504 _observeAndCache: function(element, name, observer, useCapture) {
rlm@46 1505 if (!this.observers) this.observers = [];
rlm@46 1506 if (element.addEventListener) {
rlm@46 1507 this.observers.push([element, name, observer, useCapture]);
rlm@46 1508 element.addEventListener(name, observer, useCapture);
rlm@46 1509 } else if (element.attachEvent) {
rlm@46 1510 this.observers.push([element, name, observer, useCapture]);
rlm@46 1511 element.attachEvent('on' + name, observer);
rlm@46 1512 }
rlm@46 1513 },
rlm@46 1514
rlm@46 1515 unloadCache: function() {
rlm@46 1516 if (!Event.observers) return;
rlm@46 1517 for (var i = 0; i < Event.observers.length; i++) {
rlm@46 1518 Event.stopObserving.apply(this, Event.observers[i]);
rlm@46 1519 Event.observers[i][0] = null;
rlm@46 1520 }
rlm@46 1521 Event.observers = false;
rlm@46 1522 },
rlm@46 1523
rlm@46 1524 observe: function(element, name, observer, useCapture) {
rlm@46 1525 var element = $(element);
rlm@46 1526 useCapture = useCapture || false;
rlm@46 1527
rlm@46 1528 if (name == 'keypress' &&
rlm@46 1529 (navigator.appVersion.match(/Konqueror|Safari|KHTML/)
rlm@46 1530 || element.attachEvent))
rlm@46 1531 name = 'keydown';
rlm@46 1532
rlm@46 1533 this._observeAndCache(element, name, observer, useCapture);
rlm@46 1534 },
rlm@46 1535
rlm@46 1536 stopObserving: function(element, name, observer, useCapture) {
rlm@46 1537 var element = $(element);
rlm@46 1538 useCapture = useCapture || false;
rlm@46 1539
rlm@46 1540 if (name == 'keypress' &&
rlm@46 1541 (navigator.appVersion.match(/Konqueror|Safari|KHTML/)
rlm@46 1542 || element.detachEvent))
rlm@46 1543 name = 'keydown';
rlm@46 1544
rlm@46 1545 if (element.removeEventListener) {
rlm@46 1546 element.removeEventListener(name, observer, useCapture);
rlm@46 1547 } else if (element.detachEvent) {
rlm@46 1548 element.detachEvent('on' + name, observer);
rlm@46 1549 }
rlm@46 1550 }
rlm@46 1551 });
rlm@46 1552
rlm@46 1553 /* prevent memory leaks in IE */
rlm@46 1554 Event.observe(window, 'unload', Event.unloadCache, false);
rlm@46 1555 var Position = {
rlm@46 1556 // set to true if needed, warning: firefox performance problems
rlm@46 1557 // NOT neeeded for page scrolling, only if draggable contained in
rlm@46 1558 // scrollable elements
rlm@46 1559 includeScrollOffsets: false,
rlm@46 1560
rlm@46 1561 // must be called before calling withinIncludingScrolloffset, every time the
rlm@46 1562 // page is scrolled
rlm@46 1563 prepare: function() {
rlm@46 1564 this.deltaX = window.pageXOffset
rlm@46 1565 || document.documentElement.scrollLeft
rlm@46 1566 || document.body.scrollLeft
rlm@46 1567 || 0;
rlm@46 1568 this.deltaY = window.pageYOffset
rlm@46 1569 || document.documentElement.scrollTop
rlm@46 1570 || document.body.scrollTop
rlm@46 1571 || 0;
rlm@46 1572 },
rlm@46 1573
rlm@46 1574 realOffset: function(element) {
rlm@46 1575 var valueT = 0, valueL = 0;
rlm@46 1576 do {
rlm@46 1577 valueT += element.scrollTop || 0;
rlm@46 1578 valueL += element.scrollLeft || 0;
rlm@46 1579 element = element.parentNode;
rlm@46 1580 } while (element);
rlm@46 1581 return [valueL, valueT];
rlm@46 1582 },
rlm@46 1583
rlm@46 1584 cumulativeOffset: function(element) {
rlm@46 1585 var valueT = 0, valueL = 0;
rlm@46 1586 do {
rlm@46 1587 valueT += element.offsetTop || 0;
rlm@46 1588 valueL += element.offsetLeft || 0;
rlm@46 1589 element = element.offsetParent;
rlm@46 1590 } while (element);
rlm@46 1591 return [valueL, valueT];
rlm@46 1592 },
rlm@46 1593
rlm@46 1594 positionedOffset: function(element) {
rlm@46 1595 var valueT = 0, valueL = 0;
rlm@46 1596 do {
rlm@46 1597 valueT += element.offsetTop || 0;
rlm@46 1598 valueL += element.offsetLeft || 0;
rlm@46 1599 element = element.offsetParent;
rlm@46 1600 if (element) {
rlm@46 1601 p = Element.getStyle(element, 'position');
rlm@46 1602 if (p == 'relative' || p == 'absolute') break;
rlm@46 1603 }
rlm@46 1604 } while (element);
rlm@46 1605 return [valueL, valueT];
rlm@46 1606 },
rlm@46 1607
rlm@46 1608 offsetParent: function(element) {
rlm@46 1609 if (element.offsetParent) return element.offsetParent;
rlm@46 1610 if (element == document.body) return element;
rlm@46 1611
rlm@46 1612 while ((element = element.parentNode) && element != document.body)
rlm@46 1613 if (Element.getStyle(element, 'position') != 'static')
rlm@46 1614 return element;
rlm@46 1615
rlm@46 1616 return document.body;
rlm@46 1617 },
rlm@46 1618
rlm@46 1619 // caches x/y coordinate pair to use with overlap
rlm@46 1620 within: function(element, x, y) {
rlm@46 1621 if (this.includeScrollOffsets)
rlm@46 1622 return this.withinIncludingScrolloffsets(element, x, y);
rlm@46 1623 this.xcomp = x;
rlm@46 1624 this.ycomp = y;
rlm@46 1625 this.offset = this.cumulativeOffset(element);
rlm@46 1626
rlm@46 1627 return (y >= this.offset[1] &&
rlm@46 1628 y < this.offset[1] + element.offsetHeight &&
rlm@46 1629 x >= this.offset[0] &&
rlm@46 1630 x < this.offset[0] + element.offsetWidth);
rlm@46 1631 },
rlm@46 1632
rlm@46 1633 withinIncludingScrolloffsets: function(element, x, y) {
rlm@46 1634 var offsetcache = this.realOffset(element);
rlm@46 1635
rlm@46 1636 this.xcomp = x + offsetcache[0] - this.deltaX;
rlm@46 1637 this.ycomp = y + offsetcache[1] - this.deltaY;
rlm@46 1638 this.offset = this.cumulativeOffset(element);
rlm@46 1639
rlm@46 1640 return (this.ycomp >= this.offset[1] &&
rlm@46 1641 this.ycomp < this.offset[1] + element.offsetHeight &&
rlm@46 1642 this.xcomp >= this.offset[0] &&
rlm@46 1643 this.xcomp < this.offset[0] + element.offsetWidth);
rlm@46 1644 },
rlm@46 1645
rlm@46 1646 // within must be called directly before
rlm@46 1647 overlap: function(mode, element) {
rlm@46 1648 if (!mode) return 0;
rlm@46 1649 if (mode == 'vertical')
rlm@46 1650 return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
rlm@46 1651 element.offsetHeight;
rlm@46 1652 if (mode == 'horizontal')
rlm@46 1653 return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
rlm@46 1654 element.offsetWidth;
rlm@46 1655 },
rlm@46 1656
rlm@46 1657 clone: function(source, target) {
rlm@46 1658 source = $(source);
rlm@46 1659 target = $(target);
rlm@46 1660 target.style.position = 'absolute';
rlm@46 1661 var offsets = this.cumulativeOffset(source);
rlm@46 1662 target.style.top = offsets[1] + 'px';
rlm@46 1663 target.style.left = offsets[0] + 'px';
rlm@46 1664 target.style.width = source.offsetWidth + 'px';
rlm@46 1665 target.style.height = source.offsetHeight + 'px';
rlm@46 1666 },
rlm@46 1667
rlm@46 1668 page: function(forElement) {
rlm@46 1669 var valueT = 0, valueL = 0;
rlm@46 1670
rlm@46 1671 var element = forElement;
rlm@46 1672 do {
rlm@46 1673 valueT += element.offsetTop || 0;
rlm@46 1674 valueL += element.offsetLeft || 0;
rlm@46 1675
rlm@46 1676 // Safari fix
rlm@46 1677 if (element.offsetParent==document.body)
rlm@46 1678 if (Element.getStyle(element,'position')=='absolute') break;
rlm@46 1679
rlm@46 1680 } while (element = element.offsetParent);
rlm@46 1681
rlm@46 1682 element = forElement;
rlm@46 1683 do {
rlm@46 1684 valueT -= element.scrollTop || 0;
rlm@46 1685 valueL -= element.scrollLeft || 0;
rlm@46 1686 } while (element = element.parentNode);
rlm@46 1687
rlm@46 1688 return [valueL, valueT];
rlm@46 1689 },
rlm@46 1690
rlm@46 1691 clone: function(source, target) {
rlm@46 1692 var options = Object.extend({
rlm@46 1693 setLeft: true,
rlm@46 1694 setTop: true,
rlm@46 1695 setWidth: true,
rlm@46 1696 setHeight: true,
rlm@46 1697 offsetTop: 0,
rlm@46 1698 offsetLeft: 0
rlm@46 1699 }, arguments[2] || {})
rlm@46 1700
rlm@46 1701 // find page position of source
rlm@46 1702 source = $(source);
rlm@46 1703 var p = Position.page(source);
rlm@46 1704
rlm@46 1705 // find coordinate system to use
rlm@46 1706 target = $(target);
rlm@46 1707 var delta = [0, 0];
rlm@46 1708 var parent = null;
rlm@46 1709 // delta [0,0] will do fine with position: fixed elements,
rlm@46 1710 // position:absolute needs offsetParent deltas
rlm@46 1711 if (Element.getStyle(target,'position') == 'absolute') {
rlm@46 1712 parent = Position.offsetParent(target);
rlm@46 1713 delta = Position.page(parent);
rlm@46 1714 }
rlm@46 1715
rlm@46 1716 // correct by body offsets (fixes Safari)
rlm@46 1717 if (parent == document.body) {
rlm@46 1718 delta[0] -= document.body.offsetLeft;
rlm@46 1719 delta[1] -= document.body.offsetTop;
rlm@46 1720 }
rlm@46 1721
rlm@46 1722 // set position
rlm@46 1723 if(options.setLeft) target.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px';
rlm@46 1724 if(options.setTop) target.style.top = (p[1] - delta[1] + options.offsetTop) + 'px';
rlm@46 1725 if(options.setWidth) target.style.width = source.offsetWidth + 'px';
rlm@46 1726 if(options.setHeight) target.style.height = source.offsetHeight + 'px';
rlm@46 1727 },
rlm@46 1728
rlm@46 1729 absolutize: function(element) {
rlm@46 1730 element = $(element);
rlm@46 1731 if (element.style.position == 'absolute') return;
rlm@46 1732 Position.prepare();
rlm@46 1733
rlm@46 1734 var offsets = Position.positionedOffset(element);
rlm@46 1735 var top = offsets[1];
rlm@46 1736 var left = offsets[0];
rlm@46 1737 var width = element.clientWidth;
rlm@46 1738 var height = element.clientHeight;
rlm@46 1739
rlm@46 1740 element._originalLeft = left - parseFloat(element.style.left || 0);
rlm@46 1741 element._originalTop = top - parseFloat(element.style.top || 0);
rlm@46 1742 element._originalWidth = element.style.width;
rlm@46 1743 element._originalHeight = element.style.height;
rlm@46 1744
rlm@46 1745 element.style.position = 'absolute';
rlm@46 1746 element.style.top = top + 'px';;
rlm@46 1747 element.style.left = left + 'px';;
rlm@46 1748 element.style.width = width + 'px';;
rlm@46 1749 element.style.height = height + 'px';;
rlm@46 1750 },
rlm@46 1751
rlm@46 1752 relativize: function(element) {
rlm@46 1753 element = $(element);
rlm@46 1754 if (element.style.position == 'relative') return;
rlm@46 1755 Position.prepare();
rlm@46 1756
rlm@46 1757 element.style.position = 'relative';
rlm@46 1758 var top = parseFloat(element.style.top || 0) - (element._originalTop || 0);
rlm@46 1759 var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0);
rlm@46 1760
rlm@46 1761 element.style.top = top + 'px';
rlm@46 1762 element.style.left = left + 'px';
rlm@46 1763 element.style.height = element._originalHeight;
rlm@46 1764 element.style.width = element._originalWidth;
rlm@46 1765 }
rlm@46 1766 }
rlm@46 1767
rlm@46 1768 // Safari returns margins on body which is incorrect if the child is absolutely
rlm@46 1769 // positioned. For performance reasons, redefine Position.cumulativeOffset for
rlm@46 1770 // KHTML/WebKit only.
rlm@46 1771 if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
rlm@46 1772 Position.cumulativeOffset = function(element) {
rlm@46 1773 var valueT = 0, valueL = 0;
rlm@46 1774 do {
rlm@46 1775 valueT += element.offsetTop || 0;
rlm@46 1776 valueL += element.offsetLeft || 0;
rlm@46 1777 if (element.offsetParent == document.body)
rlm@46 1778 if (Element.getStyle(element, 'position') == 'absolute') break;
rlm@46 1779
rlm@46 1780 element = element.offsetParent;
rlm@46 1781 } while (element);
rlm@46 1782
rlm@46 1783 return [valueL, valueT];
rlm@46 1784 }
rlm@46 1785 }