/** * @license * Visual Blocks Editor * * Copyright 2018 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 Variable creation events fired as a result of actions in * Blockly's editor. * @author fraser@google.com (Neil Fraser) */ 'use strict'; goog.provide('Blockly.Events.VarCreate'); goog.require('Blockly.Events'); goog.require('Blockly.Events.Abstract'); goog.require('goog.array'); goog.require('goog.math.Coordinate'); /** * Class for a variable creation event. * @param {Blockly.VariableModel} variable The created variable. * Null for a blank event. * @extends {Blockly.Events.Abstract} * @constructor */ Blockly.Events.VarCreate = function(variable) { if (!variable) { return; // Blank event to be populated by fromJson. } Blockly.Events.VarCreate.superClass_.constructor.call(this, variable); this.varType = variable.type; this.varName = variable.name; }; goog.inherits(Blockly.Events.VarCreate, Blockly.Events.Abstract); /** * Type of this event. * @type {string} */ Blockly.Events.VarCreate.prototype.type = Blockly.Events.VAR_CREATE; /** * Encode the event as JSON. * @return {!Object} JSON representation. */ Blockly.Events.VarCreate.prototype.toJson = function() { var json = Blockly.Events.VarCreate.superClass_.toJson.call(this); json['varType'] = this.varType; json['varName'] = this.varName; return json; }; /** * Decode the JSON event. * @param {!Object} json JSON representation. */ Blockly.Events.VarCreate.prototype.fromJson = function(json) { Blockly.Events.VarCreate.superClass_.fromJson.call(this, json); this.varType = json['varType']; this.varName = json['varName']; }; /** * Run a variable creation event. * @param {boolean} forward True if run forward, false if run backward (undo). */ Blockly.Events.VarCreate.prototype.run = function(forward) { var workspace = this.getEventWorkspace_(); if (forward) { workspace.createVariable(this.varName, this.varType, this.varId); } else { workspace.deleteVariableById(this.varId); } };