1// Generated by CoffeeScript 1.9.3 2( function () { 3 var Instafeed; 4 Instafeed = ( function () { 5 function Instafeed( params, context ) { 6 var option, value; 7 this.options = { 8 target: 'instafeed', 9 get: 'popular', 10 resolution: 'thumbnail', 11 sortBy: 'none', 12 links: true, 13 mock: false, 14 useHttp: false 15 }; 16 if ( typeof params === 'object' ) { 17 for ( option in params ) { 18 value = params[ option ]; 19 this.options[ option ] = value; 20 } 21 } 22 this.context = context != null ? context : this; 23 this.unique = this._genKey(); 24 } 25 Instafeed.prototype.hasNext = function () { 26 return typeof this.context.nextUrl === 'string' && this.context.nextUrl.length > 0; 27 }; 28 Instafeed.prototype.next = function () { 29 if ( !this.hasNext() ) { 30 return false; 31 } 32 return this.run( this.context.nextUrl ); 33 }; 34 Instafeed.prototype.run = function ( url ) { 35 var header, instanceName, script; 36 if ( typeof this.options.clientId !== 'string' ) { 37 if ( typeof this.options.accessToken !== 'string' ) { 38 throw new Error( "Missing clientId or accessToken." ); 39 } 40 } 41 if ( typeof this.options.accessToken !== 'string' ) { 42 if ( typeof this.options.clientId !== 'string' ) { 43 throw new Error( "Missing clientId or accessToken." ); 44 } 45 } 46 if ( ( this.options.before != null ) && typeof this.options.before === 'function' ) { 47 this.options.before.call( this ); 48 } 49 if ( typeof document !== "undefined" && document !== null ) { 50 script = document.createElement( 'script' ); 51 script.id = 'instafeed-fetcher'; 52 script.src = url || this._buildUrl(); 53 header = document.getElementsByTagName( 'head' ); 54 header[ 0 ].appendChild( script ); 55 instanceName = "instafeedCache" + this.unique; 56 window[ instanceName ] = new Instafeed( this.options, this ); 57 window[ instanceName ].unique = this.unique; 58 } 59 return true; 60 }; 61 Instafeed.prototype.parse = function ( response ) { 62 var anchor, childNodeCount, childNodeIndex, childNodesArr, e, eMsg, fragment, header, htmlString, httpProtocol, i, image, imageObj, imageString, imageUrl, images, img, imgHeight, imgOrient, imgUrl, imgWidth, instanceName, j, k, len, len1, len2, node, parsedLimit, reverse, sortSettings, targetEl, tmpEl; 63 if ( typeof response !== 'object' ) { 64 if ( ( this.options.error != null ) && typeof this.options.error === 'function' ) { 65 this.options.error.call( this, 'Invalid JSON data' ); 66 return false; 67 } 68 else { 69 throw new Error( 'Invalid JSON response' ); 70 } 71 } 72 if ( response.meta.code !== 200 ) { 73 if ( ( this.options.error != null ) && typeof this.options.error === 'function' ) { 74 this.options.error.call( this, response.meta.error_message ); 75 return false; 76 } 77 else { 78 throw new Error( "Error from Instagram: " + response.meta.error_message ); 79 } 80 } 81 if ( response.data.length === 0 ) { 82 if ( ( this.options.error != null ) && typeof this.options.error === 'function' ) { 83 this.options.error.call( this, 'No images were returned from Instagram' ); 84 return false; 85 } 86 else { 87 throw new Error( 'No images were returned from Instagram' ); 88 } 89 } 90 if ( ( this.options.success != null ) && typeof this.options.success === 'function' ) { 91 this.options.success.call( this, response ); 92 } 93 this.context.nextUrl = ''; 94 if ( response.pagination != null ) { 95 this.context.nextUrl = response.pagination.next_url; 96 } 97 if ( this.options.sortBy !== 'none' ) { 98 if ( this.options.sortBy === 'random' ) { 99 sortSettings = [ '', 'random' ]; 100 } 101 else { 102 sortSettings = this.options.sortBy.split( '-' ); 103 } 104 reverse = sortSettings[ 0 ] === 'least' ? true : false; 105 switch ( sortSettings[ 1 ] ) { 106 case 'random': 107 response.data.sort( function () { 108 return 0.5 - Math.random(); 109 } ); 110 break; 111 case 'recent': 112 response.data = this._sortBy( response.data, 'created_time', reverse ); 113 break; 114 case 'liked': 115 response.data = this._sortBy( response.data, 'likes.count', reverse ); 116 break; 117 case 'commented': 118 response.data = this._sortBy( response.data, 'comments.count', reverse ); 119 break; 120 default: 121 throw new Error( "Invalid option for sortBy: '" + this.options.sortBy + "'." ); 122 } 123 } 124 if ( ( typeof document !== "undefined" && document !== null ) && this.options.mock === false ) { 125 images = response.data; 126 parsedLimit = parseInt( this.options.limit, 10 ); 127 if ( ( this.options.limit != null ) && images.length > parsedLimit ) { 128 images = images.slice( 0, parsedLimit ); 129 } 130 fragment = document.createDocumentFragment(); 131 if ( ( this.options.filter != null ) && typeof this.options.filter === 'function' ) { 132 images = this._filter( images, this.options.filter ); 133 } 134 if ( ( this.options.template != null ) && typeof this.options.template === 'string' ) { 135 htmlString = ''; 136 imageString = ''; 137 imgUrl = ''; 138 tmpEl = document.createElement( 'div' ); 139 for ( i = 0, len = images.length; i < len; i++ ) { 140 image = images[ i ]; 141 imageObj = image.images[ this.options.resolution ]; 142 if ( typeof imageObj !== 'object' ) { 143 eMsg = "No image found for resolution: " + this.options.resolution + "."; 144 throw new Error( eMsg ); 145 } 146 imgWidth = imageObj.width; 147 imgHeight = imageObj.height; 148 imgOrient = "square"; 149 if ( imgWidth > imgHeight ) { 150 imgOrient = "landscape"; 151 } 152 if ( imgWidth < imgHeight ) { 153 imgOrient = "portrait"; 154 } 155 imageUrl = imageObj.url; 156 httpProtocol = window.location.protocol.indexOf( "http" ) >= 0; 157 if ( httpProtocol && !this.options.useHttp ) { 158 imageUrl = imageUrl.replace( /https?:\/\//, '//' ); 159 } 160 imageString = this._makeTemplate( this.options.template, { 161 model: image, 162 id: image.id, 163 link: image.link, 164 type: image.type, 165 image: imageUrl, 166 width: imgWidth, 167 height: imgHeight, 168 orientation: imgOrient, 169 caption: this._getObjectProperty( image, 'caption.text' ),
170 likes: image.likes.count, 171 comments: image.comments.count, 172 location: this._getObjectProperty( image, 'location.name' ) 173 } ); 174 htmlString += imageString; 175 } 176 tmpEl.innerHTML = htmlString; 177 childNodesArr = []; 178 childNodeIndex = 0; 179 childNodeCount = tmpEl.childNodes.length; 180 while ( childNodeIndex < childNodeCount ) { 181 childNodesArr.push( tmpEl.childNodes[ childNodeIndex ] ); 182 childNodeIndex += 1; 183 } 184 for ( j = 0, len1 = childNodesArr.length; j < len1; j++ ) { 185 node = childNodesArr[ j ]; 186 fragment.appendChild( node ); 187 } 188 } 189 else { 190 for ( k = 0, len2 = images.length; k < len2; k++ ) { 191 image = images[ k ]; 192 img = document.createElement( 'img' ); 193 imageObj = image.images[ this.options.resolution ]; 194 if ( typeof imageObj !== 'object' ) { 195 eMsg = "No image found for resolution: " + this.options.resolution + "."; 196 throw new Error( eMsg ); 197 } 198 imageUrl = imageObj.url; 199 httpProtocol = window.location.protocol.indexOf( "http" ) >= 0; 200 if ( httpProtocol && !this.options.useHttp ) { 201 imageUrl = imageUrl.replace( /https?:\/\//, '//' ); 202 } 203 img.src = imageUrl; 204 if ( this.options.links === true ) { 205 anchor = document.createElement( 'a' ); 206 anchor.href = image.link; 207 anchor.appendChild( img ); 208 fragment.appendChild( anchor ); 209 } 210 else { 211 fragment.appendChild( img ); 212 } 213 } 214 } 215 targetEl = this.options.target; 216 if ( typeof targetEl === 'string' ) { 217 targetEl = document.getElementById( targetEl ); 218 } 219 if ( targetEl == null ) { 220 eMsg = "No element with id=\"" + this.options.target + "\" on page."; 221 throw new Error( eMsg ); 222 } 223 targetEl.appendChild( fragment ); 224 header = document.getElementsByTagName( 'head' )[ 0 ]; 225 header.removeChild( document.getElementById( 'instafeed-fetcher' ) ); 226 instanceName = "instafeedCache" + this.unique; 227 window[ instanceName ] = void 0; 228 try { 229 delete window[ instanceName ]; 230 } 231 catch ( _error ) { 232 e = _error; 233 } 234 } 235 if ( ( this.options.after != null ) && typeof this.options.after === 'function' ) { 236 this.options.after.call( this ); 237 } 238 return true; 239 }; 240 Instafeed.prototype._buildUrl = function () { 241 var base, endpoint, final; 242 base = "https://api.instagram.com/v1"; 243 switch ( this.options.get ) { 244 case "popular": 245 endpoint = "media/popular"; 246 break; 247 case "tagged": 248 if ( !this.options.tagName ) { 249 throw new Error( "No tag name specified. Use the 'tagName' option." ); 250 } 251 endpoint = "tags/" + this.options.tagName + "/media/recent"; 252 break; 253 case "location": 254 if ( !this.options.locationId ) { 255 throw new Error( "No location specified. Use the 'locationId' option." ); 256 } 257 endpoint = "locations/" + this.options.locationId + "/media/recent"; 258 break; 259 case "user": 260 if ( !this.options.userId ) { 261 throw new Error( "No user specified. Use the 'userId' option." ); 262 } 263 endpoint = "users/" + this.options.userId + "/media/recent"; 264 break; 265 default: 266 throw new Error( "Invalid option for get: '" + this.options.get + "'." ); 267 }
268 final = base + "/" + endpoint; 269 if ( this.options.accessToken != null ) { 270 final += "?access_token=" + this.options.accessToken; 271 } 272 else { 273 final += "?client_id=" + this.options.clientId; 274 } 275 if ( this.options.limit != null ) { 276 final += "&count=" + this.options.limit; 277 } 278 final += "&callback=instafeedCache" + this.unique + ".parse"; 279 return final; 280 }; 281 Instafeed.prototype._genKey = function () { 282 var S4; 283 S4 = function () { 284 return ( ( ( 1 + Math.random() ) * 0x10000 ) | 0 ).toString( 16 ).substring( 1 ); 285 }; 286 return "" + ( S4() ) + ( S4() ) + ( S4() ) + ( S4() ); 287 }; 288 Instafeed.prototype._makeTemplate = function ( template, data ) { 289 var output, pattern, ref, varName, varValue; 290 pattern = /(?:\{{2})([\w\[\]\.]+)(?:\}{2})/; 291 output = template; 292 while ( pattern.test( output ) ) { 293 varName = output.match( pattern )[ 1 ]; 294 varValue = ( ref = this._getObjectProperty( data, varName ) ) != null ? ref : ''; 295 output = output.replace( pattern, function () { 296 return "" + varValue; 297 } ); 298 } 299 return output; 300 }; 301 Instafeed.prototype._getObjectProperty = function ( object, property ) { 302 var piece, pieces; 303 property = property.replace( /\[(\w+)\]/g, '.$1' ); 304 pieces = property.split( '.' ); 305 while ( pieces.length ) { 306 piece = pieces.shift(); 307 if ( ( object != null ) && piece in object ) { 308 object = object[ piece ]; 309 } 310 else { 311 return null; 312 } 313 } 314 return object; 315 }; 316 Instafeed.prototype._sortBy = function ( data, property, reverse ) { 317 var sorter; 318 sorter = function ( a, b ) { 319 var valueA, valueB; 320 valueA = this._getObjectProperty( a, property ); 321 valueB = this._getObjectProperty( b, property ); 322 if ( reverse ) { 323 if ( valueA > valueB ) { 324 return 1; 325 } 326 else { 327 return -1; 328 } 329 } 330 if ( valueA < valueB ) { 331 return 1; 332 } 333 else { 334 return -1; 335 } 336 }; 337 data.sort( sorter.bind( this ) ); 338 return data; 339 }; 340 Instafeed.prototype._filter = function ( images, filter ) { 341 var filteredImages, fn, i, image, len; 342 filteredImages = []; 343 fn = function ( image ) { 344 if ( filter( image ) ) { 345 return filteredImages.push( image ); 346 } 347 }; 348 for ( i = 0, len = images.length; i < len; i++ ) { 349 image = images[ i ]; 350 fn( image ); 351 } 352 return filteredImages; 353 }; 354 return Instafeed; 355 } )(); 356 ( function ( root, factory ) { 357 if ( typeof define === 'function' && define.amd ) { 358 return define( [], factory ); 359 } 360 else if ( typeof module === 'object' && module.exports ) { 361 return module.exports = factory(); 362 } 363 else { 364 return root.Instafeed = factory(); 365 } 366 } )( this, function () { 367 return Instafeed; 368 } ); 369} ).call( this );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.