mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-07 15:07:13 +08:00
Comments for side menu controller
This commit is contained in:
@ -1,15 +1,21 @@
|
|||||||
|
|
||||||
(function(ionic) {
|
(function(ionic) {
|
||||||
|
/**
|
||||||
|
* The SideMenuController is a controller with a left and/or right menu that
|
||||||
|
* can be slid out and toggled. Seen on many an app.
|
||||||
|
*
|
||||||
|
* The right or left menu can be disabled or not used at all, if desired.
|
||||||
|
*/
|
||||||
ionic.controllers.SideMenuController = function(options) {
|
ionic.controllers.SideMenuController = function(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
self.left = options.left;
|
this.left = options.left;
|
||||||
self.right = options.right;
|
this.right = options.right;
|
||||||
self.content = options.content;
|
this.content = options.content;
|
||||||
|
|
||||||
self._rightShowing = false;
|
this._rightShowing = false;
|
||||||
self._leftShowing = false;
|
this._leftShowing = false;
|
||||||
|
this._isDragging = false;
|
||||||
|
|
||||||
this.content.onDrag = function(e) {
|
this.content.onDrag = function(e) {
|
||||||
self._handleDrag(e);
|
self._handleDrag(e);
|
||||||
@ -18,20 +24,21 @@
|
|||||||
this.content.endDrag = function(e) {
|
this.content.endDrag = function(e) {
|
||||||
self._endDrag(e);
|
self._endDrag(e);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
// Bind release and drag listeners
|
|
||||||
window.ion.onGesture('release', function(e) {
|
|
||||||
self._endDrag(e);
|
|
||||||
}, self.center);
|
|
||||||
|
|
||||||
window.ion.onGesture('drag', function(e) {
|
|
||||||
self._handleDrag(e);
|
|
||||||
}, self.center);
|
|
||||||
*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ionic.controllers.SideMenuController.prototype = {
|
ionic.controllers.SideMenuController.prototype = {
|
||||||
|
/**
|
||||||
|
* Set the content view controller if not passed in the constructor options.
|
||||||
|
*
|
||||||
|
* @param {object} content
|
||||||
|
*/
|
||||||
|
setContent: function(content) {
|
||||||
|
this.content = content;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle the left menu to open 100%
|
||||||
|
*/
|
||||||
toggleLeft: function() {
|
toggleLeft: function() {
|
||||||
var openAmount = this.getOpenAmount();
|
var openAmount = this.getOpenAmount();
|
||||||
if(openAmount > 0) {
|
if(openAmount > 0) {
|
||||||
@ -40,6 +47,10 @@
|
|||||||
this.openPercentage(100);
|
this.openPercentage(100);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle the right menu to open 100%
|
||||||
|
*/
|
||||||
toggleRight: function() {
|
toggleRight: function() {
|
||||||
var openAmount = this.getOpenAmount();
|
var openAmount = this.getOpenAmount();
|
||||||
if(openAmount < 0) {
|
if(openAmount < 0) {
|
||||||
@ -48,9 +59,20 @@
|
|||||||
this.openPercentage(-100);
|
this.openPercentage(-100);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {float} The amount the side menu is open, either positive or negative for left (positive), or right (negative)
|
||||||
|
*/
|
||||||
getOpenAmount: function() {
|
getOpenAmount: function() {
|
||||||
return this.content.getTranslateX() || 0;
|
return this.content.getTranslateX() || 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {float} The ratio of open amount over menu width. For example, a
|
||||||
|
* menu of width 100 open 50 pixels would be open 50% or a ratio of 0.5. Value is negative
|
||||||
|
* for right menu.
|
||||||
|
*/
|
||||||
getOpenRatio: function() {
|
getOpenRatio: function() {
|
||||||
var amount = this.getOpenAmount();
|
var amount = this.getOpenAmount();
|
||||||
if(amount >= 0) {
|
if(amount >= 0) {
|
||||||
@ -58,9 +80,20 @@
|
|||||||
}
|
}
|
||||||
return amount / this.right.width;
|
return amount / this.right.width;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {float} The percentage of open amount over menu width. For example, a
|
||||||
|
* menu of width 100 open 50 pixels would be open 50%. Value is negative
|
||||||
|
* for right menu.
|
||||||
|
*/
|
||||||
getOpenPercentage: function() {
|
getOpenPercentage: function() {
|
||||||
return this.getOpenRatio() * 100;
|
return this.getOpenRatio() * 100;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open the menu with a given percentage amount.
|
||||||
|
* @param {float} percentage The percentage (positive or negative for left/right) to open the menu.
|
||||||
|
*/
|
||||||
openPercentage: function(percentage) {
|
openPercentage: function(percentage) {
|
||||||
var p = percentage / 100;
|
var p = percentage / 100;
|
||||||
var maxLeft = this.left.width;
|
var maxLeft = this.left.width;
|
||||||
@ -71,6 +104,12 @@
|
|||||||
this.openAmount(maxRight * p);
|
this.openAmount(maxRight * p);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open the menu the given pixel amount.
|
||||||
|
* @param {float} amount the pixel amount to open the menu. Positive value for left menu,
|
||||||
|
* negative value for right menu (only one menu will be visible at a time).
|
||||||
|
*/
|
||||||
openAmount: function(amount) {
|
openAmount: function(amount) {
|
||||||
var maxLeft = this.left.width;
|
var maxLeft = this.left.width;
|
||||||
var maxRight = this.right.width;
|
var maxRight = this.right.width;
|
||||||
@ -104,6 +143,14 @@
|
|||||||
this.left.pushDown();
|
this.left.pushDown();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given an event object, find the final resting position of this side
|
||||||
|
* menu. For example, if the user "throws" the content to the right and
|
||||||
|
* releases the touch, the left menu should snap open (animated, of course).
|
||||||
|
*
|
||||||
|
* @param {Event} e the gesture event to use for snapping
|
||||||
|
*/
|
||||||
snapToRest: function(e) {
|
snapToRest: function(e) {
|
||||||
// We want to animate at the end of this
|
// We want to animate at the end of this
|
||||||
this.content.enableAnimation();
|
this.content.enableAnimation();
|
||||||
@ -160,9 +207,13 @@
|
|||||||
this.openPercentage(0);
|
this.openPercentage(0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// End a drag with the given event
|
||||||
_endDrag: function(e) {
|
_endDrag: function(e) {
|
||||||
this.snapToRest(e);
|
this.snapToRest(e);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Initialize a drag with the given event
|
||||||
_initDrag: function(e) {
|
_initDrag: function(e) {
|
||||||
this.content.disableAnimation();
|
this.content.disableAnimation();
|
||||||
this._isDragging = true;
|
this._isDragging = true;
|
||||||
@ -170,6 +221,8 @@
|
|||||||
this._offsetX = 0;
|
this._offsetX = 0;
|
||||||
this._lastX = 0;
|
this._lastX = 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Handle a drag event
|
||||||
_handleDrag: function(e) {
|
_handleDrag: function(e) {
|
||||||
if(!this._isDragging) {
|
if(!this._isDragging) {
|
||||||
this._initDrag(e);
|
this._initDrag(e);
|
||||||
@ -179,7 +232,7 @@
|
|||||||
|
|
||||||
this._offsetX = this.getOpenAmount();
|
this._offsetX = this.getOpenAmount();
|
||||||
}
|
}
|
||||||
//console.log('Dragging page', this._startX, this._lastX, this._offsetX, e);
|
|
||||||
var newX = this._offsetX + (this._lastX - this._startX);
|
var newX = this._offsetX + (this._lastX - this._startX);
|
||||||
|
|
||||||
this.openAmount(newX);
|
this.openAmount(newX);
|
||||||
|
|||||||
Reference in New Issue
Block a user