UNPKG

cobuild-angular-stack

Version:

Base stack angular sass jade gulp

108 lines (97 loc) 4.48 kB
(function () { 'use strict'; angular.module('uniko.registry') .controller('ProductRegistryNewController', ProductNewController); ProductNewController.$inject = ['$scope', '$timeout', '$state', 'product', 'Store', 'Category', '$stateParams', '$uibModalInstance', 'toastr', 'Upload', 'CoupleAccount', '$translate', 'lodash']; function ProductNewController($scope, $timeout, $state, product, Store, Category, $stateParams, $uibModalInstance, toastr, Upload, CoupleAccount, $translate, lodash) { $scope.product = lodash.cloneDeep(product); $scope.productCategories = []; $scope.disabled = false; $scope.productStore = null; $scope.stores = Store.find(); $scope.stores .$promise .then(function () { $scope.$watch('product.storeId', function (newId) { $scope.productStore = lodash.find($scope.stores, {id: newId}); }); }); $scope.categories = Category.find(); $scope.categories.$promise .then(function () { $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.uploadImage = function ($file) { if ($file) { $scope.disabled = true; $scope.profilePhoto = $file; var imagesList = $scope.product.imagesList; imagesList.unshift('img/wait.png'); $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); $scope.product.imagesList = lodash.clone(imagesList); if (!$scope.product.image) { $scope.product.image = response.data.url; } $scope.newPhoto = false; $scope.disabled = false; }, 0); }); } }; $scope.removeImage = function (url) { $scope.product.$resolved = false; $scope.disabled = true; $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; $scope.disabled = false; }, 0); }; $scope.setImageAsDefault = function (url) { $scope.product.image = url; }; $scope.create = function () { $scope.disabled = true; var product = $scope.product; CoupleAccount .getCurrent(function (currentCouple) { CoupleAccount .prototype$__create__productsRegistry({id: currentCouple.id}, product, function (res) { var originalProduct = $scope.product.toJSON(); $translate('success-add-article').then(function(translation){ toastr.success(translation); }); $scope.disabled = false; $uibModalInstance.close(); }, console.log.bind(console)); }); delete product.id; delete product.imagesList; }; $scope.reset = function () { $uibModalInstance.dismiss(); }; } })();