cobuild-angular-stack
Version:
Base stack angular sass jade gulp
135 lines (118 loc) • 5.3 kB
JavaScript
(function () {
'use strict';
angular.module('uniko.products')
.controller('ProductRegistryShowController', 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.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/stores.create.html',
controller: 'StoreCreateController'
});
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(0, 0, 'img/wait.png');
$scope.product.$resolved = false;
$timeout(function () {
$scope.product.$resolved = true;
}, 0);
Upload
.upload({
url: 'http://uniko.co:3000/api/v2/producttemplates/' + $scope.product.id + '/addImage',
data: {
image: $file
}
})
.then(function (response) {
$scope.product.$resolved = false;
$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.product.$resolved = true;
}, 0);
});
}
};
$scope.removeImage = function (url) {
$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('products');
$translate('success-delete-article').then(function(translation){
toastr.success(translation);
});
}, function () {
$translate('error-delete-article').then(function(translation){
toastr.success(translation);
});
});
}
}
})();