debug click to refresh
[namibia] / public / js / app / template-element.js
1 ;(function(){
2         _App.TemplateElement = function()
3         {
4                 this.initialize();
5         };
6         _App.TemplateElement.prototype =
7         {
8
9                 initialize : function()
10                 {
11                         // Nothing to do.
12                 },
13
14                 /**
15                  * Create a new Text element for a template.
16                  * Example: var name = new App.TemplateElement.Text('name');
17                  * @param id
18                  */
19                 Text : function ( id )
20                 {
21                         this.id  = id;
22
23                         this.hydrate = function( template, value )
24                         {
25                                 value = (null == value)
26                                         ? ' '
27                                         : value;
28                                 return template.replace('[' + this.id + ']', value);
29                         };
30
31                         this.hydrateLive = function( tid, value )
32                         {
33                                 $('#' + tid + '_' + this.id).html(value);
34                         };
35
36                         this.publish = function( tid )
37                         {
38                                 // element bindings and such
39                         };
40
41                         this.harvest = function( tid )
42                         {
43                                 return $('#' + tid + '_' + this.id).html();
44                         };
45                 },
46
47                 LabeledText : function ( id )
48                 {
49                         this.id  = id;
50
51                         this.hydrate = function( template, meta )
52                         {
53                                 var value = (null == meta || null == meta.value)
54                                         ? ' '
55                                         : meta.value;
56                                 var title = (null == meta || null == meta.title)
57                                         ? ' '
58                                         : meta.title;
59                                 var labelStyle = (null == meta || null == meta.labelStyle)
60                                         ? 'valuation-line'
61                                         : meta.labelStyle;
62                                 var style = (null == meta || null == meta.style)
63                                         ? 'information-lines'
64                                         : meta.style;
65                                 var html = '<label class="control-label ' + labelStyle + '">' + title + ':</label>';
66                                 html += '<div class="controls">';
67                                 html += '<p class="' + style + '" id="' + this.id + '">' + value + '</p>';
68                                 html += '</div>';
69                                 return template.replace('[' + this.id + ']', html);
70                         };
71
72                         this.hydrateLive = function( tid, meta )
73                         {
74                                 var value = (undefined == meta || null == meta.value)
75                                         ? '&nbsp;'
76                                         : meta.value;
77                                 var title = (undefined == meta || null == meta.title)
78                                         ? '&nbsp;'
79                                         : meta.title;
80                                 var labelStyle = (null == meta || null == meta.labelStyle)
81                                 ? 'valuation-line'
82                                 : meta.labelStyle;
83                         var style = (null == meta || null == meta.style)
84                                 ? 'information-lines'
85                                 : meta.style;
86                                 var html = '<label class="control-label ' + labelStyle + '">' + title + ':</label>';
87                                 html += '<div class="controls">';
88                                 html += '<p class="' + style + '" id="' + this.id + '">' + value + '</p>';
89                                 html += '</div>';
90                                 $('#' + tid + '_' + this.id).html(html);
91                         };
92
93                         this.publish = function( tid )
94                         {
95                                 // element bindings and such
96                         };
97
98                         this.harvest = function( tid )
99                         {
100                                 return $('#' + this.id).html();
101                         };
102                 },
103
104                 ComponentCollection : function ( id )
105                 {
106                         this.id  = id;
107                         this.meta = {};
108
109                         this.hydrate = function( template, value )
110                         {
111                                 this.meta = (null == value)
112                                         ? '[...]'
113                                         : value;
114                                 var html = '';
115                                 for (var item in this.meta)
116                                 {
117                                         html += this.meta[item]['html'];
118                                 }
119                                 return template.replace('[' + this.id + ']', html);
120                         };
121
122                         this.hydrateLive = function( tid, value )
123                         {
124                                 this.meta = (null == value)
125                                         ? '[...]'
126                                         : value;
127                                 var html = '';
128                                 for (var item in this.meta)
129                                 {
130                                         if (this.meta[item]['html'])
131                                         {
132                                                 html += this.meta[item]['html'];
133                                         }
134                                 }
135                                 $('#' + tid + '_' + this.id).html(html);
136                                 for (var item in this.meta)
137                                 {
138                                         if (this.meta[item]['js'])
139                                         {
140                                                 this.meta[item]['js']();
141                                         }
142                                 }
143                         };
144
145                         this.publish = function( tid )
146                         {
147                                 for (var item in this.meta)
148                                 {
149                                         if (this.meta[item]['js'])
150                                         {
151                                                 this.meta[item]['js']();
152                                         }
153                                 }
154                         };
155
156                         this.harvest = function( tid )
157                         {
158                                 return null;
159                         };
160                 },
161
162                 ConstructorComponentCollection : function ( id )
163                 {
164                         this.id  = id;
165                         this.meta = {items:{}};
166                         this.itemMeta = null;
167
168                         this.hydrate = function( template, value )
169                         {
170                                 if (null == value)
171                                 {
172                                         return template;
173                                 }
174                                 this.meta = value;
175                                 var html = '';
176                                 for (var item in this.meta.items)
177                                 {
178                                         html += App.ElementLibrary[this.meta.constructor].html(this.meta.items[item]);
179                                 }
180                                 return template.replace('[' + this.id + ']', html);
181                         };
182
183                         this.hydrateLive = function( tid, value )
184                         {
185                                 if (null == value)
186                                 {
187                                         return;
188                                 }
189                                 this.meta = value;
190                                 var html = '';
191                                 if (this.meta.items)
192                                 {
193                                         for (var i in this.meta.items)
194                                         {
195                                                 html += App.ElementLibrary[this.meta.constructor].html(this.meta.items[i]);
196                                         }
197                                 }
198                                 $('#' + tid + '_' + this.id).html(html);
199                                 if (undefined == App.ElementLibrary[this.meta.constructor])
200                                 {
201                                         return;
202                                 }
203                                 if (App.ElementLibrary[this.meta.constructor].bind)
204                                 {
205                                         for (var item in this.meta.items)
206                                         {
207                                                 App.ElementLibrary[this.meta.constructor].bind(this.meta.items[item]);
208                                         }
209                                 }
210                         };
211
212                         this.publish = function( tid )
213                         {
214                                 if (!this.meta.constructor)
215                                 {
216                                         return;
217                                 }
218                                 if (undefined == App.ElementLibrary[this.meta.constructor])
219                                 {
220                                         //console.log('NO SUCH CONSTRUCTOR: ' + this.meta.constructor);
221                                         return;
222                                 }
223                                 if (App.ElementLibrary[this.meta.constructor].bind)
224                                 {
225                                         for (var item in this.meta.items)
226                                         {
227                                                 App.ElementLibrary[this.meta.constructor].bind(this.meta.items[item]);
228                                         }
229                                 }
230                         };
231
232                         this.harvest = function( tid )
233                         {
234                                 return null;
235                         };
236                 },
237
238                 FieldComponent : function ( id )
239                 {
240                         this.id   = id;
241                         this.meta = {};
242
243                         this.hydrate = function( template, value )
244                         {
245                                 this.meta = value;
246                                 value = '';
247                                 if (this.meta && this.meta.value)
248                                 {
249                                         value = this.meta.value;
250                                 }
251                                 var html = App.ElementLibrary.Field.html(this.meta, value);
252                                 return template.replace('[' + this.id + ']', html);
253                         };
254
255                         this.hydrateLive = function( tid, value )
256                         {
257                                 this.meta = value;
258                                 value = '';
259                                 if (this.meta && this.meta.value)
260                                 {
261                                         value = this.meta.value;
262                                 }
263                                 var html = App.ElementLibrary.Field.html(this.meta, value);
264                                 $('#' + tid + '_' + this.id).html(html);
265                                 if (this.meta)
266                                 {
267                                         App.ElementLibrary.Field.setValue( tid, this.meta, value);
268                                         App.ElementLibrary.Field.bind( tid, this.meta );
269                                 }
270                         };
271
272                         this.publish = function( tid )
273                         {
274                                 if (this.meta)
275                                 {
276                                         value = '';
277                                         if (this.meta && this.meta.value)
278                                         {
279                                                 value = this.meta.value;
280                                         }
281                                         App.ElementLibrary.Field.setValue( tid, this.meta, value);
282                                         App.ElementLibrary.Field.bind( tid, this.meta );
283                                 }
284                         };
285
286                         this.harvest = function( tid )
287                         {
288                                 return App.ElementLibrary.Field.harvest( tid, this.meta );
289                         };
290                 },
291
292                 GroupComponent : function ( id )
293                 {
294                         this.id   = id;
295                         this.meta = {};
296
297                         this.hydrate = function( template, value )
298                         {
299                                 this.meta = value;
300                                 value = '';
301                                 if (this.meta && this.meta.value)
302                                 {
303                                         value = this.meta.value;
304                                 }
305                                 var html = App.ElementLibrary.Field.html(this.meta, value);
306                                 return template.replace('[' + this.id + ']', html);
307                         };
308
309                         this.hydrateLive = function( tid, value )
310                         {
311                                 this.meta = value;
312                                 value = '';
313                                 if (this.meta && this.meta.value)
314                                 {
315                                         value = this.meta.value;
316                                 }
317                                 var html = App.ElementLibrary.Field.html(this.meta, value);
318                                 $('#' + tid + '_' + this.id).html(html);
319                                 if (this.meta)
320                                 {
321                                         App.ElementLibrary.Field.setValue( tid, this.meta, value);
322                                         App.ElementLibrary.Field.bind( tid, this.meta );
323                                 }
324                         };
325
326                         this.publish = function( tid )
327                         {
328                                 if (this.meta)
329                                 {
330                                         value = '';
331                                         if (this.meta && this.meta.value)
332                                         {
333                                                 value = this.meta.value;
334                                         }
335                                         App.ElementLibrary.Field.setValue( tid, this.meta, value);
336                                         App.ElementLibrary.Field.bind( tid, this.meta );
337                                 }
338                         };
339
340                         this.harvest = function( tid )
341                         {
342                                 return App.ElementLibrary.Field.harvest( tid, this.meta );
343                         };
344                 }
345
346
347         };
348 })();