Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
c449ac80b48405f72fd700d5cd406397b3a17d8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
;(function(){
	_App.Template = function()
	{
		this.initialize();
	};

	_App.Template.prototype =
	{

		tempStore       : {},
		templateStore   : {},
		instanceCounter : 0,

		initialize : function()
		{
			// Nothing to do.
		},

		/**
		 * Retrieve a template from the server.
		 * @param type
		 * @param name
		 * @param lifespan
		 */
		retrieve : function( type, name, lifespan, callback )
		{
			if (!this.templateStore[name])
			{
				this.templateStore[name] = {};
				this.tempStore[name]     = {};
				this.tempStore[name]["callback"] = callback;
				var rnd = new Date().getTime();
				App.Ajax.GET({
					id      : name,
		            url     : 'templates/' + type + '/' + name + '.html?t=' + rnd
		          }, $.proxy( this._retrievedHtml, this ));
				App.Ajax.SCRIPT({
					id      : name,
		            url     : 'templates/' + type + '/' + name + '.js?t=' + rnd
		          }, $.proxy( this._retrievedJs, this ));
			}
			else
			{
				if (undefined != callback && 'function' == typeof callback)
				{
					callback(name);
				}
			}
		},

		_retrievedJs : function ( name, data)
		{
			this.tempStore[name]['class'] = "template_" + name;
			if (this.tempStore[name]['html'])
			{
				this.templateStore[name] = new window[this.tempStore[name]['class']](
						this.tempStore[name]['html']
				);
				if (this.tempStore[name]["callback"])
				{
					this.tempStore[name]["callback"](name);
				}
				delete this.tempStore[name];
				//App.Event.trigger('Template.Retrieved:' + name, {"name": name});
			}
		},

		_retrievedHtml : function ( name, data)
		{
			this.tempStore[name]['html'] = data;
			if (this.tempStore[name]['class'])
			{
				this.templateStore[name] = new window[this.tempStore[name]['class']](
						this.tempStore[name]['html']
				);
				if (this.tempStore[name]["callback"])
				{
					this.tempStore[name]["callback"](name);
				}
				delete this.tempStore[name];
				//App.Event.trigger('Template.Retrieved:' + name, {"name": name});
			}
		},

		/**
		 * Register a new template instance.
		 * Template must already have been retrieved from server.
		 * @param id
		 * @param name
		 * @param target
		 * @param data
		 * @returns
		 */
		register : function( id, type, name, target, data, callback )
		{
			_t[id] = new this._templateInstance(
					this.instanceCounter,
					null,
					target, data
			);
	    	App.Event.trigger(
					'Template.Ready:' + name,
					{"id": name, "pageName": name}
			);
			if (!this.templateStore[name])
			{
				this.retrieve(type, name, true, $.proxy( this._hydrateTemplate, this, callback, id ));
			}
			else
			{
				_t[id].setTemplate(this.templateStore[name]);
				callback(id, name);
			}
			this.instanceCounter++;
			return _t[id];
		},

		_hydrateTemplate : function ( callback, id, name )
		{
			_t[id].setTemplate(this.templateStore[name]);
			callback(id, name);
		},

		_templateInstance : function( tid, template, target, data )
		{
			this.tid       = tid;
			this.template  = template;
			this.target    = target;
			this.data      = null;
			this.published = false;
			this.autoPublish = [];
			if (this.template && this.template.static)
			{
				this.construct = this.template.static.replaceAll('[tid]', this.tid);
				if (undefined != this.template.init)
				{
					this.template.init(this);
				}
			}

			/**
			 * Do something when we publish.
			 */
			this.onPublish = function ( callback )
			{
				if (!this.published)
				{
					this.autoPublish.push(callback);
				}
				else
				{
					callback();
				}
			};

			/**
			 * Set the template to work with for this instance.
			 * @param template
			 */
			this.setTemplate = function ( template )
			{
				this.template  = template;
				this.construct = this.template.static
					? this.template.static.replaceAll('[tid]', this.tid)
					: '';
				if (undefined != this.template.init)
				{
					this.template.init(this);
				}
				if (this.data)
				{
					this.hydrate(this.data);
				}
			};

			/**
			 * Hydrate template with dataset.
			 * This can be called before and after template publication.
			 * @param data
			 */
			this.hydrate = function( data )
			{
				this.data = data;
				if (this.template)
				{
					for (var element in this.template.elements)
					{
						var elem = this.template.elements[element];
						value = (data[element])
							? data[element]
							: null;
						if (this.published)
						{
							elem.hydrateLive(this.tid, value);
						}
						else
						{
							this.construct = elem.hydrate(this.construct, value);
						}
					}
					if (this.published)
					{
						$('.selectpicker').selectpicker();
					}
				}
			};
			if (this.template && this.data)
			{
				this.hydrate(this.data);
			}

			this.hydratePartial = function( data )
			{
				if (!this.data)
				{
					this.data = {};
				}
				if (this.template && this.template.elements)
				{
					for (var element in data)
					{
						if (this.template.elements[element])
						{
							var elem = this.template.elements[element];
							value = (data[element])
								? data[element]
								: null;
							this.data[element] = value;
							if (this.published)
							{
								elem.hydrateLive(this.tid, value);
							}
							else
							{
								this.construct = elem.hydrate(this.construct, value);
							}
						}
					}
					if (this.published)
					{
						$('.selectpicker').selectpicker();
					}
				}
			};

			/**
			 * Hydrate template with a single parameter.
			 * This can be called before and after template publication.
			 * @param param
			 * @param value
			 */
			this.hydrateParam = function( param, value )
			{
				if (!this.data)
				{
					this.data = {};
				}
				this.data[param] = value;
				if (this.template && this.template.elements && this.template.elements[param])
				{
					var elem = this.template.elements[param];
					if (this.published)
					{
						elem.hydrateLive(this.tid, value);
						$('.selectpicker').selectpicker();
					}
					else
					{
						this.construct = elem.hydrate(this.construct, value);
					}
				}
			};

			/**
			 * Publish template to registered target.
			 */
			this.publish = function()
			{
				$('#' + this.target).html(
						'<div id="' + this.tid + '">' + this.construct + '</div>'
						);
				this.published = true;
				$('.selectpicker').selectpicker();
				for (var element in this.template.elements)
				{
					this.template.elements[element].publish(this.tid);
				}
				if (this.template.construct)
				{
					this.template.construct();
				}
				for (var i in this.autoPublish)
				{
					this.autoPublish[i]();
				}
				this.autoPublish = [];
			};

			/**
			 * Harvest dataset from published template.
			 * @returns {___anonymous3461_3462}
			 */
			this.harvest = function()
			{
				var newData = {};
				for (var element in this.template.elements)
				{
					newData[element] = this.template.elements[element].harvest(this.tid);
				}
				return newData;
			};

			/**
			 * Unpublish template.
			 * Template can be re-published afterward.
			 */
			this.remove = function()
			{
				if (this.template)
				{
					this.template.destruct();
				}
				$('#' + this.tid).remove();
				this.published = false;
			};
		}

	};

})();

Commits for namibia/public/js/app/template.js

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit