Created
August 15, 2016 16:49
-
-
Save oleglukashev/3c04d86d5fbb429a9a4c4e522debb889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('baton.controllers', []) | |
.controller('EditActiveList', function( $scope, storage, $element, $rootScope ) { | |
$scope.isEdit = false; | |
$scope.list = null; | |
$scope.init = function( list ) { | |
$scope.list = list; | |
if($scope.list){ | |
$scope.list.last_name = $scope.list.name; | |
} | |
}; | |
$scope.edit = function() { | |
$scope.isEdit = true; | |
$scope.list.isEdit = true; | |
$scope.list.__backup = angular.copy($scope.list); | |
setTimeout(function() { | |
jQuery('div.product-list-title .product-edit-list-title input').focus(); | |
}, 200); | |
}; | |
$scope.update = function() { | |
$scope.endEdit(); | |
storage.updateList($scope.list, | |
null, | |
function(){ | |
$scope.rollbacklList($scope.list); | |
}); | |
}; | |
$scope.cancelEdit = function() { | |
$scope.endEdit(); | |
$scope.rollbacklList($scope.list); | |
}; | |
$scope.endEdit = function(){ | |
$scope.isEdit = false; | |
$scope.list.isEdit = false; | |
}; | |
$scope.rollbacklList = function(list){ | |
if(list && list.__backup){ | |
list.name = list.__backup.name; | |
} | |
}; | |
$scope.remove = function() { | |
$rootScope.$broadcast('deleteActiveList', $scope.list ); | |
}; | |
$rootScope.$on('createActiveList', function(event, list) { | |
$scope.init( list ); | |
}); | |
}) | |
.controller('Unique', function( $scope, storage, $element, $rootScope, filterFilter ) { | |
$scope.editUnique = null; | |
$scope.uniques = []; | |
$scope.sortType = storage.getSortTypeUniques(); | |
$scope.filterQueries = { | |
'byAbc': '', | |
'byGroup': '', | |
'byFavorite': '!0' | |
} | |
$scope.sortQueries = { | |
'byAbc': ['title'], | |
'byGroup': ['-group_id', 'title'], | |
'byFavorite': ['-count', 'title'] | |
} | |
$scope.getFavoriteUniques = function() { | |
return filterFilter( storage.unique_items, { count: '!0' } ) | |
}; | |
$scope.init = function() { | |
storage.loadUnique(); | |
}; | |
$scope.addOrRemoveToProducts = function( unique ) | |
{ | |
if(unique == $scope.editUnique){ | |
return; | |
} | |
$scope.cancelEdit(); | |
storage.toggleUnique(unique, changeSearchPopup); | |
}; | |
$scope.edit = function(unique, index) { | |
$scope.cancelEdit(); | |
var top = $('.product-index-'+index+'.unique-list-item').offset().top; | |
if(top < $('.header').height()){ | |
scrollUniqueToTop(index); | |
} | |
if(top > $(window).height() - $('.header').height()){ | |
scrollUniqueToBottom(index); | |
} | |
$scope.editUnique = unique; | |
$scope.editUnique.__backup = {group_id : unique.group_id}; | |
$scope.editUnique.isEdit = true; | |
$('.slimScrollDiv').css('overflow', 'visible'); | |
setTimeout(function(){ | |
if($scope.editUnique === null){ | |
$scope.edit(unique, index); | |
safeApply($scope); | |
return; | |
} | |
$('.product-index-'+index+'.unique-list-colors').show(); | |
moveColorPanelToUnique(index); | |
}, 50); | |
//$('.unique-list-colors') | |
}; | |
$scope.rollbackUnique = function(unique){ | |
if(unique && unique.__backup){ | |
unique.group_id = unique.__backup.group_id; | |
} | |
}; | |
$scope.cancelEdit = function() { | |
$scope.rollbackUnique($scope.editUnique); | |
$scope.endEdit(); | |
}; | |
$scope.endEdit = function(){ | |
if($scope.editUnique && $scope.editUnique.isEdit){ | |
$scope.editUnique.isEdit = false; | |
} | |
$scope.editUnique = null; | |
}; | |
$scope.updateGroup = function(unique, group_id){ | |
if(unique.group_id !== group_id){ | |
unique.group_id = group_id; | |
storage.updateGroupId(unique); | |
} | |
$scope.endEdit(); | |
}; | |
$scope.remove = function( unique ) { | |
$scope.cancelEdit(); | |
storage.deleteUnique(unique); | |
}; | |
$rootScope.$on('reloadUnique', function(){ | |
$scope.uniques = storage.unique_items; | |
changeSizeUniqueList(); | |
}); | |
$scope.activeUniqueList = function(event, value) { | |
$scope.active(); | |
}; | |
$scope.setSortType = function( sortType ) { | |
$scope.cancelEdit(); | |
$scope.sortType = sortType; | |
storage.setSortTypeUniques( sortType ) | |
} | |
$rootScope.$on('productsForUniques', function(event, products) { | |
for( var i = 0; i < products.length; i++ ) { | |
for( var sub_i = 0; sub_i < $scope.uniques.length; sub_i++ ) { | |
if ( products[i].title === $scope.uniques[sub_i].title ) { | |
$scope.uniques[sub_i].inProducts = true; | |
} | |
} | |
} | |
}); | |
$rootScope.$on('uniqueCancelEdit', function(){ | |
$scope.cancelEdit(); | |
}); | |
$rootScope.$on('initUnique', function( event ) { | |
$scope.init(); | |
}); | |
$rootScope.$broadcast('initUnique'); | |
function changeSizeUniqueList() { | |
return false; | |
var height = $('.unique-list-item').height(); | |
$(".unique-list-item").height(height); | |
} | |
$('.unique-list-items').on('scroll', function(){ | |
$scope.cancelEdit(); | |
safeApply($scope); | |
}); | |
function moveColorPanelToUnique(unique_index){ | |
var header_height = $('.header').height(), | |
unique_item_el = $('.product-index-'+unique_index+'.unique-list-item'), | |
color_panel_el = $('.product-index-'+unique_index+'.unique-list-colors'), | |
arrow_el = $('.product-index-'+unique_index+' .arrow-box'), | |
color_panel_height = color_panel_el.height(), | |
window_height = $(window).height(), | |
top_offset = 10, | |
bottom_offset = 40; | |
if(color_panel_el.is(':hidden')){ | |
return; | |
} | |
arrow_el.offset({top:unique_item_el.offset().top}); | |
arrow_el.height(unique_item_el.height()); | |
color_panel_el.offset({top: unique_item_el.offset().top - color_panel_height/2}); | |
var y_pos = color_panel_el.offset().top; | |
if(y_pos + color_panel_height > window_height - bottom_offset){ | |
color_panel_el.offset({top: window_height - color_panel_height - bottom_offset}); | |
} | |
y_pos = color_panel_el.offset().top; | |
if(y_pos < header_height + top_offset){ | |
color_panel_el.offset({top: header_height + top_offset}); | |
} | |
color_panel_el.css('visibility', 'visible'); | |
} | |
}) | |
.controller('Lists', function( $scope, $element, storage, $rootScope ) { | |
$scope.addActive = true; | |
$scope.lists = false; | |
//$scope.activeList = 0; | |
$scope.init = function() { | |
storage.loadLists(function(){ | |
$scope.onListsLoaded(); | |
}); | |
}; | |
$scope.onListsLoaded = function(){ | |
$scope.lists = storage.lists; | |
//$scope.active(storage.active_list); | |
changeSearchPopup(); | |
$scope.updateAddStatus(); | |
safeApply($scope); | |
}; | |
$scope.create = function() { | |
storage.createList(function(){ | |
// $scope.active(storage.active_list); | |
$scope.updateAddStatus(); | |
window.scrollTo(0,0); | |
}); | |
} | |
$scope.updateAddStatus = function(){ | |
$scope.addActive = storage.canCreateLists(); | |
}; | |
$scope.remove = function( list ) { | |
storage.deleteList(list, | |
function(){ | |
$scope.updateAddStatus(); | |
changeSearchPopup(); | |
}); | |
}; | |
$scope.active = function( list_id ) { | |
storage.setActiveList(list_id); | |
$scope.lists = storage.lists; | |
}; | |
$scope.short_line = function( elems ) { | |
return 'Bananas Bananas'; | |
}; | |
var timer = setInterval(function(){ | |
$rootScope.$broadcast('reloadData'); | |
}, 20000); | |
$rootScope.$on('reloadData', function(event){ | |
$scope.init(); | |
}); | |
$rootScope.$on('deleteActiveList', function() { | |
$scope.remove( storage.active_list ); | |
}); | |
// $rootScope.$on('reloadUnique', function(event){ | |
// $scope.init(); | |
// }); | |
$rootScope.$on('changeCountList', function( event, count ) { | |
if(storage.active_list){ | |
storage.active_list.left_count = count; | |
} | |
}); | |
$rootScope.$on('createActiveList', function(){ | |
$scope.onListsLoaded(); | |
}); | |
$rootScope.$broadcast('reloadData'); | |
}) | |
.controller('Products', function( $scope, $element, storage, parser, $rootScope, filterFilter ) { | |
$scope.list = false; | |
$scope.products = []; | |
$scope.editProduct = null; | |
$scope.isEdit = false; | |
$scope.closeTimeout = null; | |
$scope.updateTimeout = null; | |
$scope.loaded = null; | |
$scope.$watch('products', function(products) { | |
$rootScope.$broadcast('changeCountList', filterFilter(products, {state: "new"}).length); | |
refreshHeightPurchasedProdContainer(); | |
}, true); | |
$scope.init = function() { | |
storage.loadProducts(); | |
}; | |
$scope.create = function( new_products ) { | |
var items = new_products.map(function(product){ | |
var new_product = { | |
title : product.title, | |
count : product.count, | |
group_id : product.group_id ? product.group_id : storage.findGroupId(product.title) | |
}; | |
if(new_product.count && new_product.count !== ''){ | |
GAPush(["_trackEvent", "items", "amount", "add_item"]); | |
} | |
return new_product; | |
}); | |
$scope.addProducts(items); | |
}; | |
$scope.addProducts = function(new_products){ | |
storage.addProducts(new_products, function(){ | |
//$rootScope.$broadcast('reloadUnique'); | |
changeSearchPopup(); | |
}, function(){ | |
$scope.status = status; | |
}); | |
$rootScope.$broadcast('reloadProducts'); | |
}; | |
$scope.buy = function( product ) { | |
var new_state = ( product.state === 'new' ) ? 'complited' : 'new'; | |
var old_state = product.state; | |
if(new_state == 'complited'){ | |
GAPush(["_trackEvent", "items", "buy", "crossed_off"]); | |
} else { | |
GAPush(["_trackEvent", "items", "buy", "returned"]); | |
} | |
//product.__backup.state = product.state; | |
product.state = new_state; | |
$scope.saveProduct(product, null, function(){product.state = old_state;}); | |
} | |
$scope.remove = function( product ) { | |
storage.deleteProduct(product, | |
function(){ | |
$scope.list.left_count = filterFilter( $scope.products, { state: 'new' } ).length; | |
changeSearchPopup(); | |
}); | |
}; | |
$scope.edit = function(product){ | |
if($scope.editProduct){ | |
$scope.endEdit(); | |
} | |
if($scope.closeTimeout){ | |
clearTimeout($scope.closeTimeout); | |
} | |
$scope.closeTimeout = null; | |
$scope.editProduct = product; | |
$scope.editProduct.isEdit = true; | |
$scope.isEdit = true; | |
product.__backup = {state : product.state, title : product.title, group_id : product.group_id, count : product.count}; | |
product.title_with_count = product.title + " : " + ( product.count ? product.count : '' ); | |
setTimeout(function() { | |
jQuery(".product-item-edit-input:visible input").focus(); | |
}, 100); | |
}; | |
$scope.cancelEdit = function() { | |
$scope.rollbackProduct($scope.editProduct); | |
$scope.endEdit(); | |
} | |
$scope.rollbackProduct = function(product){ | |
if(product && product.__backup){ | |
var backup = product.__backup; | |
product.title = backup.title; | |
product.count = backup.count; | |
product.group_id = backup.group_id; | |
product.state = backup.state; | |
product.__backup = null; | |
} | |
if($scope.updateTimeout !== null){ | |
clearTimeout($scope.updateTimeout); | |
} | |
$scope.updateTimeout = null; | |
}; | |
$scope.endEdit = function(){ | |
if($scope.editProduct){ | |
$scope.editProduct.isEdit = false; | |
} | |
$scope.editProduct = null; | |
$scope.isEdit = false; | |
$scope.autocompleted = false; | |
safeApply($scope); | |
}; | |
$scope.update = function(p) { | |
var product = $scope.editProduct; | |
if(!product || product != p){ | |
return; | |
} | |
var old_title = product.title_with_count; | |
product.count = parser.parseEdit( old_title ).count; | |
product.title = parser.parseEdit( old_title ).title; | |
GAPush(["_trackEvent", "items", "edit"]); | |
if(product.count && product.count !== ''){ | |
GAPush(["_trackEvent", "items", "amount", "edit_item"]); | |
} | |
var group_id = product.group_id; | |
$scope.closeTimeout = setTimeout(function(){ | |
$scope.endEdit(); | |
},400); | |
$scope.updateTimeout = setTimeout(function(){ | |
$scope.saveProduct(product, function(){ | |
product.__backup = null; | |
}); | |
$scope.updateTimeout = null; | |
},400); | |
} | |
$scope.saveProduct = function(product, success_fn, error_fn){ | |
storage.saveProduct(product, success_fn, function(){ | |
if(error_fn instanceof Function){ | |
error_fn(); | |
} | |
$scope.rollbackProduct(product); | |
}); | |
}; | |
$scope.findAndChangeGroupProduct = function( group_id ) { | |
var product = $scope.editProduct; | |
$scope.editProduct.group_id = group_id; | |
$scope.editProduct.isEdit = true; | |
if($scope.updateTimeout){ | |
clearTimeout($scope.updateTimeout); | |
$scope.updateTimeout = null; | |
} | |
clearTimeout($scope.closeTimeout); | |
$scope.closeTimeout = null; | |
setTimeout(function() { | |
jQuery(".product-item-edit-input:visible input").focus(); | |
}, 200); | |
}; | |
$scope.refreshEdit = function(){ | |
if($scope.isEdit && !($scope.editProduct && $scope.products.indexOf($scope.editProduct) !== -1)){ | |
$scope.editProduct = null; | |
$scope.isEdit = false; | |
} | |
}; | |
$scope.groupInProducts = function(group_id){ | |
var products_with_group = $scope.products.filter(function(product){ | |
return product.group_id == group_id; | |
}); | |
return products_with_group.length > 0; | |
}; | |
$rootScope.$on('createActiveList', function(event, list) { | |
if(!storage.active_list || $scope.list.id != storage.active_list.id){ | |
$scope.loaded = false; | |
$scope.products = []; | |
} | |
storage.loadProducts(); | |
}); | |
$rootScope.$on('reloadProducts', function(){ | |
$scope.products = storage.products; | |
$scope.list = storage.active_list; | |
$scope.editProduct = $scope.products.filter( | |
function(product){ | |
return product.isEdit; | |
})[0]; | |
changeSearchPopup(); | |
$scope.refreshEdit(); | |
$scope.loaded = true; | |
}); | |
$rootScope.$on('reloadItems', function(event){ | |
$scope.init(); | |
}); | |
$rootScope.$on('createProducts', function( event, new_products ) { | |
$scope.create( new_products ); | |
}); | |
$rootScope.$on('getProducts', function( event ) { | |
$rootScope.$broadcast('productsForUniques', $scope.products ); | |
}); | |
}) | |
// .controller('ProductAdding', function($scope, storage, filterFilter, $element, $rootScope){ | |
// $scope.unique_items | |
// }) | |
.controller('InputSearchProducts', function( $scope, storage, parser, filterFilter, $element, $rootScope ) { | |
$scope.isSearch = false; | |
$scope.isInput = false; | |
$scope.searchedProducts = []; | |
$scope.placeholder = ''; | |
$scope.new_products = {title:"", group_id:0}; | |
$scope.autocompleted = false; | |
$scope.forceGroup = false; | |
$scope.capitalized = false; | |
setTimeout(function(){ | |
$scope.placeholder = $(".product-input-search input").attr('default-placeholder'); | |
}, 0); | |
$scope.activeUniqueList = function() { | |
$scope.isShowUnique = true; | |
storage.checkUniquesInList(); | |
$scope.closeSearchOrInput(); | |
}; | |
$scope.closeUniqueList = function() { | |
$scope.isShowUnique = false; | |
$rootScope.$broadcast('uniqueCancelEdit'); | |
}; | |
$scope.changePlaceholder = function(title){ | |
$scope.placeholder = title; | |
}; | |
$scope.startAdd = function(){ | |
if($scope.isSearch || $scope.isInput || $scope.justClosed){ | |
return; | |
} | |
$scope.searchOrInputProduct(); | |
$scope.closeUniqueList(); | |
setTimeout(function(){ | |
$(".product-input-search input").focus(); | |
},200); | |
}; | |
$scope.endAdd = function(){ | |
$scope.isSearch = false; | |
$scope.searchedProducts = false; | |
$scope.isInput = false; | |
$scope.autocompleted = false; | |
$scope.forceGroup = false; | |
$scope.capitalized = false; | |
$scope.closeUniqueList(); | |
setTimeout(function(){ | |
$(".product-input-search input").blur(); | |
},50); | |
$scope.closeSearchOrInput(); | |
$scope.justClosed = true; | |
setTimeout(function(){ | |
$scope.justClosed = false; | |
}, 50); | |
}; | |
$scope.create = function() { | |
$scope.capitalized = false; | |
if($scope.new_products.title === ""){ | |
$scope.endAdd(); | |
} | |
var products = parser.parseNew($scope.new_products.title).map( | |
function(product){ | |
if($scope.new_products.group_id !== storage.getDefaultGroupId()){ | |
product.group_id = $scope.new_products.group_id; | |
} | |
return product; | |
}); | |
$rootScope.$broadcast('createProducts', products); | |
//clear text input | |
products.forEach(function(){ | |
GAPush(["_trackEvent", "items", "add", $scope.autocompleted ? 'autocomplete' : "manually"]); | |
}); | |
$scope.new_products.title = ""; | |
$scope.forceGroup = false; | |
$scope.new_products.group_id = storage.getDefaultGroupId(); | |
$scope.searchOrInputProduct(); | |
}; | |
$scope.searchOrInputProduct = function() { | |
if($scope.justClosed){ | |
return; | |
} | |
var hints = []; | |
if(!$scope.capitalized && $scope.new_products.title !== ''){ | |
$scope.new_products.title = $scope.new_products.title.capitalize(); | |
$scope.capitalized = true; | |
} | |
if($scope.new_products.title.length === 0){ | |
$scope.new_products.group_id = storage.getDefaultGroupId(); | |
$scope.forceGroup = false; | |
} | |
if ($scope.new_products && $scope.new_products.title) { | |
hints = $scope.new_products.title.length > 0 ? | |
$scope.searchHints($scope.new_products.title) : []; | |
//$rootScope.$broadcast('searchProducts', $scope.new_products.title.toString() ); | |
} | |
$scope.setHints(hints); | |
}; | |
$scope.searchHints = function(search_expression){ | |
return filterFilter( storage.unique_items, { title: search_expression } ).map( | |
function(product){ | |
var title = product.title.toLowerCase(), | |
search = search_expression.toLowerCase(); | |
if(title.slice(0, search.length) == search){ | |
product.begin_with = true; | |
} else{ | |
product.begin_with = false; | |
} | |
return product; | |
}); | |
}; | |
$scope.refreshInputGroup = function(){ | |
if(!$scope.forceGroup && $scope.new_products.title.length > 0){ | |
var groups = parser.parseNew($scope.new_products.title).map(function(product){ | |
return storage.findGroupId(product.title); | |
}); | |
var i = 0; | |
var max = groups.length; | |
var same = true; | |
for(i = 0; i < max; i++){ | |
if(groups[i] != groups[0]){ | |
same = false; | |
break; | |
} | |
} | |
if(same){ | |
$scope.setInputGroup(groups[0] || 0); | |
} else { | |
$scope.setInputGroup(0); | |
} | |
} else { | |
$scope.setInputGroup($scope.new_products.group_id); | |
} | |
}; | |
$scope.setInputGroup = function(group_id){ | |
$scope.new_products.group_id = group_id; | |
$(".product-input-search input").focus(); | |
$scope.isInput = true; | |
$scope.closeSearch(); | |
}; | |
$scope.forceSetInputGroup = function(group_id){ | |
$scope.setInputGroup(group_id); | |
$scope.forceGroup = true; | |
}; | |
$scope.insertTitleGroupInAddProductsByTitleGroupAndClose = function( title, group_id ) { | |
$scope.new_products.title = title + " : "; | |
$scope.autocompleted = true; | |
$scope.setInputGroup(group_id); | |
}; | |
$scope.closeSearch = function() { | |
$scope.isSearch = false; | |
$scope.searchedProducts = false; | |
}; | |
$scope.closeInput = function() { | |
$scope.isInput = false; | |
}; | |
$scope.closeSearchOrInput = function( clear_input ) { | |
if ( clear_input && clear_input === 'clear_input' ) { | |
$scope.new_products.title = ''; | |
$scope.new_products.group_id = storage.getDefaultGroupId(); | |
} | |
$scope.closeSearch(); | |
$scope.closeInput(); | |
}; | |
$scope.setHints = function( products ) { | |
products = (function(products){ | |
var new_products = []; | |
var new_products_titles = []; | |
products.forEach(function(product){ | |
if(new_products_titles.indexOf(product.title) === -1){ | |
new_products.push(product); | |
new_products_titles.push(product.title); | |
} | |
}); | |
return new_products; | |
})(products); | |
$scope.searchedProducts = products; | |
$scope.isInput = false; | |
$scope.isSearch = true; | |
if ( products.length ) { | |
$scope.closeInput(); | |
} else { | |
//dont show color block if in input have : | |
if ( | |
$scope.new_products.title.toString().length > 0 | |
) { | |
$scope.refreshInputGroup(); | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment