1/** 2 * ywsbs-frontend.js 3 * 4 * @author YITH <[email protected]> 5 * @package YITH WooCommerce Subscription 6 * @version 1.0.0 7 */ 8 9/* global yith_ywsbs_frontend */ 10jQuery(function ($) { 11 'use strict'; 12 13 var $body = $('body'); 14 var blockParams = { 15 message: null, 16 overlayCSS: {background: '#fff', opacity: 0.7}, 17 ignoreIfBlocked: true 18 }; 19 20 // Change Add to cart Label on variable product when a variation is selected. 21 $.fn.yith_ywsbs_variations = function () { 22 var $form = $('.variations_form'), 23 $button = $form.find('.single_add_to_cart_button'); 24 25 26 $form.on('found_variation', function (event, variation) { 27 if ( variation.is_subscription == true ) { 28 $button.text(yith_ywsbs_frontend.add_to_cart_label); 29 } else { 30 $button.text(yith_ywsbs_frontend.default_cart_label); 31 } 32 }); 33 34 }; 35 36 if ( $body.hasClass('single-product') ) { 37 $.fn.yith_ywsbs_variations(); 38 } 39 40 /** 41 * MODAL 42 */ 43 var modal = false, 44 modal_wrapper = false; 45 46 var openModal = function () { 47 modal.fadeIn('slow'); 48 }; 49 50 var closeModal = function () { 51 modal.fadeOut('slow'); 52 }; 53 54 var modalWrapperPosition = function () { 55 var modalWidth = modal_wrapper.width(), 56 modalHeigth = modal_wrapper.width(), 57 window_w = $(window).width(), 58 window_h = $(window).height(), 59 margin = ((window_h - modalHeigth) / 2) + 'px', 60 width = ((window_w - 100) > modalWidth) ? modalWidth + 'px' : 'auto'; 61 62 modal_wrapper.css({ 63 'margin-top': margin, 64 'margin-bottom': margin, 65 'width': width, 66 }); 67 }; 68 69 $(document).on('click', '.ywsbs-dropdown-item, .open-modal', function (e) { 70 e.stopPropagation(); 71 var $t = $(this); 72 var modalToOpenID = $t.data('target'); 73 74 modal = $('#' + modalToOpenID); 75 modal_wrapper = modal.find('.ywsbs-modal-wrapper'); 76 modalWrapperPosition(); 77 openModal(); 78 }); 79 80 $(document).on('click', '.ywsbs-modal .close', function (e) { 81 e.preventDefault(); 82 closeModal(); 83 }); 84 85 $(window).on('click', function (e) { 86 var target = e.target; 87 if ( $(target).hasClass('ywsbs-modal-container') ) { 88 closeModal(); 89 } 90 }); 91 92 /** 93 * Change subscription status 94 */ 95 96 function reloadSubscriptionView() { 97 $.post(document.location.href, function (data) { 98 if ( data != '' ) { 99 var c = $("<div></div>").html(data), 100 wrap = c.find('.ywsbs-subscription-view-wrap'); 101 $('.ywsbs-subscription-view-wrap').html(wrap.html()); 102 } 103 }); 104 } 105 106 $(document).on('click', '.ywsbs-action-button', function (e) { 107 e.preventDefault(); 108 var $t = $(this), 109 container = $t.closest('.ywsbs-action-button-wrap'), 110 modalWrapper = $t.closest('.ywsbs-modal-body'), 111 modalBody = modalWrapper.find('.ywsbs-content-text'), 112 closeButton = modalWrapper.find('.close-modal-wrap'), 113 status = $t.data('action'), 114 sbs_id = $t.data('id'), 115 security = $t.data('nonce'); 116 117 container.block(blockParams); 118 var data = { 119 subscription_id: sbs_id, 120 action: 'ywsbs_' + status + '_subscription', 121 change_status: status, 122 security: security, 123 context: 'frontend' 124 }; 125 126 $.ajax({ 127 url: yith_ywsbs_frontend.ajaxurl, 128 data: data, 129 type: 'POST', 130 success: function (response) { 131 if ( response.success ) { 132 modalBody.html(response.success); 133 } 134 135 if ( response.error ) { 136 modalBody.html('<span class="error">' + response.error + '</span>'); 137 } 138 139 $t.fadeOut(); 140 closeButton.fadeOut(); 141 setTimeout(function () { 142 closeModal(); 143 reloadSubscriptionView(sbs_id); 144 }, 2500); 145 }, 146 complete: function () { 147 container.unblock(); 148 } 149 }); 150 }); 151});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.