|
|
import {DivOverlay} from './DivOverlay'; |
|
|
import {toPoint} from '../geometry/Point'; |
|
|
import {Map} from '../map/Map'; |
|
|
import {Layer} from './Layer'; |
|
|
import * as DomUtil from '../dom/DomUtil'; |
|
|
import * as DomEvent from '../dom/DomEvent'; |
|
|
import * as Util from '../core/Util'; |
|
|
import {FeatureGroup} from './FeatureGroup'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var Tooltip = DivOverlay.extend({ |
|
|
|
|
|
|
|
|
|
|
|
options: { |
|
|
|
|
|
|
|
|
pane: 'tooltipPane', |
|
|
|
|
|
|
|
|
|
|
|
offset: [0, 0], |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
direction: 'auto', |
|
|
|
|
|
|
|
|
|
|
|
permanent: false, |
|
|
|
|
|
|
|
|
|
|
|
sticky: false, |
|
|
|
|
|
|
|
|
|
|
|
opacity: 0.9 |
|
|
}, |
|
|
|
|
|
onAdd: function (map) { |
|
|
DivOverlay.prototype.onAdd.call(this, map); |
|
|
this.setOpacity(this.options.opacity); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map.fire('tooltipopen', {tooltip: this}); |
|
|
|
|
|
if (this._source) { |
|
|
this.addEventParent(this._source); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._source.fire('tooltipopen', {tooltip: this}, true); |
|
|
} |
|
|
}, |
|
|
|
|
|
onRemove: function (map) { |
|
|
DivOverlay.prototype.onRemove.call(this, map); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
map.fire('tooltipclose', {tooltip: this}); |
|
|
|
|
|
if (this._source) { |
|
|
this.removeEventParent(this._source); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._source.fire('tooltipclose', {tooltip: this}, true); |
|
|
} |
|
|
}, |
|
|
|
|
|
getEvents: function () { |
|
|
var events = DivOverlay.prototype.getEvents.call(this); |
|
|
|
|
|
if (!this.options.permanent) { |
|
|
events.preclick = this.close; |
|
|
} |
|
|
|
|
|
return events; |
|
|
}, |
|
|
|
|
|
_initLayout: function () { |
|
|
var prefix = 'leaflet-tooltip', |
|
|
className = prefix + ' ' + (this.options.className || '') + ' leaflet-zoom-' + (this._zoomAnimated ? 'animated' : 'hide'); |
|
|
|
|
|
this._contentNode = this._container = DomUtil.create('div', className); |
|
|
|
|
|
this._container.setAttribute('role', 'tooltip'); |
|
|
this._container.setAttribute('id', 'leaflet-tooltip-' + Util.stamp(this)); |
|
|
}, |
|
|
|
|
|
_updateLayout: function () {}, |
|
|
|
|
|
_adjustPan: function () {}, |
|
|
|
|
|
_setPosition: function (pos) { |
|
|
var subX, subY, |
|
|
map = this._map, |
|
|
container = this._container, |
|
|
centerPoint = map.latLngToContainerPoint(map.getCenter()), |
|
|
tooltipPoint = map.layerPointToContainerPoint(pos), |
|
|
direction = this.options.direction, |
|
|
tooltipWidth = container.offsetWidth, |
|
|
tooltipHeight = container.offsetHeight, |
|
|
offset = toPoint(this.options.offset), |
|
|
anchor = this._getAnchor(); |
|
|
|
|
|
if (direction === 'top') { |
|
|
subX = tooltipWidth / 2; |
|
|
subY = tooltipHeight; |
|
|
} else if (direction === 'bottom') { |
|
|
subX = tooltipWidth / 2; |
|
|
subY = 0; |
|
|
} else if (direction === 'center') { |
|
|
subX = tooltipWidth / 2; |
|
|
subY = tooltipHeight / 2; |
|
|
} else if (direction === 'right') { |
|
|
subX = 0; |
|
|
subY = tooltipHeight / 2; |
|
|
} else if (direction === 'left') { |
|
|
subX = tooltipWidth; |
|
|
subY = tooltipHeight / 2; |
|
|
} else if (tooltipPoint.x < centerPoint.x) { |
|
|
direction = 'right'; |
|
|
subX = 0; |
|
|
subY = tooltipHeight / 2; |
|
|
} else { |
|
|
direction = 'left'; |
|
|
subX = tooltipWidth + (offset.x + anchor.x) * 2; |
|
|
subY = tooltipHeight / 2; |
|
|
} |
|
|
|
|
|
pos = pos.subtract(toPoint(subX, subY, true)).add(offset).add(anchor); |
|
|
|
|
|
DomUtil.removeClass(container, 'leaflet-tooltip-right'); |
|
|
DomUtil.removeClass(container, 'leaflet-tooltip-left'); |
|
|
DomUtil.removeClass(container, 'leaflet-tooltip-top'); |
|
|
DomUtil.removeClass(container, 'leaflet-tooltip-bottom'); |
|
|
DomUtil.addClass(container, 'leaflet-tooltip-' + direction); |
|
|
DomUtil.setPosition(container, pos); |
|
|
}, |
|
|
|
|
|
_updatePosition: function () { |
|
|
var pos = this._map.latLngToLayerPoint(this._latlng); |
|
|
this._setPosition(pos); |
|
|
}, |
|
|
|
|
|
setOpacity: function (opacity) { |
|
|
this.options.opacity = opacity; |
|
|
|
|
|
if (this._container) { |
|
|
DomUtil.setOpacity(this._container, opacity); |
|
|
} |
|
|
}, |
|
|
|
|
|
_animateZoom: function (e) { |
|
|
var pos = this._map._latLngToNewLayerPoint(this._latlng, e.zoom, e.center); |
|
|
this._setPosition(pos); |
|
|
}, |
|
|
|
|
|
_getAnchor: function () { |
|
|
|
|
|
return toPoint(this._source && this._source._getTooltipAnchor && !this.options.sticky ? this._source._getTooltipAnchor() : [0, 0]); |
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var tooltip = function (options, source) { |
|
|
return new Tooltip(options, source); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Map.include({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openTooltip: function (tooltip, latlng, options) { |
|
|
this._initOverlay(Tooltip, tooltip, latlng, options) |
|
|
.openOn(this); |
|
|
|
|
|
return this; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
closeTooltip: function (tooltip) { |
|
|
tooltip.close(); |
|
|
return this; |
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Layer.include({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bindTooltip: function (content, options) { |
|
|
|
|
|
if (this._tooltip && this.isTooltipOpen()) { |
|
|
this.unbindTooltip(); |
|
|
} |
|
|
|
|
|
this._tooltip = this._initOverlay(Tooltip, this._tooltip, content, options); |
|
|
this._initTooltipInteractions(); |
|
|
|
|
|
if (this._tooltip.options.permanent && this._map && this._map.hasLayer(this)) { |
|
|
this.openTooltip(); |
|
|
} |
|
|
|
|
|
return this; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
unbindTooltip: function () { |
|
|
if (this._tooltip) { |
|
|
this._initTooltipInteractions(true); |
|
|
this.closeTooltip(); |
|
|
this._tooltip = null; |
|
|
} |
|
|
return this; |
|
|
}, |
|
|
|
|
|
_initTooltipInteractions: function (remove) { |
|
|
if (!remove && this._tooltipHandlersAdded) { return; } |
|
|
var onOff = remove ? 'off' : 'on', |
|
|
events = { |
|
|
remove: this.closeTooltip, |
|
|
move: this._moveTooltip |
|
|
}; |
|
|
if (!this._tooltip.options.permanent) { |
|
|
events.mouseover = this._openTooltip; |
|
|
events.mouseout = this.closeTooltip; |
|
|
events.click = this._openTooltip; |
|
|
if (this._map) { |
|
|
this._addFocusListeners(); |
|
|
} else { |
|
|
events.add = this._addFocusListeners; |
|
|
} |
|
|
} else { |
|
|
events.add = this._openTooltip; |
|
|
} |
|
|
if (this._tooltip.options.sticky) { |
|
|
events.mousemove = this._moveTooltip; |
|
|
} |
|
|
this[onOff](events); |
|
|
this._tooltipHandlersAdded = !remove; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
openTooltip: function (latlng) { |
|
|
if (this._tooltip) { |
|
|
if (!(this instanceof FeatureGroup)) { |
|
|
this._tooltip._source = this; |
|
|
} |
|
|
if (this._tooltip._prepareOpen(latlng)) { |
|
|
|
|
|
this._tooltip.openOn(this._map); |
|
|
|
|
|
if (this.getElement) { |
|
|
this._setAriaDescribedByOnLayer(this); |
|
|
} else if (this.eachLayer) { |
|
|
this.eachLayer(this._setAriaDescribedByOnLayer, this); |
|
|
} |
|
|
} |
|
|
} |
|
|
return this; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
closeTooltip: function () { |
|
|
if (this._tooltip) { |
|
|
return this._tooltip.close(); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
toggleTooltip: function () { |
|
|
if (this._tooltip) { |
|
|
this._tooltip.toggle(this); |
|
|
} |
|
|
return this; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
isTooltipOpen: function () { |
|
|
return this._tooltip.isOpen(); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
setTooltipContent: function (content) { |
|
|
if (this._tooltip) { |
|
|
this._tooltip.setContent(content); |
|
|
} |
|
|
return this; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
getTooltip: function () { |
|
|
return this._tooltip; |
|
|
}, |
|
|
|
|
|
_addFocusListeners: function () { |
|
|
if (this.getElement) { |
|
|
this._addFocusListenersOnLayer(this); |
|
|
} else if (this.eachLayer) { |
|
|
this.eachLayer(this._addFocusListenersOnLayer, this); |
|
|
} |
|
|
}, |
|
|
|
|
|
_addFocusListenersOnLayer: function (layer) { |
|
|
var el = typeof layer.getElement === 'function' && layer.getElement(); |
|
|
if (el) { |
|
|
DomEvent.on(el, 'focus', function () { |
|
|
this._tooltip._source = layer; |
|
|
this.openTooltip(); |
|
|
}, this); |
|
|
DomEvent.on(el, 'blur', this.closeTooltip, this); |
|
|
} |
|
|
}, |
|
|
|
|
|
_setAriaDescribedByOnLayer: function (layer) { |
|
|
var el = typeof layer.getElement === 'function' && layer.getElement(); |
|
|
if (el) { |
|
|
el.setAttribute('aria-describedby', this._tooltip._container.id); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
_openTooltip: function (e) { |
|
|
if (!this._tooltip || !this._map) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
if (this._map.dragging && this._map.dragging.moving() && !this._openOnceFlag) { |
|
|
this._openOnceFlag = true; |
|
|
var that = this; |
|
|
this._map.once('moveend', function () { |
|
|
that._openOnceFlag = false; |
|
|
that._openTooltip(e); |
|
|
}); |
|
|
return; |
|
|
} |
|
|
|
|
|
this._tooltip._source = e.layer || e.target; |
|
|
|
|
|
this.openTooltip(this._tooltip.options.sticky ? e.latlng : undefined); |
|
|
}, |
|
|
|
|
|
_moveTooltip: function (e) { |
|
|
var latlng = e.latlng, containerPoint, layerPoint; |
|
|
if (this._tooltip.options.sticky && e.originalEvent) { |
|
|
containerPoint = this._map.mouseEventToContainerPoint(e.originalEvent); |
|
|
layerPoint = this._map.containerPointToLayerPoint(containerPoint); |
|
|
latlng = this._map.layerPointToLatLng(layerPoint); |
|
|
} |
|
|
this._tooltip.setLatLng(latlng); |
|
|
} |
|
|
}); |
|
|
|