{"version":3,"file":"js/chunks/bonus-products-view.js","sources":["webpack:///./org_colony/cartridge/js/bonus-products-view.js"],"sourcesContent":["const dialog = require('./dialog');\nconst page = require('./page');\nconst util = require('./util');\n\nconst selectedList = [];\nlet maxItems = 1;\nlet bliUUID = '';\n\n/**\n * @private\n * @function\n * description Gets a list of bonus products related to a promoted product\n */\nfunction getBonusProducts() {\n const bonusproducts = [];\n for (let i = 0, len = selectedList.length; i < len; i += 1) {\n const p = {\n pid: selectedList[i].pid,\n qty: selectedList[i].qty,\n options: {},\n };\n const bp = selectedList[i];\n if (bp.options) {\n for (let a = 0, alen = bp.options.length; a < alen; a += 1) {\n const opt = bp.options[a];\n p.options = {optionName: opt.name, optionValue: opt.value};\n }\n }\n bonusproducts.push({product: p});\n }\n return {bonusproducts};\n}\n\nfunction selectedItemTemplate(data) {\n let attributes = '';\n Object.keys(data.attributes).forEach((attrID) => {\n const attr = data.attributes[attrID];\n attributes += `<li data-attribute-id=\"${attrID}\">\\n`;\n attributes += `<span class=\"display-name\">${attr.displayName}</span>: `;\n attributes += `<span class=\"display-value\">${attr.displayValue}</span>\\n`;\n attributes += '</li>';\n });\n attributes += '<li class=\"item-qty\">\\n';\n attributes += `<span class=\"display-name\">${Resources.QTY}</span>: `;\n attributes += `<span class=\"display-value\">${data.qty}</span>`;\n return [\n `<li class=\"selected-bonus-item\" data-uuid=\"${data.uuid}\" data-pid=\"${data.pid}\">`,\n `<a class=\"remove-link\" title=\"${Resources.REMOVE}\" href=\"#\">${util.svg('remove-icon')}</a>`,\n `<div class=\"item-name\">${data.name}</div>`,\n '<ul class=\"item-attributes\">',\n attributes,\n '<ul>',\n '<li>',\n ].join('\\n');\n}\n\n// hide swatches that are not selected or not part of a Product Variation Group\nfunction hideSwatches() {\n $('.bonus-product-item:not([data-producttype=\"master\"]) .swatches li').not('.selected').not('.variation-group-value').hide();\n // prevent unselecting the selected variant\n $('.bonus-product-item .swatches .selected').on('click', () => false);\n}\n\n/**\n * @private\n * @function\n * @description Updates the summary page with the selected bonus product\n */\nfunction updateSummary() {\n const $bonusProductList = $('#bonus-product-list');\n if (!selectedList.length) {\n $bonusProductList.find('li.selected-bonus-item').remove();\n $('.add-to-cart-bonus').attr('disabled', 'disabled');\n } else {\n const ulList = $bonusProductList.find('ul.selected-bonus-items').first();\n for (let i = 0, len = selectedList.length; i < len; i += 1) {\n const item = selectedList[i];\n const li = selectedItemTemplate(item);\n $(li).appendTo(ulList);\n }\n $('.add-to-cart-bonus').removeAttr('disabled');\n }\n\n // get remaining item count\n let sumItems = 0;\n $('.selected-bonus-item').each((index, element) => {\n sumItems += Number($(element).find('.item-qty .display-value').text());\n });\n const remain = maxItems - sumItems;\n if (remain <= 0) {\n $bonusProductList.find('.select-bonus-item').attr('disabled', 'disabled');\n } else {\n $bonusProductList.find('.bonus-product-form:not(.master-product-form) .select-bonus-item').removeAttr('disabled');\n $bonusProductList.find('.quantity-error').removeClass('error-msg').text('');\n }\n}\n\nlet firstLoad = true;\n\nfunction initializeGrid() {\n const $bonusProduct = $('#bonus-product-dialog');\n const $bonusProductList = $('#bonus-product-list');\n const bliData = $bonusProductList.data('line-item-detail');\n maxItems = bliData.maxItems;\n bliUUID = bliData.uuid;\n\n $bonusProductList.on('click', '.bonus-product-item a[href].swatchanchor', (e) => {\n e.preventDefault();\n let url = e.currentTarget.href;\n const $this = $(e.currentTarget);\n url = util.appendParamsToUrl(url, {\n source: 'bonus',\n format: 'ajax',\n });\n $.ajax({\n url,\n success(response) {\n $this.closest('.bonus-product-item').empty().html(response);\n hideSwatches();\n },\n });\n }).on('change', '.input-text', (e) => {\n let sumItems = 0;\n const parent = $(e.currentTarget).parents('.bonus-product-form');\n if (parent.hasClass('master-product-form') || $(e.currentTarget).val() === 0) {\n return;\n }\n\n $('.selected-bonus-item').each((index, element) => {\n sumItems += Number($(element).find('.item-qty .display-value').text());\n });\n\n if ($(e.currentTarget).val() <= maxItems - sumItems) {\n parent.find('.select-bonus-item').removeAttr('disabled');\n parent.find('.quantity-error').removeClass('error-msg').text('');\n } else {\n parent.find('.select-bonus-item').attr('disabled', 'disabled');\n parent.find('.quantity-error').addClass('error-msg').text(Resources.BONUS_PRODUCT_TOOMANY);\n }\n }).on('click', '.select-bonus-item', (e) => {\n e.preventDefault();\n let sumItems = 0;\n $('.selected-bonus-item').each((index, element) => {\n sumItems += Number($(element).find('.item-qty .display-value').text());\n });\n\n if (selectedList.length >= maxItems) {\n $bonusProductList.find('.select-bonus-item').attr('disabled', 'disabled');\n return;\n }\n\n const form = $(e.currentTarget).closest('.bonus-product-form');\n const detail = $(e.currentTarget).closest('.product-detail');\n const uuid = form.find('input[name=\"productUUID\"]').val();\n const qtyVal = form.find('input[name=\"Quantity\"], select[name=\"Quantity\"]').val();\n const qty = Number.isNaN(qtyVal) ? 1 : (+qtyVal);\n if (qty > maxItems - sumItems) {\n $(e.currentTarget).attr('disabled', 'disabled');\n form.find('.quantity-error').addClass('error-msg').text(Resources.BONUS_PRODUCT_TOOMANY);\n return;\n }\n\n const product = {\n uuid,\n pid: form.find('input[name=\"pid\"]').val(),\n qty,\n name: detail.find('.product-name').text(),\n attributes: detail.find('.product-variations').data('attributes'),\n options: [],\n };\n\n const optionSelects = form.find('.product-option');\n\n optionSelects.each((index, element) => {\n product.options.push({\n name: element.name,\n value: $(element).val(),\n display: $(element).children(':selected').first().html(),\n });\n });\n selectedList.push(product);\n $('#bonus-product-list li').remove('.selected-bonus-item');\n updateSummary();\n }).on('click', '.remove-link', (e) => {\n e.preventDefault();\n const container = $(e.currentTarget).closest('.selected-bonus-item');\n if (!container.data('uuid')) { return; }\n\n const uuid = container.data('uuid');\n const len = selectedList.length;\n for (let i = 0; i < len; i += 1) {\n if (selectedList[i].uuid === uuid) {\n selectedList.splice(i, 1);\n break;\n }\n }\n $('#bonus-product-list li').remove('.selected-bonus-item');\n updateSummary();\n }).on('click', '.add-to-cart-bonus', (e) => {\n e.preventDefault();\n const url = util.appendParamsToUrl(Urls.addBonusProduct, {bonusDiscountLineItemUUID: bliUUID});\n const bonusProducts = getBonusProducts();\n if (bonusProducts.bonusproducts[0].product.qty > maxItems) {\n bonusProducts.bonusproducts[0].product.qty = maxItems;\n }\n // make the server call\n $.ajax({\n type: 'POST',\n dataType: 'json',\n cache: false,\n contentType: 'application/json',\n url,\n data: JSON.stringify(bonusProducts),\n }).done(() => {\n // success\n if (window.pageContext.ns === 'cart') {\n page.refresh();\n } else {\n page.redirect(Urls.cartShow);\n }\n }).fail((xhr, textStatus) => {\n // failed\n if (textStatus === 'parsererror') {\n window.alert(Resources.BAD_RESPONSE);\n } else {\n window.alert(Resources.SERVER_CONNECTION_ERROR);\n }\n }).always(() => {\n $bonusProduct.dialog('close');\n });\n }).on('click', '#more-bonus-products', (e) => {\n e.preventDefault();\n const {uuid} = $('#bonus-product-list').data().lineItemDetail;\n\n // get the next page of choice of bonus products\n const lineItemDetail = JSON.parse($('#bonus-product-list').attr('data-line-item-detail'));\n lineItemDetail.pageStart += lineItemDetail.pageSize;\n $('#bonus-product-list').attr('data-line-item-detail', JSON.stringify(lineItemDetail));\n\n const url = util.appendParamsToUrl(Urls.getBonusProducts, {\n bonusDiscountLineItemUUID: uuid,\n format: 'ajax',\n lazyLoad: 'true',\n pageStart: lineItemDetail.pageStart,\n pageSize: $('#bonus-product-list').data().lineItemDetail.pageSize,\n bonusProductsTotal: $('#bonus-product-list').data().lineItemDetail.bpTotal,\n });\n\n $.ajax({\n type: 'GET',\n cache: false,\n contentType: 'application/json',\n url,\n }).done((data) => {\n // add the new page to DOM and remove 'More' link if it is the last page of results\n $('#more-bonus-products').before(data);\n if ((lineItemDetail.pageStart + lineItemDetail.pageSize) >= $('#bonus-product-list').data().lineItemDetail.bpTotal) {\n $('#more-bonus-products').remove();\n }\n }).fail((xhr, textStatus) => {\n if (textStatus === 'parsererror') {\n window.alert(Resources.BAD_RESPONSE);\n } else {\n window.alert(Resources.SERVER_CONNECTION_ERROR);\n }\n });\n });\n if (firstLoad) {\n const cartItems = $bonusProductList.find('.selected-bonus-item');\n cartItems.each((index, element) => {\n const selectedItem = `#item-${$(element).data('uuid')}`;\n const selectedItemQty = $(element).find('.selected-qty').text();\n $(element).remove();\n $(selectedItem).find('.bonus-product-form .input-text').val(selectedItemQty);\n $(selectedItem).find('.bonus-product-form .select-bonus-item').trigger('click');\n });\n firstLoad = false;\n }\n let sumItems = 0;\n $('.selected-bonus-item').each((index, element) => {\n sumItems += Number($(element).find('.item-qty .display-value').text());\n });\n\n if (sumItems >= maxItems) {\n $bonusProductList.find('.select-bonus-item').attr('disabled', 'disabled');\n }\n}\n\nconst bonusProductsView = {\n /**\n * @function\n * @description Open the list of bonus products selection dialog\n */\n show(url) {\n const $bonusProduct = $('#bonus-product-dialog');\n // create the dialog\n dialog.open({\n target: $bonusProduct,\n url,\n options: {\n width: 950,\n title: Resources.BONUS_PRODUCTS,\n dialogClass: 'bonus-dialog',\n },\n callback() {\n initializeGrid();\n hideSwatches();\n },\n });\n },\n};\n\nmodule.exports = bonusProductsView;\n"],"mappings":";;;;;;;;;AAAA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAAA;AAAA;AACA;AACA;AACA;AAAA;AAAA;AACA;AACA;AAAA;AAAA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AASA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;;;;A","sourceRoot":""}