mirror of
https://github.com/google/blockly.git
synced 2026-01-07 00:50:27 +01:00
* Don't require flyouts to run Blockly There was a bug with Dynamic Variables that made them required for Blockly to run. This adds checks so that the flyouts only get registered if they're included. Also does a rebuild of core to pick up the fix. * Also fix dependencies so dynamic vars don't get stripped
118 lines
4.3 KiB
JavaScript
118 lines
4.3 KiB
JavaScript
/**
|
|
* @license
|
|
* Visual Blocks Editor
|
|
*
|
|
* Copyright 2017 Google Inc.
|
|
* https://developers.google.com/blockly/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Utility functions for handling variables dynamic.
|
|
*
|
|
* @author duzc2dtw@gmail.com (Du Tian Wei)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.VariablesDynamic');
|
|
|
|
goog.require('Blockly.Variables');
|
|
goog.require('Blockly.Blocks');
|
|
goog.require('Blockly.constants');
|
|
goog.require('Blockly.VariableModel');
|
|
// TODO Fix circular dependencies
|
|
// goog.require('Blockly.Workspace');
|
|
goog.require('goog.string');
|
|
|
|
|
|
Blockly.VariablesDynamic.onCreateVariableButtonClick_String = function(button) {
|
|
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'String');
|
|
};
|
|
Blockly.VariablesDynamic.onCreateVariableButtonClick_Number = function(button) {
|
|
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'Number');
|
|
};
|
|
Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour = function(button) {
|
|
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'Colour');
|
|
};
|
|
/**
|
|
* Construct the elements (blocks and button) required by the flyout for the
|
|
* variable category.
|
|
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
|
|
* @return {!Array.<!Element>} Array of XML elements.
|
|
*/
|
|
Blockly.VariablesDynamic.flyoutCategory = function(workspace) {
|
|
var xmlList = [];
|
|
var button = goog.dom.createDom('button');
|
|
button.setAttribute('text', Blockly.Msg.NEW_STRING_VARIABLE);
|
|
button.setAttribute('callbackKey', 'CREATE_VARIABLE_STRING');
|
|
xmlList.push(button);
|
|
button = goog.dom.createDom('button');
|
|
button.setAttribute('text', Blockly.Msg.NEW_NUMBER_VARIABLE);
|
|
button.setAttribute('callbackKey', 'CREATE_VARIABLE_NUMBER');
|
|
xmlList.push(button);button = goog.dom.createDom('button');
|
|
button.setAttribute('text', Blockly.Msg.NEW_COLOUR_VARIABLE);
|
|
button.setAttribute('callbackKey', 'CREATE_VARIABLE_COLOUR');
|
|
xmlList.push(button);
|
|
|
|
workspace.registerButtonCallback('CREATE_VARIABLE_STRING',
|
|
Blockly.VariablesDynamic.onCreateVariableButtonClick_String);
|
|
workspace.registerButtonCallback('CREATE_VARIABLE_NUMBER',
|
|
Blockly.VariablesDynamic.onCreateVariableButtonClick_Number);
|
|
workspace.registerButtonCallback('CREATE_VARIABLE_COLOUR',
|
|
Blockly.VariablesDynamic.onCreateVariableButtonClick_Colour);
|
|
|
|
|
|
var blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
|
|
xmlList = xmlList.concat(blockList);
|
|
return xmlList;
|
|
};
|
|
|
|
/**
|
|
* Construct the blocks required by the flyout for the variable category.
|
|
* @param {!Blockly.Workspace} workspace The workspace contianing variables.
|
|
* @return {!Array.<!Element>} Array of XML block elements.
|
|
*/
|
|
Blockly.VariablesDynamic.flyoutCategoryBlocks = function(workspace) {
|
|
var variableModelList = workspace.getAllVariables();
|
|
variableModelList.sort(Blockly.VariableModel.compareByName);
|
|
|
|
var xmlList = [];
|
|
if (variableModelList.length > 0) {
|
|
|
|
if (Blockly.Blocks['variables_set_dynamic']) {
|
|
var firstVariable = variableModelList[0];
|
|
var gap = 24;
|
|
var blockText = '<xml>' +
|
|
'<block type="variables_set_dynamic" gap="' + gap + '">' +
|
|
Blockly.Variables.generateVariableFieldXml_(firstVariable) +
|
|
'</block>' +
|
|
'</xml>';
|
|
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
|
xmlList.push(block);
|
|
}
|
|
for (var i = 0, variable; variable = variableModelList[i]; i++) {
|
|
if (Blockly.Blocks['variables_get_dynamic']) {
|
|
var blockText = '<xml>' +
|
|
'<block type="variables_get_dynamic" gap="8">' +
|
|
Blockly.Variables.generateVariableFieldXml_(variable) +
|
|
'</block>' +
|
|
'</xml>';
|
|
var block = Blockly.Xml.textToDom(blockText).firstChild;
|
|
xmlList.push(block);
|
|
}
|
|
}
|
|
}
|
|
return xmlList;
|
|
};
|