Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
Separate out module mechanics
Browse files Browse the repository at this point in the history
  • Loading branch information
amccollum authored and ded committed Nov 13, 2013
1 parent be7f0fd commit a72cc6e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 169 deletions.
1 change: 1 addition & 0 deletions bridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window['$'] = window['ender'] = ender
220 changes: 60 additions & 160 deletions ender.js
Original file line number Diff line number Diff line change
@@ -1,166 +1,66 @@
/*!
* Ender: open module JavaScript framework (client-lib)
* copyright Dustin Diaz & Jacob Thornton 2011-2012 (@ded @fat)
* http://ender.jit.su
* http://enderjs.com
* License MIT
*/
(function (context, window, document) {

// a global object for node.js module compatiblity
// ============================================

context['global'] = context

// Implements simple module system
// loosely based on CommonJS Modules spec v1.1.1
// ============================================

var modules = {}
, old = context['$']
, oldEnder = context['ender']
, oldRequire = context['require']
, oldProvide = context['provide']

/**
* @param {string} name
*/
function require(name) {
// modules can be required from ender's build system, or found on the window
var module = modules['$' + name] || window[name]
if (!module) throw new Error("Ender Error: Requested module '" + name + "' has not been defined.")
return module
}

/**
* @param {string} name
* @param {*} what
*/
function provide(name, what) {
return (modules['$' + name] = what)
}

context['provide'] = provide
context['require'] = require

function aug(o, o2) {
for (var k in o2) k != 'noConflict' && k != '_VERSION' && (o[k] = o2[k])
return o
}

/**
* @param {*} o is an item to count
* @return {number|boolean}
*/
function count(o) {
if (typeof o != 'object' || !o || o.nodeType || o === window)
return false
return typeof (o = o.length) == 'number' && o === o ? o : false
}

/**
* @constructor
* @param {*=} item selector|node|collection|callback|anything
* @param {Object=} root node(s) from which to base selector queries
*/
function Ender(item, root) {
var i
this.length = 0 // Ensure that instance owns length

if (typeof item == 'string')
// Start @ strings so the result parlays into the other checks
// The .selector prop only applies to strings
item = ender['_select'](this['selector'] = item, root)

if (null == item)
return this // Do not wrap null|undefined

if (typeof item == 'function')
ender['_closure'](item, root)

// DOM node | scalar | not array-like
else if (false === (i = count(item)))
this[this.length++] = item

// Array-like - Bitwise ensures integer length:
else for (this.length = i = i > 0 ? i >> 0 : 0; i--;)
this[i] = item[i]
/**
* main Ender return object
* @constructor
* @param {Array|Node|string} s a CSS selector or DOM node(s)
* @param {Array.|Node} r a root node(s)
*/
function Ender(s, r) {
var elements
, i

this.selector = s
// string || node || nodelist || window
if (typeof s == 'undefined') {
elements = []
this.selector = ''
} else if (typeof s == 'string' || s.nodeName || (s.length && 'item' in s) || s == window) {
elements = ender._select(s, r)
} else {
elements = isFinite(s.length) ? s : [s]
}

/**
* @param {*=} item selector|node|collection|callback|anything
* @param {Object=} root node(s) from which to base selector queries
* @return {Ender}
*/
function ender(item, root) {
return new Ender(item, root)
}

ender['_VERSION'] = '0.4.x'

// Sync the prototypes for jQuery compatibility
ender['fn'] = ender.prototype = Ender.prototype

Ender.prototype['$'] = ender // handy reference to self

// dev tools secret sauce
Ender.prototype['splice'] = function () { throw new Error('Not implemented') }

/**
* @param {function(*, number, Ender)} fn
* @param {Object=} opt_scope
* @return {Ender}
*/
Ender.prototype['forEach'] = function (fn, opt_scope) {
var i, l
// opt out of native forEach so we can intentionally call our own scope
// defaulting to the current item and be able to return self
for (i = 0, l = this.length; i < l; ++i) i in this && fn.call(opt_scope || this[i], this[i], i, this)
// return self for chaining
return this
}

/**
* @param {Object|Function} o
* @param {boolean=} chain
*/
ender['ender'] = function (o, chain) {
aug(chain ? Ender.prototype : ender, o)
}

/**
* @param {string} s
* @param {Node=} r
*/
ender['_select'] = function (s, r) {
return s ? (r || document).querySelectorAll(s) : []
}

/**
* @param {Function} fn
*/
ender['_closure'] = function (fn) {
fn.call(document, ender)
}

/**
* @param {(boolean|Function)=} fn optional flag or callback
* To unclaim the global $, use no args. To unclaim *all* ender globals,
* use `true` or a callback that receives (require, provide, ender)
*/
ender['noConflict'] = function (fn) {
context['$'] = old
if (fn) {
context['provide'] = oldProvide
context['require'] = oldRequire
context['ender'] = oldEnder
typeof fn == 'function' && fn(require, provide, this)
}
return this
}

if (typeof module !== 'undefined' && module['exports']) module['exports'] = ender
// use subscript notation as extern for Closure compilation
// developers.google.com/closure/compiler/docs/api-tutorial3
context['ender'] = context['$'] = ender

}(this, window, document));
this.length = elements.length
for (i = this.length; i--;) this[i] = elements[i]
}

/**
* @param {function(el, i, inst)} fn
* @param {Object} opt_scope
* @returns {Ender}
*/
Ender.prototype['forEach'] = function (fn, opt_scope) {
var i, l
// opt out of native forEach so we can intentionally call our own scope
// defaulting to the current item and be able to return self
for (i = 0, l = this.length; i < l; ++i) i in this && fn.call(opt_scope || this[i], this[i], i, this)
// return self for chaining
return this
}

Ender.prototype.$ = ender // handy reference to self


function ender(s, r) {
return new Ender(s, r)
}

ender.fn = Ender.prototype // for easy compat to jQuery plugins

ender.ender = function (o, chain) {
var o2 = (chain ? Ender.prototype : ender)
for (var k in o) k != 'noConflict' && k != '_VERSION' && (o2[k] = o[k])
return o2
}

ender._select = function (s, r) {
if (typeof s == 'string') return (r || document).querySelectorAll(s)
if (s.nodeName) return [s]
return s
}

$ = ender
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"name": "ender-js"
, "description": "no-library library"
, "version": "0.5.0"
"name": "ender-core"
, "description": "core client library of Ender"
, "version": "1.0.0"
, "keywords": ["ender", "modules", "library", "framework", "packager"]
, "main": "./ender.js"
, "homepage": "http://ender.no.de"
, "ender": {
"bare": true
, "bridge": "./bridge.js"
}
, "homepage": "http://enderjs.com"
, "engines": {
"node": ">= 0.4.0"
}
, "authors": [
"Dustin Diaz <dustin@dustindiaz.com> (http://dustindiaz.com)"
, "Jacob Thornton <jacobthornton@gmail.com> (https://github.com/fat)"
, "Rod Vagg <rod@vagg.org> (https://github.com/rvagg)"
]
, "devDependencies": {
"smoosh": "*"
Expand All @@ -17,8 +23,4 @@
, "scripts": {
"make": "node make.js"
}
, "repository": {
"type": "git"
, "url": "https://github.com/ender-js/ender-js.git"
}
}

0 comments on commit a72cc6e

Please sign in to comment.