blob: 0be6245d2fa615e4b665cdfffad833021dea0182 [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001/// <reference path="../typings/index.d.ts" />
Matteo Scandolod819c922016-12-02 14:06:14 -08002
Matteo Scandolo266907e2016-12-20 13:41:42 -08003
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08004import * as angular from 'angular';
Matteo Scandolod819c922016-12-02 14:06:14 -08005
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08006import 'angular-ui-router';
7import 'angular-resource';
8import 'angular-cookies';
9import routesConfig from './routes';
10
11import {main} from './app/main';
Matteo Scandolod819c922016-12-02 14:06:14 -080012
13import './index.scss';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080014import {xosCore} from './app/core/index';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080015import {xosDataSources} from './app/datasources/index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080016import {xosViews} from './app/views/index';
Matteo Scandolo266907e2016-12-20 13:41:42 -080017import {xosTemplate} from './app/template/index';
18
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080019import {
20 interceptorConfig, userStatusInterceptor, CredentialsInterceptor,
21 NoHyperlinksInterceptor
22} from './interceptors';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080023import {IXosCrudData} from './app/views/crud/crud';
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080024import {IXosPageTitleService} from './app/core/services/page-title';
Matteo Scandolo04964232017-01-07 12:53:46 -080025import {IXosAuthService} from './app/datasources/rest/auth.rest';
Matteo Scandolob4b74a82017-01-12 13:12:26 -080026import {IXosModelSetupService} from './app/core/services/helpers/model-setup.helpers';
Matteo Scandolo828d1e82017-01-17 14:49:38 -080027import {IXosNavigationRoute} from './app/core/services/navigation';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080028
29export interface IXosState extends angular.ui.IState {
30 data: IXosCrudData;
31};
32
Matteo Scandolo828d1e82017-01-17 14:49:38 -080033export interface IXosAppConfig {
34 apiEndpoint: string;
35 websocketClient: string;
36}
37
38export interface IXosStyleConfig {
39 projectName: string;
40 payoff: string;
41 favicon: string;
42 background: string;
43 logo: string;
44 routes: IXosNavigationRoute[];
45}
46
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080047angular
Matteo Scandolo266907e2016-12-20 13:41:42 -080048 .module('app', [
49 xosCore,
50 xosDataSources,
51 xosViews,
52 'ui.router',
53 'ngResource',
54 xosTemplate // template module
55 ])
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080056 .config(routesConfig)
57 .config(interceptorConfig)
58 .factory('UserStatusInterceptor', userStatusInterceptor)
59 .factory('CredentialsInterceptor', CredentialsInterceptor)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080060 .factory('NoHyperlinksInterceptor', NoHyperlinksInterceptor)
61 .component('xos', main)
Matteo Scandolo828d1e82017-01-17 14:49:38 -080062 .run(function($rootScope: ng.IRootScopeService, $transitions: any, StyleConfig: IXosStyleConfig) {
Matteo Scandolo99fface2016-12-21 15:37:23 -080063 $rootScope['favicon'] = `./app/images/brand/${StyleConfig.favicon}`;
Matteo Scandolo266907e2016-12-20 13:41:42 -080064 $transitions.onSuccess({ to: '**' }, (transtion) => {
65 if (transtion.$to().name === 'login') {
66 $rootScope['class'] = 'blank';
67 }
68 else {
69 $rootScope['class'] = '';
70 }
71 });
72 })
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080073 .run((
Matteo Scandolob4b74a82017-01-12 13:12:26 -080074 $transitions: any,
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080075 $location: ng.ILocationService,
76 $state: ng.ui.IStateService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080077 ModelSetup: IXosModelSetupService,
Matteo Scandolo04964232017-01-07 12:53:46 -080078 AuthService: IXosAuthService,
Matteo Scandolobac22452017-01-03 16:35:32 -080079 toastr: ng.toastr.IToastrService,
Matteo Scandolo5655bdc2016-12-16 08:32:15 -080080 PageTitle: IXosPageTitleService
81 ) => {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080082
Matteo Scandolo04964232017-01-07 12:53:46 -080083 // check the user login
84 $transitions.onSuccess({ to: '**' }, (transtion) => {
85 if (!AuthService.getUser()) {
86 $state.go('login');
87 }
88 });
89
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080090 // save the last visited state before reload
91 const lastRoute = window.location.hash.replace('#', '');
92
Matteo Scandolob4b74a82017-01-12 13:12:26 -080093 // if the user is authenticated
94 if (AuthService.getUser()) {
95 ModelSetup.setup()
96 .then(() => {
97 // after setting up dynamic routes, redirect to previous state
98 $location.path(lastRoute);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080099 });
Matteo Scandolob4b74a82017-01-12 13:12:26 -0800100 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800101
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800102 });
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800103