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

improved Swiper lifecycle by properly destroying it #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# angular-swiper
Directive that wraps nolimits4web/swiper library for AngularJS.
Directive that wraps [nolimits4web/swiper](https://github.com/nolimits4web/swiper) library for AngularJS.
License: Apache

## 1 Installation
Expand All @@ -23,7 +23,7 @@ Add `<script>`s to your `html` files for [angular](https://github.com/angular/bo

```html
<link rel="stylesheet" href="bower_components/swiper/dist/css/swiper.min.css">

<script src="../bower_components/swiper/dist/js/swiper.js"></script>
<script src="../bower_components/angular/angular.js"></script>
<script src="../dist/angular-swiper.js"></script>
Expand Down Expand Up @@ -152,9 +152,9 @@ angular.module('swiperApp')
$scope.swiper = {};

$scope.onReadySwiper = function (swiper) {

swiper.on('slideChangeStart', function () {

console.log('slideChangeStart');
});
};
Expand Down
53 changes: 28 additions & 25 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
{
"name": "angular-swiper",
"version": "0.3.2",
"homepage": "https://github.com/ksachdeva/angular-swiper",
"authors": [
"Kapil Sachdeva <[email protected]>"
],
"description": "Angular directive for nolimits4web/Swiper javascript library",
"main": "dist/angular-swiper.js",
"keywords": [
"angular",
"swiper"
],
"license": "Apache",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"swiper": "~3.0.5",
"angular": ">=1.3"
}
}
"name": "angular-swiper",
"version": "0.3.3",
"homepage": "https://github.com/ksachdeva/angular-swiper",
"authors": [
"Kapil Sachdeva <[email protected]>"
],
"description": "Angular directive for nolimits4web/Swiper javascript library",
"main": "dist/angular-swiper.js",
"keywords": [
"angular",
"swiper"
],
"license": "Apache",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"swiper": "~3.3.1",
"angular": ">=1.3"
},
"devDependencies": {
"jquery": "^2.2.3"
}
}
140 changes: 140 additions & 0 deletions demos/11b-nested-and-synced.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Simple</title>

<link rel="stylesheet" href="../bower_components/swiper/dist/css/swiper.css">

<style>
.myslides {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
height: 150px;
border-bottom: 1px solid gray;
}
.swiper-container-v {
background: #eee;
}

ks-swiper-container {
width: 100%;
height: 100%;
}

.swiper-container {
width: 100%;
height: 100%;
}

.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
display: flex;
justify-content: center;
align-items: center;
}
</style>

</head>

<body>

<div ng-app="demoApp" ng-controller="MainCtrl">
<button ng-click="toggleView()">Toggle View</button>
Destroy?
<div ng-controller="TestCtrl">
<div ng-if="mainViewVisible">
<h1>1</h1>
<div class="myslides">
<ks-swiper-container swiper="test.marauder" on-ready="test.onReadySwiper(swiper)" loop="false" show-nav-buttons="false" slides-per-view="1" space-between="50" pagination-is-active="true" pagination=".swiper-pagination-v" pagination-clickable="true" pagination-cls="swiper-pagination-v">
<ks-swiper-slide ng-repeat="s in [1,2,3]">
Slide {{s}}
<img ng-src="http://api.randomuser.me/portraits/thumb/men/{{s}}.jpg">
</ks-swiper-slide>
</ks-swiper-container>
</div>
<h1>2</h1>
<div class="myslides" ng-controller="SyncedSwipersCtrl">
<ks-swiper-container on-ready="test.onReadySwiper(swiper)" loop="false" show-nav-buttons="false" slides-per-view="1" space-between="50" pagination-is-active="true" pagination=".swiper-pagination-v" pagination-clickable="true" pagination-cls="swiper-pagination-v">
<ks-swiper-slide ng-repeat="h in [1,2,3]">
<ks-swiper-container direction="vertical" initial-slide="1" on-ready="syncedSwipers.onReadySwiper(swiper)" loop="false" show-nav-buttons="false" slides-per-view="1" space-between="50" pagination-is-active="true" pagination=".swiper-pagination-h" pagination-clickable="true" container-cls="swiper-container-v" pagination-cls="swiper-pagination-h">
<ks-swiper-slide ng-repeat="s in [1,2,3]">
Horizontal {{h}}
Slide {{s}}
<img ng-src="http://api.randomuser.me/portraits/thumb/men/{{s}}.jpg">
</ks-swiper-slide>
</ks-swiper-container>
</ks-swiper-slide>
</ks-swiper-container>
</div>
</div>
</div>
</div>

<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../bower_components/swiper/dist/js/swiper.jquery.js"></script>
<script src="../bower_components/angular/angular.js"></script>
<script src="../src/angular-swiper.js"></script>

<script>
var module = angular.module('demoApp', ['ksSwiper']);
module.controller('TestCtrl', function($scope) {
$scope.swiper = {};
$scope.next = function() {
$scope.swiper.slideNext();
};
$scope.test = {
marauder: {}
};
$scope.test.onReadySwiper = function(swiper) {
console.log('onReadySwiper');
swiper.on('slideChangeStart', function() {
console.log('slideChangeStart');
});
};
});
module.controller('SyncedSwipersCtrl', function($scope) {
$scope.syncedSwipers = {
swipers: [],
index: 0
};
var onSlideChangeEnd = function(swiper) {
console.log('slide changed to', swiper.activeIndex);
var swipers = $scope.syncedSwipers.swipers
for (var i = 0; i < swipers.length; ++i) {
if (swiper === swipers[i]) {
//console.log('skipping slider', i);
continue;
}
swipers[i].slideTo(swiper.activeIndex, 0, false); //no-animation and silent
}
$scope.syncedSwipers.index = swiper.activeIndex;
}
$scope.syncedSwipers.addSwiper = function(swiper) {
$scope.syncedSwipers.swipers.push(swiper);
swiper.on('onSlideChangeEnd', onSlideChangeEnd);
$scope.syncedSwipers.index = swiper.activeIndex;
};
$scope.syncedSwipers.onReadySwiper = function(swiper) {
$scope.syncedSwipers.addSwiper(swiper);
};

});
module.controller('MainCtrl', function($scope) {
$scope.mainViewVisible = true;
$scope.toggleView = function() {
$scope.mainViewVisible = !$scope.mainViewVisible;
};
});
</script>

</body>

</html>
152 changes: 152 additions & 0 deletions demos/11c-nested-synced-keyboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Simple</title>

<link rel="stylesheet" href="../bower_components/swiper/dist/css/swiper.css">

<style>
.myslides {
background: #eee;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-size: 14px;
color: #000;
margin: 0;
padding: 0;
height: 150px;
border-bottom: 1px solid gray;
}
.swiper-container-v {
background: #eee;
}

ks-swiper-container {
width: 100%;
height: 100%;
}

.swiper-container {
width: 100%;
height: 100%;
}

.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
display: flex;
justify-content: center;
align-items: center;
}
</style>

</head>

<body>

<div ng-app="demoApp" ng-controller="MainCtrl">
<button ng-click="toggleView()">Toggle View</button>
Destroy?
<div ng-controller="TestCtrl">
<div ng-if="mainViewVisible">
<h1>1</h1>
<div class="myslides">
<ks-swiper-container swiper="test.marauder" on-ready="test.onReadySwiper(swiper)" loop="false" show-nav-buttons="false" slides-per-view="1" space-between="50" pagination-is-active="true" pagination=".swiper-pagination-v" pagination-clickable="true" pagination-cls="swiper-pagination-v">
<ks-swiper-slide ng-repeat="s in [1,2,3]">
Slide {{s}}
<img ng-src="http://api.randomuser.me/portraits/thumb/men/{{s}}.jpg">
</ks-swiper-slide>
</ks-swiper-container>
</div>
<h1>2</h1>
<div class="myslides" ng-controller="SyncedSwipersCtrl">
<ks-swiper-container on-ready="test.onReadySwiper(swiper)" loop="false" keyboard-control="true" show-nav-buttons="false" slides-per-view="1" space-between="50" pagination-is-active="true" pagination=".swiper-pagination-v" pagination-clickable="true" pagination-cls="swiper-pagination-v">
<ks-swiper-slide ng-repeat="group in grid.groups">
<ks-swiper-container direction="vertical" initial-slide="1" on-ready="syncedSwipers.onReadySwiper(swiper)" loop="false" keyboard-control="true" show-nav-buttons="false" slides-per-view="1" space-between="50" pagination-is-active="true" pagination=".swiper-pagination-h" pagination-clickable="true" container-cls="swiper-container-v" pagination-cls="swiper-pagination-h">
<ks-swiper-slide ng-repeat="slide in group">
{{slide}}
<img ng-src="http://api.randomuser.me/portraits/thumb/men/{{$index + 1}}.jpg">
</ks-swiper-slide>
</ks-swiper-container>
</ks-swiper-slide>
</ks-swiper-container>
</div>
</div>
</div>
</div>

<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../bower_components/swiper/dist/js/swiper.jquery.js"></script>
<script src="../bower_components/angular/angular.js"></script>
<script src="../src/angular-swiper.js"></script>

<script>
var module = angular.module('demoApp', ['ksSwiper']);
module.controller('TestCtrl', function($scope) {
$scope.swiper = {};
$scope.next = function() {
$scope.swiper.slideNext();
};
$scope.test = {
marauder: {}
};
$scope.test.onReadySwiper = function(swiper) {
console.log('onReadySwiper');
swiper.on('slideChangeStart', function() {
console.log('slideChangeStart');
});
};
});
module.controller('SyncedSwipersCtrl', function($scope) {
$scope.syncedSwipers = {
swipers: [],
index: 0
};
var onSlideChangeEnd = function(swiper) {
console.log('slide changed to', swiper.activeIndex);
var swipers = $scope.syncedSwipers.swipers
for (var i = 0; i < swipers.length; ++i) {
if (swiper === swipers[i]) {
//console.log('skipping slider', i);
continue;
}
swipers[i].slideTo(swiper.activeIndex, 0, false); //no-animation and silent
}
$scope.syncedSwipers.index = swiper.activeIndex;
}
$scope.syncedSwipers.addSwiper = function(swiper) {
$scope.syncedSwipers.swipers.push(swiper);
swiper.on('onSlideChangeEnd', onSlideChangeEnd);
$scope.syncedSwipers.index = swiper.activeIndex;
};
$scope.syncedSwipers.onReadySwiper = function(swiper) {
$scope.syncedSwipers.addSwiper(swiper);
};

});
module.controller('MainCtrl', function($scope) {
$scope.mainViewVisible = true;
$scope.grid = {
groups: []
};

for(var i = 0; i < 3; ++i) {
var slides = [];
for(var j = 0; j < 3; ++j) {
var name = 'Group: ' + i + ', Slide: ' + j
slides.push(name);
}
$scope.grid.groups.push(slides);
}

$scope.toggleView = function() {
$scope.mainViewVisible = !$scope.mainViewVisible;
};
});
</script>

</body>

</html>
Loading