Skip to content

Commit d9b4f9c

Browse files
committed
Add swipe to images and set as default method
1 parent 4dd9773 commit d9b4f9c

4 files changed

Lines changed: 101 additions & 12 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ npm install nativescript-ng2-carousel --save
4242

4343
3. To use the CarouselDirective, create a template that applies the directive as an attribute to a paragraph GridLayout element:
4444
<pre>
45-
&lt;GridLayout [carousel]="images" carouselLabelOverlay="true" carouselSpeed="2000"&gt;
45+
&lt;GridLayout [carousel]="images"
46+
carouselLabelOverlay="true"
47+
carouselSpeed="2000"
48+
(selectedImageChange)="select($event)"&gt;
4649

4750
&lt;/GridLayout&gt;
4851
</pre>
@@ -133,8 +136,10 @@ Currently directive supported attributes:
133136
* **carousel** list of images as JSON or CarouselSlide class, accepted parameters (title?, url?, file?)
134137
* **carouselSpeed** _(optional)_ defines the interval (number in ms) to wait before the next slide is shown
135138
* **carouselAnimationSpeed** _(optional)_ defines the animation speed (number in ms)
139+
* **arrowsEnabled** _(optional)_ Show arrows to navigate the carousel (default false since swipe it's preferred)
136140
* **carouselArrows** _(optional)_ arrow type, accepted values _none_, _small_, _normal_, _bold_ or _narrow_ (default _normal_)
137141
* **carouselLabelOverlay** _(optional)_ silde title over image, accepted values _true_ or _false_ (default false)
142+
* **selectedImageChange** _(optional)_ get the title of the selected image (key)
138143

139144
## Changelog
140145

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import {Directive, ElementRef, AfterViewInit, Input} from '@angular/core';
1+
import {AfterViewInit, Directive, ElementRef, EventEmitter, Input, Output} from '@angular/core';
22
import {AnimationCurve} from "ui/enums";
33
import {Image} from "ui/image";
44
import {StackLayout} from "ui/layouts/stack-layout";
55
import {GridLayout, ItemSpec} from "ui/layouts/grid-layout";
6-
import {GridUnitType} from "ui/layouts/grid-layout";
7-
import {HorizontalAlignment} from "ui/enums";
86
import {Label} from "ui/label";
9-
import {GestureTypes} from "ui/gestures";
7+
import {GestureTypes, SwipeGestureEventData} from "ui/gestures";
108
import {View} from "ui/core/view";
11-
import {Visibility} from "ui/enums";
12-
import {fromFile} from "image-source";
13-
import {fromResource} from "image-source";
9+
import {fromFile, fromResource} from "image-source";
10+
11+
var labelModule = require("ui/label");
1412

1513
@Directive({selector: '[carousel]'})
1614
export class CarouselDirective implements AfterViewInit {
@@ -37,19 +35,27 @@ export class CarouselDirective implements AfterViewInit {
3735
@Input() carousel: any;
3836
@Input() carouselSpeed: number; // autoplay speed (ms)
3937
@Input() carouselArrows: string; // arrows type
38+
@Input() arrowsEnabled: boolean = false; // enable arrows [default to false]
4039
@Input() carouselLabelOverlay: boolean; // title over image (bool)
4140
@Input() carouselAnimationSpeed: number; // animation speed
4241

42+
@Output() selectedImageChange: EventEmitter<string> = new EventEmitter<string>();
43+
4344
constructor(private elem: ElementRef) {
4445
this.container = elem.nativeElement;
46+
47+
4548
}
4649

4750
ngAfterViewInit() {
4851
this.initOptions();
4952
this.initContainer();
5053
this.initImagesLayout();
5154
this.initSlides();
52-
this.initControls();
55+
// Prefer swipe over arrows tap
56+
if (this.arrowsEnabled) {
57+
this.initControls();
58+
}
5359
this.initAutoPlay();
5460
}
5561

@@ -128,16 +134,46 @@ export class CarouselDirective implements AfterViewInit {
128134

129135
if (slide.url) {
130136
let image: Image = CarouselDirective.generateImageSliderFromUrl(slide.url);
137+
image.on(GestureTypes.tap, () => {
138+
this.selectedImageChange.emit( slide.title );
139+
});
140+
image.on(GestureTypes.swipe, (args: SwipeGestureEventData) => {
141+
if (args.direction === 1) {
142+
this.swipe(CarouselDirections.DIRECTION_LEFT);
143+
} else if (args.direction === 2) {
144+
this.swipe(CarouselDirections.DIRECTION_RIGHT);
145+
}
146+
});
131147
gridLayout.addChild(image);
132148
}
133149

134150
if (slide.file && slide.file.indexOf('res://') !== 0) {
135151
let image: Image = CarouselDirective.generateImageSliderFromFile(slide.file);
152+
image.on(GestureTypes.tap, () => {
153+
this.selectedImageChange.emit( slide.title );
154+
});
155+
image.on(GestureTypes.swipe, (args: SwipeGestureEventData) => {
156+
if (args.direction === 1) {
157+
this.swipe(CarouselDirections.DIRECTION_LEFT);
158+
} else if (args.direction === 2) {
159+
this.swipe(CarouselDirections.DIRECTION_RIGHT);
160+
}
161+
});
136162
gridLayout.addChild(image);
137163
}
138164

139165
if (slide.file && slide.file.indexOf('res://') === 0) {
140166
let image: Image = CarouselDirective.generateImageSliderFromResource(slide.file);
167+
image.on(GestureTypes.tap, () => {
168+
this.selectedImageChange.emit( slide.title );
169+
});
170+
image.on(GestureTypes.swipe, (args: SwipeGestureEventData) => {
171+
if (args.direction === 1) {
172+
this.swipe(CarouselDirections.DIRECTION_LEFT);
173+
} else if (args.direction === 2) {
174+
this.swipe(CarouselDirections.DIRECTION_RIGHT);
175+
}
176+
});
141177
gridLayout.addChild(image);
142178
}
143179

package-lock.json

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"version": "0.1.0",
44
"description": "A simple NativeScript + Angular images carousel for iOS and Android",
55
"license": "MIT",
6-
"main": "nativescript-ng2-carousel",
6+
"main": "nativescript-ng2-carousel-swipeable",
77
"homepage": "https://github.com/alcoceba/ngCarouselDirective#readme",
88
"repository": {
99
"type": "git",
1010
"url": "git+https://github.com/alcoceba/ngCarouselDirective.git"
1111
},
12-
"typings": "nativescript-ng2-carousel.ts",
12+
"typings": "nativescript-ng2-carousel-swipeable.ts",
1313
"keywords": [
1414
"angular",
1515
"nativescript",
@@ -44,8 +44,9 @@
4444
"zone.js": "^0.8.0"
4545
},
4646
"author": "Manel Alcoceba <manelalcoceba@gmail.com>",
47+
"contributors": ["Federico Diaz <fdiazaguirre@gmail.com>"],
4748
"bugs": {
48-
"url": "https://github.com/alcoceba/ngCarouselDirective/issues"
49+
"url": "https://github.com/fdiazaguirre/ngCarouselDirective/issues"
4950
},
5051
"scripts": {
5152
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)