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

181 lines
5.8 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, appState, treeService, notificationsService, userService, navigationService, historyService, updateChecker, assetsService, eventsService, umbRequestHelper, tmhDynamicLocale, localStorageService, tourService, editorService) {
2013-11-12 19:29:15 +01:00
//the null is important because we do an explicit bool check on this in the view
$scope.authenticated = null;
$scope.touchDevice = appState.getGlobalState("touchDevice");
$scope.editors = [];
$scope.overlay = {};
2013-11-12 19:29:15 +01:00
$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;
2016-05-24 16:20:20 +02:00
var els = ["INPUT", "A", "BUTTON"];
2013-11-12 19:29:15 +01:00
2016-05-24 16:20:20 +02:00
if (els.indexOf(el) >= 0) { return; }
2013-11-12 19:29:15 +01:00
var parents = $(event.target).parents("a,button");
2016-05-24 16:20:20 +02:00
if (parents.length > 0) {
2013-11-12 19:29:15 +01:00
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("#dialog");
2013-11-12 19:29:15 +01:00
if (nav.length === 1) {
return;
}
eventsService.emit("app.closeDialogs", event);
2013-11-12 19:29:15 +01:00
};
var evts = [];
2013-11-12 19:29:15 +01:00
//when a user logs out or timesout
2016-05-24 16:20:20 +02:00
evts.push(eventsService.on("app.notAuthenticated", function () {
2013-11-12 19:29:15 +01:00
$scope.authenticated = null;
$scope.user = null;
}));
2016-05-24 16:20:20 +02:00
evts.push(eventsService.on("app.userRefresh", function(evt) {
userService.refreshCurrentUser().then(function(data) {
$scope.user = data;
//Load locale file
if ($scope.user.locale) {
tmhDynamicLocale.set($scope.user.locale);
}
});
}));
//when the app is ready/user is logged in, setup the data
evts.push(eventsService.on("app.ready", function (evt, data) {
2016-05-24 16:20:20 +02:00
2013-11-12 19:29:15 +01:00
$scope.authenticated = data.authenticated;
$scope.user = data.user;
2017-05-26 02:15:37 +10:00
updateChecker.check().then(function (update) {
2016-05-24 16:20:20 +02:00
if (update && update !== "null") {
if (update.type !== "None") {
2013-11-12 19:29:15 +01:00
var notification = {
2016-05-24 16:20:20 +02:00
headline: "Update available",
message: "Click to download",
sticky: true,
type: "info",
url: update.url
2013-11-12 19:29:15 +01:00
};
notificationsService.add(notification);
}
}
2016-05-24 16:20:20 +02:00
});
2013-11-12 19:29:15 +01:00
//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 (NOTE: the user id can equal zero, so we cannot just do !data.lastUserId since that will resolve to true)
if (data.lastUserId !== undefined && data.lastUserId !== null && data.lastUserId !== data.user.id) {
2013-11-12 19:29:15 +01:00
$location.path("/").search("");
historyService.removeAll();
treeService.clearCache();
//if the user changed, clearout local storage too - could contain sensitive data
localStorageService.clearAll();
}
//if this is a new login (i.e. the user entered credentials), then clear out local storage - could contain sensitive data
2017-05-26 02:15:37 +10:00
if (data.loginType === "credentials") {
localStorageService.clearAll();
2013-11-12 19:29:15 +01:00
}
//Load locale file
if ($scope.user.locale) {
tmhDynamicLocale.set($scope.user.locale);
}
}));
2016-05-24 16:20:20 +02:00
evts.push(eventsService.on("app.ysod", function (name, error) {
$scope.ysodOverlay = {
view: "ysod",
error: error,
2016-05-24 16:20:20 +02:00
show: true
};
}));
// events for drawer
// manage the help dialog by subscribing to the showHelp appState
$scope.drawer = {};
evts.push(eventsService.on("appState.drawerState.changed", function (e, args) {
// set view
if (args.key === "view") {
$scope.drawer.view = args.value;
}
// set custom model
if (args.key === "model") {
$scope.drawer.model = args.value;
}
// show / hide drawer
if (args.key === "showDrawer") {
$scope.drawer.show = args.value;
}
}));
// events for overlays
evts.push(eventsService.on("appState.overlay", function (name, args) {
$scope.overlay = args;
}));
// events for tours
evts.push(eventsService.on("appState.tour.start", function (name, args) {
$scope.tour = args;
$scope.tour.show = true;
2017-10-04 09:19:33 +02:00
}));
evts.push(eventsService.on("appState.tour.end", function () {
$scope.tour = null;
2017-10-04 09:19:33 +02:00
}));
evts.push(eventsService.on("appState.tour.complete", function () {
$scope.tour = null;
}));
// events for backdrop
2017-10-09 10:19:49 +02:00
evts.push(eventsService.on("appState.backdrop", function (name, args) {
$scope.backdrop = args;
}));
2018-04-25 17:55:27 +02:00
evts.push(eventsService.on("appState.editors.add", function (name, args) {
$scope.editors = args.editors;
}));
evts.push(eventsService.on("appState.editors.remove", function (name, args) {
$scope.editors = args.editors;
}));
//ensure to unregister from all events!
$scope.$on('$destroy', function () {
for (var e in evts) {
eventsService.unsubscribe(evts[e]);
}
2013-11-12 19:29:15 +01:00
});
}
//register it
angular.module('umbraco').controller("Umbraco.MainController", MainController).
2017-05-26 02:15:37 +10:00
config(function (tmhDynamicLocaleProvider) {
//Set url for locale files
tmhDynamicLocaleProvider.localeLocationPattern('lib/angular/1.1.5/i18n/angular-locale_{{locale}}.js');
});