cobuild-angular-stack
Version:
Base stack angular sass jade gulp
151 lines (125 loc) • 5.64 kB
JavaScript
(function () {
'use strict';
angular.module('uniko.registry')
.controller('RegistryController', RegistryController);
RegistryController.$inject = ['$scope', '$rootScope', '$state', 'ProductTemplate', 'Store', 'Category', 'CoupleAccount', 'Auth', 'toastr', '$uibModal', '$translate', 'lodash'];
function RegistryController($scope, $rootScope, $state, ProductTemplate, Store, Category, CoupleAccount, Auth, toastr, $uibModal, $translate, lodash) {
$scope.stores = [];
$scope.categories = Category.find({filter: {where: {'visible': {'neq': false}}}});
$scope.account = null;
var allProducts = [];
$scope.products = [];
$scope.disabled = false;
CoupleAccount.getCurrent()
.$promise
.then(function (account) {
$scope.account = account;
allProducts = lodash.filter(account.productsRegistryList,function(product){
return !product.hidden
});
$scope.products = allProducts.slice();
$scope.url = 'http://front.uniko.co/' + $scope.account.url;
$scope.weddingData = account.weddingData;
$scope.weddingData.coverPhoto = $scope.weddingData.coverPhoto ? $scope.weddingData.coverPhoto : "https://s3-us-west-2.amazonaws.com/uniko-prod/products/undefined/images/1463434858873?timestamp=1463434861201";
$('.bg-cover').css('background-image', 'url("' + account.weddingData.coverPhoto + '")');
return Store.find().$promise
})
.then(function (stores) {
$scope.stores = stores;
lodash.forEach(allProducts, function (product) {
product.less = product.qty < product.received ? 0 : product.qty - product.received;
product.store = lodash.find(stores, {id: product.storeId});
});
})
.catch(console.log.bind(console));
$scope.sortBy = {
fields: [
{label: 'Nombre', name: 'name'},
{label: 'Precio', name: 'price'}
],
order: [
{label: 'Ascendente', name: 'ASC'},
{label: 'Descendente', name: 'DESC'}
]
};
$scope.sortBy.selected = {
field: $scope.sortBy.fields[0],
order: $scope.sortBy.order[0]
};
$scope.filterName = '';
$scope.filterPrice = {
min: 0,
max: 10000,
step: 100,
result: [0, 10000],
value: [0, 10000]
};
$scope.storeFilter = [];
$scope.categoriesFilter = [];
ProductTemplate
.highestPrice().$promise
.then(function (res) {
$scope.filterPrice.max = (res.price || 0) + 10000
$scope.filterPrice.value = [0, $scope.filterPrice.max];
});
$scope.refresh = function () {
var price = $scope.filterPrice.result;
var sortBy = $scope.sortBy.selected;
$scope.products = lodash.orderBy(lodash.filter(allProducts, function (product) {
var isValid = product.price >= price[0] && product.price <= price[1] && new RegExp($scope.filterName, 'gi');
if (isValid && $scope.categoriesFilter.length > 0) {
isValid = lodash.some($scope.categoriesFilter, function (categoryToFilter) {
return product.categoriesIds.indexOf(categoryToFilter) > -1;
});
}
if (isValid && $scope.storeFilter.length > 0) {
isValid = lodash.some($scope.storeFilter, function (storeFilter) {
return product.storeId === storeFilter;
});
}
return isValid;
}), [sortBy.field.name, sortBy.order.name]);
};
$scope.selectFieldOrder = function (field) {
$scope.sortBy.selected.field = field;
};
$scope.selectOrder = function (order) {
$scope.sortBy.selected.order = order;
};
$scope.removeFromRegistry = function (productToRemove) {
lodash.remove($scope.account.productsRegistryList, function (product) {
return product.id === productToRemove.id;
});
$scope.account.$save(function () {
$state.go('registry', {}, {reload: true});
}, console.log.bind(console));
};
$scope.animateLabel = function (index, hide) {
jQuery('#add-' + index).animate({
left: hide ? '100%' : '0'
}, 300);
};
$scope.animateOverlay = function (index, hide) {
if (hide) jQuery('#overlay-' + index + ' h4').hide();
jQuery('#overlay-' + index).animate({
height: hide ? '0' : '100%'
}, 300, function () {
jQuery('#overlay-' + index)
if (!hide)
jQuery('#overlay-' + index + ' h4').show();
});
};
$scope
.$watchGroup([
'filterName',
'filterPrice.result',
'sortBy.selected.field',
'sortBy.selected.order'
], $scope.refresh);
$scope.$watchCollection('storeFilter', $scope.refresh);
$scope.$watchCollection('categoriesFilter', $scope.refresh);
$scope.create = function () {
$state.go('products.new');
};
}
})();