-
Notifications
You must be signed in to change notification settings - Fork 0
/
Label.js
72 lines (71 loc) · 2.1 KB
/
Label.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
var compose = require('ksf/utils/compose');
var Elmt = require('./Element');
var _ContentDelegate = require('./_ContentDelegate');
module.exports = compose(_ContentDelegate, function() {
this._content = this.element = new Elmt().style({
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
});
this._vAlign = 'middle';
}, {
value: function(value) {
this._content.prop('textContent', (typeof value === 'string' ? value : ''));
return this;
},
hAlign: function(hAlign) {
this._content.styleProp('textAlign', hAlign);
return this;
},
vAlign: function(vAlign) {
this._vAlign = vAlign;
this._applyVAlign();
return this;
},
_applyVAlign: function() {
this._content.styleProp('lineHeight', null);
this._content.styleProp('padding-top', null);
if (this._vAlign === 'bottom') {
// no clean way to bottom-align a single-line text without using an extra container node
// best compromise I could find is the following, adding padding-top with a line-height of 1.1 (so that characters "legs", like q, p, etc. are not cut for most fonts)
this._content.styleProp('padding-top', 'calc(' + this._content.height() + 'px - 1.1em)');
this._content.styleProp('lineHeight', '1.1');
} else if (this._vAlign === 'middle') {
this._content.styleProp('lineHeight', this._content.height() + 'px');
}
},
height: function(height) {
if (arguments.length) {
this._content.height(height);
this._applyVAlign();
return this;
} else {
return this._content.height();
}
},
color: function(color) {
this._content.styleProp('color', color);
return this;
},
backgroundColor: function (color) {
this._content.styleProp('backgroundColor', color);
return this;
},
font: function(font) {
if (typeof font === 'string') {
this._content.styleProp('font', font);
} else {
font.family && this._content.styleProp('fontFamily', font.family);
this._content.style({
fontWeight: font.weight,
fontSize: font.size,
fontStyle: font.style,
});
}
return this;
},
textDecoration: function(value) {
this._content.styleProp('textDecoration', value);
return this;
},
});