field_checkbox.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Checkbox field. Checked or not checked.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldCheckbox');
  26. goog.require('Blockly.Field');
  27. /**
  28. * Class for a checkbox field.
  29. * @param {string} state The initial state of the field ('TRUE' or 'FALSE').
  30. * @param {Function=} opt_validator A function that is executed when a new
  31. * option is selected. Its sole argument is the new checkbox state. If
  32. * it returns a value, this becomes the new checkbox state, unless the
  33. * value is null, in which case the change is aborted.
  34. * @extends {Blockly.Field}
  35. * @constructor
  36. */
  37. Blockly.FieldCheckbox = function(state, opt_validator) {
  38. Blockly.FieldCheckbox.superClass_.constructor.call(this, '', opt_validator);
  39. // Set the initial state.
  40. this.setValue(state);
  41. };
  42. goog.inherits(Blockly.FieldCheckbox, Blockly.Field);
  43. /**
  44. * Character for the checkmark.
  45. */
  46. Blockly.FieldCheckbox.CHECK_CHAR = '\u2713';
  47. /**
  48. * Mouse cursor style when over the hotspot that initiates editability.
  49. */
  50. Blockly.FieldCheckbox.prototype.CURSOR = 'default';
  51. /**
  52. * Install this checkbox on a block.
  53. */
  54. Blockly.FieldCheckbox.prototype.init = function() {
  55. if (this.fieldGroup_) {
  56. // Checkbox has already been initialized once.
  57. return;
  58. }
  59. Blockly.FieldCheckbox.superClass_.init.call(this);
  60. // The checkbox doesn't use the inherited text element.
  61. // Instead it uses a custom checkmark element that is either visible or not.
  62. this.checkElement_ = Blockly.createSvgElement('text',
  63. {'class': 'blocklyText blocklyCheckbox', 'x': -3, 'y': 14},
  64. this.fieldGroup_);
  65. var textNode = document.createTextNode(Blockly.FieldCheckbox.CHECK_CHAR);
  66. this.checkElement_.appendChild(textNode);
  67. this.checkElement_.style.display = this.state_ ? 'block' : 'none';
  68. };
  69. /**
  70. * Return 'TRUE' if the checkbox is checked, 'FALSE' otherwise.
  71. * @return {string} Current state.
  72. */
  73. Blockly.FieldCheckbox.prototype.getValue = function() {
  74. return String(this.state_).toUpperCase();
  75. };
  76. /**
  77. * Set the checkbox to be checked if strBool is 'TRUE', unchecks otherwise.
  78. * @param {string} strBool New state.
  79. */
  80. Blockly.FieldCheckbox.prototype.setValue = function(strBool) {
  81. var newState = (strBool.toUpperCase() == 'TRUE');
  82. if (this.state_ !== newState) {
  83. if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
  84. Blockly.Events.fire(new Blockly.Events.Change(
  85. this.sourceBlock_, 'field', this.name, this.state_, newState));
  86. }
  87. this.state_ = newState;
  88. if (this.checkElement_) {
  89. this.checkElement_.style.display = newState ? 'block' : 'none';
  90. }
  91. }
  92. };
  93. /**
  94. * Toggle the state of the checkbox.
  95. * @private
  96. */
  97. Blockly.FieldCheckbox.prototype.showEditor_ = function() {
  98. var newState = !this.state_;
  99. if (this.sourceBlock_) {
  100. // Call any validation function, and allow it to override.
  101. newState = this.callValidator(newState);
  102. }
  103. if (newState !== null) {
  104. this.setValue(String(newState).toUpperCase());
  105. }
  106. };