Tooltip.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. L.Draw = L.Draw || {};
  2. /**
  3. * @class L.Draw.Tooltip
  4. * @aka Tooltip
  5. *
  6. * The tooltip class — it is used to display the tooltip while drawing
  7. * This will be depreciated
  8. *
  9. * @example
  10. *
  11. * ```js
  12. * var tooltip = L.Draw.Tooltip();
  13. * ```
  14. *
  15. */
  16. L.Draw.Tooltip = L.Class.extend({
  17. // @section Methods for modifying draw state
  18. // @method initialize(map): void
  19. // Tooltip constructor
  20. initialize: function (map) {
  21. this._map = map;
  22. this._popupPane = map._panes.popupPane;
  23. this._container = map.options.drawControlTooltips ?
  24. L.DomUtil.create('div', 'leaflet-draw-tooltip', this._popupPane) : null;
  25. this._singleLineLabel = false;
  26. this._map.on('mouseout', this._onMouseOut, this);
  27. },
  28. // @method dispose(): void
  29. // Remove Tooltip DOM and unbind events
  30. dispose: function () {
  31. this._map.off('mouseout', this._onMouseOut, this);
  32. if (this._container) {
  33. this._popupPane.removeChild(this._container);
  34. this._container = null;
  35. }
  36. },
  37. // @method updateContent(labelText): this
  38. // Changes the tooltip text to string in function call
  39. updateContent: function (labelText) {
  40. if (!this._container) {
  41. return this;
  42. }
  43. labelText.subtext = labelText.subtext || '';
  44. // update the vertical position (only if changed)
  45. if (labelText.subtext.length === 0 && !this._singleLineLabel) {
  46. L.DomUtil.addClass(this._container, 'leaflet-draw-tooltip-single');
  47. this._singleLineLabel = true;
  48. }
  49. else if (labelText.subtext.length > 0 && this._singleLineLabel) {
  50. L.DomUtil.removeClass(this._container, 'leaflet-draw-tooltip-single');
  51. this._singleLineLabel = false;
  52. }
  53. this._container.innerHTML =
  54. (labelText.subtext.length > 0 ?
  55. '<span class="leaflet-draw-tooltip-subtext">' + labelText.subtext + '</span>' + '<br />' : '') +
  56. '<span>' + labelText.text + '</span>';
  57. return this;
  58. },
  59. // @method updatePosition(latlng): this
  60. // Changes the location of the tooltip
  61. updatePosition: function (latlng) {
  62. var pos = this._map.latLngToLayerPoint(latlng),
  63. tooltipContainer = this._container;
  64. if (this._container) {
  65. tooltipContainer.style.visibility = 'inherit';
  66. L.DomUtil.setPosition(tooltipContainer, pos);
  67. }
  68. return this;
  69. },
  70. // @method showAsError(): this
  71. // Applies error class to tooltip
  72. showAsError: function () {
  73. if (this._container) {
  74. L.DomUtil.addClass(this._container, 'leaflet-error-draw-tooltip');
  75. }
  76. return this;
  77. },
  78. // @method removeError(): this
  79. // Removes the error class from the tooltip
  80. removeError: function () {
  81. if (this._container) {
  82. L.DomUtil.removeClass(this._container, 'leaflet-error-draw-tooltip');
  83. }
  84. return this;
  85. },
  86. _onMouseOut: function () {
  87. if (this._container) {
  88. this._container.style.visibility = 'hidden';
  89. }
  90. }
  91. });