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) {
* @deprecated December 2013
*/
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);
};
......@@ -644,7 +644,7 @@ Blockly.Block.prototype.setFieldValue = function(newValue, name) {
* @deprecated December 2013
*/
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);
};
......
......@@ -484,7 +484,7 @@ Blockly.BlockSvg.prototype.showHelp_ = function() {
* @private
*/
Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
if (Blockly.readOnly || !this.contextMenu) {
if (this.workspace.options.readOnly || !this.contextMenu) {
return;
}
// Save the current block in a variable for use in closures.
......@@ -505,7 +505,8 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
}
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.
var commentOption = {enabled: true};
if (this.comment) {
......@@ -539,7 +540,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
}
}
if (Blockly.collapse) {
if (this.workspace.options.collapse) {
// Option to collapse/expand block.
if (this.collapsed_) {
var expandOption = {enabled: true};
......@@ -558,7 +559,7 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) {
}
}
if (Blockly.disable) {
if (this.workspace.options.disable) {
// Option to disable/enable block.
var disableOption = {
text: this.disabled ?
......
......@@ -54,12 +54,6 @@ goog.require('goog.color');
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.
* @const
......@@ -383,7 +377,6 @@ Blockly.onKeyDown_ = function(e) {
// When focused on an HTML text input widget, don't trap any keys.
return;
}
// TODO: Add keyboard support for cursoring around the context menu.
if (e.keyCode == 27) {
// Pressing esc closes the context menu.
Blockly.hideChaff();
......@@ -494,17 +487,18 @@ Blockly.copy_ = function(block) {
* @private
*/
Blockly.showContextMenu_ = function(e) {
if (Blockly.readOnly) {
var workspace = Blockly.mainWorkspace;
if (workspace.options.readOnly) {
return;
}
var options = [];
// Add a little animation to collapsing and expanding.
var COLLAPSE_DELAY = 10;
if (Blockly.collapse) {
if (workspace.options.collapse) {
var hasCollapsedBlocks = false;
var hasExpandedBlocks = false;
var topBlocks = Blockly.mainWorkspace.getTopBlocks(false);
var topBlocks = workspace.getTopBlocks(false);
for (var i = 0; i < topBlocks.length; i++) {
var block = topBlocks[i];
while (block) {
......@@ -702,19 +696,20 @@ Blockly.playAudio = function(name, opt_volume) {
* @private
*/
Blockly.getMainWorkspaceMetrics_ = function() {
var mainWorkspace = Blockly.mainWorkspace;
var svgSize = Blockly.svgSize();
if (Blockly.mainWorkspace.toolbox_) {
svgSize.width -= Blockly.mainWorkspace.toolbox_.width;
if (mainWorkspace.toolbox_) {
svgSize.width -= mainWorkspace.toolbox_.width;
}
var viewWidth = svgSize.width - Blockly.Scrollbar.scrollbarThickness;
var viewHeight = svgSize.height - Blockly.Scrollbar.scrollbarThickness;
try {
var blockBox = Blockly.mainWorkspace.getCanvas().getBBox();
var blockBox = mainWorkspace.getCanvas().getBBox();
} catch (e) {
// Firefox has trouble with hidden elements (Bug 528969).
return null;
}
if (Blockly.mainWorkspace.scrollbar) {
if (mainWorkspace.scrollbar) {
// 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.
var MARGIN = 5;
......@@ -736,16 +731,16 @@ Blockly.getMainWorkspaceMetrics_ = function() {
var bottomEdge = topEdge + blockBox.height;
}
var absoluteLeft = 0;
if (!Blockly.RTL && Blockly.mainWorkspace.toolbox_) {
absoluteLeft = Blockly.mainWorkspace.toolbox_.width;
if (!Blockly.RTL && mainWorkspace.toolbox_) {
absoluteLeft = mainWorkspace.toolbox_.width;
}
var metrics = {
viewHeight: svgSize.height,
viewWidth: svgSize.width,
contentHeight: bottomEdge - topEdge,
contentWidth: rightEdge - leftEdge,
viewTop: -Blockly.mainWorkspace.scrollY,
viewLeft: -Blockly.mainWorkspace.scrollX,
viewTop: -mainWorkspace.scrollY,
viewLeft: -mainWorkspace.scrollX,
contentTop: topEdge,
contentLeft: leftEdge,
absoluteTop: 0,
......@@ -761,23 +756,24 @@ Blockly.getMainWorkspaceMetrics_ = function() {
* @private
*/
Blockly.setMainWorkspaceMetrics_ = function(xyRatio) {
if (!Blockly.mainWorkspace.scrollbar) {
var mainWorkspace = Blockly.mainWorkspace;
if (!mainWorkspace.scrollbar) {
throw 'Attempt to set main workspace scroll without scrollbars.';
}
var metrics = Blockly.getMainWorkspaceMetrics_();
if (goog.isNumber(xyRatio.x)) {
Blockly.mainWorkspace.scrollX = -metrics.contentWidth * xyRatio.x -
mainWorkspace.scrollX = -metrics.contentWidth * xyRatio.x -
metrics.contentLeft;
}
if (goog.isNumber(xyRatio.y)) {
Blockly.mainWorkspace.scrollY = -metrics.contentHeight * xyRatio.y -
mainWorkspace.scrollY = -metrics.contentHeight * xyRatio.y -
metrics.contentTop;
}
var x = Blockly.mainWorkspace.scrollX + metrics.absoluteLeft;
var y = Blockly.mainWorkspace.scrollY + metrics.absoluteTop;
Blockly.mainWorkspace.translate(x, y);
Blockly.mainWorkspacePattern_.setAttribute('x', x);
Blockly.mainWorkspacePattern_.setAttribute('y', y);
var x = mainWorkspace.scrollX + metrics.absoluteLeft;
var y = mainWorkspace.scrollY + metrics.absoluteTop;
mainWorkspace.translate(x, y);
mainWorkspace.gridPattern_.setAttribute('x', x);
mainWorkspace.gridPattern_.setAttribute('y', y);
};
/**
......
......@@ -66,15 +66,18 @@ Blockly.Css.mediaPath_ = '';
* a) It loads synchronously and doesn't force a redraw later.
* 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.
* @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).
var text = '.blocklyDraggable {}\n';
if (Blockly.hasCss) {
if (hasCss) {
text += Blockly.Css.CONTENT.join('\n');
}
// 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_);
Blockly.Css.styleSheet_ = goog.cssom.addCssText(text).sheet;
Blockly.Css.setCursor(Blockly.Css.Cursor.OPEN);
......
This diff is collapsed.
......@@ -102,7 +102,7 @@ Blockly.Input.prototype.appendField = function(field, opt_name) {
* @deprecated December 2013
*/
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);
};
......
......@@ -186,3 +186,35 @@ Blockly.Workspace.prototype.remainingCapacity = function() {
Blockly.Workspace.prototype.fireChangeEvent = function() {
// 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