Work in Progress
[src/app-framework-demo.git] / afb-client / app / Frontend / pages / Home / HomeModule.js
1 (function() {
2 'use strict';
3
4 // WARNING: make sure than app/frontend/services/ConfigApp.js match your server
5
6 // list all rependencies within the page + controler if needed
7 angular.module('HomeModule', ['SubmitButton', 'TokenRefresh'])
8
9   .controller('HomeController', function ($http, ConfigApp) {
10         var scope = this; // I hate JavaScript
11         scope.uuid   ="none";
12         scope.token  ="none";
13         scope.session="none";
14         scope.status ="err-no";
15
16         console.log ("Home Controller");
17         
18         scope.ProcessResponse= function(data, errcode, headers, config) {
19             var apiname= 'API'+ data.request.api.replace('-','_');
20             scope.status = "err-ok";
21             scope.errcode= errcode;
22             scope.request  = data.request;
23             scope.response = data.response;
24             
25             // if token was refresh let's update ConfigApp
26             if (data.request.token) ConfigApp.session.token = data.request.token;
27             if (data.request.uuid)  ConfigApp.session.uuid  = data.request.uuid;
28             if (data.request.timeout)  ConfigApp.session.timeout  = data.request.timeout;
29
30             // Make sure we clean everything when Open/Close is called
31             if (apiname === "APIcreate" || apiname === "APIreset") {
32                 scope["APIreset"]='';
33                 scope["APIcreate"]='';
34                 scope["APIrefresh"]='';
35                 scope["APIcheck"]='';
36             }
37             scope[apiname]="success";
38             
39             // If we have a new token let's update it
40             if (data.request.token) scope.token=data.request.token;
41             
42             console.log ("OK: "+ JSON.stringify(data));
43         };
44         
45         scope.ProcessError= function(data, errcode, headers, config) {
46             var apiname= 'API'+data.request.api.replace('-','_');
47             scope.status   = "err-fx";
48             scope.errcode  = errcode;
49             scope.request  = data.request;
50             scope.response = "";
51             scope[apiname]="fail";
52             
53             console.log ("FX: "+ JSON.stringify(data));
54         };
55
56         scope.OpenSession = function() {
57             console.log ("OpenSession"); 
58             var postdata= {/* any json your application may need */};
59             var handler = $http.post(ConfigApp.api.token + 'create?token='+ConfigApp.session.token, postdata);
60             
61             handler.success(scope.ProcessResponse);
62             handler.error(scope.ProcessError);
63         };        
64
65         scope.CheckSession = function() {
66             console.log ("CloseSession");
67             var postdata= {/* any json your application may need */};
68             var handler = $http.post(ConfigApp.api.token + 'check?token='+scope.token, postdata);
69             
70             handler.success(scope.ProcessResponse);
71             handler.error(scope.ProcessError);
72         };
73         
74         scope.RefreshSession = function() {
75             console.log ("RefreshSession");
76             var postdata= {/* any json your application may need */};
77             var handler = $http.post(ConfigApp.api.token + 'refresh?token='+scope.token, postdata);
78             
79             handler.success(scope.ProcessResponse);
80             handler.error(scope.ProcessError);
81         };
82         
83         scope.ResetSession = function() {
84             console.log ("ResetSession");
85             var postdata= {/* any json your application may need */};
86             var handler = $http.post(ConfigApp.api.token + 'reset?token='+scope.token, postdata);
87             
88             handler.success(scope.ProcessResponse);
89             handler.error(scope.ProcessError);
90         };
91         
92    });
93
94 console.log ("SampleControler Loaded");
95 })();