2018-12-27 11:54:16 +00:00
|
|
|
import {ng} from 'core/vendor';
|
2017-05-17 19:23:47 +00:00
|
|
|
import 'core';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
export const appName = 'salix';
|
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
const ngModule = ng.module('salix', ['vnCore']);
|
2017-05-18 15:35:07 +00:00
|
|
|
export default ngModule;
|
2018-02-10 15:18:01 +00:00
|
|
|
|
2019-01-24 08:25:51 +00:00
|
|
|
run.$inject = ['$window', '$rootScope', 'vnAuth', 'vnApp', '$state'];
|
|
|
|
export function run($window, $rootScope, vnAuth, vnApp, $state) {
|
2018-02-10 15:18:01 +00:00
|
|
|
$window.validations = {};
|
|
|
|
vnApp.name = appName;
|
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
vnAuth.initialize();
|
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
$rootScope.$on('$viewContentLoaded', () => {});
|
|
|
|
window.myAppErrorLog = [];
|
|
|
|
$state.defaultErrorHandler(function(error) {
|
2018-05-25 15:25:35 +00:00
|
|
|
if (error.type === 3) // ABORTED_TRANSITION
|
2018-02-10 15:18:01 +00:00
|
|
|
window.myAppErrorLog.push(error);
|
|
|
|
});
|
2018-09-14 11:51:28 +00:00
|
|
|
|
2019-01-23 16:49:28 +00:00
|
|
|
if ($window.routes) {
|
2019-01-24 08:25:51 +00:00
|
|
|
let keybindings = {};
|
|
|
|
|
2019-01-23 16:49:28 +00:00
|
|
|
for (const mod of $window.routes) {
|
|
|
|
if (!mod || !mod.keybindings)
|
|
|
|
continue;
|
2018-09-14 11:51:28 +00:00
|
|
|
|
2019-01-23 16:49:28 +00:00
|
|
|
for (const binding of mod.keybindings) {
|
2019-01-24 08:25:51 +00:00
|
|
|
let err;
|
|
|
|
if (!binding.key)
|
|
|
|
err = `Missing attribute 'key' in binding`;
|
|
|
|
else if (!binding.state)
|
|
|
|
err = `Missing attribute 'state' in binding`;
|
|
|
|
else if (keybindings[binding.key])
|
|
|
|
err = `Binding key redeclared`;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
console.warn(`${err}: ${mod.module}: ${JSON.stringify(binding)}`);
|
|
|
|
else
|
|
|
|
keybindings[binding.key] = binding.state;
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 11:51:28 +00:00
|
|
|
|
2019-01-24 08:25:51 +00:00
|
|
|
$window.addEventListener('keyup', function(event) {
|
|
|
|
if (event.defaultPrevented || !event.altKey || !event.ctrlKey)
|
|
|
|
return;
|
2019-01-23 16:49:28 +00:00
|
|
|
|
2019-01-24 08:25:51 +00:00
|
|
|
let state = keybindings[event.key];
|
|
|
|
if (state) {
|
|
|
|
$state.go(state);
|
|
|
|
event.preventDefault();
|
2018-09-14 11:51:28 +00:00
|
|
|
}
|
2019-01-24 08:25:51 +00:00
|
|
|
});
|
2018-09-14 11:51:28 +00:00
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
}
|
|
|
|
ngModule.run(run);
|
2018-05-25 15:25:35 +00:00
|
|
|
|
|
|
|
config.$inject = ['$translatePartialLoaderProvider', '$httpProvider'];
|
|
|
|
export function config($translatePartialLoaderProvider, $httpProvider) {
|
|
|
|
$translatePartialLoaderProvider.addPart(appName);
|
|
|
|
$httpProvider.interceptors.push('vnInterceptor');
|
|
|
|
}
|
|
|
|
ngModule.config(config);
|
|
|
|
|
|
|
|
// Unhandled exceptions
|
|
|
|
|
2019-01-25 14:09:29 +00:00
|
|
|
$exceptionHandler.$inject = ['vnApp', '$window', '$state', '$injector'];
|
|
|
|
function $exceptionHandler(vnApp, $window, $state, $injector) {
|
2018-05-25 15:25:35 +00:00
|
|
|
return function(exception, cause) {
|
|
|
|
let message;
|
2019-01-25 14:09:29 +00:00
|
|
|
let messageT;
|
|
|
|
let $translate = $injector.get('$translate');
|
2018-05-25 15:25:35 +00:00
|
|
|
|
|
|
|
if (exception.name == 'HttpError') {
|
|
|
|
switch (exception.xhrStatus) {
|
|
|
|
case 'timeout':
|
|
|
|
case 'abort':
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:09:29 +00:00
|
|
|
switch (exception.status) {
|
|
|
|
case 401:
|
|
|
|
if ($state.current.name != 'login') {
|
2019-01-25 16:05:53 +00:00
|
|
|
messageT = 'Session has expired';
|
2019-01-25 14:09:29 +00:00
|
|
|
let params = {continue: $window.location.hash};
|
|
|
|
$state.go('login', params);
|
|
|
|
} else
|
|
|
|
messageT = 'Invalid login';
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
messageT = 'Access denied';
|
|
|
|
break;
|
|
|
|
case 502:
|
|
|
|
messageT = 'It seems that the server has fall down';
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
messageT = 'Could not contact the server';
|
|
|
|
break;
|
|
|
|
}
|
2018-05-25 15:25:35 +00:00
|
|
|
|
2019-01-25 14:09:29 +00:00
|
|
|
if (!messageT) {
|
|
|
|
let data = exception.data;
|
2018-05-25 15:25:35 +00:00
|
|
|
|
2019-01-25 14:09:29 +00:00
|
|
|
if (data && data.error instanceof Object)
|
|
|
|
message = data.error.message;
|
|
|
|
else
|
|
|
|
message = `${exception.status}: ${exception.statusText}`;
|
2018-05-25 15:25:35 +00:00
|
|
|
}
|
2018-12-27 11:54:16 +00:00
|
|
|
} else if (exception.name == 'UserError')
|
2019-01-25 14:09:29 +00:00
|
|
|
messageT = exception.message;
|
2018-12-27 11:54:16 +00:00
|
|
|
else {
|
2018-10-18 18:48:21 +00:00
|
|
|
vnApp.showError('Ups! Something went wrong');
|
2018-10-22 06:23:10 +00:00
|
|
|
console.error(exception);
|
2018-10-18 18:48:21 +00:00
|
|
|
throw exception;
|
2018-05-25 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 14:09:29 +00:00
|
|
|
if (messageT)
|
|
|
|
message = $translate.instant(messageT);
|
2018-05-25 15:25:35 +00:00
|
|
|
vnApp.showError(message);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
ngModule.factory('$exceptionHandler', $exceptionHandler);
|