img-cacher
Version:
Serve images from localStorage – great for offline apps.
1 lines • 8.11 kB
JavaScript
!function(t){function e(o){if(i[o])return i[o].exports;var r=i[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var i={};e.m=t,e.c=i,e.d=function(t,i,o){e.o(t,i)||Object.defineProperty(t,i,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var i=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(i,"a",i),i},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,i){t.exports=i(1)},function(t,e,i){var o=i(2),r=o.generate(function(t){this.defineProperties({writable:!0},{logging:void 0===t.logging||"string"==typeof t.logging?t.logging:"ImgCacher",prefix:t.prefix||"img-"})});r.definePrototype({base64Img:i(3),getImg:i(4),getData:i(5),isValid:i(6),reset:i(7),save:i(8),src:i(9),srcFromLocalStorage:i(10),buildSrc:i(11),log:i(12)}),"undefined"!=typeof window&&(window.ImgCacher=r),t.exports=r},function(t,e,i){var o;!function(){function r(t,e){if(typeof t!==e)throw new TypeError("Expected '"+e+"' but instead found '"+typeof t+"'")}function n(t){var e,i;return!!(t&&"object"==typeof t&&(e=Object.getOwnPropertyNames(t).sort(),1===(i=e.length)&&("get"===e[0]&&"function"==typeof t.get||"set"===e[0]&&"function"==typeof t.set)||2===i&&"get"===e[0]&&"function"==typeof t.get&&"set"===e[1]&&"function"==typeof t.set))}function c(t,e,i){var o,r,c,a={},h=i||e,g=i&&e;for(i=h&&"object"==typeof h?h:{},e=g&&"object"==typeof g?g:{},r=Object.getOwnPropertyNames(i),c=r.length,o=0;o<c;o++)n(i[r[o]])?a[r[o]]={configurable:!!e.configurable,enumerable:!!e.enumerable,get:i[r[o]].get,set:i[r[o]].set}:a[r[o]]={configurable:!!e.configurable,enumerable:!!e.enumerable,writable:!!e.writable,value:i[r[o]]};return Object.defineProperties(t,a),t}function a(){}var h={defineProperties:function(t,e){return c(this,t,e),this},getProto:function(){return Object.getPrototypeOf(this)},getSuper:function(){return Object.getPrototypeOf(this.constructor.prototype)}},g={isGeneration:function(t){return r(t,"function"),this.prototype.isPrototypeOf(t.prototype)},isCreation:function(t){return t instanceof this},generate:function(t){r(t,"function");var e=this;return c(t,{configurable:!1,enumerable:!1,writable:!1},{prototype:Object.create(e.prototype)}),c(t,{configurable:!1,enumerable:!1,writable:!1},g),c(t.prototype,{configurable:!1,enumerable:!1,writable:!1},{constructor:t,generator:t}),t},definePrototype:function(t,e){return c(this.prototype,t,e),this}};c(a,{configurable:!1,enumerable:!1,writable:!1},{prototype:a.prototype}),c(a.prototype,{configurable:!1,enumerable:!1,writable:!1},h),c(a,{configurable:!1,enumerable:!1,writable:!1},g),c(a,{configurable:!1,enumerable:!1,writable:!1},{isGenerator:function(t){return this.isGeneration(t)},toGenerator:function(t,e){return console.warn("Generator.toGenerator is depreciated please use Generator.generateFrom"),this.generateFrom(t,e)},generateFrom:function(t,e){return r(t,"function"),r(e,"function"),c(e,{configurable:!1,enumerable:!1,writable:!1},{prototype:Object.create(t.prototype)}),c(e,{configurable:!1,enumerable:!1,writable:!1},g),c(e.prototype,{configurable:!1,enumerable:!1,writable:!1},{constructor:e,generator:e}),c(e.prototype,{configurable:!1,enumerable:!1,writable:!1},h),e}}),Object.freeze(a),Object.freeze(a.prototype),void 0!==(o=function(){return a}.call(e,i,e,t))&&(t.exports=o)}()},function(t,e){function i(t,e){var i=1,o=t.width,r=t.height;if(e.fillWidth||e.fillHeight){var n=(void 0!==e.fillWidth?e.fillWidth:0)/o,c=(void 0!==e.fillHeight?e.fillHeight:0)/r;i=n>c?n:c,r=t.height*i,o=t.width*i}else e.maxWidth||e.maxHeight?(e.maxWidth&&e.maxHeight?i=Math.min(e.maxWidth/t.width,e.maxHeight/t.height):e.maxWidth?i=e.maxWidth/t.width:e.maxHeight&&(i=e.maxHeight/t.height),r=t.height*i,o=t.width*i):(e.width||e.height)&&(e.width&&e.height?(o=e.width,r=e.height):e.width?(i=e.width/t.width,o=e.width,r=t.height*i):e.height&&(i=e.height/t.height,o=t.width*i,r=e.height));return{width:Math.ceil(o),height:Math.ceil(r)}}function o(t,e,i){var o={x:Math.ceil(void 0!==e.cropX?e.cropX:e.cropWidth?(i.width-e.cropWidth)/-2:0),y:Math.ceil(void 0!==e.cropY?e.cropY:e.cropHeight?(i.height-e.cropHeight)/-2:0),width:Math.ceil(i.width),height:Math.ceil(i.height)};return e.cropWidth&&(Math.ceil(e.cropWidth)<o.width&&(o.width=Math.ceil(e.cropWidth)),Math.ceil(e.cropHeight)<o.height&&(o.height=Math.ceil(e.cropHeight))),{x:Math.ceil(void 0!==e.cropX?e.cropX:e.cropWidth?(i.width-e.cropWidth)/-2:0),y:Math.ceil(void 0!==e.cropY?e.cropY:e.cropHeight?(i.height-e.cropHeight)/-2:0),width:Math.ceil(e.cropWidth?Math.floor(e.cropWidth,i.width):i.width),height:Math.ceil(e.cropHeight?Math.floor(e.cropHeight,i.height):i.height)}}function r(t,e,i){var o=document.createElement("canvas"),r=o.getContext("2d"),n=document.createElement("canvas"),c=n.getContext("2d");if(o.width=e,o.height=Math.ceil(o.width*t.height/t.width),t.width*i>e){var a=1/i,h={width:Math.ceil(Math.floor(t.width*i)),height:Math.ceil(Math.floor(t.height*i))};for(n.width=h.width,n.height=h.height,c.drawImage(t,0,0,h.width,h.height);h.width*i>e;)h={width:Math.floor(h.width*i),height:Math.floor(h.height*i)},c.drawImage(n,0,0,h.width*a,h.height*a,0,0,h.width,h.height);r.drawImage(n,0,0,h.width,h.height,0,0,o.width,o.height)}else r.drawImage(t,0,0,o.width,o.height);return o}var n={};t.exports=function(t,e,c){"function"==typeof e&&(c=e,e={});var a=this,h=a.buildSrc(t.src,e);if(n[h])return a.log("base64Img","fromMemory",t.src,e),c(void 0,n[h]);var g=document.createElement("canvas"),l=g.getContext("2d"),u=i(t,e),f=o(t,e,u),s=r(t,u.width,"number"==typeof e.sharpness&&e.sharpness<1?e.sharpness:.5);console.log("HEREE",u,f),g.width=f.width,g.height=f.height,e.bg&&(l.fillStyle=e.bg,l.fillRect(0,0,f.width,f.height)),l.drawImage(s,-1*f.x,-1*f.y,f.width,f.height,0,0,f.width,f.height),l.imageSmoothingEnabled=!0,n[h]=g.toDataURL(e.type,e.encoderOptions),a.log("base64Img","noMemory",t.src,e),c(void 0,n[h],!0)}},function(t,e){function i(t,e,i){for(var r=o[t],n=r.callbacks.length;n--;)r.callbacks[n].call(r.callbacks[n],e,i),delete r.callbacks[n]}var o={};t.exports=function(t,e,r){var n=this,c=o[t];if(c&&!c.error)return c.isLoaded?(n.log("getImg","fromMemory",t,e),r(void 0,c)):(n.log("getImg","fromMemoryLoading",t,e),c.callbacks.push(r));c=o[t]=o[t]||new Image,c.setAttribute("crossOrigin","anonymous"),c.callbacks=[r],c.onload=function(){c.isLoaded=!0,i(t,void 0,c)},c.onerror=function(e){c.error=e,i(t,e)},"function"==typeof e.src?e.src.call(n,t,function(t){c.src=t}):c.src=e.src||t,n.log("getImg","noMemory",t,e)}},function(t,e){t.exports=function(t,e,i){var o=this;"function"==typeof e&&(i=e,e={}),o.getImg(t,e,function(r,n){if(r)return i(r);o.base64Img(n,e,function(r,n,c){c&&o.save(t,e,n),i(r,n)})})}},function(t,e){t.exports=function(t){var e=window.ActiveXObject?new window.ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest("Microsoft.XMLHTTP");e.open("HEAD",t,!1);try{return e.send(),e.status>=200&&(e.status<300||304===e.status)}catch(t){return!1}}},function(t,e){t.exports=function(){for(var t,e=this,i=window.localStorage,o=0;o<i.length;o++)t=i.key(o),0===t.indexOf(e.prefix)&&i.removeItem(i.key(o));e.log("reset")}},function(t,e){t.exports=function(t,e,i){var o=this;i||(i=e,e={}),window.localStorage.setItem(o.buildSrc(t,e),i),o.log("save",t,e)}},function(t,e){t.exports=function(t,e,i){var o=this;o.srcFromLocalStorage(o.buildSrc(t,e),function(r,n){if(r)return o.getData(t,e,function(r,n){if(r)return o.log("getSrc","error",r,t,e),i(r);o.log("getSrc","success","noLocalStorage",t,e),i(void 0,n)});o.log("getSrc","success","fromLocalStorage",t,e),i(void 0,n)})}},function(t,e){t.exports=function(t,e){var i=this;if("data:"===t.slice(0,5))e(void 0,t);else{var o=window.localStorage.getItem(t);o&&i.log("cacheFromSrc","fromLocalStorage",t),e(o?void 0:new Error("Image does not exist."),o)}}},function(t,e){t.exports=function(t,e){var i=this;return e="object"==typeof e?e:{},i.prefix+t+"-"+JSON.stringify(e)}},function(t,e){t.exports=function(){var t=this;if(t.logging){var e=Array.prototype.slice.call(arguments);e.unshift(t.logging+" ~>"),console.log.apply(console.debug,e)}}}]);