cobuild-angular-stack
Version:
Base stack angular sass jade gulp
244 lines (207 loc) • 9.48 kB
JavaScript
(function () {
'use strict';
angular.module('uniko.products')
.controller('ProductNewController', ProductNewController)
.controller('PublicProductController', PublicProductController);
ProductNewController.$inject = ['$scope', '$rootScope', '$localStorage', '$timeout', '$state', 'Store', 'Category', '$stateParams', 'toastr', 'Upload', 'CoupleAccount', '$uibModal', '$analytics', '$translate', 'notifyFormError', 'lodash'];
function ProductNewController($scope, $rootScope, $localStorage, $timeout, $state, Store, Category, $stateParams, toastr, Upload, CoupleAccount, $uibModal, $analytics, $translate, notifyFormError, lodash) {
if (!$localStorage.product && $state.params.product) {
$localStorage.product = $state.params.product
}
$scope.validNumber = /^\d*$/;
var updateForm = $state.params.update;
$scope.updateForm = updateForm;
var product = $state.params.product || $localStorage.product || {};
product.categoriesIds = product.categoriesIds || [];
product.imagesList = product.imagesList || [];
$scope.product = lodash.cloneDeep(product);
$scope.product.$resolved = true;
$scope.newData = {productCategories: []};
$scope.disabled = false;
$scope.productStore = null;
$scope.quantities = lodash.range(20).map(function (o) {
return {label: o + 1, value: o + 1};
});
$scope.slickConfig = {
enabled: false
};
$timeout(function () {
$scope.slickConfig.enabled = true;
}, 500);
$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();
var currentCouple = $scope.coupleAccount = CoupleAccount.getCurrent();
currentCouple
.$promise
.then(function () {
$scope.url = 'http://front.uniko.co/' + currentCouple.url;
});
$scope.uploadImage = function ($file) {
if ($file) {
$scope.disabled = true;
$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/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);
$scope.product.image = response.data.url;
$scope.newPhoto = false;
$scope.disabled = 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-inverse" type="button" ng-click="ok()">Si, Estoy seguro</button>' +
'<button class="btn btn-dark" 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;
$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;
product.createByAccount = true;
delete product.$resolved;
if (updateForm) {
$timeout(function () {
product.store = lodash.find($scope.stores, {id: product.storeId});
CoupleAccount
.prototype$__updateById__productsRegistry({
id: currentCouple.id,
fk: product.id
}, product, function (res) {
$translate('success-save-data').then(function (translation) {
toastr.success(translation);
});
$scope.disabled = false;
$state.go('registry');
}, function (resp) {
if (resp.status === 422) {
notifyFormError('productForm', resp.data.error.details.codes);
}
});
}, 1000);
} else {
CoupleAccount
.prototype$__create__productsRegistry({id: currentCouple.id}, product, function (res) {
$analytics.eventTrack('Item Added to registry', {
'Name': product.name
});
$translate('success-add-article').then(function (translation) {
toastr.success(translation);
});
$scope.disabled = false;
$state.go('registry');
}, function (resp) {
if (resp.status === 422) {
notifyFormError('productForm', resp.data.error.details.codes);
}
});
}
if (!updateForm) {
product.id = '' + Date.now();
}
delete product.imagesList;
};
$scope.reset = function () {
$uibModalInstance.dismiss();
};
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
if (fromState.name == 'products.new' || fromState.name == 'products.show') {
delete $localStorage.product;
}
});
}
PublicProductController.$inject = ['$scope', '$rootScope', '$localStorage', '$timeout', '$state', 'Store', 'Category', '$stateParams', 'toastr', '$analytics', 'lodash'];
function PublicProductController($scope, $rootScope, $localStorage, $timeout, $state, Store, Category, $stateParams, toastr, $analytics, lodash) {
if ($state.params.product) {
$localStorage.product = $state.params.product
}
var product = $state.params.product || $localStorage.product || {};
product.categoriesIds = product.categoriesIds || [];
product.imagesList = product.imagesList || [];
$scope.product = product;
$scope.product.$resolved = true;
$scope.productCategories = [];
$scope.productStore = null;
$scope.slickConfig = {
enabled: false
};
$timeout(function () {
$scope.slickConfig.enabled = true;
}, 500);
$scope.stores = Store.find();
$scope.stores
.$promise
.then(function () {
$scope.productStore = lodash.find($scope.stores, {id: product.storeId});
});
$scope.categories = Category.find();
$scope.categories.$promise
.then(function () {
$scope.productCategories = lodash.map(lodash.filter($scope.categories, function (category) {
return $scope.product.categoriesIds.indexOf(category.id) > -1;
}), 'name');
});
$scope.uploadImage = function ($file) {
};
$scope.removeImage = function (url, $event) {
};
$scope.setImageAsDefault = function (url) {
$scope.product.image = url;
};
}
})();