salix/front/salix/module.js

98 lines
3.0 KiB
JavaScript

import {ng} from 'core/vendor';
import 'core';
import keybindings from './global-keybindings.yml';
export const appName = 'salix';
const ngModule = ng.module('salix', ['vnCore']);
export default ngModule;
run.$inject = ['$window', '$rootScope', 'vnApp', '$state', '$document'];
export function run($window, $rootScope, vnApp, $state, $document) {
$window.validations = {};
vnApp.name = appName;
$rootScope.$on('$viewContentLoaded', () => {});
window.myAppErrorLog = [];
$state.defaultErrorHandler(function(error) {
if (error.type === 3) // ABORTED_TRANSITION
window.myAppErrorLog.push(error);
});
for (const binding in keybindings) {
if (!keybindings[binding].key || !keybindings[binding].sref)
throw new Error('Binding not formed correctly');
$document.on('keyup', function(e) {
if (e.defaultPrevented) return;
let shortcut = {
altKey: true,
ctrlKey: true,
key: keybindings[binding].key
};
let correctShortcut = true;
for (const key in shortcut)
correctShortcut = correctShortcut && shortcut[key] == e[key];
if (correctShortcut) {
$state.go(keybindings[binding].sref);
e.preventDefault();
e.stopImmediatePropagation();
}
});
}
}
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'];
function $exceptionHandler(vnApp, $window) {
return function(exception, cause) {
let message;
if (exception.name == 'HttpError') {
switch (exception.xhrStatus) {
case 'timeout':
case 'abort':
return;
}
let data = exception.data;
if (data && data.error instanceof Object)
message = data.error.message;
else if (exception.status === -1)
message = `Can't contact with server`;
else
message = `${exception.status}: ${exception.statusText}`;
if (exception.status === 401) {
let location = $window.location;
let continueUrl = location.pathname + location.search + location.hash;
continueUrl = encodeURIComponent(continueUrl);
$window.location = `/auth/?apiKey=${vnApp.name}&continue=${continueUrl}`;
}
} else if (exception.name == 'UserError')
message = exception.message;
else {
vnApp.showError('Ups! Something went wrong');
console.error(exception);
throw exception;
}
vnApp.showError(message);
};
}
ngModule.factory('$exceptionHandler', $exceptionHandler);