var cart_ajax_url = '/shop/ajax/cartform';
var delivery_ajax_url = '/shop/ajax/delivery';
$(function() {
console.log('%cfunctions.rwd.js called', 'color:green');
var pull = $('.categories.first .current');
var menu = $('.categories.first ul');
var pullMainMenu = $('.menu-toggle');
var MainMenu = $('header nav.main ul');
$(pull).on('click', function(e) {
e.preventDefault();
if ($('.categories.first ul').css('display') == 'none') {
menu.css('display', 'block');
} else {
menu.css('display', 'none');
}
});
$(pullMainMenu).on('click', function(e) {
e.preventDefault();
if ($('header nav.main ul').css('display') == 'none') {
MainMenu.css('display', 'block');
} else {
MainMenu.css('display', 'none');
}
});
$('.menu-toggle p.current').text($('header nav.main ul li.active a span').text());
$('.categories.first .current .activecat').html('Select category');
$('#top-menu h3, #top-menu .current, .jumplist.first .current').click(function() {
$(this).parent().toggleClass('open');
});
$(".btn-grid").on("click", function(e) {
var postData = {
op: 'set_grid_option',
grid_opt: $(this).attr('data-id')
};
$.post(cart_ajax_url, postData, function(response) {
console.log(response);
if (response.ok) {
window.location.reload();
return;
}
}, "json");
});
$(document).on("submit", ".item-form", function(e) {
e.preventDefault();
var that = $(this);
var data = $(this).serialize() + "&op=add";
var ask_to_proceed = $(this).data("proceed-to-checkout") ? confirm("Proceed to checkout?") : true;
var hasError = false;
var custom_price = $(this).find('input[name="custom_price"]').val();
var custom_price_type = $(this).find('input[name="custom_price"]').attr('type');
var price_option = $(this).find('[name="price_option"]').val();
var gift_card_del = $(this).find('select[name="gift_card_delivery_method"]').val();
var min_price = parseFloat($(this).find('input[name="min_price"]').val());
var prod_type = $(this).find('input[name="prod_type"]');
var is_upsell = false;
if (typeof prod_type != 'undefined' && $(prod_type).val() == 'upsell') {
is_upsell = true;
hasError = false;
if (price_option == '') {
$(this).find('.error-msg').html('Please select a price.');
hasError = true;
}
} else if (custom_price == '' && price_option == '') {
var custom_amt_type = $(this).find('input[name="custom_price"]').attr('type');
if (custom_amt_type == 'hidden') {
$(this).find('.error-msg').html('Please select a price.');
} else {
$(this).find('.error-msg').html('Please select a price or enter a custom amount.');
}
var win_width = $(window).width();
if (win_width < 769) {
$('html, body').animate({
scrollTop: $(".item-form").offset().top - 100
}, 500);
}
hasError = true;
} else if (price_option == '' && !(custom_price >= min_price && custom_price <= 5000)) {
$(this).find('.error-msg').html("Enter a valid amount between " + min_price + " and 5,000");
hasError = true;
} else if (typeof price_option == 'undefined' && custom_price_type != 'hidden' && custom_price_type != 'undefined' && !isNaN(min_price) && !(custom_price >= min_price && custom_price <= 5000)) {
$(this).find('.error-msg').html("Enter a valid amount between " + min_price + " and 5,000");
hasError = true;
} else if (gift_card_del == 'gc') {
$(this).find('.error-msg').html("Select delivery method from list");
hasError = true;
}
if (!hasError) {
console.log('Valid form');
data += "&is_upsell=" + is_upsell;
$.post(cart_ajax_url, data, function(response) {
if (response.ok) {
if (is_upsell) {
$('.error-msg').html('');
var upd_count = parseInt(response.item_count);
var msgHtml = '
✔' + upd_count + ' in your basket
';
var upg_info = $(that).parents('.product-info-wrapper:eq(0)').find('.upg-info');
console.log(upg_info);
if (!upg_info || !upg_info.length) {
$(that).find('.upg-info').hide();
let tar = $(that).find('.product-links.upsell');
$(msgHtml).insertBefore(tar);
} else {
$(upg_info).html(msgHtml);
}
$(that).find('.upg-info').slideDown(800, 'easeInQuad');
return;
}
if (ask_to_proceed) {
window.location.href = response.next;
}
return;
} else {
$(that).find('.error-msg').html('
' + response.msg);
}
}, "json");
}
return false;
});
$("#cart-empty").on("click", function(e) {
e.preventDefault();
var customModal = new CustomModal($('#confirmModal'));
customModal.open("Are you sure you want to remove all items from your cart?");
customModal.confirm.then(function(resolve) {
console.log('Empty basket?', resolve);
customModal.close();
if (resolve) {
$.post(cart_ajax_url, {
op: "empty"
}, function(response) {
if (response.ok) {
location.reload();
return;
}
alert(response.msg);
}, "json");
} else {
return false;
}
}, function(reject) {
console.log('Confirm reject:', reject);
});
return false;
});
$(".basket-quantity select").on("change", function() {
var _id = $(this).data("id");
var _qty = $(this).val();
$.post(cart_ajax_url, {
op: "qty",
qty: _qty,
item_id: _id
}, function(response) {
if (response.ok) {
location.reload();
return;
}
alert(response.msg);
}, "json");
});
$(".basket-remove").on("click", function(e) {
e.preventDefault();
var _id = parseInt($(this).data("id"), 10);
var customModal = new CustomModal($('#confirmModal'));
customModal.open("Are you sure you want to remove this item?");
customModal.confirm.then(function(resolve) {
console.log('Confirm resolve:', resolve);
if (resolve) {
$.post(cart_ajax_url, {
op: "remove",
remove: _id
}, function(response) {
if (response.ok) {
customModal.close();
location.reload();
return;
}
alert(response.msg);
}, "json");
} else {
customModal.disableActionBtns(false);
customModal.close();
return false;
}
}, function(reject) {
console.log('Confirm reject:', reject);
});
return false;
});
function submitDiscountCode() {
var _code = $("#discount_code").val();
if (_code.length == 0) {
return false;
}
$.post(cart_ajax_url, {
op: "code",
code: _code,
email: custEmail
}, function(response) {
if (response.ok) {
$('.code-error').hide();
location.reload();
return;
}
$("#discount_code").val('');
$('.code-error').html(response.msg).show();
}, "json");
}
$("#submit_discount_code").on("click", function(e) {
e.preventDefault();
submitDiscountCode();
});
$("#shoppingcart").on("submit", function(e) {
var _code = $("#discount_code").val();
if (_code.length == 0) {
return true;
}
submitDiscountCode();
return false;
});
$('.basket-view').click(function() {
$cartimage = $('#cart-image img').attr('src');
$basketimage = $(this).parent().find('input[name="itemimage[]"]').val();
$('#cart-image').css('background-image', 'url(../images/imageframe.png)');
$('#cart-image img').attr('src', $basketimage);
return false;
});
if ($('input[name="delivery_date"]').length) {
if (typeof serverDate != 'undefined') {
date = new Date(serverDate);
} else {
date = new Date();
}
console.log('Current date:', date);
todaysdate = new Date(date.getFullYear(),date.getMonth(),date.getDate(),0,0,0);
today_str = todaysdate.toDateString();
dd = (date.getDate() < 10) ? '0' + date.getDate() : date.getDate();
mm = ((date.getMonth() + 1) < 10) ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
hours = (date.getHours() < 10) ? '0' + date.getHours() : date.getHours();
minutes = (date.getMinutes() < 10) ? '0' + date.getMinutes() : date.getMinutes();
yyyy = date.getFullYear();
$dateinput = selectDate();
$('.cal-container #dateselect').datepicker({
firstDay: 1,
defaultDate: $dateinput,
numberOfMonths: 2,
dateFormat: "yy-mm-dd",
gotoCurrent: false,
beforeShowDay: removeDays,
// yearRange: yyyy + ":" + (yyyy + 2),
yearRange: (yyyy - 1) + ":" + (yyyy + 2),
onSelect: function(value, date) {
$('input[name="delivery_date"]').val(value);
var sel = value;
var tdy = yyyy + '-' + mm + '-' + dd;
console.log('Selected: ', sel);
console.log('Today: ', tdy);
var calToday = false;
if (tdy == sel) {
console.log('today');
calToday = true;
if (typeof hide_sameday_am != 'undefined') {
console.log('hide_sameday_am:', hide_sameday_am);
$('select[name="delivery_time"] option[value="AM"]').attr('disabled', hide_sameday_am == 1);
$('select[name="delivery_time"]').val('');
}
} else {
console.log('not today');
$('select[name="delivery_time"] option[value="AM"]').attr('disabled', false);
}
if (typeof mandatory_collect_time != 'undefined' && typeof daywise_opening_times != 'undefined' && daywise_opening_times.length) {
var dayOfWeek = new Date(sel).getDay();
console.log('Get mandatory_collect_time for day ' + dayOfWeek);
mandatoryCollectionTimeopts(daywise_opening_times, dayOfWeek, calToday, sel);
}
if (typeof is_dyn_del !== 'undefined' && is_dyn_del == 1) {
console.log('Current date', value);
$('input[name="delivery_postcode"]').trigger('change');
}
},
onChangeMonthYear: function() {
setTimeout(function() {
hideToday();
}, 0);
}
});
if ($('.ui-datepicker').is(':visible')) {
var tdy_dt = yyyy + '-' + mm + '-' + dd;
var del_dt = '';
if ($('input[name="delivery_date"]').length) {
del_dt = $('input[name="delivery_date"]').val();
}
if ($dateinput != "" && (tdy_dt != del_dt)) {
hideToday();
}
}
}
$('#stockmessage a').click(function() {
$(this).parent().find('input').click();
return false;
});
$(".jump_menu").change(function() {
var url = $(this).val();
if (url != 'menu')
window.location = $(this).val();
});
var w = $(window).width();
if (w < 767) {
$(".products .description").each(function() {
var parent = $(this).parent();
$(this).detach().appendTo(parent);
});
}
$(window).resize(function() {
var w = $(window).width();
if (w >= 1024 && menu.is(':hidden')) {
menu.show();
}
if (w >= 1024 && MainMenu.is(':hidden')) {
MainMenu.show();
}
if (w < 767) {
$(".products .description").each(function() {
var parent = $(this).parent();
$(this).detach().appendTo(parent);
});
} else {
$(".products .description").each(function() {
$(this).insertBefore($(this).siblings('form'));
});
}
});
$('.view-deliver-to-areas').off('click').on('click', function() {
if (company_delivery_info) {
var customModal = new CustomModal($('#deliveryAreasModal'));
customModal.open();
}
return false;
});
$('#show_shop_opening_times').off('click').on('click', function() {
if (shop_opening_times) {
var customModal = new CustomModal($('#collectShopOpeningModal'));
customModal.open();
}
return false;
});
$(document).on("change", ".price_option, .item_choice", function() {
var source = $(this).attr('class');
var container = $(this).closest(".product-item");
var slider = container.find(".product-img");
var image_options = slider.data("options");
changeSlide(container, slider, image_options, $(this).val());
});
function changeSlide(container, slider, image_options, select_input) {
var overlap = [];
var product_id = slider.data('product-id');
var imgOptsWithAllColour = [];
var imgOptsWithAllPrice = [];
console.log('Image options:', image_options);
var currentItemChoice = container.find('select.item_choice').val();
if (typeof select_input != 'undefined') {
currentItemChoice = select_input;
}
var globalPriceVal = container.find(".price_option option:selected").index();
globalPriceVal = parseInt(globalPriceVal);
console.log('GLOBAL price val:', globalPriceVal);
if (image_options) {
imgOptsWithAllColour = image_options.filter(function(v) {
return (v.options.length == 0);
});
imgOptsWithAllPrice = image_options.filter(function(v) {
return (v.price_id == 0);
});
}
console.log('Img opts with all colour', imgOptsWithAllColour);
console.log('Img opts with all price', imgOptsWithAllPrice);
if (image_options) {
if (imgOptsWithAllColour.length == image_options.length) {
currentItemChoice = '';
}
var isPriceAll = false;
if (imgOptsWithAllPrice.length == image_options.length) {
isPriceAll = true;
}
for (var img_num in image_options) {
overlap[img_num] = 0;
var options = image_options[img_num];
if (!options)
continue;
if (!options['price_id'] && $.isEmptyObject(options['options']))
continue;
if (!currentItemChoice && options['price_id'] && options['price_id'] == globalPriceVal) {
overlap[img_num] += 1;
continue;
}
if ($.isEmptyObject(options['options'])) {
overlap[img_num] = -1;
} else {
var opt_val = currentItemChoice;
console.log('Options:', options['options']);
console.log('Global price val:', globalPriceVal);
console.log('Opt val:', opt_val);
for (var opt_id in options['options']) {
var choice_id = options['options'][opt_id];
if (isPriceAll) {
overlap[img_num] = (choice_id == opt_val) ? (overlap[img_num] + 1) : -1;
} else if ((choice_id == opt_val) && (options['price_id'] == 0 || options['price_id'] == globalPriceVal)) {
overlap[img_num] += 1;
} else {
overlap[img_num] = -1;
}
}
}
}
var max_overlap = Array.max(overlap);
var img_index = overlap.indexOf(max_overlap);
if (max_overlap != -1) {
slider.data('royalSlider').goTo(img_index);
}
}
}
Array.max = function(array) {
return Math.max.apply(Math, array);
}
;
$(document).on('click', '.rsImg.rsMainSlideImage', function(e) {
e.preventDefault();
var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
if (isMobile) {
console.log('Image zoom disabled for mobile');
return;
}
$(this).addClass('img-enlargable');
var src = $(this).attr('src');
var fullScreenModal;
function removeModal() {
fullScreenModal.remove();
$('body').off('keyup.modal-close');
}
var fullScreenDiv = '';
fullScreenModal = $(fullScreenDiv).css({
background: 'RGBA(0,0,0,.5) url(' + src + ') no-repeat center',
backgroundSize: 'contain',
width: '100%',
height: '100%',
position: 'fixed',
zIndex: '10000',
top: '0',
left: '0',
cursor: 'zoom-out'
}).click(function() {
removeModal();
}).appendTo('body');
$('body').on('keyup.modal-close', function(e) {
if (e.key === 'Escape') {
removeModal();
}
});
});
});
function mandatoryCollectionTimeopts(daywise_opening_times, dayOfWeek, isToday, selDate) {
console.log('Fetch timeslot for day: ', dayOfWeek);
var sel_timeslots = daywise_opening_times.find(function(day_open) {
return day_open.weekday == dayOfWeek
});
console.log('Timeslots: ', sel_timeslots);
console.log('Available exp times', available_exp_times);
if (!!sel_timeslots) {
var slots = [];
if (isToday && sel_timeslots.hasOwnProperty('same_day_times') && sel_timeslots.same_day_times.length) {
slots = sel_timeslots.same_day_times;
} else if (sel_timeslots.hasOwnProperty('times') && sel_timeslots.times.length) {
slots = sel_timeslots.times;
}
if (!!available_exp_times && available_exp_times.hasOwnProperty(selDate)) {
if (isToday) {
slots = available_exp_times[selDate].same_day_times;
} else {
slots = available_exp_times[selDate].times;
}
}
if (slots.length == 0) {
console.log('no slot found, use most common one');
var loop = 1;
while (loop) {
var sel_timeslots = daywise_opening_times.find(function(day_open) {
if (day_open.weekday == (dayOfWeek + loop)) {
return true;
}
});
console.log('sel_timeslots', sel_timeslots);
if (!sel_timeslots) {
loop = 0;
break;
}
if (sel_timeslots.hasOwnProperty('times') && sel_timeslots.times.length) {
slots = sel_timeslots.times;
loop = 0;
break;
} else {
loop++;
}
}
}
console.log('Final slots:', slots);
$('select[name="delivery_time"]').find('option').not(':first').remove();
if (!!slots) {
slots.forEach(function(slot) {
var selc = '';
if (typeof cart_del_time !== 'undefined' && cart_del_time == slot) {
selc = 'selected';
}
$('select[name="delivery_time"]').append('');
});
}
}
}
function hideToday() {
$('.ui-datepicker .ui-datepicker-today .ui-state-highlight').removeClass('ui-state-highlight');
$('.ui-datepicker .ui-datepicker-today .ui-state-active').removeClass('ui-state-active');
$('.ui-datepicker .ui-datepicker-today').removeClass('ui-datepicker-current-day');
$('.ui-datepicker .ui-datepicker-today').removeClass('ui-datepicker-today');
}
function selectDate() {
$daterequired = $('input[name="delivery_date"]').val();
if ($daterequired != "") {
$datesplit = $daterequired.split("-");
$dateday = $datesplit[2];
$datemonth = $datesplit[1];
$dateyear = $datesplit[0];
return new Date($dateyear,$datemonth - 1,$dateday,0,0,0);
}
}
function removeDays(calendardate) {
if (calendardate < todaysdate)
return [false];
if ((calendardate.toDateString() == today_str) && delivery_today == false)
return [false];
var caldate = calendardate.getDate();
var calmon = calendardate.getMonth();
var calyear = calendardate.getFullYear();
var result = true;
var changed = false;
for (var i = 0; i < excluded_dates.length; i++) {
var exdate = excluded_dates[i];
if (exdate[2] && exdate[3]) {
if (caldate == exdate[0] && calmon == exdate[1] && (calyear == exdate[2] || exdate[2] == 1971)) {
if (exdate[3] == 'closed') {
result = result && false;
} else {
result = result || true;
}
changed = true;
}
} else {
if (caldate == exdate[0] && calmon == exdate[1])
return [false];
}
}
if (changed)
return [result];
if (available_days != '') {
var daysopen = available_days.split(",");
daynum = calendardate.getDay();
for (var i = 0; i < daysopen.length; i++) {
if (daysopen[i] == daynum)
return [true];
}
}
return [false];
}
function removeDaysDeliverySchedule(calendardate) {
if (calendardate < todaysdate)
return [false, 'past-date'];
var today_cls = '';
if ((calendardate.toDateString() == today_str)) {
today_cls = 'calendar-today-date';
}
var caldate = calendardate.getDate();
var calmon = calendardate.getMonth();
var calyear = calendardate.getFullYear();
var result = true;
var changed = false;
var calm = parseInt(calendardate.getMonth()) + 1
, cald = calendardate.getDate()
, caly = calendardate.getFullYear();
if (cald < 10)
cald = '0' + cald.toString();
if (calm < 10)
calm = '0' + calm.toString();
var calMDY = calm + '/' + cald + '/' + caly;
console.log('Cal date', calMDY);
if (closedDates.indexOf(calMDY) != -1) {
console.log('Closed date', calMDY);
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
if (openDates.indexOf(calMDY) != -1) {
return [true, 'calendar-open-date ' + today_cls, 'Available'];
}
if (excluded_dates.indexOf(calMDY) != -1) {
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
if (available_days != '') {
var daysopen = available_days.split(",");
daynum = calendardate.getDay();
for (var i = 0; i < daysopen.length; i++) {
if (daysopen[i] == daynum) {
return [true, 'calendar-selectable-date ' + today_cls, 'Available'];
}
}
}
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
function removeDaysDetailsDelivery(calendardate) {
if (calendardate < todaysdate)
return [false, 'past-date'];
var today_cls = '';
if ((calendardate.toDateString() == today_str)) {
today_cls = 'calendar-today-date';
}
var caldate = calendardate.getDate();
var calmon = calendardate.getMonth();
var calyear = calendardate.getFullYear();
var result = true;
var changed = false;
var calm = parseInt(calendardate.getMonth()) + 1
, cald = calendardate.getDate()
, caly = calendardate.getFullYear();
if (cald < 10)
cald = '0' + cald.toString();
if (calm < 10)
calm = '0' + calm.toString();
var calMDY = calm + '/' + cald + '/' + calyear;
console.log('Cal date', calMDY);
if (closedDatesDelivery.indexOf(calMDY) != -1) {
console.log('Closed delivery date', calMDY);
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
if (openDatesDelivery.indexOf(calMDY) != -1) {
return [true, 'calendar-open-date ' + today_cls, 'Available'];
}
if (available_delivery_days != '') {
var daysopen = available_delivery_days.split(",");
daynum = calendardate.getDay();
for (var i = 0; i < daysopen.length; i++) {
if (daysopen[i] == daynum) {
return [true, 'calendar-selectable-date ' + today_cls, 'Available'];
}
}
}
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
function removeDaysDetailsCollection(calendardate) {
if (calendardate < todaysdate)
return [false, 'past-date'];
var today_cls = '';
if ((calendardate.toDateString() == today_str)) {
today_cls = 'calendar-today-date';
}
var caldate = calendardate.getDate();
var calmon = calendardate.getMonth();
var calyear = calendardate.getFullYear();
var result = true;
var changed = false;
var calm = parseInt(calendardate.getMonth()) + 1
, cald = calendardate.getDate()
, caly = calendardate.getFullYear();
if (cald < 10)
cald = '0' + cald.toString();
if (calm < 10)
calm = '0' + calm.toString();
var calMDY = calm + '/' + cald + '/' + calyear;
console.log('Cal date', calMDY);
if (closedDatesCollection.indexOf(calMDY) != -1) {
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
if (openDatesCollection.indexOf(calMDY) != -1) {
return [true, 'calendar-open-date ' + today_cls, 'Available'];
}
if (available_collect_days != '') {
var daysopen = available_collect_days.split(",");
daynum = calendardate.getDay();
for (var i = 0; i < daysopen.length; i++) {
if (daysopen[i] == daynum) {
return [true, 'calendar-selectable-date ' + today_cls, 'Available'];
}
}
}
return [true, 'calendar-closed-date ' + today_cls, 'Not available'];
}
function loadRSSlider() {
if ($('.product-img')[0]) {
$(".product-img:not('.lazy')").royalSlider({
autoHeight: true,
transitionType: 'fade',
transitionSpeed: 550,
controlNavigation: 'thumbnails',
arrowsNav: true,
visibleNearby: {
navigateByCenterClick: true,
},
arrowsNavHideOnTouch: false,
arrowsNavAutoHide: false,
imageScaleMode: 'fill',
imageAlignCenter: false,
loop: false,
loopRewind: true,
keyboardNavEnabled: true,
allowCSS3: true,
randomizeSlides: false,
numImagesToPreload: 4,
usePreloader: true,
autoPlay: {
delay: 2800,
enabled: false
},
thumbs: {
appendSpan: true,
arrows: false,
firstMargin: false
},
navigateByClick: false
});
$(".product-img:not('.lazy')").each(function() {
$(this).data('royalSlider').ev.on('rsAfterSlideChange', function(event) {
var slider = event.currentTarget.slider;
var img_num = event.currentTarget.currSlideId;
var options = slider.data('options');
var container = slider.closest('.product-item');
var img_options = options[img_num];
var product_id = slider.data('product-id');
console.log('Image options pid ' + product_id);
console.log('Img options', img_options);
if (img_options) {
var currentItemChoice = container.find('select.item_choice').val();
if (img_options['price_id'] && img_options['price_id'] != 0) {
var parse_opt_id = parseInt(img_options['price_id']) + 1;
console.log('parse_opt_id', parse_opt_id)
var hdn_pid = $('input[type="hidden"][name="pid"][value="' + product_id + '"]');
var price_id_data = $(hdn_pid).parents('form:eq(0)').find(".price_option option:nth-child(" + parse_opt_id + ")").val();
container.find('select[name="price_option"]').val(price_id_data);
}
if (img_options['options']) {
var prod_selects = container.find('.item-form .item_choice');
for (var opt_id in img_options['options']) {
var choice_id = img_options['options'][opt_id];
var opt_id = '#product-option-' + product_id + '-' + opt_id;
$(opt_id).val(choice_id);
prod_selects = prod_selects.not(opt_id);
}
prod_selects.each(function() {
$(this).val('');
});
}
}
});
hide_slider_thumbs_if_single_image($(this));
});
}
}
function hide_slider_thumbs_if_single_image(image_container) {
var count = image_container.find('.rsSlide').length;
if (count < 2) {
image_container.find('.rsNav.rsThumbs').addClass('hidden');
}
}
console.log('%cfunctions-rwd loaded loadRSSlider', 'color:red', typeof loadRSSlider);
function initAutoResize() {
document.querySelectorAll('[data-autoresize]').forEach(function(element) {
element.style.boxSizing = 'border-box';
element.style.resize = 'none';
var offset = element.offsetHeight - element.clientHeight;
element.style.height = 'auto';
element.style.height = element.scrollHeight + offset + 'px';
console.log('Init textarea height:');
console.log(element.scrollHeight, offset);
element.removeAttribute('data-autoresize');
});
}
function addAutoResize() {
document.querySelectorAll('[data-autoresize]').forEach(function(element) {
element.style.boxSizing = 'border-box';
element.style.resize = 'none';
var offset = element.offsetHeight - element.clientHeight;
element.addEventListener('input', function(event) {
event.target.style.height = 'auto';
event.target.style.height = event.target.scrollHeight + offset + 'px';
console.log('Textarea height:');
console.log(event.target.scrollHeight, offset);
});
element.addEventListener('click', function(event) {
event.target.style.height = 'auto';
event.target.style.height = event.target.scrollHeight + offset + 'px';
console.log('Textarea height:');
console.log(event.target.scrollHeight, offset);
});
element.removeAttribute('data-autoresize');
});
}
function CustomModal(modal) {
this.modal = modal;
this.open = function(contentHtml) {
$('body').addClass('modal-open');
if (typeof contentHtml != 'undefined') {
$(this.modal).find('p.body').html(contentHtml);
}
$(this.modal).css('display', 'block');
}
this.close = function() {
$(this.modal).css('display', 'none');
$('body').removeClass('modal-open');
}
this.disableActionBtns = function(is_disabled) {
$(this.modal).find('.button').attr('disabled', is_disabled);
$(this.modal).find('input[type="button"]').attr('disabled', is_disabled);
}
var modalRef = $(this.modal);
var actionBtns = this.disableActionBtns;
this.confirm = new Promise(function(resolve) {
let sel1 = $(modalRef).find('.button');
let sel2 = $(modalRef).find('span.close');
$([sel1, sel2]).each(function() {
$(this).on('click', function() {
actionBtns(true);
var yesCls = 'yes-btn';
var noCls = 'no-btn';
var resp = false;
console.log('Button clicked:', $(this));
if ($(this).hasClass(yesCls)) {
resp = true;
}
resolve(resp);
});
});
}
);
this.confirm3 = new Promise(function(resolve) {
$(modalRef).find('.button').on('click', function() {
actionBtns(true);
var val = $(this).attr('data-val');
switch (val) {
case 'yes':
resolve(val);
break;
case 'no':
resolve(val);
break;
case 'cancel':
default:
resolve(false);
break;
}
});
}
);
this.orderActions = new Promise(function(resolve) {
$(modalRef).find('.button').on('click', function() {
actionBtns(true);
var val = $(this).attr('data-val');
switch (val) {
case 'complete':
resolve(val);
break;
case 'reopen':
resolve(val);
break;
case 'print':
resolve(val);
break;
default:
resolve(false);
break;
}
});
}
);
}
$('.modal-content .close').on('click', function() {
$(".modal").css('display', 'none');
$('body').removeClass('modal-open');
});
$(window).on('click', function(event) {
if (event.target.className == 'modal' || event.target.className == 'close') {
$(".modal").css('display', 'none');
$('body').removeClass('modal-open');
}
});
$(document).keyup(function(e) {
if (e.keyCode === 27) {
$(".modal").css('display', 'none');
$('body').removeClass('modal-open');
}
});
function increaseValue(max_qty) {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
if (value < max_qty) {
value++;
}
document.getElementById('number').value = value;
}
function decreaseValue(max_qty) {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value <= 1 ? value = 1 : '';
if (value > 1)
value--;
document.getElementById('number').value = value;
}
function agreementPopup(target, agreementText) {
var itemForm = $(target).closest('.item-form');
console.log('Item form', itemForm);
var customModal = new CustomModal($('#prodTermsModal'));
customModal.open(agreementText);
customModal.confirm.then(function(resolve) {
console.log('Agreement value', resolve);
customModal.close();
if (resolve) {
console.log('Submit form');
$(itemForm).submit();
} else {
return false;
}
}, function(reject) {
console.log('Confirm reject:', reject);
});
return false;
}