| 1 |
/* Adapted from https://github.com/Jakobo/PTClass */ |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright (c) 2005-2010 Sam Stephenson |
| 5 |
|
| 6 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
of this software and associated documentation files (the "Software"), to deal |
| 8 |
in the Software without restriction, including without limitation the rights |
| 9 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
copies of the Software, and to permit persons to whom the Software is |
| 11 |
furnished to do so, subject to the following conditions: |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. |
| 20 |
*/ |
| 21 |
/* Based on Alex Arnell's inheritance implementation. */ |
| 22 |
/** section: Language |
| 23 |
* class Class |
| 24 |
* |
| 25 |
* Manages Prototype's class-based OOP system. |
| 26 |
* |
| 27 |
* Refer to Prototype's web site for a [tutorial on classes and |
| 28 |
* inheritance](http://prototypejs.org/learn/class-inheritance). |
| 29 |
**/ |
| 30 |
(function(globalContext) { |
| 31 |
/* ------------------------------------ */ |
| 32 |
/* Import from object.js */ |
| 33 |
/* ------------------------------------ */ |
| 34 |
var _toString = Object.prototype.toString, |
| 35 |
NULL_TYPE = 'Null', |
| 36 |
UNDEFINED_TYPE = 'Undefined', |
| 37 |
BOOLEAN_TYPE = 'Boolean', |
| 38 |
NUMBER_TYPE = 'Number', |
| 39 |
STRING_TYPE = 'String', |
| 40 |
OBJECT_TYPE = 'Object', |
| 41 |
FUNCTION_CLASS = '[object Function]'; |
| 42 |
function isFunction(object) { |
| 43 |
return _toString.call(object) === FUNCTION_CLASS; |
| 44 |
} |
| 45 |
function extend(destination, source) { |
| 46 |
for (var property in source) if (source.hasOwnProperty(property)) // modify protect primitive slaughter |
| 47 |
destination[property] = source[property]; |
| 48 |
return destination; |
| 49 |
} |
| 50 |
function keys(object) { |
| 51 |
if (Type(object) !== OBJECT_TYPE) { throw new TypeError(); } |
| 52 |
var results = []; |
| 53 |
for (var property in object) { |
| 54 |
if (object.hasOwnProperty(property)) { |
| 55 |
results.push(property); |
| 56 |
} |
| 57 |
} |
| 58 |
return results; |
| 59 |
} |
| 60 |
function Type(o) { |
| 61 |
switch(o) { |
| 62 |
case null: return NULL_TYPE; |
| 63 |
case (void 0): return UNDEFINED_TYPE; |
| 64 |
} |
| 65 |
var type = typeof o; |
| 66 |
switch(type) { |
| 67 |
case 'boolean': return BOOLEAN_TYPE; |
| 68 |
case 'number': return NUMBER_TYPE; |
| 69 |
case 'string': return STRING_TYPE; |
| 70 |
} |
| 71 |
return OBJECT_TYPE; |
| 72 |
} |
| 73 |
function isUndefined(object) { |
| 74 |
return typeof object === "undefined"; |
| 75 |
} |
| 76 |
/* ------------------------------------ */ |
| 77 |
/* Import from Function.js */ |
| 78 |
/* ------------------------------------ */ |
| 79 |
var slice = Array.prototype.slice; |
| 80 |
function argumentNames(fn) { |
| 81 |
var names = fn.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1] |
| 82 |
.replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '') |
| 83 |
.replace(/\s+/g, '').split(','); |
| 84 |
return names.length == 1 && !names[0] ? [] : names; |
| 85 |
} |
| 86 |
function wrap(fn, wrapper) { |
| 87 |
var __method = fn; |
| 88 |
return function() { |
| 89 |
var a = update([bind(__method, this)], arguments); |
| 90 |
return wrapper.apply(this, a); |
| 91 |
} |
| 92 |
} |
| 93 |
function update(array, args) { |
| 94 |
var arrayLength = array.length, length = args.length; |
| 95 |
while (length--) array[arrayLength + length] = args[length]; |
| 96 |
return array; |
| 97 |
} |
| 98 |
function merge(array, args) { |
| 99 |
array = slice.call(array, 0); |
| 100 |
return update(array, args); |
| 101 |
} |
| 102 |
function bind(fn, context) { |
| 103 |
if (arguments.length < 2 && isUndefined(arguments[0])) return this; |
| 104 |
var __method = fn, args = slice.call(arguments, 2); |
| 105 |
return function() { |
| 106 |
var a = merge(args, arguments); |
| 107 |
return __method.apply(context, a); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/* ------------------------------------ */ |
| 112 |
/* Import from Prototype.js */ |
| 113 |
/* ------------------------------------ */ |
| 114 |
var emptyFunction = function(){}; |
| 115 |
|
| 116 |
var Class = (function() { |
| 117 |
|
| 118 |
// Some versions of JScript fail to enumerate over properties, names of which |
| 119 |
// correspond to non-enumerable properties in the prototype chain |
| 120 |
var IS_DONTENUM_BUGGY = (function(){ |
| 121 |
for (var p in { toString: 1 }) { |
| 122 |
// check actual property name, so that it works with augmented Object.prototype |
| 123 |
if (p === 'toString') return false; |
| 124 |
} |
| 125 |
return true; |
| 126 |
})(); |
| 127 |
|
| 128 |
function subclass() {}; |
| 129 |
function create() { |
| 130 |
var parent = null, properties = [].slice.apply(arguments); |
| 131 |
if (isFunction(properties[0])) |
| 132 |
parent = properties.shift(); |
| 133 |
|
| 134 |
function klass() { |
| 135 |
this.initialize.apply(this, arguments); |
| 136 |
} |
| 137 |
|
| 138 |
extend(klass, Class.Methods); |
| 139 |
klass.superclass = parent; |
| 140 |
klass.subclasses = []; |
| 141 |
|
| 142 |
if (parent) { |
| 143 |
subclass.prototype = parent.prototype; |
| 144 |
klass.prototype = new subclass; |
| 145 |
try { parent.subclasses.push(klass) } catch(e) {} |
| 146 |
} |
| 147 |
|
| 148 |
for (var i = 0, length = properties.length; i < length; i++) |
| 149 |
klass.addMethods(properties[i]); |
| 150 |
|
| 151 |
if (!klass.prototype.initialize) |
| 152 |
klass.prototype.initialize = emptyFunction; |
| 153 |
|
| 154 |
klass.prototype.constructor = klass; |
| 155 |
return klass; |
| 156 |
} |
| 157 |
|
| 158 |
function addMethods(source) { |
| 159 |
var ancestor = this.superclass && this.superclass.prototype, |
| 160 |
properties = keys(source); |
| 161 |
|
| 162 |
// IE6 doesn't enumerate `toString` and `valueOf` (among other built-in `Object.prototype`) properties, |
| 163 |
// Force copy if they're not Object.prototype ones. |
| 164 |
// Do not copy other Object.prototype.* for performance reasons |
| 165 |
if (IS_DONTENUM_BUGGY) { |
| 166 |
if (source.toString != Object.prototype.toString) |
| 167 |
properties.push("toString"); |
| 168 |
if (source.valueOf != Object.prototype.valueOf) |
| 169 |
properties.push("valueOf"); |
| 170 |
} |
| 171 |
|
| 172 |
for (var i = 0, length = properties.length; i < length; i++) { |
| 173 |
var property = properties[i], value = source[property]; |
| 174 |
if (ancestor && isFunction(value) && |
| 175 |
argumentNames(value)[0] == "$super") { |
| 176 |
var method = value; |
| 177 |
value = wrap((function(m) { |
| 178 |
return function() { return ancestor[m].apply(this, arguments); }; |
| 179 |
})(property), method); |
| 180 |
|
| 181 |
value.valueOf = bind(method.valueOf, method); |
| 182 |
value.toString = bind(method.toString, method); |
| 183 |
} |
| 184 |
this.prototype[property] = value; |
| 185 |
} |
| 186 |
|
| 187 |
return this; |
| 188 |
} |
| 189 |
|
| 190 |
return { |
| 191 |
create: create, |
| 192 |
Methods: { |
| 193 |
addMethods: addMethods |
| 194 |
} |
| 195 |
}; |
| 196 |
})(); |
| 197 |
|
| 198 |
if (globalContext.exports) { |
| 199 |
globalContext.exports.Class = Class; |
| 200 |
} |
| 201 |
else { |
| 202 |
globalContext.Class = Class; |
| 203 |
} |
| 204 |
})(Rickshaw); |