UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

138 lines (116 loc) 4.59 kB
(function (module) { 'use strict'; module .controller('AdminProductListController', ProductListController); ProductListController.$inject = ['$scope', '$rootScope', '$state', '$http', 'AdminAuth', 'ProductTemplate', 'Store', 'Category', 'toastr', 'lodash']; function ProductListController($scope, $rootScope, $state, $http, Auth, ProductTemplate, Store, Category, toastr, lodash) { $scope.stores = Store.find(); $scope.categories = Category.find(); $scope.products = []; $scope.sortBy = { fields: [ {label: 'Nombre', name: 'name'}, {label: 'Precio', name: 'price'} ], order: [ {label: 'Ascendente', name: 'ASC'}, {label: 'Descendente', name: 'DESC'} ] }; $scope.sortBy.selected = { field: $scope.sortBy.fields[0].name, order: $scope.sortBy.order[0].name }; $scope.filterName = ''; $scope.filterPrice = { min: 0, max: 10000, step: 100, result: [0, 50000], 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]; }); var normalizedFilters = 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 } } return { filter: { where: where, order: sortBy ? sortBy.field + ' ' + sortBy.order : null, include: ['store', 'categories'] } }; }; $scope.refresh = function () { var isCategories = $scope.sortBy.selected.field === 'categories'; $scope.products = ProductTemplate.find(normalizedFilters()); $scope.products .$promise .then(function (products) { if (isCategories) { var strCategories = lodash.map(products, function (product) { return {product: product, str: product.categories.join('')} }); $scope.products = lodash.map(lodash.orderBy(strCategories, 'str', $scope.sortBy.selected.order.toLowerCase()), 'product'); } }); }; $scope.selectFieldOrder = function (field) { $scope.sortBy.selected.field = field; }; $scope.selectOrder = function (order) { $scope.sortBy.selected.order = order; }; $scope .$watchGroup([ 'filterName', 'filterPrice.result', 'sortBy.selected.field', 'sortBy.selected.order' ], $scope.refresh); $scope.$watchCollection('storeFilter', $scope.refresh); $scope.$watchCollection('categoriesFilter', $scope.refresh); $scope.exportData = function () { $http({ url: "http://uniko.co:3000/api/v2/producttemplates/listasxlsx?access_token=" + Auth.getAuthData().accessTokenId, method: 'post', responseType: "blob", data: normalizedFilters() }).then(function (data) { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; var blob = data.data, url = window.URL.createObjectURL(blob); a.href = url; a.download = 'productlist.xlsx'; a.click(); window.URL.revokeObjectURL(url); $(a).remove(); }); }; } })(angular.module('uniko.admin.products'));