cobuild-angular-stack
Version:
Base stack angular sass jade gulp
245 lines (204 loc) • 9.35 kB
JavaScript
(function (module) {
'use strict';
module.controller('GuestRegistryController', GuestRegistryController);
GuestRegistryController.$inject = ['$scope', '$rootScope', '$state', '$stateParams', 'ProductTemplate', 'Store', 'Category', 'CoupleAccount', 'toastr', '$uibModal', '$localStorage', 'currencies', 'currencyConversionPromiseFilter', 'currencyFilter', '$analytics', '$translate', 'account', 'lodash'];
function GuestRegistryController($scope, $rootScope, $state, $stateParams, ProductTemplate, Store, Category, CoupleAccount, toastr, $modal, $localStorage, currencies, currencyConversion, currency, $analytics, $translate, account, lodash) {
$scope.stores = [];
$scope.categories = Category.find({filter: {where: {'visible': {'neq': false}}}});
$scope.account = account;
var allProducts = [];
$scope.products = [];
$scope.disabled = false;
$scope.currencies = currencies;
$scope.currency = null;
$localStorage.timeStart = Date.now();
$rootScope.cartDisabled = $scope.account.isDisabled;
$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("' + $scope.weddingData.coverPhoto + '")');
$rootScope.$broadcast('imageProfile', $scope.account.weddingData.profilePhoto);
allProducts = lodash.filter(account.productsRegistryList,function(product){
return !product.hidden;
});
$scope.products = allProducts.slice();
$scope.currency = lodash.find(currencies, {code: 'MXN'});
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(function (err) {
if (err.status === 404) {
$translate('error-couple-invitation').then(function (translation) {
toastr.error(translation);
});
$state.go('home');
}
console.error(err);
});
$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').test(product.name);
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]);
lodash.forEach($scope.products, function (product) {
currencyConversion(product.price, 'MXN', $scope.currency.code).then(function (price) {
product.gifted = product.received > product.qty;
product.convertedPrice = currency(price, $scope.currency.symbol + ' ', 2);
});
});
};
$scope.selectCurrency = function (currency) {
$scope.currency = currency;
};
$scope.selectFieldOrder = function (field) {
$scope.sortBy.selected.field = field;
};
$scope.selectOrder = function (order) {
$scope.sortBy.selected.order = order;
};
$scope.addToCart = function (product) {
var cart = $localStorage.cart;
var weddingCart;
if (!cart) {
cart = $localStorage.cart = {};
$localStorage.cartUrls = {};
}
if (!$localStorage.cartUrls[$scope.account.id]) {
$localStorage.cartUrls[$scope.account.id] = {};
}
weddingCart = cart[$scope.account.id];
if (!weddingCart) {
weddingCart = cart[$scope.account.id] = [];
}
var p = lodash.find(weddingCart, {id: product.id});
var categories = lodash.map(product.categoriesIds, function (categoryId) {
var category = lodash.find($scope.categories, {id: categoryId});
return category ? category.name : '';
}).join(', ');
getMixpanel().people.increment({
'Adds to Cart': 1
});
$analytics.eventTrack('Add to Cart', {
'Name': product.name,
'Category': categories,
'Brand': product.store ? product.store.name : '',
'Price': product.price,
'Edited': product.createdAt != product.updatedAt,
'Time spent': ((Date.now() - $localStorage.timeStart) / 1000) + ' sec'
});
if (!product.bought) {
product.bought = 1;
}
if (p) {
if (!account.trackQuantity) {
p.bought += product.bought;
}else if(product.qty - product.received < product.bought){
p.bought += product.bought;
}else{
product.bought = p.bought = product.qty - product.received;
}
} else {
weddingCart.push(product);
}
$state.go('guest.checkout.cart', {url: $stateParams.url});
};
$scope.showProduct = function (product) {
product.categories = lodash.map(product.categoriesIds, function (categoryId) {
var category = lodash.find($scope.categories, {id: categoryId});
return category ? category.name : '';
});
product.categoriesString = product.categories.join(', ');
product.bought = 1;
var modalInstance = $modal.open({
animation: true,
templateUrl: 'partials/guest.registry.show.html',
controller: 'GuestRegistryProductShowController',
size: 'lg',
backdrop: 'static',
resolve: {
product: function () {
return product;
},
account: function () {
return $scope.account;
}
}
});
modalInstance.result
.then(function (product) {
$scope.addToCart(product);
});
};
$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',
'currency'
], $scope.refresh);
$scope.$watchCollection('storeFilter', $scope.refresh);
$scope.$watchCollection('categoriesFilter', $scope.refresh);
}
})(angular.module('uniko.guest.registry'));