-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppleButton.js
330 lines (278 loc) · 10.4 KB
/
AppleButton.js
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
/*
© Copyright 2007 Apple Inc. All rights reserved.
IMPORTANT: This Apple software and the associated images located in
/System/Library/WidgetResources/AppleClasses/ (collectively "Apple Software")
are supplied to you by Apple Computer, Inc. (“Apple”) in consideration of your
agreement to the following terms. Your use, installation and/or redistribution
of this Apple Software constitutes acceptance of these terms. If you do not
agree with these terms, please do not use, install, or redistribute this Apple
Software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under
Apple’s copyrights in the Apple Software, to use, reproduce, and redistribute
the Apple Software, in text form (for JavaScript files) or binary form (for
associated images), for the sole purpose of creating Dashboard widgets for Mac
OS X.
If you redistribute the Apple Software, you must retain this entire notice and
the warranty disclaimers and limitation of liability provisions (last two
paragraphs below) in all such redistributions of the Apple Software.
You may not use the name, trademarks, service marks or logos of Apple to endorse
or promote products that include the Apple Software without the prior written
permission of Apple. Except as expressly stated in this notice, no other rights
or licenses, express or implied, are granted by Apple herein, including but not
limited to any patent rights that may be infringed by your products that
incorporate the Apple Software or by other works in which the Apple Software may
be incorporated.
The Apple Software is provided on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION
WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE
APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function AppleButton(button, text, height,
imgLeft, imgLeftClicked, imgLeftWidth,
imgMiddle, imgMiddleClicked,
imgRight, imgRightClicked, imgRightWidth,
onclick)
{
if (button == null)
return;
/* Objects */
this.textElement = null;
/* Read-write properties */
this.onclick = onclick;
/* Read-only properties */
this.enabled = true;
this._init(button, text, height,
imgLeft, imgLeftClicked, imgLeftWidth,
imgMiddle, imgMiddleClicked,
imgRight, imgRightClicked, imgRightWidth);
this.textElement.innerHTML = text;
}
AppleButton.prototype._init = function(button, text, height,
imgLeft, imgLeftClicked, imgLeftWidth,
imgMiddle, imgMiddleClicked,
imgRight, imgRightClicked, imgRightWidth)
{
this._imgLeftPath = imgLeft;
this._imgLeftClickedPath = imgLeftClicked;
this._imgMiddlePath = imgMiddle;
this._imgMiddleClickedPath = imgMiddleClicked;
this._imgRightPath = imgRight;
this._imgRightClickedPath = imgRightClicked;
var container = document.createElement("div");
this._container = container;
button.appendChild(container);
// For JavaScript event handlers
var _self = this;
this._mousedownHandler = function(event) { _self._mousedown(event); }
this._mousemoveHandler = function(event)
{
event.stopPropagation();
event.preventDefault();
}
this._mouseoverHandler = function(event) { _self._mouseover(event); }
this._mouseoutHandler = function(event) { _self._mouseout(event); }
this._mouseupHandler = function(event) { _self._mouseup(event); }
// Create the inner elements
var element = document.createElement("div");
var style = element.style;
style.display = "inline-block";
style.verticalAlign = "bottom";
style.background = "url(" + this._imgLeftPath + ") no-repeat top left";
style.height = height + "px";
style.width = imgLeftWidth + "px";
container.appendChild(element);
element = document.createElement("div");
element.innerText = text;
style = element.style;
style.display = "inline-block";
style.verticalAlign = "bottom";
style.backgroundRepeat = "repeat-x";
style.backgroundImage = "url(" + this._imgMiddlePath + ")";
style.lineHeight = height + "px";
style.height = height + "px";
style.overflow = "hidden";
style.whiteSpace = "nowrap";
container.appendChild(element);
this.textElement = element;
element = document.createElement("div");
style = element.style;
style.display = "inline-block";
style.verticalAlign = "bottom";
style.background = "url(" + this._imgRightPath + ") no-repeat top left";
style.height = height + "px";
style.width = imgRightWidth + "px";
container.appendChild(element);
style = container.style;
style.appleDashboardRegion = "dashboard-region(control rectangle)";
style.height = height + "px";
// preload images
var img = new Image(imgLeftWidth, height);
img.src = this._imgLeftPath;
img = new Image();
img.src = this._imgMiddlePath;
img = new Image(imgRightWidth, height);
img.src = this._imgRightPath;
img = new Image(imgLeftWidth, height);
img.src = this._imgLeftClickedPath;
img = new Image();
img.src = this._imgMiddleClickedPath;
img = new Image(imgRightWidth, height);
img.src = this._imgRightClickedPath;
container.addEventListener("mousedown", this._mousedownHandler, true);
}
AppleButton.prototype.remove = function()
{
var parent = this._container.parentNode;
parent.removeChild(this._container);
}
AppleButton.prototype.setDisabledImages = function(imgLeftDisabled, imgMiddleDisabled, imgRightDisabled)
{
this._imgLeftDisabledPath = imgLeftDisabled;
this._imgMiddleDisabledPath = imgMiddleDisabled;
this._imgRightDisabledPath = imgRightDisabled;
}
AppleButton.prototype.setEnabled = function(enabled)
{
this.enabled = enabled;
if (enabled)
{
this._container.children[0].style.backgroundImage = "url(" + this._imgLeftPath + ")";
this._container.children[1].style.backgroundImage = "url(" + this._imgMiddlePath + ")";
this._container.children[2].style.backgroundImage = "url(" + this._imgRightPath + ")";
this._container.style.appleDashboardRegion = "dashboard-region(control rectangle)";
}
else if (this._imgLeftDisabledPath !== undefined)
{
this._container.children[0].style.backgroundImage = "url(" + this._imgLeftDisabledPath + ")";
this._container.children[1].style.backgroundImage = "url(" + this._imgMiddleDisabledPath + ")";
this._container.children[2].style.backgroundImage = "url(" + this._imgRightDisabledPath + ")";
this._container.style.appleDashboardRegion = "none";
}
}
/*********************
* Private handlers
*/
AppleButton.prototype._setPressed = function(pressed)
{
if (pressed)
{
this._container.children[0].style.backgroundImage = "url(" + this._imgLeftClickedPath + ")";
this._container.children[1].style.backgroundImage = "url(" + this._imgMiddleClickedPath + ")";
this._container.children[2].style.backgroundImage = "url(" + this._imgRightClickedPath + ")";
}
else
{
this._container.children[0].style.backgroundImage = "url(" + this._imgLeftPath + ")";
this._container.children[1].style.backgroundImage = "url(" + this._imgMiddlePath + ")";
this._container.children[2].style.backgroundImage = "url(" + this._imgRightPath + ")";
}
}
AppleButton.prototype._mousedown = function(event)
{
// If we're disabled, don't do anything
if (!this.enabled)
{
event.stopPropagation();
event.preventDefault();
return;
}
// Change images to clicked state
this._setPressed(true);
// add temp event listeners
document.addEventListener("mousemove", this._mousemoveHandler, true);
document.addEventListener("mouseup", this._mouseupHandler, true);
this._container.addEventListener("mouseover", this._mouseoverHandler, true);
this._container.addEventListener("mouseout", this._mouseoutHandler, true);
this._inside = true;
event.stopPropagation();
event.preventDefault();
}
AppleButton.prototype._mouseover = function(event)
{
// Change images to clicked state
this._setPressed(true);
this._inside = true;
event.stopPropagation();
event.preventDefault();
}
AppleButton.prototype._mouseout = function(event)
{
// Change images to regular state
this._setPressed(false);
this._inside = false;
event.stopPropagation();
event.preventDefault();
}
AppleButton.prototype._mouseup = function(event)
{
// Change images to regular state
this._setPressed(false);
// Remove temp event listeners
document.removeEventListener("mousemove", this._mousemoveHandler, true);
document.removeEventListener("mouseup", this._mouseupHandler, true);
this._container.removeEventListener("mouseover", this._mouseoverHandler, true);
this._container.removeEventListener("mouseout", this._mouseoutHandler, true);
// Perform callback if we're inside the button
try {
if (this._inside && this.onclick != null)
this.onclick(event);
} catch(ex) {
throw ex;
} finally {
event.stopPropagation();
event.preventDefault();
delete this._inside;
}
}
//
// AppleGlassButton class
//
function AppleGlassButton(button, text, onclick)
{
/* Objects */
this.textElement = null;
/* Read-write properties */
this.onclick = onclick;
/* Read-only properties */
this.enabled = true;
this._init(button, text, 23,
"button/glassbuttonleft.png",
"button/glassbuttonleftclicked.png",
10,
"button/glassbuttonmiddle.png",
"button/glassbuttonmiddleclicked.png",
"button/glassbuttonright.png",
"button/glassbuttonrightclicked.png",
10);
var style = this.textElement.style;
style.fontSize = "12px";
style.fontFamily = "Helvetica Neue";
style.color = "white";
style.fontWeight = "bold";
}
// Inherit from AppleButton
AppleGlassButton.prototype = new AppleButton(null);
// Override regular disabled functionality
AppleGlassButton.prototype.setEnabled = function(enabled)
{
this.enabled = enabled;
if (enabled)
{
this._container.children[1].style.color = "white";
this._container.style.appleDashboardRegion = "dashboard-region(control rectangle)";
}
else
{
this._container.children[1].style.color = "rgb(150,150,150)";
this._container.style.appleDashboardRegion = "none";
}
}