forked from Esri/geotrigger-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeotrigger.min.js
More file actions
5 lines (4 loc) · 6.39 KB
/
Copy pathgeotrigger.min.js
File metadata and controls
5 lines (4 loc) · 6.39 KB
1
2
3
4
5
/*! geotrigger-js - v1.0.1 - 2014-10-10
* Copyright (c) 2014 Environmental Systems Research Institute, Inc.
* Apache License */
!function(a,b){"object"==typeof module&&"object"==typeof module.exports&&(XMLHttpRequest=require("xmlhttprequest").XMLHttpRequest,exports=module.exports=b()),"object"==typeof window&&(a.Geotrigger=b())}(this,function(){function a(a){this._queue=[],this._requestQueue=[],this._events={};var e={preferLocalStorage:!0,persistSession:"undefined"!=typeof module&&module.exports?!1:!0,geotriggersUrl:b,tokenUrl:c,registerDeviceUrl:d,automaticRegistation:!0,proxy:!1};if(!a||!a.clientId)throw new Error("Geotrigger.Session requires an `clientId` or a `session` parameter.");if(!a.proxy&&!f)throw new Error("This browser does not support CORS and a proxy has not been set.");g.merge(this,g.merge(e,a)),this.authenticatedAs=this.clientId&&this.clientSecret?"application":"device",this.key="geotriggers_"+this.authenticatedAs+"_"+this.clientId,this.persistSession&&(this.preferLocalStorage&&h?g.merge(this,j.get(this.key)):i&&g.merge(this,k.get(this.key))),this.token&&Date.now()>new Date(this.expiresOn).getTime()||!this.token?(delete this.token,this.refresh()):this.persistSession&&this.emit("authentication:restored")}var b="https://geotrigger.arcgis.com/",c="https://www.arcgis.com/sharing/oauth2/token",d="https://www.arcgis.com/sharing/oauth2/registerDevice",e={},f=!0;"object"==typeof window&&(f=!!(window.XMLHttpRequest&&"withCredentials"in new XMLHttpRequest)),Function.prototype.bind||(Function.prototype.bind=function(a){if("function"!=typeof this)throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var b=Array.prototype.slice.call(arguments,1),c=this,d=function(){},e=function(){return c.apply(this instanceof d&&a?this:a,b.concat(Array.prototype.slice.call(arguments)))};return d.prototype=this.prototype,e.prototype=new d,e}),a.prototype.authenticated=function(){return!!this.token},a.prototype.refresh=function(){if(!this.refreshing){this.refreshing=!0;var a=this.tokenUrl,b={client_id:this.clientId,f:"json"};this.clientSecret?(b.client_secret=this.clientSecret,b.grant_type="client_credentials"):this.refreshToken?(b.refresh_token=this.refreshToken,b.grant_type="refresh_token"):this.automaticRegistation&&(a=this.registerDeviceUrl),this.request(a,b,function(a,b,c){if(this.refreshing=!1,a)return void this.emit("authentication:error",a,b,c);for(this.expiresOn=new Date((new Date).getTime()+1e3*(b.expires_in-300)),b.deviceToken?(this.refreshToken=b.deviceToken.refresh_token,this.token=b.deviceToken.access_token,this.deviceId=b.device.deviceId):this.token=b.access_token,this.persistSession&&this.persist();this._queue.length;)this._queue.shift().apply(this);for(this.emit("authentication:success");this._requestQueue.length;)this.request.apply(this,this._requestQueue.shift())}.bind(this))}},a.prototype.toJSON=function(){var a={};for(var b in this)this.hasOwnProperty(b)&&this[b]&&!b.match(/^_.+/)&&(a[b]=this[b]);return a},a.prototype.on=function(a,b){for(var c=a.split(" "),d=0;d<c.length;d++)"undefined"==typeof this._events[c[d]]&&(this._events[c[d]]=[]),this._events[c[d]].push(b)},a.prototype.emit=function(a){var b=[].splice.call(arguments,1);if(this._events[a]instanceof Array)for(var c=this._events[a],d=0,e=c.length;e>d;d++)c[d].apply(this,b)},a.prototype.off=function(a,b){if(this._events[a]instanceof Array)for(var c=this._events[a],d=0,e=c.length;e>d;d++)if(c[d]===b){c.splice(d,1);break}},a.prototype.queue=function(a){return this.token?void a.apply(this):(this._queue.push(a),void this.refresh())},a.prototype.request=function(a,b,c){var d,e,f,h,i=Array.prototype.slice.apply(arguments),j=!a.match(/^https?:\/\//),k=j?this.geotriggersUrl+a:a;if(this.proxy&&(k=this.proxy+k),"function"==typeof b&&(c=b,b={}),j&&!this.token)return this._requestQueue.push(i),void this.refresh();var l=function(){try{d=JSON.parse(h.responseText),f=d.error?null:d,e=d.error?d.error:null}catch(a){f=null,e={type:"parse_error",message:"cound not parse response as JSON"}}if(e&&"invalidHeader"===e.type&&e.headers.Authorization)this._requestQueue.push(i),this.refresh();else if(e)if(e)c(e,null,h);else{var b={type:"unexpected_response",message:"the api returned a non JSON or unexpected data"};c(b,null,h)}else c(null,f,h)}.bind(this),m=function(){var a={type:"http_error"};try{a.message=JSON.parse(h.responseText)}catch(b){a.message="http error and cound not parse response as JSON"}c(a,null,h)}.bind(this),n=function(){4===h.readyState&&h.status<400?l():4===h.readyState&&h.status>=400&&m()}.bind(this);h=new XMLHttpRequest,h.onreadystatechange=n;var o;j?(b.token=this.token,o=JSON.stringify(b)):o=g.serialize(b),h.open("POST",k),h.setRequestHeader("Content-Type",j?"application/json":"application/x-www-form-urlencoded"),j&&(h.setRequestHeader("X-GT-Client-Name","geotrigger-js"),h.setRequestHeader("X-GT-Client-Version","1.0.0")),h.send(o)},a.prototype.persist=function(){var a={};this.clientId&&(a.clientId=this.clientId),this.clientSecret&&(a.clientSecret=this.clientSecret),this.token&&(a.token=this.token),this.refreshToken&&(a.refreshToken=this.refreshToken),this.deviceId&&(a.deviceId=this.deviceId),this.preferLocalStorage&&h?j.set(this.key,a):i&&k.set(this.key,a)},a.prototype.destroy=function(){this.preferLocalStorage&&h?j.erase(this.key):i&&k.erase(this.key)},e.Session=a;var g={merge:function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},isObject:function(a){return"[object Object]"===Object.prototype.toString.call(a)},serialize:function(a,b){var c=encodeURIComponent,d=[];for(var e in a)if(a.hasOwnProperty(e)){var f,h=b?b+"["+e+"]":e,i=a[e];f=g.isObject(i)?g.serialize(i,h):c(h)+"="+c(i),d.push(f)}return d.join("&")}},h="object"==typeof window&&"object"==typeof window.localStorage?!0:!1,i="object"==typeof document&&"string"==typeof document.cookie?!0:!1,j={set:function(a,b){window.localStorage.setItem(a,JSON.stringify(b))},get:function(a){return JSON.parse(window.localStorage.getItem(a))},erase:function(a){window.localStorage.removeItem(a)}},k={get:function(a){var b=document.cookie.match(new RegExp(a+"=[a-zA-Z0-9.()=|%/_]+($|;)","g"));return b&&b[0]?JSON.parse(b[0].substring(a.length+1,b[0].length).replace(";",""))||null:null},set:function(a,b,c){var d=[a+"="+JSON.stringify(b),"path=/","domain="+window.location.host],e=new Date;return e.setFullYear(e.getFullYear()+1),d.push(e.toGMTString()),c&&d.push("secure"),document.cookie=d.join("; ")},erase:function(a){document.cookie=a+"; "+new Date(0).toUTCString()}};return e});