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

Commit 95aa220

Browse files
Merge from dev for version 0.0.1
2 parents 4be755a + fdc680b commit 95aa220

16 files changed

+1023
-102
lines changed

README.MD

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# ionic-swing
22

3+
ionic-swing is a fork of the following projects intended to add the swipeable cards capatibilies to Ionic (>= 2)
4+
5+
- swing.js (version 4.2.0, https://github.com/gajus/swing)
6+
- angular2-swing (version 0.11.0, https://github.com/ksachdeva/angular2-swing)
7+
8+
The main reason behind this fork is that I was facing a problem with hammerjs, which was freezing the scrolling of my application's on iOS and Android when I was using (swipe) on a list or on an ion-scroll.
9+
After having unsuccessfully tried many solutions I was thinking, after all, that Ionic already include is own version of hammer.
10+
Therefore I decided to create this fork with the goal to make a version of swing.js which doesn't embed any external hammerjs.
11+
12+
## Current status
13+
14+
This fork project is in beta version but seems to work (tested with Ionic 3.0.1).
15+
16+
Possible improvements not yet done:
17+
18+
- Improve typescripting and remove class swing.ts
19+
- The fork special dependencies are, inherited from swing.js, lodash, raf, rebound, sister, vendor-prefix. For bundle size reason mostly, it should be checked if these can't be replaced with native code or smaller libs.
20+
321
## Installation
422

523
To install this library, run:
@@ -8,50 +26,27 @@ To install this library, run:
826
$ npm install ionic-swing --save
927
```
1028

11-
## Consuming your library
29+
## Usage
1230

13-
Once you have published your library to npm, you can import your library in any Angular application by running:
31+
### 1. Import the IonicSwingModule
1432

15-
```bash
16-
$ npm install ionic-swing
17-
```
18-
19-
and then from your Angular `AppModule`:
33+
In your app.module.ts, import the library like following:
2034

21-
```typescript
22-
import { BrowserModule } from '@angular/platform-browser';
23-
import { NgModule } from '@angular/core';
35+
import {IonicSwingModule} from 'ionic-swing';
2436

25-
import { AppComponent } from './app.component';
37+
and add it to your imports:
2638

27-
// Import your library
28-
import { SampleModule } from 'ionic-swing';
39+
imports: [
40+
...
41+
IonicSwingModule
42+
...
43+
]
2944

30-
@NgModule({
31-
declarations: [
32-
AppComponent
33-
],
34-
imports: [
35-
BrowserModule,
45+
### 2. To implement a card stack
3646

37-
// Specify your library as an import
38-
LibraryModule
39-
],
40-
providers: [],
41-
bootstrap: [AppComponent]
42-
})
43-
export class AppModule { }
44-
```
47+
To implement a card stack, follow the example provided by angular2-swing
4548

46-
Once your library is imported, you can use its components, directives and pipes in your Angular application:
47-
48-
```xml
49-
<!-- You can now use your library component in app.component.html -->
50-
<h1>
51-
{{title}}
52-
</h1>
53-
<sampleComponent></sampleComponent>
54-
```
49+
See https://github.com/ksachdeva/angular2-swing
5550

5651
## Development
5752

@@ -67,6 +62,10 @@ To lint all `*.ts` files:
6762
$ npm run lint
6863
```
6964

65+
## Side notes
66+
67+
Project structure created with https://github.com/jvandemo/generator-angular2-library
68+
7069
## License
7170

7271
MIT © [David Dal Busco](mailto:[email protected])

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@
4444
"karma-coverage-istanbul-reporter": "^0.2.0",
4545
"karma-jasmine": "~1.1.0",
4646
"karma-jasmine-html-reporter": "^0.2.2",
47+
"lodash": "^4.6.1",
4748
"node-watch": "^0.5.2",
4849
"protractor": "~5.1.0",
50+
"raf": "^3.1.0",
51+
"rebound": "^0.0.13",
4952
"rollup": "^0.41.6",
5053
"run-sequence": "^1.2.2",
5154
"rxjs": "^5.1.0",
55+
"sister": "^3.0.0",
5256
"ts-node": "~2.0.0",
5357
"tslint": "~4.5.0",
5458
"typescript": "~2.2.0",
59+
"vendor-prefix": "^0.1.0",
5560
"zone.js": "^0.8.4"
5661
},
5762
"engines": {
5863
"node": ">=6.0.0"
59-
}
64+
},
65+
"dependencies": {}
6066
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {Component, ElementRef, OnInit} from '@angular/core';
2+
import {SwingStackComponent} from './swing-stack-component';
3+
import {Card} from './swing';
4+
5+
@Component({
6+
selector: '[swing-card]',
7+
template: `
8+
<ng-content></ng-content>
9+
`
10+
})
11+
export class SwingCardComponent implements OnInit {
12+
constructor(
13+
private elmentRef: ElementRef,
14+
private swingStack: SwingStackComponent) {
15+
}
16+
17+
ngOnInit() {
18+
this.swingStack.addCard(this);
19+
}
20+
21+
getElementRef() {
22+
return this.elmentRef;
23+
}
24+
25+
getNativeElement() {
26+
return this.elmentRef.nativeElement;
27+
}
28+
29+
getCard(): Card {
30+
return this.swingStack.stack.getCard(this.getNativeElement());
31+
}
32+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {Component, Input,
2+
AfterContentInit, EventEmitter } from '@angular/core';
3+
4+
import {SwingCardComponent} from './swing-card-component';
5+
6+
import {StackConfig, ThrowEvent, DragEvent} from './swing';
7+
8+
import Stack from '../swing/stack';
9+
10+
@Component({
11+
selector: '[swing-stack]',
12+
template: `
13+
<ng-content></ng-content>
14+
`,
15+
outputs: [
16+
'throwout',
17+
'throwoutend',
18+
'throwoutleft',
19+
'throwoutright',
20+
'throwoutup',
21+
'throwoutdown',
22+
'throwin',
23+
'throwinend',
24+
'dragstart',
25+
'dragmove',
26+
'dragend',
27+
]
28+
})
29+
export class SwingStackComponent implements AfterContentInit {
30+
31+
@Input() stackConfig: StackConfig;
32+
33+
throwout: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
34+
throwoutend: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
35+
throwoutleft: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
36+
throwoutright: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
37+
throwoutup: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
38+
throwoutdown: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
39+
throwin: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
40+
throwinend: EventEmitter<ThrowEvent> = new EventEmitter<ThrowEvent>();
41+
42+
dragstart: EventEmitter<DragEvent> = new EventEmitter<DragEvent>();
43+
dragmove: EventEmitter<DragEvent> = new EventEmitter<DragEvent>();
44+
dragend: EventEmitter<DragEvent> = new EventEmitter<DragEvent>();
45+
46+
cards: SwingCardComponent[];
47+
stack: any;
48+
49+
constructor() {
50+
this.cards = [];
51+
}
52+
53+
addCard(card: SwingCardComponent) {
54+
this.cards.push(card);
55+
if (this.stack) {
56+
this.stack.createCard(card.getNativeElement());
57+
}
58+
}
59+
60+
ngAfterContentInit() {
61+
this.stack = Stack(this.stackConfig || {});
62+
this.cards.forEach((c) => this.stack.createCard(c.getNativeElement()));
63+
64+
// Hook various events
65+
this.stack.on('throwout', $event => this.throwout.emit($event));
66+
this.stack.on('throwoutend', $event => this.throwoutend.emit($event));
67+
this.stack.on('throwoutleft', $event => this.throwoutleft.emit($event));
68+
this.stack.on('throwoutright', $event => this.throwoutright.emit($event));
69+
this.stack.on('throwin', $event => this.throwin.emit($event));
70+
this.stack.on('throwinend', $event => this.throwinend.emit($event));
71+
this.stack.on('dragstart', $event => this.dragstart.emit($event));
72+
this.stack.on('dragmove', $event => this.dragmove.emit($event));
73+
this.stack.on('dragend', $event => this.dragend.emit($event));
74+
this.stack.on('throwoutup', $event => this.throwoutup.emit($event));
75+
this.stack.on('throwoutdown', $event => this.throwoutdown.emit($event));
76+
}
77+
}

src/angular2-swing/swing.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// here the ambient definitions for the swing
2+
// module are specified. Normally they should be at DefinitelyTyped
3+
// or better with the repository
4+
5+
export interface ThrowEvent {
6+
/**
7+
* The element being dragged.
8+
*/
9+
target: HTMLElement;
10+
11+
/**
12+
* The direction in which the element is being dragged: Card.DIRECTION_LEFT
13+
* or Card.DIRECTION_RIGHT
14+
*/
15+
throwDirection: any;
16+
}
17+
18+
export interface DragEvent {
19+
/**
20+
* The element being dragged.
21+
*/
22+
target: HTMLElement;
23+
24+
/**
25+
* Only available when the event is dragmove
26+
*/
27+
throwOutConfidence?: number;
28+
/**
29+
* Only available when the event is dragmove
30+
*/
31+
throwDirection?: any;
32+
/**
33+
* Only available when the event is dragmove
34+
*/
35+
offset?: number;
36+
37+
}
38+
39+
export type ThrowEventName = 'throwin' | 'throwinend' |
40+
'throwout' | 'throwoutend' | 'throwoutleft' | 'throwoutup' | 'throwoutdown' | 'throwoutright';
41+
42+
export type DragEventName = 'dragstart' | 'dragmove' | 'dragend';
43+
44+
export interface Card {
45+
/**
46+
* Unbinds all Hammer.Manager events.
47+
* Removes the listeners from the physics simulation.
48+
*
49+
* @return {undefined}
50+
*/
51+
destroy(): void;
52+
53+
/**
54+
* Throws a card into the stack from an arbitrary position.
55+
*
56+
* @param {Number} fromX
57+
* @param {Number} fromY
58+
* @return {undefined}
59+
*/
60+
throwIn(x: number, y: number): void;
61+
62+
/**
63+
* Throws a card out of the stack in the direction away from the original offset.
64+
*
65+
* @param {Number} fromX
66+
* @param {Number} fromY
67+
* @return {undefined}
68+
*/
69+
throwOut(x: number, y: number): void;
70+
71+
on(eventName: ThrowEventName, callabck: (event: ThrowEvent) => void): void;
72+
on(eventName: DragEventName, callabck: (event: DragEvent) => void): void;
73+
}
74+
75+
export interface StackConfig {
76+
77+
minThrowOutDistance?: number;
78+
maxThrowOutDistance?: number;
79+
maxRotation?: number;
80+
allowedDirections?: Array<any>;
81+
82+
/**
83+
* Determines if element is being thrown out of the stack.
84+
*
85+
* Element is considered to be thrown out when throwOutConfidence is equal to 1.
86+
*
87+
* @param {Number} offset Distance from the dragStart.
88+
* @param {HTMLElement} element Element.
89+
* @param {Number} throwOutConfidence config.throwOutConfidence
90+
* @return {Boolean}
91+
*/
92+
isThrowOut?: (offset: number, element: HTMLElement, throwOutConfidence: number) => boolean;
93+
94+
/**
95+
* Returns a value between 0 and 1 indicating the completeness of the throw out condition.
96+
*
97+
* Ration of the absolute distance from the original card position and element width.
98+
*
99+
* @param {Number} offsetX Distance from the dragStart.
100+
* @param {Number} offsetY Distance from the dragStart.
101+
* @param {HTMLElement} element Element.
102+
* @return {Number}
103+
*/
104+
throwOutConfidence?: (offsetX: number, offsetY: number, element: HTMLElement) => number;
105+
106+
/**
107+
* Calculates a distances at which the card is thrown out of the stack.
108+
*
109+
* @param {Number} min
110+
* @param {Number} max
111+
* @return {Number}
112+
*/
113+
throwOutDistance?: (min: number, max: number) => number;
114+
115+
/**
116+
* Calculates rotation based on the element x and y offset, element width and
117+
* maxRotation variables.
118+
*
119+
* @param {Number} x Horizontal offset from the startDrag.
120+
* @param {Number} y Vertical offset from the startDrag.
121+
* @param {HTMLElement} element Element.
122+
* @param {Number} maxRotation
123+
* @return {Number} Rotation angle expressed in degrees.
124+
*/
125+
rotation?: (x: number, y: number, element: HTMLElement, maxRotation: number) => number;
126+
127+
/**
128+
* Uses CSS transform to translate element position and rotation.
129+
*
130+
* Invoked in the event of `dragmove` and every time the physics solver is triggered.
131+
*
132+
* @param {HTMLElement} element
133+
* @param {Number} x Horizontal offset from the startDrag.
134+
* @param {Number} y Vertical offset from the startDrag.
135+
* @param {Number} r
136+
* @return {undefined}
137+
*/
138+
transform?: (element: HTMLElement, x: number, y: number, r: number) => void;
139+
}
140+

0 commit comments

Comments
 (0)