forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatted-string-common.ts
More file actions
215 lines (191 loc) · 6.28 KB
/
formatted-string-common.ts
File metadata and controls
215 lines (191 loc) · 6.28 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
import spanModule = require("text/span");
import observable = require("data/observable");
import observableArray = require("data/observable-array");
import definition = require("text/formatted-string");
import view = require("ui/core/view");
import types = require("utils/types");
import colorModule = require("color");
export module knownCollections {
export var spans = "spans";
}
export class FormattedString extends observable.Observable implements definition.FormattedString, view.AddArrayFromBuilder {
private _spans: observableArray.ObservableArray<spanModule.Span>;
public _formattedText: any;
private _isDirty: boolean;
private _fontFamily: string;
private _fontSize: number;
private _foregroundColor: colorModule.Color;
private _backgroundColor: colorModule.Color;
private _underline: number;
private _strikethrough: number;
private _fontAttributes: number;
private _parent: view.View;
get parent(): view.View {
return this._parent;
}
set parent(value: view.View) {
if (this._parent !== value) {
this._parent = value;
}
}
get fontFamily(): string {
return this._fontFamily;
}
set fontFamily(value: string) {
if (this._fontFamily !== value) {
this._fontFamily = value;
}
}
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;
}
}
get foregroundColor(): colorModule.Color {
return this._foregroundColor;
}
set foregroundColor(value: colorModule.Color) {
var foreColor;
if (types.isString(value)) {
foreColor = new colorModule.Color(<any>value);
}
else {
foreColor = value;
}
if (this._foregroundColor !== foreColor) {
this._foregroundColor = foreColor;
}
}
get backgroundColor(): colorModule.Color {
return this._backgroundColor;
}
set backgroundColor(value: colorModule.Color) {
var backColor;
if (types.isString(value)) {
backColor = new colorModule.Color(<any>value);
}
else {
backColor = value;
}
if (this._backgroundColor !== backColor) {
this._backgroundColor = backColor;
}
}
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;
}
}
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;
}
}
get fontAttributes(): number {
return this._fontAttributes;
}
set fontAttributes(value: number) {
if (this._fontAttributes !== value) {
this._fontAttributes = value;
}
}
constructor() {
super();
this._spans = new observableArray.ObservableArray<spanModule.Span>();
this._spans.addEventListener(observableArray.ObservableArray.changeEvent, this.onSpansCollectionChanged, this);
this._isDirty = true;
}
get spans(): observableArray.ObservableArray<spanModule.Span> {
if (!this._spans) {
this._spans = new observableArray.ObservableArray<spanModule.Span>();
}
return this._spans;
}
private onSpansCollectionChanged(eventData: observableArray.ChangedData<spanModule.Span>) {
var i;
if (eventData.addedCount > 0) {
for (i = 0; i < eventData.addedCount; i++) {
var addedSpan: spanModule.Span = (<observableArray.ObservableArray<spanModule.Span>>eventData.object).getItem(eventData.index + i);
addedSpan.parentFormattedString = this;
addedSpan.addEventListener(observable.Observable.propertyChangeEvent, this.onSpanChanged, this);
}
}
if (eventData.removed && eventData.removed.length > 0) {
var p;
for (p = 0; p < eventData.removed.length; p++) {
var removedSpan = eventData.removed[p];
removedSpan.removeEventListener(observable.Observable.propertyChangeEvent, this.onSpanChanged, this);
}
}
this.updateFormattedText(true);
}
private onSpanChanged(eventData: observable.PropertyChangeData) {
this.updateFormattedText(true);
}
private updateFormattedText(isDirty?: boolean) {
var shouldUpdate = isDirty || this._isDirty;
if (shouldUpdate) {
this.createFormattedStringCore();
this._isDirty = false;
this.notify(this._createPropertyChangeData("", this));
}
}
public createFormattedStringCore() {
// a virtual method overriden in platform specific implementations.
}
public toString(): string {
var result = "";
var i;
for (i = 0; i < this._spans.length; i++) {
result += this._spans.getItem(i).text;
}
return result;
}
public _addArrayFromBuilder(name: string, value: Array<any>) {
var i;
var span;
if (name === knownCollections.spans) {
for (i = 0; i < value.length; i++) {
span = value[i];
this.spans.push(span);
}
}
}
public updateSpansBindingContext(newBindingContext) {
var i;
for (i = 0; i < this.spans.length; i++) {
var span = this.spans.getItem(i);
span.bindingContext = newBindingContext;
}
}
}