| 1 |
/* |
| 2 |
* WpStream admin settings page behaviour. |
| 3 |
* |
| 4 |
* Handles the "WpStream Settings" admin screen: it auto-saves the Default |
| 5 |
* Channel Settings tab and the individual setting controls (checkboxes, text |
| 6 |
* inputs, selects, range sliders) to admin-ajax.php as the user changes them, |
| 7 |
* shows a transient error message when a save fails, and drives the "update |
| 8 |
* plugin" button on the support tab. wpstream_broadcaster_bind() is a helper |
| 9 |
* (currently not called) that opens the browser broadcaster window. |
| 10 |
*/ |
| 11 |
|
| 12 |
// Shared handle for debouncing rapid text-input saves. |
| 13 |
var debounceTimer; |
| 14 |
|
| 15 |
// Wire up all settings-page handlers once the DOM is ready. |
| 16 |
jQuery(document).ready(function($) { |
| 17 |
// Save default channel options |
| 18 |
wpstream_save_default_channel_options(); |
| 19 |
|
| 20 |
// Save settings |
| 21 |
wpstream_save_settings(); |
| 22 |
|
| 23 |
// Bind the "update plugin" button on the support tab. |
| 24 |
wpstream_update_plugin_support_tab(); |
| 25 |
|
| 26 |
// wpstream_broadcaster_bind(); |
| 27 |
}); |
| 28 |
|
| 29 |
/** |
| 30 |
* Show the encryption / speed-feature exclusion on the settings screen. |
| 31 |
* |
| 32 |
* Encrypted delivery cannot run together with low latency or adaptive bitrate, |
| 33 |
* so enabling either side clears the other. The server applies this rule too; |
| 34 |
* doing it here is what lets the customer see which setting they gave up |
| 35 |
* instead of finding out after the save. |
| 36 |
* |
| 37 |
* This screen owns its copy deliberately: the equivalent helper lives in |
| 38 |
* public/js/start_streaming.js, which is only loaded on the channel screens. |
| 39 |
* Calling across to it left this handler throwing a ReferenceError before it |
| 40 |
* reached the save, so no default channel setting was ever stored. |
| 41 |
* |
| 42 |
* @param {jQuery} clicked The option toggle the user just clicked. |
| 43 |
* @return {void} |
| 44 |
*/ |
| 45 |
function wpstream_settings_apply_exclusion(clicked) { |
| 46 |
// Which setting was toggled, and the group it belongs to. |
| 47 |
var setting = clicked.attr('data-attr-ajaxname'); |
| 48 |
var group = clicked.closest('.theme_options_tab_wpstream'); |
| 49 |
var speed = group.find('.wpstream-setting-low_latency .wpstream_event_option_item, .wpstream-setting-adaptive_bitrate .wpstream_event_option_item'); |
| 50 |
var encrypt = group.find('.wpstream-setting-encrypt .wpstream_event_option_item'); |
| 51 |
|
| 52 |
// Asking for speed gives up encryption. |
| 53 |
if (setting === 'low_latency' || setting === 'adaptive_bitrate') { |
| 54 |
if (clicked.prop('checked')) { |
| 55 |
encrypt.prop('checked', false); |
| 56 |
} |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// And asking for encryption gives up both speed features. |
| 61 |
if (setting === 'encrypt' && clicked.prop('checked')) { |
| 62 |
speed.prop('checked', false); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Save the changes to the Default Channel Settings tab |
| 68 |
* |
| 69 |
* Binds each option toggle so that, shortly after a click, all toggles in the |
| 70 |
* same group are collected and persisted together via AJAX. |
| 71 |
* |
| 72 |
* @return {void} |
| 73 |
*/ |
| 74 |
function wpstream_save_default_channel_options() { |
| 75 |
// Respond to clicks on any default-channel option toggle. |
| 76 |
jQuery('.theme_options_tab_wpstream .wpstream_event_option_item').on('click',function() { |
| 77 |
|
| 78 |
// Apply any dependent UI adjustments for the clicked option. |
| 79 |
wpstream_settings_apply_exclusion(jQuery(this)); |
| 80 |
|
| 81 |
// Accumulator for the option name/value pairs to send. |
| 82 |
var optionarray ={}; |
| 83 |
// The wrapper that groups the related option toggles. |
| 84 |
var holder = jQuery(this).parents('.wpstream_option_wrapper'); |
| 85 |
// Security nonce read from a hidden field on the page. |
| 86 |
var nonce = jQuery('#wpstream-settings-nonce').val(); |
| 87 |
|
| 88 |
// Show the saving spinner. |
| 89 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','visible'); |
| 90 |
// Brief delay so the toggle's checked state settles before we read it. |
| 91 |
var timer = setTimeout(function() { |
| 92 |
// Collect every option in the group into name => 1/0 pairs. |
| 93 |
holder.find('.wpstream_event_option_item').each(function(){ |
| 94 |
optionarray[jQuery(this).attr('data-attr-ajaxname')]=jQuery(this).prop("checked") ? 1 : 0 ; |
| 95 |
}); |
| 96 |
|
| 97 |
// JSON copy of the option map (kept for potential debugging/use). |
| 98 |
var jsonOptions = JSON.stringify(optionarray); |
| 99 |
// Persist the whole option group in one request. |
| 100 |
jQuery.ajax({ |
| 101 |
type: 'POST', |
| 102 |
url: ajaxurl, |
| 103 |
timeout: 300000, |
| 104 |
data: { |
| 105 |
'action' : 'wpstream_update_default_channel_settings', |
| 106 |
'option' : optionarray, |
| 107 |
'security' : nonce |
| 108 |
}, |
| 109 |
success: function (data) { |
| 110 |
// Hide the spinner once the save completes. |
| 111 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','hidden'); |
| 112 |
}, |
| 113 |
error: function (jqXHR,textStatus,errorThrown) { |
| 114 |
// Show a transient error message on failure. |
| 115 |
wpstream_show_error_message(jQuery('.theme_options_tab_wpstream .wpstream-save-settings')); |
| 116 |
} |
| 117 |
}) |
| 118 |
}, 300); |
| 119 |
}); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Adjust the settings for WpStream Settings |
| 124 |
* |
| 125 |
* Binds change/input handlers on the individual setting controls (checkboxes, |
| 126 |
* text inputs, selects and range sliders) so each one is saved via AJAX as it |
| 127 |
* changes. Text inputs are debounced to avoid a save on every keystroke. |
| 128 |
* |
| 129 |
* @return {void} |
| 130 |
*/ |
| 131 |
function wpstream_save_settings() { |
| 132 |
// Security nonce shared by all the save requests below. |
| 133 |
var nonce = jQuery('#wpstream-settings-nonce').val(); |
| 134 |
// Checkbox settings: save immediately on change. |
| 135 |
jQuery('.wpstream_option_wrapper .wpstream_event_option_itemc').on('change',function(e){ |
| 136 |
// Get the new value (1 if checked, 0 if unchecked) |
| 137 |
var checkedState = e.target.checked ? 1 : 0; |
| 138 |
|
| 139 |
// Show the saving spinner. |
| 140 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','visible'); |
| 141 |
// Persist this single checkbox setting. |
| 142 |
jQuery.ajax({ |
| 143 |
type: 'POST', |
| 144 |
url: ajaxurl, |
| 145 |
timeout: 300000, |
| 146 |
data: { |
| 147 |
'action' : 'wpstream_update_settings', |
| 148 |
'option_name' : jQuery(this).attr('name'), |
| 149 |
'option_type' : jQuery(this).attr('type'), |
| 150 |
'option_value' : checkedState, |
| 151 |
'security' : nonce |
| 152 |
}, |
| 153 |
success: function (data) { |
| 154 |
// Hide the spinner once the save completes. |
| 155 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','hidden'); |
| 156 |
}, |
| 157 |
error: function (jqXHR,textStatus,errorThrown) { |
| 158 |
// Show a transient error message on failure. |
| 159 |
wpstream_show_error_message(jQuery('.theme_options_tab_wpstream .wpstream-save-settings')); |
| 160 |
} |
| 161 |
}) |
| 162 |
}) |
| 163 |
|
| 164 |
// Text inputs: save after the user pauses typing (debounced). |
| 165 |
jQuery('.wpstream_option_wrapper .wpstream-text-input-setting').on('input',function(e){ |
| 166 |
// Read the option identity, value and type from the input. |
| 167 |
var option_name = jQuery(this).attr('id'); |
| 168 |
var option_value = jQuery(this).val(); |
| 169 |
var option_type = jQuery(this).attr('type'); |
| 170 |
|
| 171 |
// Reset the pending debounce timer on every keystroke. |
| 172 |
clearTimeout(debounceTimer); |
| 173 |
debounceTimer = setTimeout( function() { |
| 174 |
// Show the saving spinner. |
| 175 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','visible'); |
| 176 |
// Persist this text setting. |
| 177 |
jQuery.ajax({ |
| 178 |
type: 'POST', |
| 179 |
url: ajaxurl, |
| 180 |
timeout: 300000, |
| 181 |
data: { |
| 182 |
'action' : 'wpstream_update_settings', |
| 183 |
'option_name' : option_name, |
| 184 |
'option_type' : option_type, |
| 185 |
'option_value' : option_value, |
| 186 |
'security' : nonce |
| 187 |
}, |
| 188 |
success: function (data) { |
| 189 |
// Hide the spinner once the save completes. |
| 190 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','hidden'); |
| 191 |
}, |
| 192 |
error: function (jqXHR,textStatus,errorThrown) { |
| 193 |
// Show a transient error message on failure. |
| 194 |
wpstream_show_error_message(jQuery('.theme_options_tab_wpstream .wpstream-save-settings')); |
| 195 |
} |
| 196 |
}) |
| 197 |
}, 1000); // Wait for 3 seconds after the user stops typing |
| 198 |
}); |
| 199 |
|
| 200 |
// Select / multi-select settings: save immediately on change. |
| 201 |
jQuery('.wpstream_option_wrapper select').on('change',function(e){ |
| 202 |
// Derive the option name from the associated label's "for" attribute. |
| 203 |
var option_name = jQuery(this).siblings('label').attr('for'); |
| 204 |
var option_value = jQuery(this).val(); |
| 205 |
// Distinguish single vs multiple selects for the server. |
| 206 |
var option_type = jQuery(this).prop('multiple') ? 'multiple-select' : 'select'; |
| 207 |
|
| 208 |
// Show the saving spinner. |
| 209 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','visible'); |
| 210 |
// Persist this select setting. |
| 211 |
jQuery.ajax({ |
| 212 |
type: 'POST', |
| 213 |
url: ajaxurl, |
| 214 |
timeout: 300000, |
| 215 |
data: { |
| 216 |
'action' : 'wpstream_update_settings', |
| 217 |
'option_name' : option_name, |
| 218 |
'option_type' : option_type, |
| 219 |
'option_value' : option_value, |
| 220 |
'security' : nonce |
| 221 |
}, |
| 222 |
success: function (data) { |
| 223 |
// Hide the spinner once the save completes. |
| 224 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','hidden'); |
| 225 |
}, |
| 226 |
error: function (jqXHR,textStatus,errorThrown) { |
| 227 |
// Show a transient error message on failure. |
| 228 |
wpstream_show_error_message(jQuery('.theme_options_tab_wpstream .wpstream-save-settings')); |
| 229 |
} |
| 230 |
}) |
| 231 |
}) |
| 232 |
|
| 233 |
// Range slider settings: save immediately on change. |
| 234 |
jQuery('.wpstream_option_wrapper .wpstream-range-input').on('change',function(e){ |
| 235 |
// Read the option identity, value and type from the slider. |
| 236 |
var option_name = jQuery(this).attr('id'); |
| 237 |
var option_value = jQuery(this).val(); |
| 238 |
var option_type = jQuery(this).attr('type'); |
| 239 |
|
| 240 |
// Show the saving spinner. |
| 241 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','visible'); |
| 242 |
// Persist this range setting. |
| 243 |
jQuery.ajax({ |
| 244 |
type: 'POST', |
| 245 |
url: ajaxurl, |
| 246 |
timeout: 300000, |
| 247 |
data: { |
| 248 |
'action' : 'wpstream_update_settings', |
| 249 |
'option_name' : option_name, |
| 250 |
'option_type' : option_type, |
| 251 |
'option_value' : option_value, |
| 252 |
'security' : nonce |
| 253 |
}, |
| 254 |
success: function (data) { |
| 255 |
// Hide the spinner once the save completes. |
| 256 |
jQuery('.theme_options_tab_wpstream .wpstream-save-settings').find('.spinner').css('visibility','hidden'); |
| 257 |
}, |
| 258 |
error: function (jqXHR,textStatus,errorThrown) { |
| 259 |
// Show a transient error message on failure. |
| 260 |
wpstream_show_error_message(jQuery('.theme_options_tab_wpstream .wpstream-save-settings')); |
| 261 |
} |
| 262 |
}) |
| 263 |
}) |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* |
| 268 |
* Show error message when the settings are not saved |
| 269 |
* |
| 270 |
*/ |
| 271 |
/** |
| 272 |
* Show a transient "could not save" error message inside a container. |
| 273 |
* |
| 274 |
* @param {jQuery} container The settings-save wrapper to display the error in. |
| 275 |
* @return {void} |
| 276 |
*/ |
| 277 |
function wpstream_show_error_message(container) { |
| 278 |
// Hide the spinner (the save has failed, not still running). |
| 279 |
container.find('.spinner').css('visibility', 'hidden'); |
| 280 |
// Append the localized error message. |
| 281 |
container.append('<div class="wpstream-error-message">' + wpstream_settings_vars.error_message + '</div>'); |
| 282 |
// Fade the message in, hold it briefly, then fade out and remove it. |
| 283 |
container.find('.wpstream-error-message').hide().fadeIn(400).delay(3000).fadeOut(400, function() { |
| 284 |
jQuery(this).remove(); |
| 285 |
}); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Bind the "update plugin" button on the support tab to an AJAX self-update. |
| 290 |
* |
| 291 |
* @return {void} |
| 292 |
*/ |
| 293 |
function wpstream_update_plugin_support_tab() { |
| 294 |
// Security nonce read from a hidden field on the page. |
| 295 |
var nonce = jQuery('#wpstream-settings-nonce').val(); |
| 296 |
// Handle clicks on the update-plugin button. |
| 297 |
jQuery('.wpstream-update-plugin-button').on('click', function(e) { |
| 298 |
e.preventDefault(); |
| 299 |
// Replace the button with an "Updating..." indicator. |
| 300 |
jQuery(this).parents('.update-button-wrapper').html('Updating... <span class="spinner" style="visibility: visible;"></span>'); |
| 301 |
// Ask the server to run the plugin update. |
| 302 |
jQuery.ajax({ |
| 303 |
type: 'POST', |
| 304 |
url: ajaxurl, |
| 305 |
timeout: 300000, |
| 306 |
data: { |
| 307 |
'action' : 'wpstream_settings_tab_update_plugin', |
| 308 |
'security' : nonce |
| 309 |
}, |
| 310 |
success: function (data) { |
| 311 |
// Report success or failure in place of the button. |
| 312 |
if (data.success) { |
| 313 |
jQuery('.update-button-wrapper').html(wpstream_settings_vars.update_successful); |
| 314 |
} else { |
| 315 |
jQuery('.update-button-wrapper').html(wpstream_settings_vars.update_failed); |
| 316 |
} |
| 317 |
}, |
| 318 |
error: function (jqXHR,textStatus,errorThrown) { |
| 319 |
// Show the failure message on a transport error. |
| 320 |
jQuery('.update-button-wrapper').html(wpstream_settings_vars.update_failed); |
| 321 |
} |
| 322 |
}) |
| 323 |
}); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Open the browser broadcaster for a channel in a new window. |
| 328 |
* |
| 329 |
* Bound to the "start webcaster" control; ignored when the control is inactive |
| 330 |
* or has no channel id. (Helper is defined but not currently invoked.) |
| 331 |
* |
| 332 |
* @return {void} |
| 333 |
*/ |
| 334 |
function wpstream_broadcaster_bind() { |
| 335 |
// Handle clicks on the start-webcaster control. |
| 336 |
jQuery('.start_webcaster').on('click', function(e) { |
| 337 |
// Do nothing if the control is marked inactive. |
| 338 |
if (jQuery(this).hasClass('wpstream_inactive_icon')) { |
| 339 |
return; |
| 340 |
} |
| 341 |
e.preventDefault(); |
| 342 |
e.stopPropagation(); |
| 343 |
|
| 344 |
// Get the channel ID from the data attribute |
| 345 |
var channelId = jQuery(this).closest('.event_list_unit').data('show-id'); |
| 346 |
|
| 347 |
// Bail out if no channel id is available. |
| 348 |
if (!channelId) { |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
// Open the broadcaster in a new window |
| 353 |
var broadcasterUrl = wpstream_settings_vars.broadcaster_url + channelId; |
| 354 |
window.open(broadcasterUrl, 'wpstream_broadcaster_' + channelId, 'fullscreen=yes'); |
| 355 |
}); |
| 356 |
|
| 357 |
} |