This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix(carousel): #488 Carousel fails to update model changes. #2953
Closed
kkruit
wants to merge
2
commits into
angular-ui:master
from
kkruit:488-carousel-respects-order-changes
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(carousel): #488 Carousel fails to update model changes.
- Uses dom element index. - Adds order to indicators to match slide order.
- Loading branch information
commit bfa58c0a8d99e2111dbadaf21e91b7a9ecb5fef3
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
| var destroyed = false; | ||
| /* direction: "prev" or "next" */ | ||
| self.select = $scope.select = function(nextSlide, direction) { | ||
| var nextIndex = slides.indexOf(nextSlide); | ||
| var nextIndex = self.indexOfSlide(nextSlide); | ||
| //Decide direction if it's not given | ||
| if (direction === undefined) { | ||
| direction = nextIndex > currentIndex ? 'next' : 'prev'; | ||
|
|
@@ -59,7 +59,7 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
| transitionDone(nextSlide, self.currentSlide); | ||
| } | ||
| self.currentSlide = nextSlide; | ||
| currentIndex = nextIndex; | ||
| currentIndex = self.indexOfSlide(nextSlide); | ||
| //every time you change slides, reset the timer | ||
| restartTimer(); | ||
| } | ||
|
|
@@ -72,27 +72,55 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
| $scope.$on('$destroy', function () { | ||
| destroyed = true; | ||
| }); | ||
|
|
||
| /* Allow outside people to call indexOf on slides array */ | ||
| self.indexOfSlide = function(slide) { | ||
| return slides.indexOf(slide); | ||
| if(angular.isUndefined(slide.$element)) { | ||
| return slides.indexOf(slide); | ||
| } | ||
| var slideElements = slide.$element.parent().children(); | ||
| for(var i = 0; i < slideElements.length; i++){ | ||
| if(slideElements[i] === slide.$element[0]){ | ||
| return i; | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| $scope.next = function() { | ||
| var newIndex = (currentIndex + 1) % slides.length; | ||
| $scope.indexOfSlide = function (slide) { | ||
| return self.indexOfSlide(slide); | ||
| }; | ||
|
|
||
| $scope.next = function() { | ||
| //Prevent this user-triggered transition from occurring if there is already one in progress | ||
| if (!$scope.$currentTransition) { | ||
| return self.select(slides[newIndex], 'next'); | ||
| var nextSlide = null; | ||
| if(angular.isUndefined(self.currentSlide.$element)) { | ||
| return false; | ||
| } | ||
| var nextElement = self.currentSlide.$element.next(); | ||
| if(nextElement.length === 1){ | ||
| nextSlide = getSlideFromElement(nextElement); | ||
| } else { | ||
| nextSlide = getFirstSlide(); | ||
| } | ||
| return self.select(nextSlide, 'next'); | ||
| } | ||
| }; | ||
|
|
||
| $scope.prev = function() { | ||
| var newIndex = currentIndex - 1 < 0 ? slides.length - 1 : currentIndex - 1; | ||
|
|
||
| //Prevent this user-triggered transition from occurring if there is already one in progress | ||
| if (!$scope.$currentTransition) { | ||
| return self.select(slides[newIndex], 'prev'); | ||
| var prevSlide = null; | ||
| if(angular.isUndefined(self.currentSlide.$element)) { | ||
| return false; | ||
| } | ||
| var prevElement = self.currentSlide.$element[0].previousElementSibling; | ||
| if(prevElement !== null){ | ||
| prevSlide = getSlideFromElement(prevElement); | ||
| } else { | ||
| prevSlide = getLastSlide(); | ||
| } | ||
| return self.select(prevSlide, 'next'); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -140,6 +168,25 @@ angular.module('ui.bootstrap.carousel', ['ui.bootstrap.transition']) | |
| } | ||
| }; | ||
|
|
||
| function getSlideFromElement(element) { | ||
| return angular.element(element).scope().$$childHead; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about relying on the scope hierarchy ( |
||
| } | ||
|
|
||
| function getSlideElements() { | ||
| return self.currentSlide.$element.parent().children(); | ||
| } | ||
|
|
||
| function getLastSlide() { | ||
| var slideElements = getSlideElements(); | ||
| var lastSlideElement = slideElements[slideElements.length-1]; | ||
| return getSlideFromElement(lastSlideElement); | ||
| } | ||
|
|
||
| function getFirstSlide(){ | ||
| var firstSlideElement = getSlideElements()[0]; | ||
| return getSlideFromElement(firstSlideElement); | ||
| } | ||
|
|
||
| self.addSlide = function(slide, element) { | ||
| slide.$element = element; | ||
| slides.push(slide); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
previnstead?