summaryrefslogtreecommitdiff
path: root/docroot/classes/table.class.js
blob: 1784c5ca1bbd659194df8e6d011417b80e791aea (plain)
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
/*
 * Material Experience - Card Table Class
 * 
 * EYEMG - Interactive Media Group
 * Created by Mike Crute (mcrute@eyemg.com)
 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
 */

var CardTable = Class.create();

Object.extend(CardTable.prototype, 
{
	/*
	 * Create the table object.
	 */
	initialize: function() 
	{
		this.options = Object.extend(
		{
			name:     'New Table',			// Table Name
			color:    SME.colors.blue,		// Table Color
			id:       null,				// Table ID
			decorate: true,                         // Draw table decorator
			onRender: Prototype.emptyFunction	// Callback function when the table renders
		}, arguments[0] || {});
		
		// Cache of the chips on the table
		this.chips = [];
		
		// Cache of table JSON data
		this.data  = null;
		
		this._createTable();
		
		// Initially resizes the window to the correct height, and keep
		// resizing it as the window changes
		this.resize();
		Event.observe(window, 'resize', function() 
		{ 
			this.resize(); 
		}.bindAsEventListener(this));
		
		if (this.options.decorate)
		{
			this._createAppDecorator();
		}
	},
	
	/*
	 * Create the table DOM object.
	 */
	_createTable: function() 
	{
		this.table = document.createElement('div');
		
		if (this.options.id)
		{
			this.table.id = this.options.id;
		}
		
		this.table.className = 'table';
		document.body.appendChild(this.table);
		
		this.options.onRender(this);	
	},
	
	/*
	 * Load the chip data from the server and cache it in the object.
	 */
	loadChipData: function(feedURL, urlParams, showAfterLoad) 
	{
		new Ajax.Request(feedURL.evaluate(urlParams), 
		{
			method: 'get', 
			
			onSuccess: function(transport) 
			{
				this.data = transport.responseText.evalJSON();

				if (showAfterLoad || this.options.id == SME.currentTable) 
					this.showChips();
			}.bind(this)
		});
	},
	
	/*
	 * Creates the decorator icon show in the footer of the user interface 
	 * which just links to a table hash and relies on the history manager 
	 * to load the appropriate table.
	 */
	_createAppDecorator: function() 
	{
		var link = Builder.node('a', 
		{ 
			href:  '#table' + this.options.id, 
			style: 'color: ' + this.options.color,
			alt:   this.options.name + ' Table',
			title: this.options.name + ' Table'
		}, 
		[
			Builder.node('img', 
			{ 
				className: 'appDecorator', 
				src:       'images/pill.gif', 
				style:     'background: ' + this.options.color 
			}),
			
			this.options.name.toUpperCase()
		]);

		$('footer').appendChild(link);
	},
	
	/*
	 * Show chips on the table once data is loaded. This function creates 
	 * and caches chip objects on the table.
	 */
	showChips: function() 
	{
		// Only load the chips once
		if (this.chipsDone || !this.data) 
		{
			return;
		}
		else
		{
			this.chipsDone = true;
		}

		this.bez = new Bezel(
		{ 
			onShow: function() 
			{
				this.data.each(function(chip) 
				{
					this.addChip(chip);
				}.bind(this));

				this.chips.each(function(chip) 
				{
					chip.animate();
				});
			}.bind(this)
		}).show(Strings.loadingNoAnim);
	},
	
	/*
	 * Remove a chip from the table.
	 */
	removeChip: function(chipContID) 
	{	
		this.chips.each(function(chip)
		{
			if (chip.options.contID == chipContID)
			{
				chip.hide();
				this.chips = this.chips.without(chip);
			}
		}.bind(this))
	},
	
	/*
	 * Add a chip to the table.
	 */
	addChip: function(data) 
	{
		var cords = Object.extend(
		{
			x:       data.x,
			y:       data.y,
			animate: true
		}, arguments[1] || {});
		
		var chip = new CardChip(
		{
			x:         cords.x,
			y:         cords.y,
			category:  data.category,
			contID:    data.cid,
			title:     data.title,
			image:     data.chip,
			locked:    data.locked || false,
			animate:   cords.animate
		});
		
		chip.addTo(this.table);
		this.chips.push(chip);
	},
	
	/*
	 * Clear out the chip cache. This function is not to be used within
	 * the code. It exists to be called manually on the command line for
	 * debugging purposes.
	 */
	_blowChips: function() 
	{
		this.chips = [];
	},
	
	/*
	 * Serialize all the chip data on the table to an object, presumably
	 * to be converted to JSON later. The object should match the output
	 * of card_table.pl
	 */
	getChipData: function() 
	{
		var data = [];
		
		this.chips.each(function(c)
		{
			data.push(c._serialize());
		});
		
		return data;
	},
	
	/*
	 * Tests if the table contains a chip with the particular content ID.
	 */
	hasChip: function(chipID) 
	{
		var chip = this.chips.find(function(item) 
		{
			if (item.options.contID == chipID) 
				return true;
				
			return false;
		});
		
		return chip ? true : false;
	},
	
	/*
	 * Changes the size of the table when the browser window is resized.
	 */
	resize: function() 
	{
		this.table.style.height = ($('footer').offsetTop - 
						$('footer').offsetHeight) + 'px';
	},
	
	/*
	 * Shows the table.
	 */
	show: function() 
	{
		this.showChips();
		SME.currentTable = this.options.id;
		this.table.style.display = 'block';
	},
	
	/*
	 * Hides the table.
	 */
	hide: function() 
	{
		this.table.style.display = 'none';
	}
});

Object.extend(CardTable, 
{
	/*
	 * Show a table and hide all others. Not the special exception for the
	 * sketchbook.
	 */
	showTable: function(table) 
	{
		SME.tables.each(function(item) 
		{
			if (item.options.id == table) 
			{
				item.show();
			} 
			else 
			{
				item.hide();
			}
		});
		
		if (table == 'sketchbook') 
		{
			SME.sketchbook.show();
		} 
		else 
		{
			SME.sketchbook.hide();
		}
	},
	
	/*
	 * Return the color as a string for a table based on table ID. Note
	 * the special exception for special case cards which are not associated
	 * with a particular table.
	 */
	tableColor: function(table) 
	{
		if (table == 'special')
		{
			return SME.colors.grey;
		}
	
		var color = SME.tables.find(function(item) 
		{
			if (item.options.id == table) 
			{
				return true;
			}
				
			return false;
		}).options.color;
		
		return color;
	}
});