Debian Codegraph @

tests/ender.js

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