| 1 |
/* globals wp_stream, ajaxurl */ |
| 2 |
jQuery( function( $ ) { |
| 3 |
|
| 4 |
// Shorter timeago strings for English locale |
| 5 |
if ( 'en' === wp_stream.locale && 'undefined' !== typeof $.timeago ) { |
| 6 |
$.timeago.settings.strings.seconds = 'seconds'; |
| 7 |
$.timeago.settings.strings.minute = 'a minute'; |
| 8 |
$.timeago.settings.strings.hour = 'an hour'; |
| 9 |
$.timeago.settings.strings.hours = '%d hours'; |
| 10 |
$.timeago.settings.strings.month = 'a month'; |
| 11 |
$.timeago.settings.strings.year = 'a year'; |
| 12 |
} |
| 13 |
|
| 14 |
$( 'li.toplevel_page_wp_stream ul li.wp-first-item.current' ).parent().parent().find( '.update-plugins' ).remove(); |
| 15 |
|
| 16 |
$( '.toplevel_page_wp_stream :input.chosen-select' ).each( function( i, el ) { |
| 17 |
var args = {}, |
| 18 |
formatResult = function( record, container ) { |
| 19 |
var result = '', |
| 20 |
$elem = $( record.element ), |
| 21 |
icon = ''; |
| 22 |
|
| 23 |
if ( '- ' === record.text.substring( 0, 2 ) ) { |
| 24 |
record.text = record.text.substring( 2 ); |
| 25 |
} |
| 26 |
|
| 27 |
if ( undefined !== record.icon ) { |
| 28 |
icon = record.icon; |
| 29 |
} else if ( undefined !== $elem.attr( 'data-icon' ) ) { |
| 30 |
icon = $elem.data( 'icon' ); |
| 31 |
} |
| 32 |
if ( icon ) { |
| 33 |
result += '<img src="' + icon + '" class="wp-stream-select2-icon">'; |
| 34 |
} |
| 35 |
|
| 36 |
result += record.text; |
| 37 |
|
| 38 |
// Add more info to the container |
| 39 |
container.attr( 'title', $elem.attr( 'title' ) ); |
| 40 |
|
| 41 |
return result; |
| 42 |
}, |
| 43 |
formatSelection = function( record ) { |
| 44 |
if ( '- ' === record.text.substring( 0, 2 ) ) { |
| 45 |
record.text = record.text.substring( 2 ); |
| 46 |
} |
| 47 |
return record.text; |
| 48 |
}; |
| 49 |
|
| 50 |
if ( $( el ).find( 'option' ).length > 0 ) { |
| 51 |
args = { |
| 52 |
minimumResultsForSearch: 10, |
| 53 |
formatResult: formatResult, |
| 54 |
formatSelection: formatSelection, |
| 55 |
allowClear: true, |
| 56 |
width: '165px' |
| 57 |
}; |
| 58 |
} else { |
| 59 |
args = { |
| 60 |
minimumInputLength: 3, |
| 61 |
allowClear: true, |
| 62 |
width: '165px', |
| 63 |
ajax: { |
| 64 |
url: ajaxurl, |
| 65 |
datatype: 'json', |
| 66 |
data: function( term ) { |
| 67 |
return { |
| 68 |
action: 'wp_stream_filters', |
| 69 |
filter: $( el ).attr( 'name' ), |
| 70 |
q: term |
| 71 |
}; |
| 72 |
}, |
| 73 |
results: function( data ) { |
| 74 |
return { results: data }; |
| 75 |
} |
| 76 |
}, |
| 77 |
formatResult: formatResult, |
| 78 |
formatSelection: formatSelection, |
| 79 |
initSelection: function( element, callback ) { |
| 80 |
var id = $( element ).val(); |
| 81 |
if ( '' !== id ) { |
| 82 |
$.post( |
| 83 |
ajaxurl, |
| 84 |
{ |
| 85 |
action: 'wp_stream_get_filter_value_by_id', |
| 86 |
filter: $( element ).attr( 'name' ), |
| 87 |
id: id |
| 88 |
}, |
| 89 |
function( response ) { |
| 90 |
callback({ |
| 91 |
id: id, |
| 92 |
text: response |
| 93 |
}); |
| 94 |
}, |
| 95 |
'json' |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
$( el ).select2( args ); |
| 103 |
}); |
| 104 |
|
| 105 |
var $queryVars = $.streamGetQueryVars(); |
| 106 |
var $contextInput = $( '.toplevel_page_wp_stream :input.chosen-select[name="context"]' ); |
| 107 |
|
| 108 |
if ( ( 'undefined' === typeof $queryVars.context || '' === $queryVars.context ) && 'undefined' !== typeof $queryVars.connector ) { |
| 109 |
$contextInput.select2( 'val', 'group-' + $queryVars.connector ); |
| 110 |
} |
| 111 |
|
| 112 |
$( '#record-filter-form' ).submit( function() { |
| 113 |
var $context = $( '.toplevel_page_wp_stream :input.chosen-select[name="context"]' ), |
| 114 |
$option = $context.find( 'option:selected' ), |
| 115 |
$connector = $context.parent().find( '.record-filter-connector' ), |
| 116 |
optionConnector = $option.data( 'group' ), |
| 117 |
optionClass = $option.prop( 'class' ); |
| 118 |
|
| 119 |
$connector.val( optionConnector ); |
| 120 |
|
| 121 |
if ( 'level-1' === optionClass ) { |
| 122 |
$option.val( '' ); |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
$( window ).load( function() { |
| 127 |
$( '.toplevel_page_wp_stream input[type="search"]' ).off( 'mousedown' ); |
| 128 |
}); |
| 129 |
|
| 130 |
// Confirmation on some important actions |
| 131 |
$( 'body' ).on( 'click', '#wp_stream_advanced_delete_all_records, #wp_stream_network_advanced_delete_all_records', function( e ) { |
| 132 |
if ( ! window.confirm( wp_stream.i18n.confirm_purge ) ) { |
| 133 |
e.preventDefault(); |
| 134 |
} |
| 135 |
}); |
| 136 |
|
| 137 |
$( 'body' ).on( 'click', '#wp_stream_advanced_reset_site_settings, #wp_stream_network_advanced_reset_site_settings', function( e ) { |
| 138 |
if ( ! window.confirm( wp_stream.i18n.confirm_defaults ) ) { |
| 139 |
e.preventDefault(); |
| 140 |
} |
| 141 |
}); |
| 142 |
|
| 143 |
$( 'body' ).on( 'click', '#wp_stream_uninstall', function( e ) { |
| 144 |
if ( ! window.confirm( wp_stream.i18n.confirm_uninstall ) ) { |
| 145 |
e.preventDefault(); |
| 146 |
} |
| 147 |
}); |
| 148 |
|
| 149 |
// Admin page tabs |
| 150 |
var $tabs = $( '.wp_stream_screen .nav-tab-wrapper' ), |
| 151 |
$panels = $( '.wp_stream_screen .nav-tab-content table.form-table' ), |
| 152 |
$activeTab = $tabs.find( '.nav-tab-active' ), |
| 153 |
defaultIndex = $activeTab.length > 0 ? $tabs.find( 'a' ).index( $activeTab ) : 0, |
| 154 |
hashIndex = window.location.hash.match( /^#(\d+)$/ ), |
| 155 |
currentHash = ( null !== hashIndex ? hashIndex[ 1 ] : defaultIndex ), |
| 156 |
syncFormAction = function( index ) { |
| 157 |
var $optionsForm = $( 'input[name="option_page"][value^="wp_stream"]' ).closest( 'form' ); |
| 158 |
if ( $optionsForm.length === 0 ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
var currentAction = $optionsForm.attr( 'action' ); |
| 162 |
|
| 163 |
$optionsForm.prop( 'action', currentAction.replace( /(^[^#]*).*$/, '$1#' + index ) ); |
| 164 |
}; |
| 165 |
|
| 166 |
$tabs.on( 'click', 'a', function() { |
| 167 |
var index = $tabs.find( 'a' ).index( $( this ) ), |
| 168 |
hashIndex = window.location.hash.match( /^#(\d+)$/ ); |
| 169 |
|
| 170 |
$panels.hide().eq( index ).show(); |
| 171 |
$tabs |
| 172 |
.find( 'a' ) |
| 173 |
.removeClass( 'nav-tab-active' ) |
| 174 |
.filter( $( this ) ) |
| 175 |
.addClass( 'nav-tab-active' ); |
| 176 |
|
| 177 |
if ( '' === window.location.hash || null !== hashIndex ) { |
| 178 |
window.location.hash = index; |
| 179 |
} |
| 180 |
|
| 181 |
syncFormAction( index ); |
| 182 |
|
| 183 |
return false; |
| 184 |
}); |
| 185 |
|
| 186 |
$tabs.children().eq( currentHash ).trigger( 'click' ); |
| 187 |
|
| 188 |
// Live Updates screen option |
| 189 |
$( document ).ready( function() { |
| 190 |
|
| 191 |
// Enable Live Updates checkbox ajax |
| 192 |
$( '#enable_live_update' ).click( function() { |
| 193 |
var nonce = $( '#stream_live_update_nonce' ).val(), |
| 194 |
user = $( '#enable_live_update_user' ).val(), |
| 195 |
checked = 'unchecked', |
| 196 |
heartbeat = 'true'; |
| 197 |
|
| 198 |
if ( $( '#enable_live_update' ).is( ':checked' ) ) { |
| 199 |
checked = 'checked'; |
| 200 |
} |
| 201 |
|
| 202 |
heartbeat = $( '#enable_live_update' ).data( 'heartbeat' ); |
| 203 |
|
| 204 |
$.ajax({ |
| 205 |
type: 'POST', |
| 206 |
url: ajaxurl, |
| 207 |
data: { |
| 208 |
action: 'stream_enable_live_update', |
| 209 |
nonce: nonce, |
| 210 |
user: user, |
| 211 |
checked: checked, |
| 212 |
heartbeat: heartbeat |
| 213 |
}, |
| 214 |
dataType: 'json', |
| 215 |
beforeSend: function() { |
| 216 |
$( '.stream-live-update-checkbox .spinner' ).show().css( { 'display': 'inline-block' } ); |
| 217 |
}, |
| 218 |
success: function( response ) { |
| 219 |
$( '.stream-live-update-checkbox .spinner' ).hide(); |
| 220 |
|
| 221 |
if ( false === response.success ) { |
| 222 |
$( '#enable_live_update' ).prop( 'checked', false ); |
| 223 |
|
| 224 |
if ( response.data ) { |
| 225 |
window.alert( response.data ); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
}); |
| 230 |
}); |
| 231 |
|
| 232 |
function toggle_filter_submit() { |
| 233 |
var all_hidden = true; |
| 234 |
|
| 235 |
// If all filters are hidden, hide the button |
| 236 |
if ( $( 'div.metabox-prefs [id="date-hide"]' ).is( ':checked' ) ) { |
| 237 |
all_hidden = false; |
| 238 |
} |
| 239 |
|
| 240 |
var divs = $( 'div.alignleft.actions div.select2-container' ); |
| 241 |
|
| 242 |
divs.each( function() { |
| 243 |
if ( ! $( this ).is( ':hidden' ) ) { |
| 244 |
all_hidden = false; |
| 245 |
return false; |
| 246 |
} |
| 247 |
}); |
| 248 |
|
| 249 |
if ( all_hidden ) { |
| 250 |
$( 'input#record-query-submit' ).hide(); |
| 251 |
$( 'span.filter_info' ).show(); |
| 252 |
} else { |
| 253 |
$( 'input#record-query-submit' ).show(); |
| 254 |
$( 'span.filter_info' ).hide(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
if ( $( 'div.metabox-prefs [id="date-hide"]' ).is( ':checked' ) ) { |
| 259 |
$( 'div.date-interval' ).show(); |
| 260 |
} else { |
| 261 |
$( 'div.date-interval' ).hide(); |
| 262 |
} |
| 263 |
|
| 264 |
$( 'div.actions select.chosen-select' ).each( function() { |
| 265 |
var name = $( this ).prop( 'name' ); |
| 266 |
|
| 267 |
if ( $( 'div.metabox-prefs [id="' + name + '-hide"]' ).is( ':checked' ) ) { |
| 268 |
$( this ).prev( '.select2-container' ).show(); |
| 269 |
} else { |
| 270 |
$( this ).prev( '.select2-container' ).hide(); |
| 271 |
} |
| 272 |
}); |
| 273 |
|
| 274 |
toggle_filter_submit(); |
| 275 |
|
| 276 |
$( 'div.metabox-prefs [type="checkbox"]' ).click( function() { |
| 277 |
var id = $( this ).prop( 'id' ); |
| 278 |
|
| 279 |
if ( 'date-hide' === id ) { |
| 280 |
if ( $( this ).is( ':checked' ) ) { |
| 281 |
$( 'div.date-interval' ).show(); |
| 282 |
} else { |
| 283 |
$( 'div.date-interval' ).hide(); |
| 284 |
} |
| 285 |
} else { |
| 286 |
id = id.replace( '-hide', '' ); |
| 287 |
|
| 288 |
if ( $( this ).is( ':checked' ) ) { |
| 289 |
$( '[name="' + id + '"]' ).prev( '.select2-container' ).show(); |
| 290 |
} else { |
| 291 |
$( '[name="' + id + '"]' ).prev( '.select2-container' ).hide(); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
toggle_filter_submit(); |
| 296 |
}); |
| 297 |
|
| 298 |
$( '#ui-datepicker-div' ).addClass( 'stream-datepicker' ); |
| 299 |
}); |
| 300 |
|
| 301 |
// Relative time |
| 302 |
$( 'table.wp-list-table' ).on( 'updated', function() { |
| 303 |
var timeObjects = $( this ).find( 'time.relative-time' ); |
| 304 |
timeObjects.each( function( i, el ) { |
| 305 |
var timeEl = $( el ); |
| 306 |
timeEl.removeClass( 'relative-time' ); |
| 307 |
$( '<strong><time datetime="' + timeEl.attr( 'datetime' ) + '" class="timeago"/></time></strong><br/>' ) |
| 308 |
.prependTo( timeEl.parent().parent() ) |
| 309 |
.find( 'time.timeago' ) |
| 310 |
.timeago(); |
| 311 |
}); |
| 312 |
}).trigger( 'updated' ); |
| 313 |
|
| 314 |
var intervals = { |
| 315 |
init: function( $wrapper ) { |
| 316 |
this.wrapper = $wrapper; |
| 317 |
this.save_interval( this.wrapper.find( '.button-primary' ), this.wrapper ); |
| 318 |
|
| 319 |
this.$ = this.wrapper.each( function( i, val ) { |
| 320 |
var container = $( val ), |
| 321 |
dateinputs = container.find( '.date-inputs' ), |
| 322 |
from = container.find( '.field-from' ), |
| 323 |
to = container.find( '.field-to' ), |
| 324 |
to_remove = to.prev( '.date-remove' ), |
| 325 |
from_remove = from.prev( '.date-remove' ), |
| 326 |
predefined = container.children( '.field-predefined' ), |
| 327 |
datepickers = $( '' ).add( to ).add( from ); |
| 328 |
|
| 329 |
if ( jQuery.datepicker ) { |
| 330 |
|
| 331 |
// Apply a GMT offset due to Date() using the visitor's local time |
| 332 |
var siteGMTOffsetHours = parseFloat( wp_stream.gmt_offset ), |
| 333 |
localGMTOffsetHours = new Date().getTimezoneOffset() / 60 * -1, |
| 334 |
totalGMTOffsetHours = siteGMTOffsetHours - localGMTOffsetHours, |
| 335 |
localTime = new Date(), |
| 336 |
siteTime = new Date( localTime.getTime() + ( totalGMTOffsetHours * 60 * 60 * 1000 ) ), |
| 337 |
maxOffset = 0, |
| 338 |
minOffset = null; |
| 339 |
|
| 340 |
// Check if the site date is different from the local date, and set a day offset |
| 341 |
if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) { |
| 342 |
if ( localTime.getTime() < siteTime.getTime() ) { |
| 343 |
maxOffset = '+1d'; |
| 344 |
} else { |
| 345 |
maxOffset = '-1d'; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
datepickers.datepicker({ |
| 350 |
dateFormat: 'yy/mm/dd', |
| 351 |
minDate: minOffset, |
| 352 |
maxDate: maxOffset, |
| 353 |
defaultDate: siteTime, |
| 354 |
beforeShow: function() { |
| 355 |
$( this ).prop( 'disabled', true ); |
| 356 |
}, |
| 357 |
onClose: function() { |
| 358 |
$( this ).prop( 'disabled', false ); |
| 359 |
} |
| 360 |
}); |
| 361 |
|
| 362 |
datepickers.datepicker( 'widget' ).addClass( 'stream-datepicker' ); |
| 363 |
} |
| 364 |
|
| 365 |
predefined.select2({ |
| 366 |
'allowClear': true |
| 367 |
}); |
| 368 |
|
| 369 |
if ( '' !== from.val() ) { |
| 370 |
from_remove.show(); |
| 371 |
} |
| 372 |
|
| 373 |
if ( '' !== to.val() ) { |
| 374 |
to_remove.show(); |
| 375 |
} |
| 376 |
|
| 377 |
predefined.on({ |
| 378 |
'change': function () { |
| 379 |
var value = $( this ).val(), |
| 380 |
option = predefined.find( '[value="' + value + '"]' ), |
| 381 |
to_val = option.data( 'to' ), |
| 382 |
from_val = option.data( 'from' ); |
| 383 |
|
| 384 |
if ( 'custom' === value ) { |
| 385 |
dateinputs.show(); |
| 386 |
return false; |
| 387 |
} else { |
| 388 |
dateinputs.hide(); |
| 389 |
datepickers.datepicker( 'hide' ); |
| 390 |
} |
| 391 |
|
| 392 |
from.val( from_val ).trigger( 'change', [ true ] ); |
| 393 |
to.val( to_val ).trigger( 'change', [ true ] ); |
| 394 |
|
| 395 |
if ( jQuery.datepicker && datepickers.datepicker( 'widget' ).is( ':visible' ) ) { |
| 396 |
datepickers.datepicker( 'refresh' ).datepicker( 'hide' ); |
| 397 |
} |
| 398 |
}, |
| 399 |
'select2-removed': function() { |
| 400 |
predefined.val( '' ).trigger( 'change' ); |
| 401 |
}, |
| 402 |
'check_options': function () { |
| 403 |
if ( '' !== to.val() && '' !== from.val() ) { |
| 404 |
var option = predefined |
| 405 |
.find( 'option' ) |
| 406 |
.filter( '[data-to="' + to.val() + '"]' ) |
| 407 |
.filter( '[data-from="' + from.val() + '"]' ); |
| 408 |
if ( 0 !== option.length ) { |
| 409 |
predefined.val( option.attr( 'value' ) ).trigger( 'change', [ true ] ); |
| 410 |
} else { |
| 411 |
predefined.val( 'custom' ).trigger( 'change', [ true ] ); |
| 412 |
} |
| 413 |
} else if ( '' === to.val() && '' === from.val() ) { |
| 414 |
predefined.val( '' ).trigger( 'change', [ true ] ); |
| 415 |
} else { |
| 416 |
predefined.val( 'custom' ).trigger( 'change', [ true ] ); |
| 417 |
} |
| 418 |
} |
| 419 |
}); |
| 420 |
|
| 421 |
from.on( 'change', function() { |
| 422 |
if ( '' !== from.val() ) { |
| 423 |
from_remove.show(); |
| 424 |
to.datepicker( 'option', 'minDate', from.val() ); |
| 425 |
} else { |
| 426 |
from_remove.hide(); |
| 427 |
} |
| 428 |
|
| 429 |
if ( true === arguments[ arguments.length - 1 ] ) { |
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
predefined.trigger( 'check_options' ); |
| 434 |
}); |
| 435 |
|
| 436 |
to.on( 'change', function() { |
| 437 |
if ( '' !== to.val() ) { |
| 438 |
to_remove.show(); |
| 439 |
from.datepicker( 'option', 'maxDate', to.val() ); |
| 440 |
} else { |
| 441 |
to_remove.hide(); |
| 442 |
} |
| 443 |
|
| 444 |
if ( true === arguments[ arguments.length - 1 ] ) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
predefined.trigger( 'check_options' ); |
| 449 |
}); |
| 450 |
|
| 451 |
// Trigger change on load |
| 452 |
predefined.trigger( 'change' ); |
| 453 |
|
| 454 |
$( '' ).add( from_remove ).add( to_remove ).on( 'click', function() { |
| 455 |
$( this ).next( 'input' ).val( '' ).trigger( 'change' ); |
| 456 |
}); |
| 457 |
}); |
| 458 |
}, |
| 459 |
|
| 460 |
save_interval: function( $btn ) { |
| 461 |
var $wrapper = this.wrapper; |
| 462 |
$btn.click( function() { |
| 463 |
var data = { |
| 464 |
key: $wrapper.find( 'select.field-predefined' ).find( ':selected' ).val(), |
| 465 |
start: $wrapper.find( '.date-inputs .field-from' ).val(), |
| 466 |
end: $wrapper.find( '.date-inputs .field-to' ).val() |
| 467 |
}; |
| 468 |
|
| 469 |
// Add params to URL |
| 470 |
$( this ).attr( 'href', $( this ).attr( 'href' ) + '&' + $.param( data ) ); |
| 471 |
}); |
| 472 |
} |
| 473 |
}; |
| 474 |
|
| 475 |
$( document ).ready( function() { |
| 476 |
intervals.init( $( '.date-interval' ) ); |
| 477 |
|
| 478 |
// Disable option groups whose children are all disabled |
| 479 |
$( 'select[name="context"] .level-1' ).each( function() { |
| 480 |
var all_disabled = true; |
| 481 |
|
| 482 |
$( this ).nextUntil( '.level-1' ).each( function() { |
| 483 |
if ( $( this ).is( ':not(:disabled)' ) ) { |
| 484 |
all_disabled = false; |
| 485 |
return false; |
| 486 |
} |
| 487 |
}); |
| 488 |
|
| 489 |
if ( true === all_disabled ) { |
| 490 |
$( this ).prop( 'disabled', true ); |
| 491 |
} |
| 492 |
}); |
| 493 |
}); |
| 494 |
|
| 495 |
}); |
| 496 |
|
| 497 |
jQuery.extend({ |
| 498 |
streamGetQueryVars: function( str ) { |
| 499 |
return ( str || document.location.search ).replace( /(^\?)/, '' ).split( '&' ).map( function( n ) { return n = n.split( '=' ), this[n[0]] = n[1], this; }.bind( {} ) )[0]; |
| 500 |
} |
| 501 |
}); |
| 502 |
|