widgetdiv.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2013 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview A div that floats on top of Blockly. This singleton contains
  22. * temporary HTML UI widgets that the user is currently interacting with.
  23. * E.g. text input areas, colour pickers, context menus.
  24. * @author fraser@google.com (Neil Fraser)
  25. */
  26. 'use strict';
  27. goog.provide('Blockly.WidgetDiv');
  28. goog.require('Blockly.Css');
  29. goog.require('goog.dom');
  30. goog.require('goog.style');
  31. /**
  32. * The HTML container. Set once by Blockly.WidgetDiv.createDom.
  33. * @type {Element}
  34. */
  35. Blockly.WidgetDiv.DIV = null;
  36. /**
  37. * The object currently using this container.
  38. * @type {Object}
  39. * @private
  40. */
  41. Blockly.WidgetDiv.owner_ = null;
  42. /**
  43. * Optional cleanup function set by whichever object uses the widget.
  44. * @type {Function}
  45. * @private
  46. */
  47. Blockly.WidgetDiv.dispose_ = null;
  48. /**
  49. * Create the widget div and inject it onto the page.
  50. */
  51. Blockly.WidgetDiv.createDom = function() {
  52. if (Blockly.WidgetDiv.DIV) {
  53. return; // Already created.
  54. }
  55. // Create an HTML container for popup overlays (e.g. editor widgets).
  56. Blockly.WidgetDiv.DIV = goog.dom.createDom('div', 'blocklyWidgetDiv');
  57. document.body.appendChild(Blockly.WidgetDiv.DIV);
  58. };
  59. /**
  60. * Initialize and display the widget div. Close the old one if needed.
  61. * @param {!Object} newOwner The object that will be using this container.
  62. * @param {boolean} rtl Right-to-left (true) or left-to-right (false).
  63. * @param {Function} dispose Optional cleanup function to be run when the widget
  64. * is closed.
  65. */
  66. Blockly.WidgetDiv.show = function(newOwner, rtl, dispose) {
  67. Blockly.WidgetDiv.hide();
  68. Blockly.WidgetDiv.owner_ = newOwner;
  69. Blockly.WidgetDiv.dispose_ = dispose;
  70. // Temporarily move the widget to the top of the screen so that it does not
  71. // cause a scrollbar jump in Firefox when displayed.
  72. var xy = goog.style.getViewportPageOffset(document);
  73. Blockly.WidgetDiv.DIV.style.top = xy.y + 'px';
  74. Blockly.WidgetDiv.DIV.style.direction = rtl ? 'rtl' : 'ltr';
  75. Blockly.WidgetDiv.DIV.style.display = 'block';
  76. };
  77. /**
  78. * Destroy the widget and hide the div.
  79. */
  80. Blockly.WidgetDiv.hide = function() {
  81. if (Blockly.WidgetDiv.owner_) {
  82. Blockly.WidgetDiv.owner_ = null;
  83. Blockly.WidgetDiv.DIV.style.display = 'none';
  84. Blockly.WidgetDiv.DIV.style.left = '';
  85. Blockly.WidgetDiv.DIV.style.top = '';
  86. Blockly.WidgetDiv.DIV.style.height = '';
  87. Blockly.WidgetDiv.dispose_ && Blockly.WidgetDiv.dispose_();
  88. Blockly.WidgetDiv.dispose_ = null;
  89. goog.dom.removeChildren(Blockly.WidgetDiv.DIV);
  90. }
  91. };
  92. /**
  93. * Is the container visible?
  94. * @return {boolean} True if visible.
  95. */
  96. Blockly.WidgetDiv.isVisible = function() {
  97. return !!Blockly.WidgetDiv.owner_;
  98. };
  99. /**
  100. * Destroy the widget and hide the div if it is being used by the specified
  101. * object.
  102. * @param {!Object} oldOwner The object that was using this container.
  103. */
  104. Blockly.WidgetDiv.hideIfOwner = function(oldOwner) {
  105. if (Blockly.WidgetDiv.owner_ == oldOwner) {
  106. Blockly.WidgetDiv.hide();
  107. }
  108. };
  109. /**
  110. * Position the widget at a given location. Prevent the widget from going
  111. * offscreen top or left (right in RTL).
  112. * @param {number} anchorX Horizontal location (window coorditates, not body).
  113. * @param {number} anchorY Vertical location (window coorditates, not body).
  114. * @param {!goog.math.Size} windowSize Height/width of window.
  115. * @param {!goog.math.Coordinate} scrollOffset X/y of window scrollbars.
  116. * @param {boolean} rtl True if RTL, false if LTR.
  117. */
  118. Blockly.WidgetDiv.position = function(anchorX, anchorY, windowSize,
  119. scrollOffset, rtl) {
  120. // Don't let the widget go above the top edge of the window.
  121. if (anchorY < scrollOffset.y) {
  122. anchorY = scrollOffset.y;
  123. }
  124. if (rtl) {
  125. // Don't let the widget go right of the right edge of the window.
  126. if (anchorX > windowSize.width + scrollOffset.x) {
  127. anchorX = windowSize.width + scrollOffset.x;
  128. }
  129. } else {
  130. // Don't let the widget go left of the left edge of the window.
  131. if (anchorX < scrollOffset.x) {
  132. anchorX = scrollOffset.x;
  133. }
  134. }
  135. Blockly.WidgetDiv.DIV.style.left = anchorX + 'px';
  136. Blockly.WidgetDiv.DIV.style.top = anchorY + 'px';
  137. Blockly.WidgetDiv.DIV.style.height =
  138. (windowSize.height - anchorY + scrollOffset.y) + 'px';
  139. };