Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Last active October 20, 2016 11:10
Show Gist options
  • Select an option

  • Save gabrielmontagne/19424db553d258411c64d5e3d85c741c to your computer and use it in GitHub Desktop.

Select an option

Save gabrielmontagne/19424db553d258411c64d5e3d85c741c to your computer and use it in GitHub Desktop.
Zambezi Grid -- cell popover
license: mit

Custom popovers

You can create and populate your custom popovers using the popover component. This component can be run using selection.each which makes it also usable as an event handler on selection events.

The popover dispatches two events, open and close. Your handlers will be called with the datum as its argument; the this keyword will point to the popover content DOM element.

You can, therefore, use the open event to setup the popover contents and you can use the close event to cleanup before the popover is destroyed.

The popover is automatically closed when the user clicks outside of it or when the user scrolls the containing grid. But you can also close it programmatically by dispatching a popover-close event on the popover DOM element.

<!doctype html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/[email protected]">
<style>
html, body { height: 100%; }
.fancy-popover {
background: powderblue;
color: crimson;
width: 300px;
height: 200px;
padding: 30px;
font-weight: bold;
}
.is-right .fancy-popover {
margin-left: 5px;
border-left: 4px dashed crimson;
}
.is-left .fancy-popover {
margin-right: 5px;
border-right: 4px dashed crimson;
}
.is-above .fancy-popover {
margin-bottom: 5px;
border-bottom: 4px dashed crimson;
}
.is-below .fancy-popover {
margin-top: 5px;
border-top: 4px dashed crimson;
}
.zambezi-grid-cell .formatted-text.manual-popover-button {
width: 100%;
top: 0;
outline: powderblue;
}
</style>
</head>
<div class="grid-target"></div>
<script src="https://npmcdn.com/[email protected]"></script>
<script src="https://npmcdn.com/[email protected]/faker.js"></script>
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script>
const rows = _.range(1, 50).map(faker.Helpers.createCard)
, popover = gridComponents.createPopover()
.on('open', onPopoverOpen)
.on('close', onPopoverClose)
, table = grid.createGrid()
.columns(
[
{ key: 'name', width: 200 }
, { key: 'username' }
, {
sortable: false
, width: 140
, key: 'username'
, template: '<span><button class="manual-popover-button formatted-text"></button></span>'
, components: [ configureButton, grid.updateTextIfChanged ]
, label: 'Button'
}
, { key: 'name', width: 200 }
, { key: 'email' }
, { key: 'phone' }
]
)
, target = d3.select('.grid-target')
.style('height', '300px')
.datum(rows)
.call(table)
function configureButton() {
d3.select(this)
.select('.manual-popover-button')
.on('click', popover)
}
function onPopoverOpen(d) {
const target = d3.select(this)
.classed('fancy-popover', true)
, p = target.append('p')
.text(`I am ${d.row.name}`)
, x = target.append('i')
.text('×')
.on('click', onClose)
.style('cursor', 'pointer')
}
function onClose() {
d3.select(this).dispatch('popover-close', { bubbles: true })
}
function onPopoverClose(d) {
console.log('popover closed', this, d3.select(this).html())
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment