2016-02-07 23:34:03 +01:00

175 lines
5.8 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/>.
*/
var accountantApp = angular.module("accountantApp", [
'ngResource', 'ngRoute',
'mgcrea.ngStrap',
'highcharts-ng',
'http-auth-interceptor',
'ui-notification',
'Storage.Service',
'xeditable'
])
.factory('sessionInjector', ['$storage', function($storage) {
var sessionInjector = {
request : function(config) {
var token = $storage.get('token');
if(token) {
var token_type = $storage.get('token_type');
config.headers.Authorization = authorization;
var authorization = token_type + ' ' + token;
}
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: 'static/templates/operations.html',
controller: 'OperationController',
controllerAs: 'operations'
})
.when('/account/:accountId/scheduler', {
templateUrl: 'static/templates/scheduler.html',
controller: 'SchedulerController',
controllerAs: 'scheduler'
})
.when('/accounts', {
templateUrl: 'static/templates/accounts.html',
controller: 'AccountController',
controllerAs: 'accounts'
})
.otherwise({
redirectTo: '/accounts'
});
}])
.config(['$locationProvider', function($locationProvider) {
// Enable HTML5 mode.
$locationProvider.html5Mode(true);
}])
.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']);
}])
.config(['$resourceProvider', function($resourceProvider) {
// Keep trailing slashes to avoid redirect by flask..
$resourceProvider.defaults.stripTrailingSlashes = false;
}])
.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
})
.controller('MainController', [
'$scope', '$rootScope', '$http', 'authService', '$storage',
function($scope, $rootScope, $http, authService, $storage) {
$scope.dialogShown = false;
$scope.showLoginForm = function(e) {
// First, if there are registered credentials, use them
if($scope.dialogShown) {
return;
}
$scope.dialogShown = true;
$storage.clear();
bootbox.dialog({
title: 'Authentification requise',
message: '<form class=\'form-horizontal\'>' +
' <div class=\'form-group\'>' +
' <label for=\'email\' class=\'col-sm-4 control-label\'>Adresse email</label>' +
' <div class=\'col-sm-8\'>' +
' <input type=\'text\' class=\'form-control\' id=\'email\' ng-model=\'email\' placeholder=\'Nom d\\\'utilisateur\'>' +
' </div>' +
' </div>' +
' <div class=\'form-group\'>' +
' <label for=\'password\' class=\'col-sm-4 control-label\'>Mot de passe</label>' +
' <div class=\'col-sm-8\'>' +
' <input type=\'password\' class=\'form-control\' id=\'password\' ng-model=\'password\' placeholder=\'Mot de passe\'>' +
' </div>' +
' </div>' +
' </div>' +
'</form>',
buttons: {
login: {
label: 'Login',
className: 'btn-primary',
callback: function(result) {
$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(result) {
authService.loginCancelled(null, 'Login cancelled by user action.');
$scope.dialogShown = false;
}
}
}
});
};
$rootScope.$on('event:auth-loginRequired', $scope.showLoginForm);
}])
;