Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the story change emitter and on initial load as well as on story and group change. #67

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions projects/ngx-stories/src/lib/ngx-stories.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StoryGroup } from '../lib/interfaces/interfaces';
import { CommonModule } from '@angular/common';
import { NgxStoriesService } from './ngx-stories.service';
import { NgxStoriesOptions } from '../lib/interfaces/interfaces';
import { onStoryGroupChange, triggerOnEnd, triggerOnExit, triggerOnSwipeUp } from './utils/story-event-emitters';
import { onStoryGroupChange, triggerOnEnd, triggerOnExit, triggerOnStoryChange, triggerOnSwipeUp } from './utils/story-event-emitters';
import "hammerjs";

@Component({
Expand Down Expand Up @@ -34,6 +34,7 @@ export class NgxStoriesComponent implements AfterViewInit {
@Output() triggerOnExit = triggerOnExit;
@Output() triggerOnSwipeUp = triggerOnSwipeUp;
@Output() onStoryGroupChange = onStoryGroupChange;
@Output() triggerOnStoryChange = triggerOnStoryChange;

currentStoryIndex: number = 0;
currentStoryGroupIndex: number = 0;
Expand Down Expand Up @@ -102,6 +103,7 @@ export class NgxStoriesComponent implements AfterViewInit {
// For images, start with default duration
this.startProgressInterval(storyDuration);
}
this.populateCurrentDetails(this.currentStoryIndex, this.currentStoryGroupIndex)
}

private setInitialStoryIndex() {
Expand Down Expand Up @@ -177,9 +179,9 @@ export class NgxStoriesComponent implements AfterViewInit {
direction === 'next'
? this.storyService.nextStory(this.storyGroups, this.currentStoryGroupIndex, this.currentStoryIndex, this.storyGroupChange.bind(this))
: this.storyService.prevStory(this.storyGroups, this.currentStoryGroupIndex, this.currentStoryIndex, this.storyGroupChange.bind(this));

this.currentStoryGroupIndex = storyGroupIndex;
this.currentStoryIndex = storyIndex;
this.currentStoryGroupIndex = storyGroupIndex;
this.currentStoryIndex = storyIndex;

//Trigger onEnd emitter when all the storieGroups are traversed.
if (this.currentStoryGroupIndex === this.storyGroups.length) {
Expand Down Expand Up @@ -343,4 +345,19 @@ export class NgxStoriesComponent implements AfterViewInit {
private storyGroupChange(storyGroupIndex: number = this.currentStoryGroupIndex) {
this.onStoryGroupChange.emit(storyGroupIndex);
}
private populateCurrentDetails(currentSIndex: number, currentSGIndex: number) {
try {
const dataToSend = {
currentPerson: this.storyGroups[currentSGIndex].name,
currentPersonIndex: currentSGIndex,
currentStory: this.storyGroups[currentSGIndex].stories[currentSIndex],
currentStoryIndex: currentSIndex,
previousStory: currentSIndex !== 0 ? this.storyGroups[currentSGIndex].stories[currentSIndex -1] : null,
previousStoryIndex: currentSIndex !== 0 ? currentSIndex : null
}
this.triggerOnStoryChange.emit(dataToSend);
} catch(error) {
console.error(error);
}
}
}
1 change: 1 addition & 0 deletions projects/ngx-stories/src/lib/utils/story-event-emitters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { EventEmitter } from '@angular/core';
export const triggerOnEnd: EventEmitter<void> = new EventEmitter<void>();
export const triggerOnExit: EventEmitter<void> = new EventEmitter<void>();
export const triggerOnSwipeUp: EventEmitter<void> = new EventEmitter<void>();
export const triggerOnStoryChange: EventEmitter<object> = new EventEmitter<object>();
export const onStoryGroupChange: EventEmitter<number> = new EventEmitter<number>();
1 change: 1 addition & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ <h1>Ngx-Stories Demo</h1>
(triggerOnEnd)="triggerOnEnd()"
(triggerOnExit)="triggerOnExit()"
(onStoryGroupChange)="triggerOnStoryGroupChange($event)"
(triggerOnStoryChange)="currentStoryDetails($event)"
></ngx-stories>
3 changes: 3 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ export class AppComponent {
triggerOnStoryGroupChange(storyGroup: number) {
console.log(storyGroup);
}
currentStoryDetails(eventData: object) {
console.log('dataToShow', eventData);
}
}
Loading