(function(p,c){typeof exports=="object"&&typeof module!="undefined"?module.exports=c():typeof define=="function"&&define.amd?define(c):(p=typeof globalThis!="undefined"?globalThis:p||self,p.Instafeed=c())})(this,function(){"use strict";function p(u,e){if(!u)throw new Error(e)}function c(u){p(!u||typeof u=="object","options must be an object, got "+u+" ("+typeof u+")");var e={accessToken:null,accessTokenTimeout:1e4,after:null,apiTimeout:1e4,apiLimit:null,before:null,debug:!1,error:null,filter:null,limit:null,mock:!1,render:null,sort:null,success:null,target:"instafeed",template:'',templateBoundaries:["{{","}}"],transform:null},t={running:!1,node:null,token:null,paging:null,pool:[]};if(u)for(var o in e)typeof u[o]!="undefined"&&(e[o]=u[o]);p(typeof e.target=="string"||typeof e.target=="object","target must be a string or DOM node, got "+e.target+" ("+typeof e.target+")"),p(typeof e.accessToken=="string"||typeof e.accessToken=="function","accessToken must be a string or function, got "+e.accessToken+" ("+typeof e.accessToken+")"),p(typeof e.accessTokenTimeout=="number","accessTokenTimeout must be a number, got "+e.accessTokenTimeout+" ("+typeof e.accessTokenTimeout+")"),p(typeof e.apiTimeout=="number","apiTimeout must be a number, got "+e.apiTimeout+" ("+typeof e.apiTimeout+")"),p(typeof e.debug=="boolean","debug must be true or false, got "+e.debug+" ("+typeof e.debug+")"),p(typeof e.mock=="boolean","mock must be true or false, got "+e.mock+" ("+typeof e.mock+")"),p(typeof e.templateBoundaries=="object"&&e.templateBoundaries.length===2&&typeof e.templateBoundaries[0]=="string"&&typeof e.templateBoundaries[1]=="string","templateBoundaries must be an array of 2 strings, got "+e.templateBoundaries+" ("+typeof e.templateBoundaries+")"),p(!e.template||typeof e.template=="string","template must null or string, got "+e.template+" ("+typeof e.template+")"),p(!e.error||typeof e.error=="function","error must be null or function, got "+e.error+" ("+typeof e.error+")"),p(!e.before||typeof e.before=="function","before must be null or function, got "+e.before+" ("+typeof e.before+")"),p(!e.after||typeof e.after=="function","after must be null or function, got "+e.after+" ("+typeof e.after+")"),p(!e.success||typeof e.success=="function","success must be null or function, got "+e.success+" ("+typeof e.success+")"),p(!e.filter||typeof e.filter=="function","filter must be null or function, got "+e.filter+" ("+typeof e.filter+")"),p(!e.transform||typeof e.transform=="function","transform must be null or function, got "+e.transform+" ("+typeof e.transform+")"),p(!e.sort||typeof e.sort=="function","sort must be null or function, got "+e.sort+" ("+typeof e.sort+")"),p(!e.render||typeof e.render=="function","render must be null or function, got "+e.render+" ("+typeof e.render+")"),p(!e.limit||typeof e.limit=="number","limit must be null or number, got "+e.limit+" ("+typeof e.limit+")"),p(!e.apiLimit||typeof e.apiLimit=="number","apiLimit must null or number, got "+e.apiLimit+" ("+typeof e.apiLimit+")"),this._state=t,this._options=e}return c.prototype.run=function(){var e=this;return this._debug("run","options",this._options),this._debug("run","state",this._state),this._state.running?(this._debug("run","already running, skipping"),!1):(this._start(),this._debug("run","getting dom node"),typeof this._options.target=="string"?this._state.node=document.getElementById(this._options.target):this._state.node=this._options.target,this._state.node?(this._debug("run","got dom node",this._state.node),this._debug("run","getting access token"),this._getAccessToken(function(o,r){if(o){e._debug("onTokenReceived","error",o),e._fail(new Error("error getting access token: "+o.message));return}e._debug("onTokenReceived","got token",r),e._state.token=r,e._showNext(function(n){if(n){e._debug("onNextShown","error",n),e._fail(n);return}e._finish()})}),!0):(this._fail(new Error("no element found with ID "+this._options.target)),!1))},c.prototype.hasNext=function(){var e=this._state.paging,t=this._state.pool;return this._debug("hasNext","paging",e),this._debug("hasNext","pool",t.length,t),t.length>0||e&&typeof e.next=="string"},c.prototype.next=function(){var e=this;if(!e.hasNext())return e._debug("next","hasNext is false, skipping"),!1;if(e._state.running)return e._debug("next","already running, skipping"),!1;e._start(),e._showNext(function(o){if(o){e._debug("onNextShown","error",o),e._fail(o);return}e._finish()})},c.prototype._showNext=function(e){var t=this,o=null,r=null,s=typeof this._options.limit=="number";if(t._debug("showNext","pool",t._state.pool.length,t._state.pool),t._state.pool.length>0){if(s?r=t._state.pool.splice(0,t._options.limit):r=t._state.pool.splice(0),t._debug("showNext","items from pool",r.length,r),t._debug("showNext","updated pool",t._state.pool.length,t._state.pool),t._options.mock)t._debug("showNext","mock enabled, skipping render");else try{t._renderData(r)}catch(n){e(n);return}e(null)}else t._state.paging&&typeof t._state.paging.next=="string"?o=t._state.paging.next:(o="https://graph.instagram.com/me/media?fields=caption,id,media_type,media_url,permalink,thumbnail_url,timestamp,username&access_token="+t._state.token,!t._options.apiLimit&&typeof t._options.limit=="number"?(t._debug("showNext","no apiLimit set, falling back to limit",t._options.apiLimit,t._options.limit),o=o+"&limit="+t._options.limit):typeof t._options.apiLimit=="number"&&(t._debug("showNext","apiLimit set, overriding limit",t._options.apiLimit,t._options.limit),o=o+"&limit="+t._options.apiLimit)),t._debug("showNext","making request",o),t._makeApiRequest(o,function(i,l){var a=null;if(i){t._debug("onResponseReceived","error",i),e(new Error("api request error: "+i.message));return}t._debug("onResponseReceived","data",l),t._success(l),t._debug("onResponseReceived","setting paging",l.paging),t._state.paging=l.paging;try{if(a=t._processData(l),t._debug("onResponseReceived","processed data",a),a.unused&&a.unused.length>0){t._debug("onResponseReceived","saving unused to pool",a.unused.length,a.unused);for(var f=0;f0&&(g=n.slice(n.length-i),this._debug("processData","unusedItems",g.length,g),n.splice(n.length-i,i))),{items:n,unused:g}},c.prototype._extractTags=function(e){var t=/#([^\s]+)/gi,o=/[~`!@#$%^&*\(\)\-\+={}\[\]:;"'<>\?,\./|\\\s]+/i,r=[],s=null;if(typeof e=="string")for(;(s=t.exec(e))!==null;)o.test(s[1])===!1&&r.push(s[1]);return r},c.prototype._getItemData=function(e){var t=null,o=null;switch(e.media_type){case"IMAGE":t="image",o=e.media_url;break;case"VIDEO":t="video",o=e.thumbnail_url;break;case"CAROUSEL_ALBUM":t="album",o=e.media_url;break}return{caption:e.caption,tags:this._extractTags(e.caption),id:e.id,image:o,link:e.permalink,model:e,timestamp:e.timestamp,type:t,username:e.username}},c.prototype._renderData=function(e){var t=typeof this._options.template=="string",o=typeof this._options.render=="function",r=null,s=null,n=null,i="";if(this._debug("renderData","hasTemplate",t,"hasRender",o),!(typeof e!="object"||e.length<=0)){for(var l=0;l0;)this._debug("renderData","appending child",n.childNodes[0]),this._state.node.appendChild(n.childNodes[0])}},c.prototype._basicRender=function(e){for(var t=new RegExp(this._options.templateBoundaries[0]+"([\\s\\w.]+)"+this._options.templateBoundaries[1],"gm"),o=this._options.template,r=null,s="",n=null,i=0,l=null,a=null;(r=t.exec(o))!==null;)l=r[1],n=o.slice(i,r.index),s=s+n,a=this._valueForKeyPath(l,e),a&&(s=s+a.toString()),i=t.lastIndex;return i=0)try{f=JSON.parse(s.responseText)}catch(g){r._debug("apiRequestOnLoad","json parsing error",g,s.responseText),n(new Error("error parsing response json"));return}if(s.status!==200){f&&f.error?n(new Error(f.error.code+" "+f.error.message)):n(new Error("status code "+s.status));return}n(null,f)},s.open("GET",e,!0),s.timeout=this._options.apiTimeout,s.send()},c.prototype._getAccessToken=function(e){var t=!1,o=this,r=null,s=function(i,l){t||(t=!0,clearTimeout(r),e(i,l))};if(typeof this._options.accessToken=="function"){this._debug("getAccessToken","calling accessToken as function"),r=setTimeout(function(){o._debug("getAccessToken","timeout check",t),s(new Error("accessToken timed out"),null)},this._options.accessTokenTimeout);try{this._options.accessToken(function(i,l){o._debug("getAccessToken","received accessToken callback",t,i,l),s(i,l)})}catch(n){this._debug("getAccessToken","error invoking the accessToken as function",n),s(n,null)}}else this._debug("getAccessToken","treating accessToken as static",typeof this._options.accessToken),s(null,this._options.accessToken)},c.prototype._debug=function(){var e=null;this._options.debug&&console&&typeof console.log=="function"&&(e=[].slice.call(arguments),e[0]="[Instafeed] ["+e[0]+"]",console.log.apply(null,e))},c.prototype._runHook=function(e,t){var o=!1;if(typeof this._options[e]=="function")try{this._options[e](t),o=!0}catch(r){this._debug("runHook","error calling hook",e,r)}return o},c}); //# sourceMappingURL=/s/files/1/0646/8901/2974/t/2/assets/instafeed.js.map?v=81142330342592026311654145483