From a33241e796502aa42696fe08d4fc1541f94da0b2 Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Fri, 13 Mar 2015 16:07:31 -0400 Subject: [PATCH] Refactor show/hide on toolbar buttons --- .../list-generator/list-actions.partial.html | 6 +- .../list-generator/list-generator.factory.js | 60 ++++++++++++------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/awx/ui/static/js/shared/list-generator/list-actions.partial.html b/awx/ui/static/js/shared/list-generator/list-actions.partial.html index 22982b8a60..498d1dd316 100644 --- a/awx/ui/static/js/shared/list-generator/list-actions.partial.html +++ b/awx/ui/static/js/shared/list-generator/list-actions.partial.html @@ -9,8 +9,10 @@ class="options.class" data-title="{{options.dataTitle}}" icon-size="{{options.iconSize}}" - ng-click="performAction(options.ngClick)" - ng-show="canShowAction(options)" + ng-click="$eval(options.ngClick)" + ng-hide="isHiddenByOptions(options) || + hiddenOnCurrentPage(options.basePaths) || + hiddenInCurrentMode(options.mode)" toolbar="true"> diff --git a/awx/ui/static/js/shared/list-generator/list-generator.factory.js b/awx/ui/static/js/shared/list-generator/list-generator.factory.js index 78856e4961..a57a2cfc09 100644 --- a/awx/ui/static/js/shared/list-generator/list-generator.factory.js +++ b/awx/ui/static/js/shared/list-generator/list-generator.factory.js @@ -71,34 +71,50 @@ export default ['$location', '$compile', '$rootScope', 'SearchWidget', 'Paginate this.scope.list = list; this.scope.mode = options.mode; - this.scope.performAction = function(action) { - this.scope.$eval(action); - }.bind(this); + this.scope.isHiddenByOptions = function(options) { + var hiddenByNgHide = false, shownByNgShow = false; - this.scope.shouldHideAction = function(options) { - return this.scope.$eval(options.ngHide); - }.bind(this); - this.scope.canShowAction = function(action) { - 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)) { + // If neither ngHide nor ngShow are specified we + // know it.s not hidden by options + // + if (!options.ngHide && !options.ngShow) { return false; - } else if (!scopeShow) { - return false; - } else { - return inActionMode && onScreenForAction; } - // return _.tap(scopeShow || , function(value) { - // console.log('canShow:', value, options.mode, action.mode, scopeShow); - // }); + // If ngHide option is passed && evals to true + // 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); + 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);