Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/src/views/common/main.controller.js

120 lines
3.9 KiB
JavaScript
Raw Normal View History

2013-11-12 19:29:15 +01:00
/**
* @ngdoc controller
* @name Umbraco.MainController
* @function
*
* @description
* The main application controller
*
*/
function MainController($scope, $rootScope, $location, $routeParams, $timeout, $http, $log, notificationsService, userService, navigationService, historyService, legacyJsLoader, updateChecker) {
2013-11-12 19:29:15 +01:00
var legacyTreeJsLoaded = false;
//the null is important because we do an explicit bool check on this in the view
//the avatar is by default the umbraco logo
$scope.authenticated = null;
$scope.avatar = "assets/img/application/logo.png";
//subscribes to notifications in the notification service
$scope.notifications = notificationsService.current;
$scope.$watch('notificationsService.current', function (newVal, oldVal, scope) {
if (newVal) {
$scope.notifications = newVal;
}
});
$scope.removeNotification = function (index) {
notificationsService.remove(index);
};
$scope.closeDialogs = function (event) {
//only close dialogs if non-link and non-buttons are clicked
var el = event.target.nodeName;
var els = ["INPUT","A","BUTTON"];
if(els.indexOf(el) >= 0){return;}
var parents = $(event.target).parents("a,button");
if(parents.length > 0){
return;
}
//SD: I've updated this so that we don't close the dialog when clicking inside of the dialog
var nav = $(event.target).parents("#navigation");
if (nav.length === 1) {
return;
}
$rootScope.$emit("closeDialogs", event);
};
//when a user logs out or timesout
$scope.$on("notAuthenticated", function() {
$scope.authenticated = null;
$scope.user = null;
});
//when a user is authorized setup the data
$scope.$on("authenticated", function (evt, data) {
//We need to load in the legacy tree js but only once no matter what user has logged in
if (!legacyTreeJsLoaded) {
legacyJsLoader.loadLegacyTreeJs($scope).then(
function (result) {
legacyTreeJsLoaded = true;
//TODO: We could wait for this to load before running the UI ?
});
}
$scope.authenticated = data.authenticated;
$scope.user = data.user;
updateChecker.check().then(function(update){
if(update && update !== "null"){
if(update.type !== "None"){
var notification = {
headline: "Update available",
message: "Click to download",
sticky: true,
type: "info",
url: update.url
};
notificationsService.add(notification);
}
}
});
//if the user has changed we need to redirect to the root so they don't try to continue editing the
//last item in the URL
if (data.lastUserId && data.lastUserId !== data.user.id) {
$location.path("/").search("");
historyService.removeAll();
}
if($scope.user.emailHash){
$timeout(function () {
//yes this is wrong..
$("#avatar-img").fadeTo(1000, 0, function () {
$timeout(function () {
//this can be null if they time out
if ($scope.user && $scope.user.emailHash) {
$scope.avatar = "http://www.gravatar.com/avatar/" + $scope.user.emailHash + ".jpg?s=64&d=mm";
}
});
$("#avatar-img").fadeTo(1000, 1);
});
}, 3000);
}
});
}
//register it
angular.module('umbraco').controller("Umbraco.MainController", MainController);