Commit 3122c8a9 authored by Neil Fraser's avatar Neil Fraser

De-singleton Blockly. Part 1.

parent 6dc68379
...@@ -622,7 +622,7 @@ Blockly.Block.prototype.getFieldValue = function(name) { ...@@ -622,7 +622,7 @@ Blockly.Block.prototype.getFieldValue = function(name) {
* @deprecated December 2013 * @deprecated December 2013
*/ */
Blockly.Block.prototype.getTitleValue = function(name) { Blockly.Block.prototype.getTitleValue = function(name) {
console.log('Deprecated call to getTitleValue, use getFieldValue instead.'); console.warn('Deprecated call to getTitleValue, use getFieldValue instead.');
return this.getFieldValue(name); return this.getFieldValue(name);
}; };
...@@ -644,7 +644,7 @@ Blockly.Block.prototype.setFieldValue = function(newValue, name) { ...@@ -644,7 +644,7 @@ Blockly.Block.prototype.setFieldValue = function(newValue, name) {
* @deprecated December 2013 * @deprecated December 2013
*/ */
Blockly.Block.prototype.setTitleValue = function(newValue, name) { Blockly.Block.prototype.setTitleValue = function(newValue, name) {
console.log('Deprecated call to setTitleValue, use setFieldValue instead.'); console.warn('Deprecated call to setTitleValue, use setFieldValue instead.');
this.setFieldValue(newValue, name); this.setFieldValue(newValue, name);
}; };
......
...@@ -484,7 +484,7 @@ Blockly.BlockSvg.prototype.showHelp_ = function() { ...@@ -484,7 +484,7 @@ Blockly.BlockSvg.prototype.showHelp_ = function() {
* @private * @private
*/ */
Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
if (Blockly.readOnly || !this.contextMenu) { if (this.workspace.options.readOnly || !this.contextMenu) {
return; return;
} }
// Save the current block in a variable for use in closures. // Save the current block in a variable for use in closures.
...@@ -505,7 +505,8 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { ...@@ -505,7 +505,8 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
} }
options.push(duplicateOption); options.push(duplicateOption);
if (this.isEditable() && !this.collapsed_ && Blockly.comments) { if (this.isEditable() && !this.collapsed_ &&
this.workspace.options.comments) {
// Option to add/remove a comment. // Option to add/remove a comment.
var commentOption = {enabled: true}; var commentOption = {enabled: true};
if (this.comment) { if (this.comment) {
...@@ -539,7 +540,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { ...@@ -539,7 +540,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
} }
} }
if (Blockly.collapse) { if (this.workspace.options.collapse) {
// Option to collapse/expand block. // Option to collapse/expand block.
if (this.collapsed_) { if (this.collapsed_) {
var expandOption = {enabled: true}; var expandOption = {enabled: true};
...@@ -558,7 +559,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { ...@@ -558,7 +559,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
} }
} }
if (Blockly.disable) { if (this.workspace.options.disable) {
// Option to disable/enable block. // Option to disable/enable block.
var disableOption = { var disableOption = {
text: this.disabled ? text: this.disabled ?
......
...@@ -54,12 +54,6 @@ goog.require('goog.color'); ...@@ -54,12 +54,6 @@ goog.require('goog.color');
goog.require('goog.userAgent'); goog.require('goog.userAgent');
/**
* Path to Blockly's media directory. Can be relative, absolute, or remote.
* Used for loading sounds and sprites. Defaults to demo server.
*/
Blockly.pathToMedia = 'https://blockly-demo.appspot.com/static/media/';
/** /**
* Required name space for SVG elements. * Required name space for SVG elements.
* @const * @const
...@@ -383,7 +377,6 @@ Blockly.onKeyDown_ = function(e) { ...@@ -383,7 +377,6 @@ Blockly.onKeyDown_ = function(e) {
// When focused on an HTML text input widget, don't trap any keys. // When focused on an HTML text input widget, don't trap any keys.
return; return;
} }
// TODO: Add keyboard support for cursoring around the context menu.
if (e.keyCode == 27) { if (e.keyCode == 27) {
// Pressing esc closes the context menu. // Pressing esc closes the context menu.
Blockly.hideChaff(); Blockly.hideChaff();
...@@ -494,17 +487,18 @@ Blockly.copy_ = function(block) { ...@@ -494,17 +487,18 @@ Blockly.copy_ = function(block) {
* @private * @private
*/ */
Blockly.showContextMenu_ = function(e) { Blockly.showContextMenu_ = function(e) {
if (Blockly.readOnly) { var workspace = Blockly.mainWorkspace;
if (workspace.options.readOnly) {
return; return;
} }
var options = []; var options = [];
// Add a little animation to collapsing and expanding. // Add a little animation to collapsing and expanding.
var COLLAPSE_DELAY = 10; var COLLAPSE_DELAY = 10;
if (Blockly.collapse) { if (workspace.options.collapse) {
var hasCollapsedBlocks = false; var hasCollapsedBlocks = false;
var hasExpandedBlocks = false; var hasExpandedBlocks = false;
var topBlocks = Blockly.mainWorkspace.getTopBlocks(false); var topBlocks = workspace.getTopBlocks(false);
for (var i = 0; i < topBlocks.length; i++) { for (var i = 0; i < topBlocks.length; i++) {
var block = topBlocks[i]; var block = topBlocks[i];
while (block) { while (block) {
...@@ -702,19 +696,20 @@ Blockly.playAudio = function(name, opt_volume) { ...@@ -702,19 +696,20 @@ Blockly.playAudio = function(name, opt_volume) {
* @private * @private
*/ */
Blockly.getMainWorkspaceMetrics_ = function() { Blockly.getMainWorkspaceMetrics_ = function() {
var mainWorkspace = Blockly.mainWorkspace;
var svgSize = Blockly.svgSize(); var svgSize = Blockly.svgSize();
if (Blockly.mainWorkspace.toolbox_) { if (mainWorkspace.toolbox_) {
svgSize.width -= Blockly.mainWorkspace.toolbox_.width; svgSize.width -= mainWorkspace.toolbox_.width;
} }
var viewWidth = svgSize.width - Blockly.Scrollbar.scrollbarThickness; var viewWidth = svgSize.width - Blockly.Scrollbar.scrollbarThickness;
var viewHeight = svgSize.height - Blockly.Scrollbar.scrollbarThickness; var viewHeight = svgSize.height - Blockly.Scrollbar.scrollbarThickness;
try { try {
var blockBox = Blockly.mainWorkspace.getCanvas().getBBox(); var blockBox = mainWorkspace.getCanvas().getBBox();
} catch (e) { } catch (e) {
// Firefox has trouble with hidden elements (Bug 528969). // Firefox has trouble with hidden elements (Bug 528969).
return null; return null;
} }
if (Blockly.mainWorkspace.scrollbar) { if (mainWorkspace.scrollbar) {
// Add a border around the content that is at least half a screenful wide. // Add a border around the content that is at least half a screenful wide.
// Ensure border is wide enough that blocks can scroll over entire screen. // Ensure border is wide enough that blocks can scroll over entire screen.
var MARGIN = 5; var MARGIN = 5;
...@@ -736,16 +731,16 @@ Blockly.getMainWorkspaceMetrics_ = function() { ...@@ -736,16 +731,16 @@ Blockly.getMainWorkspaceMetrics_ = function() {
var bottomEdge = topEdge + blockBox.height; var bottomEdge = topEdge + blockBox.height;
} }
var absoluteLeft = 0; var absoluteLeft = 0;
if (!Blockly.RTL && Blockly.mainWorkspace.toolbox_) { if (!Blockly.RTL && mainWorkspace.toolbox_) {
absoluteLeft = Blockly.mainWorkspace.toolbox_.width; absoluteLeft = mainWorkspace.toolbox_.width;
} }
var metrics = { var metrics = {
viewHeight: svgSize.height, viewHeight: svgSize.height,
viewWidth: svgSize.width, viewWidth: svgSize.width,
contentHeight: bottomEdge - topEdge, contentHeight: bottomEdge - topEdge,
contentWidth: rightEdge - leftEdge, contentWidth: rightEdge - leftEdge,
viewTop: -Blockly.mainWorkspace.scrollY, viewTop: -mainWorkspace.scrollY,
viewLeft: -Blockly.mainWorkspace.scrollX, viewLeft: -mainWorkspace.scrollX,
contentTop: topEdge, contentTop: topEdge,
contentLeft: leftEdge, contentLeft: leftEdge,
absoluteTop: 0, absoluteTop: 0,
...@@ -761,23 +756,24 @@ Blockly.getMainWorkspaceMetrics_ = function() { ...@@ -761,23 +756,24 @@ Blockly.getMainWorkspaceMetrics_ = function() {
* @private * @private
*/ */
Blockly.setMainWorkspaceMetrics_ = function(xyRatio) { Blockly.setMainWorkspaceMetrics_ = function(xyRatio) {
if (!Blockly.mainWorkspace.scrollbar) { var mainWorkspace = Blockly.mainWorkspace;
if (!mainWorkspace.scrollbar) {
throw 'Attempt to set main workspace scroll without scrollbars.'; throw 'Attempt to set main workspace scroll without scrollbars.';
} }
var metrics = Blockly.getMainWorkspaceMetrics_(); var metrics = Blockly.getMainWorkspaceMetrics_();
if (goog.isNumber(xyRatio.x)) { if (goog.isNumber(xyRatio.x)) {
Blockly.mainWorkspace.scrollX = -metrics.contentWidth * xyRatio.x - mainWorkspace.scrollX = -metrics.contentWidth * xyRatio.x -
metrics.contentLeft; metrics.contentLeft;
} }
if (goog.isNumber(xyRatio.y)) { if (goog.isNumber(xyRatio.y)) {
Blockly.mainWorkspace.scrollY = -metrics.contentHeight * xyRatio.y - mainWorkspace.scrollY = -metrics.contentHeight * xyRatio.y -
metrics.contentTop; metrics.contentTop;
} }
var x = Blockly.mainWorkspace.scrollX + metrics.absoluteLeft; var x = mainWorkspace.scrollX + metrics.absoluteLeft;
var y = Blockly.mainWorkspace.scrollY + metrics.absoluteTop; var y = mainWorkspace.scrollY + metrics.absoluteTop;
Blockly.mainWorkspace.translate(x, y); mainWorkspace.translate(x, y);
Blockly.mainWorkspacePattern_.setAttribute('x', x); mainWorkspace.gridPattern_.setAttribute('x', x);
Blockly.mainWorkspacePattern_.setAttribute('y', y); mainWorkspace.gridPattern_.setAttribute('y', y);
}; };
/** /**
......
...@@ -66,15 +66,18 @@ Blockly.Css.mediaPath_ = ''; ...@@ -66,15 +66,18 @@ Blockly.Css.mediaPath_ = '';
* a) It loads synchronously and doesn't force a redraw later. * a) It loads synchronously and doesn't force a redraw later.
* b) It speeds up loading by not blocking on a separate HTTP transfer. * b) It speeds up loading by not blocking on a separate HTTP transfer.
* c) The CSS content may be made dynamic depending on init options. * c) The CSS content may be made dynamic depending on init options.
* @param {boolean} hasCss If false, don't inject CSS
* (providing CSS becomes the document's responsibility).
* @param {string} pathToMedia Path from page to the Blockly media directory.
*/ */
Blockly.Css.inject = function() { Blockly.Css.inject = function(hasCss, pathToMedia) {
// Placeholder for cursor rule. Must be first rule (index 0). // Placeholder for cursor rule. Must be first rule (index 0).
var text = '.blocklyDraggable {}\n'; var text = '.blocklyDraggable {}\n';
if (Blockly.hasCss) { if (hasCss) {
text += Blockly.Css.CONTENT.join('\n'); text += Blockly.Css.CONTENT.join('\n');
} }
// Strip off any trailing slash (either Unix or Windows). // Strip off any trailing slash (either Unix or Windows).
Blockly.Css.mediaPath_ = Blockly.pathToMedia.replace(/[\\\/]$/, ''); Blockly.Css.mediaPath_ = pathToMedia.replace(/[\\\/]$/, '');
text = text.replace(/<<<PATH>>>/g, Blockly.Css.mediaPath_); text = text.replace(/<<<PATH>>>/g, Blockly.Css.mediaPath_);
Blockly.Css.styleSheet_ = goog.cssom.addCssText(text).sheet; Blockly.Css.styleSheet_ = goog.cssom.addCssText(text).sheet;
Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN); Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN);
......
This diff is collapsed.
...@@ -102,7 +102,7 @@ Blockly.Input.prototype.appendField = function(field, opt_name) { ...@@ -102,7 +102,7 @@ Blockly.Input.prototype.appendField = function(field, opt_name) {
* @deprecated December 2013 * @deprecated December 2013
*/ */
Blockly.Input.prototype.appendTitle = function(field, opt_name) { Blockly.Input.prototype.appendTitle = function(field, opt_name) {
console.log('Deprecated call to appendTitle, use appendField instead.'); console.warn('Deprecated call to appendTitle, use appendField instead.');
return this.appendField(field, opt_name); return this.appendField(field, opt_name);
}; };
......
...@@ -186,3 +186,35 @@ Blockly.Workspace.prototype.remainingCapacity = function() { ...@@ -186,3 +186,35 @@ Blockly.Workspace.prototype.remainingCapacity = function() {
Blockly.Workspace.prototype.fireChangeEvent = function() { Blockly.Workspace.prototype.fireChangeEvent = function() {
// NOP. // NOP.
}; };
/**
* Modify the block tree on the existing toolbox.
* @param {Node|string} tree DOM tree of blocks, or text representation of same.
*/
Blockly.Workspace.prototype.updateToolbox = function(tree) {
tree = Blockly.parseToolboxTree_(tree);
if (!tree) {
if (Blockly.languageTree) {
throw 'Can\'t nullify an existing toolbox.';
}
// No change (null to null).
return;
}
if (!Blockly.languageTree) {
throw 'Existing toolbox is null. Can\'t create new toolbox.';
}
var hasCategories = !!tree.getElementsByTagName('category').length;
if (hasCategories) {
if (!this.toolbox_) {
throw 'Existing toolbox has no categories. Can\'t change mode.';
}
Blockly.languageTree = tree;
this.toolbox_.populate_();
} else {
if (!this.flyout_) {
throw 'Existing toolbox has categories. Can\'t change mode.';
}
Blockly.languageTree = tree;
this.flyout_.show(Blockly.languageTree.childNodes);
}
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment