-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstagram.min.js
1 lines (1 loc) · 8.94 KB
/
instagram.min.js
1
define(["dojo/_base/declare","dojo/_base/connect","dojo/_base/array","dojo/_base/lang","dojo/_base/event","esri/layers/FeatureLayer","esri/tasks/query","esri/geometry","esri/graphic","esri/graphicsUtils","esri/InfoTemplate","esri/request"],function(a,b,c,d,e,f,g,h,i,j,k,l){return a("modules.instagram",[],{declaredClass:"modules.instagram",constructor:function(c){this.options={map:null,baseUrl:"https://api.instagram.com/v1",clientId:null,autopage:!0,maxpage:5,limit:100,id:"instagramLayer",symbolUrl:"",symbolHeight:22.5,symbolWidth:18.75,popupHeight:280,popupWidth:260,popupTitle:"Instagram"},a.safeMixin(this.options,c);if(this.options.map===null)throw"Instagram: Reference to esri.Map object required";if(this.options.clientId===null)throw"Instagram: clientId in options required";if(this.options.symbolUrl===null)throw"Instagram: symbolUrl in options required";this.featureCollection=this._getFeatureCollectionTemplate(),this.infoTemplate=new k,this.infoTemplate.setTitle(d.hitch(this,function(a){return this.options.popupTitle})),this.infoTemplate.setContent(this._getWindowContent),this.featureLayer=new f(this.featureCollection,{id:this.options.id,outFields:["*"],infoTemplate:this.infoTemplate,visible:!0}),this.options.map.addLayer(this.featureLayer),b.connect(this.featureLayer,"onClick",d.hitch(this,function(a){e.stop(a);var b=new g;b.geometry=this._pointToExtent(this.options.map,a.mapPoint,this.options.symbolWidth);var c=this.featureLayer.selectFeatures(b,f.SELECTION_NEW);this.options.map.infoWindow.setFeatures([c]),this.options.map.infoWindow.show(a.mapPoint),this.options.map.infoWindow.resize(this.options.popupWidth,this.options.popupHeight),this.onClick(a)})),this.stats={geo:0,noGeo:0},this.geocoded_ids=[],this.pageCount=0,this.loaded=!0},clear:function(){this.query=null,this.deferred&&this.deferred.cancel(),this.options.map.infoWindow.isShowing&&this.options.map.infoWindow.hide(),this.featureLayer.graphics.length>0&&this.featureLayer.applyEdits(null,null,this.featureLayer.graphics),this.stats={geo:0,noGeo:0},this.geocoded_ids=[],this.pageCount=0,this.onClear()},getStats:function(){var a=this.stats;return a.total=this.stats.geo+this.stats.noGeo,a},show:function(){this.featureLayer.setVisibility(!0)},hide:function(){this.featureLayer.setVisibility(!1)},getExtent:function(){return this.featureLayer.graphics.length==0?null:this.featureLayer.graphics.length==1?this._pointToExtent(this.options.map,this.featureLayer.graphics[0].geometry,this.options.symbolWidth):j.graphicsExtent(this.featureLayer.graphics)},zoomTo:function(){var a=this.getExtent();a&&this.options.map.setExtent(a,!0)},search:function(a){var b=this.options.map.extent.getCenter(),c={count:a||100,lat:b.getLatitude(),lng:b.getLongitude(),distance:5e3};this._sendRequest(this.options.baseUrl+"/media/search/",c)},searchByTags:function(a,b){if(!(a&&a instanceof Array&&a.length>0)){this.onError("Instagram: search array of tags not provided");return}var c={q:a.toString(),count:b||100};this._sendRequest(this.options.baseUrl+"/tags/search/",c)},popular:function(a){var b={count:a||100};this._sendRequest(this.options.baseUrl+"/media/popular")},recentByUser:function(a,b){if(!a){this.onError("Instagram: userid not provided");return}var c={count:b||100};this._sendRequest(this.options.baseUrl+"/users/"+a+"/media/recent",c)},recentByTag:function(a,b){if(!a||a.length<1){this.onError("Instagram: tagname not provided");return}var c={count:b||100};this._sendRequest(this.options.baseUrl+"/tags/"+a+"/media/recent",c)},_sendRequest:function(b,c){console.log(b),console.log("page count: "+this.pageCount),this.pageCount<1&&this.onUpdateStart();var e={client_id:this.options.clientId};a.safeMixin(e,c),this.deferred&&(console.log("esri_Request being cancelled"),this.deferred.cancel()),this.deferred=l({url:b,handleAs:"json",timeout:1e4,callbackParamName:"callback",content:e}),this.deferred.then(d.hitch(this,this._onSuccess),d.hitch(this,this._onError))},_onSuccess:function(a){console.log("** Success: Got something back from Instagram **"),console.log(a);if(a.meta&&a.meta.code!==200){console.log("Instagram: API Meta code is not 200"),this.onError(a.meta.error_message||"API error");return}var b=a.data,c=a.pagination;delete this.deferred,b&&b.length>0?(console.log("found "+b.length+" items"),this._mapResults(b),c&&c.next_url?(this.pageCount++,this.options.autopage&&this.options.maxpage>this.pageCount?this._sendRequest(c.next_url):this.onUpdateEnd()):this.onUpdateEnd()):this.onUpdateEnd()},_onError:function(a){console.log("** Error: No Results from Instagram **"),console.log("Search error: "+a.message.toString()),this.onError(a)},_mapResults:function(a){var b=[];c.forEach(a,d.hitch(this,function(a){console.log(a);if(this.geocoded_ids[a.id]){console.log("duplicate item "+a.id);return}this.geocoded_ids[a.id]=!0;if(!(a.location&&parseFloat(a.location.latitude)&&parseFloat(a.location.longitude))){this.stats.noGeo++;return}this.stats.geo++;var c=new h.Point(parseFloat(a.location.longitude),parseFloat(a.location.latitude)),d={photo_link:a.images.low_resolution.url||a.images.low_resolution,photo_thumbnail:a.images.thumbnail.url||a.images.thumbnail,id:a.id,location_name:a.location.name||null,location_latitude:a.location.latitude,location_longitude:a.location.longitude,likes_count:a.likes?a.likes.count:null,link:a.link,attribution:a.attribution||"",created_time:a.created_time,caption_text:a.caption?a.caption.text:"No caption",user_profile_name:a.user.full_name||"Anonymous",user_profile_picture:a.user.profile_picture},e=new h.webMercatorUtils.geographicToWebMercator(c),f=new i(e);f.setAttributes(d),b.push(f)})),this.featureLayer.applyEdits(b,null,null),this.onUpdate()},_getWindowContent:function(a){var b=a.attributes;console.log(b);var c=new Date;c.setTime(b.created_time*1e3);var d="";return d+='<div class="instagram">',d+='<span class="caption">'+b.caption_text+"</span>",d+='<hr style="height:1px;">',d+='<img src="'+b.photo_thumbnail+'"/>',d+="<br>",d+='<span class="likes">Likes: '+b.likes_count+"</span>",d+='<hr style="height:1px;">',d+='<span class="user"><img align="right" src="'+b.user_profile_picture+'" style="width:36px;height:36px; padding-left:5px;border-radius:5px;" alt=""/>'+b.user_profile_name+"</span>",d+="<br>",d+='<span class="created">Posted: '+c.toDateString()+"</span>",d+="<br>",b.location_name?d+='<span class="location">Location: '+b.location_name+"</span>":d+='<span class="location">Lat: '+b.location_latitude.toFixed(2)+" | Lon: "+b.location_longitude.toFixed(2)+"</span>",d+="</div>",d},_prettyDate:function(a){var b=null;typeof a=="number"?b=new Date(a):b=new Date((a||"").replace(/-/g,"/").replace(/[TZ]/g," ")),isNaN(b.getTime())&&(b=new Date(a||""));var c=((new Date).getTime()-b.getTime())/1e3,d=Math.floor(c/86400);if(isNaN(d)||d<0)return;return d===0&&(c<10&&"just now"||c<20&&"10 secs ago"||c<30&&"20 secs ago"||c<40&&"30 secs ago"||c<90&&"1 minute ago"||c<3600&&Math.floor(c/60)+" minutes ago"||c<7200&&"1 hour ago"||c<86400&&Math.floor(c/3600)+" hours ago")||d==1&&"Yesterday"||d<7&&d+" days ago"||d<31&&Math.ceil(d/7)+" weeks ago"||d<365&&Math.ceil(d/30)+" months ago"||"more than a year ago"},_replaceURLWithLinks:function(a){var b=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;return a.replace(b,"<a href='$1' target='_blank'>$1</a>")},_pointToExtent:function(a,b,c){var d=a.extent.getWidth()/a.width,e=c*d;return new h.Extent(b.x-e,b.y-e,b.x+e,b.y+e,a.spatialReference)},_parseURL:function(a){return a.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g,function(a){return' < a target = "_blank" href = "'+a+'" > '+a+" < / a > "})},_getFeatureCollectionTemplate:function(){return{layerDefinition:{geometryType:"esriGeometryPoint",drawingInfo:{renderer:{type:"simple",symbol:{type:"esriPMS",url:this.options.symbolUrl,contentType:"image/"+this.options.symbolUrl.substring(this.options.symbolUrl.lastIndexOf(".")+1),width:this.options.symbolWidth,height:this.options.symbolHeight}}},fields:[{name:"OBJECTID",type:"esriFieldTypeOID"},{name:"id",type:"esriFieldTypeString",alias:"Photo_Id"},{name:"created_time",type:"esriFieldTypeString",alias:"Created"},{name:"likes_count",type:"esriFieldTypeInteger",alias:"Likes"},{name:"link",type:"esriFieldTypeString",alias:"Link"},{name:"attribution",type:"esriFieldTypeString",alias:"Attribution"},{name:"location_latitude",type:"esriFieldTypeDouble",alias:"Latitude"},{name:"location_longitude",type:"esriFieldTypeDouble",alias:"Longitude"},{name:"location_name",type:"esriFieldTypeDouble",alias:"Location"},{name:"photo_link",type:"esriFieldTypeString",alias:"Photo"},{name:"photo_thumbnail",type:"esriFieldTypeString",alias:"Thumbnail"},{name:"caption_text",type:"esriFieldTypeString",alias:"Caption"},{name:"user_profile_name",type:"esriFieldTypeString",alias:"User"},{name:"user_profile_picture",type:"esriFieldTypeString",alias:"Profile"}],globalIdField:"id",displayField:"id"},featureSet:{features:[],geometryType:"esriGeometryPoint"}}},onError:function(a){return new Error(a)},onUpdateStart:function(){},onUpdateEnd:function(){},onUpdate:function(){},onClear:function(){},onClick:function(){}})});