Button.html
11.6 KB
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-layout-component-Button'>/**
</span> * Component layout for buttons
* @private
*/
Ext.define('Ext.layout.component.Button', {
/* Begin Definitions */
alias: ['layout.button'],
extend: 'Ext.layout.component.Auto',
/* End Definitions */
type: 'button',
cellClsRE: /-btn-(tl|br)\b/,
htmlRE: /<.*>/,
constructor: function () {
this.callParent(arguments);
this.hackWidth = Ext.isIE && (!Ext.isStrict || Ext.isIE6 || Ext.isIE7 || Ext.isIE8);
this.heightIncludesPadding = Ext.isIE6 && Ext.isStrict;
},
// TODO - use last run results if text has not changed?
beginLayout: function (ownerContext) {
this.callParent(arguments);
this.cacheTargetInfo(ownerContext);
},
beginLayoutCycle: function(ownerContext) {
var me = this,
empty = '',
owner = me.owner,
btnEl = owner.btnEl,
btnInnerEl = owner.btnInnerEl,
text = owner.text,
htmlAutoHeight;
me.callParent(arguments);
btnInnerEl.setStyle('overflow', empty);
// Clear all element widths
if (!ownerContext.widthModel.natural) {
owner.el.setStyle('width', empty);
}
// If the text is HTML we need to let the browser automatically size things to cope with the case where the text
// is multi-line. This incurs a cost as we then have to measure those elements to derive other sizes
htmlAutoHeight = ownerContext.heightModel.shrinkWrap && text && me.htmlRE.test(text);
btnEl.setStyle('width', empty);
btnEl.setStyle('height', htmlAutoHeight ? 'auto' : empty);
btnInnerEl.setStyle('width', empty);
btnInnerEl.setStyle('height', htmlAutoHeight ? 'auto' : empty);
btnInnerEl.setStyle('line-height', htmlAutoHeight ? 'normal' : empty);
btnInnerEl.setStyle('padding-top', empty);
owner.btnIconEl.setStyle('width', empty);
},
calculateOwnerHeightFromContentHeight: function (ownerContext, contentHeight) {
return contentHeight;
},
calculateOwnerWidthFromContentWidth: function (ownerContext, contentWidth) {
return contentWidth;
},
measureContentWidth: function (ownerContext) {
var me = this,
owner = me.owner,
btnEl = owner.btnEl,
btnInnerEl = owner.btnInnerEl,
text = owner.text,
btnFrameWidth, metrics, sizeIconEl, width, btnElContext, btnInnerElContext;
// IE suffers from various sizing problems, usually caused by relying on it to size elements automatically. Even
// if an element is sized correctly it can prove necessary to set that size explicitly on the element to get it
// to size and position its children correctly. While the exact nature of the problems varies depending on the
// browser version, doctype and button configuration there is a common solution: set the sizes manually.
if (owner.text && me.hackWidth && btnEl) {
btnFrameWidth = me.btnFrameWidth;
// If the button text is something like '<' or '<<' then we need to escape it or it won't be measured
// correctly. The button text is supposed to be HTML and strictly speaking '<' and '<<' aren't valid HTML.
// However in practice they are commonly used and have worked 'correctly' in previous versions.
if (text.indexOf('>') === -1) {
text = text.replace(/</g, '&lt;');
}
metrics = Ext.util.TextMetrics.measure(btnInnerEl, text);
width = metrics.width + btnFrameWidth + me.adjWidth;
btnElContext = ownerContext.getEl('btnEl');
btnInnerElContext = ownerContext.getEl('btnInnerEl');
sizeIconEl = (owner.icon || owner.iconCls) &&
(owner.iconAlign == "top" || owner.iconAlign == "bottom");
// This cheat works (barely) with publishOwnerWidth which calls setProp also
// to publish the width. Since it is the same value we set here, the dirty bit
// we set true will not be cleared by publishOwnerWidth.
ownerContext.setWidth(width); // not setWidth (no framing)
btnElContext.setWidth(metrics.width + btnFrameWidth);
btnInnerElContext.setWidth(metrics.width + btnFrameWidth);
if (sizeIconEl) {
owner.btnIconEl.setWidth(metrics.width + btnFrameWidth);
}
} else {
width = ownerContext.el.getWidth();
}
return width;
},
measureContentHeight: function (ownerContext) {
var me = this,
owner = me.owner,
btnInnerEl = owner.btnInnerEl,
btnItem = ownerContext.getEl('btnEl'),
btnInnerItem = ownerContext.getEl('btnInnerEl'),
minTextHeight = me.minTextHeight,
adjHeight = me.adjHeight,
text = owner.getText(),
height,
textHeight,
topPadding;
if (owner.vertical) {
height = Ext.util.TextMetrics.measure(btnInnerEl, owner.text).width;
height += me.btnFrameHeight + adjHeight;
// Vertical buttons need height explicitly set
ownerContext.setHeight(height, /*dirty=*/true, /*force=*/true);
}
else {
// If the button text is HTML we have to handle it specially as it could contain multiple lines
if (text && me.htmlRE.test(text)) {
textHeight = btnInnerEl.getHeight();
// HTML content doesn't guarantee multiple lines: in the single line case it could now be too short for the icon
if (textHeight < minTextHeight) {
topPadding = Math.floor((minTextHeight - textHeight) / 2);
// Resize the span and use padding to center the text vertically. The hack to remove the padding
// from the height on IE6 is especially needed for link buttons
btnInnerItem.setHeight(minTextHeight - (me.heightIncludesPadding ? topPadding : 0));
btnInnerItem.setProp('padding-top', topPadding);
textHeight = minTextHeight;
}
// Calculate the height relative to the text span, auto can't be trusted in IE quirks
height = textHeight + adjHeight;
}
else {
height = ownerContext.el.getHeight();
}
}
// IE quirks needs the button height setting using style or it won't position the icon correctly (even if the height was already correct)
btnItem.setHeight(height - adjHeight);
return height;
},
publishInnerHeight: function(ownerContext, height) {
var me = this,
owner = me.owner,
isNum = Ext.isNumber,
btnItem = ownerContext.getEl('btnEl'),
btnInnerEl = owner.btnInnerEl,
btnInnerItem = ownerContext.getEl('btnInnerEl'),
btnHeight = isNum(height) ? height - me.adjHeight : height,
btnFrameHeight = me.btnFrameHeight,
text = owner.getText(),
textHeight,
paddingTop;
btnItem.setHeight(btnHeight);
btnInnerItem.setHeight(btnHeight);
// Only need the line-height setting for regular, horizontal Buttons
if (!owner.vertical && btnHeight >= 0) {
btnInnerItem.setProp('line-height', btnHeight - btnFrameHeight + 'px');
}
// Button text may contain markup that would force it to wrap to more than one line (e.g. 'Button<br>Label').
// When this happens, we cannot use the line-height set above for vertical centering; we instead reset the
// line-height to normal, measure the rendered text height, and add padding-top to center the text block
// vertically within the button's height. This is more expensive than the basic line-height approach so
// we only do it if the text contains markup.
if (text && me.htmlRE.test(text)) {
btnInnerItem.setProp('line-height', 'normal');
btnInnerEl.setStyle('line-height', 'normal');
textHeight = Ext.util.TextMetrics.measure(btnInnerEl, text).height;
paddingTop = Math.floor(Math.max(btnHeight - btnFrameHeight - textHeight, 0) / 2);
btnInnerItem.setProp('padding-top', me.btnFrameTop + paddingTop);
btnInnerItem.setHeight(btnHeight - (me.heightIncludesPadding ? paddingTop : 0));
}
},
publishInnerWidth: function(ownerContext, width) {
var me = this,
isNum = Ext.isNumber,
btnItem = ownerContext.getEl('btnEl'),
btnInnerItem = ownerContext.getEl('btnInnerEl'),
btnWidth = isNum(width) ? width - me.adjWidth : width;
btnItem.setWidth(btnWidth);
btnInnerItem.setWidth(btnWidth);
},
clearTargetCache: function(){
delete this.adjWidth;
},
cacheTargetInfo: function(ownerContext) {
var me = this,
owner = me.owner,
scale = owner.scale,
padding, frameSize, btnWrapPadding, btnInnerEl, innerFrameSize;
// The cache is only valid for a particular scale
if (!('adjWidth' in me) || me.lastScale !== scale) {
// If there has been a previous layout run it could have sullied the line-height
if (me.lastScale) {
owner.btnInnerEl.setStyle('line-height', '');
}
me.lastScale = scale;
padding = ownerContext.getPaddingInfo();
frameSize = ownerContext.getFrameInfo();
btnWrapPadding = ownerContext.getEl('btnWrap').getPaddingInfo();
btnInnerEl = ownerContext.getEl('btnInnerEl');
innerFrameSize = btnInnerEl.getPaddingInfo();
Ext.apply(me, {
// Width adjustment must take into account the arrow area. The btnWrap is the <em> which has padding to accommodate the arrow.
adjWidth : btnWrapPadding.width + frameSize.width + padding.width,
adjHeight : btnWrapPadding.height + frameSize.height + padding.height,
btnFrameWidth : innerFrameSize.width,
btnFrameHeight : innerFrameSize.height,
btnFrameTop : innerFrameSize.top,
// Use the line-height rather than height because if the text is multi-line then the height will be 'wrong'
minTextHeight : parseInt(btnInnerEl.getStyle('line-height'), 10)
});
}
me.callParent(arguments);
},
finishedLayout: function(){
var owner = this.owner;
this.callParent(arguments);
// Fixes issue EXTJSIV-5989. Looks like a browser repaint bug
// This hack can be removed once it is resolved.
if (Ext.isWebKit) {
owner.el.dom.offsetWidth;
}
}
});
</pre>
</body>
</html>