summaryrefslogtreecommitdiff
path: root/docroot/classes/card.class.js
blob: e13525905ece94de3b428e4dfac5df56a1ae80a2 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Material Experience - Card Class
 * 
 * EYEMG - Interactive Media Group
 * Created by Mike Crute (mcrute@eyemg.com)
 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
 */

var Card = Class.create();
Object.extend(Card.prototype, 
{
	/*
	 * Creates the card and appends it to the document. This does
	 * not set the content.
	 */
	initialize: function() 
	{
		this.options = Object.extend(
		{
			title:           'New Card',			// Card Title
			id:              'mycard',			// Card ID
			preview:         false,				// This is a preview card
			addExtraButtons: true,				// Add the extra buttons to non-system cards
			contID:          null,				// Content ID of the card's contents
			color:           SME.colors.blue,		// Card Color
			width:           SME.sizes.card.width,		// Card Width
			height:          SME.sizes.card.height,		// Card Height
			onClose:         Prototype.emptyFunction,	// Card Close Callback
			onFadeComplete:  Prototype.emptyFunction,	// Card Fade Completion Callback
			onFadeOutFinish: Prototype.emptyFunction	// Card Fade Out Completion Callback
		}, arguments[0] || {});
		
		// Keep track of our card chip
		this.chip = this.options.chip;
		
		// Buttons on the card frame
		this.buttons = [];

		// Create the document overlay but don't yet show it
		this.overlay = new Overlay(
		{
			onClick: function() 
			{
				this.hide(); 
			}.bind(this)
		});

		this._createCard();

		if (this.options.addExtraButtons)
		{
			this._addExtraButtons();
		}
		
		// Always add a close button
		this.addCommandButton(Strings.closeCard,'images/close.gif', function() 
		{ 
			this.hide();
		}.bind(this));
		
		this.setTitle(this.options.title);
	},
	
	/*
	 * Create the card DOM nodes.
	 */
	_createCard: function()
	{
		// Get the rounded corner generator and the coordinates of the page center
		var Rounder  = new RoundedCorners(this.options.color);
		var cardPos  = window.calcCordsToCenter(this.options.height, this.options.width);
		var slicebox = Rounder.get(Rounder.directions.top);
				
		// Intro card will flash across the screen while card builds but 
		// before fading up if display is not set to none
		this.card = Builder.node('div', 
		{ 
			id: this.options.id, 
			style: 'display: none;', 
			className: 'card' 
		});

		// Create the card header with title and button box
		slicebox.appendChild(this.title = Builder.node('span', { className: 'cardTitle' }));
		slicebox.appendChild(this.buttonbox = Builder.node('div', { className: 'buttonbox' }, 
			this.buttonLabel = Builder.node('span', { className: 'commandlabel' })
		));
		
		// Append the header and the card content area
		this.card.appendChild(slicebox);
		this.card.appendChild(Builder.node('div', { style: 'background: white; opacity: 100%;' }, 
			this.cframe = Builder.node('div')
		));
		
		// Append the bottom of the card frame and add the card to the document
		this.card.appendChild(Rounder.get(Rounder.directions.bottom));
		document.body.appendChild(this.card);
		
		// Card frame styles
		Element.extend(this.cframe).setStyle(
		{
			width:       this.options.width  - 11 + 'px',
			height:      this.options.height - 40 + 'px',
			borderLeft:  '5px solid '   + this.options.color,
			borderRight: '5px solid '   + this.options.color,
			borderTop:   '10px solid '  + this.options.color,
			position:    'relative',
			overflow:    'hidden'
		});
		
		// Card styles
		Element.extend(this.card).setStyle(
		{
			height:   this.options.height + 'px',
			width:    this.options.width  + 'px',
			top:      cardPos.top         + 'px',
			left:     cardPos.left        + 'px',
			zIndex:   9001,
			opacity:  '100%',
			display:  'none',
			position: 'absolute'
		});
		
		// Make the card draggable
		this.drag = new Draggable(this.card, 
		{ 
			handle: 'round', 
			zindex: 9001, 
			starteffect: null, 
			endeffect: null 
		});
	},

	/*
	 * Add a command button to the top right of the card.
	 */
	addCommandButton: function(title, icon, action) 
	{
		var newButton = Builder.node('img', { className: 'commandbtn', src: icon });
		
		// Show the label on mouseover
		Event.observe(newButton, 'mouseover', function() 
		{ 
			this.buttonLabel.innerHTML = title; 
		}.bindAsEventListener(this));
		
		// Hide the label on mouse out
		Event.observe(newButton, 'mouseout', function() 
		{ 
			this.buttonLabel.innerHTML = ''; 
		}.bindAsEventListener(this));
		
		// Take the action specified when the button is clicked
		Event.observe(newButton, 'click', function(event) 
		{ 
			action(event); 
		});
		
		// Add the button to the button box and cache it.
		this.buttons[title] = this.buttonbox.appendChild(newButton);
	},

	/*
	 * Removes a command button from the card.
	 */
	removeCommandButton: function(button) 
	{
		this.buttonLabel.innerHTML = '';
		this.buttonbox.removeChild(this.buttons[button]);
	},
	
	/*
	 * Sets the title of the card.
	 */
	setTitle: function(title) 
	{
		this.options.title   = title;
		this.title.innerHTML = title;
	},
	
	/*
	 * Disables the drag on the card and removes the card
	 * from the DOM.
	 */
	destroy: function() 
	{
		// Kill the drag to prevent probable memory leaks in IE
		this.drag.destroy();
		
		// Remove the card from the DOM
		document.body.removeChild(this.card);
		
		// Destroy the overlay
		this.overlay.destroy();
	},

	/*
	 * Display the card.
	 */
	show: function() 
	{
		// Track the currently active card for the history manager
		// FLAWED LOGIC: We can have multiple cards visible at the same
		// time.
		SME.currentCard = this;
		
		// Get card positioning data
		var cardPos  = window.calcCordsToCenter(this.options.height, this.options.width);
		var windSize = window.getDimensions();
		
		// Position the card
		this.card.setStyle(
		{
			top:  cardPos.top  + 'px',
			left: cardPos.left + 'px'
		});

		this._autoSetLayout();

		// Show the overlay first so it will fade in with the card
		this.overlay.show();
		
		// Fade the card in and run its fadeComplete function.
		new Effect.Appear(this.card, 
		{ 
			afterFinish: function() 
			{ 
				this.options.onFadeComplete();
			}.bind(this) 
		});
	},
	
	/*
	 * Automatically set the layout based on the content of a JSON feed
	 * if it is supplied.
	 */
	_autoSetLayout: function() 
	{
		// Fetch data only if this is not an intro card and a content
		// id has been specified.
		if (!this.options.contID) 
		{
			return;
		}
		
		// Pull the correct data if this is a preview or a real
		// card.
		if (this.options.preview) 
		{
			var theUrl = SME.url.cardPreview.evaluate({card: this.options.contID})
		} 
		else 
		{
			var theUrl = SME.url.cards.evaluate({card: this.options.contID});
		}
	
		new Ajax.Request(theUrl, 
		{ 
			method: 'get',
				
			onSuccess: function(transport) 
			{
				try 
				{
					var data = transport.responseText.cleanJSON().evalJSON();
					this.setLayout(SME.engineMapping[data.template], data);
				} 
				catch (exception) 
				{
					if (SME.debug)
					{
						console.error(exception);
					}
				
					this.setLayout(Card.Layout.Errors, '');
				}
			}.bind(this)
		});
	},
	
	/*
	 * Add extra buttons to a card. I moved this to its own private
	 * function because some cards, like system cards, don't need all 
	 * those extra buttons.
	 */
	_addExtraButtons: function() 
	{
		this.addCommandButton(Strings.addToSketchbook,'images/plus.gif', function() 
		{
			SME.sketchbook.addChip(this.options.contID);
		}.bind(this));
		
		this.addCommandButton(Strings.sendToFriend,'images/email.gif', function() 
		{
			if ($('cardFlash'))
			{
				this.flashP = $('cardFlash').up();
				this.flash  = $('cardFlash').remove();
			}
		
			var card = new Card(
			{ 
				color: SME.colors.grey, 
				title: Strings.sendToFriend,
				addExtraButtons: false,
				
				onFadeComplete: function() 
				{
					if (this.flash)
					{
						this.flashP.appendChild(this.flash);
						this.flash = null;
					}
				}.bind(this)
			});
			
			card.setLayout(Card.Layout.Special, 
			{ 
				url: SME.url.sendToFriend.evaluate(
				{ 
					durl: 'card' + encodeURI(this.options.contID), 
					title: encodeURI(this.options.title) 
				}) 
			});
			
			card.show();
		}.bind(this));
		
		this.addCommandButton(Strings.printCard, 'images/print.gif', function() 
		{
			this.layoutEngine.print();
		}.bind(this));

		// Card history ON the card was poorly designed and implemented
		// (yeah, I know) so I'm just removing it for now till I get
		// some time to re-write it.
		//
		// TODO: Re-write this
		this.addCommandButton(Strings.myHistory,'images/history.gif', function() 
		{ 
			var center = window.calcCordsToCenter(SME.sizes.card.height, SME.sizes.card.width);
			SME.history.getDropDown(center.left + 30, center.top + 13);
		});
	},
	
	/*
	 * Hides the current card but does not remove it from the DOM.
	 */
	hide: function() 
	{
		// There is no current card so unset it
		// FLAWED LOGIC: We can have multiple cards.
		SME.currentCard = null;
		
		// Fade out and subsequently destroy the card
		new Effect.Fade(this.card, 
		{ 
			afterFinish: function() 
			{ 
				this.options.onFadeOutFinish();
				this.destroy();
			}.bind(this) 
		});
		
		// FLAWED LOGIC: Multiple cards, see above.
		SME.history.clearHash();
		this.overlay.hide();
		this.options.onClose();
	},
	
	/*
	 * Sets the layout of the card using a layout engine.
	 */
	setLayout: function(layoutE, data) 
	{
		this.layoutEngine = new layoutE(this.cframe, data, this).layout();
	}
});