UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

323 lines (264 loc) 11.2 kB
(function () { 'use strict'; angular.module('uniko.products') .controller('ProductListController', ProductListController) .controller('PublicProductListController', PublicProductListController); ProductListController.$inject = ['$scope', '$rootScope', '$state', '$timeout', 'ProductTemplate', 'Store', 'Category', 'CoupleAccount', 'Auth', 'toastr', '$uibModal', '$analytics', '$translate', 'lodash']; function ProductListController($scope, $rootScope, $state, $timeout, ProductTemplate, Store, Category, CoupleAccount, Auth, toastr, $uibModal, $analytics, $translate, lodash) { $scope.stores = Store.find(); $scope.categories = Category.find({filter: {where: {'visible': {'neq': false}}}}); $scope.products = []; var currentCouple; $scope.disabled = true; $scope.sortBy = { fields: [ {label: 'Nombres de la A a la Z', name: 'name', order: 'ASC'}, {label: 'Nombres de la Z a la A', name: 'name', order: 'DESC'}, {label: 'Precio menor a mayor', name: 'price', order: 'ASC'}, {label: 'Precio mayor a menor', name: 'price', order: 'DESC'} ] }; $scope.sortBy.selected = $scope.sortBy.fields[0]; $scope.filterName = ''; $scope.filterPrice = { min: 0, max: 10000, step: 100, result: [0, 10000], value: [0, 10000] }; $scope.storeFilter = []; $scope.categoriesFilter = []; ProductTemplate .highestPrice().$promise .then(function (res) { $scope.filterPrice.max = (res.price || 0) + 10000; $scope.filterPrice.value = [0, $scope.filterPrice.max]; }); $scope.refresh = function () { var price = $scope.filterPrice.result; var where = { name: {regexp: $scope.filterName + '/gi'}, price: { between: price }, hidden : { neq : true } }; var sortBy = $scope.sortBy.selected; if ($scope.categoriesFilter.length > 0) { where.categoriesIds = { inq: $scope.categoriesFilter } } if ($scope.storeFilter.length > 0) { where.storeId = { inq: $scope.storeFilter } } $scope.products = ProductTemplate .find({ filter: { where: where, order: sortBy.name + ' ' + sortBy.order, include: 'store' } }); $scope.products .$promise .then(function (products) { CoupleAccount .getCurrent(function (current) { currentCouple = current; var productsOnMesa = lodash.map(current.productsRegistryList, 'id'); lodash.forEach(products, function (product) { product.check = productsOnMesa.indexOf(product.id) > -1; }); $scope.disabled = false; }); }); }; $scope.selectFieldOrder = function (field) { $scope.sortBy.selected = field; }; $scope.addToRegistry = function (product) { return $state.go('products.edit', {product: product}); }; $scope.fastAddToRegistry = function (product) { $scope.disabled = true; var categories = lodash.map(product.categoriesIds, function (categoryId) { var category = lodash.find($scope.categories, {id: categoryId}); return category ? category.name : ''; }).join(', '); $analytics.eventTrack('Item Added to registry', { 'Name': product.name, 'Category': categories, 'Brand': product.store ? product.store.name : '', 'Price': product.price, 'Edited': false //there always will be false. }); CoupleAccount .prototype$__create__productsRegistry({id: currentCouple.id}, product, function (res) { $translate('success-add-article').then(function (translation) { toastr.success(translation); }); $scope.disabled = false; product.check = true; addToRegistryCheck(product.id); if (!currentCouple.hasFirstProduct) { CoupleAccount .update({where: {id: currentCouple.id}}, {hasFirstProduct: true}) .$promise .then(function () { }); $state.go('registry'); } }, function (err) { if (err.status === 422) { product = lodash.find(currentCouple.productsRegistryList, {id: product.id}); if(product){ product.qty += 1; currentCouple .$save() .then(function (res) { console.log(res); addToRegistryCheck(product.id); $translate('success-add-more-articles').then(function (translation) { toastr.success(translation); }); }); } } }); $scope.checkVisible = product.id; }; $scope .$watchGroup([ 'filterName', 'filterPrice.result', 'sortBy.selected.field', 'sortBy.selected.order' ], $scope.refresh); $scope.$watchCollection('storeFilter', $scope.refresh); $scope.$watchCollection('categoriesFilter', $scope.refresh); $scope.create = function () { $state.go('products.new'); }; $scope.animateLabel = function (index, hide) { jQuery('#add-' + index).animate({ left: hide ? '100%' : '0' }, 300); }; $scope.animateOverlay = function (index, hide) { if (hide) jQuery('#overlay-' + index + ' h4').hide(); jQuery('#overlay-' + index).animate({ height: hide ? '0' : '100%' }, 300, function () { jQuery('#overlay-' + index) if (!hide) jQuery('#overlay-' + index + ' h4').show(); }); }; function addToRegistryCheck(productId) { $scope.checkVisible = productId; $timeout(function () { $scope.checkVisible = null; }, 2000); } } PublicProductListController.$inject = ['$scope', '$rootScope', '$state', '$timeout', 'ProductTemplate', 'Store', 'Category', 'toastr', '$uibModal', '$analytics', '$translate']; function PublicProductListController($scope, $rootScope, $state, $timeout, ProductTemplate, Store, Category, toastr, $uibModal, $analytics, $translate) { $scope.onlyRead = true; $scope.stores = Store.find(); $scope.categories = Category.find({filter: {where: {'visible': {'neq': false}}}}); $scope.products = []; $scope.disabled = true; $scope.sortBy = { fields: [ {label: 'Nombres de la A a la Z', name: 'name', order: 'ASC'}, {label: 'Nombres de la Z a la A', name: 'name', order: 'DESC'}, {label: 'Precio menor a mayor', name: 'price', order: 'ASC'}, {label: 'Precio mayor a menor', name: 'price', order: 'DESC'} ] }; $scope.sortBy.selected = $scope.sortBy.fields[0]; $scope.filterName = ''; $scope.filterPrice = { min: 0, max: 10000, step: 100, result: [0, 10000], value: [0, 10000] }; $scope.storeFilter = []; $scope.categoriesFilter = []; ProductTemplate .highestPrice().$promise .then(function (res) { $scope.filterPrice.max = (res.price || 0) + 10000; $scope.filterPrice.value = [0, $scope.filterPrice.max]; }); $scope.refresh = function () { var price = $scope.filterPrice.result; var where = { name: {regexp: $scope.filterName + '/gi'}, price: { between: price } }; var sortBy = $scope.sortBy.selected; if ($scope.categoriesFilter.length > 0) { where.categoriesIds = { inq: $scope.categoriesFilter } } if ($scope.storeFilter.length > 0) { where.storeId = { inq: $scope.storeFilter } } $scope.products = ProductTemplate .find({ filter: { where: where, order: sortBy.name + ' ' + sortBy.order, include: 'store' } }); }; $scope.selectFieldOrder = function (field) { $scope.sortBy.selected = field; }; $scope.addToRegistry = function (product) { return $state.go('products_public.edit', {product: product}); }; $scope.fastAddToRegistry = function (product) { $rootScope.loginModal(); }; $scope .$watchGroup([ 'filterName', 'filterPrice.result', 'sortBy.selected.field', 'sortBy.selected.order' ], $scope.refresh); $scope.$watchCollection('storeFilter', $scope.refresh); $scope.$watchCollection('categoriesFilter', $scope.refresh); $scope.create = function () { }; $scope.animateLabel = function (index, hide) { jQuery('#add-' + index).animate({ left: hide ? '100%' : '0' }, 300); }; $scope.animateOverlay = function (index, hide) { if (hide) jQuery('#overlay-' + index + ' h4').hide(); jQuery('#overlay-' + index).animate({ height: hide ? '0' : '100%' }, 300, function () { jQuery('#overlay-' + index) if (!hide) jQuery('#overlay-' + index + ' h4').show(); }); }; } })();