(function () { 'use strict'; angular .module('app') .controller('AdminMainController', adminMain); function adminMain($scope, $state, tableSearcher, adminRepository, errorHandler, uiNotifications, adminModels, $translate, localizationHelper) { var vm = this; vm.filters = new adminModels.AdminsFilters(); vm.translationObject = {}; vm.loadTranslation = function(){ $translate(['Info', 'Success', 'AdminLocked', 'AdminUnlocked']).then(function (tr) { vm.translationObject = tr; }); }; vm.unsubscribe = localizationHelper.eventEmitter.subscribe('onLanguageChangedEvent', function (lang) { vm.loadTranslation(); }); vm.loadTranslation(); // Variables. vm.data = []; vm.searchValue = ""; vm.orderBy = "firstName"; vm.orderDirection = "desc"; vm.sorterIcon = ""; vm.status = +$state.params.status; vm.method = vm.status == 0 ? "getActive" : "getBlocked"; //Methods. vm.search = function (query, pageNumber, pageSize) { vm.filters.search = vm.searchValue; vm.filters.orderBy = vm.orderBy; vm.filters.orderDirection = vm.orderDirection; adminRepository[vm.method](pageNumber, pageSize, vm.filters).success(function (response, status, headers, config) { if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { if (response && response.data) { tableSearcher.define(response ? response.data : []); $scope.bigTotalItems = JSON.parse(headers("X-Pagination")).TotalCount; vm.filteredData = tableSearcher.getData(); } else { tableSearcher.define([]); $scope.bigTotalItems = 0; vm.filteredData = tableSearcher.getData(); } } }); }; vm.getAdmins = function () { vm.search(vm.searchValue, $scope.bigCurrentPage, $scope.itemsPerPage); }; vm.sortBy = function(key){ tableSearcher.sortBy(key, vm); $scope.bigCurrentPage = 1; }; vm.keyPress = function ($event) { tableSearcher.keypress($event, vm.getAdmins); }; vm.updateAdminStatus = function(id, lockedStatus){ adminRepository[lockedStatus + "ById"](id).success(function(response, status, headers, config){ if (errorHandler.check(response, status, config) == errorHandler.STATUS.SUCCESS) { if (lockedStatus == "lock") { uiNotifications.inform(vm.translationObject['AdminLocked'], 'info'); } else { uiNotifications.inform(vm.translationObject['AdminUnlocked'], 'success'); } $state.reload(); } }); }; $scope.bigCurrentPage = tableSearcher.CONST.CURRENT_PAGE; $scope.itemsPerPage = tableSearcher.CONST.ITEMS_PER_PAGE; $scope.$watch('bigCurrentPage + itemsPerPage', function () { vm.getAdmins(); }); $scope.$on("$destroy", function(){ vm.unsubscribe(); }); } // IoC container. adminMain.$inject = [ "$scope", "$state", "appUtils.tableSearcher", "repository.admin", "appUtils.errorHandler", "appUtils.uiNotifications", "appModels.adminModels", "$translate", "helpers.localization" ]; })();