UNPKG

minow

Version:

A drop in replacement for jquery based off enders core libs

347 lines (293 loc) 11.3 kB
define([ // Libraries. "bean", "bonzo", "qwery", "reqwest", "app/bower_components/greensock/src/uncompressed/TweenLite", "app/bower_components/greensock/src/uncompressed/plugins/CSSPlugin" ], function(bean, bonzo, qwery, reqwest) { var module = {}, slice = Array.prototype.slice, breaker = {}, ArrayProto = Array.prototype, nativeForEach = ArrayProto.forEach; var each = function(obj, iterator, context) { if (obj == null) return; if (nativeForEach && obj.forEach === nativeForEach) { obj.forEach(iterator, context); } else if (obj.length === +obj.length) { for (var i = 0, l = obj.length; i < l; i++) { if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return; } } else { for (var key in obj) { if (has(obj, key)) { if (iterator.call(context, obj[key], key, obj) === breaker) return; } } } }; var indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; }; Object.keys = Object.keys || (function() { var hasOwnProperty = Object.prototype.hasOwnProperty, hasDontEnumBug = !{ toString: null }.propertyIsEnumerable("toString"), DontEnums = [ 'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'constructor' ], DontEnumsLength = DontEnums.length; return function(o) { if (typeof o != "object" && typeof o != "function" || o === null) throw new TypeError("Object.keys called on a non-object"); var result = []; for (var name in o) { if (hasOwnProperty.call(o, name)) result.push(name); } if (hasDontEnumBug) { for (var i = 0; i < DontEnumsLength; i++) { if (hasOwnProperty.call(o, DontEnums[i])) result.push(DontEnums[i]); } } return result; }; })(); // Attach events API to the prototype of DOM wrapper: var aug = {}; var evtArr = ['on', 'off', 'one', 'add', 'fire', 'clone']; each(evtArr, function(k) { aug[k] = function() { for (var i = 0; i < this.length; i += 1) { bean[k].apply(this, [this[i]].concat(slice.call(arguments))); } return this; }; }); aug.hover = function(enter, leave) { for (var i = 0; i < this.length; i += 1) { bean.on.call(this, this[i], 'mouseenter', enter); bean.on.call(this, this[i], 'mouseleave', leave); } return this; }; var aliases = { 'fire': ['emit', 'trigger'], 'on': ['addListener', 'listen', 'bind'], 'add': ['delegate'], 'off': ['unbind', 'unlisten', 'removeListener', 'undelegate'], 'clone': ['cloneEvents'] }; each(Object.keys(aliases), function(key) { each(aliases[key], function(alias) { aug[alias] = aug[key]; }); }); var shorts = [ 'blur', 'change', 'click', 'dblclick', 'error', 'focus', 'focusin', 'focusout', 'keydown', 'keypress', 'keyup', 'load', 'mousedown', 'mouseenter', 'mouseleave', 'mouseout', 'mouseover', 'mouseup', 'mousemove', 'resize', 'scroll', 'select', 'submit', 'unload' ]; each(shorts, function(s) { aug[s] = function() { for (var i = 0; i < this.length; i += 1) { bean.on.apply(this, [this[i], s].concat(slice.call(arguments))); } return this; }; }); function uniq(ar) { var r = [], i = 0, j = 0, k, item, inIt for (; item = ar[i]; ++i) { inIt = false for (k = 0; k < r.length; ++k) { if (r[k] === item) { inIt = true; break } } if (!inIt) r[j++] = item } return r } //exposed BONZO to add methods bonzo.aug({ find: function(s) { var r = [], i, l, j, k, els for (i = 0, l = this.length; i < l; i++) { els = qwery(s, this[i]) for (j = 0, k = els.length; j < k; j++) r.push(els[j]) } return $(qwery.uniq(r)) }, parents: function(selector, closest) { //console.log("collection: ",$(selector), selector); var collection = $(selector), j, k, p, r = []; for (j = 0, k = this.length; j < k; j++) { p = this[j]; while (p = p.parentNode) { if (indexOf(collection, p) !== -1) { r.push(p); if (closest) break; } } } return $(qwery.uniq(r)); }, siblings: function() { var i, l, p, r = [] for (i = 0, l = this.length; i < l; i++) { p = this[i] while (p = p.previousSibling) p.nodeType == 1 && r.push(p) p = this[i] while (p = p.nextSibling) p.nodeType == 1 && r.push(p) } return $(r) }, children: function() { var i, el, r = [] for (i = 0, l = this.length; i < l; i++) { if (!(el = bonzo.firstChild(this[i]))) continue; r.push(el) while (el = el.nextSibling) el.nodeType == 1 && r.push(el) } return $(uniq(r)) }, closest: function(selector) { return this.parents(selector, true); }, height: function(v) { return dimension(v, this, 'height') }, width: function(v) { return dimension(v, this, 'width') }, animate: function(options) { return TweenLite.to(this, 0.5, options) }, fadeIn: function(d, fn) { return TweenLite.to(this[0], d, { css: { autoAlpha: 1 }, onComplete: fn }) }, fadeOut: function(d, fn) { return TweenLite.to(this[0], d, { css: { autoAlpha: 0 }, onComplete: fn }) }, outerWidth: function(margin) { var fp = parseFloat; return fp(this.width()) + (margin ? fp(this.css('margin-left')) + fp(this.css('margin-right')) : 0) + fp(this.css('padding-left')) + fp(this.css('padding-right')) + fp(this.css('border-left-width')) + fp(this.css('border-right-width')); }, tween: TweenLite.to }); function dimension(v, self, which) { return v ? self.css(which, v) : function(r) { r = parseInt(self.css(which), 10); return isNaN(r) ? self['offset' + which.replace(/^\w/, function(m) { return m.toUpperCase() })] : r }() } bonzo.aug(aug); var $ = function(selector) { return bonzo(qwery(selector)); }; bonzo.setQueryEngine(qwery); bean.setSelectorEngine(qwery); $.ajax = reqwest; each(['post', 'put', 'get', 'del', 'patch', 'head'], function(m) { $[m] = reqwest[m]; }); $.plugin = function(name, object, selector) { bonzo.aug(object); }; $.extend = function() { var opt, tmpName, src, copy, target = args[0] || {}; if (typeof target !== "object" && typeof target !== "function") { target = {}; } for (var i = 1, len = args.length; i < len; i++) { if ((opt = args[i]) != null) { for (tmpName in opt) { src = target[tmpName]; copy = opt[tmpName]; if (target === copy) { continue; } if (copy !== undefined) { target[tmpName] = copy; } } } } // Return the modified object return target; }; var trimReplace = /(^\s*|\s*$)/g, rvalidchars = /^[\],:{}\s]*$/, rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g; var trim = String.prototype.trim ? function(s) { return s.trim() } : function(s) { return s.replace(trimReplace, '') }; $.fEach = $.each = each; $.parseJSON = function(data) { if (typeof data !== "string" || !data) { return null; } // Make sure leading/trailing whitespace is removed (IE can't handle it) data = trim(data); // Attempt to parse using the native JSON parser first if (window.JSON && window.JSON.parse) { return window.JSON.parse(data); } // Make sure the incoming data is actual JSON // Logic borrowed from http://json.org/json2.js if (rvalidchars.test(data.replace(rvalidescape, "@") .replace(rvalidtokens, "]") .replace(rvalidbraces, ""))) { return (new Function("return " + data))(); } console.log("Invalid JSON: " + data); }; module.exports = $; return $; });