field_colour.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 Colour input field.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldColour');
  26. goog.require('Blockly.Field');
  27. goog.require('goog.dom');
  28. goog.require('goog.events');
  29. goog.require('goog.style');
  30. goog.require('goog.ui.ColorPicker');
  31. /**
  32. * Class for a colour input field.
  33. * @param {string} colour The initial colour in '#rrggbb' format.
  34. * @param {Function=} opt_validator A function that is executed when a new
  35. * colour is selected. Its sole argument is the new colour value. Its
  36. * return value becomes the selected colour, unless it is undefined, in
  37. * which case the new colour stands, or it is null, in which case the change
  38. * is aborted.
  39. * @extends {Blockly.Field}
  40. * @constructor
  41. */
  42. Blockly.FieldColour = function(colour, opt_validator) {
  43. Blockly.FieldColour.superClass_.constructor.call(this, colour, opt_validator);
  44. this.setText(Blockly.Field.NBSP + Blockly.Field.NBSP + Blockly.Field.NBSP);
  45. };
  46. goog.inherits(Blockly.FieldColour, Blockly.Field);
  47. /**
  48. * By default use the global constants for colours.
  49. * @type {Array.<string>}
  50. * @private
  51. */
  52. Blockly.FieldColour.prototype.colours_ = null;
  53. /**
  54. * By default use the global constants for columns.
  55. * @type {number}
  56. * @private
  57. */
  58. Blockly.FieldColour.prototype.columns_ = 0;
  59. /**
  60. * Install this field on a block.
  61. */
  62. Blockly.FieldColour.prototype.init = function() {
  63. Blockly.FieldColour.superClass_.init.call(this);
  64. this.borderRect_.style['fillOpacity'] = 1;
  65. this.setValue(this.getValue());
  66. };
  67. /**
  68. * Mouse cursor style when over the hotspot that initiates the editor.
  69. */
  70. Blockly.FieldColour.prototype.CURSOR = 'default';
  71. /**
  72. * Close the colour picker if this input is being deleted.
  73. */
  74. Blockly.FieldColour.prototype.dispose = function() {
  75. Blockly.WidgetDiv.hideIfOwner(this);
  76. Blockly.FieldColour.superClass_.dispose.call(this);
  77. };
  78. /**
  79. * Return the current colour.
  80. * @return {string} Current colour in '#rrggbb' format.
  81. */
  82. Blockly.FieldColour.prototype.getValue = function() {
  83. return this.colour_;
  84. };
  85. /**
  86. * Set the colour.
  87. * @param {string} colour The new colour in '#rrggbb' format.
  88. */
  89. Blockly.FieldColour.prototype.setValue = function(colour) {
  90. if (this.sourceBlock_ && Blockly.Events.isEnabled() &&
  91. this.colour_ != colour) {
  92. Blockly.Events.fire(new Blockly.Events.Change(
  93. this.sourceBlock_, 'field', this.name, this.colour_, colour));
  94. }
  95. this.colour_ = colour;
  96. if (this.borderRect_) {
  97. this.borderRect_.style.fill = colour;
  98. }
  99. };
  100. /**
  101. * Get the text from this field. Used when the block is collapsed.
  102. * @return {string} Current text.
  103. */
  104. Blockly.FieldColour.prototype.getText = function() {
  105. var colour = this.colour_;
  106. // Try to use #rgb format if possible, rather than #rrggbb.
  107. var m = colour.match(/^#(.)\1(.)\2(.)\3$/);
  108. if (m) {
  109. colour = '#' + m[1] + m[2] + m[3];
  110. }
  111. return colour;
  112. };
  113. /**
  114. * An array of colour strings for the palette.
  115. * See bottom of this page for the default:
  116. * http://docs.closure-library.googlecode.com/git/closure_goog_ui_colorpicker.js.source.html
  117. * @type {!Array.<string>}
  118. */
  119. Blockly.FieldColour.COLOURS = goog.ui.ColorPicker.SIMPLE_GRID_COLORS;
  120. /**
  121. * Number of columns in the palette.
  122. */
  123. Blockly.FieldColour.COLUMNS = 7;
  124. /**
  125. * Set a custom colour grid for this field.
  126. * @param {Array.<string>} colours Array of colours for this block,
  127. * or null to use default (Blockly.FieldColour.COLOURS).
  128. * @return {!Blockly.FieldColour} Returns itself (for method chaining).
  129. */
  130. Blockly.FieldColour.prototype.setColours = function(colours) {
  131. this.colours_ = colours;
  132. return this;
  133. };
  134. /**
  135. * Set a custom grid size for this field.
  136. * @param {number} columns Number of columns for this block,
  137. * or 0 to use default (Blockly.FieldColour.COLUMNS).
  138. * @return {!Blockly.FieldColour} Returns itself (for method chaining).
  139. */
  140. Blockly.FieldColour.prototype.setColumns = function(columns) {
  141. this.columns_ = columns;
  142. return this;
  143. };
  144. /**
  145. * Create a palette under the colour field.
  146. * @private
  147. */
  148. Blockly.FieldColour.prototype.showEditor_ = function() {
  149. Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL,
  150. Blockly.FieldColour.widgetDispose_);
  151. // Create the palette using Closure.
  152. var picker = new goog.ui.ColorPicker();
  153. picker.setSize(this.columns_ || Blockly.FieldColour.COLUMNS);
  154. picker.setColors(this.colours_ || Blockly.FieldColour.COLOURS);
  155. // Position the palette to line up with the field.
  156. // Record windowSize and scrollOffset before adding the palette.
  157. var windowSize = goog.dom.getViewportSize();
  158. var scrollOffset = goog.style.getViewportPageOffset(document);
  159. var xy = this.getAbsoluteXY_();
  160. var borderBBox = this.getScaledBBox_();
  161. var div = Blockly.WidgetDiv.DIV;
  162. picker.render(div);
  163. picker.setSelectedColor(this.getValue());
  164. // Record paletteSize after adding the palette.
  165. var paletteSize = goog.style.getSize(picker.getElement());
  166. // Flip the palette vertically if off the bottom.
  167. if (xy.y + paletteSize.height + borderBBox.height >=
  168. windowSize.height + scrollOffset.y) {
  169. xy.y -= paletteSize.height - 1;
  170. } else {
  171. xy.y += borderBBox.height - 1;
  172. }
  173. if (this.sourceBlock_.RTL) {
  174. xy.x += borderBBox.width;
  175. xy.x -= paletteSize.width;
  176. // Don't go offscreen left.
  177. if (xy.x < scrollOffset.x) {
  178. xy.x = scrollOffset.x;
  179. }
  180. } else {
  181. // Don't go offscreen right.
  182. if (xy.x > windowSize.width + scrollOffset.x - paletteSize.width) {
  183. xy.x = windowSize.width + scrollOffset.x - paletteSize.width;
  184. }
  185. }
  186. Blockly.WidgetDiv.position(xy.x, xy.y, windowSize, scrollOffset,
  187. this.sourceBlock_.RTL);
  188. // Configure event handler.
  189. var thisField = this;
  190. Blockly.FieldColour.changeEventKey_ = goog.events.listen(picker,
  191. goog.ui.ColorPicker.EventType.CHANGE,
  192. function(event) {
  193. var colour = event.target.getSelectedColor() || '#000000';
  194. Blockly.WidgetDiv.hide();
  195. if (thisField.sourceBlock_) {
  196. // Call any validation function, and allow it to override.
  197. colour = thisField.callValidator(colour);
  198. }
  199. if (colour !== null) {
  200. thisField.setValue(colour);
  201. }
  202. });
  203. };
  204. /**
  205. * Hide the colour palette.
  206. * @private
  207. */
  208. Blockly.FieldColour.widgetDispose_ = function() {
  209. if (Blockly.FieldColour.changeEventKey_) {
  210. goog.events.unlistenByKey(Blockly.FieldColour.changeEventKey_);
  211. }
  212. };