1jQuery(document).ready(function () { 2 3 // Add Twitch online button containers 4 for (var channel in twitchStatus.channels) 5 for (var i in twitchStatus.channels[channel].selectors) 6 jQuery(twitchStatus.channels[channel].selectors[i]).append('<span class="twitch-status-tag twitch-status-channel-' + channel + '" data-twitch-channel="' + channel + '"></span>'); 7 8 twitchStatusUpdate(); // Update Twitch status elements 9 setInterval(twitchStatusUpdate, 300000); // Update every 15 minuti 10 11 // Refresh widget after resize 12 jQuery(window).resize(twitchStatusRefreshWidgetSize); 13 window.addEventListener("orientationchange", twitchStatusRefreshWidgetSize, false); 14 15 twitchStatusRefresh(); // Update HTML elements with existing cached data 16}); 17 18/** 19 * Update Twitch Status HTML elements from AJAX 20 * @returns {undefined} 21 */ 22function twitchStatusUpdate() { 23 var data = { 24 'action': 'get_twitch_channel_status' 25 }; 26 27 jQuery.post(twitchStatus.ajaxurl, data, function (response, status) { 28 if (status !== 'success') 29 return; 30 31 twitchStatus.data = response; 32 33 twitchStatusRefresh(); 34 }); 35} 36 37/** 38 * Update HTML5 elements with provided data 39 * @param {string} prefix 40 * @param {object} data 41 * @param {string} channelName 42 * @returns {undefined} 43 */ 44function twitchStatusUpdateHtmlData(prefix, data, channelName) { 45 46 var channelElements = jQuery('[data-twitch-channel=' + channelName + ']'); 47 48 for (var key in data) { 49 50 // Proceed with sub elements 51 if ((typeof data[key] == 'object') && (data[key] !== null)) 52 twitchStatusUpdateHtmlData(key + '-', data[key], channelName); 53 54 else { 55 56 // Update HTML attribute values 57 var attrSelector = '[data-twitch-attr-' + prefix + key + ']' 58 channelElements.find(attrSelector).addBack(attrSelector).each(function () { 59 var attr = jQuery(this).attr('data-twitch-attr-' + prefix + key).split(','); 60 61 for (var i in attr) 62 jQuery(this).attr(attr[i], data[key]); 63 }); 64 65 // Update inner HTML 66 var dataSelector = '[data-twitch-data=' + prefix + key + ']'; 67 channelElements.find(dataSelector).addBack(dataSelector).each(function () { 68 69 var element = jQuery(this); 70 71 // Do not update if it's an embedded player and it has not been modified to avoid reload 72 if (key.match(/^(player)/) && ((data[key] === '') === (element.html() === ''))) 73 return; 74 75 // Do not update chat once it has been initialized 76 if (key.match(/^(chat)/) && (element.html() !== '')) 77 return; 78 79 if (key.match(/HTML$/)) 80 element.html(data[key]); 81 else 82 element.text(data[key]); 83 }); 84 } 85 } 86} 87 88/** 89 * Update Twitch Status HTML elements with existing data 90 * @returns {undefined} 91 */ 92function twitchStatusRefresh() { 93 for (var c in twitchStatus.data) { 94 var channel = twitchStatus.data[c]; 95 96 if (channel.status === 'error') 97 continue; 98 99 var tag = jQuery('.twitch-status-tag[data-twitch-channel=' + channel.name + '], .twitch-status-tag.twitch-status-channel-' + channel.name); 100 101 // Update status button 102 if (twitchStatus.data[channel.name].status === 'online') { 103 tag.removeClass('twitch-offline'); 104 tag.addClass('twitch-online'); 105 tag.html(twitchStatus.buttonHTML.online); 106 } 107 else { 108 tag.removeClass('twitch-online'); 109 tag.addClass('twitch-offline'); 110 tag.html(twitchStatus.buttonHTML.offline); 111 } 112 113 // Update custom elements 114 twitchStatusUpdateHtmlData('', twitchStatus.data[c], channel.name); 115 116 // Refresh widget 117 twitchStatusRefreshWidget(channel.name); 118 } 119 120 twitchStatusRefreshWidgetSize(); 121} 122 123/** 124 * Refresh Twitch Status widget and player size 125 * @returns {undefined} 126 */ 127function twitchStatusRefreshWidgetSize() { 128 129 // Set narrow mode to Twitch player with chat if the container is not wide enough 130 jQuery('.twitch-player-container.with-chat').each(function () { 131 var w = jQuery(this).parent().width(); 132 133 if (w <= 770) 134 jQuery(this).addClass('narrow-player'); 135 else 136 jQuery(this).removeClass('narrow-player'); 137 }); 138 139 140 // Resize widgets 141 jQuery('.twitch-widget').each(function () { 142 var w = jQuery(this).width();
143 var h = w / (16 / 9); 144 145 jQuery(this).find('.twitch-offline-image').css({ width: w + 'px', height: h + 'px' }); 146 jQuery(this).find('.twitch-play-button, .twitch-offline-caption').css({ lineHeight: h + 'px', width: w + 'px', height: h + 'px', marginTop: -h + 'px' }); 147 }); 148 149 // Resize players 150 jQuery('.twitch-player').each(function () { 151 var w = jQuery(this).parent().width(); 152 var h = w / (16 / 9); 153 154 jQuery(this).css({ width: w + 'px', height: h + 'px' }); 155 }); 156 157 // Resize chat windows 158 jQuery('.twitch-player-container .twitch-chat-col').each(function () { 159 var h = jQuery(this).parent().find('.twitch-player-col').height(); 160 var windowH = jQuery(window).height(); 161 162 if (jQuery(this).closest('.twitch-player-container').is('.narrow-player')) { 163 var h = Math.max(200, windowH - h - 30); 164 } 165 166 jQuery(this).find('iframe').css({ height: h + 'px', 'min-height': '0px' }); 167 }); 168} 169 170/** 171 * Update Twitch status widget 172 * @param {string} channelName 173 * @returns {undefined} 174 */ 175function twitchStatusRefreshWidget(channelName) { 176 177 var channelElements = jQuery('[data-twitch-channel=' + channelName + '], .twitch-status-channel-' + channelName); 178 var loadingElements = channelElements.find('.twitch-is-loading').addBack('.twitch-is-loading'); 179 var onlineElements = channelElements.find('.twitch-is-online').addBack('.twitch-is-online'); 180 var offlineElements = channelElements.find('.twitch-is-offline').addBack('.twitch-is-offline'); 181 182 var channel = twitchStatus.data[channelName]; 183 184 loadingElements.hide(); 185 186 if (!channel) { 187 return; 188 } 189 190 if (channel.status === 'online') { 191 channelElements.find('.twitch-channel-topic').html(channel.title); 192 channelElements.find('.twitch-game').html(channel.game_name); 193 194 channelElements.find('.twitch-viewers').html(channel.viewer_count); 195 //channelElements.find('.twitch-followers').html(channel.followers); // TODO: Not available 196 197 channelElements.find('.twitch-thumbnail-image').each(function(index, element) { 198 var width = jQuery(element.parentElement.parentElement.parentElement).width(); 199 var height = Math.round(width * 9 / 16); 200 var url = channel.thumbnail_url.replace('{width}', width).replace('{height}', height); 201 channelElements.find('.twitch-thumbnail-image').html('<img src="' + url + '">'); 202 }); 203 204 channelElements.find('.twitch-preview').show(); 205 channelElements.find('.twitch-preview-offline').hide(); 206 207 offlineElements.hide(); 208 onlineElements.show(); 209 } 210 else { 211 if (channel.status !== 'error') { 212 channelElements.find('.twitch-offline-image').html('<img src="' + channel.offline_image_url + '">'); 213 } else { 214 channelElements.find('.twitch-offline-image').empty(); 215 } 216 217 channelElements.find('.twitch-preview').hide(); 218 channelElements.find('.twitch-offline-caption').text(channel.statusTxt); 219 channelElements.find('.twitch-preview-offline').show(); 220 221 onlineElements.hide(); 222 offlineElements.show(); 223 } 224}
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.