debugging click to refresh on live
[namibia] / public / js / app / data-store.js
1 ;(function(){
2
3         _App.DataStore = function()
4         {
5                 this.initialize();
6         };
7
8         _App.DataStore.prototype =
9         {
10
11                 bulkRequestId   : 1,
12                 metaStore       : {},
13                 dataStore       : {},
14                 itemStore       : {},
15                 listenerStore   : {},
16                 reverse         : {'years': true},
17
18                 initialize : function()
19                 {
20                         this.listenerStore = {};
21                 },
22
23                 /**
24                  * Set param for storage.
25                  * @param param
26                  * @param value
27                  */
28                 setItem : function( param, value )
29                 {
30                         this.itemStore[param] = value;
31                 },
32
33                 /**
34                  * Retrieve param from storage
35                  * @param param
36                  * @param defaultValue
37                  * @returns
38                  */
39                 getItem : function( param, defaultValue )
40                 {
41                         return this.itemStore[param]
42                                 ? this.itemStore[param]
43                                 : defaultValue;
44                 },
45
46                 /**
47                  * Remove param from storage
48                  * @param param
49                  */
50                 removeItem : function( param )
51                 {
52                         if (this.itemStore[param])
53                         {
54                                 delete this.itemStore[param];
55                         }
56                 },
57
58                 /**
59                  * Register an Event Listener.
60                  * @param id
61                  * @param dataId
62                  * @param callback
63                  * @param type
64                  */
65                 listen : function( id, dataId, callback, type )
66                 {
67                         if (!this.listenerStore[dataId])
68                         {
69                                 this.listenerStore[dataId] = {};
70                         }
71                         this.listenerStore[dataId][id] = {
72                                         "callback": callback,
73                                         "type": (type ? type : "UseOnce")
74                         };
75                         if (this.dataStore[dataId])
76                         {
77                                 this.listenerStore[dataId][id]["callback"](dataId, this.dataStore[dataId]);
78                         }
79                 },
80
81                 /**
82                  * Remove a registered Event Listener.
83                  * @param id
84                  * @param dataId
85                  */
86                 removeListener : function( id, dataId )
87                 {
88                         if (this.listenerStore[dataId] && this.listenerStore[dataId][id])
89                         {
90                                 delete this.listenerStore[dataId][id];
91                         }
92                 },
93
94                 /**
95                  * Set dataset.
96                  * @param dataId
97                  * @param data
98                  */
99                 setData : function( dataId, data, noSwap )
100                 {
101                         if (!noSwap && this.reverse[dataId])
102                         {
103                                 data.reverse();
104                         }
105                         this.dataStore[dataId] = data;
106                         if (this.listenerStore[dataId])
107                         {
108                                 for (var id in this.listenerStore[dataId])
109                                 {
110                                         this.listenerStore[dataId][id]["callback"](dataId, data);
111                                         if ("UseOnce" == this.listenerStore[dataId][id]["type"])
112                                         {
113                                                 delete this.listenerStore[dataId][id];
114                                         }
115                                 }
116                         }
117                 },
118
119                 /**
120                  * Retrieve dataset.
121                  * @param dataId
122                  */
123                 getData : function ( dataId )
124                 {
125                         return undefined != this.dataStore[dataId]
126                                 ? this.dataStore[dataId]
127                                 : [];
128                 },
129
130                 /**
131                  * Extract data from api response and set to dataStore.
132                  * @param dataId
133                  * @param apiResponse
134                  */
135                 setDataFromApiResponse : function( dataId, callback, apiResponse )
136                 {
137                         this.setData(dataId,
138                                         (apiResponse.Data.DataSet)
139                                                 ? apiResponse.Data.DataSet
140                                                 : apiResponse.Data
141                                         );
142                         if (undefined != callback)
143                         {
144                                 callback();
145                         }
146                 },
147
148                 /**
149                  * Extract data from api response and set to dataStore.
150                  * @param dataId
151                  * @param apiResponse
152                  */
153                 setDataGroupFromApiResponse : function( requests, callback, responses )
154                 {
155                         for (var i in requests)
156                         {
157                                 this.setData(requests[i].dataId,
158                                                 (responses[i].Data.DataSet)
159                                                         ? responses[i].Data.DataSet
160                                                         : responses[i].Data
161                                 );
162                                 if (undefined != requests[i].callback)
163                                 {
164                                         requests[i].callback();
165                                 }
166                         }
167                         if (undefined != callback)
168                         {
169                                 callback();
170                         }
171                 },
172
173                 /**
174                  * Load a group of select lists.
175                  * @param requests
176                  * @param callback
177                  */
178                 loadSelectListGroup : function( requests, callback )
179                 {
180                         var contracts = [];
181                         var req = [];
182                         for (var i in requests)
183                         {
184                                 if (requests[i].isStatic && App.DataStore.dataStore[requests[i].dataId])
185                                 {
186                                         App.DataStore.setData(requests[i].dataId, App.DataStore.dataStore[requests[i].dataId], true);
187                                         continue;
188                                 }
189                                 req.push(requests[i]);
190                                 App.DataStore.metaStore[requests[i].dataId] = {};
191                                 App.DataStore.metaStore[requests[i].dataId].data = requests[i].data
192                                         ? requests[i].data
193                                         : {};
194                                 App.DataStore.metaStore[requests[i].dataId].options = requests[i].options
195                                         ? requests[i].options
196                                         : {};
197                                 contracts.push({
198                                         id: 'DataStore.' + requests[i].dataId,
199                                         workspace: requests[i].workspace,
200                                         task: requests[i].task,
201                                         jobId: requests[i].jobId,
202                                         data: requests[i].data,
203                                         callback: requests[i].callback,
204                                         errorCallback: requests[i].errorCallback
205                                 });
206                         }
207                         if (0 < req.length)
208                         {
209                                 App.API.getTasks(
210                                                 contracts,
211                                                 $.proxy(App.DataStore._loadDataGroup, this, req, callback)
212                                 );
213                         }
214                 },
215
216                 _loadDataGroup : function( requests, callback )
217                 {
218                         var contracts = [];
219                         for (var i in requests)
220                         {
221                                 contracts.push({
222                                         id: 'DataStore.' + requests[i].dataId,
223                                         data: App.DataStore.metaStore[requests[i].dataId].data,
224                                         options: App.DataStore.metaStore[requests[i].dataId].options
225                                 });
226                         }
227                         this.bulkRequestId++;
228                         App.API.execTasks(
229                                         contracts,
230                                         $.proxy(App.DataStore.setDataGroupFromApiResponse, this, requests, callback)
231                         );
232                 },
233
234                 /**
235                  * Load a select list dataset via api.
236                  * @param dataId
237                  * @param isStatic
238                  * @param workspace
239                  * @param task
240                  * @param jobId
241                  * @param data
242                  * @param options
243                  */
244                 loadSelectListData : function( dataId, isStatic, workspace, task, jobId, data, options, callback )
245                 {
246                         if (isStatic && App.DataStore.dataStore[dataId])
247                         {
248                                 App.DataStore.setData(dataId, App.DataStore.dataStore[dataId], true);
249                                 return;
250                         }
251                         App.DataStore.metaStore[dataId] = {};
252                         App.DataStore.metaStore[dataId].data = data
253                                 ? data
254                                 : {};
255                         App.DataStore.metaStore[dataId].options = options
256                                 ? options
257                                 : {};
258
259                         var taskContract = App.API.taskContract('DataStore.' + dataId);
260                         if (null == taskContract
261                                 || undefined == taskContract.Hash
262                                 || null == taskContract.Hash)
263                         {
264                                 App.API.getTask(
265                                                 'DataStore.' + dataId, workspace, task, jobId,
266                                                 {},
267                                                 $.proxy(App.DataStore._loadData, this, dataId, isStatic, callback),
268                                                 _w.taskContractError
269                                                 );
270                         }
271                         else
272                         {
273                                 App.DataStore._loadData(dataId, isStatic, callback);
274                         }
275                 },
276
277                 _loadData : function( dataId, isStatic, callback )
278                 {
279                         App.API.execTask(
280                                         'DataStore.' + dataId,
281                                         App.DataStore.metaStore[dataId].data,
282                                         App.DataStore.metaStore[dataId].options,
283                                         $.proxy(App.DataStore.setDataFromApiResponse, this, dataId, callback),
284                                         _w.taskExecError
285                                         );
286                 }
287
288         };
289
290 })();