Current section

Files

Jump to
thesis web static js utilities ospry.js
Raw

web/static/js/utilities/ospry.js

// @preserve Copyright 2014 Ospry Labs, LLC. All rights reserved.
export default function(e){"use strict";var t="FormData"in e&&"XMLHttpRequest"in e&&"withCredentials"in new XMLHttpRequest,r=function(e){return"object"==typeof e&&null!==e&&!(e instanceof Array)},n=function(e){return"number"==typeof e},o=function(e){return"string"==typeof e},i=function(){},a=function(e,t){var r={};for(var n in t)t.hasOwnProperty(n)&&(r[n]="undefined"==typeof e[n]?t[n]:e[n]);return r},s=function(e){if("input"===e.nodeName.toLowerCase()&&"file"===e.type.toLowerCase())return e;for(var t=e.children,r=0;r<t.length;r++){var n=s(t[r]);if(null!==n)return n}return null},u=function(e,t,r){e.addEventListener?e.addEventListener(t,r,!1):e.attachEvent&&e.attachEvent("on"+t,r)},l=function(e,t,r){e.removeEventListener?e.removeEventListener(t,r,!1):e.detachEvent&&e.detachEvent("on"+t,r)},d="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",f=function(e){for(var t="",r=0;e>r;r++)t+=d[Math.floor(Math.random()*d.length)];return t},m=function(e){if(!o(e))throw"ospry: required public key is not a string";if(""===e)throw"ospry: required public key is missing";this._key=e,this._serverURL="https://api.ospry.io/v1"};m.formats=["jpeg","png","gif"],m.prototype.up=function(e){if(!r(e))throw"ospry: required options argument is not an object";if(e=a(e,{files:null,form:null,isPrivate:!1,imageReady:i,done:i}),t){if(null===e.files){if(null===e.form)throw"ospry: both form and files are missing from options (one of them should be set)";var n=s(e.form);if(null===n)throw"ospry: file input is missing from form";e.files=n.files}this._upXHR(e.files,e)}else{if(null===e.form)throw"ospry: form property is missing from options (older browsers only support form-based uploads)";this._upIFrame(e)}},m.prototype._upXHR=function(e,t){for(var r=0,n=function(n,o,i){t.imageReady(n,o,i),r++,r===e.length&&t.done()},o=0;o<e.length;o++)this._upXHRFile(e[o],t,o,n)},m.prototype._upXHRFile=function(e,t,r,n){var o=new FormData;o.append("key",this._key),o.append("isPrivate",t.isPrivate),o.append("file",e);var i=new XMLHttpRequest;i.onerror=function(){var e={httpStatusCode:0,cause:"network-error",message:"Network error.",docsUrl:"https://ospry.io/docs#network-error"};n(e,null,r)},i.onload=function(){var e=JSON.parse(i.responseText);if(e.hasOwnProperty("error"))return void n(e.error,null,r);var t=e.results[0];if(t.hasOwnProperty("error"))return void n(t.error,null,r);var o=t.metadata;o.hasOwnProperty("timeCreated")&&(o.timeCreated=new Date(o.timeCreated)),n(null,o,r)},i.open("POST",this._serverURL+"/images",!0),i.send(o)},m.prototype._upIFrame=function(t){var r=s(t.form);if(null===r)throw"ospry: file input is missing from form";var n=r.getAttribute("name");r.setAttribute("name","file");var o="ospry-"+f(24),i=document.createElement("iframe");i.id=o,i.name=o,i.style.display="none",document.body.appendChild(i);var a=this._serverURL+"/images?frameId="+encodeURIComponent(o),d=document.createElement("form");d.setAttribute("method","POST"),d.setAttribute("action",a),d.setAttribute("target",o),d.setAttribute("enctype","multipart/form-data"),d.setAttribute("encoding","multipart/form-data"),d.style.display="none",document.body.appendChild(d);var m=document.createElement("input");m.setAttribute("name","key"),m.setAttribute("type","hidden"),m.setAttribute("value",this._key);var p=document.createElement("input");p.setAttribute("name","iframe"),p.setAttribute("type","hidden"),p.setAttribute("value","true");var h=document.createElement("input");h.setAttribute("name","isPrivate"),h.setAttribute("type","hidden"),h.setAttribute("value",t.isPrivate),d.appendChild(m),d.appendChild(p);var c=r.previousSibling;d.appendChild(r);var y=setTimeout(function(){document.body.removeChild(i),t.done()},6e4),v=function(r){if(r.source===i.contentWindow){clearTimeout(y),document.body.removeChild(d),document.body.removeChild(i),l(e,"message",v);var n=JSON.parse(r.data);if(n.hasOwnProperty("error"))return void t.done();for(var o=n.results,a=0;a<o.length;a++){var s=o[a];if(s.hasOwnProperty("error"))t.imageReady(s.error,null,a);else{var u=s.metadata;u.hasOwnProperty("timeCreated")&&(u.timeCreated=new Date(u.timeCreated)),t.imageReady(null,u,a)}}t.done()}};u(e,"message",v),d.submit(),null!==n&&""!==n?r.setAttribute("name",n):r.removeAttribute("name"),null!==c?c.parentNode.insertBefore(r,c.nextSibling):t.form.insertBefore(r,t.form.firstChild)},m.prototype.get=function(e){if(!r(e))throw"ospry: required options argument is not an object";if(e=a(e,{url:null,urls:null,format:null,maxWidth:null,maxHeight:null,preload:!0,imageReady:i,done:i}),null!==e.url&&(e.urls=[e.url]),null===e.urls)throw"ospry: no urls provided in options";if(0===e.urls.length)return void e.done();for(var t=0,n=function(r,n,o){e.imageReady(r,n,o),t++,t===e.urls.length&&e.done()},o=function(e,t){return function(){var r={httpStatusCode:0,cause:"image-load-error",message:"Failed to load image.",docsUrl:"https://ospry.io/docs#image-load-error"};n(r,e,t)}},s=function(e,t){return function(){n(null,e,t)}},u=0;u<e.urls.length;u++){var l=new Image;e.preload&&(l.onerror=o(l,u),l.onload=s(l,u)),l.src=this.formatURL(e.urls[u],e),e.preload||n(null,l,u)}},m.prototype.formatURL=function(e,t){if(!o(e))throw"ospry: required url is not a string";if("undefined"==typeof t&&(t={}),!r(t))throw"ospry: options argument is not an object";if(t=a(t,{format:null,maxWidth:null,maxHeight:null}),null===t.format&&null===t.maxWidth&&null===t.maxHeight)return e;var i=p.parse(e);if(null===i)throw"ospry: invalid url";if(null!==t.format){if(!o(t.format))throw"ospry: format should be a string";if(""!==t.format){"jpg"===t.format&&(t.format="jpeg");for(var s=!1,u=0;u<m.formats.length;u++)if(m.formats[u]===t.format){s=!0;break}if(!s)throw"ospry: invalid format";i.set("format",t.format)}else i.remove("format")}if(null!==t.maxHeight){if(!n(t.maxHeight))throw"ospry: maxHeight should be a number";t.maxHeight>0?i.set("maxHeight",t.maxHeight):i.remove("maxHeight")}if(null!==t.maxWidth){if(!n(t.maxWidth))throw"ospry: maxWidth should be a number";t.maxWidth>0?i.set("maxWidth",t.maxWidth):i.remove("maxWidth")}return i.toString()};var p=function(){this.scheme=null,this.host=null,this.path=null,this.query={}};p.parse=function(e){var t=new p,r=e.indexOf(":");if(-1===r)return null;t.scheme=e.slice(0,r),r+="://".length;var n=e.indexOf("/",r);if(-1===n)return null;t.host=e.slice(r,n);var o=e.indexOf("?",n);if(-1===o&&(o=e.length),t.path=e.slice(n,o),o!==e.length)for(var i=e.slice(o+1).split("&"),a=0;a<i.length;a++){var s=i[a],u=s.split("=");if(2!==u.length)return null;t.add(decodeURIComponent(u[0]),decodeURIComponent(u[1]))}return t},p.prototype.set=function(e,t){this.query[e]=[t]},p.prototype.add=function(e,t){this.query.hasOwnProperty(e)?this.query[e].push(t):this.query[e]=[t]},p.prototype.remove=function(e){delete this.query[e]},p.prototype.toString=function(){var e="";null!==this.scheme&&(e+=this.scheme+"://"),null!==this.host&&(e+=this.host),null!==this.path&&(e+=this.path);var t=this.query,r=[];for(var n in t)t.hasOwnProperty(n)&&r.push(n);r.sort();for(var o=!0,i=0;i<r.length;i++)for(var n=r[i],a=t[n],s=0;s<a.length;s++)o?(e+="?"+encodeURIComponent(n)+"="+encodeURIComponent(a[s]),o=!1):e+="&"+encodeURIComponent(n)+"="+encodeURIComponent(a[s]);return e},"undefined"!=typeof module&&"undefined"!=typeof module.exports?module.exports=m:"function"==typeof e.define&&e.define.amd?e.define("Ospry",function(){return m}):e.Ospry=m;return m;};