salix/front/salix/module.js

161 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
import {ng} from 'core/vendor';
2020-04-25 09:50:04 +00:00
import appConfig from './config.json';
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
2020-11-27 12:10:39 +00:00
run.$inject = ['$window', '$rootScope', 'vnAuth', 'vnApp', 'vnToken', '$state'];
export function run($window, $rootScope, vnAuth, vnApp, vnToken, $state) {
$rootScope.imagePath = (collection, size, id) => {
if (!collection || !size || !id) return;
const basePath = `/api/Images/${collection}/${size}/${id}`;
return `${basePath}/download?access_token=${vnToken.tokenMultimedia}`;
2020-11-27 12:10:39 +00:00
};
2020-04-25 09:50:04 +00:00
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);
2020-04-25 09:50:04 +00:00
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];
2020-06-17 11:09:40 +00:00
return langOptions.fallbackLang;
2020-04-25 09:50:04 +00:00
});
$translatePartialLoaderProvider.addPart(appName);
$httpProvider.interceptors.push('vnInterceptor');
2019-10-09 22:47:29 +00:00
2019-11-10 10:08:44 +00:00
$compileProvider
.commentDirectivesEnabled(false)
.cssClassDirectivesEnabled(false);
2020-03-07 12:52:02 +00:00
let env = process.env.NODE_ENV;
2020-03-13 19:33:12 +00:00
if (env && env !== 'development')
2020-03-07 12:52:02 +00:00
$compileProvider.debugInfoEnabled(false);
}
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:
2022-09-21 13:15:19 +00:00
if (!$state.current.name.includes('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);
2019-01-25 14:09:29 +00:00
} else
messageT = 'Invalid login';
break;
case 403:
2023-12-14 14:09:07 +00:00
messageT = exception.data?.error?.message || 'Access Denied';
2019-01-25 14:09:29 +00:00
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.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);
const additonalData = {
frontPath: $state.current.name,
httpRequest: cause?.replace('Possibly unhandled rejection: ', ''),
backError: exception
};
vnApp.showError(message, additonalData);
};
}
ngModule.factory('$exceptionHandler', $exceptionHandler);