Changeset 6512
- Timestamp:
- 12/28/07 19:17:21 (1 year ago)
- Files:
-
- trunk/wp-includes/js/prototype.js (modified) (117 diffs)
- trunk/wp-includes/js/scriptaculous/builder.js (modified) (1 diff)
- trunk/wp-includes/js/scriptaculous/controls.js (modified) (14 diffs)
- trunk/wp-includes/js/scriptaculous/dragdrop.js (modified) (26 diffs)
- trunk/wp-includes/js/scriptaculous/effects.js (modified) (52 diffs)
- trunk/wp-includes/js/scriptaculous/prototype.js (modified) (117 diffs)
- trunk/wp-includes/js/scriptaculous/scriptaculous.js (modified) (3 diffs)
- trunk/wp-includes/js/scriptaculous/slider.js (modified) (19 diffs)
- trunk/wp-includes/js/scriptaculous/sound.js (modified) (2 diffs)
- trunk/wp-includes/js/scriptaculous/unittest.js (modified) (3 diffs)
- trunk/wp-includes/js/scriptaculous/wp-scriptaculous.js (modified) (1 diff)
- trunk/wp-includes/script-loader.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/wp-includes/js/prototype.js
r5743 r6512 1 /* Prototype JavaScript framework, version 1. 5.1.11 /* Prototype JavaScript framework, version 1.6.0 2 2 * (c) 2005-2007 Sam Stephenson 3 3 * … … 5 5 * For details, see the Prototype web site: http://www.prototypejs.org/ 6 6 * 7 /*--------------------------------------------------------------------------*/7 *--------------------------------------------------------------------------*/ 8 8 9 9 var Prototype = { 10 Version: '1. 5.1.1',10 Version: '1.6.0', 11 11 12 12 Browser: { … … 14 14 Opera: !!window.opera, 15 15 WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1, 16 Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 16 Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1, 17 MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/) 17 18 }, 18 19 … … 21 22 ElementExtensions: !!window.HTMLElement, 22 23 SpecificElementExtensions: 23 (document.createElement('div').__proto__ !== 24 document.createElement('form').__proto__) 24 document.createElement('div').__proto__ && 25 document.createElement('div').__proto__ !== 26 document.createElement('form').__proto__ 25 27 }, 26 28 … … 30 32 emptyFunction: function() { }, 31 33 K: function(x) { return x } 32 } 33 34 }; 35 36 if (Prototype.Browser.MobileSafari) 37 Prototype.BrowserFeatures.SpecificElementExtensions = false; 38 39 if (Prototype.Browser.WebKit) 40 Prototype.BrowserFeatures.XPath = false; 41 42 /* Based on Alex Arnell's inheritance implementation. */ 34 43 var Class = { 35 44 create: function() { 36 return function() { 45 var parent = null, properties = $A(arguments); 46 if (Object.isFunction(properties[0])) 47 parent = properties.shift(); 48 49 function klass() { 37 50 this.initialize.apply(this, arguments); 38 51 } 39 } 40 } 41 42 var Abstract = new Object(); 52 53 Object.extend(klass, Class.Methods); 54 klass.superclass = parent; 55 klass.subclasses = []; 56 57 if (parent) { 58 var subclass = function() { }; 59 subclass.prototype = parent.prototype; 60 klass.prototype = new subclass; 61 parent.subclasses.push(klass); 62 } 63 64 for (var i = 0; i < properties.length; i++) 65 klass.addMethods(properties[i]); 66 67 if (!klass.prototype.initialize) 68 klass.prototype.initialize = Prototype.emptyFunction; 69 70 klass.prototype.constructor = klass; 71 72 return klass; 73 } 74 }; 75 76 Class.Methods = { 77 addMethods: function(source) { 78 var ancestor = this.superclass && this.superclass.prototype; 79 var properties = Object.keys(source); 80 81 if (!Object.keys({ toString: true }).length) 82 properties.push("toString", "valueOf"); 83 84 for (var i = 0, length = properties.length; i < length; i++) { 85 var property = properties[i], value = source[property]; 86 if (ancestor && Object.isFunction(value) && 87 value.argumentNames().first() == "$super") { 88 var method = value, value = Object.extend((function(m) { 89 return function() { return ancestor[m].apply(this, arguments) }; 90 })(property).wrap(method), { 91 valueOf: function() { return method }, 92 toString: function() { return method.toString() } 93 }); 94 } 95 this.prototype[property] = value; 96 } 97 98 return this; 99 } 100 }; 101 102 var Abstract = { }; 43 103 44 104 Object.extend = function(destination, source) { 45 for (var property in source) {105 for (var property in source) 46 106 destination[property] = source[property]; 47 }48 107 return destination; 49 } 108 }; 50 109 51 110 Object.extend(Object, { … … 63 122 toJSON: function(object) { 64 123 var type = typeof object; 65 switch (type) {124 switch (type) { 66 125 case 'undefined': 67 126 case 'function': … … 69 128 case 'boolean': return object.toString(); 70 129 } 130 71 131 if (object === null) return 'null'; 72 132 if (object.toJSON) return object.toJSON(); 73 if (object.ownerDocument === document) return; 133 if (Object.isElement(object)) return; 134 74 135 var results = []; 75 136 for (var property in object) { … … 78 139 results.push(property.toJSON() + ': ' + value); 79 140 } 141 80 142 return '{' + results.join(', ') + '}'; 143 }, 144 145 toQueryString: function(object) { 146 return $H(object).toQueryString(); 147 }, 148 149 toHTML: function(object) { 150 return object && object.toHTML ? object.toHTML() : String.interpret(object); 81 151 }, 82 152 … … 96 166 97 167 clone: function(object) { 98 return Object.extend({}, object); 168 return Object.extend({ }, object); 169 }, 170 171 isElement: function(object) { 172 return object && object.nodeType == 1; 173 }, 174 175 isArray: function(object) { 176 return object && object.constructor === Array; 177 }, 178 179 isHash: function(object) { 180 return object instanceof Hash; 181 }, 182 183 isFunction: function(object) { 184 return typeof object == "function"; 185 }, 186 187 isString: function(object) { 188 return typeof object == "string"; 189 }, 190 191 isNumber: function(object) { 192 return typeof object == "number"; 193 }, 194 195 isUndefined: function(object) { 196 return typeof object == "undefined"; 99 197 } 100 198 }); 101 199 102 Function.prototype.bind = function() { 103 var __method = this, args = $A(arguments), object = args.shift(); 104 return function() { 105 return __method.apply(object, args.concat($A(arguments))); 106 } 107 } 108 109 Function.prototype.bindAsEventListener = function(object) { 110 var __method = this, args = $A(arguments), object = args.shift(); 111 return function(event) { 112 return __method.apply(object, [event || window.event].concat(args)); 113 } 114 } 115 116 Object.extend(Number.prototype, { 117 toColorPart: function() { 118 return this.toPaddedString(2, 16); 119 }, 120 121 succ: function() { 122 return this + 1; 123 }, 124 125 times: function(iterator) { 126 $R(0, this, true).each(iterator); 127 return this; 128 }, 129 130 toPaddedString: function(length, radix) { 131 var string = this.toString(radix || 10); 132 return '0'.times(length - string.length) + string; 133 }, 134 135 toJSON: function() { 136 return isFinite(this) ? this.toString() : 'null'; 200 Object.extend(Function.prototype, { 201 argumentNames: function() { 202 var names = this.toString().match(/^[\s\(]*function[^(]*\((.*?)\)/)[1].split(",").invoke("strip"); 203 return names.length == 1 && !names[0] ? [] : names; 204 }, 205 206 bind: function() { 207 if (arguments.length < 2 && arguments[0] === undefined) return this; 208 var __method = this, args = $A(arguments), object = args.shift(); 209 return function() { 210 return __method.apply(object, args.concat($A(arguments))); 211 } 212 }, 213 214 bindAsEventListener: function() { 215 var __method = this, args = $A(arguments), object = args.shift(); 216 return function(event) { 217 return __method.apply(object, [event || window.event].concat(args)); 218 } 219 }, 220 221 curry: function() { 222 if (!arguments.length) return this; 223 var __method = this, args = $A(arguments); 224 return function() { 225 return __method.apply(this, args.concat($A(arguments))); 226 } 227 }, 228 229 delay: function() { 230 var __method = this, args = $A(arguments), timeout = args.shift() * 1000; 231 return window.setTimeout(function() { 232 return __method.apply(__method, args); 233 }, timeout); 234 }, 235 236 wrap: function(wrapper) { 237 var __method = this; 238 return function() { 239 return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); 240 } 241 }, 242 243 methodize: function() { 244 if (this._methodized) return this._methodized; 245 var __method = this; 246 return this._methodized = function() { 247 return __method.apply(null, [this].concat($A(arguments))); 248 }; 137 249 } 138 250 }); 139 251 252 Function.prototype.defer = Function.prototype.delay.curry(0.01); 253 140 254 Date.prototype.toJSON = function() { 141 return '"' + this.get FullYear() + '-' +142 (this.get Month() + 1).toPaddedString(2) + '-' +143 this.get Date().toPaddedString(2) + 'T' +144 this.get Hours().toPaddedString(2) + ':' +145 this.get Minutes().toPaddedString(2) + ':' +146 this.get Seconds().toPaddedString(2) + '"';255 return '"' + this.getUTCFullYear() + '-' + 256 (this.getUTCMonth() + 1).toPaddedString(2) + '-' + 257 this.getUTCDate().toPaddedString(2) + 'T' + 258 this.getUTCHours().toPaddedString(2) + ':' + 259 this.getUTCMinutes().toPaddedString(2) + ':' + 260 this.getUTCSeconds().toPaddedString(2) + 'Z"'; 147 261 }; 148 262 … … 156 270 returnValue = lambda(); 157 271 break; 158 } catch (e) { }272 } catch (e) { } 159 273 } 160 274 161 275 return returnValue; 162 276 } 163 } 277 }; 278 279 RegExp.prototype.match = RegExp.prototype.test; 280 281 RegExp.escape = function(str) { 282 return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); 283 }; 164 284 165 285 /*--------------------------------------------------------------------------*/ 166 286 167 var PeriodicalExecuter = Class.create(); 168 PeriodicalExecuter.prototype = { 287 var PeriodicalExecuter = Class.create({ 169 288 initialize: function(callback, frequency) { 170 289 this.callback = callback; … … 179 298 }, 180 299 300 execute: function() { 301 this.callback(this); 302 }, 303 181 304 stop: function() { 182 305 if (!this.timer) return; … … 189 312 try { 190 313 this.currentlyExecuting = true; 191 this. callback(this);314 this.execute(); 192 315 } finally { 193 316 this.currentlyExecuting = false; … … 195 318 } 196 319 } 197 } 320 }); 198 321 Object.extend(String, { 199 322 interpret: function(value) { … … 239 362 scan: function(pattern, iterator) { 240 363 this.gsub(pattern, iterator); 241 return this;364 return String(this); 242 365 }, 243 366 … … 246 369 truncation = truncation === undefined ? '...' : truncation; 247 370 return this.length > length ? 248 this.slice(0, length - truncation.length) + truncation : this;371 this.slice(0, length - truncation.length) + truncation : String(this); 249 372 }, 250 373 … … 280 403 281 404 unescapeHTML: function() { 282 var div = document.createElement('div');405 var div = new Element('div'); 283 406 div.innerHTML = this.stripTags(); 284 407 return div.childNodes[0] ? (div.childNodes.length > 1 ? … … 289 412 toQueryParams: function(separator) { 290 413 var match = this.strip().match(/([^?#]*)(#.*)?$/); 291 if (!match) return { };292 293 return match[1].split(separator || '&').inject({ }, function(hash, pair) {414 if (!match) return { }; 415 416 return match[1].split(separator || '&').inject({ }, function(hash, pair) { 294 417 if ((pair = pair.split('='))[0]) { 295 418 var key = decodeURIComponent(pair.shift()); … … 298 421 299 422 if (key in hash) { 300 if ( hash[key].constructor != Array) hash[key] = [hash[key]];423 if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; 301 424 hash[key].push(value); 302 425 } … … 317 440 318 441 times: function(count) { 319 var result = ''; 320 for (var i = 0; i < count; i++) result += this; 321 return result; 442 return count < 1 ? '' : new Array(count + 1).join(this); 322 443 }, 323 444 … … 397 518 blank: function() { 398 519 return /^\s*$/.test(this); 520 }, 521 522 interpolate: function(object, pattern) { 523 return new Template(this, pattern).evaluate(object); 399 524 } 400 525 }); … … 410 535 411 536 String.prototype.gsub.prepareReplacement = function(replacement) { 412 if ( typeof replacement == 'function') return replacement;537 if (Object.isFunction(replacement)) return replacement; 413 538 var template = new Template(replacement); 414 539 return function(match) { return template.evaluate(match) }; 415 } 540 }; 416 541 417 542 String.prototype.parseQuery = String.prototype.toQueryParams; … … 424 549 with (String.prototype.escapeHTML) div.appendChild(text); 425 550 426 var Template = Class.create(); 427 Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; 428 Template.prototype = { 551 var Template = Class.create({ 429 552 initialize: function(template, pattern) { 430 553 this.template = template.toString(); 431 this.pattern = pattern || Template.Pattern;554 this.pattern = pattern || Template.Pattern; 432 555 }, 433 556 434 557 evaluate: function(object) { 558 if (Object.isFunction(object.toTemplateReplacements)) 559 object = object.toTemplateReplacements(); 560 435 561 return this.template.gsub(this.pattern, function(match) { 436 var before = match[1]; 562 if (object == null) return ''; 563 564 var before = match[1] || ''; 437 565 if (before == '\\') return match[2]; 438 return before + String.interpret(object[match[3]]); 439 }); 440 } 441 } 442 443 var $break = {}, $continue = new Error('"throw $continue" is deprecated, use "return" instead'); 566 567 var ctx = object, expr = match[3]; 568 var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/, match = pattern.exec(expr); 569 if (match == null) return before; 570 571 while (match != null) { 572 var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1]; 573 ctx = ctx[comp]; 574 if (null == ctx || '' == match[3]) break; 575 expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); 576 match = pattern.exec(expr); 577 } 578 579 return before + String.interpret(ctx); 580 }.bind(this)); 581 } 582 }); 583 Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; 584 585 var $break = { }; 444 586 445 587 var Enumerable = { 446 each: function(iterator ) {588 each: function(iterator, context) { 447 589 var index = 0; 590 iterator = iterator.bind(context); 448 591 try { 449 592 this._each(function(value) { … … 456 599 }, 457 600 458 eachSlice: function(number, iterator) { 601 eachSlice: function(number, iterator, context) { 602 iterator = iterator ? iterator.bind(context) : Prototype.K; 459 603 var index = -number, slices = [], array = this.toArray(); 460 604 while ((index += number) < array.length) 461 605 slices.push(array.slice(index, index+number)); 462 return slices.map(iterator); 463 }, 464 465 all: function(iterator) { 606 return slices.collect(iterator, context); 607 }, 608 609 all: function(iterator, context) { 610 iterator = iterator ? iterator.bind(context) : Prototype.K; 466 611 var result = true; 467 612 this.each(function(value, index) { 468 result = result && !! (iterator || Prototype.K)(value, index);613 result = result && !!iterator(value, index); 469 614 if (!result) throw $break; 470 615 }); … … 472 617 }, 473 618 474 any: function(iterator) { 619 any: function(iterator, context) { 620 iterator = iterator ? iterator.bind(context) : Prototype.K; 475 621 var result = false; 476 622 this.each(function(value, index) { 477 if (result = !! (iterator || Prototype.K)(value, index))623 if (result = !!iterator(value, index)) 478 624 throw $break; 479 625 }); … … 481 627 }, 482 628 483 collect: function(iterator) { 629 collect: function(iterator, context) { 630 iterator = iterator ? iterator.bind(context) : Prototype.K; 484 631 var results = []; 485 632 this.each(function(value, index) { 486 results.push( (iterator || Prototype.K)(value, index));633 results.push(iterator(value, index)); 487 634 }); 488 635 return results; 489 636 }, 490 637 491 detect: function(iterator) { 638 detect: function(iterator, context) { 639 iterator = iterator.bind(context); 492 640 var result; 493 641 this.each(function(value, index) { … … 500 648 }, 501 649 502 findAll: function(iterator) { 650 findAll: function(iterator, context) { 651 iterator = iterator.bind(context); 503 652 var results = []; 504 653 this.each(function(value, index) { … … 509 658 }, 510 659 511 grep: function(pattern, iterator) { 660 grep: function(filter, iterator, context) { 661 iterator = iterator ? iterator.bind(context) : Prototype.K; 512 662 var results = []; 663 664 if (Object.isString(filter)) 665 filter = new RegExp(filter); 666 513 667 this.each(function(value, index) { 514 var stringValue = value.toString(); 515 if (stringValue.match(pattern)) 516 results.push((iterator || Prototype.K)(value, index)); 517 }) 668 if (filter.match(value)) 669 results.push(iterator(value, index)); 670 }); 518 671 return results; 519 672 }, 520 673 521 674 include: function(object) { 675 if (Object.isFunction(this.indexOf)) 676 if (this.indexOf(object) != -1) return true; 677 522 678 var found = false; 523 679 this.each(function(value) { … … 538 694 }, 539 695 540 inject: function(memo, iterator) { 696 inject: function(memo, iterator, context) { 697 iterator = iterator.bind(context); 541 698 this.each(function(value, index) { 542 699 memo = iterator(memo, value, index); … … 552 709 }, 553 710 554 max: function(iterator) { 711 max: function(iterator, context) { 712 iterator = iterator ? iterator.bind(context) : Prototype.K; 555 713 var result; 556 714 this.each(function(value, index) { 557 value = (iterator || Prototype.K)(value, index);715 value = iterator(value, index); 558 716 if (result == undefined || value >= result) 559 717 result = value; … … 562 720 }, 563 721 564 min: function(iterator) { 722 min: function(iterator, context) { 723 iterator = iterator ? iterator.bind(context) : Prototype.K; 565 724 var result; 566 725 this.each(function(value, index) { 567 value = (iterator || Prototype.K)(value, index);726 value = iterator(value, index); 568 727 if (result == undefined || value < result) 569 728 result = value; … … 572 731 }, 573 732 574 partition: function(iterator) { 733 partition: function(iterator, context) { 734 iterator = iterator ? iterator.bind(context) : Prototype.K; 575 735 var trues = [], falses = []; 576 736 this.each(function(value, index) { 577 ( (iterator || Prototype.K)(value, index) ?737 (iterator(value, index) ? 578 738 trues : falses).push(value); 579 739 }); … … 583 743 pluck: function(property) { 584 744 var results = []; 585 this.each(function(value , index) {745 this.each(function(value) { 586 746 results.push(value[property]); 587 747 }); … … 589 749 }, 590 750 591 reject: function(iterator) { 751 reject: function(iterator, context) { 752 iterator = iterator.bind(context); 592 753 var results = []; 593 754 this.each(function(value, index) { … … 598 759 }, 599 760 600 sortBy: function(iterator) { 761 sortBy: function(iterator, context) { 762 iterator = iterator.bind(context); 601 763 return this.map(function(value, index) { 602 764 return {value: value, criteria: iterator(value, index)}; … … 613 775 zip: function() { 614 776 var iterator = Prototype.K, args = $A(arguments); 615 if ( typeof args.last() == 'function')777 if (Object.isFunction(args.last())) 616 778 iterator = args.pop(); 617 779 … … 629 791 return '#<Enumerable:' + this.toArray().inspect() + '>'; 630 792 } 631 } 793 }; 632 794 633 795 Object.extend(Enumerable, { … … 635 797 find: Enumerable.detect, 636 798 select: Enumerable.findAll, 799 filter: Enumerable.findAll, 637 800 member: Enumerable.include, 638 entries: Enumerable.toArray 801 entries: Enumerable.toArray, 802 every: Enumerable.all, 803 some: Enumerable.any 639 804 }); 640 var $A = Array.from = function(iterable) {805 function $A(iterable) { 641 806 if (!iterable) return []; 642 if (iterable.toArray) { 643 return iterable.toArray(); 644 } else { 645 var results = []; 646 for (var i = 0, length = iterable.length; i < length; i++) 647 results.push(iterable[i]); 807 if (iterable.toArray) return iterable.toArray(); 808 var length = iterable.length, results = new Array(length); 809 while (length--) results[length] = iterable[length]; 810 return results; 811 } 812 813 if (Prototype.Browser.WebKit) { 814 function $A(iterable) { 815 if (!iterable) return []; 816 if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && 817 iterable.toArray) return iterable.toArray(); 818 var length = iterable.length, results = new Array(length); 819 while (length--) results[length] = iterable[length]; 648 820 return results; 649 821 } 650 822 } 651 823 652 if (Prototype.Browser.WebKit) { 653 $A = Array.from = function(iterable) { 654 if (!iterable) return []; 655 if (!(typeof iterable == 'function' && iterable == '[object NodeList]') && 656 iterable.toArray) { 657 return iterable.toArray(); 658 } else { 659 var results = []; 660 for (var i = 0, length = iterable.length; i < length; i++) 661 results.push(iterable[i]); 662 return results; 663 } 664 } 665 } 824 Array.from = $A; 666 825 667 826 Object.extend(Array.prototype, Enumerable); 668 827 669 if (!Array.prototype._reverse) 670 Array.prototype._reverse = Array.prototype.reverse; 828 if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse; 671 829 672 830 Object.extend(Array.prototype, { … … 697 855 flatten: function() { 698 856 return this.inject([], function(array, value) { 699 return array.concat( value && value.constructor == Array?857 return array.concat(Object.isArray(value) ? 700 858 value.flatten() : [value]); 701 859 }); … … 709 867 }, 710 868 711 indexOf: function(object) {712 for (var i = 0, length = this.length; i < length; i++)713 if (this[i] == object) return i;714 return -1;715 },716 717 869 reverse: function(inline) { 718 870 return (inline !== false ? this : this.toArray())._reverse(); … … 728 880 array.push(value); 729 881 return array; 882 }); 883 }, 884 885 intersect: function(array) { 886 return this.uniq().findAll(function(item) { 887 return array.detect(function(value) { return item === value }); 730 888 }); 731 889 }, … … 753 911 }); 754 912 913 // use native browser JS 1.6 implementation if available 914 if (Object.isFunction(Array.prototype.forEach)) 915 Array.prototype._each = Array.prototype.forEach; 916 917 if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { 918 i || (i = 0); 919 var length = this.length; 920 if (i < 0) i = length + i; 921 for (; i < length; i++) 922 if (this[i] === item) return i; 923 return -1; 924 }; 925 926 if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { 927 i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; 928 var n = this.slice(0, i).reverse().indexOf(item); 929 return (n < 0) ? n : i - n - 1; 930 }; 931 755 932 Array.prototype.toArray = Array.prototype.clone; 756 933 757 934 function $w(string) { 935 if (!Object.isString(string)) return []; 758 936 string = string.strip(); 759 937 return string ? string.split(/\s+/) : []; … … 765 943 for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); 766 944 for (var i = 0, length = arguments.length; i < length; i++) { 767 if ( arguments[i].constructor == Array) {945 if (Object.isArray(arguments[i])) { 768 946 for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) 769 947 array.push(arguments[i][j]); … … 773 951 } 774 952 return array; 775 } 953 }; 776 954 } 777 var Hash = function(object) { 778 if (object instanceof Hash) this.merge(object); 779 else Object.extend(this, object || {}); 780 }; 781 782 Object.extend(Hash, { 783 toQueryString: function(obj) { 784 var parts = []; 785 parts.add = arguments.callee.addPair; 786 787 this.prototype._each.call(obj, function(pair) { 788 if (!pair.key) return; 789 var value = pair.value; 790 791 if (value && typeof value == 'object') { 792 if (value.constructor == Array) value.each(function(value) { 793 parts.add(pair.key, value); 794 }); 795 return; 796 } 797 parts.add(pair.key, value); 798 }); 799 800 return parts.join('&'); 801 }, 802 803 toJSON: function(object) { 804 var results = []; 805 this.prototype._each.call(object, function(pair) { 806 var value = Object.toJSON(pair.value); 807 if (value !== undefined) results.push(pair.key.toJSON() + ': ' + value); 808 }); 809 return '{' + results.join(', ') + '}'; 955 Object.extend(Number.prototype, { 956 toColorPart: function() { 957 return this.toPaddedString(2, 16); 958 }, 959 960 succ: function() { 961 return this + 1; 962 }, 963 964 times: function(iterator) { 965 $R(0, this, true).each(iterator); 966 return this; 967 }, 968 969 toPaddedString: function(length, radix) { 970 var string = this.toString(radix || 10); 971 return '0'.times(length - string.length) + string; 972 }, 973 974 toJSON: function() { 975 return isFinite(this) ? this.toString() : 'null'; 810 976 } 811 977 }); 812 978 813 Hash.toQueryString.addPair = function(key, value, prefix) { 814 key = encodeURIComponent(key); 815 if (value === undefined) this.push(key); 816 else this.push(key + '=' + (value == null ? '' : encodeURIComponent(value))); 817 } 818 819 Object.extend(Hash.prototype, Enumerable); 820 Object.extend(Hash.prototype, { 821 _each: function(iterator) { 822 for (var key in this) { 823 var value = this[key]; 824 if (value && value == Hash.prototype[key]) continue; 825 826 var pair = [key, value]; 827 pair.key = key; 828 pair.value = value; 829 iterator(pair); 830 } 831 }, 832 833 keys: function() { 834 return this.pluck('key'); 835 }, 836 837 values: function() { 838 return this.pluck('value'); 839 }, 840 841 merge: function(hash) { 842 return $H(hash).inject(this, function(mergedHash, pair) { 843 mergedHash[pair.key] = pair.value; 844 return mergedHash; 845 }); 846 }, 847 848 remove: function() { 849 var result; 850 for(var i = 0, length = arguments.length; i < length; i++) { 851 var value = this[arguments[i]]; 852 if (value !== undefined){ 853 if (result === undefined) result = value; 854 else { 855 if (result.constructor != Array) result = [result]; 856 result.push(value) 857 } 858 } 859 delete this[arguments[i]]; 860 } 861 return result; 862 }, 863 864 toQueryString: function() { 865 return Hash.toQueryString(this); 866 }, 867 868 inspect: function() { 869 return '#<Hash:{' + this.map(function(pair) { 870 return pair.map(Object.inspect).join(': '); 871 }).join(', ') + '}>'; 872 }, 873 874 toJSON: function() { 875 return Hash.toJSON(this); 876 } 979 $w('abs round ceil floor').each(function(method){ 980 Number.prototype[method] = Math[method].methodize(); 877 981 }); 878 879 982 function $H(object) { 880 if (object instanceof Hash) return object;881 983 return new Hash(object); 882 984 }; 883 985 884 // Safari iterates over shadowed properties 885 if (function() { 886 var i = 0, Test = function(value) { this.key = value }; 887 Test.prototype.key = 'foo'; 888 for (var property in new Test('bar')) i++; 889 return i > 1; 890 }()) Hash.prototype._each = function(iterator) { 891 var cache = []; 892 for (var key in this) { 893 var value = this[key]; 894 if ((value && value == Hash.prototype[key]) || cache.include(key)) continue; 895 cache.push(key); 896 var pair = [key, value]; 897 pair.key = key; 898 pair.value = value; 899 iterator(pair); 900 } 901 }; 902 ObjectRange = Class.create(); 903 Object.extend(ObjectRange.prototype, Enumerable); 904 Object.extend(ObjectRange.prototype, { 986 var Hash = Class.create(Enumerable, (function() { 987 if (function() { 988 var i = 0, Test = function(value) { this.key = value }; 989 Test.prototype.key = 'foo'; 990 for (var property in new Test('bar')) i++; 991 return i > 1; 992 }()) { 993 function each(iterator) { 994 var cache = []; 995 for (var key in this._object) { 996 var value = this._object[key]; 997 if (cache.include(key)) continue; 998 cache.push(key); 999 var pair = [key, value]; 1000 pair.key = key; 1001 pair.value = value; 1002 iterator(pair); 1003 } 1004 } 1005 } else { 1006 function each(iterator) { 1007 for (var key in this._object) { 1008 var value = this._object[key], pair = [key, value]; 1009 pair.key = key; 1010 pair.value = value; 1011 iterator(pair); 1012 } 1013 } 1014 } 1015 1016 function toQueryPair(key, value) { 1017 if (Object.isUndefined(value)) return key; 1018 return key + '=' + encodeURIComponent(String.interpret(value)); 1019 } 1020 1021 return { 1022 initialize: function(object) { 1023 this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); 1024 }, 1025 1026 _each: each, 1027 1028 set: function(key, value) { 1029 return this._object[key] = value; 1030 }, 1031 1032 get: function(key) { 1033 return this._object[key]; 1034 }, 1035 1036 unset: function(key) { 1037 var value = this._object[key]; 1038 delete this._object[key]; 1039 return value; 1040 }, 1041 1042 toObject: function() { 1043 return Object.clone(this._object); 1044 }, 1045 1046 keys: function() { 1047 return this.pluck('key'); 1048 }, 1049 1050 values: function() { 1051 return this.pluck('value'); 1052 }, 1053 1054 index: function(value) { 1055 var match = this.detect(function(pair) { 1056 return pair.value === value; 1057 }); 1058 return match && match.key; 1059 }, 1060 1061 merge: function(object) { 1062 return this.clone().update(object); 1063 }, 1064 1065 update: function(object) { 1066 return new Hash(object).inject(this, function(result, pair) { 1067 result.set(pair.key, pair.value); 1068 return result; 1069 }); 1070 }, 1071 1072 toQueryString: function() { 1073 return this.map(function(pair) { 1074 var key = encodeURIComponent(pair.key), values = pair.value; 1075 1076 if (values && typeof values == 'object') { 1077 if (Object.isArray(values)) 1078 return values.map(toQueryPair.curry(key)).join('&'); 1079 } 1080 return toQueryPair(key, values); 1081 }).join('&'); 1082 }, 1083 1084 inspect: function() { 1085 return '#<Hash:{' + this.map(function(pair) { 1086 return pair.map(Object.inspect).join(': '); 1087 }).join(', ') + '}>'; 1088 }, 1089 1090 toJSON: function() { 1091 return Object.toJSON(this.toObject()); 1092 }, 1093 1094 clone: function() { 1095 return new Hash(this); 1096 } 1097 } 1098 })()); 1099 1100 Hash.prototype.toTemplateReplacements = Hash.prototype.toObject; 1101 Hash.from = $H; 1102 var ObjectRange = Class.create(Enumerable, { 905 1103 initialize: function(start, end, exclusive) { 906 1104 this.start = start; … … 928 1126 var $R = function(start, end, exclusive) { 929 1127 return new ObjectRange(start, end, exclusive); 930 } 1128 }; 931 1129 932 1130 var Ajax = { … … 940 1138 941 1139 activeRequestCount: 0 942 } 1140 }; 943 1141 944 1142 Ajax.Responders = { … … 960 1158 dispatch: function(callback, request, transport, json) { 961 1159 this.each(function(responder) { 962 if ( typeof responder[callback] == 'function') {1160 if (Object.isFunction(responder[callback])) { 963 1161 try { 964 1162 responder[callback].apply(responder, [request, transport, json]); 965 } catch (e) { }1163 } catch (e) { } 966 1164 } 967 1165 }); … … 972 1170 973 1171 Ajax.Responders.register({ 974 onCreate: function() { 975 Ajax.activeRequestCount++; 976 }, 977 onComplete: function() { 978 Ajax.activeRequestCount--; 979 } 1172 onCreate: function() { Ajax.activeRequestCount++ }, 1173 onComplete: function() { Ajax.activeRequestCount-- } 980 1174 }); 981 1175 982 Ajax.Base = function() {}; 983 Ajax.Base.prototype = { 984 setOptions: function(options) { 1176 Ajax.Base = Class.create({ 1177 initialize: function(options) { 985 1178 this.options = { 986 1179 method: 'post', … … 988 1181 contentType: 'application/x-www-form-urlencoded', 989 1182 encoding: 'UTF-8', 990 parameters: '' 991 } 992 Object.extend(this.options, options || {}); 1183 parameters: '', 1184 evalJSON: true, 1185 evalJS: true 1186 }; 1187 Object.extend(this.options, options || { }); 993 1188 994 1189 this.options.method = this.options.method.toLowerCase(); 995 if ( typeof this.options.parameters == 'string')1190 if (Object.isString(this.options.parameters)) 996 1191 this.options.parameters = this.options.parameters.toQueryParams(); 997 1192 } 998 } 999 1000 Ajax.Request = Class.create(); 1001 Ajax.Request.Events = 1002 ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; 1003 1004 Ajax.Request.prototype = Object.extend(new Ajax.Base(), { 1193 }); 1194 1195 Ajax.Request = Class.create(Ajax.Base, { 1005 1196 _complete: false, 1006 1197 1007 initialize: function(url, options) { 1198 initialize: function($super, url, options) { 1199 $super(options); 1008 1200 this.transport = Ajax.getTransport(); 1009 this.setOptions(options);1010 1201 this.request(url); 1011 1202 }, … … 1024 1215 this.parameters = params; 1025 1216 1026 if (params = Hash.toQueryString(params)) {1217 if (params = Object.toQueryString(params)) { 1027 1218 // when GET, append parameters to URL 1028 1219 if (this.method == 'get') … … 1033 1224 1034 1225 try { 1035 if (this.options.onCreate) this.options.onCreate(this.transport); 1036 Ajax.Responders.dispatch('onCreate', this, this.transport); 1226 var response = new Ajax.Response(this); 1227 if (this.options.onCreate) this.options.onCreate(response);&nbs
