salix/front/salix/module.js

150 lines
4.8 KiB
JavaScript

import {ng} from 'core/vendor';
import appConfig from './config.json';
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) {
$rootScope.imagePath = appConfig.imagePath;
$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 = ['$translateProvider', '$translatePartialLoaderProvider', '$httpProvider', '$compileProvider'];
export function config($translateProvider, $translatePartialLoaderProvider, $httpProvider, $compileProvider) {
const langOptions = appConfig.langOptions;
$translateProvider
.registerAvailableLanguageKeys(langOptions.langs, langOptions.langAliases)
// TODO: Circular dependency due to vnInterceptor
// .fallbackLanguage(langOptions.fallbackLang)
.determinePreferredLanguage(() => {
const locale = $translateProvider.resolveClientLocale();
if (langOptions.langs.indexOf(locale) !== -1)
return locale;
if (langOptions.langAliases[locale])
return langOptions.langAliases[locale];
return langOptions.fallbackLang;
});
$translatePartialLoaderProvider.addPart(appName);
$httpProvider.interceptors.push('vnInterceptor');
$compileProvider
.commentDirectivesEnabled(false)
.cssClassDirectivesEnabled(false);
let env = process.env.NODE_ENV;
if (env && env !== 'development')
$compileProvider.debugInfoEnabled(false);
}
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);