diff --git a/core/block_render_svg.js b/core/block_render_svg.js index b077d8197..7535db4a6 100644 --- a/core/block_render_svg.js +++ b/core/block_render_svg.js @@ -287,7 +287,7 @@ Blockly.BlockSvg.prototype.render = function(opt_bubble) { parentBlock.render(true); } else { // Top-most block. Fire an event to allow scrollbars to resize. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(this.workspace); } } Blockly.Field.stopCache(); diff --git a/core/block_svg.js b/core/block_svg.js index 8e1a954e0..d67e1acc1 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -257,7 +257,7 @@ Blockly.BlockSvg.terminateDrag_ = function() { Blockly.Events.setGroup(false); }, Blockly.BUMP_DELAY); // Fire an event to allow scrollbars to resize. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(this.workspace); } } Blockly.dragMode_ = Blockly.DRAG_NONE; @@ -597,7 +597,7 @@ Blockly.BlockSvg.prototype.onMouseUp_ = function(e) { // Dropping a block on the trash can will usually cause the workspace to // resize to contain the newly positioned block. Force a second resize // now that the block has been deleted. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(this.workspace); } if (Blockly.highlightedConnection_) { Blockly.highlightedConnection_.unhighlight(); diff --git a/core/blockly.js b/core/blockly.js index aee1a20ca..17305ec83 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -132,12 +132,37 @@ Blockly.svgSize = function(svg) { height: svg.cachedHeight_}; }; +/** + * Schedule a call to the resize handler. Groups of simultaneous events (e.g. + * a tree of blocks being deleted) are merged into one call. + * @param {Blockly.WorkspaceSvg} workspace Any workspace in the SVG. + */ +Blockly.asyncSvgResize = function(workspace) { + if (Blockly.svgResizePending_) { + return; + } + if (!workspace) { + workspace = Blockly.getMainWorkspace(); + } + Blockly.svgResizePending_ = true; + setTimeout(function() {Blockly.svgResize(workspace);}, 0); +}; + +/** + * Flag indicating a resize event is scheduled. + * Used to fire only one resize after multiple changes. + * @type {boolean} + * @private + */ +Blockly.svgResizePending_ = false; + /** * Size the SVG image to completely fill its container. * Record the height/width of the SVG image. * @param {!Blockly.WorkspaceSvg} workspace Any workspace in the SVG. */ Blockly.svgResize = function(workspace) { + Blockly.svgResizePending_ = false; var mainWorkspace = workspace; while (mainWorkspace.options.parentWorkspace) { mainWorkspace = mainWorkspace.options.parentWorkspace; diff --git a/core/flyout.js b/core/flyout.js index 1f05597f4..d943c91f0 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -433,7 +433,7 @@ Blockly.Flyout.prototype.show = function(xmlList) { this.filterForCapacity_(); // Fire a resize event to update the flyout's scrollbar. - Blockly.fireUiEventNow(window, 'resize'); + Blockly.svgResize(this.workspace_); this.reflowWrapper_ = this.reflow.bind(this); this.workspace_.addChangeListener(this.reflowWrapper_); }; @@ -578,7 +578,7 @@ Blockly.Flyout.prototype.reflow = function() { // Record the width for .getMetrics_ and .position. this.width_ = flyoutWidth; // Fire a resize event to update the flyout's scrollbar. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(this.workspace_); } }; diff --git a/core/inject.js b/core/inject.js index a6abdb385..98935a5c9 100644 --- a/core/inject.js +++ b/core/inject.js @@ -263,7 +263,10 @@ Blockly.init_ = function(mainWorkspace) { }); Blockly.bindEvent_(window, 'resize', null, - function() {Blockly.svgResize(mainWorkspace);}); + function() { + Blockly.hideChaff(true); + Blockly.asyncSvgResize(mainWorkspace); + }); Blockly.inject.bindDocumentEvents_(); @@ -317,7 +320,7 @@ Blockly.inject.bindDocumentEvents_ = function() { // Some iPad versions don't fire resize after portrait to landscape change. if (goog.userAgent.IPAD) { Blockly.bindEvent_(window, 'orientationchange', document, function() { - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(); }); } } diff --git a/core/toolbox.js b/core/toolbox.js index 8435ec5cf..2d765da6e 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -267,7 +267,7 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) { } // Fire a resize event since the toolbox may have changed width and height. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(this.workspace_); }; /** @@ -442,7 +442,7 @@ Blockly.Toolbox.TreeNode = function(toolbox, html, opt_config, opt_domHelper) { goog.ui.tree.TreeNode.call(this, html, opt_config, opt_domHelper); if (toolbox) { var resize = function() { - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(toolbox.workspace_); }; // Fire a resize event since the toolbox may have changed width. goog.events.listen(toolbox.tree_, diff --git a/core/utils.js b/core/utils.js index 02d07df4f..aecc851b5 100644 --- a/core/utils.js +++ b/core/utils.js @@ -163,63 +163,6 @@ Blockly.unbindEvent_ = function(bindData) { return func; }; -/** - * Fire a synthetic event synchronously. - * @param {!EventTarget} node The event's target node. - * @param {string} eventName Name of event (e.g. 'click'). - */ -Blockly.fireUiEventNow = function(node, eventName) { - // Remove the event from the anti-duplicate database. - var list = Blockly.fireUiEvent.DB_[eventName]; - if (list) { - var i = list.indexOf(node); - if (i != -1) { - list.splice(i, 1); - } - } - // Create a UI event in a browser-compatible way. - if (typeof UIEvent == 'function') { - // W3 - var evt = new UIEvent(eventName, {}); - } else { - // MSIE - var evt = document.createEvent('UIEvent'); - evt.initUIEvent(eventName, false, false, window, 0); - } - node.dispatchEvent(evt); -}; - -/** - * Fire a synthetic event asynchronously. Groups of simultaneous events (e.g. - * a tree of blocks being deleted) are merged into one event. - * @param {!EventTarget} node The event's target node. - * @param {string} eventName Name of event (e.g. 'click'). - */ -Blockly.fireUiEvent = function(node, eventName) { - var list = Blockly.fireUiEvent.DB_[eventName]; - if (list) { - if (list.indexOf(node) != -1) { - // This event is already scheduled to fire. - return; - } - list.push(node); - } else { - Blockly.fireUiEvent.DB_[eventName] = [node]; - } - var fire = function() { - Blockly.fireUiEventNow(node, eventName); - }; - setTimeout(fire, 0); -}; - -/** - * Database of upcoming firing event types. - * Used to fire only one event after multiple changes. - * @type {!Object} - * @private - */ -Blockly.fireUiEvent.DB_ = {}; - /** * Don't do anything for this event, just halt propagation. * @param {!Event} e An event. diff --git a/core/workspace_svg.js b/core/workspace_svg.js index b44df8d4a..af0580b05 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -708,7 +708,7 @@ Blockly.WorkspaceSvg.prototype.cleanUp_ = function() { } Blockly.Events.setGroup(false); // Fire an event to allow scrollbars to resize. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(this); }; /** diff --git a/core/xml.js b/core/xml.js index e6e3a1c5a..3e9e3cdc2 100644 --- a/core/xml.js +++ b/core/xml.js @@ -349,7 +349,7 @@ Blockly.Xml.domToBlock = function(xmlBlock, workspace) { }, 1); topBlock.updateDisabled(); // Fire an event to allow scrollbars to resize. - Blockly.fireUiEvent(window, 'resize'); + Blockly.asyncSvgResize(workspace); } Blockly.Events.enable(); if (Blockly.Events.isEnabled()) {