1 | import { profile } from '../../profiling';
|
2 | import { View, CSSType } from '../core/view';
|
3 | import { ViewBase, booleanConverter } from '../core/view-base';
|
4 | import { Trace } from '../../trace';
|
5 | import { ShorthandProperty, CssProperty, Property, unsetValue } from '../core/properties';
|
6 | import { Length, horizontalAlignmentProperty, verticalAlignmentProperty } from '../styling/style-properties';
|
7 | import { Style } from '../styling/style';
|
8 | let ActionBarBase = class ActionBarBase extends View {
|
9 | disposeNativeView() {
|
10 | this._actionItems = null;
|
11 | super.disposeNativeView();
|
12 | }
|
13 | get navigationButton() {
|
14 | return this._navigationButton;
|
15 | }
|
16 | set navigationButton(value) {
|
17 | if (this._navigationButton !== value) {
|
18 | if (this._navigationButton) {
|
19 | this._removeView(this._navigationButton);
|
20 | this._navigationButton.actionBar = undefined;
|
21 | }
|
22 | this._navigationButton = value;
|
23 | if (this._navigationButton) {
|
24 | this._navigationButton.actionBar = this;
|
25 | this._addView(this._navigationButton);
|
26 | }
|
27 | this.update();
|
28 | }
|
29 | }
|
30 | get actionItems() {
|
31 | return this._actionItems;
|
32 | }
|
33 | set actionItems(value) {
|
34 | throw new Error('actionItems property is read-only');
|
35 | }
|
36 | get titleView() {
|
37 | return this._titleView;
|
38 | }
|
39 | set titleView(value) {
|
40 | if (this._titleView !== value) {
|
41 | if (this._titleView) {
|
42 | this._removeView(this._titleView);
|
43 | this._titleView.style[horizontalAlignmentProperty.cssName] = unsetValue;
|
44 | this._titleView.style[verticalAlignmentProperty.cssName] = unsetValue;
|
45 | }
|
46 | this._titleView = value;
|
47 | if (value) {
|
48 |
|
49 | this._addView(value);
|
50 | const style = value.style;
|
51 | if (!horizontalAlignmentProperty.isSet(style)) {
|
52 | style[horizontalAlignmentProperty.cssName] = 'center';
|
53 | }
|
54 | if (!verticalAlignmentProperty.isSet(style)) {
|
55 | style[verticalAlignmentProperty.cssName] = 'middle';
|
56 | }
|
57 | }
|
58 | this.update();
|
59 | }
|
60 | }
|
61 | get androidContentInset() {
|
62 | return this.style.androidContentInset;
|
63 | }
|
64 | set androidContentInset(value) {
|
65 | this.style.androidContentInset = value;
|
66 | }
|
67 | get androidContentInsetLeft() {
|
68 | return this.style.androidContentInsetLeft;
|
69 | }
|
70 | set androidContentInsetLeft(value) {
|
71 | this.style.androidContentInsetLeft = value;
|
72 | }
|
73 | get androidContentInsetRight() {
|
74 | return this.style.androidContentInsetRight;
|
75 | }
|
76 | set androidContentInsetRight(value) {
|
77 | this.style.androidContentInsetRight = value;
|
78 | }
|
79 |
|
80 | get ios() {
|
81 | return undefined;
|
82 | }
|
83 |
|
84 | get android() {
|
85 | return undefined;
|
86 | }
|
87 | get _childrenCount() {
|
88 | let actionViewsCount = 0;
|
89 | this._actionItems?.getItems().forEach((actionItem) => {
|
90 | if (actionItem.actionView) {
|
91 | actionViewsCount++;
|
92 | }
|
93 | });
|
94 | return actionViewsCount + (this.titleView ? 1 : 0);
|
95 | }
|
96 | constructor() {
|
97 | super();
|
98 | this._actionItems = new ActionItems(this);
|
99 | }
|
100 | update() {
|
101 |
|
102 | }
|
103 | _onTitlePropertyChanged() {
|
104 |
|
105 | }
|
106 | _addArrayFromBuilder(name, value) {
|
107 | if (name === 'actionItems') {
|
108 | this.actionItems?.setItems(value);
|
109 | }
|
110 | }
|
111 | eachChildView(callback) {
|
112 | const titleView = this.titleView;
|
113 | if (titleView) {
|
114 | callback(titleView);
|
115 | }
|
116 | }
|
117 | eachChild(callback) {
|
118 | const titleView = this.titleView;
|
119 | if (titleView) {
|
120 | callback(titleView);
|
121 | }
|
122 | const navigationButton = this._navigationButton;
|
123 | if (navigationButton) {
|
124 | callback(navigationButton);
|
125 | }
|
126 | this.actionItems?.getItems().forEach((actionItem) => {
|
127 | callback(actionItem);
|
128 | });
|
129 | }
|
130 | _isEmpty() {
|
131 | if (this.title || this.titleView || (this.android && this.android.icon) || this.navigationButton || this.actionItems?.getItems().length > 0) {
|
132 | return false;
|
133 | }
|
134 | return true;
|
135 | }
|
136 | };
|
137 | ActionBarBase = __decorate([
|
138 | CSSType('ActionBar'),
|
139 | __metadata("design:paramtypes", [])
|
140 | ], ActionBarBase);
|
141 | export { ActionBarBase };
|
142 | export class ActionItems {
|
143 | constructor(actionBar) {
|
144 | this._items = new Array();
|
145 | this._actionBar = actionBar;
|
146 | }
|
147 | addItem(item) {
|
148 | if (!item) {
|
149 | throw new Error('Cannot add empty item');
|
150 | }
|
151 | this._items.push(item);
|
152 | item.actionBar = this._actionBar;
|
153 | this._actionBar._addView(item);
|
154 | this.invalidate();
|
155 | }
|
156 | removeItem(item) {
|
157 | if (!item) {
|
158 | throw new Error('Cannot remove empty item');
|
159 | }
|
160 | const itemIndex = this._items.indexOf(item);
|
161 | if (itemIndex < 0) {
|
162 | throw new Error('Cannot find item to remove');
|
163 | }
|
164 | this._items.splice(itemIndex, 1);
|
165 | this._actionBar._removeView(item);
|
166 | item.actionBar = undefined;
|
167 | this.invalidate();
|
168 | }
|
169 | getItems() {
|
170 | return this._items.slice();
|
171 | }
|
172 | getVisibleItems() {
|
173 | const visibleItems = [];
|
174 | this._items.forEach((item) => {
|
175 | if (isVisible(item)) {
|
176 | visibleItems.push(item);
|
177 | }
|
178 | });
|
179 | return visibleItems;
|
180 | }
|
181 | getItemAt(index) {
|
182 | if (index < 0 || index >= this._items.length) {
|
183 | return undefined;
|
184 | }
|
185 | return this._items[index];
|
186 | }
|
187 | setItems(items) {
|
188 |
|
189 | while (this._items.length > 0) {
|
190 | this.removeItem(this._items[this._items.length - 1]);
|
191 | }
|
192 |
|
193 | for (let i = 0; i < items.length; i++) {
|
194 | this.addItem(items[i]);
|
195 | }
|
196 | this.invalidate();
|
197 | }
|
198 | invalidate() {
|
199 | if (this._actionBar) {
|
200 | this._actionBar.update();
|
201 | }
|
202 | }
|
203 | }
|
204 | export class ActionItemBase extends ViewBase {
|
205 | get actionView() {
|
206 | return this._actionView;
|
207 | }
|
208 | set actionView(value) {
|
209 | if (this._actionView !== value) {
|
210 | if (this._actionView) {
|
211 | this._actionView.style[horizontalAlignmentProperty.cssName] = unsetValue;
|
212 | this._actionView.style[verticalAlignmentProperty.cssName] = unsetValue;
|
213 | this._removeView(this._actionView);
|
214 | }
|
215 | this._actionView = value;
|
216 | if (this._actionView) {
|
217 | this._addView(this._actionView);
|
218 | }
|
219 | if (this._actionBar) {
|
220 | this._actionBar.update();
|
221 | }
|
222 | }
|
223 | }
|
224 | get actionBar() {
|
225 | return this._actionBar;
|
226 | }
|
227 | set actionBar(value) {
|
228 | if (value !== this._actionBar) {
|
229 | this._actionBar = value;
|
230 | }
|
231 | }
|
232 | onLoaded() {
|
233 | if (this._actionView) {
|
234 | this._actionView.style[horizontalAlignmentProperty.cssName] = 'center';
|
235 | this._actionView.style[verticalAlignmentProperty.cssName] = 'middle';
|
236 | }
|
237 | super.onLoaded();
|
238 | }
|
239 | _raiseTap() {
|
240 | this._emit(ActionItemBase.tapEvent);
|
241 | }
|
242 | _addChildFromBuilder(name, value) {
|
243 | this.actionView = value;
|
244 | }
|
245 | _onVisibilityChanged(visibility) {
|
246 | if (this.actionBar) {
|
247 | this.actionBar.update();
|
248 | }
|
249 | }
|
250 | eachChild(callback) {
|
251 | if (this._actionView) {
|
252 | callback(this._actionView);
|
253 | }
|
254 | }
|
255 | }
|
256 | ActionItemBase.tapEvent = 'tap';
|
257 | __decorate([
|
258 | profile,
|
259 | __metadata("design:type", Function),
|
260 | __metadata("design:paramtypes", []),
|
261 | __metadata("design:returntype", void 0)
|
262 | ], ActionItemBase.prototype, "onLoaded", null);
|
263 | export function isVisible(item) {
|
264 | return item.visibility === 'visible';
|
265 | }
|
266 | function onTitlePropertyChanged(actionBar, oldValue, newValue) {
|
267 | actionBar._onTitlePropertyChanged();
|
268 | }
|
269 | export const titleProperty = new Property({
|
270 | name: 'title',
|
271 | valueChanged: onTitlePropertyChanged,
|
272 | });
|
273 | titleProperty.register(ActionBarBase);
|
274 | function onItemChanged(item, oldValue, newValue) {
|
275 | if (item.actionBar) {
|
276 | item.actionBar.update();
|
277 | }
|
278 | }
|
279 | function onVisibilityChanged(item, oldValue, newValue) {
|
280 | item._onVisibilityChanged(newValue);
|
281 | }
|
282 | export function traceMissingIcon(icon) {
|
283 | Trace.write('Could not load action bar icon: ' + icon, Trace.categories.Error, Trace.messageType.error);
|
284 | }
|
285 | function convertToContentInset(value) {
|
286 | if (typeof value === 'string' && value !== 'auto') {
|
287 | const insets = value.split(/[ ,]+/);
|
288 | return [
|
289 | [androidContentInsetLeftProperty, Length.parse(insets[0])],
|
290 | [androidContentInsetRightProperty, Length.parse(insets[1] || insets[0])],
|
291 | ];
|
292 | }
|
293 | else {
|
294 | return [
|
295 | [androidContentInsetLeftProperty, value],
|
296 | [androidContentInsetRightProperty, value],
|
297 | ];
|
298 | }
|
299 | }
|
300 | export const iosIconRenderingModeProperty = new Property({ name: 'iosIconRenderingMode', defaultValue: 'alwaysOriginal' });
|
301 | iosIconRenderingModeProperty.register(ActionBarBase);
|
302 | export const textProperty = new Property({
|
303 | name: 'text',
|
304 | defaultValue: '',
|
305 | valueChanged: onItemChanged,
|
306 | });
|
307 | textProperty.register(ActionItemBase);
|
308 | export const iconProperty = new Property({
|
309 | name: 'icon',
|
310 | valueChanged: onItemChanged,
|
311 | });
|
312 | iconProperty.register(ActionItemBase);
|
313 | export const visibilityProperty = new Property({
|
314 | name: 'visibility',
|
315 | defaultValue: 'visible',
|
316 | valueChanged: onVisibilityChanged,
|
317 | });
|
318 | visibilityProperty.register(ActionItemBase);
|
319 | export const flatProperty = new Property({
|
320 | name: 'flat',
|
321 | defaultValue: false,
|
322 | valueConverter: booleanConverter,
|
323 | });
|
324 | flatProperty.register(ActionBarBase);
|
325 | const androidContentInsetProperty = new ShorthandProperty({
|
326 | name: 'androidContentInset',
|
327 | cssName: 'android-content-inset',
|
328 | getter: function () {
|
329 | if (Length.equals(this.androidContentInsetLeft, this.androidContentInsetRight)) {
|
330 | return this.androidContentInsetLeft;
|
331 | }
|
332 | return `${Length.convertToString(this.androidContentInsetLeft)} ${Length.convertToString(this.androidContentInsetRight)}`;
|
333 | },
|
334 | converter: convertToContentInset,
|
335 | });
|
336 | androidContentInsetProperty.register(Style);
|
337 | export const androidContentInsetLeftProperty = new CssProperty({
|
338 | name: 'androidContentInsetLeft',
|
339 | cssName: 'android-content-inset-left',
|
340 | defaultValue: 'auto',
|
341 | equalityComparer: Length.equals,
|
342 | valueChanged: (target, oldValue, newValue) => {
|
343 | const view = target.viewRef.get();
|
344 | if (view) {
|
345 | view.effectiveContentInsetLeft = Length.toDevicePixels(newValue);
|
346 | }
|
347 | else {
|
348 | Trace.write(`${newValue} not set to view's property because ".viewRef" is cleared`, Trace.categories.Style, Trace.messageType.warn);
|
349 | }
|
350 | },
|
351 | valueConverter: Length.parse,
|
352 | });
|
353 | androidContentInsetLeftProperty.register(Style);
|
354 | export const androidContentInsetRightProperty = new CssProperty({
|
355 | name: 'androidContentInsetRight',
|
356 | cssName: 'android-content-inset-right',
|
357 | defaultValue: 'auto',
|
358 | equalityComparer: Length.equals,
|
359 | valueChanged: (target, oldValue, newValue) => {
|
360 | const view = target.viewRef.get();
|
361 | if (view) {
|
362 | view.effectiveContentInsetRight = Length.toDevicePixels(newValue);
|
363 | }
|
364 | else {
|
365 | Trace.write(`${newValue} not set to view's property because ".viewRef" is cleared`, Trace.categories.Style, Trace.messageType.warn);
|
366 | }
|
367 | },
|
368 | valueConverter: Length.parse,
|
369 | });
|
370 | androidContentInsetRightProperty.register(Style);
|
371 |
|
\ | No newline at end of file |