UNPKG

4.63 kBJavaScriptView Raw
1define([
2 'directives/ng-directives',
3 'directives/datatable',
4
5 'services/ng-services',
6 'services/log',
7 'services/ng-session',
8
9 'views/settings',
10 'views/tournament',
11 'views/scoresheet',
12 'views/scores',
13
14 'angular-bootstrap',
15 'angular-touch',
16 'angular-animate',
17 'angular-sanitize',
18 'angular-storage',
19 'angular'
20
21],function(directives, datatable, services, log, session, settings, tournament, scoresheet, scores) {
22
23 log('device ready');
24
25 //initialize main controller and load main view
26 //load other main views to create dynamic views for different device layouts
27 angular.module('main',['ngAnimate']).controller('mainCtrl',[
28 '$scope', '$session', '$location',
29 function($scope, $session, $location) {
30 log('init main ctrl');
31
32 const PAGES = [
33 { name: 'scoresheet', title: 'Scoresheet', icon: 'check' },
34 { name: 'scores', title: 'Scores', icon: 'list' },
35 { name: 'tournament', title: 'Tournament', icon: 'people' },
36 { name: 'settings', title: 'Settings', icon: 'settings' }
37 ];
38
39 const pageLoader = angular.element('.viewMain .dimmer');
40
41 $scope.drawer = 'views/drawer.html';
42 $scope.scoringPages = ['scoresheet','settings'];
43 $scope.validationErrors = [];
44 $scope.drawerVisible = false;
45
46 $session.load().then(function(session) {
47 if(session['passport']) {
48 $scope.user = session['passport'].user;
49 $scope.pages = PAGES.filter(page => $scope.user.pages.includes(page.name));
50 } else {
51 $scope.pages = PAGES;
52 }
53 // Enrich pages
54 $scope.pages.forEach(page => {
55 page.route = `views/pages/${page.name}.html`;
56 page.classes = `page view-${page.name}${($scope.pages.length === 1 ? ' only-page' : '')}`;
57 });
58 // Set current page
59 let urlPath = $location.path();
60 let pageFromURL = $scope.pages.find(page => `/${page.name}` === urlPath);
61
62 $scope.setPage(pageFromURL || $scope.pages[0]);
63 });
64
65 $scope.toggleDrawer = function(set) {
66 if (set !== undefined) {
67 $scope.drawerVisible = set;
68 } else {
69 $scope.drawerVisible = !$scope.drawerVisible;
70 }
71 };
72
73 $scope.setPage = function(page) {
74 if($scope.currentPage) {
75 $scope.currentPage.scope = undefined;
76 }
77 $scope.currentPage = page;
78 $location.path(page.name);
79 pageLoader.removeClass('disabled');
80 $scope.drawerVisible = false;
81 };
82
83 $scope.goTo = function(pageName, callback) {
84 var page = $scope.pages.find(page => page.name === pageName);
85 if(page) {
86 $scope.pageLoadCallback = callback;
87 setPage(page);
88 }
89 }
90
91 $scope.pageLoaded = function() {
92 if($scope.pageLoadCallback) {
93 $scope.pageLoadCallback($scope.currentPage.scope);
94 $scope.pageLoadCallback = undefined;
95 }
96 pageLoader.addClass('disabled');
97 };
98
99 $scope.initPage = function(pageName, pageScope) {
100 log(`init ${pageName} page`);
101 $scope.pages.find(page => page.name === pageName).scope = pageScope;
102 }
103
104 // Because of a bug in iOS (https://bugs.webkit.org/show_bug.cgi?id=136041) We'll add
105 // A class 'ios' to the body if we're in iOS so we could change it in the css. see file css/ios.css
106 // TODO delete this when the bug is fixed
107 if (navigator.appVersion.indexOf("Mac")!=-1) {
108 angular.element('body').addClass('ios');
109 }
110
111 }
112 ]);
113 angular.module('main').config(function($compileProvider){
114 // Override to allow data: URI's (e.g. for CSV export)
115 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/);
116 });
117 angular.bootstrap(document.body,[
118 'main',
119 'ui.bootstrap',
120 'ngSanitize',
121 'ngTouch',
122 settings.name,
123 tournament.name,
124 scoresheet.name,
125 scores.name,
126 services.name,
127 directives.name
128 ]);
129});