summaryrefslogtreecommitdiffstats
path: root/afb-client/app/Frontend/pages/Home/HomeModule.js
blob: fb03ac8bff56617e88497e22441e5e3ad5fc0a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(function() {
'use strict';

// WARNING: make sure than app/frontend/services/AppConfig.js match your server

// list all rependencies within the page + controler if needed
angular.module('HomeModule', ['SubmitButton', 'TokenRefresh'])

  .controller('HomeController', function ($http, AppConfig) {
        var scope = this; // I hate JavaScript
        scope.uuid   ="none";
        scope.token  ="none";
        scope.session="none";
        scope.status ="err-no";

        console.log ("Home Controller");
        
        scope.ProcessResponse= function(data, errcode, headers, config) {
            var apiname= 'API'+ data.request.api.replace('-','_');
            scope.status = "err-ok";
            scope.errcode= errcode;
            scope.request  = data.request;
            scope.response = data.response;
            
            // if token was refresh let's update AppConfig
            if (data.request.token) AppConfig.session.token = data.request.token;
            if (data.request.uuid)  AppConfig.session.uuid  = data.request.uuid;
            if (data.request.timeout)  AppConfig.session.timeout  = data.request.timeout;

            // Make sure we clean everything when Open/Close is called
            if (apiname === "APIcreate" || apiname === "APIreset") {
                scope.APIreset  ='';
                scope.APIcreate ='';
                scope.APIrefresh='';
                scope.APIcheck  ='';
            }
            scope[apiname]="success";
            
            // If we have a new token let's update it
            if (data.request.token) scope.token=data.request.token;
            
            console.log ("OK: "+ JSON.stringify(data));
        };
        
        scope.ProcessError= function(data, errcode, headers, config) {
            var apiname= 'API'+data.request.api.replace('-','_');
            scope.status   = "err-fx";
            scope.errcode  = errcode;
            scope.request  = data.request;
            scope.response = "";
            scope[apiname]="fail";
            
            console.log ("FX: "+ JSON.stringify(data));
        };

        scope.OpenSession = function() {
            console.log ("OpenSession"); 
            var postdata= {/* any json your application may need */};
            var handler = $http.post(AppConfig.session.create + '?token='+AppConfig.session.initial, postdata);
            
            handler.success(scope.ProcessResponse);
            handler.error(scope.ProcessError);
        };        

        scope.CheckSession = function() {
            console.log ("CloseSession");
            var postdata= {/* any json your application may need */};
            var handler = $http.post(AppConfig.session.check + '?token='+AppConfig.session.token, postdata);
            
            handler.success(scope.ProcessResponse);
            handler.error(scope.ProcessError);
        };
        
        scope.RefreshSession = function() {
            console.log ("RefreshSession");
            var postdata= {/* any json your application may need */};
            var handler = $http.post(AppConfig.session.refresh + '?token='+AppConfig.session.token, postdata);
            
            handler.success(scope.ProcessResponse);
            handler.error(scope.ProcessError);
        };
        
        scope.ResetSession = function() {
            console.log ("ResetSession");
            var postdata= {/* any json your application may need */};
            var handler = $http.post(AppConfig.session.reset + '?token='+AppConfig.session.token, postdata);
            
            handler.success(scope.ProcessResponse);
            handler.error(scope.ProcessError);
        };
        
   });

console.log ("SampleControler Loaded");
})();