forked from kurtextrem/sprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprint.min.js
7 lines (7 loc) · 16.5 KB
/
sprint.min.js
1
2
3
4
5
6
7
/*
* Sprint JavaScript Library v0.9.3
*
* Copyright (c) 2014, 2015 Benjamin De Cock
* Released under the MIT license
*/
(function(){"use strict";var t,e,n,i,r=function(t,e){for(var n=B(e),i=Object.keys(t),r=i.length,o=0;o<r;o++)for(var s=i[o],f=t[s],h=f.length,u=0;u<h;u++)n.on(s,f[u])},o=(t=["animation-iteration-count","column-count","flex-grow","flex-shrink","font-weight","line-height","opacity","order","orphans","widows","z-index"],function(e,n){if(v(e,t))return n;var i="string"==typeof n?n:n.toString();return n&&!/\D/.test(i)&&(i+="px"),i}),s=function(t,e,n){return function(i){B(i.target).closest(e,n).size()&&t.apply(i.target,arguments)}},f={afterbegin:function(t){this.insertBefore(t,this.firstChild)},afterend:function(t){var e=this.parentElement;e&&e.insertBefore(t,this.nextSibling)},beforebegin:function(t){var e=this.parentElement;e&&e.insertBefore(t,this)},beforeend:function(t){this.appendChild(t)}},h=function(t,e){if(!(t.nodeType>1)){(f=a(t))&&r(f,e);for(var n,i=L("*",t),o=i.length,s=0;s<o;s++){var f;(f=a(i[s]))&&(n||(n=L("*",e)),r(f,n[s]))}}},u=function(t,e,n,i,r){var o=[],s=this;return this.each(function(){for(var f=t?this.parentElement:this;f&&(!r||r!=f)&&(i&&!s.is(i,f)||(o.push(f),!n))&&!e;)f=f.parentElement}),B(b(o))},c=function(t){return j(t)[0]},a=function(t){return t.sprintEventListeners},l=function(t,e){return Object.keys(a(t)).filter(function(t){return j(e).every(function(e){return v(e,j(t))})})},p=function(t,e,n){if(null==n){var i=t.get(0);if(!i||i.nodeType>1&&9!=i.nodeType)return;var r=e[0].toUpperCase()+e.substring(1);return i==document?Math.max(i.body["scroll"+r]||0,i.body["offset"+r]||0,w["scroll"+r]||0,w["offset"+r]||0):i==window?window["inner"+r]:i.getBoundingClientRect()[e]}var s="function"==typeof n,f=s?"":o(e,n);return t.each(function(t){this==document||this==window||this.nodeType>1||(s&&(f=o(e,n.call(this,t,B(this)[e]()))),this.style[e]=f)})},d=function(t,e){var n=e.length,i=e;if(n>1&&t.indexOf("after")>-1){i=[];for(var r=n;r--;)i.push(e[r])}for(r=0;r<n;r++){var o=i[r];if("string"==typeof o||"number"==typeof o)this.each(function(){this.insertAdjacentHTML(t,o)});else{if("function"!=typeof o){var s=o instanceof O,u=[],c=s?o.get():Array.isArray(o)?C(o,!0,!0):o.nodeType?[o]:k(o),a=c.length;if(this.each(function(e){for(var n=document.createDocumentFragment(),i=0;i<a;i++){var r,o=c[i];e?(r=o.cloneNode(!0),h(o,r)):r=o,n.appendChild(r),u.push(r)}f[t].call(this,n)}),s&&(o.dom=u,o.length=u.length),r<n-1)continue;return u}this.each(function(e){var n=o.call(this,e,this.innerHTML);d.call(B(this),t,[n])})}}},v=function(t,e){for(var n=e.length;n--;)if(e[n]===t)return!0;return!1},g=function(t){return/\./.test(t)},y=function(t,e,n){return null==e?"add"==t?this:this.removeAttr("class"):("string"==typeof e&&(i=!0,r=e.trim().split(" "),o=r.length),this.each(function(s,f){if(!(this.nodeType>1)){if(!i){var h=e.call(f,s,f.className);if(!h)return;r=h.trim().split(" "),o=r.length}for(var u=0;u<o;u++){var c=r[u];c&&(null==n?f.classList[t](c):f.classList.toggle(c,n))}}}));var i,r,o},m=function(){for(var t=["mozMatchesSelector","webkitMatchesSelector","msMatchesSelector","matches"],e=t.length;e--;){var n=t[e];if(Element.prototype[n])return n}}(),b=function(t){for(var e=[],n=0,i=t.length,r=0;r<i;r++){for(var o=t[r],s=!1,f=0;f<n;f++)if(o===e[f]){s=!0;break}s||(e[n++]=o)}return e},T=(e=function(t,e,n){return function(i){var r,o,s;n&&n!==i||(t.removeEventListener(e,i),g(e)&&(r=t,o=e,s=i,Object.keys(a(r)).filter(function(t){return c(o)===c(t)}).map(function(t){return a(r)[t]}).reduce(function(t,e){return t.concat(e)}).filter(function(t){return t===s}).length<2)&&t.removeEventListener(c(e),i))}},function(t,n){return function(i){var r,o;a(t)[i].forEach(e(t,i,n)),a(t)[i]=(r=a(t)[i],o=n,r.filter(function(t){return o&&o!==t}))}}),w=document.documentElement,C=function(t,e,n){for(var i=t.length,r=i;r--;)if(!t[r]&&0!==t[r]||e&&t[r]instanceof O||n&&("string"==typeof t[r]||"number"==typeof t[r])){for(var o=[],s=0;s<i;s++){var f=t[s];if(f||0===f)if(e&&f instanceof O)for(var h=0;h<f.length;h++)o.push(f.get(h));else!n||"string"!=typeof f&&"number"!=typeof f?o.push(f):o.push(document.createTextNode(f))}return o}return t},E=function(t,e,i){if(!n){var r=w.scrollTop;w.scrollTop=r+1;var o=w.scrollTop;w.scrollTop=r,n=o>r?w:document.body}if(null==i){var s=t.get(0);if(!s)return;return s!=window&&s!=document||(s=n),s[e]}return t.each(function(){var t=this;t!=window&&t!=document||(t=n),t[e]=i})},x=function(t,e,n,i){var r=[],o=e+"ElementSibling";return t.each(function(){for(var e=this;(e=e[o])&&(!i||!t.is(i,e));)n&&!t.is(n,e)||r.push(e)}),B(b(r))},A=function(t,e,n){var i=e+"ElementSibling";return t.map(function(){var e=this[i];if(e&&(!n||t.is(n,e)))return e},!1)},L=function(t,e){if(e=e||document,/^[\#.]?[\w-]+$/.test(t)){var n=t[0];if("."===n)return k(e.getElementsByClassName(t.slice(1)));if("#"===n){var i=void 0!==e.getElementById?e.getElementById(t.slice(1)):e.querySelector(t);return i?[i]:[]}return"body"===t?[document.body]:"head"===t?[document.head]:k(e.getElementsByTagName(t))}return k(e.querySelectorAll(t))},j=function(t){return C(t.split("."))},k=function(t){for(var e=[],n=t.length;n--;)e[n]=t[n];return e},S=(i=function(t,e){var n=B(t).clone(!0).get(0),i=n;if(n&&!(this.nodeType>1)){for(;i.firstChild;)i=i.firstChild;if("inner"==e){for(;this.firstChild;)i.appendChild(this.firstChild);this.appendChild(n)}else{var r="all"==e?this.get(0):this,o=r.parentNode,s=r.nextSibling;"all"==e?this.each(function(){i.appendChild(this)}):i.appendChild(r),o.insertBefore(n,s)}}},function(t,e){return"function"==typeof t?this.each(function(n){B(this)["inner"==e?"wrapInner":"wrap"](t.call(this,n))}):"all"==e?i.call(this,t,e):this.each(function(){i.call(this,t,e)}),this}),M={legend:{intro:"<fieldset>",outro:"</fieldset>"},area:{intro:"<map>",outro:"</map>"},param:{intro:"<object>",outro:"</object>"},thead:{intro:"<table>",outro:"</table>"},tr:{intro:"<table><tbody>",outro:"</tbody></table>"},col:{intro:"<table><tbody></tbody><colgroup>",outro:"</colgroup></table>"},td:{intro:"<table><tbody><tr>",outro:"</tr></tbody></table>"}};["tbody","tfoot","colgroup","caption"].forEach(function(t){M[t]=M.thead}),M.th=M.td;var O=function(t,e){if("string"==typeof t)"<"==t[0]?this.dom=[function(t){var e=document.createElement("div"),n=/[\w:-]+/.exec(t)[0],i=M[n],r=t.trim();i&&(r=i.intro+r+i.outro),e.insertAdjacentHTML("afterbegin",r);var o=e.lastChild;if(i)for(var s=i.outro.match(/</g).length;s--;)o=o.lastChild;return e.textContent="",o}(t)]:this.dom=e&&e instanceof O?e.find(t).get():L(t,e);else if(Array.isArray(t))this.dom=C(t);else if(t instanceof NodeList||t instanceof HTMLCollection)this.dom=k(t);else{if(t instanceof O)return t;if("function"==typeof t)return this.ready(t);this.dom=t?[t]:[]}this.length=this.dom.length};O.prototype={add:function(t){for(var e=this.get(),n=B(t),i=n.get(),r=0;r<n.length;r++)e.push(i[r]);return B(b(e))},addClass:function(t){return y.call(this,"add",t)},after:function(){return d.call(this,"afterend",arguments),this},append:function(){return d.call(this,"beforeend",arguments),this},appendTo:function(t){return B(d.call(B(t),"beforeend",[this]))},attr:function(t,e){var n="function"==typeof e;if("string"==typeof e||"number"==typeof e||n)return this.each(function(i){this.nodeType>1||this.setAttribute(t,n?e.call(this,i,this.getAttribute(t)):e)});if("object"==typeof t){var i=Object.keys(t),r=i.length;return this.each(function(){if(!(this.nodeType>1))for(var e=0;e<r;e++){var n=i[e];this.setAttribute(n,t[n])}})}var o=this.get(0);if(o&&!(o.nodeType>1)){var s=o.getAttribute(t);if(null!=s)return s||t}},before:function(){return d.call(this,"beforebegin",arguments),this},children:function(t){var e=[],n=this;return this.each(function(){if(!(this.nodeType>1))for(var i=this.children,r=i.length,o=0;o<r;o++){var s=i[o];t&&!n.is(t,s)||e.push(s)}}),B(e)},clone:function(t){return this.map(function(){if(this){var e=this.cloneNode(!0);return t&&h(this,e),e}},!1)},closest:function(t,e){return u.call(this,!1,!1,!0,t,e)},css:function(t,e){var n=typeof e,i="string"==n;if(i||"number"==n){var r=i&&/=/.test(e);if(r)var s=parseInt(e[0]+e.slice(2));return this.each(function(){if(!(this.nodeType>1)){if(r)var n=parseInt(getComputedStyle(this).getPropertyValue(t))+s;this.style[t]=o(t,r?n:e)}})}if("function"==n)return this.each(function(n){if(!(this.nodeType>1)){var i=getComputedStyle(this).getPropertyValue(t);this.style[t]=e.call(this,n,i)}});if("string"==typeof t){if(!(f=this.get(0))||f.nodeType>1)return;return getComputedStyle(f).getPropertyValue(t)}if(Array.isArray(t)){var f;if(!(f=this.get(0))||f.nodeType>1)return;for(var h={},u=getComputedStyle(f),c=t.length,a=0;a<c;a++){var l=t[a];h[l]=u.getPropertyValue(l)}return h}var p=Object.keys(t),d=p.length;return this.each(function(){if(!(this.nodeType>1))for(var e=0;e<d;e++){var n=p[e];this.style[n]=o(n,t[n])}})},detach:function(){return this.map(function(){var t=this.parentElement;if(t)return t.removeChild(this),this},!1)},each:function(t){for(var e=this.dom,n=this.length,i=0;i<n;i++){var r=e[i];if(!1===t.call(r,i,r))break}return this},empty:function(){return this.each(function(){this.innerHTML=""})},eq:function(t){return B(this.get(t))},filter:function(t){var e="function"==typeof t,n=this;return this.map(function(i){if(!(this.nodeType>1||!e&&!n.is(t,this)||e&&!t.call(this,i,this)))return this},!1)},find:function(t){if("string"==typeof t){var e=[];return this.each(function(){if(!(this.nodeType>1))for(var n=L(t,this),i=n.length,r=0;r<i;r++)e.push(n[r])}),B(b(e))}for(var n=t.nodeType?[t]:t.get(),i=n.length,r=[],o=0,s=0;s<this.length;s++){var f=this.get(s);if(!(f.nodeType>1))for(var h=0;h<i;h++){var u=n[h];if(f.contains(u)&&(r[o++]=u,!(o<i)))return B(r)}}return B(r)},first:function(){return this.eq(0)},get:function(t){return null==t?this.dom:(t<0&&(t+=this.length),this.dom[t])},has:function(t){if("string"==typeof t)return this.map(function(){if(!(this.nodeType>1)&&L(t,this)[0])return this},!1);for(var e=[],n=this.length;n--;){var i=this.get(n);if(i.contains(t)){e.push(i);break}}return B(e)},hasClass:function(t){for(var e=this.length;e--;){var n=this.get(e);if(n.nodeType>1)return;if(n.classList.contains(t))return!0}return!1},height:function(t){return p(this,"height",t)},html:function(t){if(null==t){var e=this.get(0);if(!e)return;return e.innerHTML}return"function"==typeof t?this.each(function(e){var n=t.call(this,e,this.innerHTML);B(this).html(n)}):this.each(function(){this.innerHTML=t})},index:function(t){if(this.length){var e,n;t?"string"==typeof t?(e=this.get(0),n=B(t)):(e=t instanceof O?t.get(0):t,n=this):(e=this.get(0),n=this.first().parent().children());for(var i=n.get(),r=i.length;r--;)if(i[r]==e)return r;return-1}},insertAfter:function(t){return B(t).after(this),this},insertBefore:function(t){return B(t).before(this),this},is:function(t,e){var n=e?[e]:this.get(),i=n.length;if("string"==typeof t){for(var r=0;r<i;r++){var o=n[r];if(!(o.nodeType>1)&&o[m](t))return!0}return!1}if("object"==typeof t){var s,f=(s=t instanceof O?t.get():t.length?t:[t]).length;for(r=0;r<i;r++)for(var h=0;h<f;h++)if(n[r]===s[h])return!0;return!1}if("function"==typeof t){for(r=0;r<i;r++)if(t.call(this,r,this))return!0;return!1}},last:function(){return this.eq(-1)},map:function(t,e){null==e&&(e=!0);for(var n=this.get(),i=this.length,r=[],o=0;o<i;o++){var s=n[o],f=t.call(s,o,s);if(e&&Array.isArray(f))for(var h=f.length,u=0;u<h;u++)r.push(f[u]);else r.push(f)}return B(r)},next:function(t){return A(this,"next",t)},nextAll:function(t){return x(this,"next",t)},nextUntil:function(t,e){return x(this,"next",e,t)},not:function(t){var e="function"==typeof t,n=this;return this.map(function(i){if(e){if(t.call(this,i,this))return}else if(n.is(t,this))return;return this},!1)},off:function(t,e){return"object"==typeof t?(Object.keys(t).forEach(function(e){this.off(e,t[e])},this),this):(t&&(t=t.trim().split(" ")),this.each(function(){var n,i;a(this)&&(t?t.forEach((n=this,i=e,function(t){l(n,t).forEach(T(n,i))})):Object.keys(a(this)).forEach(T(this)))}))},offset:function(t){if(!t){var e=this.get(0);if(!e||e.nodeType>1)return;var n=e.getBoundingClientRect();return{top:n.top+window.pageYOffset,left:n.left+window.pageXOffset}}return"object"==typeof t?this.each(function(){if(!(this.nodeType>1)){var e=B(this);"static"==e.css("position")?e.css("position","relative"):e.css({top:0,left:0});var n=e.offset();e.css({top:t.top-n.top+"px",left:t.left-n.left+"px"})}}):"function"==typeof t?this.each(function(e){var n=B(this),i=t.call(this,e,n.offset());n.offset(i)}):void 0},offsetParent:function(){var t=[];return this.each(function(){if(!(this.nodeType>1)){for(var e=this;e!=w;){e=e.parentNode;var n=getComputedStyle(e).getPropertyValue("position");if(!n)break;if("static"!=n)return void t.push(e)}t.push(w)}}),B(t)},on:function(t,e,n){if("string"!=typeof e&&(n=e,e=null),n){e&&(n=s(n,e));var i=t.trim().split(" ");return this.each(function(){e&&(n=s(n,e,this)),a(this)||(this.sprintEventListeners={}),i.forEach(function(t){a(this)[t]||(a(this)[t]=[]),a(this)[t].push(n),this.addEventListener(t,n),g(t)&&this.addEventListener(c(t),n)},this)})}return Object.keys(t).forEach(function(e){this.on(e,t[e])},this),this},parent:function(t){return u.call(this,!0,!0,!1,t)},parents:function(t){return u.call(this,!0,!1,!1,t)},position:function(){var t=this.offset(),e=this.parent().offset();if(t)return{top:t.top-e.top,left:t.left-e.left}},prop:function(t,e){if("object"==typeof t){var n=Object.keys(t),i=n.length;return this.each(function(){for(var e=0;e<i;e++){var r=n[e];this[r]=t[r]}})}if(null==e){var r=this.get(0);if(!r)return;return r[t]}var o="function"==typeof e;return this.each(function(n){this[t]=o?e.call(this,n,this[t]):e})},prepend:function(){return d.call(this,"afterbegin",arguments),this},prependTo:function(t){return B(d.call(B(t),"afterbegin",[this]))},prev:function(t){return A(this,"previous",t)},prevAll:function(t){return x(this,"previous",t)},prevUntil:function(t,e){return x(this,"previous",e,t)},ready:function(t){return this.dom=[document],this.length=1,this.on("DOMContentLoaded",t)},remove:function(t){var e=this;return this.each(function(){var n=this.parentElement;n&&(t&&!e.is(t,this)||n.removeChild(this))})},removeAttr:function(t){if(t){var e=t.trim().split(" "),n=e.length;this.each(function(){if(!(this.nodeType>1))for(var t=0;t<n;t++)this.removeAttribute(e[t])})}return this},removeClass:function(t){return y.call(this,"remove",t)},removeProp:function(t){return this.each(function(){this[t]=void 0})},replaceAll:function(t){return B(t).replaceWith(this),this},replaceWith:function(t){return"function"==typeof t?this.each(function(e){B(this).replaceWith(t.call(this,e,this))}):this.before(t).remove()},scrollLeft:function(t){return E(this,"scrollLeft",t)},scrollTop:function(t){return E(this,"scrollTop",t)},siblings:function(t){var e=[],n=this;return this.each(function(i,r){B(this).parent().children().each(function(){this==r||t&&!n.is(t,this)||e.push(this)})}),B(e)},size:function(){return this.length},slice:function(t,e){var n=this.get(),i=[],r=t>=0?t:t+this.length,o=this.length;for(e<0?o+=e:e>=0&&(o=e>this.length?this.length:e);r<o;r++)i.push(n[r]);return B(i)},text:function(t){if(null==t){var e=[];return this.each(function(){e.push(this.textContent)}),e.join("")}var n="function"==typeof t;return this.each(function(e){this.textContent=n?t.call(this,e,this.textContent):t})},toggleClass:function(t,e){return y.call(this,"toggle",t,e)},trigger:function(t){if(!window.CustomEvent||"function"!=typeof window.CustomEvent){var e=function(t,e){var n;return e=e||{bubbles:!1,cancelable:!1,detail:void 0},(n=document.createEvent("CustomEvent")).initCustomEvent(t,e.bubbles,e.cancelable,e.detail),n};e.prototype=window.Event.prototype,window.CustomEvent=e}return this.each(function(){l(this,t).forEach(function(t){this.dispatchEvent(new e(t,{bubbles:!0,cancelable:!0}))},this)})},unwrap:function(){return this.parent().each(function(){this!=document.body&&this!=w&&B(this).replaceWith(this.childNodes)}),this},val:function(t){if(null==t){var e=this.get(0);if(!e)return;if(e.multiple){var n=[];return this.first().children(":checked").each(function(){n.push(this.value)}),n}return e.value}if(Array.isArray(t)){var i=this;return this.each(function(){this.multiple?i.children().each(function(){this.selected=v(this.value,t)}):this.checked=v(this.value,t)})}return"function"==typeof t?this.each(function(e){B(this).val(t.call(this,e,this.value))}):this.each(function(){this.value=t})},width:function(t){return p(this,"width",t)},wrap:function(t){return S.call(this,t)},wrapAll:function(t){return S.call(this,t,"all")},wrapInner:function(t){return S.call(this,t,"inner")}};var B=function(t,e){return new O(t,e)};B.fn=O.prototype,"function"==typeof define&&define.amd?define(function(){return B}):"undefined"!=typeof module&&module.exports?module.exports=B:(this.Sprint=B,null==this.$&&(this.$=B))}).call(this);