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.dom.TagName');
  31. goog.require('goog.style');
  32. /**
  33. * The HTML container. Set once by Blockly.WidgetDiv.createDom.
  34. * @type {Element}
  35. */
  36. Blockly.WidgetDiv.DIV = null;
  37. /**
  38. * The object currently using this container.
  39. * @type {Object}
  40. * @private
  41. */
  42. Blockly.WidgetDiv.owner_ = null;
  43. /**
  44. * Optional cleanup function set by whichever object uses the widget.
  45. * @type {Function}
  46. * @private
  47. */
  48. Blockly.WidgetDiv.dispose_ = null;
  49. /**
  50. * Create the widget div and inject it onto the page.
  51. */
  52. Blockly.WidgetDiv.createDom = function() {
  53. if (Blockly.WidgetDiv.DIV) {
  54. return; // Already created.
  55. }
  56. // Create an HTML container for popup overlays (e.g. editor widgets).
  57. Blockly.WidgetDiv.DIV =
  58. goog.dom.createDom(goog.dom.TagName.DIV, 'blocklyWidgetDiv');
  59. document.body.appendChild(Blockly.WidgetDiv.DIV);
  60. };
  61. /**
  62. * Initialize and display the widget div. Close the old one if needed.
  63. * @param {!Object} newOwner The object that will be using this container.
  64. * @param {boolean} rtl Right-to-left (true) or left-to-right (false).
  65. * @param {Function} dispose Optional cleanup function to be run when the widget
  66. * is closed.
  67. */
  68. Blockly.WidgetDiv.show = function(newOwner, rtl, dispose) {
  69. Blockly.WidgetDiv.hide();
  70. Blockly.WidgetDiv.owner_ = newOwner;
  71. Blockly.WidgetDiv.dispose_ = dispose;
  72. // Temporarily move the widget to the top of the screen so that it does not
  73. // cause a scrollbar jump in Firefox when displayed.
  74. var xy = goog.style.getViewportPageOffset(document);
  75. Blockly.WidgetDiv.DIV.style.top = xy.y + 'px';
  76. Blockly.WidgetDiv.DIV.style.direction = rtl ? 'rtl' : 'ltr';
  77. Blockly.WidgetDiv.DIV.style.display = 'block';
  78. };
  79. /**
  80. * Destroy the widget and hide the div.
  81. */
  82. Blockly.WidgetDiv.hide = function() {
  83. if (Blockly.WidgetDiv.owner_) {
  84. Blockly.WidgetDiv.owner_ = null;
  85. Blockly.WidgetDiv.DIV.style.display = 'none';
  86. Blockly.WidgetDiv.DIV.style.left = '';
  87. Blockly.WidgetDiv.DIV.style.top = '';
  88. Blockly.WidgetDiv.dispose_ && Blockly.WidgetDiv.dispose_();
  89. Blockly.WidgetDiv.dispose_ = null;
  90. goog.dom.removeChildren(Blockly.WidgetDiv.DIV);
  91. }
  92. };
  93. /**
  94. * Is the container visible?
  95. * @return {boolean} True if visible.
  96. */
  97. Blockly.WidgetDiv.isVisible = function() {
  98. return !!Blockly.WidgetDiv.owner_;
  99. };
  100. /**
  101. * Destroy the widget and hide the div if it is being used by the specified
  102. * object.
  103. * @param {!Object} oldOwner The object that was using this container.
  104. */
  105. Blockly.WidgetDiv.hideIfOwner = function(oldOwner) {
  106. if (Blockly.WidgetDiv.owner_ == oldOwner) {
  107. Blockly.WidgetDiv.hide();
  108. }
  109. };
  110. /**
  111. * Position the widget at a given location. Prevent the widget from going
  112. * offscreen top or left (right in RTL).
  113. * @param {number} anchorX Horizontal location (window coorditates, not body).
  114. * @param {number} anchorY Vertical location (window coorditates, not body).
  115. * @param {!goog.math.Size} windowSize Height/width of window.
  116. * @param {!goog.math.Coordinate} scrollOffset X/y of window scrollbars.
  117. * @param {boolean} rtl True if RTL, false if LTR.
  118. */
  119. Blockly.WidgetDiv.position = function(anchorX, anchorY, windowSize,
  120. scrollOffset, rtl) {
  121. // Don't let the widget go above the top edge of the window.
  122. if (anchorY < scrollOffset.y) {
  123. anchorY = scrollOffset.y;
  124. }
  125. if (rtl) {
  126. // Don't let the widget go right of the right edge of the window.
  127. if (anchorX > windowSize.width + scrollOffset.x) {
  128. anchorX = windowSize.width + scrollOffset.x;
  129. }
  130. } else {
  131. // Don't let the widget go left of the left edge of the window.
  132. if (anchorX < scrollOffset.x) {
  133. anchorX = scrollOffset.x;
  134. }
  135. }
  136. Blockly.WidgetDiv.DIV.style.left = anchorX + 'px';
  137. Blockly.WidgetDiv.DIV.style.top = anchorY + 'px';
  138. Blockly.WidgetDiv.DIV.style.height = windowSize.height + 'px';
  139. };