382c0a38a218a5dd41f730b86499c7724de39d31
[src/app-framework-demo.git] / afb-client / app / Frontend / etc / AppConfig.js
1 (function () {
2     'use strict';
3
4     // _all modules only reference dependencies
5     angular.module('AppConfig', [])
6
7             // Factory is a singleton and share its context within all instances.
8             .factory('AppConfig', function (urlquery) {
9
10                 var myConfig = {
11                     paths: { // Warning paths should end with /
12                         image : 'images/',
13                         avatar: 'images/avatars/'
14                     },
15                                         
16                     session: { // Those data are updated by session service
17                        initial : urlquery.token || '123456789',  // typical dev initial token
18                        timeout : 3600,         // timeout is updated client sessin context creation
19                        pingrate: 30,           // Ping rate to check if server is still alive
20                        uuid    : '',           // uuid map with cookie or long term session access key
21                        token   : ''            // will be returned from authentication    
22                     }
23                 };
24
25                 return myConfig;
26             })
27             
28             // Factory is a singleton and share its context within all instances.
29             .factory('AppCall', function ($http, AppConfig, $log) {
30                 
31                 var myCalls = {
32                     get : function(plugin, action, query, cbresponse, cberror) {                                                
33                         
34                         var onerror = function(response) {
35                             if (cberror) cberror(response.data, response.status, response.config);
36                             else cbresponse(response.data, response.status, response.config);
37                         };
38                         
39                         var onsuccess =function(response) {
40                             if (!response.data || !response.data.request) {
41                                 onerror (response);
42                                 return;
43                             }                            
44                         
45                             var request=response.data.request;
46                             
47                             // if token was updated keep it within application cache
48                             if (request.token) AppConfig.session.token = request.token;
49                             if (request.uuid)  AppConfig.session.uuid  = request.uuid;
50                             if (request.timeout) AppConfig.session.timeout = request.timeout;
51                         
52                             cbresponse(response.data, response.status, response.config);
53                         };
54                         
55                         
56                         if (!query.token) query.token = AppConfig.session.token; // add token to provided query
57                         if (!query.reqid) query.reqid = action; // use action as default requestID
58                         var handle= $http.get('/api/' + plugin + '/' + action , {params: query}).then(onsuccess, onerror);
59                         
60                     }
61                 };
62                 return myCalls;
63             });
64
65 })();