Refactor show/hide on toolbar buttons

This commit is contained in:
Joe Fiorini
2015-03-13 16:07:31 -04:00
parent 9aba3bfa6a
commit a33241e796
2 changed files with 42 additions and 24 deletions

View File

@@ -9,8 +9,10 @@
class="options.class" class="options.class"
data-title="{{options.dataTitle}}" data-title="{{options.dataTitle}}"
icon-size="{{options.iconSize}}" icon-size="{{options.iconSize}}"
ng-click="performAction(options.ngClick)" ng-click="$eval(options.ngClick)"
ng-show="canShowAction(options)" ng-hide="isHiddenByOptions(options) ||
hiddenOnCurrentPage(options.basePaths) ||
hiddenInCurrentMode(options.mode)"
toolbar="true"> toolbar="true">
</button> </button>
</span> </span>

View File

@@ -71,34 +71,50 @@ export default ['$location', '$compile', '$rootScope', 'SearchWidget', 'Paginate
this.scope.list = list; this.scope.list = list;
this.scope.mode = options.mode; this.scope.mode = options.mode;
this.scope.performAction = function(action) { this.scope.isHiddenByOptions = function(options) {
this.scope.$eval(action); var hiddenByNgHide = false, shownByNgShow = false;
}.bind(this);
this.scope.shouldHideAction = function(options) { // If neither ngHide nor ngShow are specified we
return this.scope.$eval(options.ngHide); // know it.s not hidden by options
}.bind(this); //
this.scope.canShowAction = function(action) { if (!options.ngHide && !options.ngShow) {
var base = $location.path().replace(/^\//, '').split('/')[0];
var inActionMode = options.mode === action.mode || action.mode === 'all';
var onScreenForAction =
(!options.basePaths) ||
(options.basePaths.indexOf(base) > -1);
var scopeShow = action.ngShow ? this.scope.$eval(action.ngShow) : true;
if (this.scope.shouldHideAction(action)) {
return false; return false;
} else if (!scopeShow) {
return false;
} else {
return inActionMode && onScreenForAction;
} }
// return _.tap(scopeShow || , function(value) { // If ngHide option is passed && evals to true
// console.log('canShow:', value, options.mode, action.mode, scopeShow); // it's hidden
// }); if (options.ngHide && this.scope.$eval(options.ngHide)) {
return true;
}
// If ngShow option is passed && evals to false
// it's hidden
if (options.ngShow && !this.scope.$eval(options.ngShow)) {
return true;
}
// Otherwise, it's not hidden
return false;
}.bind(this); }.bind(this);
this.scope.hiddenOnCurrentPage = function(actionPages) {
var base = $location.path().replace(/^\//, '').split('/')[0];
if (!actionPages) {
return false;
} else if (actionPages.indexOf(base) > -1) {
return false;
} else {
return true;
}
};
this.scope.hiddenInCurrentMode = function(actionMode) {
if (options.mode === actionMode || actionMode === 'all') {
return false;
}
};
$compile(element)(this.scope); $compile(element)(this.scope);