api-keys.js
5 years ago
api-keys.min.js
2 years ago
backbone-modal.js
2 years ago
backbone-modal.min.js
2 years ago
marketplace-suggestions.js
10 months ago
marketplace-suggestions.min.js
10 months ago
meta-boxes-coupon.js
1 year ago
meta-boxes-coupon.min.js
1 year ago
meta-boxes-order.js
4 weeks ago
meta-boxes-order.min.js
4 weeks ago
meta-boxes-product-variation.js
1 year ago
meta-boxes-product-variation.min.js
1 year ago
meta-boxes-product.js
4 weeks ago
meta-boxes-product.min.js
4 weeks ago
meta-boxes.js
4 months ago
meta-boxes.min.js
4 months ago
network-orders.js
8 years ago
network-orders.min.js
2 years ago
order-attribution-admin.js
2 years ago
order-attribution-admin.min.js
2 years ago
product-editor.js
11 months ago
product-editor.min.js
11 months ago
product-ordering.js
3 months ago
product-ordering.min.js
3 months ago
quick-edit.js
1 year ago
quick-edit.min.js
1 year ago
reports.js
1 year ago
reports.min.js
1 year ago
settings-views-html-settings-tax.js
1 year ago
settings-views-html-settings-tax.min.js
1 year ago
settings.js
1 year ago
settings.min.js
1 year ago
system-status.js
3 months ago
system-status.min.js
3 months ago
term-ordering.js
3 months ago
term-ordering.min.js
3 months ago
users.js
5 years ago
users.min.js
2 years ago
variation-gallery.js
4 weeks ago
variation-gallery.min.js
4 weeks ago
wc-brands-enhanced-select.js
1 year ago
wc-brands-enhanced-select.min.js
1 year ago
wc-clipboard.js
5 years ago
wc-clipboard.min.js
5 years ago
wc-customer-stock-notifications.js
10 months ago
wc-customer-stock-notifications.min.js
10 months ago
wc-enhanced-select.js
2 years ago
wc-enhanced-select.min.js
2 years ago
wc-orders.js
3 years ago
wc-orders.min.js
2 years ago
wc-product-export.js
1 year ago
wc-product-export.min.js
1 year ago
wc-product-import.js
3 years ago
wc-product-import.min.js
2 years ago
wc-recent-reviews-widget-async.js
4 months ago
wc-recent-reviews-widget-async.min.js
4 months ago
wc-setup.js
5 years ago
wc-setup.min.js
2 years ago
wc-shipping-classes.js
1 year ago
wc-shipping-classes.min.js
1 year ago
wc-shipping-providers.js
3 months ago
wc-shipping-providers.min.js
3 months ago
wc-shipping-zone-methods.js
5 months ago
wc-shipping-zone-methods.min.js
5 months ago
wc-shipping-zones.js
1 year ago
wc-shipping-zones.min.js
1 year ago
wc-status-widget-async.js
4 months ago
wc-status-widget-async.min.js
4 months ago
wc-status-widget.js
1 year ago
wc-status-widget.min.js
1 year ago
woocommerce_admin.js
4 weeks ago
woocommerce_admin.min.js
4 weeks ago
settings.js
423 lines
| 1 | /* global woocommerce_settings_params, wp */ |
| 2 | ( function ( $, params, wp ) { |
| 3 | $( function () { |
| 4 | // Sell Countries |
| 5 | $( 'select#woocommerce_allowed_countries' ) |
| 6 | .on( 'change', function () { |
| 7 | if ( 'specific' === $( this ).val() ) { |
| 8 | $( this ).closest( 'tr' ).next( 'tr' ).hide(); |
| 9 | $( this ).closest( 'tr' ).next().next( 'tr' ).show(); |
| 10 | } else if ( 'all_except' === $( this ).val() ) { |
| 11 | $( this ).closest( 'tr' ).next( 'tr' ).show(); |
| 12 | $( this ).closest( 'tr' ).next().next( 'tr' ).hide(); |
| 13 | } else { |
| 14 | $( this ).closest( 'tr' ).next( 'tr' ).hide(); |
| 15 | $( this ).closest( 'tr' ).next().next( 'tr' ).hide(); |
| 16 | } |
| 17 | } ) |
| 18 | .trigger( 'change' ); |
| 19 | |
| 20 | // Ship Countries |
| 21 | $( 'select#woocommerce_ship_to_countries' ) |
| 22 | .on( 'change', function () { |
| 23 | if ( 'specific' === $( this ).val() ) { |
| 24 | $( this ).closest( 'tr' ).next( 'tr' ).show(); |
| 25 | } else { |
| 26 | $( this ).closest( 'tr' ).next( 'tr' ).hide(); |
| 27 | } |
| 28 | } ) |
| 29 | .trigger( 'change' ); |
| 30 | |
| 31 | // Stock management |
| 32 | $( 'input#woocommerce_manage_stock' ) |
| 33 | .on( 'change', function () { |
| 34 | if ( $( this ).is( ':checked' ) ) { |
| 35 | $( this ) |
| 36 | .closest( 'tbody' ) |
| 37 | .find( '.manage_stock_field' ) |
| 38 | .closest( 'tr' ) |
| 39 | .show(); |
| 40 | } else { |
| 41 | $( this ) |
| 42 | .closest( 'tbody' ) |
| 43 | .find( '.manage_stock_field' ) |
| 44 | .closest( 'tr' ) |
| 45 | .hide(); |
| 46 | } |
| 47 | } ) |
| 48 | .trigger( 'change' ); |
| 49 | |
| 50 | // Color picker |
| 51 | $( '.colorpick' ) |
| 52 | .iris( { |
| 53 | change: function ( event, ui ) { |
| 54 | const $this = $( this ); |
| 55 | $this |
| 56 | .parent() |
| 57 | .find( '.colorpickpreview' ) |
| 58 | .css( { backgroundColor: ui.color.toString() } ); |
| 59 | setTimeout( function () { |
| 60 | $this.trigger( 'change' ); |
| 61 | } ); |
| 62 | }, |
| 63 | hide: true, |
| 64 | border: true, |
| 65 | } ) |
| 66 | |
| 67 | .on( 'click focus', function ( event ) { |
| 68 | event.stopPropagation(); |
| 69 | $( '.iris-picker' ).hide(); |
| 70 | $( this ).closest( 'td' ).find( '.iris-picker' ).show(); |
| 71 | $( this ).data( 'originalValue', $( this ).val() ); |
| 72 | } ) |
| 73 | |
| 74 | .on( 'change', function () { |
| 75 | if ( $( this ).is( '.iris-error' ) ) { |
| 76 | var original_value = $( this ).data( 'originalValue' ); |
| 77 | |
| 78 | if ( |
| 79 | original_value.match( |
| 80 | /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ |
| 81 | ) |
| 82 | ) { |
| 83 | $( this ) |
| 84 | .val( $( this ).data( 'originalValue' ) ) |
| 85 | .trigger( 'change' ); |
| 86 | } else { |
| 87 | $( this ).val( '' ).trigger( 'change' ); |
| 88 | } |
| 89 | } |
| 90 | } ); |
| 91 | |
| 92 | $( '.iris-square-value' ).on( 'click', function ( event ) { |
| 93 | event.preventDefault(); |
| 94 | } ); |
| 95 | |
| 96 | $( '.colorpickpreview' ).on( 'click', function ( event ) { |
| 97 | event.stopPropagation(); |
| 98 | $( this ).next( '.colorpick' ).click(); |
| 99 | } ); |
| 100 | |
| 101 | $( 'body' ).on( 'click', function () { |
| 102 | $( '.iris-picker' ).hide(); |
| 103 | } ); |
| 104 | |
| 105 | // Edit prompt |
| 106 | function editPrompt () { |
| 107 | var changed = false; |
| 108 | let $prevent_change_elements = $( '.wp-list-table .check-column, .wc-settings-prevent-change-event' ); |
| 109 | |
| 110 | $( 'input, textarea, select, checkbox' ).on( 'change input', function ( |
| 111 | event |
| 112 | ) { |
| 113 | // Prevent change event on specific elements, that don't change the form. E.g.: |
| 114 | // - WP List Table checkboxes that only (un)select rows |
| 115 | // - Changing email type in email preview |
| 116 | if ( |
| 117 | $prevent_change_elements.length && |
| 118 | $prevent_change_elements.has( event.target ).length |
| 119 | ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( ! changed ) { |
| 124 | window.onbeforeunload = function () { |
| 125 | return params.i18n_nav_warning; |
| 126 | }; |
| 127 | changed = true; |
| 128 | $( '.woocommerce-save-button' ).removeAttr( 'disabled' ); |
| 129 | } |
| 130 | } ); |
| 131 | |
| 132 | $( '.iris-picker' ).on( 'click', function () { |
| 133 | if ( ! changed ) { |
| 134 | changed = true; |
| 135 | $( '.woocommerce-save-button' ).removeAttr( 'disabled' ); |
| 136 | } |
| 137 | } ); |
| 138 | |
| 139 | $( '.submit :input, input#search-submit' ).on( |
| 140 | 'click', |
| 141 | function () { |
| 142 | window.onbeforeunload = ''; |
| 143 | } |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | $( editPrompt ); |
| 148 | |
| 149 | const nodeListContainsFormElements = ( nodes ) => { |
| 150 | if ( ! nodes.length ) { |
| 151 | return false; |
| 152 | } |
| 153 | return Array.from( nodes ).some( ( element ) => { |
| 154 | return $( element ).find( 'input, textarea, select, checkbox' ).length; |
| 155 | } ); |
| 156 | } |
| 157 | |
| 158 | const form = document.querySelector( '#mainform' ); |
| 159 | const observer = new MutationObserver( ( mutationsList ) => { |
| 160 | for ( const mutation of mutationsList ) { |
| 161 | if ( mutation.type === 'childList' ) { |
| 162 | if ( nodeListContainsFormElements( mutation.addedNodes ) ) { |
| 163 | editPrompt(); |
| 164 | $( '.woocommerce-save-button' ).removeAttr( 'disabled' ); |
| 165 | } else if ( nodeListContainsFormElements( mutation.removedNodes ) ) { |
| 166 | $( '.woocommerce-save-button' ).removeAttr( 'disabled' ); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } ); |
| 171 | |
| 172 | observer.observe( form, { childList: true, subtree: true } ); |
| 173 | |
| 174 | // Sorting |
| 175 | $( 'table.wc_gateways tbody, table.wc_shipping tbody' ).sortable( { |
| 176 | items: 'tr', |
| 177 | cursor: 'move', |
| 178 | axis: 'y', |
| 179 | handle: 'td.sort', |
| 180 | scrollSensitivity: 40, |
| 181 | helper: function ( event, ui ) { |
| 182 | ui.children().each( function () { |
| 183 | $( this ).width( $( this ).width() ); |
| 184 | } ); |
| 185 | ui.css( 'left', '0' ); |
| 186 | return ui; |
| 187 | }, |
| 188 | start: function ( event, ui ) { |
| 189 | ui.item.css( 'background-color', '#f6f6f6' ); |
| 190 | }, |
| 191 | stop: function ( event, ui ) { |
| 192 | ui.item.removeAttr( 'style' ); |
| 193 | ui.item.trigger( 'updateMoveButtons', { isInitialLoad: false } ); |
| 194 | }, |
| 195 | } ); |
| 196 | |
| 197 | // Select all/none |
| 198 | $( '.woocommerce' ).on( 'click', '.select_all', function () { |
| 199 | $( this ) |
| 200 | .closest( 'td' ) |
| 201 | .find( 'select option' ) |
| 202 | .prop( 'selected', true ); |
| 203 | $( this ).closest( 'td' ).find( 'select' ).trigger( 'change' ); |
| 204 | return false; |
| 205 | } ); |
| 206 | |
| 207 | $( '.woocommerce' ).on( 'click', '.select_none', function () { |
| 208 | $( this ) |
| 209 | .closest( 'td' ) |
| 210 | .find( 'select option' ) |
| 211 | .prop( 'selected', false ); |
| 212 | $( this ).closest( 'td' ).find( 'select' ).trigger( 'change' ); |
| 213 | return false; |
| 214 | } ); |
| 215 | |
| 216 | // Re-order buttons. |
| 217 | $( '.wc-item-reorder-nav' ) |
| 218 | .find( '.wc-move-up, .wc-move-down' ) |
| 219 | .on( 'click', function () { |
| 220 | var moveBtn = $( this ), |
| 221 | $row = moveBtn.closest( 'tr' ); |
| 222 | |
| 223 | moveBtn.trigger( 'focus' ); |
| 224 | |
| 225 | var isMoveUp = moveBtn.is( '.wc-move-up' ), |
| 226 | isMoveDown = moveBtn.is( '.wc-move-down' ); |
| 227 | |
| 228 | if ( isMoveUp ) { |
| 229 | var $previewRow = $row.prev( 'tr' ); |
| 230 | |
| 231 | if ( $previewRow && $previewRow.length ) { |
| 232 | $previewRow.before( $row ); |
| 233 | wp.a11y.speak( params.i18n_moved_up ); |
| 234 | } |
| 235 | } else if ( isMoveDown ) { |
| 236 | var $nextRow = $row.next( 'tr' ); |
| 237 | |
| 238 | if ( $nextRow && $nextRow.length ) { |
| 239 | $nextRow.after( $row ); |
| 240 | wp.a11y.speak( params.i18n_moved_down ); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | moveBtn.trigger( 'focus' ); // Re-focus after the container was moved. |
| 245 | moveBtn.closest( 'table' ).trigger( 'updateMoveButtons', { isInitialLoad: false } ); |
| 246 | } ); |
| 247 | |
| 248 | $( '.wc-item-reorder-nav' ) |
| 249 | .closest( 'table' ) |
| 250 | .on( 'updateMoveButtons', function ( event, data ) { |
| 251 | var table = $( this ), |
| 252 | lastRow = $( this ).find( 'tbody tr:last' ), |
| 253 | firstRow = $( this ).find( 'tbody tr:first' ); |
| 254 | |
| 255 | table |
| 256 | .find( '.wc-item-reorder-nav .wc-move-disabled' ) |
| 257 | .removeClass( 'wc-move-disabled' ) |
| 258 | .attr( { tabindex: '0', 'aria-hidden': 'false' } ); |
| 259 | firstRow |
| 260 | .find( '.wc-item-reorder-nav .wc-move-up' ) |
| 261 | .addClass( 'wc-move-disabled' ) |
| 262 | .attr( { tabindex: '-1', 'aria-hidden': 'true' } ); |
| 263 | lastRow |
| 264 | .find( '.wc-item-reorder-nav .wc-move-down' ) |
| 265 | .addClass( 'wc-move-disabled' ) |
| 266 | .attr( { tabindex: '-1', 'aria-hidden': 'true' } ); |
| 267 | if ( ! data.isInitialLoad ) { |
| 268 | $( '.woocommerce-save-button' ).removeAttr( 'disabled' ); |
| 269 | } |
| 270 | } ); |
| 271 | |
| 272 | $( '.wc-item-reorder-nav' ) |
| 273 | .closest( 'table' ) |
| 274 | .trigger( 'updateMoveButtons', { isInitialLoad: true } ); |
| 275 | |
| 276 | $( '.submit button' ).on( 'click', function () { |
| 277 | if ( |
| 278 | $( 'select#woocommerce_allowed_countries' ).val() === |
| 279 | 'specific' && |
| 280 | ! $( '[name="woocommerce_specific_allowed_countries[]"]' ).val() |
| 281 | ) { |
| 282 | if ( |
| 283 | window.confirm( |
| 284 | woocommerce_settings_params.i18n_no_specific_countries_selected |
| 285 | ) |
| 286 | ) { |
| 287 | return true; |
| 288 | } |
| 289 | return false; |
| 290 | } |
| 291 | } ); |
| 292 | |
| 293 | $( '#settings-other-payment-methods' ).on( 'click', function ( e ) { |
| 294 | if ( |
| 295 | typeof window.wcTracks.recordEvent === 'undefined' && |
| 296 | typeof window.wc.tracks.recordEvent === 'undefined' |
| 297 | ) { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | var recordEvent = |
| 302 | window.wc.tracks.recordEvent || window.wcTracks.recordEvent; |
| 303 | |
| 304 | var payment_methods = $.map( |
| 305 | $( |
| 306 | 'td.wc_payment_gateways_wrapper tbody tr[data-gateway_id] ' |
| 307 | ), |
| 308 | function ( tr ) { |
| 309 | return $( tr ).attr( 'data-gateway_id' ); |
| 310 | } |
| 311 | ); |
| 312 | |
| 313 | recordEvent( 'settings_payments_recommendations_other_options', { |
| 314 | available_payment_methods: payment_methods, |
| 315 | } ); |
| 316 | } ); |
| 317 | |
| 318 | $( '.woocommerce-save-button.components-button' ).on( 'click', function ( e ) { |
| 319 | if ( ! $( this ).attr( 'disabled' ) ) { |
| 320 | $( this ).addClass( 'is-busy' ); |
| 321 | } |
| 322 | } ); |
| 323 | |
| 324 | /** |
| 325 | * Support conditionally displaying a settings field description when another element |
| 326 | * is set to a specific value. |
| 327 | * |
| 328 | * This logic is subject to change, and is not intended for use by other plugins. |
| 329 | * Note that we can't avoid jQuery here, because of our current dependence on Select2 |
| 330 | * for various controls. |
| 331 | */ |
| 332 | document.querySelectorAll( 'body.woocommerce_page_wc-settings #mainform .conditional.description' ).forEach( description => { |
| 333 | const $underObservation = $( description.dataset.dependsOn ); |
| 334 | const showIfEquals = description.dataset.showIfEquals; |
| 335 | |
| 336 | if ( undefined === showIfEquals || $underObservation.length === 0 ) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Set visibility of the description element according to whether its value |
| 342 | * matches that of showIfEquals. |
| 343 | */ |
| 344 | const changeAgent = () => { |
| 345 | description.style.visibility = $underObservation.val() === showIfEquals ? 'visible' : 'hidden'; |
| 346 | }; |
| 347 | |
| 348 | // Monitor future changes, and take action based on the current state. |
| 349 | $underObservation.on( 'change', changeAgent ); |
| 350 | changeAgent(); |
| 351 | } ); |
| 352 | |
| 353 | // Ensures the active tab is visible and centered on small screens if it's out of view in a scrollable tab list. |
| 354 | function settings_scroll_to_active_tab() { |
| 355 | const body = document.body; |
| 356 | if ( |
| 357 | ! body.classList.contains('mobile') || |
| 358 | ! body.classList.contains('woocommerce_page_wc-settings') |
| 359 | ) { |
| 360 | return; |
| 361 | } |
| 362 | // Select the currently active tab |
| 363 | const activeTab = document.querySelector( '.nav-tab-active' ); |
| 364 | |
| 365 | // Exit if there's no active tab or screen is wider than 500px (desktop) |
| 366 | if ( ! activeTab || window.innerWidth >= 500 ) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // Get the parent element, assumed to be the scrollable container |
| 371 | const parent = activeTab.parentElement; |
| 372 | |
| 373 | // Exit if no parent or if scrolling isn't needed (content fits) |
| 374 | if ( ! parent || parent.scrollWidth <= parent.clientWidth ) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // Get the position of the active tab relative to its parent |
| 379 | const tabLeft = activeTab.offsetLeft; |
| 380 | const tabRight = tabLeft + activeTab.offsetWidth; |
| 381 | const scrollLeft = parent.scrollLeft; |
| 382 | const visibleLeft = scrollLeft; |
| 383 | const visibleRight = scrollLeft + parent.clientWidth; |
| 384 | const isOutOfView = tabLeft < visibleLeft || tabRight > visibleRight; |
| 385 | |
| 386 | // If it’s out of view, scroll the parent so the tab is centered |
| 387 | if ( isOutOfView ) { |
| 388 | const offset = tabLeft - parent.clientWidth / 2 + activeTab.offsetWidth / 2; |
| 389 | parent.scrollTo( { |
| 390 | left: offset, |
| 391 | behavior: 'auto' // Instant scroll (no animation) |
| 392 | } ); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Some legacy setting pages have tables that span beyond the set width of its parents |
| 397 | // causing layout issues. |
| 398 | // Fixe the width of the nav tab wrapper to match the window width on mobile. |
| 399 | function settings_fix_nav_width() { |
| 400 | const body = document.body; |
| 401 | if ( |
| 402 | ! body.classList.contains('mobile') || |
| 403 | ! body.classList.contains('woocommerce_page_wc-settings') |
| 404 | ) { |
| 405 | return; |
| 406 | } |
| 407 | const navWrapper = document.getElementsByClassName('nav-tab-wrapper'); |
| 408 | if ( ! navWrapper.length ) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | const navWrapperWidth = navWrapper[0].offsetWidth; |
| 413 | if ( navWrapperWidth !== window.innerWidth) { |
| 414 | navWrapper[0].style.width = window.innerWidth + 'px'; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | settings_scroll_to_active_tab(); |
| 419 | settings_fix_nav_width(); |
| 420 | |
| 421 | } ); |
| 422 | } )( jQuery, woocommerce_settings_params, wp ); |
| 423 |