image-selector
Version:
A responsive image selector
2 lines • 3.43 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("imageSelector",[],e):"object"==typeof exports?exports.imageSelector=e():t.imageSelector=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var i=r[n]={exports:{},id:n,loaded:!1};return t[n].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){function n(t){0===h.length&&s.addEventListener(window,"resize",function(){for(var t=0;t<h.length;t++)h[t]()}),h.push(t)}function i(t,e){var r;return t.currentStyle?r=t.currentStyle[e]:window.getComputedStyle&&(r=document.defaultView.getComputedStyle(t,null).getPropertyValue(e)),r}function o(t){var e=t.clientWidth;if(0===e){var r=i(t,"width");if("auto"===r)e=o(t.parentNode);else if("%"===r.charAt(r.length-1)){var n=parseInt(r,10),a=o(t.parentNode);e=a*(n/100)}else e=parseInt(r,10)}return e}function a(t){var e=t.lastIndexOf(".");return t.substr(0,e)+"@2x"+t.substr(e)}function u(t,e){var r=t/e;return r<1&&(r/=2),Math.abs(1-r)}function c(t){return l[t]||(l[t]=function(e){return e.aspectRatio===t}),l[t]}function d(t,e){var r=t/e,n=r.toString();return v[n]||(v[n]=function(t){return t.width/t.height===r||(t.width+1)/t.height>=r&&(t.width-1)/t.height<=r}),v[n]}var s=r(1),f=window.devicePixelRatio&&window.devicePixelRatio>1.5,h=[],l={},v={};e.selectCutWithAspectRatio=function(t,r,n,i){return t=t.filter(c(n)),e.selectCut(t,r,i)},e.selectCutWithWidthAndHeight=function(t,r,n,i){return t=t.filter(d(r,n)),e.selectCut(t,r,i)},e.selectCut=function(t,e,r){void 0===r&&(r=.75);var n,i=1/0;for(var o in t)if(t.hasOwnProperty(o)){var a=t[o];if(e===a.width)return a;var c=u(a.width,e);c<r&&c<i&&(n=a,i=c)}return n},e.addSource=function(t,r,n){r=r||"src",n=n||JSON.parse(t.getAttribute("data-cuts"));var u,c=o(t),d=t.getAttribute("data-aspect-ratio"),s=!1;for(var h in n)if(n.hasOwnProperty(h)&&n[h].height){s=!0;break}u=t.attributes.height&&t.attributes.height.specified?t.height:i(t,"height"),u=parseInt(u,10),r=t.getAttribute("data-src-attribute")||r,u<30&&(u=!1);var l;if(l=d?e.selectCutWithAspectRatio(n,c,d):s&&u?e.selectCutWithWidthAndHeight(n,c,u):e.selectCut(n,c)){var v=l.src;f&&l.at2x&&l.width<1.5*c&&(v="string"==typeof l.at2x?l.at2x:a(v)),t.setAttribute(r,v)}else t.className+=" no-cut-found",t.setAttribute(r,"")},e.watchImage=function(t,r,i){var o=t.offsetWidth;e.addSource(t,i,r),n(function(){t.offsetWidth!==o&&(o=t.offsetWidth,e.addSource(t,i,r))})},e.selectImages=function(t,r){t=t||document.body;for(var n,i=t.querySelectorAll("img[data-cuts]"),o=0;o<i.length;o++){n=i[o];try{var a=JSON.parse(n.getAttribute("data-cuts"));n.removeAttribute("data-cuts"),"true"===n.getAttribute("data-responsive")?e.watchImage(n,a,r):e.addSource(n,r,a)}catch(t){console&&console.error&&console.error(t)}}}},function(t,e){function r(t,e,r,n){return c=c||(document.addEventListener?{add:i,rm:o}:{add:a,rm:u}),c.add(t,e,r,n)}function n(t,e,r,n){return c=c||(document.addEventListener?{add:i,rm:o}:{add:a,rm:u}),c.rm(t,e,r,n)}function i(t,e,r,n){t.addEventListener(e,r,n)}function o(t,e,r,n){t.removeEventListener(e,r,n)}function a(t,e,r,n){if(n)throw new Error("cannot useCapture in oldIE");t.attachEvent("on"+e,r)}function u(t,e,r,n){t.detachEvent("on"+e,r)}r.removeEventListener=n,r.addEventListener=r,t.exports=r;var c=null}])});
//# sourceMappingURL=imageSelector.js.map