yith-wcwl.js
413 lines
| 1 | /** |
| 2 | * Admin YITH WooCommerce Wishlist JS |
| 3 | * |
| 4 | * @author YITH <plugins@yithemes.com> |
| 5 | * @package YITH WooCommerce Wishlist |
| 6 | * @version 3.0.0 |
| 7 | * |
| 8 | * global yith_wcwl_admin |
| 9 | */ |
| 10 | |
| 11 | jQuery( function ( $ ) { |
| 12 | |
| 13 | /* === CUSTOM DEPENDENCIES HANDLER === */ |
| 14 | |
| 15 | $.fn.dependency = function ( deps, test, complete, args ) { |
| 16 | var t = $( this ); |
| 17 | |
| 18 | t.on( 'change', function () { |
| 19 | var val = test( t ); |
| 20 | |
| 21 | $.each( deps, function ( i, v ) { |
| 22 | var elem = $( v ); |
| 23 | |
| 24 | if ( ! elem.length ) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | var target = elem.closest( '.yith-plugin-fw__panel__option' ); |
| 29 | |
| 30 | if ( ! target.length ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | if ( val ) { |
| 35 | target.show(); |
| 36 | } else { |
| 37 | target.hide(); |
| 38 | } |
| 39 | } ); |
| 40 | |
| 41 | if ( typeof complete !== 'undefined' ) { |
| 42 | complete( t, args ); |
| 43 | } |
| 44 | } ).change(); |
| 45 | }; |
| 46 | |
| 47 | /* === UTILITY FUNCTIONS === */ |
| 48 | |
| 49 | var getRadioValue = function ( t ) { |
| 50 | if ( ! t.is( 'input[type="radio"]' ) ) { |
| 51 | t = t.find( 'input[type="radio"]:checked' ); |
| 52 | } |
| 53 | |
| 54 | return t?.val(); |
| 55 | }, |
| 56 | isRadioYes = function ( t ) { |
| 57 | return getRadioValue( t ) === 'yes'; |
| 58 | }, |
| 59 | isRadioNo = function ( t ) { |
| 60 | return getRadioValue( t ) === 'no'; |
| 61 | }, |
| 62 | isChecked = function ( t ) { |
| 63 | return t.is( ':checked' ); |
| 64 | }; |
| 65 | |
| 66 | /* === SETTINGS HANDLING === */ |
| 67 | |
| 68 | var disable_wishlist_for_unauth_users = $( '#yith_wcwl_disable_wishlist_for_unauthenticated_users' ), |
| 69 | multi_wishlist_enable = $( '#yith_wcwl_multi_wishlist_enable' ), |
| 70 | modal_enable = $( '#yith_wcwl_modal_enable' ), |
| 71 | loop_position = $( '#yith_wcwl_loop_position' ), |
| 72 | icon_select = $( '.icon-select' ), |
| 73 | ask_an_estimate_style = $( '[name="yith_wcwl_ask_an_estimate_style"]' ), |
| 74 | ask_an_estimate_icon = $( '#yith_wcwl_ask_an_estimate_icon' ), |
| 75 | enable_share = $( '#yith_wcwl_enable_share' ), |
| 76 | share_facebook = $( '#yith_wcwl_share_fb' ), |
| 77 | share_facebook_icon = $( '#yith_wcwl_fb_button_icon' ), |
| 78 | share_twitter = $( '#yith_wcwl_share_twitter' ), |
| 79 | share_twitter_icon = $( '#yith_wcwl_tw_button_icon' ), |
| 80 | share_pinterest = $( '#yith_wcwl_share_pinterest' ), |
| 81 | share_pinterest_icon = $( '#yith_wcwl_pr_button_icon' ), |
| 82 | share_email = $( '#yith_wcwl_share_email' ), |
| 83 | share_email_icon = $( '#yith_wcwl_em_button_icon' ), |
| 84 | share_whatsapp = $( '#yith_wcwl_share_whatsapp' ), |
| 85 | share_whatsapp_icon = $( '#yith_wcwl_wa_button_icon' ), |
| 86 | show_estimate_button = $( '#yith_wcwl_show_estimate_button' ), |
| 87 | show_additional_info_textarea = $( '#yith_wcwl_show_additional_info_textarea' ), |
| 88 | ask_an_estimate_fields = $( '#yith_wcwl_ask_an_estimate_fields' ), |
| 89 | promotion_mail_type = $( '#woocommerce_promotion_mail_settings\\[email_type\\]' ), |
| 90 | back_in_stock_mail_enabled = $( '#woocommerce_yith_wcwl_back_in_stock_settings\\[enabled\\]' ), |
| 91 | back_in_stock_mail_type = $( '#woocommerce_yith_wcwl_back_in_stock_settings\\[email_type\\]' ), |
| 92 | on_sale_item_mail_enabled = $( '#woocommerce_yith_wcwl_on_sale_item_settings\\[enabled\\]' ), |
| 93 | on_sale_item_mail_type = $( '#woocommerce_yith_wcwl_on_sale_item_settings\\[email_type\\]' ), |
| 94 | ask_an_estimate_type = $( '[id^="type_"]' ), |
| 95 | add_to_wishlist_icon_types = $( '#yith_wcwl_add_to_wishlist_icon_type, #yith_wcwl_added_to_wishlist_icon_type' ), |
| 96 | show_add_to_wishlist_in_loop = $( '#yith_wcwl_show_on_loop' ); |
| 97 | |
| 98 | loop_position.add( 'select#yith_wcwl_button_position' ).on( 'change', function () { |
| 99 | var t = $( this ), |
| 100 | v = t.val(); |
| 101 | |
| 102 | if ( 'shortcode' === v ) { |
| 103 | t.parent().parent().next().find( '.addon' ).show(); |
| 104 | } else { |
| 105 | t.parent().parent().next().find( '.addon' ).hide(); |
| 106 | } |
| 107 | } ).change(); |
| 108 | |
| 109 | ask_an_estimate_type.on( 'change', function () { |
| 110 | var t = $( this ), |
| 111 | v = t.val(), |
| 112 | options_field = t.closest( '.yith-toggle-content-row' ).next(); |
| 113 | |
| 114 | if ( v === 'radio' || v === 'select' ) { |
| 115 | options_field.show().fadeTo( 'slow', 1 ); |
| 116 | } else { |
| 117 | options_field.is( ':visible' ) ? options_field.fadeTo( 'slow', 0, function () { |
| 118 | options_field.hide(); |
| 119 | } ) : options_field.css( 'opacity', 0 ).hide(); |
| 120 | } |
| 121 | } ).change(); |
| 122 | |
| 123 | ask_an_estimate_style.on( 'change', function () { |
| 124 | ask_an_estimate_icon.change(); |
| 125 | } ); |
| 126 | |
| 127 | icon_select.each( function () { |
| 128 | var t = $( this ), |
| 129 | renderOption = function ( state ) { |
| 130 | if ( ! state.id ) { |
| 131 | return state.text; |
| 132 | } |
| 133 | return $( |
| 134 | '<span class="yith-wcwl-icon-option-container">' + yith_wcwl_admin.plugin_icons[ state.element.value.toLowerCase() ] + state.text + '</span>' |
| 135 | ); |
| 136 | }; |
| 137 | |
| 138 | t.select2( { |
| 139 | templateResult: renderOption |
| 140 | } ); |
| 141 | } ); |
| 142 | |
| 143 | disable_wishlist_for_unauth_users.dependency( [ |
| 144 | '#yith_wcwl_enable_multi_wishlist_for_unauthenticated_users', |
| 145 | ], function () { |
| 146 | return isChecked( multi_wishlist_enable ) && 'no' === getRadioValue(disable_wishlist_for_unauth_users); |
| 147 | } ); |
| 148 | |
| 149 | modal_enable.dependency( [ |
| 150 | '#yith_wcwl_show_exists_in_a_wishlist' |
| 151 | ], function () { |
| 152 | var res = modal_enable.find( ':checked' ).val() !== 'default'; |
| 153 | |
| 154 | if ( ! res ) { |
| 155 | $( '#yith_wcwl_show_exists_in_a_wishlist' ).prop( 'checked', true ); |
| 156 | } |
| 157 | |
| 158 | return res; |
| 159 | } ); |
| 160 | |
| 161 | ask_an_estimate_icon.dependency( [ |
| 162 | '#yith_wcwl_ask_an_estimate_custom_icon' |
| 163 | ], function () { |
| 164 | return 'custom' === ask_an_estimate_icon.val() && 'button_custom' === ask_an_estimate_style.filter( ':checked' ).val(); |
| 165 | } ); |
| 166 | |
| 167 | enable_share.dependency( [ |
| 168 | '#yith_wcwl_share_fb' |
| 169 | ], isChecked, function () { |
| 170 | share_facebook.change(); |
| 171 | share_facebook_icon.change(); |
| 172 | share_twitter.change(); |
| 173 | share_twitter_icon.change(); |
| 174 | share_pinterest.change(); |
| 175 | share_pinterest_icon.change(); |
| 176 | share_email.change(); |
| 177 | share_email_icon.change(); |
| 178 | share_whatsapp.change(); |
| 179 | share_whatsapp_icon.change(); |
| 180 | } ); |
| 181 | |
| 182 | show_estimate_button.dependency( [ |
| 183 | '#yith_wcwl_show_additional_info_textarea' |
| 184 | ], isChecked, function () { |
| 185 | show_additional_info_textarea.change(); |
| 186 | } ); |
| 187 | |
| 188 | show_estimate_button.on( 'change', function () { |
| 189 | show_additional_info_textarea.change(); |
| 190 | } ); |
| 191 | |
| 192 | show_additional_info_textarea.dependency( [ |
| 193 | '#yith_wcwl_additional_info_textarea_label' |
| 194 | ], function () { |
| 195 | return isChecked( show_estimate_button ) && isChecked( show_additional_info_textarea ); |
| 196 | } ); |
| 197 | |
| 198 | show_additional_info_textarea.on( 'change', function () { |
| 199 | var t = $( this ); |
| 200 | |
| 201 | if ( t.is( ':checked' ) && show_estimate_button.is( ':checked' ) ) { |
| 202 | ask_an_estimate_fields.removeClass( 'yith-disabled' ); |
| 203 | } else { |
| 204 | ask_an_estimate_fields.addClass( 'yith-disabled' ); |
| 205 | } |
| 206 | } ); |
| 207 | |
| 208 | promotion_mail_type.dependency( [ |
| 209 | '#woocommerce_promotion_mail_settings\\[content_html\\]' |
| 210 | ], function () { |
| 211 | return 'multipart' === promotion_mail_type.val() || 'html' === promotion_mail_type.val(); |
| 212 | } ); |
| 213 | |
| 214 | promotion_mail_type.dependency( [ |
| 215 | '#woocommerce_promotion_mail_settings\\[content_text\\]' |
| 216 | ], function () { |
| 217 | return 'multipart' === promotion_mail_type.val() || 'plain' === promotion_mail_type.val(); |
| 218 | } ); |
| 219 | |
| 220 | back_in_stock_mail_enabled.dependency( [ |
| 221 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[product_exclusions\\]', |
| 222 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[category_exclusions\\]', |
| 223 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[email_type\\]', |
| 224 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[heading\\]', |
| 225 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[subject\\]', |
| 226 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[content_html\\]', |
| 227 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[content_text\\]' |
| 228 | ], function () { |
| 229 | return isChecked( back_in_stock_mail_enabled ); |
| 230 | }, function () { |
| 231 | back_in_stock_mail_type.change(); |
| 232 | } ); |
| 233 | |
| 234 | back_in_stock_mail_type.dependency( [ |
| 235 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[content_html\\]' |
| 236 | ], function () { |
| 237 | return ('multipart' === back_in_stock_mail_type.val() || 'html' === back_in_stock_mail_type.val()) && isChecked( back_in_stock_mail_enabled ); |
| 238 | } ); |
| 239 | |
| 240 | back_in_stock_mail_type.dependency( [ |
| 241 | '#woocommerce_yith_wcwl_back_in_stock_settings\\[content_text\\]' |
| 242 | ], function () { |
| 243 | return ('multipart' === back_in_stock_mail_type.val() || 'plain' === back_in_stock_mail_type.val()) && isChecked( back_in_stock_mail_enabled ); |
| 244 | } ); |
| 245 | |
| 246 | on_sale_item_mail_enabled.dependency( [ |
| 247 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[product_exclusions\\]', |
| 248 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[category_exclusions\\]', |
| 249 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[email_type\\]', |
| 250 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[heading\\]', |
| 251 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[subject\\]', |
| 252 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[content_html\\]', |
| 253 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[content_text\\]' |
| 254 | ], function () { |
| 255 | return isChecked( on_sale_item_mail_enabled ); |
| 256 | }, function () { |
| 257 | on_sale_item_mail_type.change(); |
| 258 | } ); |
| 259 | |
| 260 | on_sale_item_mail_type.dependency( [ |
| 261 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[content_html\\]' |
| 262 | ], function () { |
| 263 | return ('multipart' === on_sale_item_mail_type.val() || 'html' === on_sale_item_mail_type.val()) && isChecked( on_sale_item_mail_enabled ); |
| 264 | } ); |
| 265 | |
| 266 | on_sale_item_mail_type.dependency( [ |
| 267 | '#woocommerce_yith_wcwl_on_sale_item_settings\\[content_text\\]' |
| 268 | ], function () { |
| 269 | return ('multipart' === on_sale_item_mail_type.val() || 'plain' === on_sale_item_mail_type.val()) && isChecked( on_sale_item_mail_enabled ); |
| 270 | } ); |
| 271 | |
| 272 | add_to_wishlist_icon_types.dependency( |
| 273 | [ '#yith_wcwl_added_to_wishlist_icon_color' ], |
| 274 | function () { |
| 275 | const addType = getRadioValue( $( '#yith_wcwl_add_to_wishlist_icon_type' ) ), |
| 276 | addedType = getRadioValue( $( '#yith_wcwl_added_to_wishlist_icon_type' ) ); |
| 277 | |
| 278 | return 'default' === addedType || ('same' === addedType && 'default' === addType); |
| 279 | } |
| 280 | ); |
| 281 | |
| 282 | show_add_to_wishlist_in_loop.dependency( |
| 283 | [ '#yith_wcwl_position_over_image_in_loop', '#yith_wcwl_button_over_image_style' ], |
| 284 | function () { |
| 285 | const showAddToWishlistInLoop = $( '#yith_wcwl_show_on_loop' ).val(); |
| 286 | const loopPosition = $( '#yith_wcwl_loop_position' ).val(); |
| 287 | |
| 288 | return 'yes' === showAddToWishlistInLoop && 'before_image' === loopPosition; |
| 289 | } |
| 290 | ); |
| 291 | |
| 292 | ask_an_estimate_style.dependency( |
| 293 | [ '#yith_wcwl_ask_an_estimate_custom_icon' ], |
| 294 | function () { |
| 295 | const askAnEstimateButtonStyle = $( '#yith_wcwl_ask_an_estimate_style' ).val(); |
| 296 | const iconType = $('#yith_wcwl_ask_an_estimate_icon_type').val(); |
| 297 | |
| 298 | return 'button_custom' === askAnEstimateButtonStyle && 'custom' === iconType; |
| 299 | } |
| 300 | ); |
| 301 | |
| 302 | ask_an_estimate_style.dependency( |
| 303 | [ '#yith_wcwl_ask_an_estimate_icon' ], |
| 304 | function () { |
| 305 | const askAnEstimateButtonStyle = $( '#yith_wcwl_ask_an_estimate_style' ).val(); |
| 306 | const iconType = $('#yith_wcwl_ask_an_estimate_icon_type').val(); |
| 307 | |
| 308 | return 'button_custom' === askAnEstimateButtonStyle && 'default' === iconType; |
| 309 | } |
| 310 | ); |
| 311 | |
| 312 | /* === DATEPICKER === */ |
| 313 | |
| 314 | // commissions filter |
| 315 | $( '.date-picker-field, .date-picker' ).datepicker( { |
| 316 | dateFormat: 'yy-mm-dd', |
| 317 | numberOfMonths: 1, |
| 318 | showButtonPanel: true |
| 319 | } ); |
| 320 | |
| 321 | /* === TOGGLE BOX HANDLING === */ |
| 322 | |
| 323 | $( document ).on( 'yith-add-box-button-toggle', function () { |
| 324 | var ask_an_estimate_type_new = $( '#new_type' ), |
| 325 | ask_an_estimate_options_new = $( '#new_options' ), |
| 326 | target = ask_an_estimate_options_new.closest( '.yith-add-box-row' ); |
| 327 | |
| 328 | ask_an_estimate_type_new.on( 'change', function () { |
| 329 | var v = ask_an_estimate_type_new.val(); |
| 330 | |
| 331 | if ( v === 'radio' || v === 'select' ) { |
| 332 | target.show().fadeTo( 'slow', 1 ); |
| 333 | } else { |
| 334 | target.is( ':visible' ) ? target.fadeTo( 'slow', 0, function () { |
| 335 | $( this ).hide() |
| 336 | } ) : target.css( 'opacity', 0 ).hide(); |
| 337 | } |
| 338 | } ).change(); |
| 339 | } ); |
| 340 | |
| 341 | // Email settings actions |
| 342 | $( document ).on( 'click', '.toggle-settings', function ( e ) { |
| 343 | e.preventDefault(); |
| 344 | $( this ).closest( '.yith-wcwl-row' ).toggleClass( 'active' ); |
| 345 | const target = $( this ).data( 'target' ); |
| 346 | $( '#' + target ).slideToggle(); |
| 347 | } ); |
| 348 | |
| 349 | $( document ).on( 'click', '.yith-wcwl-save-settings', function ( e ) { |
| 350 | e.preventDefault(); |
| 351 | $( this ).closest( 'form' ).find( '.wp-switch-editor.switch-html' ).trigger( 'click' ); |
| 352 | const email_key = $( this.closest( '.email-settings' ) ).attr( 'id' ); |
| 353 | const data = { |
| 354 | 'action': 'yith_wcwl_save_email_settings', |
| 355 | 'params': $( this ).closest( 'form' ).serialize(), |
| 356 | 'email_key': email_key, |
| 357 | }; |
| 358 | $.ajax( { |
| 359 | type: 'POST', |
| 360 | data: data, |
| 361 | url: ajaxurl, |
| 362 | success: function () { |
| 363 | const row_active = $( '.yith-wcwl-row.active' ); |
| 364 | row_active.find( '.email-settings' ).slideToggle(); |
| 365 | row_active.toggleClass( 'active' ); |
| 366 | }, |
| 367 | } ); |
| 368 | } ); |
| 369 | |
| 370 | $( document ).on( 'change', '#yith-wcwl-email-status', function () { |
| 371 | |
| 372 | const data = { |
| 373 | 'action': 'yith_wcwl_save_mail_status', |
| 374 | 'enabled': $( this ).val(), |
| 375 | 'email_key': $( this ).closest( '.yith-plugin-fw-onoff-container ' ).data( 'email_key' ), |
| 376 | }; |
| 377 | |
| 378 | $.ajax( { |
| 379 | type: 'POST', |
| 380 | data: data, |
| 381 | url: ajaxurl, |
| 382 | success: function () { |
| 383 | console.log( 'Email status updated' ); |
| 384 | } |
| 385 | } ); |
| 386 | |
| 387 | } ); |
| 388 | |
| 389 | $( '#popular-filter .tablenav #post-query-submit' ).on( 'click', function () { |
| 390 | window.onbeforeunload = null; |
| 391 | } ); |
| 392 | |
| 393 | $( document ).on( 'click', '#wishlist-filter table.wishlists .yith-plugin-fw__action-button--delete-action a', function ( e ) { |
| 394 | e.preventDefault(); |
| 395 | e.stopPropagation(); |
| 396 | |
| 397 | var url = $( this ).attr( 'href' ), |
| 398 | wishlistName = $( this ).closest( 'tr' ).find( 'td.name a' ).text(); |
| 399 | |
| 400 | yith.ui.confirm( { |
| 401 | title: yith_wcwl_admin.i18n.deleteWishlistConfirmModal.title, |
| 402 | message: yith_wcwl_admin.i18n.deleteWishlistConfirmModal.message.replace( '%s', `<b>"${ wishlistName }"</b>` ), |
| 403 | confirmButtonType: 'delete', |
| 404 | confirmButton: yith_wcwl_admin.i18n.deleteWishlistConfirmModal.confirmButton, |
| 405 | closeAfterConfirm: false, |
| 406 | onConfirm: function () { |
| 407 | window.location.href = url; |
| 408 | }, |
| 409 | closeWhenClickingOnOverlay: true, |
| 410 | } ); |
| 411 | } ); |
| 412 | } ); |
| 413 |