Skip to content

Commit 52cbcd9

Browse files
committed
Correct Angular SwipeMenu
1 parent 2f1941c commit 52cbcd9

File tree

7 files changed

+77
-58
lines changed

7 files changed

+77
-58
lines changed

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v20.9.0

demo-snippets/ng/styles.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.item {
2+
padding: 10;
3+
color: white;
4+
5+
.title {
6+
font-size: 17;
7+
font-weight: bold;
8+
}
9+
10+
.subtitle {
11+
font-size: 14;
12+
}
13+
}
Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
<ActionBar>
22
<NavigationButton android.systemIcon="ic_menu_back" (tap)="goBack()"></NavigationButton>
3-
<Label text="Simple Grid"></Label>
3+
<Label text="Swipe Menu"></Label>
44
</ActionBar>
55

6-
<GridLayout>
7-
<CollectionView
8-
9-
[items]="items"
10-
(itemTap)="onItemTap($event)"
11-
(loadMoreItems)="onLoadMoreItems()"
12-
colWidth="50%"
13-
rowHeight="200"
14-
automationText="collectionView"
15-
>
6+
<GridLayout rows="auto,*">
7+
<CollectionView [items]="items" (itemTap)="onItemTap($event)" row="1" rowHeight="100" autoReloadItemOnLayout="true">
168
<ng-template let-item="item">
17-
<SwipeMenu
18-
leftSwipeDistance="300"
19-
[translationFunction]="drawerTranslationFunction"
20-
[startingSide]="item.startingSide"
21-
>
22-
<Gridlayout rows="*, auto" [backgroundColor]="item.color" class="item" mainContent width="100%">
23-
<Stacklayout row="1">
24-
<Label row="1" [text]="item.name" class="title" > </Label>
25-
<Label row="1" [text]="item.color" class="subtitle" > </Label>
26-
</Stacklayout>
27-
</Gridlayout>
28-
<Stacklayout leftDrawer orientation="horizontal" width="200">
29-
<Label [text]="item.menuOpened ? 'opened' : 'a'" width="100" height="100%" backgroundColor="red" textAlignment="center" > </Label>
30-
<Label text="b" width="100" height="100%" backgroundColor="blue" textAlignment="center" > </Label>
31-
</Stacklayout>
32-
</SwipeMenu>
9+
<SwipeMenu [leftSwipeDistance]="swipeMenuThreshold" [rightSwipeDistance]="swipeMenuThreshold" [translationFunction]="drawerTranslationFunction" [startingSide]="item.startingSide">
10+
<Gridlayout rows="*, auto" [backgroundColor]="item.color" class="item" mainContent width="100%">
11+
<Stacklayout row="1">
12+
<Label row="1" [text]="item.name" class="title"></Label>
13+
<Label row="1" [text]="item.color" class="subtitle"></Label>
14+
</Stacklayout>
15+
</Gridlayout>
16+
<Stacklayout leftSwipeMenu orientation="horizontal" width="200">
17+
<Label (tap)="menuItemTap($event)" [text]="item.startingSide ? 'opened' : 'a'" width="100" height="100%" backgroundColor="red" textAlignment="center"></Label>
18+
<Label (tap)="menuItemTap($event)" text="b" width="100" height="100%" backgroundColor="blue" textAlignment="center"></Label>
19+
</Stacklayout>
20+
<Stacklayout rightSwipeMenu orientation="horizontal" width="200">
21+
<Label (tap)="menuItemTap($event)" [text]="item.startingSide ? 'opened' : 'a'" width="100" height="100%" backgroundColor="red" textAlignment="center"></Label>
22+
<Label (tap)="menuItemTap($event)" text="b" width="100" height="100%" backgroundColor="blue" textAlignment="center"></Label>
23+
</Stacklayout>
24+
</SwipeMenu>
3325
</ng-template>
3426
</CollectionView>
3527
</GridLayout>
Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
1+
import { Component } from '@angular/core';
22
import { RouterExtensions } from '@nativescript/angular';
3+
import { ObservableArray, Screen } from '@nativescript/core';
34

