Tooltip.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. L.DrawTooltip = L.Class.extend({
  2. initialize: function (map) {
  3. this._map = map;
  4. this._popupPane = map._panes.popupPane;
  5. this._container = L.DomUtil.create('div', 'leaflet-draw-tooltip', this._popupPane);
  6. this._singleLineLabel = false;
  7. },
  8. dispose: function () {
  9. this._popupPane.removeChild(this._container);
  10. this._container = null;
  11. },
  12. updateContent: function (labelText) {
  13. labelText.subtext = labelText.subtext || '';
  14. // update the vertical position (only if changed)
  15. if (labelText.subtext.length === 0 && !this._singleLineLabel) {
  16. L.DomUtil.addClass(this._container, 'leaflet-draw-tooltip-single');
  17. this._singleLineLabel = true;
  18. }
  19. else if (labelText.subtext.length > 0 && this._singleLineLabel) {
  20. L.DomUtil.removeClass(this._container, 'leaflet-draw-tooltip-single');
  21. this._singleLineLabel = false;
  22. }
  23. this._container.innerHTML =
  24. (labelText.subtext.length > 0 ? '<span class="leaflet-draw-tooltip-subtext">' + labelText.subtext + '</span>' + '<br />' : '') +
  25. '<span>' + labelText.text + '</span>';
  26. return this;
  27. },
  28. updatePosition: function (latlng) {
  29. var pos = this._map.latLngToLayerPoint(latlng);
  30. L.DomUtil.setPosition(this._container, pos);
  31. return this;
  32. },
  33. showAsError: function () {
  34. L.DomUtil.addClass(this._container, 'leaflet-error-draw-tooltip');
  35. return this;
  36. },
  37. removeError: function () {
  38. L.DomUtil.removeClass(this._container, 'leaflet-error-draw-tooltip');
  39. return this;
  40. }
  41. });