| 12 | | XPath: !!document.evaluate |
|---|
| 13 | | }, |
|---|
| 14 | | |
|---|
| 15 | | ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', |
|---|
| 16 | | emptyFunction: function() {}, |
|---|
| | 21 | XPath: !!document.evaluate, |
|---|
| | 22 | ElementExtensions: !!window.HTMLElement, |
|---|
| | 23 | SpecificElementExtensions: |
|---|
| | 24 | document.createElement('div').__proto__ && |
|---|
| | 25 | document.createElement('div').__proto__ !== |
|---|
| | 26 | document.createElement('form').__proto__ |
|---|
| | 27 | }, |
|---|
| | 28 | |
|---|
| | 29 | ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>', |
|---|
| | 30 | JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, |
|---|
| | 31 | |
|---|
| | 32 | emptyFunction: function() { }, |
|---|
| 25 | | } |
|---|
| 26 | | } |
|---|
| 27 | | |
|---|
| 28 | | var Abstract = new Object(); |
|---|
| | 50 | |
|---|
| | 51 | Object.extend(klass, Class.Methods); |
|---|
| | 52 | klass.superclass = parent; |
|---|
| | 53 | klass.subclasses = []; |
|---|
| | 54 | |
|---|
| | 55 | if (parent) { |
|---|
| | 56 | var subclass = function() { }; |
|---|
| | 57 | subclass.prototype = parent.prototype; |
|---|
| | 58 | klass.prototype = new subclass; |
|---|
| | 59 | parent.subclasses.push(klass); |
|---|
| | 60 | } |
|---|
| | 61 | |
|---|
| | 62 | for (var i = 0; i < properties.length; i++) |
|---|
| | 63 | klass.addMethods(properties[i]); |
|---|
| | 64 | |
|---|
| | 65 | if (!klass.prototype.initialize) |
|---|
| | 66 | klass.prototype.initialize = Prototype.emptyFunction; |
|---|
| | 67 | |
|---|
| | 68 | klass.prototype.constructor = klass; |
|---|
| | 69 | |
|---|
| | 70 | return klass; |
|---|
| | 71 | } |
|---|
| | 72 | }; |
|---|
| | 73 | |
|---|
| | 74 | Class.Methods = { |
|---|
| | 75 | addMethods: function(source) { |
|---|
| | 76 | var ancestor = this.superclass && this.superclass.prototype; |
|---|
| | 77 | var properties = Object.keys(source); |
|---|
| | 78 | |
|---|
| | 79 | if (!Object.keys({ toString: true }).length) |
|---|
| | 80 | properties.push("toString", "valueOf"); |
|---|
| | 81 | |
|---|
| | 82 | for (var i = 0, length = properties.length; i < length; i++) { |
|---|
| | 83 | var property = properties[i], value = source[property]; |
|---|
| | 84 | if (ancestor && Object.isFunction(value) && |
|---|
| | 85 | value.argumentNames().first() == "$super") { |
|---|
| | 86 | var method = value, value = Object.extend((function(m) { |
|---|
| | 87 | return function() { return ancestor[m].apply(this, arguments) }; |
|---|
| | 88 | })(property).wrap(method), { |
|---|
| | 89 | valueOf: function() { return method }, |
|---|
| | 90 | toString: function() { return method.toString() } |
|---|
| | 91 | }); |
|---|
| | 92 | } |
|---|
| | 93 | this.prototype[property] = value; |
|---|
| | 94 | } |
|---|
| | 95 | |
|---|
| | 96 | return this; |
|---|
| | 97 | } |
|---|
| | 98 | }; |
|---|
| | 99 | |
|---|
| | 100 | var Abstract = { }; |
|---|
| | 118 | }, |
|---|
| | 119 | |
|---|
| | 120 | toJSON: function(object) { |
|---|
| | 121 | var type = typeof object; |
|---|
| | 122 | switch (type) { |
|---|
| | 123 | case 'undefined': |
|---|
| | 124 | case 'function': |
|---|
| | 125 | case 'unknown': return; |
|---|
| | 126 | case 'boolean': return object.toString(); |
|---|
| | 127 | } |
|---|
| | 128 | |
|---|
| | 129 | if (object === null) return 'null'; |
|---|
| | 130 | if (object.toJSON) return object.toJSON(); |
|---|
| | 131 | if (Object.isElement(object)) return; |
|---|
| | 132 | |
|---|
| | 133 | var results = []; |
|---|
| | 134 | for (var property in object) { |
|---|
| | 135 | var value = Object.toJSON(object[property]); |
|---|
| | 136 | if (!Object.isUndefined(value)) |
|---|
| | 137 | results.push(property.toJSON() + ': ' + value); |
|---|
| | 138 | } |
|---|
| | 139 | |
|---|
| | 140 | return '{' + results.join(', ') + '}'; |
|---|
| | 141 | }, |
|---|
| | 142 | |
|---|
| | 143 | toQueryString: function(object) { |
|---|
| | 144 | return $H(object).toQueryString(); |
|---|
| | 145 | }, |
|---|
| | 146 | |
|---|
| | 147 | toHTML: function(object) { |
|---|
| | 148 | return object && object.toHTML ? object.toHTML() : String.interpret(object); |
|---|
| 64 | | return Object.extend({}, object); |
|---|
| | 166 | return Object.extend({ }, object); |
|---|
| | 167 | }, |
|---|
| | 168 | |
|---|
| | 169 | isElement: function(object) { |
|---|
| | 170 | return object && object.nodeType == 1; |
|---|
| | 171 | }, |
|---|
| | 172 | |
|---|
| | 173 | isArray: function(object) { |
|---|
| | 174 | return object != null && typeof object == "object" && |
|---|
| | 175 | 'splice' in object && 'join' in object; |
|---|
| | 176 | }, |
|---|
| | 177 | |
|---|
| | 178 | isHash: function(object) { |
|---|
| | 179 | return object instanceof Hash; |
|---|
| | 180 | }, |
|---|
| | 181 | |
|---|
| | 182 | isFunction: function(object) { |
|---|
| | 183 | return typeof object == "function"; |
|---|
| | 184 | }, |
|---|
| | 185 | |
|---|
| | 186 | isString: function(object) { |
|---|
| | 187 | return typeof object == "string"; |
|---|
| | 188 | }, |
|---|
| | 189 | |
|---|
| | 190 | isNumber: function(object) { |
|---|
| | 191 | return typeof object == "number"; |
|---|
| | 192 | }, |
|---|
| | 193 | |
|---|
| | 194 | isUndefined: function(object) { |
|---|
| | 195 | return typeof object == "undefined"; |
|---|
| 68 | | Function.prototype.bind = function() { |
|---|
| 69 | | var __method = this, args = $A(arguments), object = args.shift(); |
|---|
| 70 | | return function() { |
|---|
| 71 | | return __method.apply(object, args.concat($A(arguments))); |
|---|
| 72 | | } |
|---|
| 73 | | } |
|---|
| 74 | | |
|---|
| 75 | | Function.prototype.bindAsEventListener = function(object) { |
|---|
| 76 | | var __method = this, args = $A(arguments), object = args.shift(); |
|---|
| 77 | | return function(event) { |
|---|
| 78 | | return __method.apply(object, [( event || window.event)].concat(args).concat($A(arguments))); |
|---|
| 79 | | } |
|---|
| 80 | | } |
|---|
| 81 | | |
|---|
| 82 | | Object.extend(Number.prototype, { |
|---|
| 83 | | toColorPart: function() { |
|---|
| 84 | | var digits = this.toString(16); |
|---|
| 85 | | if (this < 16) return '0' + digits; |
|---|
| 86 | | return digits; |
|---|
| 87 | | }, |
|---|
| 88 | | |
|---|
| 89 | | succ: function() { |
|---|
| 90 | | return this + 1; |
|---|
| 91 | | }, |
|---|
| 92 | | |
|---|
| 93 | | times: function(iterator) { |
|---|
| 94 | | $R(0, this, true).each(iterator); |
|---|
| 95 | | return this; |
|---|
| | 199 | Object.extend(Function.prototype, { |
|---|
| | 200 | argumentNames: function() { |
|---|
| | 201 | var names = this.toString().match(/^[\s\(]*function[^(]*\((.*?)\)/)[1].split(",").invoke("strip"); |
|---|
| | 202 | return names.length == 1 && !names[0] ? [] : names; |
|---|
| | 203 | }, |
|---|
| | 204 | |
|---|
| | 205 | bind: function() { |
|---|
| | 206 | if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; |
|---|
| | 207 | var __method = this, args = $A(arguments), object = args.shift(); |
|---|
| | 208 | return function() { |
|---|
| | 209 | return __method.apply(object, args.concat($A(arguments))); |
|---|
| | 210 | } |
|---|
| | 211 | }, |
|---|
| | 212 | |
|---|
| | 213 | bindAsEventListener: function() { |
|---|
| | 214 | var __method = this, args = $A(arguments), object = args.shift(); |
|---|
| | 215 | return function(event) { |
|---|
| | 216 | return __method.apply(object, [event || window.event].concat(args)); |
|---|
| | 217 | } |
|---|
| | 218 | }, |
|---|
| | 219 | |
|---|
| | 220 | curry: function() { |
|---|
| | 221 | if (!arguments.length) return this; |
|---|
| | 222 | var __method = this, args = $A(arguments); |
|---|
| | 223 | return function() { |
|---|
| | 224 | return __method.apply(this, args.concat($A(arguments))); |
|---|
| | 225 | } |
|---|
| | 226 | }, |
|---|
| | 227 | |
|---|
| | 228 | delay: function() { |
|---|
| | 229 | var __method = this, args = $A(arguments), timeout = args.shift() * 1000; |
|---|
| | 230 | return window.setTimeout(function() { |
|---|
| | 231 | return __method.apply(__method, args); |
|---|
| | 232 | }, timeout); |
|---|
| | 233 | }, |
|---|
| | 234 | |
|---|
| | 235 | wrap: function(wrapper) { |
|---|
| | 236 | var __method = this; |
|---|
| | 237 | return function() { |
|---|
| | 238 | return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); |
|---|
| | 239 | } |
|---|
| | 240 | }, |
|---|
| | 241 | |
|---|
| | 242 | methodize: function() { |
|---|
| | 243 | if (this._methodized) return this._methodized; |
|---|
| | 244 | var __method = this; |
|---|
| | 245 | return this._methodized = function() { |
|---|
| | 246 | return __method.apply(null, [this].concat($A(arguments))); |
|---|
| | 247 | }; |
|---|
| 221 | | return div.childNodes[0] ? div.childNodes[0].nodeValue : ''; |
|---|
| 222 | | }, |
|---|
| 223 | | |
|---|
| 224 | | toQueryParams: function() { |
|---|
| 225 | | var pairs = this.match(/^\??(.*)$/)[1].split('&'); |
|---|
| 226 | | return pairs.inject({}, function(params, pairString) { |
|---|
| 227 | | var pair = pairString.split('='); |
|---|
| 228 | | var value = pair[1] ? decodeURIComponent(pair[1]) : undefined; |
|---|
| 229 | | params[decodeURIComponent(pair[0])] = value; |
|---|
| 230 | | return params; |
|---|
| | 406 | return div.childNodes[0] ? (div.childNodes.length > 1 ? |
|---|
| | 407 | $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : |
|---|
| | 408 | div.childNodes[0].nodeValue) : ''; |
|---|
| | 409 | }, |
|---|
| | 410 | |
|---|
| | 411 | toQueryParams: function(separator) { |
|---|
| | 412 | var match = this.strip().match(/([^?#]*)(#.*)?$/); |
|---|
| | 413 | if (!match) return { }; |
|---|
| | 414 | |
|---|
| | 415 | return match[1].split(separator || '&').inject({ }, function(hash, pair) { |
|---|
| | 416 | if ((pair = pair.split('='))[0]) { |
|---|
| | 417 | var key = decodeURIComponent(pair.shift()); |
|---|
| | 418 | var value = pair.length > 1 ? pair.join('=') : pair[0]; |
|---|
| | 419 | if (value != undefined) value = decodeURIComponent(value); |
|---|
| | 420 | |
|---|
| | 421 | if (key in hash) { |
|---|
| | 422 | if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; |
|---|
| | 423 | hash[key].push(value); |
|---|
| | 424 | } |
|---|
| | 425 | else hash[key] = value; |
|---|
| | 426 | } |
|---|
| | 427 | return hash; |
|---|
| 239 | | var oStringList = this.split('-'); |
|---|
| 240 | | if (oStringList.length == 1) return oStringList[0]; |
|---|
| 241 | | |
|---|
| 242 | | var camelizedString = this.indexOf('-') == 0 |
|---|
| 243 | | ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) |
|---|
| 244 | | : oStringList[0]; |
|---|
| 245 | | |
|---|
| 246 | | for (var i = 1, len = oStringList.length; i < len; i++) { |
|---|
| 247 | | var s = oStringList[i]; |
|---|
| 248 | | camelizedString += s.charAt(0).toUpperCase() + s.substring(1); |
|---|
| 249 | | } |
|---|
| 250 | | |
|---|
| 251 | | return camelizedString; |
|---|
| | 445 | var parts = this.split('-'), len = parts.length; |
|---|
| | 446 | if (len == 1) return parts[0]; |
|---|
| | 447 | |
|---|
| | 448 | var camelized = this.charAt(0) == '-' |
|---|
| | 449 | ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1) |
|---|
| | 450 | : parts[0]; |
|---|
| | 451 | |
|---|
| | 452 | for (var i = 1; i < len; i++) |
|---|
| | 453 | camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); |
|---|
| | 454 | |
|---|
| | 455 | return camelized; |
|---|
| | 456 | }, |
|---|
| | 457 | |
|---|
| | 458 | capitalize: function() { |
|---|
| | 459 | return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); |
|---|
| | 460 | }, |
|---|
| | 461 | |
|---|
| | 462 | underscore: function() { |
|---|
| | 463 | return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); |
|---|
| | 464 | }, |
|---|
| | 465 | |
|---|
| | 466 | dasherize: function() { |
|---|
| | 467 | return this.gsub(/_/,'-'); |
|---|
| 255 | | var escapedString = this.replace(/\\/g, '\\\\'); |
|---|
| 256 | | if (useDoubleQuotes) |
|---|
| 257 | | return '"' + escapedString.replace(/"/g, '\\"') + '"'; |
|---|
| 258 | | else |
|---|
| 259 | | return "'" + escapedString.replace(/'/g, '\\\'') + "'"; |
|---|
| | 471 | var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) { |
|---|
| | 472 | var character = String.specialChar[match[0]]; |
|---|
| | 473 | return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16); |
|---|
| | 474 | }); |
|---|
| | 475 | if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; |
|---|
| | 476 | return "'" + escapedString.replace(/'/g, '\\\'') + "'"; |
|---|
| | 477 | }, |
|---|
| | 478 | |
|---|
| | 479 | toJSON: function() { |
|---|
| | 480 | return this.inspect(true); |
|---|
| | 481 | }, |
|---|
| | 482 | |
|---|
| | 483 | unfilterJSON: function(filter) { |
|---|
| | 484 | return this.sub(filter || Prototype.JSONFilter, '#{1}'); |
|---|
| | 485 | }, |
|---|
| | 486 | |
|---|
| | 487 | isJSON: function() { |
|---|
| | 488 | var str = this; |
|---|
| | 489 | if (str.blank()) return false; |
|---|
| | 490 | str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); |
|---|
| | 491 | return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); |
|---|
| | 492 | }, |
|---|
| | 493 | |
|---|
| | 494 | evalJSON: function(sanitize) { |
|---|
| | 495 | var json = this.unfilterJSON(); |
|---|
| | 496 | try { |
|---|
| | 497 | if (!sanitize || json.isJSON()) return eval('(' + json + ')'); |
|---|
| | 498 | } catch (e) { } |
|---|
| | 499 | throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); |
|---|
| | 500 | }, |
|---|
| | 501 | |
|---|
| | 502 | include: function(pattern) { |
|---|
| | 503 | return this.indexOf(pattern) > -1; |
|---|
| | 504 | }, |
|---|
| | 505 | |
|---|
| | 506 | startsWith: function(pattern) { |
|---|
| | 507 | return this.indexOf(pattern) === 0; |
|---|
| | 508 | }, |
|---|
| | 509 | |
|---|
| | 510 | endsWith: function(pattern) { |
|---|
| | 511 | var d = this.length - pattern.length; |
|---|
| | 512 | return d >= 0 && this.lastIndexOf(pattern) === d; |
|---|
| | 513 | }, |
|---|
| | 514 | |
|---|
| | 515 | empty: function() { |
|---|
| | 516 | return this == ''; |
|---|
| | 517 | }, |
|---|
| | 518 | |
|---|
| | 519 | blank: function() { |
|---|
| | 520 | return /^\s*$/.test(this); |
|---|
| | 521 | }, |
|---|
| | 522 | |
|---|
| | 523 | interpolate: function(object, pattern) { |
|---|
| | 524 | return new Template(this, pattern).evaluate(object); |
|---|
| 283 | | return before + (object[match[3]] || '').toString(); |
|---|
| | 567 | |
|---|
| | 568 | var ctx = object, expr = match[3]; |
|---|
| | 569 | var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; |
|---|
| | 570 | match = pattern.exec(expr); |
|---|
| | 571 | if (match == null) return before; |
|---|
| | 572 | |
|---|
| | 573 | while (match != null) { |
|---|
| | 574 | var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1]; |
|---|
| | 575 | ctx = ctx[comp]; |
|---|
| | 576 | if (null == ctx || '' == match[3]) break; |
|---|
| | 577 | expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); |
|---|
| | 578 | match = pattern.exec(expr); |
|---|
| | 579 | } |
|---|
| | 580 | |
|---|
| | 581 | return before + String.interpret(ctx); |
|---|
| 305 | | }, |
|---|
| 306 | | |
|---|
| 307 | | all: function(iterator) { |
|---|
| | 600 | return this; |
|---|
| | 601 | }, |
|---|
| | 602 | |
|---|
| | 603 | eachSlice: function(number, iterator, context) { |
|---|
| | 604 | iterator = iterator ? iterator.bind(context) : Prototype.K; |
|---|
| | 605 | var index = -number, slices = [], array = this.toArray(); |
|---|
| | 606 | while ((index += number) < array.length) |
|---|
| | 607 | slices.push(array.slice(index, index+number)); |
|---|
| | 608 | return slices.collect(iterator, context); |
|---|
| | 609 | }, |
|---|
| | 610 | |
|---|
| | 611 | all: function(iterator, context) { |
|---|
| | 612 | iterator = iterator ? iterator.bind(context) : Prototype.K; |
|---|
| 472 | | if (iterable.toArray) { |
|---|
| 473 | | return iterable.toArray(); |
|---|
| 474 | | } else { |
|---|
| 475 | | var results = []; |
|---|
| 476 | | for (var i = 0; i < iterable.length; i++) |
|---|
| 477 | | results.push(iterable[i]); |
|---|
| | 809 | if (iterable.toArray) return iterable.toArray(); |
|---|
| | 810 | var length = iterable.length || 0, results = new Array(length); |
|---|
| | 811 | while (length--) results[length] = iterable[length]; |
|---|
| | 812 | return results; |
|---|
| | 813 | } |
|---|
| | 814 | |
|---|
| | 815 | if (Prototype.Browser.WebKit) { |
|---|
| | 816 | $A = function(iterable) { |
|---|
| | 817 | if (!iterable) return []; |
|---|
| | 818 | if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') && |
|---|
| | 819 | iterable.toArray) return iterable.toArray(); |
|---|
| | 820 | var length = iterable.length || 0, results = new Array(length); |
|---|
| | 821 | while (length--) results[length] = iterable[length]; |
|---|
| 550 | | var Hash = { |
|---|
| 551 | | _each: function(iterator) { |
|---|
| 552 | | for (var key in this) { |
|---|
| 553 | | var value = this[key]; |
|---|
| 554 | | if (typeof value == 'function') continue; |
|---|
| 555 | | |
|---|
| 556 | | var pair = [key, value]; |
|---|
| 557 | | pair.key = key; |
|---|
| 558 | | pair.value = value; |
|---|
| 559 | | iterator(pair); |
|---|
| 560 | | } |
|---|
| 561 | | }, |
|---|
| 562 | | |
|---|
| 563 | | keys: function() { |
|---|
| 564 | | return this.pluck('key'); |
|---|
| 565 | | }, |
|---|
| 566 | | |
|---|
| 567 | | values: function() { |
|---|
| 568 | | return this.pluck('value'); |
|---|
| 569 | | }, |
|---|
| 570 | | |
|---|
| 571 | | merge: function(hash) { |
|---|
| 572 | | return $H(hash).inject($H(this), function(mergedHash, pair) { |
|---|
| 573 | | mergedHash[pair.key] = pair.value; |
|---|
| 574 | | return mergedHash; |
|---|
| 575 | | }); |
|---|
| 576 | | }, |
|---|
| 577 | | |
|---|
| 578 | | toQueryString: function() { |
|---|
| 579 | | return this.map(function(pair) { |
|---|
| 580 | | return pair.map(encodeURIComponent).join('='); |
|---|
| 581 | | }).join('&'); |
|---|
| 582 | | }, |
|---|
| 583 | | |
|---|
| 584 | | inspect: function() { |
|---|
| 585 | | return '#<Hash:{' + this.map(function(pair) { |
|---|
| 586 | | return pair.map(Object.inspect).join(': '); |
|---|
| 587 | | }).join(', ') + '}>'; |
|---|
| 588 | | } |
|---|
| | 914 | |
|---|
| | 915 | // use native browser JS 1.6 implementation if available |
|---|
| | 916 | if (Object.isFunction(Array.prototype.forEach)) |
|---|
| | 917 | Array.prototype._each = Array.prototype.forEach; |
|---|
| | 918 | |
|---|
| | 919 | if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) { |
|---|
| | 920 | i || (i = 0); |
|---|
| | 921 | var length = this.length; |
|---|
| | 922 | if (i < 0) i = length + i; |
|---|
| | 923 | for (; i < length; i++) |
|---|
| | 924 | if (this[i] === item) return i; |
|---|
| | 925 | return -1; |
|---|
| | 926 | }; |
|---|
| | 927 | |
|---|
| | 928 | if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) { |
|---|
| | 929 | i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1; |
|---|
| | 930 | var n = this.slice(0, i).reverse().indexOf(item); |
|---|
| | 931 | return (n < 0) ? n : i - n - 1; |
|---|
| | 932 | }; |
|---|
| | 933 | |
|---|
| | 934 | Array.prototype.toArray = Array.prototype.clone; |
|---|
| | 935 | |
|---|
| | 936 | function $w(string) { |
|---|
| | 937 | if (!Object.isString(string)) return []; |
|---|
| | 938 | string = string.strip(); |
|---|
| | 939 | return string ? string.split(/\s+/) : []; |
|---|
| | 942 | if (Prototype.Browser.Opera){ |
|---|
| | 943 | Array.prototype.concat = function() { |
|---|
| | 944 | var array = []; |
|---|
| | 945 | for (var i = 0, length = this.length; i < length; i++) array.push(this[i]); |
|---|
| | 946 | for (var i = 0, length = arguments.length; i < length; i++) { |
|---|
| | 947 | if (Object.isArray(arguments[i])) { |
|---|
| | 948 | for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++) |
|---|
| | 949 | array.push(arguments[i][j]); |
|---|
| | 950 | } else { |
|---|
| | 951 | array.push(arguments[i]); |
|---|
| | 952 | } |
|---|
| | 953 | } |
|---|
| | 954 | return array; |
|---|
| | 955 | }; |
|---|
| | 956 | } |
|---|
| | 957 | Object.extend(Number.prototype, { |
|---|
| | 958 | toColorPart: function() { |
|---|
| | 959 | return this.toPaddedString(2, 16); |
|---|
| | 960 | }, |
|---|
| | 961 | |
|---|
| | 962 | succ: function() { |
|---|
| | 963 | return this + 1; |
|---|
| | 964 | }, |
|---|
| | 965 | |
|---|
| | 966 | times: function(iterator) { |
|---|
| | 967 | $R(0, this, true).each(iterator); |
|---|
| | 968 | return this; |
|---|
| | 969 | }, |
|---|
| | 970 | |
|---|
| | 971 | toPaddedString: function(length, radix) { |
|---|
| | 972 | var string = this.toString(radix || 10); |
|---|
| | 973 | return '0'.times(length - string.length) + string; |
|---|
| | 974 | }, |
|---|
| | 975 | |
|---|
| | 976 | toJSON: function() { |
|---|
| | 977 | return isFinite(this) ? this.toString() : 'null'; |
|---|
| | 978 | } |
|---|
| | 979 | }); |
|---|
| | 980 | |
|---|
| | 981 | $w('abs round ceil floor').each(function(method){ |
|---|
| | 982 | Number.prototype[method] = Math[method].methodize(); |
|---|
| | 983 | }); |
|---|
| 592 | | var hash = Object.extend({}, object || {}); |
|---|
| 593 | | Object.extend(hash, Enumerable); |
|---|
| 594 | | Object.extend(hash, Hash); |
|---|
| 595 | | return hash; |
|---|
| 596 | | } |
|---|
| 597 | | ObjectRange = Class.create(); |
|---|
| 598 | | Object.extend(ObjectRange.prototype, Enumerable); |
|---|
| 599 | | Object.extend(ObjectRange.prototype, { |
|---|
| | 985 | return new Hash(object); |
|---|
| | 986 | }; |
|---|
| | 987 | |
|---|
| | 988 | var Hash = Class.create(Enumerable, (function() { |
|---|
| | 989 | |
|---|
| | 990 | function toQueryPair(key, value) { |
|---|
| | 991 | if (Object.isUndefined(value)) return key; |
|---|
| | 992 | return key + '=' + encodeURIComponent(String.interpret(value)); |
|---|
| | 993 | } |
|---|
| | 994 | |
|---|
| | 995 | return { |
|---|
| | 996 | initialize: function(object) { |
|---|
| | 997 | this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); |
|---|
| | 998 | }, |
|---|
| | 999 | |
|---|
| | 1000 | _each: function(iterator) { |
|---|
| | 1001 | for (var key in this._object) { |
|---|
| | 1002 | var value = this._object[key], pair = [key, value]; |
|---|
| | 1003 | pair.key = key; |
|---|
| | 1004 | pair.value = value; |
|---|
| | 1005 | iterator(pair); |
|---|
| | 1006 | } |
|---|
| | 1007 | }, |
|---|
| | 1008 | |
|---|
| | 1009 | set: function(key, value) { |
|---|
| | 1010 | return this._object[key] = value; |
|---|
| | 1011 | }, |
|---|
| | 1012 | |
|---|
| | 1013 | get: function(key) { |
|---|
| | 1014 | return this._object[key]; |
|---|
| | 1015 | }, |
|---|
| | 1016 | |
|---|
| | 1017 | unset: function(key) { |
|---|
| | 1018 | var value = this._object[key]; |
|---|
| | 1019 | delete this._object[key]; |
|---|
| | 1020 | return value; |
|---|
| | 1021 | }, |
|---|
| | 1022 | |
|---|
| | 1023 | toObject: function() { |
|---|
| | 1024 | return Object.clone(this._object); |
|---|
| | 1025 | }, |
|---|
| | 1026 | |
|---|
| | 1027 | keys: function() { |
|---|
| | 1028 | return this.pluck('key'); |
|---|
| | 1029 | }, |
|---|
| | 1030 | |
|---|
| | 1031 | values: function() { |
|---|
| | 1032 | return this.pluck('value'); |
|---|
| | 1033 | }, |
|---|
| | 1034 | |
|---|
| | 1035 | index: function(value) { |
|---|
| | 1036 | var match = this.detect(function(pair) { |
|---|
| | 1037 | return pair.value === value; |
|---|
| | 1038 | }); |
|---|
| | 1039 | return match && match.key; |
|---|
| | 1040 | }, |
|---|
| | 1041 | |
|---|
| | 1042 | merge: function(object) { |
|---|
| | 1043 | return this.clone().update(object); |
|---|
| | 1044 | }, |
|---|
| | 1045 | |
|---|
| | 1046 | update: function(object) { |
|---|
| | 1047 | return new Hash(object).inject(this, function(result, pair) { |
|---|
| | 1048 | result.set(pair.key, pair.value); |
|---|
| | 1049 | return result; |
|---|
| | 1050 | }); |
|---|
| | 1051 | }, |
|---|
| | 1052 | |
|---|
| | 1053 | toQueryString: function() { |
|---|
| | 1054 | return this.map(function(pair) { |
|---|
| | 1055 | var key = encodeURIComponent(pair.key), values = pair.value; |
|---|
| | 1056 | |
|---|
| | 1057 | if (values && typeof values == 'object') { |
|---|
| | 1058 | if (Object.isArray(values)) |
|---|
| | 1059 | return values.map(toQueryPair.curry(key)).join('&'); |
|---|
| | 1060 | } |
|---|
| | 1061 | return toQueryPair(key, values); |
|---|
| | 1062 | }).join('&'); |
|---|
| | 1063 | }, |
|---|
| | 1064 | |
|---|
| | 1065 | inspect: function() { |
|---|
| | 1066 | return '#<Hash:{' + this.map(function(pair) { |
|---|
| | 1067 | return pair.map(Object.inspect).join(': '); |
|---|
| | 1068 | }).join(', ') + '}>'; |
|---|
| | 1069 | }, |
|---|
| | 1070 | |
|---|
| | 1071 | toJSON: function() { |
|---|
| | 1072 | return Object.toJSON(this.toObject()); |
|---|
| | 1073 | }, |
|---|
| | 1074 | |
|---|
| | 1075 | clone: function() { |
|---|
| | 1076 | return new Hash(this); |
|---|
| | 1077 | } |
|---|
| | 1078 | } |
|---|
| | 1079 | |
|---|