UNPKG

6.29 kBJavaScriptView Raw
1import { Span } from './span';
2import { Observable } from '../../data/observable';
3import { ObservableArray } from '../../data/observable-array';
4import { ViewBase } from '../core/view-base';
5export class FormattedString extends ViewBase {
6 constructor() {
7 super();
8 this._spans = new ObservableArray();
9 this._spans.addEventListener(ObservableArray.changeEvent, this.onSpansCollectionChanged, this);
10 }
11 get fontFamily() {
12 return this.style.fontFamily;
13 }
14 set fontFamily(value) {
15 this.style.fontFamily = value;
16 }
17 get fontSize() {
18 return this.style.fontSize;
19 }
20 set fontSize(value) {
21 this.style.fontSize = value;
22 }
23 get fontStyle() {
24 return this.style.fontStyle;
25 }
26 set fontStyle(value) {
27 this.style.fontStyle = value;
28 }
29 get fontWeight() {
30 return this.style.fontWeight;
31 }
32 set fontWeight(value) {
33 this.style.fontWeight = value;
34 }
35 get fontVariationSettings() {
36 return this.style.fontVariationSettings;
37 }
38 set fontVariationSettings(value) {
39 this.style.fontVariationSettings = value;
40 }
41 get textDecoration() {
42 return this.style.textDecoration;
43 }
44 set textDecoration(value) {
45 this.style.textDecoration = value;
46 }
47 get color() {
48 return this.style.color;
49 }
50 set color(value) {
51 this.style.color = value;
52 }
53 get backgroundColor() {
54 return this.style.backgroundColor;
55 }
56 set backgroundColor(value) {
57 this.style.backgroundColor = value;
58 }
59 get iosAccessibilityAdjustsFontSize() {
60 return this.style.iosAccessibilityAdjustsFontSize;
61 }
62 set iosAccessibilityAdjustsFontSize(value) {
63 this.style.iosAccessibilityAdjustsFontSize = value;
64 }
65 get iosAccessibilityMinFontScale() {
66 return this.style.iosAccessibilityMinFontScale;
67 }
68 set iosAccessibilityMinFontScale(value) {
69 this.style.iosAccessibilityMinFontScale = value;
70 }
71 get iosAccessibilityMaxFontScale() {
72 return this.style.iosAccessibilityMaxFontScale;
73 }
74 set iosAccessibilityMaxFontScale(value) {
75 this.style.iosAccessibilityMaxFontScale = value;
76 }
77 get spans() {
78 if (!this._spans) {
79 this._spans = new ObservableArray();
80 }
81 return this._spans;
82 }
83 toString() {
84 let result = '';
85 for (let i = 0, length = this.spans.length; i < length; i++) {
86 result += this.spans.getItem(i).text;
87 }
88 return result;
89 }
90 _addArrayFromBuilder(name, value) {
91 if (name === 'spans') {
92 this.spans.push(...value);
93 }
94 }
95 _addChildFromBuilder(name, value) {
96 if (value instanceof Span) {
97 this.spans.push(value);
98 }
99 }
100 onSpansCollectionChanged(eventData) {
101 if (eventData.addedCount > 0) {
102 for (let i = 0; i < eventData.addedCount; i++) {
103 const span = eventData.object.getItem(eventData.index + i);
104 // First add to logical tree so that inherited properties are set.
105 this._addView(span);
106 // Then attach handlers - we skip the first notification because
107 // we raise change for the whole instance.
108 this.addPropertyChangeHandler(span);
109 }
110 }
111 if (eventData.removed && eventData.removed.length > 0) {
112 for (let p = 0; p < eventData.removed.length; p++) {
113 const span = eventData.removed[p];
114 // First remove handlers so that we don't listen for changes
115 // on inherited properties.
116 this.removePropertyChangeHandler(span);
117 // Then remove the element.
118 this._removeView(span);
119 }
120 }
121 this.notifyPropertyChange('.', this);
122 }
123 addPropertyChangeHandler(span) {
124 const style = span.style;
125 span.on(Observable.propertyChangeEvent, this.onPropertyChange, this);
126 style.on('fontFamilyChange', this.onPropertyChange, this);
127 style.on('fontSizeChange', this.onPropertyChange, this);
128 style.on('fontStyleChange', this.onPropertyChange, this);
129 style.on('fontWeightChange', this.onPropertyChange, this);
130 style.on('fontVariationSettingsChange', this.onPropertyChange, this);
131 style.on('textDecorationChange', this.onPropertyChange, this);
132 style.on('colorChange', this.onPropertyChange, this);
133 style.on('backgroundColorChange', this.onPropertyChange, this);
134 // These handlers will trigger font scale update
135 style.on('iosAccessibilityAdjustsFontSizeChange', this.onPropertyChange, this);
136 style.on('iosAccessibilityMinFontScaleChange', this.onPropertyChange, this);
137 style.on('iosAccessibilityMaxFontScaleChange', this.onPropertyChange, this);
138 style.on('fontScaleInternalChange', this.onPropertyChange, this);
139 }
140 removePropertyChangeHandler(span) {
141 const style = span.style;
142 span.off(Observable.propertyChangeEvent, this.onPropertyChange, this);
143 style.off('fontFamilyChange', this.onPropertyChange, this);
144 style.off('fontSizeChange', this.onPropertyChange, this);
145 style.off('fontStyleChange', this.onPropertyChange, this);
146 style.off('fontWeightChange', this.onPropertyChange, this);
147 style.off('fontVariationSettingsChange', this.onPropertyChange, this);
148 style.off('textDecorationChange', this.onPropertyChange, this);
149 style.off('colorChange', this.onPropertyChange, this);
150 style.off('backgroundColorChange', this.onPropertyChange, this);
151 style.off('iosAccessibilityAdjustsFontSizeChange', this.onPropertyChange, this);
152 style.off('iosAccessibilityMinFontScaleChange', this.onPropertyChange, this);
153 style.off('iosAccessibilityMaxFontScaleChange', this.onPropertyChange, this);
154 style.off('fontScaleInternalChange', this.onPropertyChange, this);
155 }
156 onPropertyChange(data) {
157 this.notifyPropertyChange(data.propertyName, this);
158 }
159 eachChild(callback) {
160 this.spans.forEach((v, i, arr) => callback(v));
161 }
162}
163//# sourceMappingURL=formatted-string.js.map
\No newline at end of file