// 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'])
+angular.module('HomeModule', ['SubmitButton', 'TokenRefresh','ModalNotification'])
- .controller('HomeController', function ($http, AppConfig) {
+ .controller('HomeController', function (AppCall, Notification) {
var scope = this; // I hate JavaScript
scope.uuid ="none";
scope.token ="none";
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;
+ scope.OnResponse= function(jresp, errcode) {
- // 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";
+ // Update UI response global display zone
+ scope.status = jresp.request.status;
+ scope.errcode = errcode;
+ scope.request = jresp.request;
+ scope.response = jresp.response;
- // If we have a new token let's update it
- if (data.request.token) scope.token=data.request.token;
+ if (jresp.request.status !== "success") {
+ Notification.error ({message: "Invalid API call:" + jresp.request.info , delay: 5000});
+ scope.class [jresp.request.reqid]="fail";
+ return;
+ }
- console.log ("OK: "+ JSON.stringify(data));
+ switch (jresp.request.reqid) {
+ case 'login':
+ case 'logout':
+ scope.class={};
+ break;
+
+ case 'refresh':
+ case 'check':
+ break;
+
+ default:
+ Notification.error ({message: "Invalid RequestID:" + jresp.request.reqid , delay: 5000});
+ return;
+ }
+
+ // update button classes within home.html
+ scope.class [jresp.request.reqid]="success";
+ console.log ("OK: "+ JSON.stringify(jresp));
};
- scope.ProcessError= function(data, errcode, headers, config) {
- var apiname= 'API'+data.request.api.replace('-','_');
+ scope.ProcessError= function(response, errcode, config) {
+ Notification.error ({message: "Invalid API:" + response.request.reqid , delay: 5000});
scope.status = "err-fx";
scope.errcode = errcode;
- scope.request = data.request;
- scope.response = "";
- scope[apiname]="fail";
-
- console.log ("FX: "+ JSON.stringify(data));
+ scope.request = response.request;
+ scope.response = "";
+ console.log ("FX: "+ JSON.stringify(response));
};
- 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.LoginClient = function() {
+ console.log ("LoginClient");
+ AppCall.get ("auth", "login", {/*query*/}, scope.OnResponse, scope.InvalidApiCall);
};
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);
+ console.log ("CheckSession");
+ AppCall.get ("auth", "check", {/*query*/}, scope.OnResponse, scope.InvalidApiCall);
+
};
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);
+ AppCall.get ("auth", "refresh", {/*query*/}, scope.OnResponse, scope.InvalidApiCall);
};
- 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);
+ scope.LogoutClient = function() {
+ console.log ("LogoutClient");
+ AppCall.get ("auth", "logout", {/*query*/}, scope.OnResponse, scope.InvalidApiCall);
};
+ scope.Initialised = function () {
+ scope.class = {login: "success"};
+ }
+
});
console.log ("SampleControler Loaded");