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

add delegate service toggle from other controller #1

Open
wants to merge 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.*.sw*
/.idea
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,59 @@ ionic-contrib-drawer
====================

A side menu drawer for Ionic apps

#####Setting up:

Clone the ionic-ion-drawer repo into the lib folder in your ionic project so it looks like:

*yourProjectName/www/lib/ionic-ion-drawer*

Add the ionic-contrib-drawer.js and ionic-contrib-drawer.css files to your index file

<link href="lib/ionic-ion-drawer/ionic.contrib.drawer.css" rel="stylesheet">
<script src="lib/ionic-ion-drawer/ionic.contrib.drawer.js"></script>

Add ionic.contrib.drawer to your Angular app:

angular.module('App', [
'ionic',
'ionic.contrib.drawer'
])

#####Usage:

Add the `<drawer>` directive to your ionic templates or index.html files.

#####Example:

<ion-view>
<ion-content>
<div class="bar bar-header bar-dark">
<h1 class="title">Example</h1>
</div>
</ion-content>
<drawer side="right">
<ion-content>
<h2>Menu</h2>
<button ng-click="closeDrawer()">Close</button>
<ion-list>
<ion-item>Friends</ion-item>
<ion-item>Favorites</ion-item>
<ion-item>Search</ion-item>
</ion-list>
</ion-content>
</drawer>
</ion-view>



Change the 'side' attribute of the `<drawer>` to either "right" or "left" to switch the side the drawer opens from.

#####Credits:

driftyco: https://github.com/driftyco (original build)

vitalyrotari: https://github.com/vitalyrotari (bug fixes, improved performance, background fading
transition)

brybott: https://github.com/brybott (right side functionality)
114 changes: 73 additions & 41 deletions ionic.contrib.drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ angular.module('ionic.contrib.drawer', ['ionic'])
.controller('drawerCtrl', ['$element', '$attrs', '$ionicGesture', '$document', '$ionicPlatform', function($element, $attr, $ionicGesture, $document, $ionicPlatform) {
var el = $element[0];
var dragging = false;
var startX, lastX, offsetX, newX, startDir;
var startX, lastX, offsetX, newX;

// How far to drag before triggering
var thresholdX = 15;
Expand All @@ -28,6 +28,8 @@ angular.module('ionic.contrib.drawer', ['ionic'])

var side = $attr.side === SIDE_LEFT ? SIDE_LEFT : SIDE_RIGHT;
var width = el.clientWidth;
var docWidth = $document[0].body.clientWidth;
console.log(docWidth)

// Handle back button
var unregisterBackAction;
Expand Down Expand Up @@ -94,7 +96,7 @@ angular.module('ionic.contrib.drawer', ['ionic'])
};

var doEndDrag = function(e) {
startX = lastX = offsetX = startDir = null;
startX = lastX = offsetX = null;
isTargetDrag = false;

if (!dragging) {
Expand All @@ -108,12 +110,22 @@ angular.module('ionic.contrib.drawer', ['ionic'])
var translateX = 0;
var opacity = 0;

if (newX < (-width / 2)) {
translateX = -width;
drawerState = STATE_CLOSE;
} else {
opacity = 1;
drawerState = STATE_OPEN;
if (side === SIDE_RIGHT){
if (newX > width / 2) {
translateX = width;
drawerState = STATE_CLOSE;
} else {
opacity = 1;
drawerState = STATE_OPEN;
}
} else if (side === SIDE_LEFT){
if (newX < (-width / 2)) {
translateX = -width;
drawerState = STATE_CLOSE;
} else {
opacity = 1;
drawerState = STATE_OPEN;
}
}

toggleOverlay(drawerState);
Expand All @@ -136,53 +148,82 @@ angular.module('ionic.contrib.drawer', ['ionic'])
startX = finger.pageX;
}

if (!startDir) {
startDir = dir;
}

lastX = finger.pageX;

if (startDir === 'down' || startDir === 'up') {
if (dir === 'down' || dir === 'up') {
return;
}

if (!dragging) {
//here at just the beginning of drag
// Dragged 15 pixels and finger is by edge
if (Math.abs(lastX - startX) > thresholdX) {
if (isOpen()) {
if (dir === SIDE_RIGHT) {
return;
if (side === SIDE_LEFT){
if (isOpen()) {
if (dir === SIDE_RIGHT) {
return;
}
} else {
if (dir === SIDE_LEFT) {
return;
}
}
} else {
if (dir === SIDE_LEFT) {
return;
} else if (side === SIDE_RIGHT){
if (isOpen()) {
if (dir === SIDE_LEFT) {
return;
}
} else {
if (dir === SIDE_RIGHT) {
return;
}
}
}

if (isTarget(e.target)) {
// Menu is open and drag has reached target
if (e.gesture.center.pageX < width && isOpen()) {
startTargetDrag(e);
} else if(startX < edgeX) {
} else if((startX < edgeX && side === SIDE_LEFT) || (startX > docWidth-edgeX && side === SIDE_RIGHT)) {
startDrag(e);
}
}
} else {
//here when we are dragging
e.gesture.srcEvent.stopImmediatePropagation();

// if fast gesture
if (e.gesture.deltaTime < 200) {
if (isOpen()) {
if (dir === SIDE_LEFT) {
return newX = -width;
if (side === SIDE_LEFT){
if (isOpen()) {
if (dir === SIDE_LEFT) {
return newX = -width;
}
} else {
if (dir === SIDE_RIGHT) {
return newX = 0;
}
}
} else {
if (dir === SIDE_RIGHT) {
return newX = 0;
} else if (side === SIDE_RIGHT){
if (isOpen()) {
if (dir === SIDE_RIGHT) {
return newX = width;
}
} else {
if (dir === SIDE_LEFT) {
return newX = 0;
}
}
}
}

newX = Math.min(0, (-width + (lastX - offsetX)));
if (side === SIDE_LEFT){
newX = Math.min(0, (-width + (lastX - offsetX)));
var opacity = 1 + (newX / width);
} else if (side === SIDE_RIGHT){
newX = Math.max(0, (width - (docWidth - lastX + offsetX)));
var opacity = 1 - (newX / width);
}

var opacity = 1 + (newX / width);

if (opacity < 0) {
opacity = 0;
Expand Down Expand Up @@ -241,6 +282,9 @@ angular.module('ionic.contrib.drawer', ['ionic'])
return {
restrict: 'E',
controller: 'drawerCtrl',
scope: {
side: '=side'
},
link: function($scope, $element, $attr, ctrl) {
$element.addClass($attr.side);

Expand All @@ -261,18 +305,6 @@ angular.module('ionic.contrib.drawer', ['ionic'])
};
}
}
}])

.directive('drawerClose', ['$rootScope', function($rootScope) {
return {
restrict: 'A',
link: function($scope, $element) {
$element.bind('click', function() {
var drawerCtrl = $element.inheritedData('$drawerController');
drawerCtrl.close();
});
}
}
}]);

})();