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