Skip to content

Commit

Permalink
Make link state (hidden, disabled, tooltip) dynamic via methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jul 2, 2016
1 parent ee29dc6 commit ea468f9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/plugins/console/public/src/directives/sense_navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require('ui/modules')
this.menu = new KbnTopNavController([
{
key: 'welcome',
hideButton: true,
hideButton: () => true,
template: `<sense-welcome></sense-welcome>`
},
{
Expand Down
35 changes: 30 additions & 5 deletions src/ui/public/kbn_top_nav/__tests__/kbn_top_nav_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@ describe('KbnTopNavController', function () {
});

describe('hideButton:', function () {
it('defaults to false', function () {
it('defaults to function that returns false', function () {
const controller = new KbnTopNavController([
{ key: 'foo' },
{ key: '1234' },
]);

expect(pluck(controller.opts, 'hideButton')).to.eql([
false,
false
]);
pluck(controller.opts, 'hideButton').forEach(f => {
expect(f()).to.be(false);
});
});

it('excludes opts from opts when true', function () {
Expand All @@ -86,6 +85,32 @@ describe('KbnTopNavController', function () {
});
});

describe('disableButton:', function () {
it('defaults to function that returns false', function () {
const controller = new KbnTopNavController([
{ key: 'foo' },
{ key: '1234' },
]);

pluck(controller.opts, 'disableButton').forEach(f => {
expect(f()).to.be(false);
});
});
});

describe('tooltip:', function () {
it('defaults to function that returns empty string', function () {
const controller = new KbnTopNavController([
{ key: 'foo' },
{ key: '1234' },
]);

pluck(controller.opts, 'tooltip').forEach(f => {
expect(f()).to.be('');
});
});
});

describe('run:', function () {
it('defaults to a function that toggles the key', function () {
const controller = new KbnTopNavController([
Expand Down
11 changes: 8 additions & 3 deletions src/ui/public/kbn_top_nav/kbn_top_nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
aria-label="{{::menuItem.description}}"
aria-haspopup="{{!menuItem.hasFunction}}"
aria-expanded="{{kbnTopNav.isCurrent(menuItem.key)}}"
ng-class="{active: kbnTopNav.isCurrent(menuItem.key)}"
ng-class="{active: kbnTopNav.isCurrent(menuItem.key), 'is-kbn-top-nav-button-disabled': menuItem.disableButton()}"
ng-click="menuItem.run(menuItem, kbnTopNav)"
ng-bind="menuItem.label">
ng-bind="menuItem.label"
tooltip="{{menuItem.tooltip()}}"
tooltip-placement="bottom"
tooltip-popup-delay="400"
tooltip-append-to-body="1"
>
</button>
</div>
<kbn-global-timepicker></kbn-global-timepicker>
Expand All @@ -18,4 +23,4 @@
<div class="config-close remove">
<i class="fa fa-chevron-circle-up" ng-click="kbnTopNav.close()"></i>
</div>
</div>
</div>
10 changes: 6 additions & 4 deletions src/ui/public/kbn_top_nav/kbn_top_nav_controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, capitalize, isArray } from 'lodash';
import { defaults, capitalize, isArray, isFunction } from 'lodash';

import uiModules from 'ui/modules';
import filterTemplate from 'ui/chrome/config/filter.html';
Expand Down Expand Up @@ -29,7 +29,7 @@ export default function ($compile) {
const opt = this._applyOptDefault(rawOpt);
if (!opt.key) throw new TypeError('KbnTopNav: menu items must have a key');
this.opts.push(opt);
if (!opt.hideButton) this.menuItems.push(opt);
if (!opt.hideButton()) this.menuItems.push(opt);
if (opt.template) this.templates[opt.key] = opt.template;
});
}
Expand Down Expand Up @@ -57,8 +57,10 @@ export default function ($compile) {
label: capitalize(opt.key),
hasFunction: !!opt.run,
description: opt.run ? opt.key : `Toggle ${opt.key} view`,
hideButton: !!opt.hideButton,
run: (item) => this.toggle(item.key)
hideButton: isFunction(opt.hideButton) ? opt.hideButton : () => false,
disableButton: isFunction(opt.disableButton) ? opt.disableButton : () => false,
tooltip: isFunction(opt.tooltip) ? opt.tooltip : () => '',
run: (item) => !item.disableButton() && this.toggle(item.key)
});
}

Expand Down

0 comments on commit ea468f9

Please sign in to comment.