UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

169 lines (146 loc) 6.55 kB
(function (module) { 'use strict'; module.controller('AdminProductShowController', ProductShowController); ProductShowController.$inject = ['$scope', '$timeout', '$state', 'ProductTemplate', 'Store', 'Category', '$stateParams', '$uibModal', 'toastr', 'Upload', '$translate', 'lodash']; function ProductShowController($scope, $timeout, $state, ProductTemplate, Store, Category, $stateParams, $uibModal, toastr, Upload, $translate, lodash) { $scope.product = ProductTemplate .findOne({filter: {where: {id: $stateParams.productId}}}); var originalProduct; $scope.productStore = null; $scope.stores = Store.find(); $scope.categories = Category.find(); $scope.productCategories = []; $scope.slickConfig = { enabled: false }; $timeout(function(){ $scope.slickConfig.enabled = true; },500); $scope.product.$promise .then(function () { originalProduct = $scope.product.toJSON(); originalProduct.imagesList = lodash.clone(originalProduct.imagesList); return $scope.stores.$promise; }) .then(function (b) { $scope.$watch('product.storeId', function (newId) { $scope.productStore = lodash.find($scope.stores, {id: newId}); }); return $scope.categories.$promise; }) .then(function (a) { $scope.$watchCollection('product.categoriesIds', function (val) { console.log(val); $scope.productCategories = lodash.map(lodash.filter($scope.categories, function (category) { return $scope.product.categoriesIds.indexOf(category.id) > -1; }), 'name'); }); }); $scope.addStore = function () { var modalInstance = $uibModal.open({ animation: true, templateUrl: 'partials/admin.stores.create.html', controller: 'AdminStoreCreateController' }); modalInstance.result.then(function (newStore) { $scope.stores.push(newStore); $scope.product.storeId = newStore.id; }); }; $scope.uploadImage = function ($file) { if ($file) { $scope.profilePhoto = $file; var imagesList = $scope.product.imagesList; imagesList.unshift(null); $scope.newPhoto = true; $timeout(function () { $scope.newPhoto = false; }, 0); Upload .upload({ url: 'http://uniko.co:3000/api/v2/producttemplates/' + $scope.product.id + '/addImage', data: { image: $file } }) .then(function (response) { $scope.newPhoto = true; $timeout(function () { imagesList.splice(0, 1, response.data.url); originalProduct.imagesList = lodash.clone(imagesList); if (!$scope.product.image) { $scope.product.image = response.data.url; } $scope.newPhoto = false; }, 0); }); } }; $scope.removeImage = function (url, $event) { $event.stopImmediatePropagation(); $event.stopPropagation(); $event.preventDefault(); var modalInstance = $uibModal.open({ animation: true, template: '<div class="modal-body">' + '<p>¿Esta seguro de querer eliminar esta Imagen?</p>' + '</div>' + '<div class="modal-footer">' + '<button class="btn btn-primary" type="button" ng-click="ok()">Si, Estoy seguro</button>' + '<button class="btn btn-warning" type="button" ng-click="cancel()">No</button>' + '</div>', controller: ['$scope', '$uibModalInstance', function ($scope, $uibModalInstance) { $scope.ok = function () { $uibModalInstance.close(); }; $scope.cancel = function () { $uibModalInstance.dismiss(); }; }] }); modalInstance.result.then(function () { $scope.product.$resolved = false; $timeout(function () { var removed = lodash.pullAt($scope.product.imagesList, $scope.product.imagesList.indexOf(url)); if ($scope.product.image === removed[0]) { $scope.product.image = $scope.product.imagesList[0]; } $scope.product.$resolved = true; }, 0); }); }; $scope.setImageAsDefault = function (url) { $scope.product.image = url; }; $scope.update = function () { $scope.product.$save(function () { originalProduct = $scope.product.toJSON(); $translate('success-save-data').then(function(translation){ toastr.success(translation); }); }, function (err) { }); }; $scope.reset = function () { $scope.product.$resolved = false; $timeout(function () { lodash.assign($scope.product, originalProduct); originalProduct.imagesList = lodash.clone(originalProduct.imagesList); $scope.product.$resolved = true; }, 0); }; $scope.delete = function () { $scope.product.$remove(function () { $scope.product = null; $state.go('admin.products'); $translate('success-delete-article').then(function(translation){ toastr.success(translation); }); }, function (err) { if (err.status === 422) { toastr.error(err.data.error.message); } }); } } })(angular.module('uniko.admin.products'));