| 1 |
|
| 2 |
/* |
| 3 |
* Provide a parent container_id value to initialize a single metabox (when loading a single metabox via ajax, for example). |
| 4 |
*/ |
| 5 |
function sucomInitMetabox( container_id, doing_ajax ) { |
| 6 |
|
| 7 |
var table_id = 'table.sucom-settings'; |
| 8 |
|
| 9 |
if ( 'string' === typeof container_id && container_id ) { |
| 10 |
|
| 11 |
table_id = container_id + ' ' + table_id; |
| 12 |
} |
| 13 |
|
| 14 |
/* |
| 15 |
* Style the datepicker. |
| 16 |
*/ |
| 17 |
jQuery( table_id + ' input.datepicker' ).datepicker( { |
| 18 |
beforeShow:function( input, inst ) { |
| 19 |
jQuery( '#ui-datepicker-div' ).addClass( 'sucom-settings' ); |
| 20 |
}, |
| 21 |
changeMonth:true, |
| 22 |
changeYear:true, |
| 23 |
showButtonPanel:false, |
| 24 |
dateFormat:'yy-mm-dd' |
| 25 |
} ); |
| 26 |
|
| 27 |
/* |
| 28 |
* Softly disable input fields for the 'disabled' CSS class (instead of using the standard 'disabled' HTML tag attribute |
| 29 |
* which prevents values from being submitted). |
| 30 |
*/ |
| 31 |
jQuery( table_id + ' input' ).on( 'click', sucomBlurDisabled ); |
| 32 |
jQuery( table_id + ' input' ).on( 'focus', sucomBlurDisabled ); |
| 33 |
jQuery( table_id + ' textarea' ).on( 'focus', sucomBlurDisabled ); |
| 34 |
jQuery( table_id + ' select' ).on( 'focus', sucomBlurDisabled ); |
| 35 |
jQuery( table_id + ' select' ).on( 'mousedown', sucomBlurDisabled ); // Prevents dropdown from appearing. |
| 36 |
|
| 37 |
/* |
| 38 |
* Add a "changed" class when the value might have changed. |
| 39 |
* |
| 40 |
* Note that the focusin and focusout events bubble, and the focus and blur events don't. |
| 41 |
*/ |
| 42 |
jQuery( table_id + ' input.colorpicker' ).wpColorPicker( { change:sucomColorChanged } ); |
| 43 |
jQuery( table_id + ' input' ).on( 'blur change', sucomMarkChanged ); |
| 44 |
jQuery( table_id + ' textarea' ).on( 'blur change', sucomMarkChanged ); |
| 45 |
jQuery( table_id + ' select' ).on( 'blur change', sucomMarkChanged ); |
| 46 |
|
| 47 |
jQuery( document ).on( 'click', table_id + ' input[type="checkbox"][data-group]', function() { |
| 48 |
|
| 49 |
var actor = jQuery( this ); |
| 50 |
var checked = actor.prop( 'checked' ); |
| 51 |
var group = actor.data( 'group' ); |
| 52 |
var grouped = jQuery( 'input[type="checkbox"][data-group="' + group + '"]' ); |
| 53 |
|
| 54 |
grouped.prop( 'checked', checked ); |
| 55 |
|
| 56 |
grouped.addClass( 'changed' ); |
| 57 |
} ); |
| 58 |
|
| 59 |
/* |
| 60 |
* Add a "default" class when the value has changed and is the default. |
| 61 |
*/ |
| 62 |
jQuery( table_id + ' input' ).on( 'change', sucomMarkDefault ); |
| 63 |
jQuery( table_id + ' select' ).on( 'change', sucomMarkDefault ); |
| 64 |
|
| 65 |
/* |
| 66 |
* The 'sucom_init_metabox' event is hooked by sucomInitAdminMedia(), sucomInitToolTips(). |
| 67 |
*/ |
| 68 |
jQuery( document ).trigger( 'sucom_init_metabox', [ container_id, doing_ajax ] ); |
| 69 |
|
| 70 |
/* |
| 71 |
* If we're refreshing a metabox via ajax, trigger a 'show' event for each table row displayed. |
| 72 |
*/ |
| 73 |
if ( doing_ajax ) { |
| 74 |
|
| 75 |
jQuery( table_id + ' tr' ).each( function() { |
| 76 |
|
| 77 |
if ( 'none' !== jQuery( this ).css( 'display' ) ) { |
| 78 |
|
| 79 |
jQuery( this ).show(); |
| 80 |
} |
| 81 |
} ); |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* When an editing page is submitted, disable unchanged fields in 'table.sucom-settings'. |
| 86 |
*/ |
| 87 |
jQuery( 'form:not(#adv-settings)' ).each( function() { // Exclude the Screen Options form. |
| 88 |
|
| 89 |
if ( jQuery( this ).has( table_id ).length != 0 ) { |
| 90 |
|
| 91 |
jQuery( this ).submit( function ( event ) { |
| 92 |
|
| 93 |
sucomDisableUnchanged( container_id ); |
| 94 |
} ); |
| 95 |
} |
| 96 |
|
| 97 |
} ); |
| 98 |
} |
| 99 |
|
| 100 |
function sucomSelectLoadJson( select_id, json_name ) { |
| 101 |
|
| 102 |
/* |
| 103 |
* A select ID must be provided. |
| 104 |
* |
| 105 |
* Example: "select_schema_type_for_home_posts" |
| 106 |
*/ |
| 107 |
if ( ! select_id ) { |
| 108 |
|
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
/* |
| 113 |
* The variable name of the JSON array. |
| 114 |
* |
| 115 |
* Example: "wpsso_select_person_names_4f65aec5b650c0f4acc0f033dc81d39f" |
| 116 |
*/ |
| 117 |
if ( ! window[ json_name + '_keys' ] || ! window[ json_name + '_vals' ] ) { |
| 118 |
|
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
var container = jQuery( select_id + ':not( .json_loaded )' ); |
| 123 |
|
| 124 |
if ( ! container.length ) { |
| 125 |
|
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Avoid contention by signaling the json load early. |
| 131 |
*/ |
| 132 |
container.addClass( 'json_loaded' ); |
| 133 |
|
| 134 |
var default_value = container.data( 'default-value' ); |
| 135 |
var default_text = container.data( 'default-text' ); |
| 136 |
var selected_val = container.val(); |
| 137 |
var select_opt_html = '' |
| 138 |
|
| 139 |
/* |
| 140 |
* json_encode() cannot encode an associative array - only an object or a standard numerically indexed array - and the |
| 141 |
* object element order, when read by the browser, cannot be controlled. Firefox, for example, will sort an object |
| 142 |
* numerically instead of maintaining the original object element order. For this reason, we must use different arrays for |
| 143 |
* the array keys and their values. |
| 144 |
*/ |
| 145 |
jQuery.each( window[ json_name + '_keys' ], function ( index, option_value ) { |
| 146 |
|
| 147 |
label_transl = window[ json_name + '_vals' ][ index ]; |
| 148 |
|
| 149 |
if ( 'string' === typeof option_value && option_value.indexOf( ':optgroup-' ) > 0 ) { |
| 150 |
|
| 151 |
if ( option_value.indexOf( ':optgroup-begin' ) > 0 ) { |
| 152 |
|
| 153 |
select_opt_html += '<optgroup label="' + label_transl + '">'; |
| 154 |
|
| 155 |
} else select_opt_html += '</optgroup>'; /* ':optgroup-end' */ |
| 156 |
|
| 157 |
} else { |
| 158 |
|
| 159 |
select_opt_html += '<option value="' + option_value + '"'; |
| 160 |
|
| 161 |
if ( option_value == selected_val ) { /* Allow numeric string/integer comparison. */ |
| 162 |
|
| 163 |
select_opt_html += ' selected="selected"'; |
| 164 |
} |
| 165 |
|
| 166 |
if ( default_value == option_value ) { /* Allow numeric string/integer comparison. */ |
| 167 |
|
| 168 |
label_transl += ' ' + default_text; |
| 169 |
} |
| 170 |
|
| 171 |
select_opt_html += '>' + label_transl + '</option>'; |
| 172 |
} |
| 173 |
} ); |
| 174 |
|
| 175 |
/* |
| 176 |
* Update the select option list. |
| 177 |
* |
| 178 |
* Do not trigger a change event as the selected option has not changed. ;-) |
| 179 |
*/ |
| 180 |
container.empty(); |
| 181 |
|
| 182 |
container.append( select_opt_html ); |
| 183 |
} |
| 184 |
|
| 185 |
function sucomSelectUniquePair( main_id, other_id ) { |
| 186 |
|
| 187 |
var main = jQuery( main_id ); |
| 188 |
var other = jQuery( other_id ); |
| 189 |
var main_val = main.val(); |
| 190 |
var other_val = other.val(); |
| 191 |
|
| 192 |
if ( 'none' !== main_val ) { // If the main select has a value. |
| 193 |
|
| 194 |
if ( 'none' !== other_val ) { // Maybe set the other select value to 'none'. |
| 195 |
|
| 196 |
other.trigger( 'sucom_load_json' ).val( 'none' ).trigger( 'change' ); |
| 197 |
} |
| 198 |
|
| 199 |
other.addClass( 'disabled' ); // Disable the other select. |
| 200 |
|
| 201 |
} else { |
| 202 |
|
| 203 |
other.removeClass( 'disabled' ); // Re-enable the other select. |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
function sucomTabs( metabox_name, tab_name ) { |
| 208 |
|
| 209 |
metabox_name = metabox_name ? metabox_name : '_default'; |
| 210 |
tab_name = tab_name ? tab_name : '_default'; |
| 211 |
|
| 212 |
var scroll_to_tab_id = ''; |
| 213 |
var location_hash = window.location.hash; |
| 214 |
var active_tab_class = '.sucom-tabset' + metabox_name + '-tab' + tab_name; |
| 215 |
var tabs_vert_height = jQuery( 'ul.sucom-metabox-tabs' + metabox_name + '.vertical' ).height(); |
| 216 |
|
| 217 |
/* |
| 218 |
* Set the minimum height of settings containers to the height of the vertical tabs. |
| 219 |
*/ |
| 220 |
if ( tabs_vert_height ) { |
| 221 |
|
| 222 |
jQuery( 'div.sucom-tabset' + metabox_name ).css( { 'min-height': tabs_vert_height + 'px' } ); |
| 223 |
} |
| 224 |
|
| 225 |
if ( location_hash !== '' && location_hash.search( 'sucom-tabset' + metabox_name + '-tab_' ) !== -1 ) { |
| 226 |
|
| 227 |
active_tab_class = location_hash.replace( '#', '.' ); |
| 228 |
scroll_to_tab_id = 'div#sucom-metabox-tabs' + metabox_name; |
| 229 |
} |
| 230 |
|
| 231 |
jQuery( active_tab_class ).addClass( 'active' ); |
| 232 |
jQuery( active_tab_class + '-msg' ).addClass( 'active' ); |
| 233 |
jQuery( '.sucom-metabox-tabs' ).show(); |
| 234 |
|
| 235 |
if ( scroll_to_tab_id ) { |
| 236 |
|
| 237 |
/* |
| 238 |
* Prevent scrolling the metabox into view if this is a block editor page or scrolling is disabled. |
| 239 |
*/ |
| 240 |
var block_editor_content = jQuery( 'div.interface-interface-skeleton__content' ); |
| 241 |
var is_block_editor = block_editor_content.length ? true : false; |
| 242 |
var allow_scroll_to_hash = 'undefined' === typeof window.allowScrollToHash ? true : window.allowScrollToHash; |
| 243 |
|
| 244 |
if ( ! is_block_editor && allow_scroll_to_hash ) { |
| 245 |
|
| 246 |
sucomScrollIntoView( scroll_to_tab_id ); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
jQuery( 'a.sucom-tablink' + metabox_name ).on( 'click', function(){ |
| 251 |
|
| 252 |
jQuery( 'ul.sucom-metabox-tabs' + metabox_name + ' li' ).removeClass( 'active' ); |
| 253 |
jQuery( '.sucom-tabset' + metabox_name ).removeClass( 'active' ); |
| 254 |
jQuery( '.sucom-tabset' + metabox_name + '-msg' ).removeClass( 'active' ); |
| 255 |
|
| 256 |
/* |
| 257 |
* Example tablink: |
| 258 |
* |
| 259 |
* <a class="sucom-tablink sucom-tablink_sso sucom-tablink-icon sucom-tablink-href_edit_general" href="#sucom-tabset_sso-tab_edit_general"></a> |
| 260 |
* <a class="sucom-tablink sucom-tablink_sso sucom-tablink-text sucom-tablink-href_edit_general" href="#sucom-tabset_sso-tab_edit_general">Edit General</a> |
| 261 |
*/ |
| 262 |
var href = jQuery( this ).attr( 'href' ).replace( '#', '' ); |
| 263 |
|
| 264 |
jQuery( '.' + href ).addClass( 'active' ); |
| 265 |
jQuery( '.' + href + '-msg' ).addClass( 'active' ); |
| 266 |
jQuery( this ).parent().addClass( 'active' ); |
| 267 |
|
| 268 |
scroll_to_tab_id = 'div#sucom-metabox-tabs' + metabox_name; |
| 269 |
|
| 270 |
sucomScrollIntoView( scroll_to_tab_id ); |
| 271 |
} ); |
| 272 |
} |
| 273 |
|
| 274 |
function sucomScrollIntoView( container_id ) { |
| 275 |
|
| 276 |
if ( ! container_id ) return false; // A container id string is required. |
| 277 |
|
| 278 |
var wpbody_content = jQuery( 'div#wpbody-content' ); // Located below the admin toolbar. |
| 279 |
var container = jQuery( container_id ); |
| 280 |
|
| 281 |
if ( ! wpbody_content.length || ! container.length ) return false; |
| 282 |
|
| 283 |
var block_editor_content = jQuery( 'div.interface-interface-skeleton__content' ); // Page might be a block editor container. |
| 284 |
var toolbar_offset = wpbody_content.offset().top; // Just under the admin toolbar. |
| 285 |
var footer_offset = 0; |
| 286 |
var viewport = {}; |
| 287 |
var bounds = {}; |
| 288 |
var scroll_offset = 0; |
| 289 |
|
| 290 |
viewport.top = jQuery( window ).scrollTop(); |
| 291 |
viewport.bottom = viewport.top + jQuery( window ).height(); |
| 292 |
|
| 293 |
bounds.top = container.offset().top; |
| 294 |
bounds.bottom = bounds.top + container.outerHeight(); |
| 295 |
|
| 296 |
if ( ! bounds.top || ! bounds.bottom ) { // Just in case. |
| 297 |
|
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
if ( block_editor_content.length ) { // This is a block editor page. |
| 302 |
|
| 303 |
var editor_top = jQuery( 'div.edit-post-visual-editor' ).offset().top; // Block editor section. |
| 304 |
var footer_offset = jQuery( 'div.interface-interface-skeleton__footer' ).height(); |
| 305 |
|
| 306 |
toolbar_offset = block_editor_content.offset().top; // Just under the 'interface-interface-skeleton__header'. |
| 307 |
scroll_container = block_editor_content; |
| 308 |
scroll_offset = bounds.top - editor_top + 1; |
| 309 |
|
| 310 |
} else { |
| 311 |
|
| 312 |
scroll_container = jQuery( 'html, body' ); |
| 313 |
scroll_offset = bounds.top - toolbar_offset; |
| 314 |
} |
| 315 |
|
| 316 |
if ( bounds.top < viewport.top + toolbar_offset || bounds.bottom > viewport.bottom - footer_offset ) { |
| 317 |
|
| 318 |
scroll_container.stop().animate( { scrollTop:scroll_offset }, 'fast' ); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/* |
| 323 |
* Example: sucomViewUnhideRows( 'sucom-tabset_doc_types-tab_schema_types', 'basic' ) |
| 324 |
*/ |
| 325 |
function sucomViewUnhideRows( container_id, show_opts_key, hide_in_pre ) { |
| 326 |
|
| 327 |
hide_in_pre = hide_in_pre ? hide_in_pre : 'hide_in'; |
| 328 |
|
| 329 |
if ( ! container_id ) { // A container id string is required. |
| 330 |
|
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
var message = jQuery( 'div.' + container_id + '-msg' ); |
| 335 |
|
| 336 |
if ( ! message.length ) { // Just in case. |
| 337 |
|
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
message.hide(); |
| 342 |
|
| 343 |
jQuery( 'div.' + container_id ).find( '.' + hide_in_pre + '_' + show_opts_key ).show(); |
| 344 |
|
| 345 |
var parent_id = message.parent( 'div' ).attr( 'id' ); |
| 346 |
|
| 347 |
sucomScrollIntoView( 'div#' + parent_id ); |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
* Example: sucomChangeHideUnhideRows( "hide_schema_type", "hide_schema_type_article" ); |
| 352 |
*/ |
| 353 |
function sucomChangeHideUnhideRows( row_hide_class, row_show_class ) { |
| 354 |
|
| 355 |
if ( row_hide_class ) { |
| 356 |
|
| 357 |
row_hide_class = sucomSanitizeCssId( row_hide_class ); |
| 358 |
|
| 359 |
jQuery( 'tr.' + row_hide_class ).hide(); |
| 360 |
} |
| 361 |
|
| 362 |
if ( row_show_class ) { |
| 363 |
|
| 364 |
row_show_class = sucomSanitizeCssId( row_show_class ); |
| 365 |
|
| 366 |
jQuery( 'tr.' + row_show_class ).show(); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
function sucomSelectChangeRedirect( name, value, redirect_url ) { |
| 371 |
|
| 372 |
url = redirect_url + window.location.hash; |
| 373 |
|
| 374 |
window.location = url.replace( '%%' + name + '%%', value ); |
| 375 |
} |
| 376 |
|
| 377 |
/* |
| 378 |
* Softly disable input fields using the 'disabled' CSS class instead of using the standard 'disabled' HTML tag attribute (which |
| 379 |
* prevents values from being submitted). |
| 380 |
*/ |
| 381 |
function sucomBlurDisabled( e ) { |
| 382 |
|
| 383 |
var is_disabled = jQuery( this ).hasClass( 'disabled' ); |
| 384 |
|
| 385 |
if ( is_disabled ) { |
| 386 |
|
| 387 |
this.blur(); |
| 388 |
|
| 389 |
window.focus(); |
| 390 |
|
| 391 |
e.preventDefault(); |
| 392 |
|
| 393 |
e.stopPropagation(); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/* |
| 398 |
* Add a "changed" class when the value might have changed. |
| 399 |
*/ |
| 400 |
function sucomMarkChanged( e, el ) { |
| 401 |
|
| 402 |
if ( 'undefined' === typeof el ) { |
| 403 |
|
| 404 |
el = jQuery( this ); |
| 405 |
} |
| 406 |
|
| 407 |
el.addClass( 'changed' ); |
| 408 |
|
| 409 |
jQuery( el ).trigger( 'sucom_changed' ); |
| 410 |
} |
| 411 |
|
| 412 |
/* |
| 413 |
* Add a "default" class when the value has changed and is the default. |
| 414 |
*/ |
| 415 |
function sucomMarkDefault( e, el ) { |
| 416 |
|
| 417 |
if ( this.hasAttribute( 'data-default-value' ) ) { |
| 418 |
|
| 419 |
if ( 'undefined' === typeof el ) { |
| 420 |
|
| 421 |
el = jQuery( this ); |
| 422 |
} |
| 423 |
|
| 424 |
var value = this.value; |
| 425 |
var def_value = this.getAttribute( 'data-default-value' ); |
| 426 |
|
| 427 |
if ( 'checkbox' == this.type ) { |
| 428 |
|
| 429 |
value = el.is( ':checked' ) ? 1 : 0; |
| 430 |
} |
| 431 |
|
| 432 |
if ( value == def_value ) { |
| 433 |
|
| 434 |
el.addClass( 'default' ); |
| 435 |
|
| 436 |
} else { |
| 437 |
|
| 438 |
el.removeClass( 'default' ); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
function sucomPlaceholderDep( container_id, container_dep_id ) { |
| 444 |
|
| 445 |
var el = jQuery( container_id ); |
| 446 |
var dep_el = jQuery( container_dep_id ); |
| 447 |
var dep_val = dep_el.val(); |
| 448 |
|
| 449 |
if ( '' === dep_val ) dep_val = dep_el.attr( 'placeholder' ); |
| 450 |
|
| 451 |
el.attr( 'placeholder', dep_val ).change(); |
| 452 |
} |
| 453 |
|
| 454 |
/* |
| 455 |
* A callback function for the wpColorPicker, which does not automatically update the input field value on changes. |
| 456 |
*/ |
| 457 |
function sucomColorChanged( e, ui ) { |
| 458 |
|
| 459 |
var el = jQuery( this ); |
| 460 |
var name = el.attr( 'name' ); |
| 461 |
var value = ui.color.toString(); |
| 462 |
|
| 463 |
sucomMarkChanged( e, el ) |
| 464 |
} |
| 465 |
|
| 466 |
/* |
| 467 |
* sucomDisableUnchanged() should be called from a form submit event. |
| 468 |
* |
| 469 |
* Example: container_id = '#sucom_settings_form_general' |
| 470 |
*/ |
| 471 |
function sucomDisableUnchanged( container_id ) { |
| 472 |
|
| 473 |
var table_id = 'table.sucom-settings'; |
| 474 |
|
| 475 |
if ( 'string' === typeof container_id && container_id ) { |
| 476 |
|
| 477 |
table_id = container_id + ' ' + table_id; |
| 478 |
} |
| 479 |
|
| 480 |
jQuery( table_id + ' input[type="checkbox"]:not( .changed )' ).each( function() { |
| 481 |
|
| 482 |
var checkbox_name = jQuery( this ).attr( 'name' ); |
| 483 |
|
| 484 |
if ( 'undefined' !== typeof checkbox_name && checkbox_name.length ) { |
| 485 |
|
| 486 |
is_checkbox_name = checkbox_name.replace( /^(.*)\[(.*)\]$/, '$1\\[is_checkbox_$2\\]' ); |
| 487 |
|
| 488 |
jQuery( table_id + ' input[name="' + checkbox_name + '"]' ).prop( 'disabled', true ); |
| 489 |
|
| 490 |
jQuery( table_id + ' input[name="' + is_checkbox_name + '"]' ).remove(); |
| 491 |
} |
| 492 |
} ); |
| 493 |
|
| 494 |
jQuery( table_id + ' input[type!="hidden"]:not( .changed )' ).prop( 'disabled', true ); |
| 495 |
jQuery( table_id + ' textarea:not( .changed )' ).prop( 'disabled', true ); |
| 496 |
jQuery( table_id + ' select:not( .changed )' ).prop( 'disabled', true ); |
| 497 |
|
| 498 |
jQuery( table_id + ' .changed' ).prop( 'disabled', false ); |
| 499 |
} |
| 500 |
|
| 501 |
function sucomToggle( css_id ) { |
| 502 |
|
| 503 |
jQuery( '#' + css_id ).toggle(); |
| 504 |
|
| 505 |
return false; |
| 506 |
} |
| 507 |
|