45
@Component({
56
selector: 'ns-collectionview-swipe-menu',
6-
templateUrl: './swipe-menu.component.html'
7+
templateUrl: './swipe-menu.component.html',
8+
styleUrls: ['../styles.scss']
79
})
8-
export class SwipeMenuComponent implements OnInit {
10+
export class SwipeMenuComponent {
911
constructor(private router: RouterExtensions) {}
1012

11-
items = [
13+
items = new ObservableArray([
1214
{ index: 0, name: 'TURQUOISE', color: '#1abc9c' },
1315
{ index: 1, name: 'EMERALD', color: '#2ecc71' },
1416
{ index: 2, name: 'PETER RIVER', color: '#3498db' },
@@ -29,28 +31,37 @@ export class SwipeMenuComponent implements OnInit {
2931
{ index: 17, name: 'POMEGRANATE', color: '#c0392b' },
3032
{ index: 18, name: 'SILVER', color: '#bdc3c7' },
3133
{ index: 19, name: 'ASBESTOS', color: '#7f8c8d' }
32-
];
34+
]);
3335

34-
ngOnInit(): void {}
35-
36-
goBack(): void {
37-
this.router.back();
38-
}
39-
40-
onItemTap({ index, item }) {
41-
console.log(`EVENT TRIGGERED: Tapped on ${index} ${item.name}`);
42-
}
43-
44-
onLoadMoreItems() {
45-
console.log('EVENT TRIGGERED: onLoadMoreItems()');
46-
}
4736
drawerTranslationFunction(side, width, value, delta, progress) {
4837
const result = {
4938
mainContent: {
50-
translateX: side === 'right' ? -delta : delta
39+
translateX: side === 'right' ? -delta : delta,
40+
opacity: progress < 1 ? 1 : 0.8
41+
},
42+
backDrop: {
43+
opacity: progress * 0.00001
5144
}
5245
} as any;
53-
46+
console.log(`drawerTranslation invoked\n side: ${side}, width: ${width}, value: ${value}, delta: ${delta}, progress: ${progress}`);
5447
return result;
5548
}
49+
50+
swipeMenuThreshold = Screen.mainScreen.widthPixels;
51+
menuItemTap(e) {
52+
console.log(`EVENT TRIGGERED: Tapped on ${e.object.text}`);
53+
}
54+
onItemTap({ index, item, view }) {
55+
console.log(`EVENT TRIGGERED: Tapped on ${index} ${item.name}`);
56+
57+
// startingSide means that a swipemenu is open, so close it
58+
if (item.startingSide) {
59+
console.log('Menu open, closing ...');
60+
view.close();
61+
}
62+
}
63+
64+
goBack(): void {
65+
this.router.back();
66+
}
5667
}

src/collectionview/index.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,10 @@ export class CollectionView extends CollectionViewBase {
12061206
parentView.id = 'collectionViewHolder';
12071207
view = parentView;
12081208
}
1209-
view._setupAsRootView(this._context);
1210-
view._isAddedToNativeVisualTree = true;
12111209
//@ts-ignore
12121210
view.parent = this;
1211+
view._setupAsRootView(this._context);
1212+
view._isAddedToNativeVisualTree = true;
12131213
view.callLoaded();
12141214
if (!CollectionViewCellHolder) {
12151215
CollectionViewCellHolder = com.nativescript.collectionview.CollectionViewCellHolder as any;

src/collectionview/index.ios.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,9 @@ export class CollectionView extends CollectionViewBase {
790790
cell.owner = new WeakRef(view);
791791
} else if (cell.view !== view) {
792792
this._removeContainer(cell);
793-
cell.view.nativeViewProtected.removeFromSuperview();
793+
if (cell.view.nativeViewProtected) {
794+
cell.view.nativeViewProtected.removeFromSuperview();
795+
}
794796
cell.owner = new WeakRef(view);
795797
}
796798
cell.currentIndex = indexPath.row;

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,12 +3539,12 @@ __metadata:
35393539

35403540
"@nativescript-community/template-snippet@file:demo-snippets::locator=root-workspace-0b6124%40workspace%3A.":
35413541
version: 0.0.1
3542-
resolution: "@nativescript-community/template-snippet@file:demo-snippets#demo-snippets::hash=1ef04f&locator=root-workspace-0b6124%40workspace%3A."
3542+
resolution: "@nativescript-community/template-snippet@file:demo-snippets#demo-snippets::hash=da5a49&locator=root-workspace-0b6124%40workspace%3A."
35433543
dependencies:
35443544
"@nativescript-community/ui-collectionview": "npm:*"
35453545
"@nativescript-community/ui-collectionview-swipemenu": "npm:*"
35463546
"@nativescript-community/ui-collectionview-waterfall": "npm:*"
3547-
checksum: a1f744e99ae9cee1d2eea10b7c1d4ca52388bb7ffb8fcef8d6ee70e96dd63c815967fca15bad6433df4226ca850e4a01e442a2cfc689d3d8a2b766e042be044a
3547+
checksum: 596e411195c9d7d7fda2044c29ee9ea298af546acc0eba2f0b7a2a9e7951ffdc739483ac2b7db14fbe4d4aec1ce12d4c5744dbb1f70ba04551b14f9a30cf5fab
35483548
languageName: node
35493549
linkType: hard
35503550

@@ -3563,7 +3563,7 @@ __metadata:
35633563
resolution: "@nativescript-community/ui-collectionview-swipemenu@workspace:packages/swipemenu"
35643564
dependencies:
35653565
"@nativescript-community/class-mixins": "npm:^1.0.0"
3566-
"@nativescript-community/ui-collectionview": "npm:^5.3.31"
3566+
"@nativescript-community/ui-collectionview": "npm:^5.3.32"
35673567
"@nativescript-community/ui-drawer": "npm:^0.1.27"
35683568
languageName: unknown
35693569
linkType: soft
@@ -3572,11 +3572,11 @@ __metadata:
35723572
version: 0.0.0-use.local
35733573
resolution: "@nativescript-community/ui-collectionview-waterfall@workspace:packages/waterfall"
35743574
dependencies:
3575-
"@nativescript-community/ui-collectionview": "npm:^5.3.31"
3575+
"@nativescript-community/ui-collectionview": "npm:^5.3.32"
35763576
languageName: unknown
35773577
linkType: soft
35783578

3579-
"@nativescript-community/ui-collectionview@npm:*, @nativescript-community/ui-collectionview@npm:^5.3.31, @nativescript-community/ui-collectionview@workspace:packages/collectionview":
3579+
"@nativescript-community/ui-collectionview@npm:*, @nativescript-community/ui-collectionview@npm:^5.3.32, @nativescript-community/ui-collectionview@workspace:packages/collectionview":
35803580
version: 0.0.0-use.local
35813581
resolution: "@nativescript-community/ui-collectionview@workspace:packages/collectionview"
35823582
languageName: unknown
@@ -19294,11 +19294,11 @@ __metadata:
1929419294

1929519295
"typescript@patch:typescript@npm%3A5.3.3#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A~5.3.3#optional!builtin<compat/typescript>":
1929619296
version: 5.3.3
19297-
resolution: "typescript@patch:typescript@npm%3A5.3.3#optional!builtin<compat/typescript>::version=5.3.3&hash=e012d7"
19297+
resolution: "typescript@patch:typescript@npm%3A5.3.3#optional!builtin<compat/typescript>::version=5.3.3&hash=29ae49"
1929819298
bin:
1929919299
tsc: bin/tsc
1930019300
tsserver: bin/tsserver
19301-
checksum: c93786fcc9a70718ba1e3819bab56064ead5817004d1b8186f8ca66165f3a2d0100fee91fa64c840dcd45f994ca5d615d8e1f566d39a7470fc1e014dbb4cf15d
19301+
checksum: 57bf073a0b88f9bf76e5a24be3df864a8f9d0e70f8316969f4992ae26e30cb8d078708922da2ade1f2c8d1faee13b28bcb69876ebb1e37910e31c54aa9126aa2
1930219302
languageName: node
1930319303
linkType: hard
1930419304

@@ -19324,7 +19324,7 @@ __metadata:
1932419324

1932519325
"typescript@patch:typescript@npm%3A~5.4.5#optional!builtin<compat/typescript>":
1932619326
version: 5.4.5
19327-
resolution: "typescript@patch:typescript@npm%3A5.4.5#optional!builtin<compat/typescript>::version=5.4.5&hash=e012d7"
19327+
resolution: "typescript@patch:typescript@npm%3A5.4.5#optional!builtin<compat/typescript>::version=5.4.5&hash=29ae49"
1932819328
bin:
1932919329
tsc: bin/tsc
1933019330
tsserver: bin/tsserver

0 commit comments

Comments
 (0)