accountant-ui/src/js/app.js

200 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-10-09 20:17:11 +02:00
// vim: set tw=80 ts=4 sw=4 sts=4:
2016-04-12 10:55:08 +02:00
/*
This file is part of Accountant.
Accountant is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Accountant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
*/
2017-06-10 18:02:19 +02:00
/* jshint node: true */
2016-04-12 10:55:08 +02:00
'use strict';
2017-06-10 18:02:19 +02:00
var angular = require('angular');
var ngRoute = require('angular-route'),
ngBootbox = require('ngbootbox'),
ngStorage = require('meanie-angular-storage'),
ngHttpAuth = require('angular-http-auth');
var base64 =require('base64util');
// Note: ngBootbox seems to have no module.exports.
ngBootbox = 'ngBootbox';
// Note: ngHttpAuth seems to have no module.exports.
ngHttpAuth = 'http-auth-interceptor';
var accountModule = require('./accounts'),
operationModule = require('./operations'),
schedulerModule = require('./scheduler');
require('bootstrap-webpack!./bootstrap.config.js');
2017-06-10 22:06:36 +02:00
var operationsTmpl = require('./operations/operations.html');
var accountsTmpl = require('./accounts/accounts.html');
var schedulerTmpl = require('./scheduler/scheduler.html');
2017-06-10 18:02:19 +02:00
2017-06-10 22:06:36 +02:00
var loginTmpl = require('../views/login.tmpl.html');
2017-06-10 18:02:19 +02:00
var app = angular.module('accountant', [
accountModule.name,
operationModule.name,
schedulerModule.name,
ngRoute,
ngBootbox,
ngHttpAuth,
ngStorage
2016-04-12 10:55:08 +02:00
])
2016-10-14 07:50:06 +02:00
.factory('sessionInjector', function($storage) {
2016-04-12 10:55:08 +02:00
var sessionInjector = {
2016-10-09 20:17:11 +02:00
request: function(config) {
2017-06-10 18:19:00 +02:00
var access_token = $storage.session.get('access_token');
2016-04-12 10:55:08 +02:00
2017-06-10 18:19:00 +02:00
if (access_token) {
//var tokenType = $storage.get('token_type');
var tokenType = 'Bearer';
var authorization = tokenType + ' ' + access_token;
config.headers.authorization = authorization;
2016-04-12 10:55:08 +02:00
}
return config;
}
};
return sessionInjector;
2016-10-14 07:50:06 +02:00
})
2016-04-12 10:55:08 +02:00
2016-10-14 07:50:06 +02:00
.config(function($httpProvider) {
2016-04-12 10:55:08 +02:00
// Define interceptors.
$httpProvider.interceptors.push('sessionInjector');
2016-10-14 07:50:06 +02:00
})
2016-04-12 10:55:08 +02:00
2016-10-14 07:50:06 +02:00
.config(function($routeProvider) {
2016-04-12 10:55:08 +02:00
// Defining template and controller in function of route.
$routeProvider
2016-10-09 20:17:11 +02:00
.when('/account/:accountId/operations', {
2017-06-10 22:06:36 +02:00
templateUrl: operationsTmpl,
2016-10-09 20:17:11 +02:00
controller: 'OperationController',
controllerAs: 'operationsCtrl'
})
.when('/account/:accountId/scheduler', {
2017-06-10 22:06:36 +02:00
templateUrl: schedulerTmpl,
2016-10-09 20:17:11 +02:00
controller: 'SchedulerController',
controllerAs: 'schedulerCtrl'
})
.when('/accounts', {
2017-06-10 22:06:36 +02:00
templateUrl: accountsTmpl,
2016-10-09 20:17:11 +02:00
controller: 'AccountController',
controllerAs: 'accountsCtrl'
})
.otherwise({
redirectTo: '/accounts'
});
2016-10-14 07:50:06 +02:00
})
2016-04-12 10:55:08 +02:00
2016-10-14 07:50:06 +02:00
.config(function($storageProvider) {
2016-04-12 10:55:08 +02:00
// Configure storage
2016-10-09 20:17:11 +02:00
// Set global prefix for stored keys
2016-04-12 10:55:08 +02:00
$storageProvider.setPrefix('accountant');
// Change the default storage engine
// Defaults to 'local'
$storageProvider.setDefaultStorageEngine('session');
// Change the enabled storage engines
// Defaults to ['memory', 'cookie', 'session', 'local']
$storageProvider.setEnabledStorageEngines(['local', 'session']);
2016-10-14 07:50:06 +02:00
})
2016-04-12 10:55:08 +02:00
.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
})
2016-10-14 09:00:55 +02:00
.factory('LoginService', function($http) {
2016-10-14 08:50:54 +02:00
var login = function(email, password) {
2017-06-10 18:19:00 +02:00
var authdata = base64.encode(email + ':' + password);
return $http.post('/api/user/login', {}, {
headers: {
'authorization': 'Basic ' + authdata
}
2016-10-14 08:50:54 +02:00
});
};
return {
'login': login
};
})
2017-06-10 18:19:00 +02:00
.controller('MainController', function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) {
2016-10-14 07:50:06 +02:00
var vm = this;
2016-10-12 19:57:19 +02:00
2016-10-14 07:50:06 +02:00
vm.dialogShown = false;
vm.showLoginForm = function() {
// First, if there are registered credentials, use them
2016-10-14 08:19:23 +02:00
if (vm.dialogShown) {
2016-10-14 07:50:06 +02:00
return;
}
2016-10-09 20:17:11 +02:00
2016-10-14 07:50:06 +02:00
vm.dialogShown = true;
2017-06-10 18:19:00 +02:00
$storage.session.clear();
2016-10-14 07:50:06 +02:00
$ngBootbox.customDialog({
title: 'Authentification requise',
2017-06-10 22:06:36 +02:00
templateUrl: loginTmpl,
2016-10-14 07:50:06 +02:00
buttons: {
login: {
label: 'Login',
className: 'btn-primary',
callback: function() {
vm.dialogShown = false;
2017-06-10 20:36:36 +02:00
var email = angular.element($document[0].querySelector('#email')).val();
var password = angular.element($document[0].querySelector('#password')).val();
2017-06-10 18:19:00 +02:00
LoginService.login(
email, password
).then(function(result) {
2016-10-14 07:50:06 +02:00
// TODO Alexis Lahouze 2015-08-28 Handle callback.
// Call to /api/login to retrieve the token
2017-06-10 18:19:00 +02:00
$storage.session.set('refresh_token', result.data.refresh_token);
$storage.session.set('access_token', result.data.access_token);
2016-10-14 07:50:06 +02:00
authService.loginConfirmed();
});
}
},
cancel: {
label: 'Annuler',
className: 'btn-default',
callback: function() {
authService.loginCancelled(null, 'Login cancelled by user action.');
vm.dialogShown = false;
2016-10-09 20:17:11 +02:00
}
}
2016-10-14 07:50:06 +02:00
}
});
};
2016-04-12 10:55:08 +02:00
2016-10-14 08:19:23 +02:00
vm.onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', vm.showLoginForm);
2016-10-14 07:50:06 +02:00
2016-10-14 08:51:35 +02:00
$rootScope.$on('$destroy', function() {
2016-10-14 08:19:23 +02:00
vm.onAuthLoginRequired = angular.noop();
2016-10-14 07:50:06 +02:00
});
})
2016-04-12 10:55:08 +02:00
;
2017-06-10 18:02:19 +02:00
module.exports = app;