tooltip.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("./lib/oop");
  33. var dom = require("./lib/dom");
  34. /**
  35. * @class Tooltip
  36. **/
  37. /**
  38. * @param {Element} parentNode
  39. *
  40. * @constructor
  41. **/
  42. function Tooltip (parentNode) {
  43. this.isOpen = false;
  44. this.$element = null;
  45. this.$parentNode = parentNode;
  46. }
  47. (function() {
  48. this.$init = function() {
  49. this.$element = dom.createElement("div");
  50. this.$element.className = "ace_tooltip";
  51. this.$element.style.display = "none";
  52. this.$parentNode.appendChild(this.$element);
  53. return this.$element;
  54. };
  55. /**
  56. * @returns {Element}
  57. **/
  58. this.getElement = function() {
  59. return this.$element || this.$init();
  60. };
  61. /**
  62. * @param {String} text
  63. **/
  64. this.setText = function(text) {
  65. dom.setInnerText(this.getElement(), text);
  66. };
  67. /**
  68. * @param {String} html
  69. **/
  70. this.setHtml = function(html) {
  71. this.getElement().innerHTML = html;
  72. };
  73. /**
  74. * @param {Number} x
  75. * @param {Number} y
  76. **/
  77. this.setPosition = function(x, y) {
  78. this.getElement().style.left = x + "px";
  79. this.getElement().style.top = y + "px";
  80. };
  81. /**
  82. * @param {String} className
  83. **/
  84. this.setClassName = function(className) {
  85. dom.addCssClass(this.getElement(), className);
  86. };
  87. /**
  88. * @param {String} text
  89. * @param {Number} x
  90. * @param {Number} y
  91. **/
  92. this.show = function(text, x, y) {
  93. if (text != null)
  94. this.setText(text);
  95. if (x != null && y != null)
  96. this.setPosition(x, y);
  97. if (!this.isOpen) {
  98. this.getElement().style.display = "block";
  99. this.isOpen = true;
  100. }
  101. };
  102. this.hide = function() {
  103. if (this.isOpen) {
  104. this.getElement().style.display = "none";
  105. this.isOpen = false;
  106. }
  107. };
  108. /**
  109. * @returns {Number}
  110. **/
  111. this.getHeight = function() {
  112. return this.getElement().offsetHeight;
  113. };
  114. /**
  115. * @returns {Number}
  116. **/
  117. this.getWidth = function() {
  118. return this.getElement().offsetWidth;
  119. };
  120. }).call(Tooltip.prototype);
  121. exports.Tooltip = Tooltip;
  122. });