refactor(backButton): separate show/hide logic

This commit is contained in:
Adam Bradley
2014-11-26 12:46:14 -06:00
parent 17d0c5b852
commit 05fb7a09ea
7 changed files with 151 additions and 31 deletions

View File

@@ -23,6 +23,8 @@ function($scope, $element, $attrs, $q, $ionicConfig, $ionicHistory) {
var titleCss = '';
var isBackEnabled = false;
var isBackShown = true;
var isNavBackShown = true;
var isBackElementShown = false;
var titleTextWidth = 0;
@@ -41,30 +43,45 @@ function($scope, $element, $attrs, $q, $ionicConfig, $ionicHistory) {
};
self.enableBack = function(shouldEnable) {
self.enableBack = function(shouldEnable, disableReset) {
// whether or not the back button show be visible, according
// to the navigation and history
if (arguments.length && shouldEnable !== isBackEnabled) {
var backBtnEle = getEle(BACK_BUTTON);
backBtnEle && backBtnEle.classList[ shouldEnable ? 'remove' : 'add' ]('back-disabled');
if (arguments.length) {
isBackEnabled = shouldEnable;
if (!disableReset) self.updateBackButton();
}
return isBackEnabled;
};
self.showBack = function(shouldShow) {
self.showBack = function(shouldShow, disableReset) {
// different from enableBack() because this will always have the back
// visually hidden if false, even if the history says it should show
if (arguments.length && shouldShow !== isBackShown) {
var backBtnEle = getEle(BACK_BUTTON);
backBtnEle && backBtnEle.classList[ shouldShow ? 'remove' : 'add' ](HIDE);
if (arguments.length) {
isBackShown = shouldShow;
if (!disableReset) self.updateBackButton();
}
return isBackShown;
};
self.showNavBack = function(shouldShow) {
// different from showBack() because this is for the entire nav bar's
// setting for all of it's child headers. For internal use.
isNavBackShown = shouldShow;
self.updateBackButton();
};
self.updateBackButton = function() {
if ( (isBackShown && isNavBackShown && isBackEnabled) !== isBackElementShown) {
isBackElementShown = isBackShown && isNavBackShown && isBackEnabled;
var backBtnEle = getEle(BACK_BUTTON);
backBtnEle && backBtnEle.classList[ isBackElementShown ? 'remove' : 'add' ](HIDE);
}
};
self.titleTextWidth = function() {
if (!titleTextWidth) {
var bounds = ionic.DomUtil.getTextBounds(getEle(TITLE));
@@ -320,7 +337,7 @@ function($scope, $element, $attrs, $q, $ionicConfig, $ionicHistory) {
var eleCache = {};
function getEle(className) {
if (!eleCache[className]) {
if (!isDefined(eleCache[className])) {
eleCache[className] = $element[0].querySelector('.' + className);
}
return eleCache[className];