Created
November 17, 2016 16:55
-
-
Save phsacramento/9403d2e940cf00496ad72cb55804487c to your computer and use it in GitHub Desktop.
This file contains 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
(function() { | |
'use strict'; | |
angular | |
.module('app.restaurants') | |
.controller('RestaurantsController', RestaurantsController); | |
RestaurantsController.$inject = ['RestaurantsService', 'LocationStateService', '$scope', '$location', '$ionicPopup', '$ionicScrollDelegate', 'FilterService', '$timeout']; | |
function RestaurantsController(RestaurantsService, LocationStateService, $scope, $location, $ionicPopup, $ionicScrollDelegate, FilterService, $timeout) { | |
var vm = this; | |
vm.query; | |
vm.location; | |
vm.viewAddress; | |
$scope.establishments = []; | |
$scope.types = ''; | |
vm.currentTab = 'all'; | |
vm.address = function() { | |
$location.path('/tab/address-zip'); | |
} | |
vm.getAll = function(page) { | |
var state = LocationStateService.getData(); | |
vm.currentTab = 'all'; | |
vm.viewAddress = state.viewAddress; | |
vm.location = state.query; | |
vm.currentPage = page || 1; | |
RestaurantsService.searchByLocation(vm.location, vm.currentPage, FilterService.get()).then(function(data) { | |
$scope.establishments = $scope.establishments.concat(data.establishments); | |
$scope.$broadcast('scroll.infiniteScrollComplete'); | |
if(vm.currentPage < data.total_pages){ | |
$scope.loadingEstablishments = false; | |
}else if(vm.currentPage === data.total_pages){ | |
$scope.loadingEstablishments = true; | |
} | |
$ionicScrollDelegate.scrollTop(); | |
}); | |
} | |
vm.getParcel = function(page) { | |
var state = LocationStateService.getData(); | |
vm.currentTab = 'parcel'; | |
vm.viewAddress = state.viewAddress; | |
vm.location = state.query; | |
vm.shipping_options = "parcel" | |
vm.currentPage = page || 1; | |
RestaurantsService.searchParcelOrDelivery(vm.location, vm.shipping_options, vm.currentPage, FilterService.get()).then(function(data) { | |
$scope.establishments = $scope.establishments.concat(data.establishments); | |
$scope.$broadcast('scroll.infiniteScrollComplete'); | |
if(vm.currentPage < data.total_pages){ | |
$scope.loadingEstablishments = false; | |
}else if(vm.currentPage === data.total_pages){ | |
$scope.loadingEstablishments = true; | |
} | |
$ionicScrollDelegate.scrollTop(); | |
}); | |
} | |
vm.getDelivery = function(page) { | |
var state = LocationStateService.getData(); | |
vm.currentTab = 'delivery'; | |
vm.viewAddress = state.viewAddress; | |
vm.location = state.query; | |
vm.shipping_options = "delivery" | |
vm.currentPage = page || 1; | |
RestaurantsService.searchParcelOrDelivery(vm.location, vm.shipping_options, vm.currentPage, FilterService.get()).then(function(data) { | |
$scope.establishments = $scope.establishments.concat(data.establishments); | |
$scope.$broadcast('scroll.infiniteScrollComplete'); | |
if(vm.currentPage < data.total_pages){ | |
$scope.loadingEstablishments = false; | |
}else if(vm.currentPage === data.total_pages){ | |
$scope.loadingEstablishments = true; | |
} | |
$ionicScrollDelegate.scrollTop(); | |
}); | |
} | |
vm.getRestaurantsNear = function() { | |
LocationStateService.setToNear().then(function(data) { | |
vm.displayTab('all', vm.currentPage, FilterService.get()); | |
}).catch(function(reason){ | |
$ionicPopup.alert({ | |
title: 'Não conseguimos encontrar sua localização! Por favor, verifique se o GPS está corretamente ativado para uma melhor experiência de uso.' | |
}); | |
}); | |
} | |
vm.searchAll = function(keyEvent) { | |
// Verifies if the happened keypress it was 'enter' or 'spacebar' | |
if (keyEvent.which === 13 || keyEvent.which === 32) { | |
RestaurantsService.search(vm.location, vm.query, FilterService.get()).then(function(data) { | |
if(data.length != 0) { | |
$scope.establishments = data; | |
} else { | |
$ionicPopup.alert({ | |
title: 'Nenhum item encontrado! Tente novamente com outras palavras chave.' | |
}); | |
} | |
}).catch(function(reason) { | |
$ionicPopup.alert({ | |
title: 'Ocorreu um erro! Por favor, tente novamente mais tarde.' | |
}); | |
}); | |
} | |
} | |
vm.displayShippingOption = function(shippingOption) { | |
if (shippingOption == "parcel"){ | |
return "Sob Encomenda"; | |
} else if (shippingOption == "delivery"){ | |
return "Delivery"; | |
} else { | |
return "Encomenda e Delivery"; | |
} | |
} | |
vm.classForTab = function(tabName){ | |
if(tabName === vm.currentTab){ | |
return 'active'; | |
} | |
} | |
vm.displayTab = function(tabName, toPage){ | |
var options = { | |
'all': vm.getAll, | |
'delivery': vm.getDelivery, | |
'parcel': vm.getParcel | |
} | |
if(toPage === undefined){ | |
$scope.establishments = []; | |
$scope.loadingEstablishments = false; | |
} | |
options[tabName](toPage); | |
} | |
$scope.$on('$ionicView.loaded', function () { | |
$timeout(function() { | |
$scope.completed = true; | |
}, 1000); | |
}); | |
$scope.loadMoreEstablishments = function(){ | |
vm.currentPage += 1; | |
$scope.loadingEstablishments = true; | |
vm.displayTab(vm.currentTab); | |
} | |
$scope.moreEstablishmentsCanBeLoaded = function(){ | |
return !$scope.loadingEstablishments; | |
} | |
$scope.$on('$ionicView.beforeEnter', function(){ | |
vm.displayTab('all'); | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment