c8f1abef85d5bea21b3ee7811b0723463654adea
[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                         audio : 'images/audio/',
15                         appli : 'images/appli/'
16                     },
17                                         
18                     session: { // Those data are updated by session service
19                        initial : urlquery.token || '123456789',  // typical dev initial token
20                        timeout : 3600,         // timeout is updated client sessin context creation
21                        pingrate: 30,           // Ping rate to check if server is still alive
22                        uuid    : '',           // uuid map with cookie or long term session access key
23                        token   : ''            // will be returned from authentication    
24                     }
25                 };
26
27                 return myConfig;
28             })
29             
30             // Factory is a singleton and share its context within all instances.
31             .factory('AppCall', function ($http, AppConfig, $log) {
32                 
33                 var myCalls = {
34                     get : function(plugin, action, query, cbresponse, cberror) {                                                
35                         
36                         var onerror = function(response) {
37                             if (cberror) cberror(response.data, response.status, response.config);
38                             else cbresponse(response.data, response.status, response.config);
39                         };
40                         
41                         var onsuccess =function(response) {
42                             if (!response.data || !response.data.request) {
43                                 onerror (response);
44                                 return;
45                             }                            
46                         
47                             var request=response.data.request;
48                             
49                             // if token was updated keep it within application cache
50                             if (request.token) AppConfig.session.token = request.token;
51                             if (request.uuid)  AppConfig.session.uuid  = request.uuid;
52                             if (request.timeout) AppConfig.session.timeout = request.timeout;
53                         
54                             cbresponse(response.data, response.status, response.config);
55                         };
56                         
57                         
58                         if (!query.token) query.token = AppConfig.session.token; // add token to provided query
59                         if (!query.reqid) query.reqid = action; // use action as default requestID
60                         var handle= $http.get('/api/' + plugin + '/' + action , {params: query}).then(onsuccess, onerror);
61                         
62                     }
63                 };
64                 return myCalls;
65             });
66
67 })();