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

feat(carousel): add event on carousel slide with direction info #1406

Conversation

giampierobono
Copy link
Contributor

@giampierobono giampierobono commented Mar 25, 2017

Before submitting a pull request, please make sure you have at least performed the following:

  • read and followed the CONTRIBUTING.md guide.
  • built and tested the changes locally.
  • added/updated any applicable tests.
  • added/updated any applicable API documentation.
  • added/updated any applicable demos.

@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch 3 times, most recently from b73773e to 11a2818 Compare March 26, 2017 12:32
@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch 3 times, most recently from a654011 to 933d037 Compare March 28, 2017 20:15
@giampierobono
Copy link
Contributor Author

Don't understand why the build fails...in local it works

*/
export enum NgbSlideCarouselEventDirection {
LEFT = <any>'left',
RIGHT = <any>'right'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why type these to any and not make use of the implicit string typing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, you cannot assign directly a string to an enum value. In fact if I remove , typescript says: type string is no assignable to type NgbSlideCarouselEventDirection.
Is there another way to do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you should just be able to do

export enum NgbSlideCarouselEventDirection {
  left,
  right
}

and use it like NgbSlideCarouselEventDirection.left to represent the string value left

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but doing like this, NgbSlideCarouselEventDirection.left and NgbSlideCarouselEventDirection.right values won't be 0 and 1 instead of 'left' and 'right'?

Copy link
Contributor

@thbt thbt Apr 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using an union type on the variable ('left' | 'right') instead of an enum ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be easier to use as an enum instead of strings. I was imaging using it from outside and doing something:
event.direction === NgbSlideCarouselEventDirection.LEFT
instead of
event.direction === 'left'

@wesleycho
Copy link
Member

The build failure is some flakiness with Edge testing in Sauce Labs - that is to be looked into.

This PR looks mostly good to me.

this.onCarouselSlide.emit({
type: 'slide',
prevSlideId: this.activeId,
newSlideId: selectedSlide.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these can be shortened to prev and current.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree. I will change them. Thanks for the suggestion :)

@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch from 933d037 to 3973084 Compare April 10, 2017 05:58
const currentActiveSlideIdx = this._getSlideIdxById(currentActiveSlideId);
const nextActiveSlideIdx = this._getSlideIdxById(nextActiveSlideId);

return currentActiveSlideIdx > nextActiveSlideIdx ? NgbSlideCarouselEventDirection.RIGHT :
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that when you are on the first slide and click the back arrow that the direction would be RIGHT. However, when you are on the first slide with this logic it will always return LEFT, regardless of which direction you are moving in. The same thing applies if you are on the last slide, the direction will always be RIGHT, regardless of which direction you move in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are totally right! Thanks a lot!

I have just updated my PR. Now the logic is simpler, basically when you click on prev is always RIGHT and when you click on next is always LEFT. The computation based on the index is now done only clicking on the indicators.

@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch from 3973084 to a483b45 Compare April 13, 2017 07:20
@@ -43,7 +45,7 @@ export class NgbSlide {
},
template: `
<ol class="carousel-indicators">
<li *ngFor="let slide of slides" [id]="slide.id" [class.active]="slide.id === activeId" (click)="cycleToSelected(slide.id)"></li>
<li *ngFor="let slide of slides" [id]="slide.id" [class.active]="slide.id === activeId" (click)="select(slide.id)"></li>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behaviour in subtle way: now we would reset timer on click. Was it intentional? If so we should add a test for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it comes from the rebase. I am sorry, I will revert it. Let me know if you want to revert the package json as well and I will update my PR. Thanks for the review

* A carousel slide event fired when the slide transition is completed.
* See NgbSlideCarouselEvent for payload details
*/
@Output() onCarouselSlide = new EventEmitter<NgbSlideCarouselEvent>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we name it just slide? We are in the carousel context so no need to add the Carousel in the name. Similarly we know that this is an event so no need for on (we are not adding it for other directives / events).

package.json Outdated
"@angular/common": "2.3.1",
"@angular/core": "2.3.1",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? While it doesn't hurt it always best idea to avoid unrelated changes in a single PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this has been moved automatically by yarn when I did the update. It was not in alphabetical order. I can revert it if you prefer, no problem.

@@ -8,6 +8,7 @@ import {DEMO_SNIPPETS} from './demos';
<ngbd-api-docs directive="NgbCarousel"></ngbd-api-docs>
<ngbd-api-docs directive="NgbSlide"></ngbd-api-docs>
<ngbd-api-docs-config type="NgbCarouselConfig"></ngbd-api-docs-config>
<ngbd-api-docs-class type="NgbSlideCarouselEvent"></ngbd-api-docs-class>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this new functionality warrants a separate demo - 2 carousel side by side already look wired on our demo page so I would prefer adding more demos unless those really add a lot of value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it the way to update the documentation adding specific information about this new event?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation is generated automatically from the source code - as long as you've got JS doc on the @Output it will make it to the demo site.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to remove this but then in the demo I don't see any doc related to the NgbSlideCarouselEvent, am I doing something wrong?
I am talking about this:
screen shot 2017-04-23 at 22 36 35

@pkozlowski-opensource
Copy link
Member

@giampierobono thnx, it looks good generally speaking but I've left some comments that I would love to see addressed before we merge this one. Thnx again!

@pkozlowski-opensource pkozlowski-opensource added this to the 1.0.0-alpha.25 milestone Apr 23, 2017
@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch 2 times, most recently from cdbee4d to 940b069 Compare April 23, 2017 20:46
@pkozlowski-opensource
Copy link
Member

@giampierobono are you planning to do further work on this PR? If so could you please remove the demo part and address all the remaining comments so I could merge this PR? Thnx!

@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch from 940b069 to 3fd6a6d Compare May 2, 2017 11:33
@giampierobono
Copy link
Contributor Author

@pkozlowski-opensource I have just reverted the demo changes and I have already applied all the other changes in comments. I was waiting for your reply to my comment about the documentation.

The PR should be ok now. Let me know if something is still missing.

Thanks again.

@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch from 3fd6a6d to 305091e Compare May 2, 2017 13:05
/**
* Event type
*/
type: 'slide';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need type? Do we have other types and we need to distinguish between those? Isn't interface enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I have done this the first time, I have checked the bootstrap carousel doc and there are 2 events: slide and slid (the first is for transition start and the second for transition end if I remember correctly) and the initial plan was to reproduce the same behavior, that's why we have type.
Need to add the slid event but I had not time till now (don't even know if it's really needed), so I have pushed this. I can remove it if you want, I agree that since by now there is only one event it's not necessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, let's remove it. We should rather have 2 different event interfaces rather than trying to discriminate those by a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course 2 different events and not only one discriminated by strings :) it's the bs event structure having the 'type' prop so I have reproduced it. I am going to update the PR

@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch from 305091e to b9d8374 Compare May 2, 2017 14:12
@giampierobono giampierobono force-pushed the feature/event-on-carusel-change branch from b9d8374 to 8422200 Compare May 2, 2017 14:24
@pkozlowski-opensource
Copy link
Member

@giampierobono I was about to merge this PR but then I had a closer look and started to ask myself the following question: what is the use-case for exposing slide direction in the new event? Do you have a specific need? AFAIK it wasn't explicitly asked for in any of the prior issues (#1331, #1356).

If you don't have specific use-case I would prefer that we leave off this functionality as it adds a bit of code and currently people can't even visually see slide direction as we don't support animations (!)

@giampierobono
Copy link
Contributor Author

@pkozlowski-opensource sorry I have just seen your message.

For me one use-case is keep in sync the carousel and some panels below it (with text adding more details to each carousel image), that will be displayed or not, following the event direction without relying only on the image id. Or another use case could be bind to the event and perform an action only if the direction is "right" or "left" and not both.

@pkozlowski-opensource
Copy link
Member

@giampierobono sure, I can also imagine use cases - I was rather asking if you, or any users that you know, already has a specific use-case :-)

@giampierobono
Copy link
Contributor Author

giampierobono commented May 11, 2017

Ah ok :) For something I am working on, the second use-case is exactly what I would need...would be nice to have it.

@kesarion
Copy link

Is something still blocking this pull from being merged?

@jspizziri
Copy link

@pkozlowski-opensource,

Is there further work on this PR before it get's merged in? I have a couple of use-cases that this would be greatly helpful for.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants