Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.

Commit 8528dea

Browse files
committed
update URL with slide number
1 parent 540f2fa commit 8528dea

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/js/index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,38 @@ angular.module('ngSlidedeck', ['ngSlidedeckTemplates', 'ngAnimate'])
1414
},
1515
transclude: true,
1616
templateUrl: 'ngSlidedeckTemplates/slides.html',
17-
controller: ['$scope', function($scope) {
17+
controller: ['$scope', '$location', function($scope, $location) {
18+
// watch URL search part
19+
$scope.$watch(function() {
20+
return $location.search();
21+
}, function(val) {
22+
val = Number.parseInt(val.slide);
23+
if (Number.isNaN(val)) {
24+
val = 1;
25+
}
26+
$scope.slideIndex = val - 1;
27+
});
28+
1829
var slides = $scope.slides = [];
1930

2031
var setSlideIndex = function(index, oldIndex) {
32+
if (oldIndex !== undefined &&
33+
oldIndex >= 0 && oldIndex < slides.length) {
34+
slides[oldIndex].selected = false;
35+
}
2136
if (index === undefined || index < 0 || index >= slides.length) {
2237
return;
2338
}
24-
if (oldIndex !== undefined) {
25-
slides[oldIndex].selected = false;
26-
}
2739
slides[index].selected = true;
2840
$scope.slideIndex = index;
41+
$location.search({slide: index + 1});
2942
};
3043
$scope.$watch('slideIndex', setSlideIndex);
3144

3245
this.addSlide = function(slide) {
3346
slides.push(slide);
34-
if (slides.length === 1) {
35-
setSlideIndex(0);
47+
if (slides.length === $scope.slideIndex + 1) {
48+
setSlideIndex($scope.slideIndex);
3649
}
3750
};
3851

0 commit comments

Comments
 (0)