testing click to refresh issue
[namibia] / public / js / app / template.js
1 ;(function(){
2         _App.Template = function()
3         {
4                 this.initialize();
5         };
6
7         _App.Template.prototype =
8         {
9
10                 tempStore       : {},
11                 templateStore   : {},
12                 instanceCounter : 0,
13
14                 initialize : function()
15                 {
16                         // Nothing to do.
17                 },
18
19                 /**
20                  * Retrieve a template from the server.
21                  * @param type
22                  * @param name
23                  * @param lifespan
24                  */
25                 retrieve : function( type, name, lifespan, callback )
26                 {
27                         if (!this.templateStore[name])
28                         {
29                                 this.templateStore[name] = {};
30                                 this.tempStore[name]     = {};
31                                 this.tempStore[name]["callback"] = callback;
32                                 var rnd = new Date().getTime();
33                                 App.Ajax.GET({
34                                         id      : name,
35                             url     : 'templates/' + type + '/' + name + '.html?t=' + rnd
36                           }, $.proxy( this._retrievedHtml, this ));
37                                 App.Ajax.SCRIPT({
38                                         id      : name,
39                             url     : 'templates/' + type + '/' + name + '.js?t=' + rnd
40                           }, $.proxy( this._retrievedJs, this ));
41                         }
42                         else
43                         {
44                                 if (undefined != callback && 'function' == typeof callback)
45                                 {
46                                         callback(name);
47                                 }
48                         }
49                 },
50
51                 _retrievedJs : function ( name, data)
52                 {
53                         this.tempStore[name]['class'] = "template_" + name;
54                         if (this.tempStore[name]['html'])
55                         {
56                                 this.templateStore[name] = new window[this.tempStore[name]['class']](
57                                                 this.tempStore[name]['html']
58                                 );
59                                 if (this.tempStore[name]["callback"])
60                                 {
61                                         this.tempStore[name]["callback"](name);
62                                 }
63                                 delete this.tempStore[name];
64                                 //App.Event.trigger('Template.Retrieved:' + name, {"name": name});
65                         }
66                 },
67
68                 _retrievedHtml : function ( name, data)
69                 {
70                         this.tempStore[name]['html'] = data;
71                         if (this.tempStore[name]['class'])
72                         {
73                                 this.templateStore[name] = new window[this.tempStore[name]['class']](
74                                                 this.tempStore[name]['html']
75                                 );
76                                 if (this.tempStore[name]["callback"])
77                                 {
78                                         this.tempStore[name]["callback"](name);
79                                 }
80                                 delete this.tempStore[name];
81                                 //App.Event.trigger('Template.Retrieved:' + name, {"name": name});
82                         }
83                 },
84
85                 /**
86                  * Register a new template instance.
87                  * Template must already have been retrieved from server.
88                  * @param id
89                  * @param name
90                  * @param target
91                  * @param data
92                  * @returns
93                  */
94                 register : function( id, type, name, target, data, callback )
95                 {
96                         _t[id] = new this._templateInstance(
97                                         this.instanceCounter,
98                                         null,
99                                         target, data
100                         );
101                 App.Event.trigger(
102                                         'Template.Ready:' + name,
103                                         {"id": name, "pageName": name}
104                         );
105                         if (!this.templateStore[name])
106                         {
107                                 this.retrieve(type, name, true, $.proxy( this._hydrateTemplate, this, callback, id ));
108                         }
109                         else
110                         {
111                                 _t[id].setTemplate(this.templateStore[name]);
112                                 callback(id, name);
113                         }
114                         this.instanceCounter++;
115                         return _t[id];
116                 },
117
118                 _hydrateTemplate : function ( callback, id, name )
119                 {
120                         _t[id].setTemplate(this.templateStore[name]);
121                         callback(id, name);
122                 },
123
124                 _templateInstance : function( tid, template, target, data )
125                 {
126                         this.tid       = tid;
127                         this.template  = template;
128                         this.target    = target;
129                         this.data      = null;
130                         this.published = false;
131                         this.autoPublish = [];
132                         if (this.template && this.template.static)
133                         {
134                                 this.construct = this.template.static.replaceAll('[tid]', this.tid);
135                                 if (undefined != this.template.init)
136                                 {
137                                         this.template.init(this);
138                                 }
139                         }
140
141                         /**
142                          * Do something when we publish.
143                          */
144                         this.onPublish = function ( callback )
145                         {
146                                 if (!this.published)
147                                 {
148                                         this.autoPublish.push(callback);
149                                 }
150                                 else
151                                 {
152                                         callback();
153                                 }
154                         };
155
156                         /**
157                          * Set the template to work with for this instance.
158                          * @param template
159                          */
160                         this.setTemplate = function ( template )
161                         {
162                                 this.template  = template;
163                                 this.construct = this.template.static
164                                         ? this.template.static.replaceAll('[tid]', this.tid)
165                                         : '';
166                                 if (undefined != this.template.init)
167                                 {
168                                         this.template.init(this);
169                                 }
170                                 if (this.data)
171                                 {
172                                         this.hydrate(this.data);
173                                 }
174                         };
175
176                         /**
177                          * Hydrate template with dataset.
178                          * This can be called before and after template publication.
179                          * @param data
180                          */
181                         this.hydrate = function( data )
182                         {
183                                 this.data = data;
184                                 if (this.template)
185                                 {
186                                         for (var element in this.template.elements)
187                                         {
188                                                 var elem = this.template.elements[element];
189                                                 value = (data[element])
190                                                         ? data[element]
191                                                         : null;
192                                                 if (this.published)
193                                                 {
194                                                         elem.hydrateLive(this.tid, value);
195                                                 }
196                                                 else
197                                                 {
198                                                         this.construct = elem.hydrate(this.construct, value);
199                                                 }
200                                         }
201                                         if (this.published)
202                                         {
203                                                 $('.selectpicker').selectpicker();
204                                         }
205                                 }
206                         };
207                         if (this.template && this.data)
208                         {
209                                 this.hydrate(this.data);
210                         }
211
212                         this.hydratePartial = function( data )
213                         {
214                                 if (!this.data)
215                                 {
216                                         this.data = {};
217                                 }
218                                 if (this.template && this.template.elements)
219                                 {
220                                         for (var element in data)
221                                         {
222                                                 if (this.template.elements[element])
223                                                 {
224                                                         var elem = this.template.elements[element];
225                                                         value = (data[element])
226                                                                 ? data[element]
227                                                                 : null;
228                                                         this.data[element] = value;
229                                                         if (this.published)
230                                                         {
231                                                                 elem.hydrateLive(this.tid, value);
232                                                         }
233                                                         else
234                                                         {
235                                                                 this.construct = elem.hydrate(this.construct, value);
236                                                         }
237                                                 }
238                                         }
239                                         if (this.published)
240                                         {
241                                                 $('.selectpicker').selectpicker();
242                                         }
243                                 }
244                         };
245
246                         /**
247                          * Hydrate template with a single parameter.
248                          * This can be called before and after template publication.
249                          * @param param
250                          * @param value
251                          */
252                         this.hydrateParam = function( param, value )
253                         {
254                                 if (!this.data)
255                                 {
256                                         this.data = {};
257                                 }
258                                 this.data[param] = value;
259                                 if (this.template && this.template.elements && this.template.elements[param])
260                                 {
261                                         var elem = this.template.elements[param];
262                                         if (this.published)
263                                         {
264                                                 elem.hydrateLive(this.tid, value);
265                                                 $('.selectpicker').selectpicker();
266                                         }
267                                         else
268                                         {
269                                                 this.construct = elem.hydrate(this.construct, value);
270                                         }
271                                 }
272                         };
273
274                         /**
275                          * Publish template to registered target.
276                          */
277                         this.publish = function()
278                         {
279                                 $('#' + this.target).html(
280                                                 '<div id="' + this.tid + '">' + this.construct + '</div>'
281                                                 );
282                                 this.published = true;
283                                 $('.selectpicker').selectpicker();
284                                 for (var element in this.template.elements)
285                                 {
286                                         this.template.elements[element].publish(this.tid);
287                                 }
288                                 if (this.template.construct)
289                                 {
290                                         this.template.construct();
291                                 }
292                                 for (var i in this.autoPublish)
293                                 {
294                                         this.autoPublish[i]();
295                                 }
296                                 this.autoPublish = [];
297                         };
298
299                         /**
300                          * Harvest dataset from published template.
301                          * @returns {___anonymous3461_3462}
302                          */
303                         this.harvest = function()
304                         {
305                                 var newData = {};
306                                 for (var element in this.template.elements)
307                                 {
308                                         newData[element] = this.template.elements[element].harvest(this.tid);
309                                 }
310                                 return newData;
311                         };
312
313                         /**
314                          * Unpublish template.
315                          * Template can be re-published afterward.
316                          */
317                         this.remove = function()
318                         {
319                                 if (this.template)
320                                 {
321                                         this.template.destruct();
322                                 }
323                                 $('#' + this.tid).remove();
324                                 this.published = false;
325                         };
326                 }
327
328         };
329
330 })();