-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navbar directive #5940
Merged
Merged
Navbar directive #5940
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const ngMock = require('ngMock'); | ||
const sinon = require('sinon'); | ||
const expect = require('expect.js'); | ||
const angular = require('angular'); | ||
const _ = require('lodash'); | ||
|
||
require('ui/navbar'); | ||
const navbarExtensionsRegistry = require('ui/registry/navbar_extensions'); | ||
const Registry = require('ui/registry/_registry'); | ||
|
||
const defaultMarkup = ` | ||
<navbar name="testing"> | ||
<div class="button-group" role="toolbar"> | ||
<button> | ||
<i aria-hidden="true" class="fa fa-file-new-o"></i> | ||
</button> | ||
<button> | ||
<i aria-hidden="true" class="fa fa-save"></i> | ||
</button> | ||
<button> | ||
<i aria-hidden="true" class="fa fa-folder-open-o"></i> | ||
</button> | ||
</div> | ||
</navbar>`; | ||
|
||
|
||
describe('navbar directive', function () { | ||
let $rootScope; | ||
let $compile; | ||
let stubRegistry; | ||
|
||
beforeEach(function () { | ||
ngMock.module('kibana', function (PrivateProvider) { | ||
stubRegistry = new Registry({ | ||
index: ['name'], | ||
group: ['appName'], | ||
order: ['order'] | ||
}); | ||
|
||
PrivateProvider.swap(navbarExtensionsRegistry, stubRegistry); | ||
}); | ||
|
||
ngMock.module('kibana/navbar'); | ||
|
||
// Create the scope | ||
ngMock.inject(function ($injector) { | ||
$rootScope = $injector.get('$rootScope'); | ||
$compile = $injector.get('$compile'); | ||
}); | ||
}); | ||
|
||
function init(markup = defaultMarkup) { | ||
// Give us a scope | ||
const $el = angular.element(markup); | ||
$compile($el)($rootScope); | ||
$el.scope().$digest(); | ||
return $el; | ||
} | ||
|
||
describe('incorrect use', function () { | ||
|
||
it('should throw if missing a name property', function () { | ||
let markup = `<navbar><div class="button-group" role="toolbar"></div></navbar>`; | ||
expect(() => init(markup)).to.throwException(/requires a name attribute/); | ||
}); | ||
|
||
it('should throw if missing a button group', function () { | ||
let markup = `<navbar name="testing"></navbar>`; | ||
expect(() => init(markup)).to.throwException(/must have exactly 1 button group/); | ||
}); | ||
}); | ||
|
||
describe('injecting extensions', function () { | ||
function registerExtension(def = {}) { | ||
stubRegistry.register(function () { | ||
return _.defaults(def, { | ||
name: 'exampleButton', | ||
appName: 'testing', | ||
order: 0, | ||
template: ` | ||
<button class="test-button"> | ||
<i aria-hidden="true" class="fa fa-rocket"></i> | ||
</button>` | ||
}); | ||
}); | ||
} | ||
|
||
it('should use the default markup', function () { | ||
var $el = init(); | ||
expect($el.find('.button-group button').length).to.equal(3); | ||
}); | ||
|
||
it('should append to end then order == 0', function () { | ||
registerExtension({ order: 0 }); | ||
var $el = init(); | ||
|
||
expect($el.find('.button-group button').length).to.equal(4); | ||
expect($el.find('.button-group button').last().hasClass('test-button')).to.be.ok(); | ||
}); | ||
|
||
it('should append to end then order > 0', function () { | ||
registerExtension({ order: 1 }); | ||
var $el = init(); | ||
|
||
expect($el.find('.button-group button').length).to.equal(4); | ||
expect($el.find('.button-group button').last().hasClass('test-button')).to.be.ok(); | ||
}); | ||
|
||
it('should append to end then order < 0', function () { | ||
registerExtension({ order: -1 }); | ||
var $el = init(); | ||
|
||
expect($el.find('.button-group button').length).to.equal(4); | ||
expect($el.find('.button-group button').first().hasClass('test-button')).to.be.ok(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const _ = require('lodash'); | ||
const $ = require('jquery'); | ||
const navbar = require('ui/modules').get('kibana/navbar'); | ||
|
||
require('ui/render_directive'); | ||
|
||
navbar.directive('navbar', function (Private, $compile) { | ||
const navbarExtensions = Private(require('ui/registry/navbar_extensions')); | ||
const getExtensions = _.memoize(function (name) { | ||
if (!name) throw new Error('navbar directive requires a name attribute'); | ||
return _.sortBy(navbarExtensions.byAppName[name], 'order'); | ||
}); | ||
|
||
return { | ||
restrict: 'E', | ||
template: function ($el, $attrs) { | ||
const $buttonGroup = $el.children('.button-group'); | ||
if ($buttonGroup.length !== 1) throw new Error('navbar must have exactly 1 button group'); | ||
|
||
const extensions = getExtensions($attrs.name); | ||
const controls = $buttonGroup.children().detach().toArray().map(function (button) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would probably read cleaner if it was broken up a bit. Also, _spread 'em!_ const buttons = $buttonGroup.children().detach().toArray();
const controls = [
...buttons.map((button) => {
return {
order: 0,
$el: $(button)
};
}),
...extensions.map((extension, index) => {
return {
extension,
index,
order: extension.order
};
})
] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oooooh, I hadn't thought about spread that way before. That's hawt! |
||
return { | ||
order: 0, | ||
$el: $(button), | ||
}; | ||
}).concat(extensions.map(function (extension, i) { | ||
return { | ||
order: extension.order, | ||
index: i, | ||
extension: extension, | ||
}; | ||
})); | ||
|
||
_.sortBy(controls, 'order').forEach(function (control) { | ||
if (control.$el) { | ||
return $buttonGroup.append(control.$el); | ||
} | ||
|
||
const { extension, index } = control; | ||
const $ext = $(`<render-directive definition="navbar.extensions[${index}]"></render-directive>`); | ||
$ext.html(extension.template); | ||
$buttonGroup.append($ext); | ||
}); | ||
|
||
return $el.html(); | ||
}, | ||
controllerAs: 'navbar', | ||
controller: function ($attrs) { | ||
this.extensions = getExtensions($attrs.name); | ||
} | ||
}; | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably wouldn't hurt to mention some of the other invalid cases: