angular.module('app.frontend') .directive("header", function(authManager){ return { restrict: 'E', scope: {}, templateUrl: 'frontend/header.html', replace: true, controller: 'HeaderCtrl', controllerAs: 'ctrl', bindToController: true, link:function(scope, elem, attrs, ctrl) { scope.$on("sync:updated_token", function(){ ctrl.syncUpdated(); ctrl.findErrors(); ctrl.updateOfflineStatus(); }) scope.$on("sync:error", function(){ ctrl.findErrors(); ctrl.updateOfflineStatus(); }) } } }) .controller('HeaderCtrl', function (authManager, modelManager, $timeout, dbManager, syncManager) { this.user = authManager.user; this.updateOfflineStatus = function() { this.offline = authManager.offline(); } this.updateOfflineStatus(); this.findErrors = function() { this.error = syncManager.syncStatus.error; } this.findErrors(); this.accountMenuPressed = function() { this.serverData = {}; this.showAccountMenu = !this.showAccountMenu; this.showFaq = false; this.showNewPasswordForm = false; this.showExtensionsMenu = false; this.showIOMenu = false; } this.toggleExtensions = function() { this.showAccountMenu = false; this.showIOMenu = false; this.showExtensionsMenu = !this.showExtensionsMenu; } this.toggleIO = function() { this.showIOMenu = !this.showIOMenu; this.showExtensionsMenu = false; this.showAccountMenu = false; } this.refreshData = function() { this.isRefreshing = true; syncManager.sync(function(response){ $timeout(function(){ this.isRefreshing = false; }.bind(this), 200) if(response && response.error) { alert("There was an error syncing. Please try again. If all else fails, log out and log back in."); } else { this.syncUpdated(); } }.bind(this)); } this.syncUpdated = function() { this.lastSyncDate = new Date(); } });