app.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. 'use strict';
  2. /*
  3. Copyright 2015-2017 Davide Alberani <da@erlug.linux.it>
  4. RaspiBO <info@raspibo.org>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. /* Register our fantastic app. */
  15. var eventManApp = angular.module('eventManApp', [
  16. 'ngRoute',
  17. 'ngAnimate',
  18. 'eventManServices',
  19. 'eventManControllers',
  20. 'ui.bootstrap',
  21. 'ui.router',
  22. 'pascalprecht.translate',
  23. 'angularFileUpload',
  24. 'angular-websocket',
  25. 'eda.easyFormViewer',
  26. 'eda.easyformGen.stepway'
  27. ]);
  28. /* Add some utilities to the global scope. */
  29. eventManApp.run(['$rootScope', '$state', '$stateParams', '$log', 'Info',
  30. function($rootScope, $state, $stateParams, $log, Info) {
  31. $rootScope.app_uuid = guid();
  32. $rootScope.info = {};
  33. $log.debug('App UUID: ' + $rootScope.app_uuid);
  34. $rootScope.$state = $state;
  35. $rootScope.$stateParams = $stateParams;
  36. $rootScope.error = {error: false, message: '', code: 0};
  37. $rootScope.readInfo = function(callback) {
  38. Info.get({}, function(data) {
  39. $rootScope.info = data || {};
  40. if (data.authentication_required && !(data.user && data.user._id)) {
  41. $state.go('login');
  42. } else if (callback) {
  43. callback(data);
  44. }
  45. });
  46. };
  47. $rootScope.showError = function(error) {
  48. $rootScope.error.code = error.code;
  49. $rootScope.error.message = error.message;
  50. $rootScope.error.error = true;
  51. };
  52. $rootScope.clearError = function() {
  53. $rootScope.error.code = null;
  54. $rootScope.error.message = '';
  55. $rootScope.error.error = false;
  56. };
  57. $rootScope.errorHandler = function(response) {
  58. $log.debug('Handling error message:');
  59. $log.debug(response);
  60. $rootScope.error.status = response.status;
  61. $rootScope.error.statusText = response.statusText;
  62. if (response.data && response.data.error) {
  63. $rootScope.showError(response.data);
  64. } else {
  65. $rootScope.clearError();
  66. }
  67. };
  68. /* Check privileges of the currently logged in user or of the one specified with the second parameter. */
  69. $rootScope.hasPermission = function(permission, user) {
  70. if (!(user || ($rootScope.info && $rootScope.info.user && $rootScope.info.user.permissions))) {
  71. return false;
  72. }
  73. if (!user) {
  74. user = $rootScope.info.user;
  75. }
  76. var granted = false;
  77. var splitted_permission = permission.split('|');
  78. var global_permission = splitted_permission[0] + '|all';
  79. angular.forEach(user.permissions || [],
  80. function(value, idx) {
  81. if (value === 'admin|all' || value === global_permission || value === permission) {
  82. granted = true;
  83. return;
  84. }
  85. }
  86. );
  87. return granted;
  88. };
  89. $rootScope.readInfo();
  90. }]
  91. );
  92. /* Configure the states. */
  93. eventManApp.config(['$stateProvider', '$urlRouterProvider', '$compileProvider',
  94. function($stateProvider, $urlRouterProvider, $compileProvider) {
  95. $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|blob):/);
  96. $urlRouterProvider.otherwise('/events');
  97. $stateProvider
  98. .state('events', {
  99. url: '/events',
  100. templateUrl: 'events-list.html',
  101. controller: 'EventsListCtrl'
  102. })
  103. .state('event', {
  104. url: '/event',
  105. templateUrl: 'event-main.html'
  106. })
  107. .state('event.new', {
  108. url: '/new',
  109. templateUrl: 'event-edit.html',
  110. controller: 'EventDetailsCtrl'
  111. })
  112. .state('event.view', {
  113. url: '/:id/view',
  114. templateUrl: 'event-edit.html',
  115. controller: 'EventDetailsCtrl'
  116. })
  117. .state('event.edit', {
  118. url: '/:id/edit',
  119. templateUrl: 'event-edit.html',
  120. controller: 'EventDetailsCtrl'
  121. })
  122. .state('event.tickets', {
  123. url: '/:id/tickets',
  124. templateUrl: 'event-tickets.html',
  125. controller: 'EventTicketsCtrl'
  126. })
  127. .state('event.ticket', {
  128. url: '/:id/ticket',
  129. templateUrl: 'ticket-main.html'
  130. })
  131. .state('event.ticket.new', {
  132. url: '/new',
  133. templateUrl: 'ticket-edit.html',
  134. controller: 'EventTicketsCtrl'
  135. })
  136. .state('event.ticket.edit', {
  137. url: '/:ticket_id/edit',
  138. templateUrl: 'ticket-edit.html',
  139. controller: 'EventTicketsCtrl'
  140. })
  141. .state('tickets', {
  142. url: '/tickets',
  143. templateUrl: 'tickets-list.html',
  144. controller: 'EventsListCtrl'
  145. })
  146. .state('import', {
  147. url: '/import',
  148. templateUrl: 'import-main.html'
  149. })
  150. .state('import.persons', {
  151. url: '/persons',
  152. templateUrl: 'import-persons.html',
  153. controller: 'FileUploadCtrl'
  154. })
  155. .state('users', {
  156. url: '/users',
  157. templateUrl: 'users-list.html',
  158. controller: 'UsersCtrl'
  159. })
  160. .state('user', {
  161. url: '/user',
  162. templateUrl: 'user-main.html'
  163. })
  164. .state('user.edit', {
  165. url: ':id/edit',
  166. templateUrl: 'user-edit.html',
  167. controller: 'UsersCtrl'
  168. })
  169. .state('login', {
  170. url: '/login',
  171. templateUrl: 'login.html',
  172. controller: 'UsersCtrl'
  173. });
  174. }
  175. ]);