1// Build A Proper jQuery Plugin... 2(function ($) { 3 4 "use strict"; 5 6 $.fn.socialFeed = function (options) { 7 8 // Instance 9 var $this = this; 10 11 var settings = options; 12 13 // ------------------------------------ 14 // Initialize 15 // ------------------------------------ 16 17 var init = function ($element) { 18 19 // Get Properties 20 var options = { 21 platform: $element.data('social'), 22 searchby: (typeof $element.data('socialSearchBy') !== 'undefined') ? $element.data('socialSearchBy') : false, 23 username: (typeof $element.data('socialUsername') !== 'undefined') ? $element.data('socialUsername') : false, 24 hashtag: (typeof $element.data('socialHashtag') !== 'undefined') ? $element.data('socialHashtag') : true, 25 limit: (typeof $element.data('socialLimit') !== 'undefined') ? $element.data('socialLimit') : false, 26 } 27 28 // Add A Class 29 $element.addClass("js-social"); 30 $element.addClass(options.type); 31 32 // Request 33 var params = {}; 34 35 if (options.searchby == 'username') 36 params.user = options.username; 37 else if (options.searchby == 'hashtag') 38 params.tag = options.hashtag 39 40 if (options.limit) params.limit = options.limit; 41 42 switch (options.platform) { 43 44 case 'instagram': 45 46 $element.addClass('-loading'); 47 // request('/api/instagram?'+ parameters(params),null, $element) 48 request(instagramUrl(options), null, $element, options); 49 50 51 break; 52 53 } 54 55 }; 56 57 var instagramUrl = function (options) { 58 if (options.searchby == 'username') { 59 // New Way 60 return '/api/v1/instagram/media?handle=' + options.username; 61 // Old Way 62 //return 'https://www.instagram.com/'+options.username+'/?__a=1'; 63 } else if (options.searchby == 'hashtag') { 64 return 'https://www.instagram.com/explore/tags/' + options.hashtag + '/?__a=1' 65 } 66 } 67 68 // ------------------------------------ 69 // Request 70 // ------------------------------------ 71 72 var request = function (path, data, $element, options) { 73 $.ajax({ 74 75 url: path, 76 type: 'GET', 77 data: data, 78 success: function (data, textStatus, jqXHR) { 79 80 $element.removeClass('-loading'); 81 setTimeout(function () { $element.addClass('-success'); }, 200); 82 if (settings.callbackSuccess) settings.callbackSuccess(data, $element, res) 83 else callbackSuccess(data, $element, options); 84 85 }, 86 error: function (jqXHR, textStatus, errorThrown) { 87 88 $element.removeClass('-loading'); 89 $element.addClass('-fail'); 90 if (settings.callbackFail) settings.callbackFail($element) 91 else callbackFail($element); 92 93 } 94 95 }); 96 97 }; 98 99 // ------------------------------------ 100 // Callback Success 101 // ------------------------------------ 102 103 var callbackSuccess = function (data, $element, options) { 104 105 if (options.platform == 'instagram' && options.searchby === 'hashtag') { 106 return displayInstagram(data, $element, options); 107 } else if (options.platform == 'instagram' && options.searchby === 'username') { 108 return renderInstagramUserMedia(data, $element, options); 109 } 110 for (var i in data) { 111 112 var $img = $("<img>"); 113 $img.attr('src', data[i].node.thumbnail_src); 114 $element.append($img); 115 116 } 117 118 119 } 120 121 var renderInstagramUserMedia = function (res, $element, options) { 122 var nodes = res.data; 123 var limit = (options.limit) ? options.limit : 12; 124 for (var i in nodes) { 125 if (i >= limit) return; 126 var $a = $('<a>'); 127 $a.attr('href', nodes[i].permalink); 128 $a.attr('target', '_blank'); 129 var $item; 130 if (nodes[i].media_type === 'VIDEO') { 131 $item = $("<img>"); 132 $item.attr('src', nodes[i].thumbnail_url); 133 } else { 134 $item = $("<img>"); 135 $item.attr('src', nodes[i].media_url); 136 } 137 $a.append($item) 138 $element.append($a); 139 } 140 } 141 142 var displayInstagram = function (res, $element, options) { 143 var nodes = (options.searchby == 'username') ? res.graphql.user.edge_owner_to_timeline_media.edges : res.graphql.hashtag.edge_hashtag_to_media.edges; 144 var limit = (options.limit) ? options.limit : 12; 145 for (var i in nodes) { 146 if (i >= limit) return; 147 var $img = $("<img>"); 148 $img.attr('src', nodes[i].node.thumbnail_src); 149 $element.append($img); 150 } 151 } 152 153 // ------------------------------------ 154 // Callback Failed 155 // ------------------------------------ 156 157 var callbackFail = function ($element) { 158 159 $element.html("We're sorry, we are unable to load your feed at this time."); 160 161 } 162 163 164 // ------------------------------------ 165 // Parameters 166 // ------------------------------------ 167 168 var parameters = function (obj) { 169 if (!Object.keys) { 170 Object.keys = function (obj) { 171 var keys = []; 172 for (var i in obj) { 173 if (obj.hasOwnProperty(i)) { 174 keys.push(i); 175 } 176 } 177 return keys; 178 }; 179 } 180 return $.map(Object.keys(obj), function (key) { 181 if (!obj[key]) return ""; 182 return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]); 183 }).join('&'); 184 } 185 186 187 // ------------------------------------ 188 // Ready, Set, Go 189 // ------------------------------------ 190 191 return this.each(function () { 192 193 init($(this)); 194 195 }); 196 197 }; 198 199}(jQuery)); 200 201 202jQuery(document).ready(function ($) { 203 204 // Instagram 205 $('[data-social="instagram"]').socialFeed({}); 206 207 208});
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.