Tooltip.js (Leaflet-1.8.0) | : | Tooltip.js (Leaflet-1.9.0) | ||
---|---|---|---|---|
import {DivOverlay} from './DivOverlay'; | import {DivOverlay} from './DivOverlay'; | |||
import {toPoint} from '../geometry/Point'; | import {toPoint} from '../geometry/Point'; | |||
import {Map} from '../map/Map'; | import {Map} from '../map/Map'; | |||
import {Layer} from './Layer'; | import {Layer} from './Layer'; | |||
import * as DomUtil from '../dom/DomUtil'; | import * as DomUtil from '../dom/DomUtil'; | |||
import * as DomEvent from '../dom/DomEvent'; | ||||
import * as Util from '../core/Util'; | ||||
/* | /* | |||
* @class Tooltip | * @class Tooltip | |||
* @inherits DivOverlay | * @inherits DivOverlay | |||
* @aka L.Tooltip | * @aka L.Tooltip | |||
* Used to display small texts on top of map layers. | * Used to display small texts on top of map layers. | |||
* | * | |||
* @example | * @example | |||
* If you want to just bind a tooltip to marker: | ||||
* | * | |||
* ```js | * ```js | |||
* marker.bindTooltip("my tooltip text").openTooltip(); | * marker.bindTooltip("my tooltip text").openTooltip(); | |||
* ``` | * ``` | |||
* Path overlays like polylines also have a `bindTooltip` method. | ||||
* | ||||
* A tooltip can be also standalone: | ||||
* | ||||
* ```js | ||||
* var tooltip = L.tooltip() | ||||
* .setLatLng(latlng) | ||||
* .setContent('Hello world!<br />This is a nice tooltip.') | ||||
* .addTo(map); | ||||
* ``` | ||||
* or | ||||
* ```js | ||||
* var tooltip = L.tooltip(latlng, {content: 'Hello world!<br />This is a nice t | ||||
ooltip.'}) | ||||
* .addTo(map); | ||||
* ``` | ||||
* | ||||
* | ||||
* Note about tooltip offset. Leaflet takes two options in consideration | * Note about tooltip offset. Leaflet takes two options in consideration | |||
* for computing tooltip offsetting: | * for computing tooltip offsetting: | |||
* - the `offset` Tooltip option: it defaults to [0, 0], and it's specific to on e tooltip. | * - the `offset` Tooltip option: it defaults to [0, 0], and it's specific to on e tooltip. | |||
* Add a positive x offset to move the tooltip to the right, and a positive y offset to | * Add a positive x offset to move the tooltip to the right, and a positive y offset to | |||
* move it to the bottom. Negatives will move to the left and top. | * move it to the bottom. Negatives will move to the left and top. | |||
* - the `tooltipAnchor` Icon option: this will only be considered for Marker. Y ou | * - the `tooltipAnchor` Icon option: this will only be considered for Marker. Y ou | |||
* should adapt this value if you use a custom icon. | * should adapt this value if you use a custom icon. | |||
*/ | */ | |||
// @namespace Tooltip | // @namespace Tooltip | |||
skipping to change at line 117 | skipping to change at line 137 | |||
} | } | |||
return events; | return events; | |||
}, | }, | |||
_initLayout: function () { | _initLayout: function () { | |||
var prefix = 'leaflet-tooltip', | var prefix = 'leaflet-tooltip', | |||
className = prefix + ' ' + (this.options.className || '') + ' leaflet-zoom-' + (this._zoomAnimated ? 'animated' : 'hide'); | className = prefix + ' ' + (this.options.className || '') + ' leaflet-zoom-' + (this._zoomAnimated ? 'animated' : 'hide'); | |||
this._contentNode = this._container = DomUtil.create('div', class Name); | this._contentNode = this._container = DomUtil.create('div', class Name); | |||
this._container.setAttribute('role', 'tooltip'); | ||||
this._container.setAttribute('id', 'leaflet-tooltip-' + Util.stam | ||||
p(this)); | ||||
}, | }, | |||
_updateLayout: function () {}, | _updateLayout: function () {}, | |||
_adjustPan: function () {}, | _adjustPan: function () {}, | |||
_setPosition: function (pos) { | _setPosition: function (pos) { | |||
var subX, subY, | var subX, subY, | |||
map = this._map, | map = this._map, | |||
container = this._container, | container = this._container, | |||
skipping to change at line 197 | skipping to change at line 220 | |||
_getAnchor: function () { | _getAnchor: function () { | |||
// Where should we anchor the tooltip on the source layer? | // Where should we anchor the tooltip on the source layer? | |||
return toPoint(this._source && this._source._getTooltipAnchor && !this.options.sticky ? this._source._getTooltipAnchor() : [0, 0]); | return toPoint(this._source && this._source._getTooltipAnchor && !this.options.sticky ? this._source._getTooltipAnchor() : [0, 0]); | |||
} | } | |||
}); | }); | |||
// @namespace Tooltip | // @namespace Tooltip | |||
// @factory L.tooltip(options?: Tooltip options, source?: Layer) | // @factory L.tooltip(options?: Tooltip options, source?: Layer) | |||
// Instantiates a Tooltip object given an optional `options` object that describ | // Instantiates a `Tooltip` object given an optional `options` object that descr | |||
es its appearance and location and an optional `source` object that is used to t | ibes its appearance and location and an optional `source` object that is used to | |||
ag the tooltip with a reference to the Layer to which it refers. | tag the tooltip with a reference to the Layer to which it refers. | |||
// @alternative | ||||
// @factory L.tooltip(latlng: LatLng, options?: Tooltip options) | ||||
// Instantiates a `Tooltip` object given `latlng` where the tooltip will open an | ||||
d an optional `options` object that describes its appearance and location. | ||||
export var tooltip = function (options, source) { | export var tooltip = function (options, source) { | |||
return new Tooltip(options, source); | return new Tooltip(options, source); | |||
}; | }; | |||
// @namespace Map | // @namespace Map | |||
// @section Methods for Layers and Controls | // @section Methods for Layers and Controls | |||
Map.include({ | Map.include({ | |||
// @method openTooltip(tooltip: Tooltip): this | // @method openTooltip(tooltip: Tooltip): this | |||
// Opens the specified tooltip. | // Opens the specified tooltip. | |||
skipping to change at line 285 | skipping to change at line 311 | |||
if (!remove && this._tooltipHandlersAdded) { return; } | if (!remove && this._tooltipHandlersAdded) { return; } | |||
var onOff = remove ? 'off' : 'on', | var onOff = remove ? 'off' : 'on', | |||
events = { | events = { | |||
remove: this.closeTooltip, | remove: this.closeTooltip, | |||
move: this._moveTooltip | move: this._moveTooltip | |||
}; | }; | |||
if (!this._tooltip.options.permanent) { | if (!this._tooltip.options.permanent) { | |||
events.mouseover = this._openTooltip; | events.mouseover = this._openTooltip; | |||
events.mouseout = this.closeTooltip; | events.mouseout = this.closeTooltip; | |||
events.click = this._openTooltip; | events.click = this._openTooltip; | |||
if (this._map) { | ||||
this._addFocusListeners(); | ||||
} else { | ||||
events.add = this._addFocusListeners; | ||||
} | ||||
} else { | } else { | |||
events.add = this._openTooltip; | events.add = this._openTooltip; | |||
} | } | |||
if (this._tooltip.options.sticky) { | if (this._tooltip.options.sticky) { | |||
events.mousemove = this._moveTooltip; | events.mousemove = this._moveTooltip; | |||
} | } | |||
this[onOff](events); | this[onOff](events); | |||
this._tooltipHandlersAdded = !remove; | this._tooltipHandlersAdded = !remove; | |||
}, | }, | |||
// @method openTooltip(latlng?: LatLng): this | // @method openTooltip(latlng?: LatLng): this | |||
// Opens the bound tooltip at the specified `latlng` or at the default to oltip anchor if no `latlng` is passed. | // Opens the bound tooltip at the specified `latlng` or at the default to oltip anchor if no `latlng` is passed. | |||
openTooltip: function (latlng) { | openTooltip: function (latlng) { | |||
if (this._tooltip && this._tooltip._prepareOpen(latlng)) { | if (this._tooltip && this._tooltip._prepareOpen(latlng)) { | |||
// open the tooltip on the map | // open the tooltip on the map | |||
this._tooltip.openOn(this._map); | this._tooltip.openOn(this._map); | |||
if (this.getElement) { | ||||
this._setAriaDescribedByOnLayer(this); | ||||
} else if (this.eachLayer) { | ||||
this.eachLayer(this._setAriaDescribedByOnLayer, t | ||||
his); | ||||
} | ||||
} | } | |||
return this; | return this; | |||
}, | }, | |||
// @method closeTooltip(): this | // @method closeTooltip(): this | |||
// Closes the tooltip bound to this layer if it is open. | // Closes the tooltip bound to this layer if it is open. | |||
closeTooltip: function () { | closeTooltip: function () { | |||
if (this._tooltip) { | if (this._tooltip) { | |||
return this._tooltip.close(); | return this._tooltip.close(); | |||
} | } | |||
skipping to change at line 343 | skipping to change at line 380 | |||
} | } | |||
return this; | return this; | |||
}, | }, | |||
// @method getTooltip(): Tooltip | // @method getTooltip(): Tooltip | |||
// Returns the tooltip bound to this layer. | // Returns the tooltip bound to this layer. | |||
getTooltip: function () { | getTooltip: function () { | |||
return this._tooltip; | return this._tooltip; | |||
}, | }, | |||
_addFocusListeners: function () { | ||||
if (this.getElement) { | ||||
this._addFocusListenersOnLayer(this); | ||||
} else if (this.eachLayer) { | ||||
this.eachLayer(this._addFocusListenersOnLayer, this); | ||||
} | ||||
}, | ||||
_addFocusListenersOnLayer: function (layer) { | ||||
DomEvent.on(layer.getElement(), 'focus', function () { | ||||
this._tooltip._source = layer; | ||||
this.openTooltip(); | ||||
}, this); | ||||
DomEvent.on(layer.getElement(), 'blur', this.closeTooltip, this); | ||||
}, | ||||
_setAriaDescribedByOnLayer: function (layer) { | ||||
layer.getElement().setAttribute('aria-describedby', this._tooltip | ||||
._container.id); | ||||
}, | ||||
_openTooltip: function (e) { | _openTooltip: function (e) { | |||
if (!this._tooltip || !this._map || (this._map.dragging && this._ map.dragging.moving())) { | if (!this._tooltip || !this._map || (this._map.dragging && this._ map.dragging.moving())) { | |||
return; | return; | |||
} | } | |||
this._tooltip._source = e.layer || e.target; | this._tooltip._source = e.layer || e.target; | |||
this.openTooltip(this._tooltip.options.sticky ? e.latlng : undefi ned); | this.openTooltip(this._tooltip.options.sticky ? e.latlng : undefi ned); | |||
}, | }, | |||
_moveTooltip: function (e) { | _moveTooltip: function (e) { | |||
End of changes. 8 change blocks. | ||||
3 lines changed or deleted | 65 lines changed or added |