|
| 1 | +/** |
| 2 | + * Scripts for the options/settings administration screens. |
| 3 | + * |
| 4 | + * @output wp-admin/js/options.js |
| 5 | + */ |
| 6 | + |
| 7 | +/* global ajaxurl */ |
| 8 | + |
| 9 | +jQuery( function( $ ) { |
| 10 | + var $showAvatars = $( '#show_avatars' ), |
| 11 | + $avatarSettings = $( '.avatar-settings' ), |
| 12 | + $siteName = $( '#wp-admin-bar-site-name' ).children( 'a' ).first(), |
| 13 | + $siteIconPreview = $( '#site-icon-preview-site-title' ), |
| 14 | + $languageSelect = $( '#WPLANG' ), |
| 15 | + $frontStaticPages = $( '#front-static-pages' ), |
| 16 | + homeURL = ( ( window.optionsL10n && window.optionsL10n.homeURL ) || '' ).replace( /^(https?:\/\/)?(www\.)?/, '' ); |
| 17 | + |
| 18 | + // options-discussion.php: Toggle avatar settings when 'Show Avatars' is changed. |
| 19 | + $showAvatars.on( 'change', function() { |
| 20 | + $avatarSettings.toggleClass( 'hide-if-js', ! this.checked ); |
| 21 | + } ); |
| 22 | + |
| 23 | + // options-general.php: Update admin bar site name and site icon preview on title input. |
| 24 | + $( '#blogname' ).on( 'input', function() { |
| 25 | + var title = $.trim( $( this ).val() ) || homeURL; |
| 26 | + |
| 27 | + // Truncate to 40 characters. |
| 28 | + if ( 40 < title.length ) { |
| 29 | + title = title.substring( 0, 40 ) + '\u2026'; |
| 30 | + } |
| 31 | + |
| 32 | + $siteName.text( title ); |
| 33 | + $siteIconPreview.text( title ); |
| 34 | + } ); |
| 35 | + |
| 36 | + // options-general.php: Date and time format pickers. |
| 37 | + $( 'input[name="date_format"]' ).on( 'click', function() { |
| 38 | + if ( 'date_format_custom_radio' !== $( this ).attr( 'id' ) ) { |
| 39 | + $( 'input[name="date_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() ); |
| 40 | + } |
| 41 | + } ); |
| 42 | + |
| 43 | + $( 'input[name="date_format_custom"]' ).on( 'click input', function() { |
| 44 | + $( '#date_format_custom_radio' ).prop( 'checked', true ); |
| 45 | + } ); |
| 46 | + |
| 47 | + $( 'input[name="time_format"]' ).on( 'click', function() { |
| 48 | + if ( 'time_format_custom_radio' !== $( this ).attr( 'id' ) ) { |
| 49 | + $( 'input[name="time_format_custom"]' ).val( $( this ).val() ).closest( 'fieldset' ).find( '.example' ).text( $( this ).parent( 'label' ).children( '.format-i18n' ).text() ); |
| 50 | + } |
| 51 | + } ); |
| 52 | + |
| 53 | + $( 'input[name="time_format_custom"]' ).on( 'click input', function() { |
| 54 | + $( '#time_format_custom_radio' ).prop( 'checked', true ); |
| 55 | + } ); |
| 56 | + |
| 57 | + $( 'input[name="date_format_custom"], input[name="time_format_custom"]' ).on( 'input', function() { |
| 58 | + var format = $( this ), |
| 59 | + fieldset = format.closest( 'fieldset' ), |
| 60 | + example = fieldset.find( '.example' ), |
| 61 | + spinner = fieldset.find( '.spinner' ); |
| 62 | + |
| 63 | + // Debounce the event callback while users are typing. |
| 64 | + clearTimeout( $.data( this, 'timer' ) ); |
| 65 | + $( this ).data( 'timer', setTimeout( function() { |
| 66 | + // If custom date is not empty. |
| 67 | + if ( format.val() ) { |
| 68 | + spinner.addClass( 'is-active' ); |
| 69 | + |
| 70 | + $.post( ajaxurl, { |
| 71 | + action: 'date_format_custom' === format.attr( 'name' ) ? 'date_format' : 'time_format', |
| 72 | + date: format.val() |
| 73 | + }, function( d ) { spinner.removeClass( 'is-active' ); example.text( d ); } ); |
| 74 | + } |
| 75 | + }, 500 ) ); |
| 76 | + } ); |
| 77 | + |
| 78 | + // options-general.php: Language install spinner. |
| 79 | + $( 'form' ).on( 'submit', function() { |
| 80 | + /* |
| 81 | + * Don't show a spinner for English and installed languages, |
| 82 | + * as there is nothing to download. |
| 83 | + */ |
| 84 | + if ( $languageSelect.length && ! $languageSelect.find( 'option:selected' ).data( 'installed' ) ) { |
| 85 | + $( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' ); |
| 86 | + } |
| 87 | + } ); |
| 88 | + |
| 89 | + // options-reading.php: Enable/disable page selects based on 'Your homepage displays' radio. |
| 90 | + if ( $frontStaticPages.length ) { |
| 91 | + var $staticPage = $frontStaticPages.find( 'input:radio[value="page"]' ), |
| 92 | + $selects = $frontStaticPages.find( 'select' ), |
| 93 | + checkDisabled = function() { |
| 94 | + $selects.prop( 'disabled', ! $staticPage.prop( 'checked' ) ); |
| 95 | + }; |
| 96 | + checkDisabled(); |
| 97 | + $frontStaticPages.find( 'input:radio' ).on( 'change', checkDisabled ); |
| 98 | + } |
| 99 | +} ); |
0 commit comments