Skip to content

Commit e02a35d

Browse files
committed
refactor(action-bar): insert ActionItems at correct positions
ActionBar's insertChild method is now passed a `next` view argument. When the view to insert is an ActionItem, `next` is used to find the correct position to insert the new item. fixes #689
1 parent 4f8f00a commit e02a35d

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

Diff for: nativescript-angular/directives/action-bar.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { Directive, Component, ElementRef, Optional, OnDestroy } from "@angular/core";
2-
import { ActionItem, ActionBar, NavigationButton } from "tns-core-modules/ui/action-bar";
2+
import {
3+
ActionBar,
4+
ActionItem,
5+
ActionItems,
6+
NavigationButton,
7+
} from "tns-core-modules/ui/action-bar";
38
import { Page } from "tns-core-modules/ui/page";
49
import { View } from "tns-core-modules/ui/core/view";
510

@@ -25,14 +30,14 @@ type NgActionBar = (ActionBar & ViewExtensions);
2530

2631
const actionBarMeta: ViewClassMeta = {
2732
skipAddToDom: true,
28-
insertChild: (parent: NgActionBar, child: NgView, previous: NgView) => {
33+
insertChild: (parent: NgActionBar, child: NgView, next: any) => {
2934
if (isInvisibleNode(child)) {
3035
return;
3136
} else if (isNavigationButton(child)) {
3237
parent.navigationButton = child;
3338
child.templateParent = parent;
3439
} else if (isActionItem(child)) {
35-
parent.actionItems.addItem(child);
40+
addActionItem(parent, child, next);
3641
child.templateParent = parent;
3742
} else if (isView(child)) {
3843
parent.titleView = child;
@@ -56,6 +61,28 @@ const actionBarMeta: ViewClassMeta = {
5661
},
5762
};
5863

64+
const addActionItem = (bar: NgActionBar, item: ActionItem, next: ActionItem) => {
65+
if (next) {
66+
insertActionItemBefore(bar, item, next);
67+
} else {
68+
appendActionItem(bar, item);
69+
}
70+
};
71+
72+
const insertActionItemBefore = (bar: NgActionBar, item: ActionItem, next: ActionItem) => {
73+
const actionItems: ActionItems = bar.actionItems;
74+
const actionItemsCollection: ActionItem[] = actionItems.getItems();
75+
76+
const indexToInsert = actionItemsCollection.indexOf(next);
77+
actionItemsCollection.splice(indexToInsert, 0, item);
78+
79+
(<any>actionItems).setItems(actionItemsCollection);
80+
};
81+
82+
const appendActionItem = (bar: NgActionBar, item: ActionItem) => {
83+
bar.actionItems.addItem(item);
84+
};
85+
5986
registerElement("ActionBar", () => require("ui/action-bar").ActionBar, actionBarMeta);
6087
registerElement("ActionItem", () => require("ui/action-bar").ActionItem);
6188
registerElement("NavigationButton", () => require("ui/action-bar").NavigationButton);

Diff for: nativescript-angular/element-registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const getClassName = instance => instance.constructor.name;
7272

7373
export interface ViewClassMeta {
7474
skipAddToDom?: boolean;
75-
insertChild?: (parent: any, child: any, previous?: any, next?: any) => void;
75+
insertChild?: (parent: any, child: any, next?: any) => void;
7676
removeChild?: (parent: any, child: any) => void;
7777
}
7878

Diff for: nativescript-angular/view-util.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export class ViewUtil {
6767
}
6868

6969
if (!isDetachedElement(child)) {
70-
this.addToVisualTree(parent, child, next);
70+
const nextVisual = this.findNextVisual(next);
71+
this.addToVisualTree(parent, child, nextVisual);
7172
}
7273
}
7374

@@ -104,10 +105,10 @@ export class ViewUtil {
104105
}
105106

106107
private addToVisualTree(parent: NgView, child: NgView, next: NgView): void {
107-
traceLog(`ViewUtil.addToVisualTreee parent: ${parent}, view: ${child}, next: ${next}`);
108+
traceLog(`ViewUtil.addToVisualTree parent: ${parent}, view: ${child}, next: ${next}`);
108109

109110
if (parent.meta && parent.meta.insertChild) {
110-
parent.meta.insertChild(parent, child);
111+
parent.meta.insertChild(parent, child, next);
111112
} else if (isLayout(parent)) {
112113
this.insertToLayout(parent, child, next);
113114
} else if (isContentView(parent)) {
@@ -135,7 +136,7 @@ export class ViewUtil {
135136
}
136137
}
137138

138-
private findNextVisual(view: NgView) {
139+
private findNextVisual(view: NgView): NgView {
139140
let next = view;
140141
while (next && isDetachedElement(next)) {
141142
next = next.nextSibling;

0 commit comments

Comments
 (0)