accountant-ui/accountant-ui/js/app.js

155 lines
4.7 KiB
JavaScript

/*
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/>.
*/
// vim: set tw=80 ts=2 sw=2 sts=2:
'use strict';
angular.module('accountant', [
'accountant.accounts',
'accountant.operations',
'accountant.scheduler',
'ngRoute',
'ngBootbox',
'http-auth-interceptor',
'Storage.Service'
])
.factory('sessionInjector', ['$storage', function($storage) {
var sessionInjector = {
request : function(config) {
var token = $storage.get('token');
if(token) {
var token_type = $storage.get('token_type');
var authorization = token_type + ' ' + token;
config.headers.Authorization = authorization;
}
return config;
}
};
return sessionInjector;
}])
.config(['$httpProvider', function($httpProvider) {
// Define interceptors.
$httpProvider.interceptors.push('sessionInjector');
}])
.config(['$routeProvider', function($routeProvider) {
// Defining template and controller in function of route.
$routeProvider
.when('/account/:accountId/operations', {
templateUrl: 'views/operations.html',
controller: 'OperationController',
controllerAs: 'operationsCtrl'
})
.when('/account/:accountId/scheduler', {
templateUrl: 'views/scheduler.html',
controller: 'SchedulerController',
controllerAs: 'schedulerCtrl'
})
.when('/accounts', {
templateUrl: 'views/accounts.html',
controller: 'AccountController',
controllerAs: 'accountsCtrl'
})
.otherwise({
redirectTo: '/accounts'
});
}])
.config(['$storageProvider', function($storageProvider) {
// Configure storage
// Set global prefix for stored keys
$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']);
}])
.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
})
.controller('MainController', [
'$scope', '$rootScope', '$http', 'authService', '$storage', '$ngBootbox',
function($scope, $rootScope, $http, authService, $storage, $ngBootbox) {
$scope.dialogShown = false;
$scope.showLoginForm = function() {
// First, if there are registered credentials, use them
if($scope.dialogShown) {
return;
}
$scope.dialogShown = true;
$storage.clear();
$ngBootbox.customDialog({
title: 'Authentification requise',
templateUrl: 'views/login.tmpl.html',
buttons: {
login: {
label: 'Login',
className: 'btn-primary',
callback: function() {
$scope.dialogShown = false;
var email = $('#email').val();
var password = $('#password').val();
$http.post(
'/api/user/login',
{
'email': email,
'password': password
}
).success(function(result) {
// TODO Alexis Lahouze 2015-08-28 Handle callback.
// Call to /api/login to retrieve the token
$storage.set('token_type', result.token_type);
$storage.set('token', result.token);
$storage.set('expiration_date', result.expiration_date);
authService.loginConfirmed();
});
}
},
cancel: {
label: 'Annuler',
className: 'btn-default',
callback: function() {
authService.loginCancelled(null, 'Login cancelled by user action.');
$scope.dialogShown = false;
}
}
}
});
};
$rootScope.$on('event:auth-loginRequired', $scope.showLoginForm);
}])
;