125 lines
3.8 KiB
JavaScript
125 lines
3.8 KiB
JavaScript
import {ng} from 'core/vendor';
|
|
import 'core';
|
|
|
|
export const appName = 'salix';
|
|
|
|
const ngModule = ng.module('salix', ['vnCore']);
|
|
export default ngModule;
|
|
|
|
run.$inject = ['$window', '$rootScope', 'vnAuth', 'vnApp', '$state'];
|
|
export function run($window, $rootScope, vnAuth, vnApp, $state) {
|
|
$window.validations = {};
|
|
vnApp.name = appName;
|
|
|
|
vnAuth.initialize();
|
|
|
|
$rootScope.$on('$viewContentLoaded', () => {});
|
|
window.myAppErrorLog = [];
|
|
$state.defaultErrorHandler(function(error) {
|
|
if (error.type === 3) // ABORTED_TRANSITION
|
|
window.myAppErrorLog.push(error);
|
|
});
|
|
|
|
if ($window.routes) {
|
|
let keybindings = {};
|
|
|
|
for (const mod of $window.routes) {
|
|
if (!mod || !mod.keybindings)
|
|
continue;
|
|
|
|
for (const binding of mod.keybindings) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
$window.addEventListener('keyup', function(event) {
|
|
if (event.defaultPrevented || !event.altKey || !event.ctrlKey)
|
|
return;
|
|
|
|
let state = keybindings[event.key];
|
|
if (state) {
|
|
$state.go(state);
|
|
event.preventDefault();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
ngModule.run(run);
|
|
|
|
config.$inject = ['$translatePartialLoaderProvider', '$httpProvider'];
|
|
export function config($translatePartialLoaderProvider, $httpProvider) {
|
|
$translatePartialLoaderProvider.addPart(appName);
|
|
$httpProvider.interceptors.push('vnInterceptor');
|
|
}
|
|
ngModule.config(config);
|
|
|
|
// Unhandled exceptions
|
|
|
|
$exceptionHandler.$inject = ['vnApp', '$window', '$state', '$injector'];
|
|
function $exceptionHandler(vnApp, $window, $state, $injector) {
|
|
return function(exception, cause) {
|
|
let message;
|
|
let messageT;
|
|
let $translate = $injector.get('$translate');
|
|
|
|
if (exception.name == 'HttpError') {
|
|
switch (exception.xhrStatus) {
|
|
case 'timeout':
|
|
case 'abort':
|
|
return;
|
|
}
|
|
|
|
switch (exception.status) {
|
|
case 401:
|
|
if ($state.current.name != 'login') {
|
|
messageT = 'Session has expired';
|
|
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;
|
|
}
|
|
|
|
if (!messageT) {
|
|
let data = exception.data;
|
|
|
|
if (data && data.error instanceof Object)
|
|
message = data.error.message;
|
|
else
|
|
message = exception.statusText;
|
|
}
|
|
} else if (exception.name == 'UserError')
|
|
messageT = exception.message;
|
|
else {
|
|
vnApp.showError('Ups! Something went wrong');
|
|
console.error(exception);
|
|
throw exception;
|
|
}
|
|
|
|
if (messageT)
|
|
message = $translate.instant(messageT);
|
|
vnApp.showError(message);
|
|
};
|
|
}
|
|
ngModule.factory('$exceptionHandler', $exceptionHandler);
|