mirror of
https://github.com/google/blockly.git
synced 2026-06-16 16:15:14 +02:00
Dispose of toolbox when workspace is disposed.
This commit is contained in:
@@ -992,7 +992,7 @@ Blockly.WorkspaceSvg=function(a){Blockly.WorkspaceSvg.superClass_.constructor.ca
|
||||
Blockly.WorkspaceSvg.prototype.trashcan=null;Blockly.WorkspaceSvg.prototype.scrollbar=null;
|
||||
Blockly.WorkspaceSvg.prototype.createDom=function(a){this.svgGroup_=Blockly.createSvgElement("g",{},null);a&&(this.svgBackground_=Blockly.createSvgElement("rect",{height:"100%",width:"100%","class":a},this.svgGroup_),"blocklyMainBackground"==a&&(this.svgBackground_.style.fill="url(#"+this.options.gridPattern.id+")"));this.svgBlockCanvas_=Blockly.createSvgElement("g",{},this.svgGroup_);this.svgBubbleCanvas_=Blockly.createSvgElement("g",{},this.svgGroup_);this.options.hasTrashcan&&this.addTrashcan_();
|
||||
Blockly.bindEvent_(this.svgGroup_,"mousedown",this,this.onMouseDown_);this.options.hasCategories?this.toolbox_=new Blockly.Toolbox(this):this.options.languageTree&&this.addFlyout_();this.fireChangeEvent();return this.svgGroup_};
|
||||
Blockly.WorkspaceSvg.prototype.dispose=function(){this.rendered=!1;Blockly.WorkspaceSvg.superClass_.dispose.call(this);this.svgGroup_&&(goog.dom.removeNode(this.svgGroup_),this.svgGroup_=null);this.svgBubbleCanvas_=this.svgBlockCanvas_=null;this.flyout_&&(this.flyout_.dispose(),this.flyout_=null);this.trashcan&&(this.trashcan.dispose(),this.trashcan=null);this.options.parentWorkspace||goog.dom.removeNode(this.options.svg)};
|
||||
Blockly.WorkspaceSvg.prototype.dispose=function(){this.rendered=!1;Blockly.WorkspaceSvg.superClass_.dispose.call(this);this.svgGroup_&&(goog.dom.removeNode(this.svgGroup_),this.svgGroup_=null);this.svgBubbleCanvas_=this.svgBlockCanvas_=null;this.toolbox_&&(this.toolbox_.dispose(),this.toolbox_=null);this.flyout_&&(this.flyout_.dispose(),this.flyout_=null);this.trashcan&&(this.trashcan.dispose(),this.trashcan=null);this.options.parentWorkspace||goog.dom.removeNode(this.options.svg)};
|
||||
Blockly.WorkspaceSvg.prototype.addTrashcan_=function(){this.trashcan=new Blockly.Trashcan(this);var a=this.trashcan.createDom();this.svgGroup_.insertBefore(a,this.svgBlockCanvas_);this.trashcan.init()};Blockly.WorkspaceSvg.prototype.addFlyout_=function(){this.flyout_=new Blockly.Flyout({parentWorkspace:this,RTL:this.RTL});this.flyout_.autoClose=!1;var a=this.flyout_.createDom();this.svgGroup_.insertBefore(a,this.svgBlockCanvas_)};
|
||||
Blockly.WorkspaceSvg.prototype.resize=function(){this.toolbox_&&this.toolbox_.position();this.flyout_&&this.flyout_.position();this.trashcan&&this.trashcan.position();this.scrollbar&&this.scrollbar.resize()};Blockly.WorkspaceSvg.prototype.getCanvas=function(){return this.svgBlockCanvas_};Blockly.WorkspaceSvg.prototype.getBubbleCanvas=function(){return this.svgBubbleCanvas_};
|
||||
Blockly.WorkspaceSvg.prototype.translate=function(a,b){var c="translate("+a+","+b+")";this.svgBlockCanvas_.setAttribute("transform",c);this.svgBubbleCanvas_.setAttribute("transform",c)};Blockly.WorkspaceSvg.prototype.addTopBlock=function(a){Blockly.WorkspaceSvg.superClass_.addTopBlock.call(this,a);Blockly.Realtime.isEnabled()&&!this.options.parentWorkspace&&Blockly.Realtime.addTopBlock(a)};
|
||||
@@ -1281,7 +1281,7 @@ Blockly.Flyout.onMouseUpWrapper_=null);Blockly.Flyout.startDownEvent_=null;Block
|
||||
Blockly.Toolbox=function(a){this.workspace_=a};Blockly.Toolbox.prototype.width=0;Blockly.Toolbox.prototype.selectedOption_=null;Blockly.Toolbox.prototype.lastCategory_=null;Blockly.Toolbox.prototype.CONFIG_={indentWidth:19,cssRoot:"blocklyTreeRoot",cssHideRoot:"blocklyHidden",cssItem:"",cssTreeRow:"blocklyTreeRow",cssItemLabel:"blocklyTreeLabel",cssTreeIcon:"blocklyTreeIcon",cssExpandedFolderIcon:"blocklyTreeIconOpen",cssFileIcon:"blocklyTreeIconNone",cssSelectedRow:"blocklyTreeSelected"};
|
||||
Blockly.Toolbox.prototype.init=function(){var a=this.workspace_;this.HtmlDiv=goog.dom.createDom("div","blocklyToolboxDiv");this.HtmlDiv.setAttribute("dir",this.workspace_.RTL?"RTL":"LTR");document.body.appendChild(this.HtmlDiv);Blockly.bindEvent_(this.HtmlDiv,"mousedown",this,function(a){Blockly.isRightButton(a)||a.target==this.HtmlDiv?Blockly.hideChaff(!1):Blockly.hideChaff(!0)});this.flyout_=new Blockly.Flyout({parentWorkspace:a,RTL:a.RTL});goog.dom.insertSiblingAfter(this.flyout_.createDom(),a.svgGroup_);
|
||||
this.flyout_.init(a);this.CONFIG_.cleardotPath=a.options.pathToMedia+"1x1.gif";this.CONFIG_.cssCollapsedFolderIcon="blocklyTreeIconClosed"+(this.workspace_.RTL?"Rtl":"Ltr");var b=new Blockly.Toolbox.TreeControl(this,this.CONFIG_);this.tree_=b;b.setShowRootNode(!1);b.setShowLines(!1);b.setShowExpandIcons(!1);b.setSelectedItem(null);this.populate_(a.options.languageTree);b.render(this.HtmlDiv);this.position()};
|
||||
Blockly.Toolbox.prototype.position=function(){var a=this.HtmlDiv;if(a){var b=this.workspace_.options.svg,c=goog.style.getPageOffset(b),b=Blockly.svgSize(b);a.style.left=this.workspace_.RTL?c.x+b.width-a.offsetWidth+"px":c.x+"px";a.style.height=b.height+"px";a.style.top=c.y+"px";this.width=a.offsetWidth;this.workspace_.RTL||--this.width;this.flyout_.position()}};
|
||||
Blockly.Toolbox.prototype.dispose=function(){this.flyout_.dispose();this.tree_.dispose();goog.dom.removeNode(this.HtmlDiv)};Blockly.Toolbox.prototype.position=function(){var a=this.HtmlDiv;if(a){var b=this.workspace_.options.svg,c=goog.style.getPageOffset(b),b=Blockly.svgSize(b);a.style.left=this.workspace_.RTL?c.x+b.width-a.offsetWidth+"px":c.x+"px";a.style.height=b.height+"px";a.style.top=c.y+"px";this.width=a.offsetWidth;this.workspace_.RTL||--this.width;this.flyout_.position()}};
|
||||
Blockly.Toolbox.prototype.populate_=function(a){function b(a,e){for(var f=0,g;g=a.childNodes[f];f++)if(g.tagName)switch(g.tagName.toUpperCase()){case "CATEGORY":var h=c.createNode(g.getAttribute("name"));h.blocks=[];"true"==g.getAttribute("expanded")&&h.setExpanded(!0);e.add(h);var k=g.getAttribute("custom");k?h.blocks=k:b(g,h);break;case "SEP":e.add(new Blockly.Toolbox.TreeSeparator);break;case "BLOCK":e.blocks.push(g)}}var c=this.tree_;c.removeChildren();c.blocks=[];b(a,this.tree_);if(c.blocks.length)throw"Toolbox cannot have both blocks and categories in the root level.";
|
||||
Blockly.fireUiEvent(window,"resize")};Blockly.Toolbox.prototype.clearSelection=function(){this.tree_.setSelectedItem(null)};Blockly.Toolbox.prototype.getRect=function(){var a=this.workspace_.RTL?Blockly.svgSize(this.workspace_.options.svg).width-this.width:-1E7;return new goog.math.Rect(a,-1E7,1E7+this.width,2E7)};Blockly.Toolbox.TreeControl=function(a,b){this.toolbox_=a;goog.ui.tree.TreeControl.call(this,goog.html.SafeHtml.EMPTY,b)};goog.inherits(Blockly.Toolbox.TreeControl,goog.ui.tree.TreeControl);
|
||||
Blockly.Toolbox.TreeControl.prototype.enterDocument=function(){Blockly.Toolbox.TreeControl.superClass_.enterDocument.call(this);if(goog.events.BrowserFeature.TOUCH_ENABLED){var a=this.getElement();Blockly.bindEvent_(a,goog.events.EventType.TOUCHSTART,this,this.handleTouchEvent_)}};Blockly.Toolbox.TreeControl.prototype.handleTouchEvent_=function(a){a.preventDefault();var b=this.getNodeFromEvent_(a);b&&a.type===goog.events.EventType.TOUCHSTART&&setTimeout(function(){b.onMouseDown(a)},1)};
|
||||
|
||||
+43
-43
File diff suppressed because one or more lines are too long
@@ -79,7 +79,8 @@ class Gen_uncompressed(threading.Thread):
|
||||
f = open(target_filename, 'w')
|
||||
f.write(HEADER)
|
||||
f.write("""
|
||||
window.BLOCKLY_DIR = (function() {
|
||||
// 'this' is 'window' in a browser, or 'global' in node.js.
|
||||
this.BLOCKLY_DIR = (function() {
|
||||
// Find name of current directory.
|
||||
var scripts = document.getElementsByTagName('script');
|
||||
var re = new RegExp('(.+)[\/]blockly_uncompressed\.js$');
|
||||
@@ -93,15 +94,15 @@ window.BLOCKLY_DIR = (function() {
|
||||
return '';
|
||||
})();
|
||||
|
||||
window.BLOCKLY_BOOT = function() {
|
||||
this.BLOCKLY_BOOT = function() {
|
||||
// Execute after Closure has loaded.
|
||||
if (!window.goog) {
|
||||
if (!this.goog) {
|
||||
alert('Error: Closure not found. Read this:\\n' +
|
||||
'developers.google.com/blockly/hacking/closure');
|
||||
}
|
||||
|
||||
// Build map of all dependencies (used and unused).
|
||||
var dir = window.BLOCKLY_DIR.match(/[^\\/]+$/)[0];
|
||||
var dir = this.BLOCKLY_DIR.match(/[^\\/]+$/)[0];
|
||||
""")
|
||||
add_dependency = []
|
||||
base_path = calcdeps.FindClosureBasePath(self.search_paths)
|
||||
@@ -127,16 +128,16 @@ var dir = window.BLOCKLY_DIR.match(/[^\\/]+$/)[0];
|
||||
f.write('goog.require(\'%s\');\n' % provide)
|
||||
|
||||
f.write("""
|
||||
delete window.BLOCKLY_DIR;
|
||||
delete window.BLOCKLY_BOOT;
|
||||
delete this.BLOCKLY_DIR;
|
||||
delete this.BLOCKLY_BOOT;
|
||||
};
|
||||
|
||||
// Delete any existing Closure (e.g. Soy's nogoog_shim).
|
||||
document.write('<script>var goog = undefined;</script>');
|
||||
// Load fresh Closure Library.
|
||||
document.write('<script src="' + window.BLOCKLY_DIR +
|
||||
document.write('<script src="' + this.BLOCKLY_DIR +
|
||||
'/../closure-library/closure/goog/base.js"></script>');
|
||||
document.write('<script>window.BLOCKLY_BOOT()</script>');
|
||||
document.write('<script>this.BLOCKLY_BOOT()</script>');
|
||||
""")
|
||||
f.close()
|
||||
print('SUCCESS: ' + target_filename)
|
||||
|
||||
@@ -139,6 +139,15 @@ Blockly.Toolbox.prototype.init = function() {
|
||||
this.position();
|
||||
};
|
||||
|
||||
/**
|
||||
* Dispose of this toolbox.
|
||||
*/
|
||||
Blockly.Toolbox.prototype.dispose = function() {
|
||||
this.flyout_.dispose();
|
||||
this.tree_.dispose();
|
||||
goog.dom.removeNode(this.HtmlDiv);
|
||||
};
|
||||
|
||||
/**
|
||||
* Move the toolbox to the edge.
|
||||
*/
|
||||
|
||||
@@ -162,6 +162,10 @@ Blockly.WorkspaceSvg.prototype.dispose = function() {
|
||||
}
|
||||
this.svgBlockCanvas_ = null;
|
||||
this.svgBubbleCanvas_ = null;
|
||||
if (this.toolbox_) {
|
||||
this.toolbox_.dispose();
|
||||
this.toolbox_ = null;
|
||||
}
|
||||
if (this.flyout_) {
|
||||
this.flyout_.dispose();
|
||||
this.flyout_ = null;
|
||||
|
||||
@@ -81,7 +81,8 @@
|
||||
<script>
|
||||
var workspace = Blockly.inject('blocklyDiv',
|
||||
{media: '../../media/',
|
||||
maxBlocks: 5, toolbox: document.getElementById('toolbox')});
|
||||
maxBlocks: 5,
|
||||
toolbox: document.getElementById('toolbox')});
|
||||
|
||||
function onchange() {
|
||||
document.getElementById('capacity').innerHTML = workspace.remainingCapacity();
|
||||
|
||||
Reference in New Issue
Block a user