From 2da2177c499ccb13dbde7c4c9d9d6516fb3c7ef2 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sat, 5 Sep 2015 17:15:37 +0200 Subject: [PATCH] Autogenerate the main menu. --- openslides/agenda/static/js/agenda/agenda.js | 13 +++ .../static/js/assignments/assignments.js | 13 +++ openslides/core/static/js/core/core.js | 88 ++++++++++++++++-- openslides/core/static/templates/index.html | 73 +++++---------- .../static/js/mediafiles/mediafiles.js | 13 +++ .../motions/static/js/motions/motions.js | 13 +++ openslides/users/static/js/users/users.js | 89 ++++++++++++------- 7 files changed, 213 insertions(+), 89 deletions(-) diff --git a/openslides/agenda/static/js/agenda/agenda.js b/openslides/agenda/static/js/agenda/agenda.js index 62d4868f9..7934cbd80 100644 --- a/openslides/agenda/static/js/agenda/agenda.js +++ b/openslides/agenda/static/js/agenda/agenda.js @@ -47,6 +47,19 @@ angular.module('OpenSlidesApp.agenda', ['OpenSlidesApp.users']) angular.module('OpenSlidesApp.agenda.site', ['OpenSlidesApp.agenda']) +.config([ + 'mainMenuProvider', + function (mainMenuProvider) { + mainMenuProvider.register({ + 'ui_sref': 'agenda.item.list', + 'img_class': 'calendar-o', + 'title': 'Agenda', + 'weight': 200, + 'perm': 'agenda.can_see', + }); + } +]) + .config(function($stateProvider) { $stateProvider .state('agenda', { diff --git a/openslides/assignments/static/js/assignments/assignments.js b/openslides/assignments/static/js/assignments/assignments.js index 7eaef749c..c9f4ced1d 100644 --- a/openslides/assignments/static/js/assignments/assignments.js +++ b/openslides/assignments/static/js/assignments/assignments.js @@ -20,6 +20,19 @@ angular.module('OpenSlidesApp.assignments', []) angular.module('OpenSlidesApp.assignments.site', ['OpenSlidesApp.assignments']) +.config([ + 'mainMenuProvider', + function (mainMenuProvider) { + mainMenuProvider.register({ + 'ui_sref': 'assignments.assignment.list', + 'img_class': 'pie-chart', + 'title': 'Elections', + 'weight': 400, + 'perm': 'assignments.can_see' + }); + } +]) + .config(function($stateProvider) { $stateProvider .state('assignments', { diff --git a/openslides/core/static/js/core/core.js b/openslides/core/static/js/core/core.js index ad837df9e..6c72ed062 100644 --- a/openslides/core/static/js/core/core.js +++ b/openslides/core/static/js/core/core.js @@ -99,9 +99,15 @@ angular.module('OpenSlidesApp.core', [ } ]) -.run(['loadGlobalData', function(loadGlobalData) { - loadGlobalData(); -}]) +// Load the global data on startup and when the operator changes +.run([ + 'loadGlobalData', + 'operator', + function(loadGlobalData, operator) { + loadGlobalData(); + operator.onOperatorChange(loadGlobalData); + } +]) .factory('jsDataModel', ['$http', 'Projector', function($http, Projector) { var BaseModel = function() {}; @@ -176,6 +182,70 @@ angular.module('OpenSlidesApp.core.site', [ 'xeditable', ]) +// Provider to register entries for the main menu. +.provider('mainMenu', [ + function() { + var mainMenuList = []; + var scope; + + this.register = function(config) { + mainMenuList.push(config); + }; + + this.$get = ['operator', function(operator) { + return { + registerScope: function (scope) { + var that = this; + this.scope = scope; + this.updateMainMenu(); + operator.onOperatorChange(function () {that.updateMainMenu()}); + }, + updateMainMenu: function () { + this.scope.elements = this.getElements(); + }, + getElements: function() { + var elements = mainMenuList.filter(function (element) { + return typeof element.perm === "undefined" || operator.hasPerms(element.perm); + }); + + elements.sort(function (a, b) { + return a.weight - b.weight; + }); + return elements; + } + } + }]; + } +]) + +.config([ + 'mainMenuProvider', + function (mainMenuProvider) { + mainMenuProvider.register({ + 'ui_sref': 'dashboard', + 'img_class': 'home', + 'title': 'Home', + 'weight': 100, + }); + + mainMenuProvider.register({ + 'ui_sref': 'core.customslide.list', + 'img_class': 'video-camera', + 'title': 'Projector', + 'weight': 110, + 'perm': 'core.can_see_projector', + }); + + mainMenuProvider.register({ + 'ui_sref': 'config', + 'img_class': 'cog', + 'title': 'Settings', + 'weight': 1000, + 'perm': 'core.can_manage_config', + }); + } +]) + .config(function($urlRouterProvider, $locationProvider) { // define fallback url and html5Mode $urlRouterProvider.otherwise('/'); @@ -419,6 +489,14 @@ angular.module('OpenSlidesApp.core.site', [ } }) +.controller("MainMenuCtrl", [ + '$scope', + 'mainMenu', + function ($scope, mainMenu) { + mainMenu.registerScope($scope); + } +]) + .controller("LanguageCtrl", function ($scope, gettextCatalog) { // controller to switch app language // TODO: detect browser language for default language @@ -449,8 +527,7 @@ angular.module('OpenSlidesApp.core.site', [ '$modalInstance', '$http', 'operator', - 'loadGlobalData', - function ($scope, $modalInstance, $http, operator, loadGlobalData) { + function ($scope, $modalInstance, $http, operator) { $scope.login = function () { $http.post( '/users/login/', @@ -458,7 +535,6 @@ angular.module('OpenSlidesApp.core.site', [ ).success(function(data) { if (data.success) { operator.setUser(data.user_id); - loadGlobalData(); $scope.loginFailed = false; $modalInstance.close(); } else { diff --git a/openslides/core/static/templates/index.html b/openslides/core/static/templates/index.html index 51af20959..11e7641ad 100644 --- a/openslides/core/static/templates/index.html +++ b/openslides/core/static/templates/index.html @@ -103,18 +103,21 @@ @@ -124,48 +127,12 @@
- diff --git a/openslides/mediafiles/static/js/mediafiles/mediafiles.js b/openslides/mediafiles/static/js/mediafiles/mediafiles.js index 4d2b74377..e08cb4f77 100644 --- a/openslides/mediafiles/static/js/mediafiles/mediafiles.js +++ b/openslides/mediafiles/static/js/mediafiles/mediafiles.js @@ -13,6 +13,19 @@ angular.module('OpenSlidesApp.mediafiles', []) angular.module('OpenSlidesApp.mediafiles.site', ['OpenSlidesApp.mediafiles']) +.config([ + 'mainMenuProvider', + function (mainMenuProvider) { + mainMenuProvider.register({ + 'ui_sref': 'mediafiles.mediafile.list', + 'img_class': 'paperclip', + 'title': 'Files', + 'weight': 600, + 'perm': 'mediafiles.can_see', + }); + } +]) + .config(function($stateProvider) { $stateProvider .state('mediafiles', { diff --git a/openslides/motions/static/js/motions/motions.js b/openslides/motions/static/js/motions/motions.js index 41abff805..e6fc8711d 100644 --- a/openslides/motions/static/js/motions/motions.js +++ b/openslides/motions/static/js/motions/motions.js @@ -77,6 +77,19 @@ angular.module('OpenSlidesApp.motions', []) angular.module('OpenSlidesApp.motions.site', ['OpenSlidesApp.motions']) +.config([ + 'mainMenuProvider', + function (mainMenuProvider) { + mainMenuProvider.register({ + 'ui_sref': 'motions.motion.list', + 'img_class': 'file-text', + 'title': 'Motions', + 'weight': 300, + 'perm': 'motions.can_see', + }); + } +]) + .config(function($stateProvider) { $stateProvider .state('motions', { diff --git a/openslides/users/static/js/users/users.js b/openslides/users/static/js/users/users.js index 16ca545be..9540b0168 100644 --- a/openslides/users/static/js/users/users.js +++ b/openslides/users/static/js/users/users.js @@ -77,6 +77,19 @@ angular.module('OpenSlidesApp.users', []) angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users']) +.config([ + 'mainMenuProvider', + function (mainMenuProvider) { + mainMenuProvider.register({ + 'ui_sref': 'users.user.list', + 'img_class': 'user', + 'title': 'Participants', + 'weight': 500, + 'perm': 'users.can_see_name', + }); + } +]) + .config(function($stateProvider) { $stateProvider .state('users', { @@ -165,38 +178,54 @@ angular.module('OpenSlidesApp.users.site', ['OpenSlidesApp.users']) }); }) -.factory('operator', function(User, Group) { - var operator = { - user: null, - perms: [], - isAuthenticated: function() { - return !!this.user; - }, - setUser: function(user_id) { - if (user_id) { - User.find(user_id).then(function(user) { - operator.user = user; - // TODO: load only the needed groups - Group.findAll().then(function() { - operator.perms = user.getPerms(); +.factory('operator', [ + 'User', + 'Group', + 'loadGlobalData', + function(User, Group, loadGlobalData) { + var operatorChangeCallbacks = []; + var operator = { + user: null, + perms: [], + isAuthenticated: function () { + return !!this.user; + }, + onOperatorChange: function (func) { + operatorChangeCallbacks.push(func); + }, + setUser: function(user_id) { + if (user_id) { + User.find(user_id).then(function(user) { + operator.user = user; + // TODO: load only the needed groups + Group.findAll().then(function() { + operator.perms = user.getPerms(); + _.forEach(operatorChangeCallbacks, function (callback) { + callback(); + }); + }); }); - }); - } else { - operator.user = null; - Group.find(1).then(function(group) { - operator.perms = group.permissions; - }); - } - }, - hasPerms: function(perms) { - if (typeof perms == 'string') { - perms = perms.split(' '); - } - return _.intersection(perms, operator.perms).length > 0; - }, + } else { + operator.user = null; + Group.find(1).then(function(group) { + operator.perms = group.permissions; + _.forEach(operatorChangeCallbacks, function (callback) { + callback(); + }); + }); + } + }, + // Returns true if the operator has at least one perm of the perms-list. + hasPerms: function(perms) { + if (typeof perms == 'string') { + perms = perms.split(' '); + } + return _.intersection(perms, operator.perms).length > 0; + }, + } + return operator; } - return operator; -}) +]) .run(function(operator, $rootScope, $http) { // Put the operator into the root scope