mirror of
https://github.com/google/blockly.git
synced 2026-05-19 18:40:11 +02:00
d63670662b
I've added the import statement immediately before the goog.declareModuleId calls that depend on it. There is an argument to be made that we should put the import statement in their normal place amongst any other imports, and move the declareModuleId statement to below the double blank line below the imports, but as these are so tightly coupled, replace the previous goog.module calls, and will both be deleted at the same time once the transition to TypeScript is fully complete I think it's fine (and certainly much easier) to do it this way.
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2017 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Components for the variable model.
|
|
*/
|
|
|
|
|
|
/**
|
|
* Components for the variable model.
|
|
* @class
|
|
*/
|
|
import * as goog from '../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.VariableModel');
|
|
|
|
// Unused import preserved for side-effects. Remove if unneeded.
|
|
import './events/events_var_create';
|
|
|
|
import * as eventUtils from './events/utils.js';
|
|
import * as idGenerator from './utils/idgenerator.js';
|
|
/* eslint-disable-next-line no-unused-vars */
|
|
import {Workspace} from './workspace.js';
|
|
|
|
|
|
/**
|
|
* Class for a variable model.
|
|
* Holds information for the variable including name, ID, and type.
|
|
* @see {Blockly.FieldVariable}
|
|
* @alias Blockly.VariableModel
|
|
*/
|
|
export class VariableModel {
|
|
type: string;
|
|
private readonly id_: string;
|
|
|
|
/**
|
|
* @param workspace The variable's workspace.
|
|
* @param name The name of the variable. This is the user-visible name (e.g.
|
|
* 'my var' or '私の変数'), not the generated name.
|
|
* @param opt_type The type of the variable like 'int' or 'string'.
|
|
* Does not need to be unique. Field_variable can filter variables based
|
|
* on their type. This will default to '' which is a specific type.
|
|
* @param opt_id The unique ID of the variable. This will default to a UUID.
|
|
*/
|
|
constructor(
|
|
public workspace: Workspace, public name: string, opt_type?: string,
|
|
opt_id?: string) {
|
|
/**
|
|
* The type of the variable, such as 'int' or 'sound_effect'. This may be
|
|
* used to build a list of variables of a specific type. By default this is
|
|
* the empty string '', which is a specific type.
|
|
* @see {Blockly.FieldVariable}
|
|
*/
|
|
this.type = opt_type || '';
|
|
|
|
/**
|
|
* A unique ID for the variable. This should be defined at creation and
|
|
* not change, even if the name changes. In most cases this should be a
|
|
* UUID.
|
|
*/
|
|
this.id_ = opt_id || idGenerator.genUid();
|
|
|
|
eventUtils.fire(new (eventUtils.get(eventUtils.VAR_CREATE))!(this));
|
|
}
|
|
|
|
/** @return The ID for the variable. */
|
|
getId(): string {
|
|
return this.id_;
|
|
}
|
|
|
|
/**
|
|
* A custom compare function for the VariableModel objects.
|
|
* @param var1 First variable to compare.
|
|
* @param var2 Second variable to compare.
|
|
* @return -1 if name of var1 is less than name of var2, 0 if equal, and 1 if
|
|
* greater.
|
|
*/
|
|
static compareByName(var1: VariableModel, var2: VariableModel): number {
|
|
return var1.name.localeCompare(var2.name, undefined, {sensitivity: 'base'});
|
|
}
|
|
}
|