diff --git a/README.md b/README.md index a409a9d..d1a6fdf 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ The methods supported by Sprint are, for the most part, identical to jQuery's. T * [off](http://api.jquery.com/off/) - _no support for selector_ * [offset](http://api.jquery.com/offset/) * [offsetParent](http://api.jquery.com/offsetParent/) -* [on](http://api.jquery.com/on/) - _no support for selector and data_ +* [on](http://api.jquery.com/on/) - _no support for data_ * [parent](http://api.jquery.com/parent/) * [parents](http://api.jquery.com/parents/) * [position](http://api.jquery.com/position/) diff --git a/sprint.js b/sprint.js index f5f0c60..0d9d44f 100644 --- a/sprint.js +++ b/sprint.js @@ -50,7 +50,15 @@ var Sprint; } return stringValue } - }()) + }()); + + var createDelegator = function(handler, selector, context){ + return function(e){ + if (Sprint(e.target).closest(selector, context).size()){ + handler.apply(e.target, arguments); + } + } + }; var createDOM = function(HTMLString) { var tmp = document.createElement("div") @@ -1126,12 +1134,19 @@ var Sprint; }) return Sprint(dom) }, - on: function(events, handler) { + on: function(events, selector, handler) { + if(typeof selector !== "string"){ + handler = selector; + selector = null; + } // .on(events, handler) if (handler) { var eventsArr = events.trim().split(" ") return this.each(function() { + if(selector){ + handler = createDelegator(handler, selector, this); + } if (!getEvents(this)) { this.sprintEventListeners = {} } diff --git a/sprint.min.js b/sprint.min.js index d591e56..233cbfa 100644 --- a/sprint.min.js +++ b/sprint.min.js @@ -1,35 +1 @@ -// Sprint v0.9.2 - sprintjs.com/license -var Sprint; -(function(){var D=function(a,b){for(var c=Sprint(b),d=Object.keys(a),e=d.length,f=0;fa?d:a):d==window?window["inner"+a]:d.getBoundingClientRect()[b]}var e="function"==typeof c,f=e?"":w(b,c);return a.each(function(a){this==document||this== -window||1 -Object.keys(a.sprintEventListeners).filter(function(a){return q(b)[0]===q(a)[0]}).map(function(b){return a.sprintEventListeners[b]}).reduce(function(a,b){return a.concat(b)}).filter(function(a){return a===c}).length?!1:!0},b=function(b,c,f){return function(g){f&&f!==g||(b.removeEventListener(c,g),/\./.test(c)&&!a(b,c,g)&&b.removeEventListener(q(c)[0],g))}},c=function(a,b){return a.filter(function(a){return b&&b!==a})};return function(a,e){return function(f){a.sprintEventListeners[f].forEach(b(a,f, -e));a.sprintEventListeners[f]=c(a.sprintEventListeners[f],e)}}}(),M=function(a,b){return function(c){F(a,c).forEach(H(a,b))}},m=document.documentElement,A=function(a,b,c){for(var d=a.length,e=d;e--;)if(!a[e]&&0!==a[e]||b&&a[e]instanceof n||c&&("string"==typeof a[e]||"number"==typeof a[e])){for(var e=[],f=0;fe?m:document.body}if(null==d){b=b.get(0);if(!b)return;if(b==window||b==document)b=a;return b[c]}return b.each(function(){var b=this;if(b==window||b==document)b=a;b[c]=d})}}(),y=function(a,b,c,d){var e=[],f=b+"ElementSibling";a.each(function(){for(var b=this;(b=b[f])&&(!d||!a.is(d,b));)c&&!a.is(c,b)||e.push(b)});return Sprint(x(e))},J=function(a,b,c){var d=b+"ElementSibling";return a.map(function(){var b= -this[d];if(b&&(!c||a.is(c,b)))return b},!1)},r=function(a,b){b=b||document;if(/^[\#.]?[\w-]+$/.test(a)){var c=a[0];return"."==c?t(b.getElementsByClassName(a.slice(1))):"#"==c?(c=b.getElementById(a.slice(1)))?[c]:[]:"body"==a?[document.body]:t(b.getElementsByTagName(a))}return t(b.querySelectorAll(a))},q=function(a){return A(a.split("."))},t=function(a){for(var b=[],c=a.length;c--;)b[c]=a[c];return b},C=function(){var a=function(a,c){var d=Sprint(a).clone(!0).get(0),e=d;if(d&&!(1",outro:""}, -area:{intro:"",outro:""},param:{intro:"",outro:""},thead:{intro:"",outro:"
"},tr:{intro:"",outro:"
"},col:{intro:"",outro:"
"},td:{intro:"",outro:"
"}};["tbody","tfoot","colgroup","caption"].forEach(function(a){u[a]=u.thead});u.th=u.td;var n=function(a,b){if("string"==typeof a)if("<"==a[0]){var c=document.createElement("div"),d=/[\w:-]+/.exec(a)[0], -d=u[d],e=a.trim();d&&(e=d.intro+e+d.outro);c.insertAdjacentHTML("afterbegin",e);e=c.lastChild;if(d)for(d=d.outro.match(/a&&(a+=this.length);return this.dom[a]}, -has:function(a){if("string"==typeof a)return this.map(function(){if(!(1b?f+=b:0<=b&&(f=b>this.length?this.length:b);e1)return;var listeners=getEvents(el);listeners&&addEventListeners(listeners,clone);var descendants=selectElements("*",el);var descendantsLen=descendants.length;var cloneDescendants;for(var i=0;i1)return;var capitalizedProp=prop[0].toUpperCase()+prop.substring(1);if(el==document){var offset=root["offset"+capitalizedProp];var inner=window["inner"+capitalizedProp];return offset>inner?offset:inner}if(el==window){return window["inner"+capitalizedProp]}return el.getBoundingClientRect()[prop]}var isFunction=typeof value=="function";var stringValue=isFunction?"":addPx(prop,value);return obj.each(function(index){if(this==document||this==window||this.nodeType>1)return;if(isFunction){stringValue=addPx(prop,value.call(this,index,Sprint(this)[prop]()))}this.style[prop]=stringValue})};var insertHTML=function(position,args){var argsLen=args.length;var contents=args;if(argsLen>1&&position.indexOf("after")>-1){contents=[];var i=argsLen;while(i--){contents.push(args[i])}}for(var i=0;i1)return;if(!isString){var callbackValue=className.call(el,i,el.className);if(!callbackValue)return;classNames=callbackValue.trim().split(" ");classNamesLen=classNames.length}for(var j=0;jinitialScrollPos?root:document.body}if(value==null){var el=sprintObj.get(0);if(!el)return;if(el==window||el==document){el=scrollRoot}return el[method]}return sprintObj.each(function(){var el=this;if(el==window||el==document){el=scrollRoot}el[method]=value})}}();var selectAdjacentSiblings=function(sprintObj,direction,selector,until){var dom=[];var prop=direction+"ElementSibling";sprintObj.each(function(){var el=this;while(el=el[prop]){if(until&&sprintObj.is(until,el))break;if(selector&&!sprintObj.is(selector,el))continue;dom.push(el)}});return Sprint(removeDuplicates(dom))};var selectImmediateAdjacentSibling=function(sprintObj,direction,selector){var prop=direction+"ElementSibling";return sprintObj.map(function(){var el=this[prop];if(!el||selector&&!sprintObj.is(selector,el))return;return el},false)};var selectElements=function(selector,context){context=context||document;if(/^[\#.]?[\w-]+$/.test(selector)){var firstChar=selector[0];if(firstChar=="."){return toArray(context.getElementsByClassName(selector.slice(1)))}if(firstChar=="#"){var el=context.getElementById(selector.slice(1));return el?[el]:[]}if(selector=="body"){return[document.body]}return toArray(context.getElementsByTagName(selector))}return toArray(context.querySelectorAll(selector))};var splitNamespaces=function(event){return sanitize(event.split("."))};var toArray=function(obj){var arr=[];var i=obj.length;while(i--){arr[i]=obj[i]}return arr};var wrap=function(){var callback=function(wrappingElement,variant){var wrap=Sprint(wrappingElement).clone(true).get(0);var innerWrap=wrap;if(!wrap||this.nodeType>1)return;while(innerWrap.firstChild){innerWrap=innerWrap.firstChild}if(variant=="inner"){while(this.firstChild){innerWrap.appendChild(this.firstChild)}this.appendChild(wrap)}else{var el=variant=="all"?this.get(0):this;var prt=el.parentNode;var next=el.nextSibling;variant=="all"?this.each(function(){innerWrap.appendChild(this)}):innerWrap.appendChild(el);prt.insertBefore(wrap,next)}};return function(wrappingElement,variant){if(typeof wrappingElement=="function"){this.each(function(i){Sprint(this)[variant=="inner"?"wrapInner":"wrap"](wrappingElement.call(this,i))})}else{variant=="all"?callback.call(this,wrappingElement,variant):this.each(function(){callback.call(this,wrappingElement,variant)})}return this}}();var wrapMap={legend:{intro:"
",outro:"
"},area:{intro:"",outro:""},param:{intro:"",outro:""},thead:{intro:"",outro:"
"},tr:{intro:"",outro:"
"},col:{intro:"",outro:"
"},td:{intro:"",outro:"
"}};["tbody","tfoot","colgroup","caption"].forEach(function(tag){wrapMap[tag]=wrapMap.thead});wrapMap.th=wrapMap.td;var Init=function(selector,context){if(typeof selector=="string"){if(selector[0]=="<"){this.dom=[createDOM(selector)]}else{this.dom=context&&context instanceof Init?context.find(selector).get():selectElements(selector,context)}}else if(Array.isArray(selector)){this.dom=sanitize(selector)}else if(selector instanceof NodeList||selector instanceof HTMLCollection){this.dom=toArray(selector)}else if(selector instanceof Init){return selector}else if(typeof selector=="function"){return this.ready(selector)}else{this.dom=selector?[selector]:[]}this.length=this.dom.length};Init.prototype={add:function(selector){var dom=this.get();var objToAdd=Sprint(selector);var domToAdd=objToAdd.get();for(var i=0;i1)return;this.setAttribute(name,isFunc?value.call(this,i,this.getAttribute(name)):value)})}if(typeof name=="object"){var attrNames=Object.keys(name);var attrNamesLen=attrNames.length;return this.each(function(){if(this.nodeType>1)return;for(var i=0;i1)return;var attrValue=el.getAttribute(name);if(attrValue==null){return undefined}if(!attrValue){return name}return attrValue},before:function(){insertHTML.call(this,"beforebegin",arguments);return this},children:function(selector){var dom=[];var self=this;this.each(function(){if(this.nodeType>1)return;var nodes=this.children;var nodesLen=nodes.length;for(var i=0;i1)return;if(isRelativeValue){var current=parseInt(getComputedStyle(this).getPropertyValue(property));var result=current+relativeValue}this.style[property]=addPx(property,isRelativeValue?result:value)})}if(valueType=="function"){return this.each(function(index){if(this.nodeType>1)return;var oldValue=getComputedStyle(this).getPropertyValue(property);this.style[property]=value.call(this,index,oldValue)})}if(typeof property=="string"){var el=this.get(0);if(!el||el.nodeType>1)return;return getComputedStyle(el).getPropertyValue(property)}if(Array.isArray(property)){var el=this.get(0);if(!el||el.nodeType>1)return;var o={};var styles=getComputedStyle(el);var propertyLen=property.length;for(var i=0;i1)return;for(var i=0;i1||!isFunc&&!self.is(selector,this)||isFunc&&!selector.call(this,i,this))return;return this},false)},find:function(selector){if(typeof selector=="string"){var dom=[];this.each(function(){if(this.nodeType>1)return;var elements=selectElements(selector,this);var elementsLen=elements.length;for(var i=0;i1)continue;for(var j=0;j1||!selectElements(selector,this)[0])return;return this},false)}var result=[];var i=this.length;while(i--){var el=this.get(i);if(!el.contains(selector))continue;result.push(el);break}return Sprint(result)},hasClass:function(name){var i=this.length;while(i--){var el=this.get(i);if(el.nodeType>1)return;if(el.classList.contains(name)){return true}}return false},height:function(value){return getSetDimension(this,"height",value)},html:function(htmlString){if(htmlString==null){var el=this.get(0);if(!el)return;return el.innerHTML}if(typeof htmlString=="function"){return this.each(function(i){var content=htmlString.call(this,i,this.innerHTML);Sprint(this).html(content)})}return this.each(function(){this.innerHTML=htmlString})},index:function(el){if(!this.length)return;var toFind;var sprintElements;if(!el){toFind=this.get(0);sprintElements=this.first().parent().children()}else if(typeof el=="string"){toFind=this.get(0);sprintElements=Sprint(el)}else{toFind=el instanceof Init?el.get(0):el;sprintElements=this}var elements=sprintElements.get();var i=elements.length;while(i--){if(elements[i]==toFind){return i}}return-1},insertAfter:function(target){Sprint(target).after(this);return this},insertBefore:function(target){Sprint(target).before(this);return this},is:function(selector,element){var set=element?[element]:this.get();var setLen=set.length;if(typeof selector=="string"){for(var i=0;i1)continue;if(el[matches](selector)){return true}}return false}if(typeof selector=="object"){var obj;if(selector instanceof Init){obj=selector.get()}else{obj=selector.length?selector:[selector]}var objLen=obj.length;for(var i=0;i1)return;var pos=el.getBoundingClientRect();return{top:pos.top,left:pos.left}}if(typeof coordinates=="object"){return this.each(function(){if(this.nodeType>1)return;var $this=Sprint(this);$this.css("position")=="static"?$this.css("position","relative"):$this.css({top:0,left:0});var pos=$this.offset();$this.css({top:coordinates.top-pos.top+"px",left:coordinates.left-pos.left+"px"})})}if(typeof coordinates=="function"){return this.each(function(i){var $this=Sprint(this);var posObj=coordinates.call(this,i,$this.offset());$this.offset(posObj)})}},offsetParent:function(){var dom=[];this.each(function(){if(this.nodeType>1)return;var prt=this;while(prt!=root){prt=prt.parentNode;var pos=getComputedStyle(prt).getPropertyValue("position");if(!pos)break;if(pos!="static"){dom.push(prt);return}}dom.push(root)});return Sprint(dom)},on:function(events,selector,handler){if(typeof selector!=="string"){handler=selector;selector=null}if(handler){var eventsArr=events.trim().split(" ");return this.each(function(){if(selector){handler=createDelegator(handler,selector,this)}if(!getEvents(this)){this.sprintEventListeners={}}eventsArr.forEach(function(event){if(!getEvents(this)[event]){getEvents(this)[event]=[]}getEvents(this)[event].push(handler);this.addEventListener(event,handler);if(!isNamespaced(event))return;this.addEventListener(getEventFromNamespace(event),handler)},this)})}Object.keys(events).forEach(function(event){this.on(event,events[event])},this);return this},parent:function(selector){return findAncestors.call(this,true,true,false,selector)},parents:function(selector){return findAncestors.call(this,true,false,false,selector)},position:function(){var pos={first:this.offset(),prt:this.parent().offset()};if(!pos.first)return;return{top:pos.first.top-pos.prt.top,left:pos.first.left-pos.prt.left}},prop:function(propertyName,value){if(typeof propertyName=="object"){var props=Object.keys(propertyName);var propsLen=props.length;return this.each(function(){for(var i=0;i1)return;for(var i=0;i=0?start:start+this.length;var l=this.length;if(end<0){l+=end}else if(end>=0){l=end>this.length?this.length:end}for(;i