123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. * Ender: open module JavaScript framework (client-lib)
  3. * copyright Dustin Diaz & Jacob Thornton 2011-2012 (@ded @fat)
  4. * http://ender.no.de
  5. * License MIT
  6. */
  7. (function (context) {
  8. // a global object for node.js module compatiblity
  9. // ============================================
  10. context['global'] = context
  11. // Implements simple module system
  12. // losely based on CommonJS Modules spec v1.1.1
  13. // ============================================
  14. var modules = {}
  15. , old = context['$']
  16. , oldRequire = context['require']
  17. , oldProvide = context['provide']
  18. function require (identifier) {
  19. // modules can be required from ender's build system, or found on the window
  20. var module = modules['$' + identifier] || window[identifier]
  21. if (!module) throw new Error("Ender Error: Requested module '" + identifier + "' has not been defined.")
  22. return module
  23. }
  24. function provide (name, what) {
  25. return (modules['$' + name] = what)
  26. }
  27. context['provide'] = provide
  28. context['require'] = require
  29. function aug(o, o2) {
  30. for (var k in o2) k != 'noConflict' && k != '_VERSION' && (o[k] = o2[k])
  31. return o
  32. }
  33. /**
  34. * main Ender return object
  35. * @constructor
  36. * @param {Array|Node|string} s a CSS selector or DOM node(s)
  37. * @param {Array.|Node} r a root node(s)
  38. */
  39. function Ender(s, r) {
  40. var elements
  41. , i
  42. this.selector = s
  43. // string || node || nodelist || window
  44. if (typeof s == 'undefined') {
  45. elements = []
  46. this.selector = ''
  47. } else if (typeof s == 'string' || s.nodeName || (s.length && 'item' in s) || s == window) {
  48. elements = ender._select(s, r)
  49. } else {
  50. elements = isFinite(s.length) ? s : [s]
  51. }
  52. this.length = elements.length
  53. for (i = this.length; i--;) this[i] = elements[i]
  54. }
  55. /**
  56. * @param {function(el, i, inst)} fn
  57. * @param {Object} opt_scope
  58. * @returns {Ender}
  59. */
  60. Ender.prototype['forEach'] = function (fn, opt_scope) {
  61. var i, l
  62. // opt out of native forEach so we can intentionally call our own scope
  63. // defaulting to the current item and be able to return self
  64. for (i = 0, l = this.length; i < l; ++i) i in this && fn.call(opt_scope || this[i], this[i], i, this)
  65. // return self for chaining
  66. return this
  67. }
  68. Ender.prototype.$ = ender // handy reference to self
  69. function ender(s, r) {
  70. return new Ender(s, r)
  71. }
  72. ender['_VERSION'] = '0.4.3-dev'
  73. ender.fn = Ender.prototype // for easy compat to jQuery plugins
  74. ender.ender = function (o, chain) {
  75. aug(chain ? Ender.prototype : ender, o)
  76. }
  77. ender._select = function (s, r) {
  78. if (typeof s == 'string') return (r || document).querySelectorAll(s)
  79. if (s.nodeName) return [s]
  80. return s
  81. }
  82. // use callback to receive Ender's require & provide
  83. ender.noConflict = function (callback) {
  84. context['$'] = old
  85. if (callback) {
  86. context['provide'] = oldProvide
  87. context['require'] = oldRequire
  88. callback(require, provide, this)
  89. }
  90. return this
  91. }
  92. if (typeof module !== 'undefined' && module.exports) module.exports = ender
  93. // use subscript notation as extern for Closure compilation
  94. context['ender'] = context['$'] = context['ender'] || ender
  95. }(this));