salix/front/salix/module.js

125 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
import {ng} from 'core/vendor';
2017-05-17 19:23:47 +00:00
import 'core';
2018-02-10 15:18:01 +00:00
export const appName = 'salix';
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;
vnAuth.initialize();
2018-02-10 15:18:01 +00:00
$rootScope.$on('$viewContentLoaded', () => {});
window.myAppErrorLog = [];
$state.defaultErrorHandler(function(error) {
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
if ($window.routes) {
2019-01-24 08:25:51 +00:00
let keybindings = {};
for (const mod of $window.routes) {
if (!mod || !mod.keybindings)
continue;
2018-09-14 11:51: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-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);
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) {
return function(exception, cause) {
let message;
2019-01-25 14:09:29 +00:00
let messageT;
let $translate = $injector.get('$translate');
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;
}
2019-01-25 14:09:29 +00:00
if (!messageT) {
let data = exception.data;
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-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;
}
2019-01-25 14:09:29 +00:00
if (messageT)
message = $translate.instant(messageT);
vnApp.showError(message);
};
}
ngModule.factory('$exceptionHandler', $exceptionHandler);