Skip to content

Commit fa41a89

Browse files
committed
Create basic framework for animating fields on disposal
* Create basic framework for animating fields on disposal * disposeAnimation* instead of disposeAnimate* * Fix owner_, hideandClearDom logic in WidgetDiv.hide
1 parent 4e5717d commit fa41a89

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

core/widgetdiv.js

+72-10
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,36 @@ Blockly.WidgetDiv.owner_ = null;
4848

4949
/**
5050
* Optional cleanup function set by whichever object uses the widget.
51+
* This is called as soon as a dispose is desired. If the dispose should
52+
* be animated, the animation should start on the call of dispose_.
5153
* @type {Function}
5254
* @private
5355
*/
5456
Blockly.WidgetDiv.dispose_ = null;
5557

58+
/**
59+
* Optional function called at the end of a dispose animation.
60+
* Set by whichever object is using the widget.
61+
* @type {Function}
62+
* @private
63+
*/
64+
Blockly.WidgetDiv.disposeAnimationFinished_ = null;
65+
66+
/**
67+
* Timer ID for the dispose animation.
68+
* @type {number}
69+
* @private
70+
*/
71+
Blockly.WidgetDiv.disposeAnimationTimer_ = null;
72+
73+
/**
74+
* Length of time in seconds for the dispose animation.
75+
* @type {number}
76+
* @private
77+
*/
78+
Blockly.WidgetDiv.disposeAnimationTimerLength_ = 0;
79+
80+
5681
/**
5782
* Create the widget div and inject it onto the page.
5883
*/
@@ -69,13 +94,20 @@ Blockly.WidgetDiv.createDom = function() {
6994
* Initialize and display the widget div. Close the old one if needed.
7095
* @param {!Object} newOwner The object that will be using this container.
7196
* @param {boolean} rtl Right-to-left (true) or left-to-right (false).
72-
* @param {Function} dispose Optional cleanup function to be run when the widget
73-
* is closed.
97+
* @param {Function=} opt_dispose Optional cleanup function to be run when the widget
98+
* is closed. If the dispose is animated, this function must start the animation.
99+
* @param {Function=} opt_disposeAnimationFinished Optional cleanup function to be run
100+
* when the widget is done animating and must disappear.
101+
* @param {number=} opt_disposeAnimationTimerLength Length of animation time in seconds
102+
if a dispose animation is provided.
74103
*/
75-
Blockly.WidgetDiv.show = function(newOwner, rtl, dispose) {
104+
Blockly.WidgetDiv.show = function(newOwner, rtl, opt_dispose,
105+
opt_disposeAnimationFinished, opt_disposeAnimationTimerLength) {
76106
Blockly.WidgetDiv.hide();
77107
Blockly.WidgetDiv.owner_ = newOwner;
78-
Blockly.WidgetDiv.dispose_ = dispose;
108+
Blockly.WidgetDiv.dispose_ = opt_dispose;
109+
Blockly.WidgetDiv.disposeAnimationFinished_ = opt_disposeAnimationFinished;
110+
Blockly.WidgetDiv.disposeAnimationTimerLength_ = opt_disposeAnimationTimerLength;
79111
// Temporarily move the widget to the top of the screen so that it does not
80112
// cause a scrollbar jump in Firefox when displayed.
81113
var xy = goog.style.getViewportPageOffset(document);
@@ -89,19 +121,49 @@ Blockly.WidgetDiv.show = function(newOwner, rtl, dispose) {
89121
* Destroy the widget and hide the div.
90122
*/
91123
Blockly.WidgetDiv.hide = function() {
92-
if (Blockly.WidgetDiv.owner_) {
124+
if (Blockly.WidgetDiv.disposeAnimationTimer_) {
125+
// An animation timer is set already.
126+
// This happens when a previous widget was animating out,
127+
// but Blockly is hiding the widget to create a new one.
128+
// So, short-circuit the animation and clear the timer.
129+
window.clearTimeout(Blockly.WidgetDiv.disposeAnimationTimer_);
130+
Blockly.WidgetDiv.disposeAnimationFinished_ && Blockly.WidgetDiv.disposeAnimationFinished_();
131+
Blockly.WidgetDiv.disposeAnimationFinished_ = null;
132+
Blockly.WidgetDiv.disposeAnimationTimer_ = null;
93133
Blockly.WidgetDiv.owner_ = null;
94-
Blockly.WidgetDiv.DIV.style.display = 'none';
95-
Blockly.WidgetDiv.DIV.style.left = '';
96-
Blockly.WidgetDiv.DIV.style.top = '';
97-
Blockly.WidgetDiv.DIV.style.height = '';
134+
Blockly.WidgetDiv.hideAndClearDom_();
135+
} else if (Blockly.WidgetDiv.isVisible()) {
136+
// No animation timer set, but the widget is visible
137+
// Start animation out (or immediately hide)
98138
Blockly.WidgetDiv.dispose_ && Blockly.WidgetDiv.dispose_();
99139
Blockly.WidgetDiv.dispose_ = null;
100-
goog.dom.removeChildren(Blockly.WidgetDiv.DIV);
140+
// If we want to animate out, set the appropriate timer for final dispose.
141+
if (Blockly.WidgetDiv.disposeAnimationFinished_) {
142+
Blockly.WidgetDiv.disposeAnimationTimer_ = window.setTimeout(
143+
Blockly.WidgetDiv.hide, // Come back to hide and take the first branch.
144+
Blockly.WidgetDiv.disposeAnimationTimerLength_ * 1000
145+
);
146+
} else {
147+
// No timer provided - auto-hide the DOM now.
148+
Blockly.WidgetDiv.owner_ = null;
149+
Blockly.WidgetDiv.hideAndClearDom_();
150+
}
101151
Blockly.Events.setGroup(false);
102152
}
103153
};
104154

155+
/**
156+
* Hide all DOM for the WidgetDiv, and clear its children.
157+
* @private
158+
*/
159+
Blockly.WidgetDiv.hideAndClearDom_ = function() {
160+
Blockly.WidgetDiv.DIV.style.display = 'none';
161+
Blockly.WidgetDiv.DIV.style.left = '';
162+
Blockly.WidgetDiv.DIV.style.top = '';
163+
Blockly.WidgetDiv.DIV.style.height = '';
164+
goog.dom.removeChildren(Blockly.WidgetDiv.DIV);
165+
};
166+
105167
/**
106168
* Is the container visible?
107169
* @return {boolean} True if visible.

0 commit comments

Comments
 (0)