PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/ajax/libs/es-class/1.1.0/es-class.max.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 426 lines | 311 code | 31 blank | 84 comment | 67 complexity | 4e0cbfc16ee5ef991544cdb6e6bb2ee4 MD5 | raw file
  1. /*!
  2. Copyright (C) 2014 by Andrea Giammarchi - @WebReflection
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. var Class = Class || (function (Object) {
  20. 'use strict';
  21. /*! (C) Andrea Giammarchi - MIT Style License */
  22. var
  23. // shortcuts for minifiers and ES3 private keywords too
  24. CONSTRUCTOR = 'constructor',
  25. EXTENDS = 'extends',
  26. IMPLEMENTS = 'implements',
  27. INIT = 'init',
  28. PROTOTYPE = 'prototype',
  29. STATIC = 'static',
  30. SUPER = 'super',
  31. WITH = 'with',
  32. // infamous property used as fallback
  33. // for IE8 and lower only
  34. PROTO = '__proto__',
  35. // used to copy non enumerable properties on IE
  36. nonEnumerables = [
  37. 'hasOwnProperty',
  38. 'isPrototypeOf',
  39. 'propertyIsEnumerable',
  40. 'toLocaleString',
  41. 'toString',
  42. 'valueOf'
  43. ],
  44. // IE < 9 bug only
  45. hasIEEnumerableBug = !{valueOf:0}[nonEnumerables[2]](nonEnumerables[5]),
  46. hOP = Object[nonEnumerables[0]],
  47. // basic ad-hoc private fallback for old browsers
  48. // use es5-shim if you want a properly patched polyfill
  49. create = Object.create || function (proto) {
  50. /*jshint newcap: false */
  51. var isInstance = this instanceof create;
  52. create[PROTOTYPE] = isInstance ? createPrototype : proto;
  53. return isInstance ? this : new create();
  54. },
  55. // very old browsers actually work better
  56. // without assigning null as prototype
  57. createPrototype = create[PROTOTYPE],
  58. // redefined if not present
  59. defineProperty = Object.defineProperty,
  60. // redefined if not present
  61. gOPD = Object.getOwnPropertyDescriptor,
  62. // basic ad-hoc private fallback for old browsers
  63. // use es5-shim if you want a properly patched polyfill
  64. gOPN = Object.getOwnPropertyNames || function (object) {
  65. var names = [], i, key;
  66. for (key in object) {
  67. if (hOP.call(object, key)) {
  68. names.push(key);
  69. }
  70. }
  71. if (hasIEEnumerableBug) {
  72. for (i = 0; i < nonEnumerables.length; i++) {
  73. key = nonEnumerables[i];
  74. if (hOP.call(object, key)) {
  75. names.push(key);
  76. }
  77. }
  78. }
  79. return names;
  80. },
  81. // needed to verify the existence
  82. getPrototypeOf = Object.getPrototypeOf,
  83. // needed to allow Classes as traits
  84. gPO = getPrototypeOf || function (o) {
  85. return o[PROTO] || null;
  86. },
  87. // used to avoid setting `arguments` and other function properties
  88. // when public static are copied over
  89. nativeFunctionOPN = new RegExp('^(?:' + gOPN(function () {}).join('|') + ')$'),
  90. superRegExp = /\bsuper\b/.test(function () {
  91. // this test should never be minifier sensistive
  92. this['super']();
  93. }) ? /\bsuper\b/ : /.*/
  94. // In 2010 Opera 10.5 for Linux Debian 6
  95. // goes nut with methods to string representation,
  96. // truncating pieces of text in an unpredictable way.
  97. // If you are targeting such browser
  98. // be aware that super invocation might fail.
  99. // This is the only exception I could find
  100. // from year 2000 to modern days browsers
  101. // plus everything else would work just fine.
  102. ;
  103. // verified broken IE8 or older browsers
  104. try {
  105. defineProperty({}, '{}', {});
  106. } catch(o_O) {
  107. if ('__defineGetter__' in {}) {
  108. defineProperty = function (object, name, descriptor) {
  109. if (hOP.call(descriptor, 'value')) {
  110. object[name] = descriptor.value;
  111. } else {
  112. if (hOP.call(descriptor, 'get')) {
  113. object.__defineGetter__(name, descriptor.get);
  114. }
  115. if (hOP.call(descriptor, 'set')) {
  116. object.__defineSetter__(name, descriptor.set);
  117. }
  118. }
  119. return object;
  120. };
  121. gOPD = function (object, key) {
  122. var
  123. get = object.__lookupGetter__(key),
  124. set = object.__lookupSetter__(key),
  125. descriptor = {}
  126. ;
  127. if (get || set) {
  128. if (get) {
  129. descriptor.get = get;
  130. }
  131. if (set) {
  132. descriptor.set = set;
  133. }
  134. } else {
  135. descriptor.value = object[key];
  136. }
  137. return descriptor;
  138. };
  139. } else {
  140. defineProperty = function (object, name, descriptor) {
  141. object[name] = descriptor.value;
  142. return object;
  143. };
  144. gOPD = function (object, key) {
  145. return {value: object[key]};
  146. };
  147. }
  148. }
  149. // copy all imported enumerable methods and properties
  150. function addMixins(mixins, target, inherits) {
  151. for (var
  152. source,
  153. init = [],
  154. i = 0; i < mixins.length; i++
  155. ) {
  156. source = transformMixin(mixins[i]);
  157. if (hOP.call(source, INIT)) {
  158. init.push(source[INIT]);
  159. }
  160. copyOwn(source, target, inherits, false, false);
  161. }
  162. return init;
  163. }
  164. // configure source own properties in the target
  165. function copyOwn(source, target, inherits, publicStatic, allowInit) {
  166. for (var
  167. key,
  168. noFunctionCheck = typeof source !== 'function',
  169. names = gOPN(source),
  170. i = 0; i < names.length; i++
  171. ) {
  172. key = names[i];
  173. if (
  174. (noFunctionCheck || !nativeFunctionOPN.test(key)) &&
  175. isNotASpecialKey(key, allowInit)
  176. ) {
  177. if (hOP.call(target, key)) {
  178. warn('duplicated: ' + key);
  179. }
  180. setProperty(inherits, target, key, gOPD(source, key), publicStatic);
  181. }
  182. }
  183. }
  184. // return the right constructor analyzing the parent.
  185. // if the parent is empty there is no need to call it.
  186. function createConstructor(hasParentPrototype, parent) {
  187. var Class = function Class() {};
  188. return hasParentPrototype && ('' + parent) !== ('' + Class) ?
  189. function Class() {
  190. return parent.apply(this, arguments);
  191. } :
  192. Class
  193. ;
  194. }
  195. // common defineProperty wrapper
  196. function define(target, key, value, publicStatic) {
  197. var configurable = isConfigurable(key, publicStatic);
  198. defineProperty(target, key, {
  199. enumerable: false, // was: publicStatic,
  200. configurable: configurable,
  201. writable: configurable,
  202. value: value
  203. });
  204. }
  205. // is key is UPPER_CASE and the property is public static
  206. // it will define the property as non configurable and non writable
  207. function isConfigurable(key, publicStatic) {
  208. return publicStatic ? !/^[A-Z_]+$/.test(key) : true;
  209. }
  210. // verifies a key is not special for the class
  211. function isNotASpecialKey(key, allowInit) {
  212. return key !== CONSTRUCTOR &&
  213. key !== EXTENDS &&
  214. key !== IMPLEMENTS &&
  215. // Blackberry 7 and old WebKit bug only:
  216. // user defined functions have
  217. // enumerable prototype and constructor
  218. key !== PROTOTYPE &&
  219. key !== STATIC &&
  220. key !== SUPER &&
  221. key !== WITH &&
  222. (allowInit || key !== INIT);
  223. }
  224. // will eventually convert classes or constructors
  225. // into trait objects, before assigning them as such
  226. function transformMixin(trait) {
  227. if (typeof trait === 'object') return trait;
  228. if (trait.length) {
  229. warn((trait.name || 'Class') + ' should not expect arguments');
  230. }
  231. for (var
  232. i, key, keys,
  233. object = {init: trait},
  234. proto = trait.prototype;
  235. proto && proto !== Object.prototype;
  236. proto = gPO(proto)
  237. ) {
  238. for (i = 0, keys = gOPN(proto); i < keys.length; i++) {
  239. key = keys[i];
  240. if (isNotASpecialKey(key, false) && !hOP.call(object, key)) {
  241. defineProperty(object, key, gOPD(proto, key));
  242. }
  243. }
  244. }
  245. return object;
  246. }
  247. // set a property via defineProperty using a common descriptor
  248. // only if properties where not defined yet.
  249. // If publicStatic is true, properties are both non configurable and non writable
  250. function setProperty(inherits, target, key, descriptor, publicStatic) {
  251. var
  252. hasValue = hOP.call(descriptor, 'value'),
  253. configurable,
  254. value
  255. ;
  256. if (publicStatic) {
  257. if (hOP.call(target, key)) {
  258. return;
  259. }
  260. } else if (hasValue) {
  261. value = descriptor.value;
  262. if (typeof value === 'function' && superRegExp.test(value)) {
  263. descriptor.value = wrap(inherits, key, value, publicStatic);
  264. }
  265. } else {
  266. wrapGetOrSet(inherits, key, descriptor, 'get');
  267. wrapGetOrSet(inherits, key, descriptor, 'set');
  268. }
  269. configurable = isConfigurable(key, publicStatic);
  270. descriptor.enumerable = false; // was: publicStatic;
  271. descriptor.configurable = configurable;
  272. if (hasValue) {
  273. descriptor.writable = configurable;
  274. }
  275. defineProperty(target, key, descriptor);
  276. }
  277. // basic check against expected properties or methods
  278. // used when `implements` is used
  279. function verifyImplementations(interfaces, target) {
  280. for (var
  281. current,
  282. key,
  283. i = 0; i < interfaces.length; i++
  284. ) {
  285. current = interfaces[i];
  286. for (key in current) {
  287. if (hOP.call(current, key) && !hOP.call(target, key)) {
  288. warn(key + ' is not implemented');
  289. }
  290. }
  291. }
  292. }
  293. // warn if something doesn't look right
  294. // such overwritten public statics
  295. // or traits / mixins assigning twice same thing
  296. function warn(message) {
  297. try {
  298. console.warn(message);
  299. } catch(meh) {
  300. /*\_(ツ)_*/
  301. }
  302. }
  303. // lightweight wrapper for methods that requires
  304. // .super(...) invokaction - inspired by old klass.js
  305. function wrap(inherits, key, method, publicStatic) {
  306. return function () {
  307. if (!hOP.call(this, SUPER)) {
  308. // define it once in order to use
  309. // fast assignment every other time
  310. define(this, SUPER, null, publicStatic);
  311. }
  312. var
  313. previous = this[SUPER],
  314. current = (this[SUPER] = inherits[key]),
  315. result = method.apply(this, arguments)
  316. ;
  317. this[SUPER] = previous;
  318. return result;
  319. };
  320. }
  321. // get/set shortcut for the eventual wrapper
  322. function wrapGetOrSet(inherits, key, descriptor, gs, publicStatic) {
  323. if (hOP.call(descriptor, gs) && superRegExp.test(descriptor[gs])) {
  324. descriptor[gs] = wrap(
  325. gOPD(inherits, key),
  326. gs,
  327. descriptor[gs],
  328. publicStatic
  329. );
  330. }
  331. }
  332. // the actual Class({ ... }) definition
  333. return function (description) {
  334. var
  335. hasConstructor = hOP.call(description, CONSTRUCTOR),
  336. hasParent = hOP.call(description, EXTENDS),
  337. parent = hasParent && description[EXTENDS],
  338. hasParentPrototype = hasParent && typeof parent === 'function',
  339. inherits = hasParentPrototype ? parent[PROTOTYPE] : parent,
  340. constructor = hasConstructor ?
  341. description[CONSTRUCTOR] :
  342. createConstructor(hasParentPrototype, parent),
  343. hasSuper = hasParent && hasConstructor && superRegExp.test(constructor),
  344. prototype = hasParent ? create(inherits) : constructor[PROTOTYPE],
  345. mixins,
  346. length
  347. ;
  348. if (hasSuper) {
  349. constructor = wrap(inherits, CONSTRUCTOR, constructor, false);
  350. }
  351. // add modules/mixins (that might swap the constructor)
  352. if (hOP.call(description, WITH)) {
  353. mixins = addMixins([].concat(description[WITH]), prototype, inherits);
  354. length = mixins.length;
  355. if (length) {
  356. constructor = (function (parent) {
  357. return function () {
  358. var i = 0;
  359. while (i < length) mixins[i++].call(this);
  360. return parent.apply(this, arguments);
  361. };
  362. }(constructor));
  363. constructor[PROTOTYPE] = prototype;
  364. }
  365. }
  366. if (hOP.call(description, STATIC)) {
  367. // add new public static properties first
  368. copyOwn(description[STATIC], constructor, inherits, true, true);
  369. }
  370. if (hasParent) {
  371. // in case it's a function
  372. if (parent !== inherits) {
  373. // copy possibly inherited statics too
  374. copyOwn(parent, constructor, inherits, true, true);
  375. }
  376. constructor[PROTOTYPE] = prototype;
  377. }
  378. if (prototype[CONSTRUCTOR] !== constructor) {
  379. define(prototype, CONSTRUCTOR, constructor, false);
  380. }
  381. // enrich the prototype
  382. copyOwn(description, prototype, inherits, false, true);
  383. if (hOP.call(description, IMPLEMENTS)) {
  384. verifyImplementations([].concat(description[IMPLEMENTS]), prototype);
  385. }
  386. if (hasParent && !getPrototypeOf) {
  387. define(prototype, PROTO, inherits, false);
  388. }
  389. return constructor;
  390. };
  391. }(Object));