{"version":3,"file":"jquery.shopping_list_updater.min.js","sources":["jquery.shopping_list_updater.js"],"sourcesContent":["(function ($, _, w) {\n 'use strict';\n\n let $addToCollectionDialog = null;\n const template = '
';\n let systemUrls, labels;\n let isContextLoaded = false;\n let isAnonymous;\n\n w.showAddToCollectionPopup = function (products) {\n if (typeof (products) === 'undefined') {\n w.PubSub.publish('notification.warning', labels.noProductsFound);\n } else {\n if (!isContextLoaded) {\n $.when(getContext())\n .done(function (ctx) {\n systemUrls = ctx.urls;\n labels = ctx.labels;\n isContextLoaded = true;\n isAnonymous = ctx.isAnonymous;\n\n let $shoppingListSelector;\n let $newShoppingListInput;\n\n if (isAnonymous === true) {\n showLoginRequiredWarning();\n } else {\n const dialogHtml = _.template(template, ctx);\n $addToCollectionDialog = $(dialogHtml).dialog({\n resizable: false,\n modal: true,\n autoOpen: false,\n width: 400,\n buttons: [\n {\n 'class': 'btn btn-default',\n text: labels.close,\n click: function () {\n $(this).dialog('close');\n }\n }\n ]\n });\n\n $shoppingListSelector = $addToCollectionDialog.find('.js-existing-shoppinglist-selector');\n $shoppingListSelector.keyup(function (e) {\n if (e.keyCode === 13) {\n _handleAddProductToExistingShoppingList();\n }\n });\n $newShoppingListInput = $addToCollectionDialog.find('.js-new-shoppinglist-name');\n $newShoppingListInput.keyup(function (e) {\n if (e.keyCode === 13) {\n _handleAddProductToNewShoppingList();\n }\n });\n\n const $addButton = $addToCollectionDialog.find(\"[data-action='addtoexistingshoppinglist']\");\n if (ctx.shoppingLists.length <= 0) {\n $addButton.prop('disabled', true);\n $addButton.prop('title', labels.emptyShoppingLists);\n\n $shoppingListSelector.prop('disabled', true);\n }\n\n $addToCollectionDialog.find('.js-action-button').click(function (evt) {\n evt.preventDefault();\n\n const $clickTarget = $(evt.target);\n const action = $clickTarget.data('action');\n\n switch (action) {\n case 'addtoexistingshoppinglist':\n _handleAddProductToExistingShoppingList();\n break;\n case 'addtonewshoppinglist':\n _handleAddProductToNewShoppingList();\n break;\n }\n });\n\n $addToCollectionDialog.data('products', products);\n $addToCollectionDialog.dialog('open');\n }\n\n function _handleAddProductToExistingShoppingList() {\n const productsToAdd = $addToCollectionDialog.data('products');\n const selectedShoppingListId = $shoppingListSelector.val();\n\n blockUI();\n $.when(w.PublicEStore.addToShoppingList(selectedShoppingListId, productsToAdd))\n .always(unblockUI)\n .done(function () {\n w.PubSub.publish('notification.success', labels.productAddedToShoppingList);\n $addToCollectionDialog.dialog('close');\n })\n .fail(errorHandler);\n }\n\n function _handleAddProductToNewShoppingList() {\n const productsToAdd = $addToCollectionDialog.data('products');\n\n const newShoppingListName = $newShoppingListInput.val();\n if (newShoppingListName) {\n blockUI();\n $.when(w.PublicEStore.addShoppingList(newShoppingListName, '', productsToAdd))\n .always(unblockUI)\n .done(function (dataContract) {\n const newShoppingList = dataContract.result;\n // Add to selector and select\n const $newShoppingListOption = $('',\n { 'value': newShoppingList.shoppingListID, 'text': newShoppingList.name });\n $shoppingListSelector.append($newShoppingListOption);\n $shoppingListSelector.val(newShoppingList.shoppingListID).change();\n\n // Cleanup\n $newShoppingListInput.val('');\n\n // Notify\n w.PubSub.publish('notification.success', labels.productAddedToShoppingList);\n $addToCollectionDialog.dialog('close');\n })\n .fail(errorHandler);\n } else {\n w.PubSub.publish('notification.warning', labels.shoppingListNameIsEmpty);\n }\n }\n })\n .fail(errorHandler);\n } else if (isAnonymous === true) {\n showLoginRequiredWarning();\n } else if ($addToCollectionDialog) {\n $addToCollectionDialog.data('products', products);\n $addToCollectionDialog.dialog('open');\n }\n }\n\n function getContext() {\n return $.get(w.R + 'handlers/public/estoredata.ashx?a=GetAddToShoppingListContext');\n }\n\n function blockUI() {\n if (typeof ($.blockUI) === 'function') {\n $addToCollectionDialog.block({ message: null });\n }\n }\n\n function unblockUI() {\n if (typeof ($.unblockUI) === 'function') {\n $addToCollectionDialog.unblock();\n }\n }\n\n function errorHandler(error) {\n let message;\n\n if (typeof (error) === 'string') {\n message = error;\n } else {\n message = typeof (error) !== 'undefined' ? (error.message ? error.message : error.statusText) : 'Unknown error';\n }\n\n if (typeof (error.status) !== 'undefined' && error.status.toLowerCase() === 'unauthorized') {\n showLoginRequiredWarning();\n } else {\n w.PubSub.publish('notification.error', message);\n }\n }\n\n function showLoginRequiredWarning() {\n const loginUrl = typeof (systemUrls) !== 'undefined' ? $.setQueryStringParam(systemUrls.loginUrl, 'ret', encodeURIComponent(location.pathname)) : '#';\n w.PubSub.publish('notification.warning',\n {\n text: labels.availableToRegisteredOnly + '