forked from evadne/LPKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LPAnchorButton.j
202 lines (164 loc) · 5.84 KB
/
LPAnchorButton.j
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
/*
* LPAnchorButton.j
* LPKit
*
* Created by Ludwig Pettersson on November 9, 2009.
*
* The MIT License
*
* Copyright (c) 2009 Ludwig Pettersson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
LPAnchorButtonNoUnderline = 0;
LPAnchorButtonNormalUnderline = 1;
LPAnchorButtonHoverUnderline = 2;
@implementation LPAnchorButton : CPControl
{
unsigned _underlineMask @accessors(property=underlineMask);
CPString _title @accessors(property=title);
CPURL _URL;
id _DOMAnchorElement;
}
+ (id)buttonWithTitle:(CPString)aTitle
{
var button = [[self alloc] init];
[button setTitle:aTitle];
[button sizeToFit];
return button;
}
- (id)init
{
if (self = [super init])
{
// Set default underline mask
_underlineMask = LPAnchorButtonNormalUnderline | LPAnchorButtonHoverUnderline;
}
return self;
}
- (void)setTitle:(CPString)aTitle
{
_title = aTitle;
[self setNeedsLayout];
}
- (void)openURLOnClick:(CPURL)aURL
{
_URL = aURL;
[self setNeedsLayout];
}
- (void)setTextColor:(CPColor)aColor
{
[self setValue:aColor forThemeAttribute:@"text-color"];
}
- (void)setTextHoverColor:(CPColor)aColor
{
[self setValue:aColor forThemeAttribute:@"text-color" inState:CPThemeStateHighlighted];
}
- (void)mouseEntered:(CPEvent)anEvent
{
[self setThemeState:CPThemeStateHighlighted];
}
- (void)mouseExited:(CPEvent)anEvent
{
[self unsetThemeState:CPThemeStateHighlighted];
}
- (void)mouseDown:(CPEvent)anEvent
{
if (_URL)
{
[[[self window] platformWindow] _propagateCurrentDOMEvent:YES];
}
else
[super mouseDown:anEvent];
}
- (void)sizeToFit
{
[self setFrameSize:[(_title || " ") sizeWithFont:[self currentValueForThemeAttribute:@"font"]]];
}
- (CGRect)rectForEphemeralSubviewNamed:(CPString)aName
{
return [self bounds];
}
- (CPView)createEphemeralSubviewNamed:(CPString)aName
{
return [[_CPImageAndTextView alloc] initWithFrame:CGRectMakeZero()];
}
- (void)layoutSubviews
{
// Set up anchor element if needed
if (_URL)
{
if (!_DOMAnchorElement)
{
_DOMAnchorElement = document.createElement("a");
_DOMAnchorElement.target = @"_blank";
_DOMAnchorElement.style.position = "absolute";
_DOMAnchorElement.style.zIndex = "100";
self._DOMElement.appendChild(_DOMAnchorElement)
}
_DOMAnchorElement.href = typeof _URL == 'string' ? _URL : [_URL absoluteString];
var bounds = [self bounds];
_DOMAnchorElement.style.width = CGRectGetWidth(bounds) + @"px";
_DOMAnchorElement.style.height = CGRectGetHeight(bounds) + @"px";
}
var cssTextDecoration = @"none";
// Check if we should underline
if (((_themeState === CPThemeStateNormal || _themeState === CPThemeStateSelected) && (_underlineMask & LPAnchorButtonNormalUnderline)) ||
((_themeState & CPThemeStateHighlighted) && (_underlineMask & LPAnchorButtonHoverUnderline)))
{
cssTextDecoration = @"underline";
}
var contentView = [self layoutEphemeralSubviewNamed:@"content-view"
positioned:CPWindowAbove
relativeToEphemeralSubviewNamed:nil];
if (contentView)
{
[contentView setText:_title];
[contentView setTextColor:[self currentValueForThemeAttribute:@"text-color"]];
[contentView setFont:[self currentValueForThemeAttribute:@"font"]];
[contentView setAlignment:[self currentValueForThemeAttribute:@"alignment"]];
[contentView setVerticalAlignment:[self currentValueForThemeAttribute:@"vertical-alignment"]];
[contentView setLineBreakMode:[self currentValueForThemeAttribute:@"line-break-mode"]];
[contentView setTextShadowColor:[self currentValueForThemeAttribute:@"text-shadow-color"]];
[contentView setTextShadowOffset:[self currentValueForThemeAttribute:@"text-shadow-offset"]];
if (contentView._DOMTextElement)
contentView._DOMTextElement.style.setProperty(@"text-decoration", cssTextDecoration, null);
if (contentView._DOMTextShadowElement)
contentView._DOMTextShadowElement.style.setProperty(@"text-decoration", cssTextDecoration, null);
}
}
@end
var LPAnchorButtonUnderlineMaskKey = @"LPAnchorButtonUnderlineMaskKey";
@implementation LPAnchorButton (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
if (self = [super initWithCoder:aCoder])
{
_underlineMask = [aCoder decodeIntForKey:LPAnchorButtonUnderlineMaskKey] || LPAnchorButtonNoUnderline;
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
if (_underlineMask !== LPAnchorButtonNoUnderline)
[aCoder encodeInt:_underlineMask forKey:LPAnchorButtonUnderlineMaskKey];
}
@end