Skip to content
This repository was archived by the owner on Aug 24, 2020. It is now read-only.

Commit 58eddc8

Browse files
breaking: refactor components to directives
1 parent 112a261 commit 58eddc8

File tree

11 files changed

+130
-126
lines changed

11 files changed

+130
-126
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<a name="3.0.0"></a>
2+
## [3.0.0](https://github.com/peterpeterparker/ionic-swing/compare/v2.3.1...v3.0.0) (2018-08-28)
3+
* **breaking**: Refactor components to directives in order to be compatible with Ionic v4 (>= beta.5) and/or Typescript v2.9.2
4+
15
<a name="2.3.1"></a>
26
## [2.3.1](https://github.com/peterpeterparker/ionic-swing/compare/v2.3.0...v2.3.1) (2018-08-27)
37
* **revert**: Revert rename `swing-card`

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ To implement a card stack, follow the example provided by angular2-swing
3535

3636
See https://github.com/ksachdeva/angular2-swing
3737

38+
### 3. ViewChild and ViewChildren in Ionic v4
39+
40+
In Ionic v4, in order to access the stack and cards as `ViewChild` and `ViewChildren`, it's mandatory to use the keyword `read` to identify correctly the elements
41+
42+
Html:
43+
44+
<div swingStack #swingStack>
45+
<ion-card swingCard #swingCards>
46+
</ion-card>
47+
</div>
48+
49+
Ts:
50+
51+
@ViewChild('swingStack', {read: SwingStackDirective}) swingStack: SwingStackDirective;
52+
@ViewChildren('swingCards', {read: SwingCardDirective}) swingCards: QueryList<SwingCardDirective>;
53+
3854
## Notes regarding hammerjs
3955

4056
This library need `hammerjs` but isn't shipped with it because some framework, like `Ionic v3`, already include it in their own resources. If it isn't your case, you would need to install `hammerjs` in your project

angular.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@
122122
"defaultProject": "ionic-swing",
123123
"schematics": {
124124
"@schematics/angular:component": {
125-
"prefix": "app",
125+
"prefix": "swing",
126126
"styleext": "scss"
127127
},
128128
"@schematics/angular:directive": {
129-
"prefix": "app"
129+
"prefix": "swing"
130130
}
131131
}
132-
}
132+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ionic-swing",
3-
"version": "2.3.1",
3+
"version": "3.0.0",
44
"license": "MIT",
55
"scripts": {
66
"ng": "ng",

src/app/modules/ionic-swing/components/swing-card.component.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/app/modules/ionic-swing/components/swing-stack.component.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Directive, ElementRef, OnInit} from '@angular/core';
2+
3+
import {SwingStackDirective} from './swing-stack.directive';
4+
import {Card} from '../interfaces/swing';
5+
6+
@Directive({
7+
selector: '[swingCard]'
8+
})
9+
export class SwingCardDirective implements OnInit {
10+
private card: Card;
11+
12+
constructor(
13+
private elementRef: ElementRef,
14+
private swingStack: SwingStackDirective) {
15+
}
16+
17+
ngOnInit() {
18+
this.card = this.swingStack.addCard(this);
19+
}
20+
21+
getElementRef() {
22+
return this.elementRef;
23+
}
24+
25+
getNativeElement() {
26+
return this.elementRef.nativeElement;
27+
}
28+
29+
getCard(): Card {
30+
return this.card;
31+
}
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
Input,
3+
AfterContentInit, EventEmitter, Output, Directive
4+
} from '@angular/core';
5+
6+
import {SwingCardDirective} from './swing-card.directive';
7+
8+
import {StackConfig, ThrowEvent, DragEvent} from '../interfaces/swing';
9+
10+
import Stack from '../swing/stack';
11+
12+
@Directive({
13+
selector: '[swingStack]'
14+
})
15+
export class SwingStackDirective implements AfterContentInit {
16+
17+
@Input() stackConfig: StackConfig;
18+
19+
@Output() throwout: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
20+
@Output() throwoutend: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
21+
@Output() throwoutleft: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
22+
@Output() throwoutright: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
23+
@Output() throwoutup: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
24+
@Output() throwoutdown: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
25+
@Output() throwin: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
26+
@Output() throwinend: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
27+
28+
@Output() dragstart: EventEmitter<DragEvent> = new EventEmitter<DragEvent>();
29+
@Output() dragmove: EventEmitter<DragEvent> = new EventEmitter<DragEvent>();
30+
@Output() dragend: EventEmitter<DragEvent> = new EventEmitter<DragEvent>();
31+
32+
cards: SwingCardDirective[];
33+
stack: any;
34+
35+
constructor() {
36+
this.cards = [];
37+
}
38+
39+
addCard(card: SwingCardDirective) {
40+
this.cards.push(card);
41+
if (this.stack) {
42+
return this.stack.createCard(card.getNativeElement());
43+
}
44+
}
45+
46+
ngAfterContentInit() {
47+
this.stack = Stack(this.stackConfig || {});
48+
this.cards.forEach((c) => this.stack.createCard(c.getNativeElement()));
49+
50+
// Hook various events
51+
this.stack.on('throwout', $event => this.throwout.emit($event));
52+
this.stack.on('throwoutend', $event => this.throwoutend.emit($event));
53+
this.stack.on('throwoutleft', $event => this.throwoutleft.emit($event));
54+
this.stack.on('throwoutright', $event => this.throwoutright.emit($event));
55+
this.stack.on('throwin', $event => this.throwin.emit($event));
56+
this.stack.on('throwinend', $event => this.throwinend.emit($event));
57+
this.stack.on('dragstart', $event => this.dragstart.emit($event));
58+
this.stack.on('dragmove', $event => this.dragmove.emit($event));
59+
this.stack.on('dragend', $event => this.dragend.emit($event));
60+
this.stack.on('throwoutup', $event => this.throwoutup.emit($event));
61+
this.stack.on('throwoutdown', $event => this.throwoutdown.emit($event));
62+
}
63+
}

src/app/modules/ionic-swing/ionic-swing.module.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {NgModule} from '@angular/core';
22
import {CommonModule} from '@angular/common';
33

4-
import {SwingCardComponent} from './components/swing-card.component';
5-
import {SwingStackComponent} from './components/swing-stack.component';
4+
import {SwingCardDirective} from './directives/swing-card.directive';
5+
import {SwingStackDirective} from './directives/swing-stack.directive';
66

7-
export * from './components/swing-card.component';
8-
export * from './components/swing-stack.component';
7+
export * from './directives/swing-card.directive';
8+
export * from './directives/swing-stack.directive';
99

1010
export * from './interfaces/swing';
1111

@@ -19,12 +19,12 @@ export * from './swing/utilities';
1919
CommonModule
2020
],
2121
declarations: [
22-
SwingCardComponent,
23-
SwingStackComponent
22+
SwingCardDirective,
23+
SwingStackDirective
2424
],
2525
exports: [
26-
SwingCardComponent,
27-
SwingStackComponent
26+
SwingCardDirective,
27+
SwingStackDirective
2828
]
2929
})
3030
export class IonicSwingModule {

0 commit comments

Comments
 (0)