forked from stevenschobert/instafeed.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstafeed.min.js
1 lines (1 loc) · 7.4 KB
/
instafeed.min.js
1
'use strict';var _typeof='function'==typeof Symbol&&'symbol'==typeof Symbol.iterator?function(c){return typeof c}:function(c){return c&&'function'==typeof Symbol&&c.constructor===Symbol&&c!==Symbol.prototype?'symbol':typeof c},_createClass=function(){function c(d,f){for(var h,g=0;g<f.length;g++)h=f[g],h.enumerable=h.enumerable||!1,h.configurable=!0,'value'in h&&(h.writable=!0),Object.defineProperty(d,h.key,h)}return function(d,f,g){return f&&c(d.prototype,f),g&&c(d,g),d}}();function _classCallCheck(c,d){if(!(c instanceof d))throw new TypeError('Cannot call a class as a function')}var Instafeed=function(){function c(d,f){_classCallCheck(this,c);var g,h;if(this.options={target:'instafeed',get:'popular',resolution:'thumbnail',sortBy:'none',links:!0,mock:!1},'object'===('undefined'==typeof d?'undefined':_typeof(d)))for(g in d)h=d[g],this.options[g]=h;this.context=null==f?this:f,this.unique=this._genKey()}return _createClass(c,[{key:'hasNext',value:function hasNext(){return'string'==typeof this.context.nextUrl&&0<this.context.nextUrl.length}},{key:'next',value:function next(){return!!this.hasNext()&&this.run(this.context.nextUrl)}},{key:'run',value:function run(d){var f,g,h;if('string'!=typeof this.options.clientId&&'string'!=typeof this.options.accessToken)throw new Error('Missing clientId or accessToken.');if('string'!=typeof this.options.accessToken&&'string'!=typeof this.options.clientId)throw new Error('Missing clientId or accessToken.');return null!=this.options.before&&'function'==typeof this.options.before&&this.options.before.call(this),'undefined'!=typeof document&&null!==document&&(h=document.createElement('script'),h.id='instafeed-fetcher',h.src=d||this._buildUrl(),f=document.getElementsByTagName('head'),f[0].appendChild(h),g='instafeedCache'+this.unique,window[g]=new c(this.options,this),window[g].unique=this.unique),!0}},{key:'parse',value:function parse(d){var f,g,h,l,n,o,p,q,r,s,t,u,v,w,x,y,A,B,C,D,E,F,G,H,I,J,K,L,M,N='',O=[];if('object'!==('undefined'==typeof d?'undefined':_typeof(d))){if(null!=this.options.error&&'function'==typeof this.options.error)return this.options.error.call(this,'Invalid JSON data'),!1;throw new Error('Invalid JSON response')}if(200!==d.meta.code){if(null!=this.options.error&&'function'==typeof this.options.error)return this.options.error.call(this,d.meta.error_message),!1;throw new Error('Error from Instagram: '+d.meta.error_message)}if(0===d.data.length){if(null!=this.options.error&&'function'==typeof this.options.error)return this.options.error.call(this,'No images were returned from Instagram'),!1;throw new Error('No images were returned from Instagram')}if(null!=this.options.success&&'function'==typeof this.options.success&&this.options.success.call(this,d),this.context.nextUrl='',null!=d.pagination&&(this.context.nextUrl=d.pagination.next_url),'none'!==this.options.sortBy)switch(K='random'===this.options.sortBy?['','random']:this.options.sortBy.split('-'),J='least'===K[0],K[1]){case'random':d.data.sort(function(){return 0.5-Math.random()});break;case'recent':d.data=this._sortBy(d.data,'created_time',J);break;case'liked':d.data=this._sortBy(d.data,'likes.count',J);break;case'commented':d.data=this._sortBy(d.data,'comments.count',J);break;default:throw new Error('Invalid option for sortBy: \''+this.options.sortBy+'\'.');}if('undefined'!=typeof document&&null!==document&&!1===this.options.mock){if(v=d.data,I=parseInt(this.options.limit,10),null!=this.options.limit&&v.length>I&&(v=v.slice(0,I)),o=document.createDocumentFragment(),null!=this.options.filter&&'function'==typeof this.options.filter&&(v=this._filter(v,this.options.filter)),null!=this.options.template&&'string'==typeof this.options.template){for(q='',u='',M=document.createElement('div'),(r=0,E=v.length);r<E;r++){if(s=v[r],t=s.images[this.options.resolution],O=[],Object.keys(s.images).forEach(function(P){var Q=s.images[P];O.push(Q.url+' '+Q.width+'w')}),'object'!==('undefined'==typeof t?'undefined':_typeof(t)))throw n='No image found for resolution: '+this.options.resolution+'.',new Error(n);A=t.width,x=t.height,y='square',A>x&&(y='landscape'),A<x&&(y='portrait'),N=t.url,u=this._makeTemplate(this.options.template,{model:s,id:s.id,link:s.link,type:s.type,image:N,srcset:O.join(', '),width:A,height:x,orientation:y,caption:this._getObjectProperty(s,'caption.text'),likes:s.likes.count,comments:s.comments.count,location:this._getObjectProperty(s,'location.name')}),q+=u}for(M.innerHTML=q,l=[],h=0,g=M.childNodes.length;h<g;)l.push(M.childNodes[h]),h+=1;for(C=0,F=l.length;C<F;C++)H=l[C],o.appendChild(H)}else for(D=0,G=v.length;D<G;D++){if(s=v[D],w=document.createElement('img'),t=s.images[this.options.resolution],'object'!==('undefined'==typeof t?'undefined':_typeof(t)))throw n='No image found for resolution: '+this.options.resolution+'.',new Error(n);N=t.url,w.src=N,!0===this.options.links?(f=document.createElement('a'),f.href=s.link,f.appendChild(w),o.appendChild(f)):o.appendChild(w)}if(L=this.options.target,'string'==typeof L&&(L=document.getElementById(L)),null==L)throw n='No element with id="'+this.options.target+'" on page.',new Error(n);L.appendChild(o),p=document.getElementsByTagName('head')[0],p.removeChild(document.getElementById('instafeed-fetcher')),B='instafeedCache'+this.unique,window[B]=void 0;try{delete window[B]}catch(P){}}return null!=this.options.after&&'function'==typeof this.options.after&&this.options.after.call(this),!0}},{key:'_buildUrl',value:function _buildUrl(){var d,f,g;switch(d='https://api.instagram.com/v1',this.options.get){case'popular':f='media/popular';break;case'tagged':if(!this.options.tagName)throw new Error('No tag name specified. Use the \'tagName\' option.');f='tags/'+this.options.tagName+'/media/recent';break;case'location':if(!this.options.locationId)throw new Error('No location specified. Use the \'locationId\' option.');f='locations/'+this.options.locationId+'/media/recent';break;case'user':if(!this.options.userId)throw new Error('No user specified. Use the \'userId\' option.');f='users/'+this.options.userId+'/media/recent';break;default:throw new Error('Invalid option for get: \''+this.options.get+'\'.');}return g=d+'/'+f,g+=null==this.options.accessToken?'?client_id='+this.options.clientId:'?access_token='+this.options.accessToken,null!=this.options.limit&&(g+='&count='+this.options.limit),g+='&callback=instafeedCache'+this.unique+'.parse',g}},{key:'_genKey',value:function _genKey(){var d=function(){return(0|65536*(1+Math.random())).toString(16).substring(1)};return''+d()+d()+d()+d()}},{key:'_makeTemplate',value:function _makeTemplate(d,f){var g,h,l,m,n;for(h=/(?:\{{2})([\w\[\]\.]+)(?:\}{2})/,g=d;h.test(g);)m=g.match(h)[1],n=null==(l=this._getObjectProperty(f,m))?'':l,g=g.replace(h,function(){return''+n});return g}},{key:'_getObjectProperty',value:function _getObjectProperty(d,f){var g,h;for(f=f.replace(/\[(\w+)\]/g,'.$1'),h=f.split('.');h.length;)if(g=h.shift(),null!=d&&g in d)d=d[g];else return null;return d}},{key:'_sortBy',value:function _sortBy(d,f,g){var h;return h=function sorter(l,m){var n,o;return n=this._getObjectProperty(l,f),o=this._getObjectProperty(m,f),g?n>o?1:-1:n<o?1:-1},d.sort(h.bind(this)),d}},{key:'_filter',value:function _filter(d,f){var g,h,l,m,n;for(g=[],h=function fn(o){if(f(o))return g.push(o)},(l=0,n=d.length);l<n;l++)m=d[l],h(m);return g}}]),c}();(function(c,d){return'function'==typeof define&&define.amd?define([],d):'object'===('undefined'==typeof module?'undefined':_typeof(module))&&module.exports?module.exports=d():c.Instafeed=d()})(void 0,function(){return Instafeed});