diff --git a/dist/js/ionic-angular.js b/dist/js/ionic-angular.js index 11199ec5a0..5a9635369a 100644 --- a/dist/js/ionic-angular.js +++ b/dist/js/ionic-angular.js @@ -975,27 +975,18 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) angular.extend(this, ionic.controllers.SideMenuController.prototype); ionic.controllers.SideMenuController.call(this, { + // Our quick implementation of the left side menu left: { width: 270, - pushDown: function() { - $scope.leftZIndex = -1; - }, - bringUp: function() { - $scope.leftZIndex = 0; - } }, + + // Our quick implementation of the right side menu right: { width: 270, - pushDown: function() { - $scope.rightZIndex = -1; - }, - bringUp: function() { - $scope.rightZIndex = 0; - } } }); - $scope.contentTranslateX = 0; + $scope.sideMenuContentTranslateX = 0; $scope.sideMenuCtrl = this; }) @@ -1027,28 +1018,32 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) defaultPrevented = e.defaultPrevented; }); - Gesture.on('drag', function(e) { + var dragFn = function(e) { if(defaultPrevented) { return; } sideMenuCtrl._handleDrag(e); - }, $element[0]); + }; - Gesture.on('release', function(e) { + Gesture.on('drag', dragFn, $element[0]); + + var dragReleaseFn = function(e) { if(!defaultPrevented) { sideMenuCtrl._endDrag(e); } defaultPrevented = false; - }, $element[0]); + }; + + Gesture.on('release', dragReleaseFn, $element[0]); sideMenuCtrl.setContent({ onDrag: function(e) {}, endDrag: function(e) {}, getTranslateX: function() { - return $scope.contentTranslateX || 0; + return $scope.sideMenuContentTranslateX || 0; }, setTranslateX: function(amount) { - $scope.contentTranslateX = amount; + $scope.sideMenuContentTranslateX = amount; $element[0].style.webkitTransform = 'translate3d(' + amount + 'px, 0, 0)'; }, enableAnimation: function() { @@ -1062,6 +1057,12 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) $element[0].classList.remove('menu-animated'); } }); + + // Cleanup + $scope.$on('$destroy', function() { + Gesture.off('drag', dragFn); + Gesture.off('release', dragReleaseFn); + }); }; } }; @@ -1080,10 +1081,23 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) return function($scope, $element, $attr, sideMenuCtrl) { $scope.side = $attr.side; + if($scope.side == 'left') { sideMenuCtrl.left.isEnabled = true; + sideMenuCtrl.left.pushDown = function() { + $element[0].style.zIndex = -1; + }; + sideMenuCtrl.left.bringUp = function() { + $element[0].style.zIndex = 0; + }; } else if($scope.side == 'right') { sideMenuCtrl.right.isEnabled = true; + sideMenuCtrl.right.pushDown = function() { + $element[0].style.zIndex = -1; + }; + sideMenuCtrl.right.bringUp = function() { + $element[0].style.zIndex = 0; + }; } $element.append(transclude($scope)); diff --git a/dist/js/ionic.js b/dist/js/ionic.js index b12d4a9664..facf3265eb 100644 --- a/dist/js/ionic.js +++ b/dist/js/ionic.js @@ -1836,6 +1836,15 @@ window.ionic = { (function(ionic) { ionic.Utils = { + + // Return a function that will be called with the given context + proxy: function(func, context) { + var args = Array.prototype.slice.call(arguments, 2); + return function() { + return func.apply(context, args.concat(Array.prototype.slice.call(arguments))); + }; + }, + throttle: function(func, wait, options) { var context, args, result; var timeout = null; @@ -1915,9 +1924,11 @@ window.ionic = { } }; + // Bind a few of the most useful functions to the ionic scope ionic.inherit = ionic.Utils.inherit; ionic.extend = ionic.Utils.extend; ionic.throttle = ionic.Utils.throttle; + ionic.proxy = ionic.Utils.proxy; })(window.ionic); ; @@ -3467,6 +3478,39 @@ window.ionic = { } }); + ionic.views.SideMenuContent = ionic.views.View.inherit({ + initialize: function(opts) { + var _this = this; + + ionic.extend(this, { + animationClass: 'menu-animated', + onDrag: function(e) {}, + onEndDrag: function(e) {}, + }, opts); + + ionic.onGesture('drag', ionic.proxy(this._onDrag, this), this.el); + ionic.onGesture('release', ionic.proxy(this._onEndDrag, this), this.el); + }, + _onDrag: function(e) { + this.onDrag && this.onDrag(e); + }, + _onEndDrag: function(e) { + this.onEndDrag && this.onEndDrag(e); + }, + disableAnimation: function() { + this.el.classList.remove(this.animationClass); + }, + enableAnimation: function() { + this.el.classList.add(this.animationClass); + }, + getTranslateX: function() { + return parseFloat(this.el.style.webkitTransform.replace('translate3d(', '').split(',')[0]); + }, + setTranslateX: function(x) { + this.el.style.webkitTransform = 'translate3d(' + x + 'px, 0, 0)'; + } + }); + })(ionic); ; /** @@ -4264,7 +4308,7 @@ ionic.controllers.NavController = ionic.controllers.ViewController.inherit({ self._handleDrag(e); }; - this.content.endDrag = function(e) { + this.content.onEndDrag =function(e) { self._endDrag(e); }; } @@ -4354,12 +4398,12 @@ ionic.controllers.NavController = ionic.controllers.ViewController.inherit({ */ openPercentage: function(percentage) { var p = percentage / 100; - var maxLeft = this.left.width; - var maxRight = this.right.width; - if(percentage >= 0) { - this.openAmount(maxLeft * p); - } else { - this.openAmount(maxRight * p); + + if(this.left && percentage >= 0) { + this.openAmount(this.left.width * p); + } else if(this.right && percentage < 0) { + var maxRight = this.right.width; + this.openAmount(this.right.width * p); } }, @@ -4369,11 +4413,11 @@ ionic.controllers.NavController = ionic.controllers.ViewController.inherit({ * negative value for right menu (only one menu will be visible at a time). */ openAmount: function(amount) { - var maxLeft = this.left.width; - var maxRight = this.right.width; + var maxLeft = this.left && this.left.width || 0; + var maxRight = this.right && this.right.width || 0; // Check if we can move to that side, depending if the left/right panel is enabled - if((!this.left.isEnabled && amount > 0) || (!this.right.isEnabled && amount < 0)) { + if((!(this.left && this.left.isEnabled) && amount > 0) || (!(this.right && this.right.isEnabled) && amount < 0)) { return; } @@ -4388,17 +4432,17 @@ ionic.controllers.NavController = ionic.controllers.ViewController.inherit({ this._rightShowing = false; // Push the z-index of the right menu down - this.right.pushDown(); + this.right && this.right.pushDown(); // Bring the z-index of the left menu up - this.left.bringUp(); + this.left && this.left.bringUp(); } else { this._rightShowing = true; this._leftShowing = false; // Bring the z-index of the right menu up - this.right.bringUp(); + this.right && this.right.bringUp(); // Push the z-index of the left menu down - this.left.pushDown(); + this.left && this.left.pushDown(); } }, diff --git a/js/controllers/sideMenuController.js b/js/controllers/sideMenuController.js index e196d946ac..dd1aff5e15 100644 --- a/js/controllers/sideMenuController.js +++ b/js/controllers/sideMenuController.js @@ -25,7 +25,7 @@ self._handleDrag(e); }; - this.content.endDrag = function(e) { + this.content.onEndDrag =function(e) { self._endDrag(e); }; } @@ -115,12 +115,12 @@ */ openPercentage: function(percentage) { var p = percentage / 100; - var maxLeft = this.left.width; - var maxRight = this.right.width; - if(percentage >= 0) { - this.openAmount(maxLeft * p); - } else { - this.openAmount(maxRight * p); + + if(this.left && percentage >= 0) { + this.openAmount(this.left.width * p); + } else if(this.right && percentage < 0) { + var maxRight = this.right.width; + this.openAmount(this.right.width * p); } }, @@ -130,11 +130,11 @@ * negative value for right menu (only one menu will be visible at a time). */ openAmount: function(amount) { - var maxLeft = this.left.width; - var maxRight = this.right.width; + var maxLeft = this.left && this.left.width || 0; + var maxRight = this.right && this.right.width || 0; // Check if we can move to that side, depending if the left/right panel is enabled - if((!this.left.isEnabled && amount > 0) || (!this.right.isEnabled && amount < 0)) { + if((!(this.left && this.left.isEnabled) && amount > 0) || (!(this.right && this.right.isEnabled) && amount < 0)) { return; } @@ -149,17 +149,17 @@ this._rightShowing = false; // Push the z-index of the right menu down - this.right.pushDown(); + this.right && this.right.pushDown(); // Bring the z-index of the left menu up - this.left.bringUp(); + this.left && this.left.bringUp(); } else { this._rightShowing = true; this._leftShowing = false; // Bring the z-index of the right menu up - this.right.bringUp(); + this.right && this.right.bringUp(); // Push the z-index of the left menu down - this.left.pushDown(); + this.left && this.left.pushDown(); } }, diff --git a/js/ext/angular/src/directive/ionicSideMenu.js b/js/ext/angular/src/directive/ionicSideMenu.js index d596846bcf..07841e7062 100644 --- a/js/ext/angular/src/directive/ionicSideMenu.js +++ b/js/ext/angular/src/directive/ionicSideMenu.js @@ -20,27 +20,18 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) angular.extend(this, ionic.controllers.SideMenuController.prototype); ionic.controllers.SideMenuController.call(this, { + // Our quick implementation of the left side menu left: { width: 270, - pushDown: function() { - $scope.leftZIndex = -1; - }, - bringUp: function() { - $scope.leftZIndex = 0; - } }, + + // Our quick implementation of the right side menu right: { width: 270, - pushDown: function() { - $scope.rightZIndex = -1; - }, - bringUp: function() { - $scope.rightZIndex = 0; - } } }); - $scope.contentTranslateX = 0; + $scope.sideMenuContentTranslateX = 0; $scope.sideMenuCtrl = this; }) @@ -72,28 +63,32 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) defaultPrevented = e.defaultPrevented; }); - Gesture.on('drag', function(e) { + var dragFn = function(e) { if(defaultPrevented) { return; } sideMenuCtrl._handleDrag(e); - }, $element[0]); + }; - Gesture.on('release', function(e) { + Gesture.on('drag', dragFn, $element[0]); + + var dragReleaseFn = function(e) { if(!defaultPrevented) { sideMenuCtrl._endDrag(e); } defaultPrevented = false; - }, $element[0]); + }; + + Gesture.on('release', dragReleaseFn, $element[0]); sideMenuCtrl.setContent({ onDrag: function(e) {}, endDrag: function(e) {}, getTranslateX: function() { - return $scope.contentTranslateX || 0; + return $scope.sideMenuContentTranslateX || 0; }, setTranslateX: function(amount) { - $scope.contentTranslateX = amount; + $scope.sideMenuContentTranslateX = amount; $element[0].style.webkitTransform = 'translate3d(' + amount + 'px, 0, 0)'; }, enableAnimation: function() { @@ -107,6 +102,12 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) $element[0].classList.remove('menu-animated'); } }); + + // Cleanup + $scope.$on('$destroy', function() { + Gesture.off('drag', dragFn); + Gesture.off('release', dragReleaseFn); + }); }; } }; @@ -125,10 +126,23 @@ angular.module('ionic.ui.sideMenu', ['ionic.service.gesture']) return function($scope, $element, $attr, sideMenuCtrl) { $scope.side = $attr.side; + if($scope.side == 'left') { sideMenuCtrl.left.isEnabled = true; + sideMenuCtrl.left.pushDown = function() { + $element[0].style.zIndex = -1; + }; + sideMenuCtrl.left.bringUp = function() { + $element[0].style.zIndex = 0; + }; } else if($scope.side == 'right') { sideMenuCtrl.right.isEnabled = true; + sideMenuCtrl.right.pushDown = function() { + $element[0].style.zIndex = -1; + }; + sideMenuCtrl.right.bringUp = function() { + $element[0].style.zIndex = 0; + }; } $element.append(transclude($scope)); diff --git a/js/ext/angular/test/sideMenu.html b/js/ext/angular/test/sideMenu.html index a4bb9ae2fe..a5cb930f14 100644 --- a/js/ext/angular/test/sideMenu.html +++ b/js/ext/angular/test/sideMenu.html @@ -16,24 +16,37 @@