Provides

The heart of MooTools.

License:
MIT-style license.
Authors:
The MooTools production team (http://mootools.net/developers/)
  1. 24
  2. 25
  3. 26
  4. 27
  5. 28
  6. 29
(function(){ this.MooTools = { version: '1.3.1dev', build: '%build%' };

typeOf, instanceOf

  1. 33
  2. 34
  3. 35
  4. 36
  5. 37
  6. 38
  7. 39
  8. 40
  9. 41
  10. 42
  11. 43
  12. 44
  13. 45
  14. 46
  15. 47
  16. 48
  17. 49
  18. 50
  19. 51
  20. 52
  21. 53
  22. 54
  23. 55
  24. 56
var typeOf = this.typeOf = function(item){ if (item == null) return 'null'; if (item.$family) return item.$family(); if (item.nodeName){ if (item.nodeType == 1) return 'element'; if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace'; } else if (typeof item.length == 'number'){ if (item.callee) return 'arguments'; if ('item' in item) return 'collection'; } return typeof item; }; var instanceOf = this.instanceOf = function(item, object){ if (item == null) return false; var constructor = item.$constructor || item.constructor; while (constructor){ if (constructor === object) return true; constructor = constructor.parent; } return item instanceof object; };

Function overloading

  1. 60
  2. 61
  3. 62
  4. 63
  5. 64
  6. 65
  7. 66
  8. 67
  9. 68
  10. 69
  11. 70
  12. 71
  13. 72
  14. 73
  15. 74
  16. 75
  17. 76
  18. 77
  19. 78
  20. 79
  21. 80
  22. 81
  23. 82
  24. 83
  25. 84
  26. 85
  27. 86
  28. 87
  29. 88
  30. 89
  31. 90
  32. 91
  33. 92
  34. 93
  35. 94
  36. 95
  37. 96
  38. 97
  39. 98
  40. 99
  41. 100
  42. 101
  43. 102
  44. 103
  45. 104
  46. 105
var Function = this.Function; var enumerables = true; for (var i in {toString: 1}) enumerables = null; if (enumerables) enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor']; Function.prototype.overloadSetter = function(usePlural){ var self = this; return function(a, b){ if (a == null) return this; if (usePlural || typeof a != 'string'){ for (var k in a) self.call(this, k, a[k]); if (enumerables) for (var i = enumerables.length; i--;){ k = enumerables[i]; if (a.hasOwnProperty(k)) self.call(this, k, a[k]); } } else { self.call(this, a, b); } return this; }; }; Function.prototype.overloadGetter = function(usePlural){ var self = this; return function(a){ var args, result; if (usePlural || typeof a != 'string') args = a; else if (arguments.length > 1) args = arguments; if (args){ result = {}; for (var i = 0; i < args.length; i++) result[args[i]] = self.call(this, args[i]); } else { result = self.call(this, a); } return result; }; }; Function.prototype.extend = function(key, value){ this[key] = value; }.overloadSetter(); Function.prototype.implement = function(key, value){ this.prototype[key] = value; }.overloadSetter();

From

  1. 109
  2. 110
  3. 111
  4. 112
  5. 113
  6. 114
  7. 115
  8. 116
  9. 117
  10. 118
  11. 119
  12. 120
  13. 121
  14. 122
  15. 123
  16. 124
  17. 125
  18. 126
  19. 127
  20. 128
  21. 129
var slice = Array.prototype.slice; Function.from = function(item){ return (typeOf(item) == 'function') ? item : function(){ return item; }; }; Array.from = function(item){ if (item == null) return []; return (Type.isEnumerable(item) && typeof item != 'string') ? (typeOf(item) == 'array') ? item : slice.call(item) : [item]; }; Number.from = function(item){ var number = parseFloat(item); return isFinite(number) ? number : null; }; String.from = function(item){ return item + ''; };

hide, protect

  1. 133
  2. 134
  3. 135
  4. 136
  5. 137
  6. 138
  7. 139
  8. 140
  9. 141
  10. 142
  11. 143
  12. 144
  13. 145
Function.implement({ hide: function(){ this.$hidden = true; return this; }, protect: function(){ this.$protected = true; return this; } });

Type

  1. 149
  2. 150
  3. 151
  4. 152
  5. 153
  6. 154
  7. 155
  8. 156
  9. 157
  10. 158
  11. 159
  12. 160
var Type = this.Type = function(name, object){ if (name){ var lower = name.toLowerCase(); var typeCheck = function(item){ return (typeOf(item) == lower); }; Type['is' + name] = typeCheck; if (object != null){ object.prototype.$family = (function(){ return lower; }).hide();

<1.2compat>

  1. 162
object.type = typeCheck;

</1.2compat>

  1. 164
  2. 165
  3. 166
  4. 167
  5. 168
  6. 169
  7. 170
  8. 171
  9. 172
  10. 173
  11. 174
  12. 175
  13. 176
  14. 177
  15. 178
  16. 179
  17. 180
  18. 181
  19. 182
  20. 183
  21. 184
  22. 185
  23. 186
  24. 187
  25. 188
  26. 189
  27. 190
  28. 191
  29. 192
  30. 193
  31. 194
  32. 195
  33. 196
  34. 197
  35. 198
  36. 199
  37. 200
  38. 201
  39. 202
  40. 203
  41. 204
  42. 205
  43. 206
  44. 207
  45. 208
  46. 209
  47. 210
  48. 211
  49. 212
  50. 213
  51. 214
  52. 215
  53. 216
  54. 217
  55. 218
  56. 219
  57. 220
  58. 221
  59. 222
  60. 223
  61. 224
  62. 225
  63. 226
  64. 227
  65. 228
  66. 229
  67. 230
  68. 231
  69. 232
  70. 233
  71. 234
} } if (object == null) return null; object.extend(this); object.$constructor = Type; object.prototype.$constructor = object; return object; }; var toString = Object.prototype.toString; Type.isEnumerable = function(item){ return (item != null && typeof item.length == 'number' && toString.call(item) != '[object Function]' ); }; var hooks = {}; var hooksOf = function(object){ var type = typeOf(object.prototype); return hooks[type] || (hooks[type] = []); }; var implement = function(name, method){ if (method && method.$hidden) return this; var hooks = hooksOf(this); for (var i = 0; i < hooks.length; i++){ var hook = hooks[i]; if (typeOf(hook) == 'type') implement.call(hook, name, method); else hook.call(this, name, method); } var previous = this.prototype[name]; if (previous == null || !previous.$protected) this.prototype[name] = method; if (this[name] == null && typeOf(method) == 'function') extend.call(this, name, function(item){ return method.apply(item, slice.call(arguments, 1)); }); return this; }; var extend = function(name, method){ if (method && method.$hidden) return this; var previous = this[name]; if (previous == null || !previous.$protected) this[name] = method; return this; }; Type.implement({ implement: implement.overloadSetter(), extend: extend.overloadSetter(), alias: function(name, existing){ implement.call(this, name, this.prototype[existing]); }.overloadSetter(), mirror: function(hook){ hooksOf(this).push(hook); return this; } }); new Type('Type', Type);

Default Types

  1. 238
  2. 239
  3. 240
  4. 241
  5. 242
  6. 243
  7. 244
  8. 245
  9. 246
  10. 247
  11. 248
  12. 249
  13. 250
  14. 251
  15. 252
  16. 253
  17. 254
  18. 255
  19. 256
  20. 257
  21. 258
  22. 259
  23. 260
  24. 261
  25. 262
  26. 263
  27. 264
  28. 265
  29. 266
  30. 267
  31. 268
  32. 269
  33. 270
  34. 271
  35. 272
  36. 273
  37. 274
  38. 275
  39. 276
  40. 277
  41. 278
  42. 279
  43. 280
  44. 281
  45. 282
  46. 283
  47. 284
  48. 285
  49. 286
var force = function(name, object, methods){ var isType = (object != Object), prototype = object.prototype; if (isType) object = new Type(name, object); for (var i = 0, l = methods.length; i < l; i++){ var key = methods[i], generic = object[key], proto = prototype[key]; if (generic) generic.protect(); if (isType && proto){ delete prototype[key]; prototype[key] = proto.protect(); } } if (isType) object.implement(prototype); return force; }; force('String', String, [ 'charAt', 'charCodeAt', 'concat', 'indexOf', 'lastIndexOf', 'match', 'quote', 'replace', 'search', 'slice', 'split', 'substr', 'substring', 'toLowerCase', 'toUpperCase' ])('Array', Array, [ 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'concat', 'join', 'slice', 'indexOf', 'lastIndexOf', 'filter', 'forEach', 'every', 'map', 'some', 'reduce', 'reduceRight' ])('Number', Number, [ 'toExponential', 'toFixed', 'toLocaleString', 'toPrecision' ])('Function', Function, [ 'apply', 'call', 'bind' ])('RegExp', RegExp, [ 'exec', 'test' ])('Object', Object, [ 'create', 'defineProperty', 'defineProperties', 'keys', 'getPrototypeOf', 'getOwnPropertyDescriptor', 'getOwnPropertyNames', 'preventExtensions', 'isExtensible', 'seal', 'isSealed', 'freeze', 'isFrozen' ])('Date', Date, ['now']); Object.extend = extend.overloadSetter(); Date.extend('now', function(){ return +(new Date); }); new Type('Boolean', Boolean);

fixes NaN returning as Number

  1. 290
  2. 291
  3. 292
Number.prototype.$family = function(){ return isFinite(this) ? 'number' : 'null'; }.hide();

Number.random

  1. 296
  2. 297
  3. 298
Number.extend('random', function(min, max){ return Math.floor(Math.random() * (max - min + 1) + min); });

forEach, each

  1. 302
  2. 303
  3. 304
  4. 305
  5. 306
  6. 307
  7. 308
  8. 309
  9. 310
  10. 311
  11. 312
  12. 313
  13. 314
  14. 315
  15. 316
  16. 317
  17. 318
  18. 319
  19. 320
  20. 321
  21. 322
  22. 323
Object.extend('forEach', function(object, fn, bind){ for (var key in object){ if (object.hasOwnProperty(key)) fn.call(bind, object[key], key, object); } }); Object.each = Object.forEach; Array.implement({ forEach: function(fn, bind){ for (var i = 0, l = this.length; i < l; i++){ if (i in this) fn.call(bind, this[i], i, this); } }, each: function(fn, bind){ Array.forEach(this, fn, bind); return this; } });

Array & Object cloning, Object merging and appending

  1. 327
  2. 328
  3. 329
  4. 330
  5. 331
  6. 332
  7. 333
  8. 334
  9. 335
  10. 336
  11. 337
  12. 338
  13. 339
  14. 340
  15. 341
  16. 342
  17. 343
  18. 344
  19. 345
  20. 346
  21. 347
  22. 348
  23. 349
  24. 350
  25. 351
  26. 352
  27. 353
  28. 354
  29. 355
  30. 356
  31. 357
  32. 358
  33. 359
  34. 360
  35. 361
  36. 362
  37. 363
  38. 364
  39. 365
  40. 366
  41. 367
  42. 368
  43. 369
  44. 370
  45. 371
  46. 372
  47. 373
  48. 374
  49. 375
  50. 376
  51. 377
  52. 378
var cloneOf = function(item){ switch (typeOf(item)){ case 'array': return item.clone(); case 'object': return Object.clone(item); default: return item; } }; Array.implement('clone', function(){ var i = this.length, clone = new Array(i); while (i--) clone[i] = cloneOf(this[i]); return clone; }); var mergeOne = function(source, key, current){ switch (typeOf(current)){ case 'object': if (typeOf(source[key]) == 'object') Object.merge(source[key], current); else source[key] = Object.clone(current); break; case 'array': source[key] = current.clone(); break; default: source[key] = current; } return source; }; Object.extend({ merge: function(source, k, v){ if (typeOf(k) == 'string') return mergeOne(source, k, v); for (var i = 1, l = arguments.length; i < l; i++){ var object = arguments[i]; for (var key in object) mergeOne(source, key, object[key]); } return source; }, clone: function(object){ var clone = {}; for (var key in object) clone[key] = cloneOf(object[key]); return clone; }, append: function(original){ for (var i = 1, l = arguments.length; i < l; i++){ var extended = arguments[i] || {}; for (var key in extended) original[key] = extended[key]; } return original; } });

Object-less types

  1. 382
  2. 383
  3. 384
['Object', 'WhiteSpace', 'TextNode', 'Collection', 'Arguments'].each(function(name){ new Type(name); });

Unique ID

  1. 388
  2. 389
  3. 390
  4. 391
  5. 392
var UID = Date.now(); String.extend('uniqueID', function(){ return (UID++).toString(36); });

<1.2compat>

  1. 396
  2. 397
  3. 398
  4. 399
  5. 400
  6. 401
  7. 402
  8. 403
  9. 404
  10. 405
  11. 406
  12. 407
  13. 408
  14. 409
  15. 410
  16. 411
  17. 412
  18. 413
  19. 414
  20. 415
  21. 416
  22. 417
  23. 418
  24. 419
  25. 420
  26. 421
  27. 422
  28. 423
  29. 424
  30. 425
  31. 426
  32. 427
  33. 428
  34. 429
  35. 430
  36. 431
  37. 432
  38. 433
  39. 434
  40. 435
  41. 436
  42. 437
  43. 438
  44. 439
  45. 440
  46. 441
  47. 442
  48. 443
  49. 444
  50. 445
  51. 446
  52. 447
  53. 448
  54. 449
  55. 450
  56. 451
  57. 452
  58. 453
  59. 454
  60. 455
  61. 456
  62. 457
  63. 458
  64. 459
  65. 460
  66. 461
  67. 462
  68. 463
  69. 464
  70. 465
  71. 466
  72. 467
  73. 468
  74. 469
  75. 470
  76. 471
  77. 472
  78. 473
  79. 474
  80. 475
  81. 476
  82. 477
  83. 478
  84. 479
  85. 480
  86. 481
  87. 482
  88. 483
  89. 484
  90. 485
  91. 486
  92. 487
  93. 488
  94. 489
  95. 490
  96. 491
  97. 492
  98. 493
  99. 494
  100. 495
  101. 496
  102. 497
  103. 498
  104. 499
  105. 500
  106. 501
  107. 502
  108. 503
  109. 504
  110. 505
  111. 506
  112. 507
  113. 508
  114. 509
  115. 510
var Hash = this.Hash = new Type('Hash', function(object){ if (typeOf(object) == 'hash') object = Object.clone(object.getClean()); for (var key in object) this[key] = object[key]; return this; }); Hash.implement({ forEach: function(fn, bind){ Object.forEach(this, fn, bind); }, getClean: function(){ var clean = {}; for (var key in this){ if (this.hasOwnProperty(key)) clean[key] = this[key]; } return clean; }, getLength: function(){ var length = 0; for (var key in this){ if (this.hasOwnProperty(key)) length++; } return length; } }); Hash.alias('each', 'forEach'); Object.type = Type.isObject; var Native = this.Native = function(properties){ return new Type(properties.name, properties.initialize); }; Native.type = Type.type; Native.implement = function(objects, methods){ for (var i = 0; i < objects.length; i++) objects[i].implement(methods); return Native; }; var arrayType = Array.type; Array.type = function(item){ return instanceOf(item, Array) || arrayType(item); }; this.$A = function(item){ return Array.from(item).slice(); }; this.$arguments = function(i){ return function(){ return arguments[i]; }; }; this.$chk = function(obj){ return !!(obj || obj === 0); }; this.$clear = function(timer){ clearTimeout(timer); clearInterval(timer); return null; }; this.$defined = function(obj){ return (obj != null); }; this.$each = function(iterable, fn, bind){ var type = typeOf(iterable); ((type == 'arguments' || type == 'collection' || type == 'array' || type == 'elements') ? Array : Object).each(iterable, fn, bind); }; this.$empty = function(){}; this.$extend = function(original, extended){ return Object.append(original, extended); }; this.$H = function(object){ return new Hash(object); }; this.$merge = function(){ var args = Array.slice(arguments); args.unshift({}); return Object.merge.apply(null, args); }; this.$lambda = Function.from; this.$mixin = Object.merge; this.$random = Number.random; this.$splat = Array.from; this.$time = Date.now; this.$type = function(object){ var type = typeOf(object); if (type == 'elements') return 'array'; return (type == 'null') ? false : type; }; this.$unlink = function(object){ switch (typeOf(object)){ case 'object': return Object.clone(object); case 'array': return Array.clone(object); case 'hash': return new Hash(object); default: return object; } };

</1.2compat>

  1. 514
})();