mirror of
https://github.com/scratchfoundation/scratch-blocks.git
synced 2025-08-28 22:10:31 -04:00
New initial commit with .svn directories and their contents ignored.
This commit is contained in:
commit
a8acffd81c
754 changed files with 85941 additions and 0 deletions
98
core/field_checkbox.js
Normal file
98
core/field_checkbox.js
Normal file
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* Visual Blocks Editor
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* 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 Checkbox field. Checked or not checked.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.FieldCheckbox');
|
||||
|
||||
goog.require('Blockly.Field');
|
||||
|
||||
|
||||
/**
|
||||
* Class for a checkbox field.
|
||||
* @param {string} state The initial state of the field ('TRUE' or 'FALSE').
|
||||
* @param {Function} opt_changeHandler A function that is executed when a new
|
||||
* option is selected.
|
||||
* @extends {Blockly.Field}
|
||||
* @constructor
|
||||
*/
|
||||
Blockly.FieldCheckbox = function(state, opt_changeHandler) {
|
||||
Blockly.FieldCheckbox.superClass_.constructor.call(this, '');
|
||||
|
||||
this.changeHandler_ = opt_changeHandler;
|
||||
// The checkbox doesn't use the inherited text element.
|
||||
// Instead it uses a custom checkmark element that is either visible or not.
|
||||
this.checkElement_ = Blockly.createSvgElement('text',
|
||||
{'class': 'blocklyText', 'x': -3}, this.fieldGroup_);
|
||||
var textNode = document.createTextNode('\u2713');
|
||||
this.checkElement_.appendChild(textNode);
|
||||
// Set the initial state.
|
||||
this.setValue(state);
|
||||
};
|
||||
goog.inherits(Blockly.FieldCheckbox, Blockly.Field);
|
||||
|
||||
/**
|
||||
* Mouse cursor style when over the hotspot that initiates editability.
|
||||
*/
|
||||
Blockly.FieldCheckbox.prototype.CURSOR = 'default';
|
||||
|
||||
/**
|
||||
* Return 'TRUE' if the checkbox is checked, 'FALSE' otherwise.
|
||||
* @return {string} Current state.
|
||||
*/
|
||||
Blockly.FieldCheckbox.prototype.getValue = function() {
|
||||
return String(this.state_).toUpperCase();
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the checkbox to be checked if strBool is 'TRUE', unchecks otherwise.
|
||||
* @param {string} strBool New state.
|
||||
*/
|
||||
Blockly.FieldCheckbox.prototype.setValue = function(strBool) {
|
||||
var newState = (strBool == 'TRUE');
|
||||
if (this.state_ !== newState) {
|
||||
this.state_ = newState;
|
||||
this.checkElement_.style.display = newState ? 'block' : 'none';
|
||||
if (this.sourceBlock_ && this.sourceBlock_.rendered) {
|
||||
this.sourceBlock_.workspace.fireChangeEvent();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Toggle the state of the checkbox.
|
||||
* @private
|
||||
*/
|
||||
Blockly.FieldCheckbox.prototype.showEditor_ = function() {
|
||||
var newState = !this.state_;
|
||||
if (this.changeHandler_) {
|
||||
// Call any change handler, and allow it to override.
|
||||
var override = this.changeHandler_(newState);
|
||||
if (override !== undefined) {
|
||||
newState = override;
|
||||
}
|
||||
}
|
||||
if (newState !== null) {
|
||||
this.setValue(String(newState).toUpperCase());
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue