From 514df99ce102d24d3b920f55c53f7c76d6b7056f Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Tue, 9 Dec 2014 18:51:38 +0200 Subject: [PATCH] Update zepto, add data plugin --- js/00-zepto.js | 214 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 155 insertions(+), 59 deletions(-) diff --git a/js/00-zepto.js b/js/00-zepto.js index ea0b9ff..aa969f9 100644 --- a/js/00-zepto.js +++ b/js/00-zepto.js @@ -1,8 +1,7 @@ -/* Zepto v1.1.2-15-g59d3fe5 - zepto event ie - zeptojs.com/license */ - +/* Zepto v1.1.4-13-geba5344 - zepto event data ie - zeptojs.com/license */ var Zepto = (function() { - var undefined, key, $, classList, emptyArray = [], slice = emptyArray.slice, filter = emptyArray.filter, + var undefined, key, $, classList, emptyArray = [], concat = emptyArray.concat, filter = emptyArray.filter, slice = emptyArray.slice, document = window.document, elementDisplay = {}, classCache = {}, cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1,'opacity': 1, 'z-index': 1, 'zoom': 1 }, @@ -25,8 +24,6 @@ var Zepto = (function() { '*': document.createElement('div') }, readyRE = /complete|loaded|interactive/, - classSelectorRE = /^\.([\w-]+)$/, - idSelectorRE = /^#([\w-]*)$/, simpleSelectorRE = /^[\w-]*$/, class2type = {}, toString = class2type.toString, @@ -117,6 +114,13 @@ var Zepto = (function() { $.map(element.childNodes, function(node){ if (node.nodeType == 1) return node }) } + function Z(dom, selector) { + var i, len = dom ? dom.length : 0 + for (i = 0; i < len; i++) this[i] = dom[i] + this.length = len + this.selector = selector || '' + } + // `$.zepto.fragment` takes a html string and an optional tag name // to generate DOM nodes nodes from the given html string. // The generated DOM nodes are returned as an array. @@ -153,13 +157,9 @@ var Zepto = (function() { // `$.zepto.Z` swaps out the prototype of the given `dom` array // of nodes with `$.fn` and thus supplying all the Zepto functions - // to the array. Note that `__proto__` is not supported on Internet - // Explorer. This method can be overriden in plugins. + // to the array. This method can be overriden in plugins. zepto.Z = function(dom, selector) { - dom = dom || [] - dom.__proto__ = $.fn - dom.selector = selector || '' - return dom + return new Z(dom, selector) } // `$.zepto.isZ` should return `true` if the given object is a Zepto @@ -269,9 +269,15 @@ var Zepto = (function() { return selector == null ? $(nodes) : $(nodes).filter(selector) } - $.contains = function(parent, node) { - return parent !== node && parent.contains(node) - } + $.contains = document.documentElement.contains ? + function(parent, node) { + return parent !== node && parent.contains(node) + } : + function(parent, node) { + while (node && (node = node.parentNode)) + if (node === parent) return true + return false + } function funcArg(context, arg, idx, payload) { return isFunction(arg) ? arg.call(context, idx, payload) : arg @@ -283,7 +289,7 @@ var Zepto = (function() { // access className property while respecting SVGAnimatedString function className(node, value){ - var klass = node.className, + var klass = node.className || '', svg = klass && klass.baseVal !== undefined if (value === undefined) return svg ? klass.baseVal : klass @@ -382,14 +388,25 @@ var Zepto = (function() { // Define methods that will be available on all // Zepto collections $.fn = { + constructor: zepto.Z, + length: 0, + // Because a collection acts like an array // copy over these useful array functions. forEach: emptyArray.forEach, reduce: emptyArray.reduce, push: emptyArray.push, sort: emptyArray.sort, + splice: emptyArray.splice, indexOf: emptyArray.indexOf, - concat: emptyArray.concat, + concat: function(){ + var i, value, args = [] + for (i = 0; i < arguments.length; i++) { + value = arguments[i] + args[i] = zepto.isZ(value) ? value.toArray() : value + } + return concat.apply(zepto.isZ(this) ? this.toArray() : this, args) + }, // `map` and `slice` in the jQuery API work differently // from their array counterparts @@ -473,7 +490,8 @@ var Zepto = (function() { }, find: function(selector){ var result, $this = this - if (typeof selector == 'object') + if (!selector) result = [] + else if (typeof selector == 'object') result = $(selector).filter(function(){ var node = this return emptyArray.some.call($this, function(parent){ @@ -585,23 +603,25 @@ var Zepto = (function() { prev: function(selector){ return $(this.pluck('previousElementSibling')).filter(selector || '*') }, next: function(selector){ return $(this.pluck('nextElementSibling')).filter(selector || '*') }, html: function(html){ - return arguments.length === 0 ? - (this.length > 0 ? this[0].innerHTML : null) : + return 0 in arguments ? this.each(function(idx){ var originHtml = this.innerHTML $(this).empty().append( funcArg(this, html, idx, originHtml) ) - }) + }) : + (0 in this ? this[0].innerHTML : null) }, text: function(text){ - return arguments.length === 0 ? - (this.length > 0 ? this[0].textContent : null) : - this.each(function(){ this.textContent = (text === undefined) ? '' : ''+text }) + return 0 in arguments ? + this.each(function(idx){ + var newText = funcArg(this, text, idx, this.textContent) + this.textContent = newText == null ? '' : ''+newText + }) : + (0 in this ? this[0].textContent : null) }, attr: function(name, value){ var result - return (typeof name == 'string' && value === undefined) ? - (this.length == 0 || this[0].nodeType !== 1 ? undefined : - (name == 'value' && this[0].nodeName == 'INPUT') ? this.val() : + return (typeof name == 'string' && !(1 in arguments)) ? + (!this.length || this[0].nodeType !== 1 ? undefined : (!(result = this[0].getAttribute(name)) && name in this[0]) ? this[0][name] : result ) : this.each(function(idx){ @@ -611,29 +631,36 @@ var Zepto = (function() { }) }, removeAttr: function(name){ - return this.each(function(){ this.nodeType === 1 && setAttribute(this, name) }) + return this.each(function(){ this.nodeType === 1 && name.split(' ').forEach(function(attribute){ + setAttribute(this, attribute) + }, this)}) }, prop: function(name, value){ name = propMap[name] || name - return (value === undefined) ? - (this[0] && this[0][name]) : + return (1 in arguments) ? this.each(function(idx){ this[name] = funcArg(this, value, idx, this[name]) - }) + }) : + (this[0] && this[0][name]) }, data: function(name, value){ - var data = this.attr('data-' + name.replace(capitalRE, '-$1').toLowerCase(), value) + var attrName = 'data-' + name.replace(capitalRE, '-$1').toLowerCase() + + var data = (1 in arguments) ? + this.attr(attrName, value) : + this.attr(attrName) + return data !== null ? deserializeValue(data) : undefined }, val: function(value){ - return arguments.length === 0 ? + return 0 in arguments ? + this.each(function(idx){ + this.value = funcArg(this, value, idx, this.value) + }) : (this[0] && (this[0].multiple ? $(this[0]).find('option').filter(function(){ return this.selected }).pluck('value') : this[0].value) - ) : - this.each(function(idx){ - this.value = funcArg(this, value, idx, this.value) - }) + ) }, offset: function(coordinates){ if (coordinates) return this.each(function(index){ @@ -648,7 +675,7 @@ var Zepto = (function() { if ($this.css('position') == 'static') props['position'] = 'relative' $this.css(props) }) - if (this.length==0) return null + if (!this.length) return null var obj = this[0].getBoundingClientRect() return { left: obj.left + window.pageXOffset, @@ -665,7 +692,7 @@ var Zepto = (function() { return element.style[camelize(property)] || computedStyle.getPropertyValue(property) else if (isArray(property)) { var props = {} - $.each(isArray(property) ? property: [property], function(_, prop){ + $.each(property, function(_, prop){ props[prop] = (element.style[camelize(prop)] || computedStyle.getPropertyValue(prop)) }) return props @@ -700,6 +727,7 @@ var Zepto = (function() { addClass: function(name){ if (!name) return this return this.each(function(idx){ + if (!('className' in this)) return classList = [] var cls = className(this), newName = funcArg(this, name, idx, cls) newName.split(/\s+/g).forEach(function(klass){ @@ -710,6 +738,7 @@ var Zepto = (function() { }, removeClass: function(name){ return this.each(function(idx){ + if (!('className' in this)) return if (name === undefined) return className(this, '') classList = className(this) funcArg(this, name, idx, classList).split(/\s+/g).forEach(function(klass){ @@ -802,7 +831,8 @@ var Zepto = (function() { function traverseNode(node, fun) { fun(node) - for (var key in node.childNodes) traverseNode(node.childNodes[key], fun) + for (var i = 0, len = node.childNodes.length; i < len; i++) + traverseNode(node.childNodes[i], fun) } // Generate the `after`, `prepend`, `before`, `append`, @@ -829,11 +859,14 @@ var Zepto = (function() { operatorIndex == 2 ? target : null + var parentInDocument = $.contains(document.documentElement, parent) + nodes.forEach(function(node){ if (copyByClone) node = node.cloneNode(true) else if (!parent) return $(node).remove() - traverseNode(parent.insertBefore(node, target), function(el){ + parent.insertBefore(node, target) + if (parentInDocument) traverseNode(node, function(el){ if (el.nodeName != null && el.nodeName.toUpperCase() === 'SCRIPT' && (!el.type || el.type === 'text/javascript') && !el.src) window['eval'].call(window, el.innerHTML) @@ -852,7 +885,7 @@ var Zepto = (function() { } }) - zepto.Z.prototype = $.fn + zepto.Z.prototype = Z.prototype = $.fn // Export internal API functions in the `$.zepto` namespace zepto.uniq = uniq @@ -866,7 +899,7 @@ window.Zepto = Zepto window.$ === undefined && (window.$ = Zepto) ;(function($){ - var $$ = $.zepto.qsa, _zid = 1, undefined, + var _zid = 1, undefined, slice = Array.prototype.slice, isFunction = $.isFunction, isString = function(obj){ return typeof obj == 'string' }, @@ -953,12 +986,18 @@ window.$ === undefined && (window.$ = Zepto) $.event = { add: add, remove: remove } $.proxy = function(fn, context) { + var args = (2 in arguments) && slice.call(arguments, 2) if (isFunction(fn)) { - var proxyFn = function(){ return fn.apply(context, arguments) } + var proxyFn = function(){ return fn.apply(context, args ? args.concat(slice.call(arguments)) : arguments) } proxyFn._zid = zid(fn) return proxyFn } else if (isString(context)) { - return $.proxy(fn[context], fn) + if (args) { + args.unshift(fn[context], fn) + return $.proxy.apply(null, args) + } else { + return $.proxy(fn[context], fn) + } } else { throw new TypeError("expected function") } @@ -1139,24 +1178,81 @@ window.$ === undefined && (window.$ = Zepto) })(Zepto) ;(function($){ - // __proto__ doesn't exist on IE<11, so redefine - // the Z function to use object extension instead - if (!('__proto__' in {})) { - $.extend($.zepto, { - Z: function(dom, selector){ - dom = dom || [] - $.extend(dom, $.fn) - dom.selector = selector || '' - dom.__Z = true - return dom - }, - // this is a kludge but works - isZ: function(object){ - return $.type(object) === 'array' && '__Z' in object + var data = {}, dataAttr = $.fn.data, camelize = $.camelCase, + exp = $.expando = 'Zepto' + (+new Date()), emptyArray = [] + + // Get value from node: + // 1. first try key as given, + // 2. then try camelized key, + // 3. fall back to reading "data-*" attribute. + function getData(node, name) { + var id = node[exp], store = id && data[id] + if (name === undefined) return store || setData(node) + else { + if (store) { + if (name in store) return store[name] + var camelName = camelize(name) + if (camelName in store) return store[camelName] } + return dataAttr.call($(node), name) + } + } + + // Store value under camelized key on node + function setData(node, name, value) { + var id = node[exp] || (node[exp] = ++$.uuid), + store = data[id] || (data[id] = attributeData(node)) + if (name !== undefined) store[camelize(name)] = value + return store + } + + // Read all "data-*" attributes from a node + function attributeData(node) { + var store = {} + $.each(node.attributes || emptyArray, function(i, attr){ + if (attr.name.indexOf('data-') == 0) + store[camelize(attr.name.replace('data-', ''))] = + $.zepto.deserializeValue(attr.value) }) + return store } + $.fn.data = function(name, value) { + return value === undefined ? + // set multiple values via object + $.isPlainObject(name) ? + this.each(function(i, node){ + $.each(name, function(key, value){ setData(node, key, value) }) + }) : + // get value from first element + (0 in this ? getData(this[0], name) : undefined) : + // set value on all elements + this.each(function(){ setData(this, name, value) }) + } + + $.fn.removeData = function(names) { + if (typeof names == 'string') names = names.split(/\s+/) + return this.each(function(){ + var id = this[exp], store = id && data[id] + if (store) $.each(names || store, function(key){ + delete store[names ? camelize(this) : key] + }) + }) + } + + // Generate extended `remove` and `empty` functions + ;['remove', 'empty'].forEach(function(methodName){ + var origFn = $.fn[methodName] + $.fn[methodName] = function() { + var elements = this.find('*') + if (methodName === 'remove') elements = elements.add(this) + elements.removeData() + return origFn.call(this) + } + }) +})(Zepto) + +;(function(){ // getComputedStyle shouldn't freak out when called // without a valid element as argument try { @@ -1171,4 +1267,4 @@ window.$ === undefined && (window.$ = Zepto) } } } -})(Zepto) +})() -- 2.39.2