Guest User

Untitled

a guest
Jul 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. var Tooltip = Class.create();
  2. Tooltip.prototype = {
  3. initialize: function(element, tool_tip) {
  4. var options = Object.extend({
  5. default_css: false,
  6. margin: "0px",
  7. padding: "5px",
  8. backgroundColor: "#d6d6fc",
  9. delta_x: 5,
  10. delta_y: 5,
  11. zindex: 1000
  12. }, arguments[1] || {});
  13.  
  14. this.element = $(element);
  15. this.tool_tip = $(tool_tip);
  16.  
  17. this.options = options;
  18.  
  19. // hide the tool-tip by default
  20. this.tool_tip.hide();
  21.  
  22. this.eventMouseOver = this.showTooltip.bindAsEventListener(this);
  23. this.eventMouseOut = this.hideTooltip.bindAsEventListener(this);
  24.  
  25. this.registerEvents();
  26. },
  27.  
  28. destroy: function() {
  29. Event.stopObserving(this.element, "mouseover", this.eventMouseOver);
  30. Event.stopObserving(this.element, "mouseout", this.eventMouseOut);
  31. },
  32.  
  33. registerEvents: function() {
  34. Event.observe(this.element, "mouseover", this.eventMouseOver);
  35. Event.observe(this.element, "mouseout", this.eventMouseOut);
  36. },
  37.  
  38. showTooltip: function(event){
  39. Event.stop(event);
  40. // get Mouse position
  41. var mouse_x = Event.pointerX(event);
  42. var mouse_y = Event.pointerY(event);
  43.  
  44.  
  45. // decide if wee need to switch sides for the tooltip
  46. var dimensions = Element.getDimensions( this.tool_tip );
  47. var element_width = dimensions.width;
  48. var element_height = dimensions.height;
  49.  
  50. if ( (element_width + mouse_x) >= ( this.getWindowWidth() - this.options.delta_x) ){ // too big for X
  51. mouse_x = mouse_x - element_width;
  52. // apply delta to make sure that the mouse is not on the tool-tip
  53. mouse_x = mouse_x - this.options.delta_x;
  54. } else {
  55. mouse_x = mouse_x + this.options.delta_x;
  56. }
  57.  
  58. if ( (element_height + mouse_y) >= ( this.getWindowHeight() - this.options.delta_y) ){ // too big for Y
  59. mouse_y = mouse_y - element_height;
  60. // apply delta to make sure that the mouse is not on the tool-tip
  61. mouse_y = mouse_y - this.options.delta_y;
  62. } else {
  63. mouse_y = mouse_y + this.options.delta_y;
  64. }
  65.  
  66. // now set the right styles
  67. this.setStyles(mouse_x, mouse_y);
  68.  
  69.  
  70. // finally show the Tooltip
  71. //new Effect.Appear(this.tool_tip);
  72. new Element.show(this.tool_tip);
  73.  
  74. },
  75.  
  76. setStyles: function(x, y){
  77. // set the right styles to position the tool tip
  78. Element.setStyle(this.tool_tip, { position:'absolute',
  79. top:y + "px",
  80. left:x + "px",
  81. zindex:this.options.zindex
  82. });
  83.  
  84. // apply default theme if wanted
  85. if (this.options.default_css){
  86. Element.setStyle(this.tool_tip, { margin:this.options.margin,
  87. padding:this.options.padding,
  88. backgroundColor:this.options.backgroundColor,
  89. zindex:this.options.zindex
  90. });
  91. }
  92. },
  93.  
  94. hideTooltip: function(event){
  95. //new Effect.Fade(this.tool_tip);
  96. new Element.hide(this.tool_tip);
  97. },
  98.  
  99. getWindowHeight: function(){
  100. var innerHeight;
  101. if (navigator.appVersion.indexOf('MSIE')>0) {
  102. innerHeight = document.body.clientHeight;
  103. } else {
  104. innerHeight = window.innerHeight;
  105. }
  106. return innerHeight;
  107. },
  108.  
  109. getWindowWidth: function(){
  110. var innerWidth;
  111. if (navigator.appVersion.indexOf('MSIE')>0) {
  112. innerWidth = document.body.clientWidth;
  113. } else {
  114. innerWidth = window.innerWidth;
  115. }
  116. return innerWidth;
  117. }
  118.  
  119. }
Add Comment
Please, Sign In to add comment