summaryrefslogtreecommitdiff
path: root/docroot/classes/bezel.class.js
blob: d29ed4ff968a044282710e6d9773a1be9621ab55 (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
/*
 * Material Experience - Bezel Class
 * 
 * EYEMG - Interactive Media Group
 * Created by Mike Crute (mcrute@eyemg.com)
 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
 * 
 * Notification bezel class mimics the notification bezel in BBEdit 
 * (and to a lesser extent, OS X).
 */

var Bezel = Class.create();
Object.extend(Bezel.prototype, 
{
	/*
	 * Creates the bezel HTML elements and adds them to the document body. 
	 */
	initialize: function() 
	{
		this.options = Object.extend(
		{
			background:  "black",			// Background color of the bezel
			opacity:     0.8,			// % Opacity of the bezel
			fontSize:    "2em",			// Font Size
			fadeTime:    2,				// Fade Time
			destroy:     true,			// Destroy bezel on fade out
			displayTime: 1,				// How long to display the bezel - 0 is "sticky"
			onShow:      Prototype.emptyFunction,	// After show callback
			afterHide:   Prototype.emptyFunction	// After hide callback
		}, arguments[0] || {});
	
		this.visible = false;
	
		var Rounder  = new RoundedCorners(this.options.background);
		
		this.bezel   = Element.extend(document.createElement("div"));
		this.message = Element.extend(document.createElement("div"));
		
		this.bezel.appendChild(Rounder.get(Rounder.directions.top));
		this.bezel.appendChild(this.message);
		this.bezel.appendChild(Rounder.get(Rounder.directions.bottom));
		
		document.body.appendChild(this.bezel);
		
		this.bezel.setStyle(
		{
			opacity:  this.options.opacity,
			zIndex:   9999999,
			position: "absolute",
			display:  "none",
			width:    "auto",
			cursor:   "pointer"
		});
		
		this.message.setStyle(
		{
			background: this.options.background,
			fontSize:   this.options.fontSize,
			color:      "white",
			padding:    "0px 1em",
			textAlign:  "center"
		});
		
		// IE does not properly size the bezel so we must constrain it
		if (Prototype.Browser.IE)
		{
			this.bezel.setStyle({ width: "50%" });
		}
		
		this.bezel.onclick = function() 
		{ 
			this.hide();
		}.bind(this);
	},
	
	/*
	 * Displays the notification bezel with the requested message.
	 */
	show: function(message) 
	{
		// Sets the bezel message
		this.message.innerHTML = message;
		this.visible           = true;
		
		// Center the bezel
		var mysize = this.bezel.getDimensions();
		mysize     = window.calcCordsToCenter(mysize.height, mysize.width);
		
		// Set the position of the bezel
		this.bezel.setStyle(
		{
			top:  mysize.top  + "px",
			left: mysize.left + "px"
		});
		
		this.bezel.show();
		
		if (typeof this.options.onShow == "function") 
		{
			this.options.onShow();
		}
		
		if (this.options.displayTime > 0) 
		{
			new PeriodicalExecuter(function(executer)
			{
				this.hide();
				executer.stop();
			}.bind(this), this.options.displayTime);
		}
		
		return this;
	},
	
	/*
	 * Removes the bezel from the DOM.
	 */
	destroy: function() 
	{
		try 
		{
			document.body.removeChild(this.bezel);
		} 
		catch (e) 
		{
			// Ignore errors
		}
	},
	
	/*
	 * Fades out the notification bezel. If this is not a sticky bezel 
	 * (displayTime > 0) then this function will be called automatically 
	 * by the show routine.
	 */
	hide: function() 
	{
		new Effect.Fade(this.bezel, 
		{ 	
			duration:    this.options.fadeTime, 
			
			afterFinish: function() 
			{
				this.visible = false;
			
				if (this.options.destroy) 
				{
					this.destroy();
				}

				this.options.afterHide(this);
			}.bind(this)
		});
	}
});