field_checkbox.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // how big should toggleable images be?
  28. var sz = 25;
  29. /**
  30. * Class for a checkbox field.
  31. * @param {string} state The initial state of the field ('TRUE' or 'FALSE').
  32. * @param {Function=} opt_validator A function that is executed when a new
  33. * option is selected. Its sole argument is the new checkbox state. If
  34. * it returns a value, this becomes the new checkbox state, unless the
  35. * value is null, in which case the change is aborted.
  36. * @extends {Blockly.Field}
  37. * @constructor
  38. */
  39. Blockly.FieldCheckbox = function(state, opt_validator,img1,img2) {
  40. Blockly.FieldCheckbox.superClass_.constructor.call(this, '', opt_validator);
  41. // do I have two images?
  42. if (img1 && img2) {
  43. this.img1 = img1;
  44. this.img2 = img2;
  45. this.size_ = new goog.math.Size(sz, sz * 1.1);
  46. }
  47. // Set the initial state.
  48. // console.log("setting fieldCheckbox initial state to:",state);
  49. this.setValue(state);
  50. };
  51. goog.inherits(Blockly.FieldCheckbox, Blockly.Field);
  52. /**
  53. * Character for the checkmark.
  54. */
  55. Blockly.FieldCheckbox.CHECK_CHAR = '\u2713';
  56. /**
  57. * Mouse cursor style when over the hotspot that initiates editability.
  58. */
  59. Blockly.FieldCheckbox.prototype.CURSOR = 'default';
  60. Blockly.FieldCheckbox.prototype.EDITABLE = true;
  61. /**
  62. * Install this checkbox on a block.
  63. */
  64. Blockly.FieldCheckbox.prototype.init = function() {
  65. if (this.fieldGroup_) {
  66. // Checkbox has already been initialized once.
  67. return;
  68. }
  69. if (!(this.img1 && this.img2)) {
  70. Blockly.FieldCheckbox.superClass_.init.call(this);
  71. // The checkbox doesn't use the inherited text element.
  72. // Instead it uses a custom checkmark element that is either visible or not.
  73. this.checkElement_ = Blockly.createSvgElement('text',
  74. {'class': 'blocklyText blocklyCheckbox', 'x': -3, 'y': 14},
  75. this.fieldGroup_);
  76. var textNode = document.createTextNode(Blockly.FieldCheckbox.CHECK_CHAR);
  77. this.checkElement_.appendChild(textNode);
  78. this.checkElement_.style.display = this.state_ ? 'block' : 'none';
  79. }
  80. else {
  81. // code from field_image and field prototype init
  82. // console.log("got some images");
  83. // Build the DOM.
  84. this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
  85. if (!this.visible_) {
  86. this.fieldGroup_.style.display = 'none';
  87. }
  88. this.imageElement_ = Blockly.createSvgElement('image',
  89. {'height': sz + 'px',
  90. 'width': sz * 1.1 + 'px',
  91. 'x': 5, 'y': -5}, this.fieldGroup_);
  92. this.imageElement_.setAttributeNS('http://www.w3.org/1999/xlink',
  93. 'xlink:href', (this.state_) ? this.img1 : this.img2);
  94. // if (goog.userAgent.GECKO) {
  95. // // Due to a Firefox bug which eats mouse events on image elements,
  96. // // a transparent rectangle needs to be placed on top of the image.
  97. // this.rectElement_ = Blockly.createSvgElement('rect',
  98. // {'height': sz + 'px',
  99. // 'width': sz + 'px',
  100. // 'fill-opacity': 0}, this.fieldGroup_);
  101. // }
  102. this.updateEditable();
  103. this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
  104. this.mouseUpWrapper_ =
  105. Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_);
  106. }
  107. };
  108. /**
  109. * Return 'TRUE' if the checkbox is checked, 'FALSE' otherwise.
  110. * @return {string} Current state.
  111. */
  112. Blockly.FieldCheckbox.prototype.getValue = function() {
  113. return String(this.state_).toUpperCase();
  114. };
  115. /**
  116. * Set the checkbox to be checked if strBool is 'TRUE', unchecks otherwise.
  117. * Can also toggle between two images if they exist.
  118. * @param {string} strBool New state.
  119. */
  120. Blockly.FieldCheckbox.prototype.setValue = function(strBool) {
  121. var newState = (strBool == 'TRUE');
  122. if (this.state_ !== newState) {
  123. if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
  124. Blockly.Events.fire(new Blockly.Events.Change(
  125. this.sourceBlock_, 'field', this.name, this.state_, newState));
  126. }
  127. this.state_ = newState;
  128. // console.log("setting checkbox to:",this.state_);
  129. if (this.checkElement_) {
  130. this.checkElement_.style.display = newState ? 'block' : 'none';
  131. }
  132. else if (this.imageElement_) {
  133. this.imageElement_.setAttributeNS('http://www.w3.org/1999/xlink',
  134. 'xlink:href', newState ? this.img1 : this.img2);
  135. }
  136. }
  137. };
  138. /**
  139. * Toggle the state of the checkbox.
  140. * @private
  141. */
  142. Blockly.FieldCheckbox.prototype.showEditor_ = function() {
  143. var newState = !this.state_;
  144. if (this.sourceBlock_) {
  145. // Call any validation function, and allow it to override.
  146. newState = this.callValidator(newState);
  147. }
  148. if (newState !== null) {
  149. this.setValue(String(newState).toUpperCase());
  150. }
  151. };