Skip to content

Commit

Permalink
#107 contextmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihkel Oviir committed Jun 1, 2020
1 parent 217637d commit 7007ada
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
33 changes: 17 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@mapbox/polyline": "^1.1.1",
"@turf/line-slice-along": "^5.1.5",
"bootstrap": "^4.5.0",
"bootstrap.native": "github:thednp/bootstrap.native#master",
"bootstrap.native": "^3.0.1",
"file-saver": "^2.0.2",
"idb-keyval": "^3.2.0",
"jquery": "^3.5.1",
Expand Down
2 changes: 1 addition & 1 deletion src/geop/Geop.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Geop extends Component {
// components
this.components = {
map: new MapEngine({ target }),
contextmenu: new ContextMenu({ target }),
contextmenu: new ContextMenu(), // no target
header: new Header({ target }),
statusbar: new StatusBar({ target }),
toolbar: new ToolBar({ target }),
Expand Down
58 changes: 30 additions & 28 deletions src/geop/components/contextmenu/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { getState, setState } from 'Utilities/store'
import { closestFeatureTo } from 'Components/map/MapEngine'
import Overlay from 'ol/Overlay'
import Point from 'ol/geom/Point'
import Popper from 'popper.js'
import Popover from 'bootstrap.native/src/components/popover-native'
import './ContextMenu.styl'

Expand All @@ -26,7 +25,8 @@ class ContextMenu extends Component {
items: items,
timeout: null,
disableClick: false,
popover: null
popover: null,
popContent: null
}
const map = getState('map')
if (map) {
Expand All @@ -45,9 +45,7 @@ class ContextMenu extends Component {
if (e.originalEvent.ctrlKey || this.state.disableClick) {
return
}
if (this.popover) {
this.popover.dispose()
}
this.close()
})
this.$.on('contextmenu', map.getViewport(), e => {
e.preventDefault()
Expand All @@ -57,32 +55,38 @@ class ContextMenu extends Component {
if (hit && hit[1].getGeometry() instanceof Point) {
coords = hit[1].getGeometry().getCoordinates()
}
this.open(coords, this.getContent(coords, hit))
this.state.popContent = this.getContent(coords, hit)
this.open(coords, this.state.popContent)
this.state.disableClick = true
this.state.timeout = setTimeout(() => { this.state.disableClick = false }, 1000)
})
}

open (coord, popContent) {
// FIXME
Popper.Defaults.modifiers.preventOverflow.enabled = false
Popper.Defaults.modifiers.hide.enabled = false
if (this.popover) {
this.popover.dispose()
}
this.state.overlay.setPosition(coord)
this.popover = new Popover(this.el, popContent.definition)
// when popover's content is shown
this.$.on('shown.bs.popover', this.el, e => {
popContent.onShow(e.target.Popover.element.childNodes[0])
if (this.state.popContent) {
console.log(e.target.Popover)
this.state.popContent.onShow(e.target.Popover.element)
}
})
// when popover's content is hidden
this.$.on('hidden.bs.popover', this.el, e => {
popContent.onHide()
Popper.Defaults.modifiers.preventOverflow.enabled = true
Popper.Defaults.modifiers.hide.enabled = true
if (this.state.popContent) {
this.state.popContent.onHide()
}
})
this.popover.show()
}

open (coord, popContent) {
this.close()
this.state.overlay.setPosition(coord)
this.state.popover = new Popover(this.el, popContent.definition).show()
}

close () {
if (this.state.popover) {
this.state.popover.hide()
this.state.popover.dispose()
this.state.popover = null
}
}

getContent (coord, feature) {
Expand All @@ -94,8 +98,8 @@ class ContextMenu extends Component {
definition: {
container: this.el,
placement: 'right',
animation: false,
html: true,
animation: 'none',
trigger: 'click',
content: content.join(''),
offset: (this.state.items.length - 1) * 20 + 'px, 0',
template: `
Expand All @@ -114,8 +118,7 @@ class ContextMenu extends Component {
e.preventDefault()
item.onClick(e, coord, feature)
if (item.closeOnClick) {
// FIXME
this.el.popover('dispose')
this.close()
}
})
}
Expand All @@ -125,8 +128,7 @@ class ContextMenu extends Component {
e.stopPropagation()
item.onBtnClick(e, coord, feature)
if (item.closeOnClick) {
// FIXME
this.el.popover('dispose')
this.close()
}
})
}
Expand Down

0 comments on commit 7007ada

Please sign in to comment.