| 1 |
(function ($) { |
| 2 |
var is_cart = false |
| 3 |
var popupLoaded = false |
| 4 |
var _initevent = function () { |
| 5 |
var showpopup = $("#oney-show-popup") |
| 6 |
var oneyData = $("#oney-show-popup").closest('.payplug-oney') |
| 7 |
var showpopuponey = $("#oney-show-popup").closest('.payplug-oney-popup') |
| 8 |
var qtyInput = $('input[name=quantity]') |
| 9 |
var popup = $("#oney-popup") |
| 10 |
var loading = popup.find('.payplug-lds-roller') |
| 11 |
var oneyError = popup.find('#oney-popup-error') |
| 12 |
var totalsProduct = oneyData.data('total-products') |
| 13 |
var maxOneyQty = oneyData.data('max-oney-qty') |
| 14 |
is_cart = oneyData.data('is-cart') |
| 15 |
|
| 16 |
function _showPopup(show) { |
| 17 |
if (!oneyData.hasClass('disabled') && !popupLoaded && !show) { |
| 18 |
return |
| 19 |
} |
| 20 |
popup.show(0, function () { |
| 21 |
if (!$.browser.mobile) { |
| 22 |
_checkOneyError() |
| 23 |
var top = oneyData.hasClass('disabled') ? 50 : 110 |
| 24 |
popup.css('position', 'fixed') |
| 25 |
popup.position({ |
| 26 |
my: popupLoaded ? "left top-" + top : "left top-75", |
| 27 |
at: popupLoaded ? "right+40 bottom" : "right bottom", |
| 28 |
of: showpopup, |
| 29 |
}) |
| 30 |
} |
| 31 |
}) |
| 32 |
} |
| 33 |
|
| 34 |
function _hidePopup() { |
| 35 |
popup.hide(); |
| 36 |
} |
| 37 |
|
| 38 |
function _calculTotals() { |
| 39 |
var price = oneyData.data('price') |
| 40 |
return qtyInput.length ? totalsProduct * price : price |
| 41 |
} |
| 42 |
|
| 43 |
function _isInOneyRange() { |
| 44 |
var totalPrice = _calculTotals(); |
| 45 |
var minOney = oneyData.data('min-oney'); |
| 46 |
var maxOney = oneyData.data('max-oney'); |
| 47 |
return (totalPrice >= minOney && totalPrice <= maxOney); |
| 48 |
}; |
| 49 |
|
| 50 |
function _checkOneyError() { |
| 51 |
if (oneyData.hasClass('disabled')) { |
| 52 |
popupLoaded = true |
| 53 |
popup.html('').append($(oneyError)) |
| 54 |
popup.addClass('loaded') |
| 55 |
popup.find('.payplug-lds-roller').hide() |
| 56 |
popup.find('#oney-popup-error .oney-error').hide() |
| 57 |
totalsProduct >= maxOneyQty ? |
| 58 |
popup.find('#oney-popup-error .oney-error.qty').show() : |
| 59 |
popup.find('#oney-popup-error .oney-error.range').show() |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
function _bindCloseOneyPopup() { |
| 64 |
$(document).unbind('mouseup') |
| 65 |
$(document).mouseup(function (e) { |
| 66 |
// if the target of the click isn't the container nor a descendant of the container |
| 67 |
if (!popup.is(e.target) && popup.has(e.target).length === 0) { |
| 68 |
_hidePopup(); |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
$('#oney-popup-close').unbind(); |
| 73 |
$('#oney-popup-close').on('click', function () { |
| 74 |
_hidePopup(); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
_bindCloseOneyPopup(); |
| 79 |
qtyInput.unbind(); |
| 80 |
qtyInput.on('change', function () { |
| 81 |
totalsProduct = $(this).val() |
| 82 |
popupLoaded = false |
| 83 |
if (_isInOneyRange() && totalsProduct < maxOneyQty) { |
| 84 |
oneyData.removeClass('disabled') |
| 85 |
popup.removeClass('disabled').removeClass('loaded') |
| 86 |
popup.html('') |
| 87 |
} else { |
| 88 |
_checkOneyError() |
| 89 |
oneyData.addClass('disabled') |
| 90 |
popup.addClass('disabled') |
| 91 |
} |
| 92 |
}); |
| 93 |
showpopuponey.unbind(); |
| 94 |
showpopuponey.on('click', function () { |
| 95 |
_showPopup(true); |
| 96 |
if (_isInOneyRange() && totalsProduct < maxOneyQty) { |
| 97 |
if (popupLoaded) { |
| 98 |
return |
| 99 |
} |
| 100 |
popup.html('').append($(loading)) |
| 101 |
$.post( |
| 102 |
payplug_config.ajax_url, |
| 103 |
{ |
| 104 |
'action': payplug_config.ajax_action, |
| 105 |
'price': _calculTotals() |
| 106 |
}, function (response) { |
| 107 |
if (response.data.popup) { |
| 108 |
popupLoaded = true; |
| 109 |
popup.addClass('loaded'); |
| 110 |
popup.html(response.data.popup); |
| 111 |
_showPopup(); |
| 112 |
_bindCloseOneyPopup() |
| 113 |
} |
| 114 |
} |
| 115 |
); |
| 116 |
} else { |
| 117 |
_checkOneyError() |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
$(window).on('scroll', function () { |
| 122 |
if(popup.is(':visible')) { |
| 123 |
_showPopup() |
| 124 |
} |
| 125 |
}); |
| 126 |
|
| 127 |
$(document).ready(function () { |
| 128 |
if (!(is_cart)) { |
| 129 |
totalsProduct = qtyInput.val() |
| 130 |
popupLoaded = false |
| 131 |
if (_isInOneyRange() && totalsProduct < maxOneyQty) { |
| 132 |
oneyData.removeClass('disabled') |
| 133 |
popup.removeClass('disabled').removeClass('loaded') |
| 134 |
popup.html('') |
| 135 |
} else { |
| 136 |
_checkOneyError() |
| 137 |
oneyData.addClass('disabled') |
| 138 |
popup.addClass('disabled') |
| 139 |
} |
| 140 |
} |
| 141 |
}); |
| 142 |
}; |
| 143 |
|
| 144 |
$(function() { |
| 145 |
_initevent(); |
| 146 |
if (is_cart) { |
| 147 |
$(document).ajaxSuccess(function (event, request, settings) { |
| 148 |
if (settings.data.includes(payplug_config.ajax_action)) { |
| 149 |
return; |
| 150 |
} |
| 151 |
_initevent(); |
| 152 |
popupLoaded = false; |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
}); |
| 157 |
|
| 158 |
|
| 159 |
})(jQuery); |
| 160 |
|