cobuild-angular-stack
Version:
Base stack angular sass jade gulp
151 lines (130 loc) • 5.77 kB
JavaScript
(function (module) {
'use strict';
module.controller('AdminProductNewController', ProductNewController);
ProductNewController.$inject = ['$scope', '$timeout', '$state', 'ProductTemplate', 'Store', '$stateParams', '$uibModal', 'toastr', 'Upload', 'Category', '$translate', 'lodash'];
function ProductNewController($scope, $timeout, $state, ProductTemplate, Store, $stateParams, $uibModal, toastr, Upload, Category, $translate, lodash) {
$scope.product = {
isActive: true,
imagesList: [],
$resolved: true
};
$scope.slickConfig = {
enabled: false
};
$timeout(function(){
$scope.slickConfig.enabled = true;
},500);
$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.productCategories = [];
$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.product.$resolved = false;
$scope.newPhoto = true;
$timeout(function () {
$scope.product.$resolved = true;
$scope.newPhoto = false;
}, 0);
Upload
.upload({
url: 'http://uniko.co:3000/api/v2/producttemplates/addImage',
data: {
image: $file
}
})
.then(function (response) {
$scope.product.$resolved = false;
$scope.newPhoto = true;
$timeout(function () {
imagesList.splice(0, 1, response.data.url);
if (!$scope.product.image) {
$scope.product.image = response.data.url;
}
$scope.product.$resolved = true;
$scope.newPhoto = false;
}, 0);
});
}
};
$scope.removeImage = function (url) {
$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.create = function () {
delete $scope.product.$resolved;
ProductTemplate.create($scope.product, function (product) {
$state.go("admin.products.show", {productId: product.id, name: $scope.slug(product.name)});
$translate('success-save-data').then(function(translation){
toastr.success(translation);
});
}, function (response) {
if (response.status === 422) {
toastr.error(response.data.error.message);
}
});
};
$scope.reset = function () {
$state.go('admin.products');
};
}
})(angular.module('uniko.admin.products'));