forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan-common.ts
More file actions
216 lines (192 loc) · 6.13 KB
/
span-common.ts
File metadata and controls
216 lines (192 loc) · 6.13 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
import colorModule = require("color");
import definition = require("text/span");
import bindable = require("ui/core/bindable");
import types = require("utils/types");
import view = require("ui/core/view");
import enums = require("ui/enums");
import formattedString = require("text/formatted-string");
export class Span extends bindable.Bindable implements definition.Span, view.ApplyXmlAttributes {
private _fontFamily: string;
private _fontSize: number;
private _foregroundColor: colorModule.Color;
private _backgroundColor: colorModule.Color;
private _text: string;
private _underline: number;
private _strikethrough: number;
private _fontAttributes: number;
private _spanModifiers: Array<any>;
private _parentFormattedString: formattedString.FormattedString;
private _isInEditMode: boolean;
get fontFamily(): string {
return this._fontFamily;
}
set fontFamily(value: string) {
if (this._fontFamily !== value) {
this._fontFamily = value;
this.updateAndNotify();
}
}
get fontSize(): number {
return this._fontSize;
}
set fontSize(value: number) {
var fSize: number;
if (types.isString(value)) {
fSize = parseInt(<any>value);
}
else {
fSize = value;
}
if (this._fontSize !== fSize) {
this._fontSize = fSize;
this.updateAndNotify();
}
}
private _getColorValue(value: any): colorModule.Color {
var result;
if (types.isString(value) && (<any>value).indexOf("#") === 0) {
result = new colorModule.Color(<any>value);
}
else {
result = value;
}
return result;
}
get foregroundColor(): colorModule.Color {
return this._foregroundColor;
}
set foregroundColor(value: colorModule.Color) {
var convertedColor = this._getColorValue(value);
if (this._foregroundColor !== convertedColor) {
this._foregroundColor = convertedColor;
this.updateAndNotify();
}
}
get backgroundColor(): colorModule.Color {
return this._backgroundColor;
}
set backgroundColor(value: colorModule.Color) {
var convertedColor = this._getColorValue(value);
if (this._backgroundColor !== convertedColor) {
this._backgroundColor = convertedColor;
this.updateAndNotify();
}
}
get underline(): number {
return this._underline;
}
set underline(value: number) {
var underlineIntValue: number;
if (types.isString(value)) {
underlineIntValue = parseInt(<any>value);
}
else {
underlineIntValue = value;
}
if (this._underline !== underlineIntValue) {
this._underline = underlineIntValue;
this.updateAndNotify();
}
}
get strikethrough(): number {
return this._strikethrough;
}
set strikethrough(value: number) {
var strikethroughIntValue: number;
if (types.isString(value)) {
strikethroughIntValue = parseInt(<any>value);
}
else {
strikethroughIntValue = value;
}
if (this._strikethrough !== strikethroughIntValue) {
this._strikethrough = strikethroughIntValue;
this.updateAndNotify();
}
}
get fontAttributes(): number {
return this._fontAttributes;
}
set fontAttributes(value: number) {
if (this._fontAttributes !== value) {
this._fontAttributes = value;
this.updateAndNotify();
}
}
get spanModifiers(): Array<any> {
if (!this._spanModifiers) {
this._spanModifiers = new Array();
}
return this._spanModifiers;
}
get text(): string {
return this._text;
}
set text(value: string) {
if (this._text !== value) {
this._text = value;
this.updateAndNotify();
}
}
get parentFormattedString(): formattedString.FormattedString {
return this._parentFormattedString;
}
set parentFormattedString(value: formattedString.FormattedString) {
if (this._parentFormattedString !== value) {
this._parentFormattedString = value;
this.updateAndNotify();
}
}
public updateSpanModifiers(parent: formattedString.FormattedString) {
// a virtual method overriden in platform specific implementations.
if (this._isInEditMode) {
throw new Error("Cannot update span modifiers during update!");
}
this._spanModifiers = new Array();
}
public beginEdit(): void {
this._isInEditMode = true;
}
private updateAndNotify() {
if (!this._isInEditMode) {
this.updateSpanModifiers(this.parentFormattedString);
this.notify(this._createPropertyChangeData(".", this));
}
}
public endEdit(): void {
this._isInEditMode = false;
this.updateAndNotify();
}
public applyXmlAttribute(attribute, value): boolean {
if (attribute === "fontAttributes") {
if (value.indexOf(",")) {
var fontAttr = value.split(",");
var fontAttrValue;
var j;
for (j = 0; j < fontAttr.length; j++) {
fontAttrValue = Span.getFontAttributeFromString(fontAttr[j]);
this.fontAttributes |= fontAttrValue;
}
}
else {
this.fontAttributes |= value;
}
return true;
}
else {
return false;
}
}
private static getFontAttributeFromString(fontAttr: string) {
var fontAttrTrimmedAndLowerCase = fontAttr.trim().toLowerCase();
if (fontAttrTrimmedAndLowerCase === "bold") {
return enums.FontAttributes.Bold;
}
else if (fontAttrTrimmedAndLowerCase === "italic") {
return enums.FontAttributes.Italic;
}
else {
return enums.FontAttributes.Normal;
}
}
}