angular.module("CartDirective", ["AjaxService", "CartService", "GeneralFilters", "UserService"]).directive('cart', function (Ajax, Cart, $mdDialog, User, $state, $timeout) { return { templateUrl: "?component=" + "Directives/Cart" + "&html", restrict: "E", scope: { restaurant: "=", readonly: "=" }, link: function ($scope) { $scope.load = function () { $scope.cart = {}; $scope.total = {}; $scope.variations = []; $scope.mapIngredients = {}; $scope.ingredients = []; $scope.gifts = []; $scope.user = null; $scope.variationsLoaded = false; $scope.ingredientsLoaded = false; $scope.giftsLoaded = false; $scope.loaded_user = false; $scope.loaded_cart = false; if ($scope.readonly !== true) { Ajax.get('Restaurant', 'getIngredienti', [$scope.restaurant], function (data) { $scope.ingredients = data || []; $scope.ingredientsLoaded = true; $scope.mapIngredients = {}; if ($scope.ingredients) { $scope.ingredients.forEach(function(value) { $scope.mapIngredients[value.pietanza_id] = true; }); } }); Ajax.get('Restaurant', 'getVariazioni', [$scope.restaurant], function (data) { $scope.variations = data; $scope.variationsLoaded = true; }); Ajax.get('Restaurant', 'getGifts', [$scope.restaurant], function (data) { $scope.gifts = data; $scope.giftsLoaded = true; }); } User.getOb().regAndCall($scope.$id, function () { $scope.getUser(); }); Cart.getOb().regAndCall($scope.$id, function () { $scope.getCart(); }); $scope.$on("$destroy", function () { User.getOb().unreg($scope.$id); Cart.getOb().unreg($scope.$id); }); }; $scope.removeVariation = function (index, variation_index) { Cart.removeVariation(index, variation_index, $scope.restaurant).then(function (response) { }); }; $scope.removeIngredient = function (index, ingredient_index) { Cart.removeIngredient(index, ingredient_index, $scope.restaurant).then(function (response) { }); }; $scope.showVariations = function (index, variations) { $mdDialog.show({ templateUrl: '?component=Dialog/Variations&html', parent: angular.element(document.body), clickOutsideToClose: false, locals: { items: $scope.variations, variations: variations }, controller: function ($scope, $mdDialog, $mdToast, items, variations) { $scope.items = angular.copy(items); $scope.variations = angular.copy(variations); $scope.count_variations = {}; $scope.init = function () { if ($scope.variations) { angular.forEach($scope.variations, function (value) { $scope.count_variations[value.item.variazione_id] = { count: value.count, type: value.type }; }); } }; $scope.init(); $scope.addVariation = function (id) { if (!$scope.count_variations[id]) { $scope.count_variations[id] = {count: 1, type: "add"}; } else if ($scope.count_variations[id].type !== "add") { delete $scope.count_variations[id]; } else { $scope.count_variations[id].count = 1; $scope.count_variations[id].type = "add"; } }; $scope.removeVariation = function (id) { if ($scope.count_variations[id] && $scope.count_variations[id].type === "add") { $scope.count_variations[id].count--; $scope.count_variations[id].type = "add"; if ($scope.count_variations[id].count <= 0) { delete $scope.count_variations[id]; } } else { $scope.count_variations[id] = {count: 0, type: "less"}; } }; $scope.noVariation = function (id) { $scope.count_variations[id] = {count: 0, type: "remove"}; }; $scope.cancel = function () { $mdDialog.cancel(); }; $scope.save = function () { $mdDialog.hide($scope.count_variations); }; } }).then(function (result) { $scope.setVariations(result, index); }, function () { }); }; $scope.setVariations = function (variations, index) { Cart.setVariations(variations, index, $scope.restaurant).then(function (response) { }); }; $scope.showIngredients = function (index, item_id, ingredients) { var filtered_items = $scope.ingredients.filter(function (ingredient) { return ingredient.pietanza_id === item_id; }); $mdDialog.show({ templateUrl: '?component=Dialog/Ingredients&html', parent: angular.element(document.body), clickOutsideToClose: false, locals: { items: filtered_items, ingredients: ingredients }, controller: function ($scope, $mdDialog, $mdToast, items, ingredients) { $scope.items = angular.copy(items); $scope.ingredients = angular.copy(ingredients); $scope.count_ingredients = {}; $scope.init = function () { if ($scope.ingredients) { angular.forEach($scope.ingredients, function (value) { $scope.count_ingredients[value.item.ingrediente_id] = true; }); } }; $scope.init(); $scope.addIngredient = function (id) { console.log(id); $scope.count_ingredients[id] = true; console.log($scope.count_ingredients); }; $scope.removeIngredient = function (id) { delete $scope.count_ingredients[id]; }; $scope.cancel = function () { $mdDialog.cancel(); }; $scope.save = function () { $mdDialog.hide($scope.count_ingredients); }; } }).then(function (result) { $scope.setIngredients(result, index); }, function () { }); }; $scope.setIngredients = function (ingredients, index) { Cart.setIngredients(ingredients, index, $scope.restaurant).then(function (response) { }); }; $scope.showGift = function (index, gift) { $mdDialog.show({ templateUrl: '?component=Dialog/Gifts&html', parent: angular.element(document.body), clickOutsideToClose: false, locals: { items: $scope.gifts, gift: gift }, controller: function ($scope, $mdDialog, $mdToast, items, gift) { $scope.items = angular.copy(items); $scope.gift = angular.copy(gift); $scope.cancel = function () { $mdDialog.cancel(); }; $scope.save = function (gift_id) { $mdDialog.hide(gift_id); }; } }).then(function (result) { $scope.setGift(result, index); }, function () { }); }; $scope.setGift = function (gift_id, index) { Cart.setGift(gift_id, index, $scope.restaurant).then(function (response) { }); }; $scope.removeGift = function (index) { Cart.removeGift(index, $scope.restaurant).then(function (response) { }); }; $scope.showNote = function () { var confirm = $mdDialog.prompt() .title('Lascia una nota per il ristorante') .placeholder('Nota') .ariaLabel('Nota') .initialValue($scope.cart.note) .targetEvent() .ok('Applica') .cancel('Chiudi'); $mdDialog.show(confirm).then(function (result) { $scope.cart.note = result; var text = $scope.cart.note || ""; text = text.substring(0, 200); Cart.setNote(text, $scope.restaurant).then(function (response) { }); }, function () { }); }; $scope.clearCart = function () { $mdDialog.show( $mdDialog.confirm() .title("Sei sicuro di voler svuotare il carrello?") .ok('Si, svuota') .cancel('No') ).then(function () { Cart.clearCart($scope.restaurant).then(function (response) { }); }, function () { }); }; $scope.removeCart = function (index) { Cart.removeCart(index, $scope.restaurant).then(function (response) { }); }; $scope.getCart = function () { var timer = $timeout(function() { $scope.loaded_cart = false; }, 500); Cart.getCart($scope.restaurant).then(function (response) { $timeout.cancel(timer); $scope.loaded_cart = true; $scope.cart = response.cart; $scope.total = response.total; }); }; $scope.hasCart = function () { return $scope.user && Cart.hasCart($scope.cart, $scope.restaurant_id); }; $scope.calcolaRimanente = function () { if ($scope.total && $scope.total.min_total && $scope.total.subtotal < $scope.total.min_total) { return $scope.total.min_total - $scope.total.subtotal; } return 0; }; $scope.calcolaRimanentePercentuale = function () { if ($scope.total && $scope.total.min_total && $scope.total.subtotal < $scope.total.min_total) { return ($scope.total.subtotal) * 100 / $scope.total.min_total; } return 100; }; $scope.calcolaRimanenteSconto = function () { if ($scope.total && $scope.total.discount_min_total && $scope.total.subtotal < $scope.total.discount_min_total) { return $scope.total.discount_min_total - $scope.total.subtotal; } return 0; }; $scope.calcolaRimanenteScontoPercentuale = function () { if ($scope.total && $scope.total.discount_min_total && $scope.total.subtotal < $scope.total.discount_min_total) { return ($scope.total.subtotal) * 100 / $scope.total.discount_min_total; } return 100; }; $scope.go = function (name) { $state.go(name); }; $scope.getUser = function() { $scope.loaded_user = false; User.get().then(function (user) { $scope.user = angular.copy(user); $scope.loaded_user = true; }); }; $scope.login = function() { $mdDialog.show({ templateUrl: '?component=Dialog/Login&html', parent: angular.element(document.body), controller: function ($scope, $mdDialog) { $scope.close = function() { $mdDialog.hide(); }; } }).then(function () { }, function () { }); }; $scope.load(); } }; });