First version
[src/app-framework-demo.git] / afm-client / app / Frontend / pages / Home / DashboardModule.js
1 (function() {
2 'use strict';
3
4 // WARNING: make sure than app/frontend/services/AppConfig.js match your server
5
6 // list all rependencies within the page + controler if needed
7 angular.module('DashboardModule', ['SubmitButton', 'TokenRefresh', 'AppliButton'])
8
9   .controller('DashboardController', function ($http, AppConfig, Notification) {
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         scope.appliIDs =[]; // array to hold applications ID
16         scope.appliStore={}; // array to hold applications json description
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 AppConfig
26             if (data.request.token) AppConfig.session.token = data.request.token;
27             if (data.request.uuid)  AppConfig.session.uuid  = data.request.uuid;
28             if (data.request.timeout)  AppConfig.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 handler = $http.get(AppConfig.session.create + '?token='+AppConfig.session.initial);
59             
60             handler.success(scope.ProcessResponse);
61             handler.error(scope.ProcessError);
62         };        
63
64         scope.CheckSession = function() {
65             console.log ("CloseSession");
66             var handler = $http.get(AppConfig.session.check + '?token='+AppConfig.session.token);
67             
68             handler.success(scope.ProcessResponse);
69             handler.error(scope.ProcessError);
70         };
71         
72         scope.RefreshSession = function() {
73             console.log ("RefreshSession");
74             var handler = $http.get(AppConfig.session.refresh + '?token='+AppConfig.session.token);
75             
76             handler.success(scope.ProcessResponse);
77             handler.error(scope.ProcessError);
78         };
79         
80         scope.ResetSession = function() {
81             console.log ("ResetSession");
82             var handler = $http.get(AppConfig.session.reset + '?token='+AppConfig.session.token);
83             
84             handler.success(scope.ProcessResponse);
85             handler.error(scope.ProcessError);
86         };
87         
88         scope.AppliCB = function (appliID) {
89             console.log ("Application Clicked ID=[%s]", appliID);
90             
91         };
92         
93         scope.AutoStart = function () {
94             console.log ("AutoStart requesting Apps list");
95             var handler = $http.get('/api/afm-main/runnables' + '?token='+AppConfig.session.token);
96             handler.success(function(result) {
97                 
98                 // Check this is a valid response from Binder
99                 if (result.request.jtype !== "AJB_reply" && result.request.api !== "runnables") {
100                   Notification.error ({message: "Invalid Respond to /opa/afm-main/runnable result="+result, delay: 5000}); 
101                   return;
102                 }
103                 
104                 // loop on runnable application to prepare for display
105                 var  appliIDs=[];
106                 for (var idx=0; idx < result.response.length; idx ++) {
107                     appliIDs[idx] = result.response [idx].id;
108                     scope.appliStore [result.response [idx].id] =  result.response [idx];
109                 }
110                 scope.appliIDs = appliIDs; // avoid partial update to limit UI refresh
111                 
112             });
113         };
114           
115    });
116
117 console.log ("Dashboard Controller Loaded");
118 })();