summaryrefslogtreecommitdiff
path: root/docroot/classes/layouts/layout.primary.class.js
blob: 87bc910e4bb3a6aa1347fda49f95b39d03fd87f0 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Material Experience - Primary Card Layout Class
 * 
 * EYEMG - Interactive Media Group
 * Created by Mike Crute (mcrute@eyemg.com)
 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
 * 
 * WARNING: Here be dragons, OK so not as many as before but good luck you poor
 * sap.
 */
 
Card.Layout.Primary = Class.create();
Object.extend(Object.extend(Card.Layout.Primary.prototype, Card.Layout.prototype), 
{
	/*
	 * Initialize the layout class
	 */
	initialize: function(cframe, data, card) 
	{
		this.cframe       = cframe;
		this.data         = data;
		this.card         = card;
		this.color        = this.card.options.color;
		this.hasResources = false;
		
		var direction          = data.template.replace(/narrow-/,'');
		this.direction         = direction ? direction : 'left';
		this.oppositeDirection = (direction == 'left') ? 'right' : 'left';
	},
	
	/*
	 * Set the column content. This can be flash, an image or an iframe.
	 */
	setColumn: function(url, direction, contTitle) 
	{
		var content = Builder.node('div', { style: 'float: ' + this.oppositeDirection }); 
		var myCol   = direction + 'Col';
		
		if (/swf$/.test(url)) // Flash
		{
			content.innerHTML = new SWFObject(url, 'cardFlash', 580, 479, 9, '#FFFFFF').getSWFHTML();
		} 
		else if (/(png|gif|jpg|jpeg)$/.test(url))  // Image
		{
			content = Builder.node('img', 
			{
				src:   url, 
				alt:   contTitle, 
				title: contTitle, 
				style: 'float: ' + this.oppositeDirection 
			});
		}
		else // iFrame
		{
			// Can't use DOM methods because IE is allergic
			content.innerHTML = '<iframe '                          + 
						'src         = "' + url + '" '  + 
						'frameborder = "0" '            +
						'scrolling   = "auto" '         +
						'width       = "580" '          +
						'height      = "480" '          +
						'id          = "wide_content" ' +
						'name        = "wide_content" ' +
					'></iframe>';
		}
		
		// We need to replace the content of the column if it exists to
		// facilitate changing content later on (i.e. thumbnails)
		if (typeof this[myCol] != 'undefined') 
		{
			this.cframe.replaceChild(content, this[myCol]);
			this[myCol] = content;
		} 
		else 
		{
			this[myCol] = this.cframe.appendChild(content);
		}

	},
	
	/*
	 * Build the resource (more information) list.
	 */
	getResourceList: function() 
	{
		// Only run if we need to
		if (!this.data.resources)
		{
			return;
		}
	
		var numResources  = 0;
		var iter          = 0;
		var limit         = 5;
		this.hasResources = true;
		
		this.resourceContainer = this.contentArea.appendChild(
			Builder.node('div', 
			[
				Builder.node('h2', { style: 'color: ' + this.card.options.color }, Strings.moreInfo),
				this.resources = Builder.node('ul', { className: 'resourceList' })
			]
		));
		
		this.data.resources.each(function(item) 
		{
			// Limit the number of allowable resources
			if (typeof item == 'undefined' || ++iter > limit)
			{
				return;
			}
			
			numResources++;
			
			this.resources.appendChild(Builder.node('li', 
				Builder.node('a', 
				{ 
					href:   item.link + '?entrypoint=DESIGNER', 
					target: '_new', 
					style:  'color: ' + this.color 
				}, item.title)
			));
		}.bind(this));
	},
	
	/*
	 * Set the contents of the narrow column.
	 */
	setHTML: function(content) 
	{
		this.htmlDiv.innerHTML = content;
	},
	
	/*
	 * Setup the thumbnails.
	 */
	getThumbnails: function() 
	{
		// Only run if we need to
		if (this.data.media.size() <= 1)
		{
			return;
		}
		
		var iter  = 0;
		var limit = 5; // Includes main media file
		var lData = this.data.media;
		
		this.thumbStrip = this.contentArea.appendChild(Builder.node('div', { className: 'thumbStrip' }));

		// Add the main media file to the data array
		lData.push(
		{
			thumb: this.data.contentWide[0].thumb,
			url:   this.data.contentWide[0].url
		});
		
		this.data.media.each(function(item) 
		{
			// Limit the number of thumbnails
			if (typeof item == 'undefined' || ++iter > limit) 
			{
				return;
			}
				
			var mediaPiece = this.thumbStrip.appendChild(
				Builder.node('img', 
				{
					src:        item.thumb, 
					className: 'mediaThumb' 
				})
			);
		
			Event.observe(mediaPiece, 'click', function() 
			{
				this.setColumn(item.url, this.direction, '');
			}.bindAsEventListener(this));
		}.bind(this));
	},
	
	/*
	 * Clean up the data.
	 */
	_cleanData: function() 
	{
		var lData = this.data.contentNarrow[0].htmlContent;
		this.data.contentNarrow[0].htmlContent = lData.replace(/<a/gi, '<a style="color: '+this.color+'"');
	},
	
	/*
	 * Calculate the heights of various interface elements for use in
	 * laying out the page.
	 */
	_calculateHeights: function()
	{
		// This data structure will hold the heights of all the elements 
		// in the narrow column for use in dynamically calculating the 
		// layout of the column.
		this.heightTable = 
		{
			headline      : this.headLine          ? this.headLine.getHeight()          : 0,
			content       : this.htmlDiv           ? this.htmlDiv.getHeight()           : 0,
			thumbnails    : this.thumbStrip        ? this.thumbStrip.getHeight()        : 0,
			resources     : this.resourceContainer ? this.resourceContainer.getHeight() : 0,
			paddingFactor : !Prototype.Browser.IE  ? 40                                 : 0
		};
		
		// If there is no headline IE comes up with a ridiculous height for some
		// reason so we correct for that here.
		if (Prototype.Browser.IE && this.data.contentNarrow[0].title.length == 0)
		{
			this.heightTable.headline = 0;
		}

		// A variety of exceptions/tweaks for IE6 found by testing every combination
		// of layouts and finding the variance. There isn't much of another way to
		// do it.
		if (Prototype.Browser.IE) 
		{
			this.heightTable.thumbnail += 2;
			this.heightTable.content   += 24;
		}
		
		if (Prototype.Browser.IE && this.resourceContainer) 
		{
			this.heightTable.resources -= 30;
		}
		
		if (Prototype.Browser.IE && this.htmlDiv && this.thumbStrip && !this.resourceContainer)
		{
			this.heightTable.thumbnails += 10;
		}
		
		
		if (Prototype.Browser.IE && this.resourceContainer && !this.thumbStrip)
		{
			this.heightTable.resources += 70;
		}

		// Calculate the height available for content
		this.heightTable.avaliableForContent = SME.sizes.innerCard.height - (
							this.heightTable.headline   + 
							this.heightTable.thumbnails + 
							this.heightTable.resources  + 
							this.heightTable.paddingFactor
						);
	},
	
	/*
	 * Setup the scroll bar on the wide content area.
	 */
	_setupScroller: function() 
	{
		// We only want to show the scroll bar if there is a need for it
		if (this.heightTable.content < this.heightTable.avaliableForContent) 
		{
			return;
		}
		
		this.contentArea.appendChild(
			this.scrollCont = Builder.node('div', 
			{ 
				style: 'border-left: 1px solid ' + this.card.options.color + ';' +
					'width: 1px; right: 0px; position: absolute; ' +
					'height:' + this.heightTable.avaliableForContent + 'px; ' +
					'top: ' + (this.heightTable.headline + 10) + 'px;' 
			},
			
			this.scrollBar  = Builder.node('img', 
			{ 
				src: 'images/pill.gif', 
				style: 'display: block; margin-left: -3px; cursor: move; ' +
					'background: ' + this.card.options.color + ';' 
			})
		));
		
		new Control.Slider(this.scrollBar, this.scrollCont, 
		{
			axis: 'vertical',
			range: $R(0, this.heightTable.content),
			
			onSlide: function(value) 
			{
				this.htmlDiv.scrollTop = value;
			}.bind(this)
		});
	
	},
	
	/*
	 * Main function to layout the card.
	 */
	layout: function() 
	{
		this._cleanData();
		
		this.setColumn(this.data.contentWide[0].url, this.direction, this.data.contentWide[0].title);
		
		this.contentArea = this.cframe.appendChild(
			Builder.node('div', { className: 'narrowCol' },
				this.headLine = Builder.node('h1', { style: 'color: ' + this.color }, this.data.contentNarrow[0].title)
			)
		);
		
		// We've gotta set the width here otherwise the height 
		// calculations will be incorrect
		this.htmlDiv = Builder.node('div', { style: 'width: 98%; overflow: hidden; width: 300px;' });
		
		// Must set this early on otherwise there is no way to determine 
		// the actual height of the content div
		this.setHTML(this.data.contentNarrow[0].htmlContent);
		this.contentArea.appendChild(this.htmlDiv);

		this.getResourceList();
		this.getThumbnails();

		// IE doesn't let prototype mess with the default object 
		// prototypes so we have to manually extend them. Sigh...
		[
			this.headLine, 
			this.htmlDiv, 
			this.thumbStrip, 
			this.resourceContainer, 
			this.contentArea
		].each(Element.extend);
	
		// Set these styles up here or we get a -21px bug in our 
		// calculation code
		this.contentArea.setStyle(
		{ 
			width:    '310px',
			position: 'absolute',
			top:      '0px'
		});
	
		this._calculateHeights();
		this._setupScroller();
		
		// We set this last, after all the height calculations are 
		// completed.  That makes it a lot easier to work with.
		this.htmlDiv.setStyle({ height: this.heightTable.avaliableForContent + 'px' });

		// Webkit doesn't pad things correctly
		if (this.direction == 'left') 
		{
			this.contentArea.setStyle({ left: '10px' });
		}
		else 
		{
			this.contentArea.setStyle({ right: '10px' });
		}
		
		// If this is an intro card do the intro card stuff.
		if (this.data.template == 'intro')
		{
			this._doIntro();
		}
		
		return this;
	},
	
	/*
	 * Setup an intro card. Because intro cards aren't really that much 
	 * different from a basic card and because class inheritance sucks in
	 * Prototype > 1.6 I'm just going to add this into here. 
	 */
	_doIntro: function() 
	{
		this.contentArea.appendChild(Builder.node('div', { className: 'skipbox' },
			this.skipLink = Builder.node('a', { href: '#tablehome', className: 'skiplink' }, 
			[
				Strings.skipButton,
				Builder.node('img', { src: 'images/arrow_right_grey.gif', className: 'skipimg' })
			])
		));
	},
	
	/*
	 * Throw the card into a popup window for printing.
	 */
	print: function() 
	{
		var wind = window.open('', '', 'width=' + SME.sizes.innerCard.width + ',height=' + (SME.sizes.innerCard.height + 60));

		with (wind) 
		{
			with (document) 
			{
				write('<html><head><style type="text/css">@import url(application.css); .narrowCol{top: 30px !important;}</style></head><body>');
				write('<img src="images/logo.jpg" style="display: block; margin: 5px 0px 30px 5px;"/>');
				write(this.cframe.innerHTML);
				write('</body></html>');
				close();
			}
			
			print();
			close();
		}
		
		return true;
	},
	
	/*
	 * Debug the card layout. Generally this should be used to spot out
	 * data issues. But it could also be used for other debugging purposes.
	 */
	_debug: function() 
	{
	
	}
});