field_colour.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. * Array of colors for a 7-cell wide simple-grid color picker.
  33. * @type {Array.<string>}
  34. */
  35. Blockly.SIMPLE_GRID_COLORS = [
  36. // grays
  37. '#ffffff', '#cccccc', '#c0c0c0', '#999999', '#666666', '#333333', '#000000',
  38. // reds
  39. '#ffcccc', '#ff6666', '#ee0000', '#cc0000', '#990000', '#660000', '#330000',
  40. // oranges
  41. '#ffcc99', '#ff9966', '#ff9900', '#ff6600', '#cc6600', '#993300', '#663300',
  42. // yellows
  43. '#ffff99', '#ffff66', '#ffcc66', '#ffcc33', '#cc9933', '#996633', '#663333',
  44. // olives
  45. '#e2c48c', '#ffff33', '#ffff00', '#ffcc00', '#999900', '#666600', '#333300',
  46. // greens
  47. '#99ff99', '#66ff99', '#33ff33', '#33cc00', '#009900', '#006600', '#003300',
  48. // turquoises
  49. '#99ffff', '#33ffff', '#66cccc', '#00cccc', '#339999', '#336666', '#003333',
  50. // blues
  51. '#ccffff', '#66ffff', '#33ccff', '#3366ff', '#3333ff', '#000099', '#000066',
  52. // purples
  53. '#ccccff', '#9999ff', '#6666cc', '#6633ff', '#6600cc', '#333399', '#330099',
  54. // violets
  55. '#ffccff', '#ff80ff', '#cc66cc', '#cc33cc', '#993399', '#663366', '#330033'
  56. ];
  57. /**
  58. * Class for a colour input field.
  59. * @param {string} colour The initial colour in '#rrggbb' format.
  60. * @param {Function=} opt_validator A function that is executed when a new
  61. * colour is selected. Its sole argument is the new colour value. Its
  62. * return value becomes the selected colour, unless it is undefined, in
  63. * which case the new colour stands, or it is null, in which case the change
  64. * is aborted.
  65. * @extends {Blockly.Field}
  66. * @constructor
  67. */
  68. Blockly.FieldColour = function(colour, opt_validator) {
  69. Blockly.FieldColour.superClass_.constructor.call(this, colour, opt_validator);
  70. this.setText(Blockly.Field.NBSP + Blockly.Field.NBSP + Blockly.Field.NBSP);
  71. };
  72. goog.inherits(Blockly.FieldColour, Blockly.Field);
  73. /**
  74. * By default use the global constants for colours.
  75. * @type {Array.<string>}
  76. * @private
  77. */
  78. Blockly.FieldColour.prototype.colours_ = null;
  79. /**
  80. * By default use the global constants for columns.
  81. * @type {number}
  82. * @private
  83. */
  84. Blockly.FieldColour.prototype.columns_ = 0;
  85. /**
  86. * Install this field on a block.
  87. */
  88. Blockly.FieldColour.prototype.init = function() {
  89. Blockly.FieldColour.superClass_.init.call(this);
  90. this.borderRect_.style['fillOpacity'] = 1;
  91. this.setValue(this.getValue());
  92. };
  93. /**
  94. * Mouse cursor style when over the hotspot that initiates the editor.
  95. */
  96. Blockly.FieldColour.prototype.CURSOR = 'default';
  97. /**
  98. * Close the colour picker if this input is being deleted.
  99. */
  100. Blockly.FieldColour.prototype.dispose = function() {
  101. Blockly.WidgetDiv.hideIfOwner(this);
  102. Blockly.FieldColour.superClass_.dispose.call(this);
  103. };
  104. /**
  105. * Return the current colour.
  106. * @return {string} Current colour in '#rrggbb' format.
  107. */
  108. Blockly.FieldColour.prototype.getValue = function() {
  109. return this.colour_;
  110. };
  111. /**
  112. * Set the colour.
  113. * @param {string} colour The new colour in '#rrggbb' format.
  114. */
  115. Blockly.FieldColour.prototype.setValue = function(colour) {
  116. if (this.sourceBlock_ && Blockly.Events.isEnabled() &&
  117. this.colour_ != colour) {
  118. Blockly.Events.fire(new Blockly.Events.Change(
  119. this.sourceBlock_, 'field', this.name, this.colour_, colour));
  120. }
  121. this.colour_ = colour;
  122. if (this.borderRect_) {
  123. this.borderRect_.style.fill = colour;
  124. }
  125. };
  126. /**
  127. * Get the text from this field. Used when the block is collapsed.
  128. * @return {string} Current text.
  129. */
  130. Blockly.FieldColour.prototype.getText = function() {
  131. var colour = this.colour_;
  132. // Try to use #rgb format if possible, rather than #rrggbb.
  133. var m = colour.match(/^#(.)\1(.)\2(.)\3$/);
  134. if (m) {
  135. colour = '#' + m[1] + m[2] + m[3];
  136. }
  137. return colour;
  138. };
  139. /**
  140. * An array of colour strings for the palette.
  141. * See bottom of this page for the default:
  142. * http://docs.closure-library.googlecode.com/git/closure_goog_ui_colorpicker.js.source.html
  143. * @type {!Array.<string>}
  144. */
  145. Blockly.FieldColour.COLOURS = Blockly.SIMPLE_GRID_COLORS;
  146. /**
  147. * Number of columns in the palette.
  148. */
  149. Blockly.FieldColour.COLUMNS = 7;
  150. /**
  151. * Set a custom colour grid for this field.
  152. * @param {Array.<string>} colours Array of colours for this block,
  153. * or null to use default (Blockly.FieldColour.COLOURS).
  154. * @return {!Blockly.FieldColour} Returns itself (for method chaining).
  155. */
  156. Blockly.FieldColour.prototype.setColours = function(colours) {
  157. this.colours_ = colours;
  158. return this;
  159. };
  160. /**
  161. * Set a custom grid size for this field.
  162. * @param {number} columns Number of columns for this block,
  163. * or 0 to use default (Blockly.FieldColour.COLUMNS).
  164. * @return {!Blockly.FieldColour} Returns itself (for method chaining).
  165. */
  166. Blockly.FieldColour.prototype.setColumns = function(columns) {
  167. this.columns_ = columns;
  168. return this;
  169. };
  170. /**
  171. * Create a palette under the colour field.
  172. * @private
  173. */
  174. Blockly.FieldColour.prototype.showEditor_ = function() {
  175. Blockly.WidgetDiv.show(this, this.sourceBlock_.RTL,
  176. Blockly.FieldColour.widgetDispose_);
  177. // Create the palette using Closure.
  178. var picker = new goog.ui.ColorPicker();
  179. picker.setSize(this.columns_ || Blockly.FieldColour.COLUMNS);
  180. picker.setColors(this.colours_ || Blockly.FieldColour.COLOURS);
  181. // Position the palette to line up with the field.
  182. // Record windowSize and scrollOffset before adding the palette.
  183. var windowSize = goog.dom.getViewportSize();
  184. var scrollOffset = goog.style.getViewportPageOffset(document);
  185. var xy = this.getAbsoluteXY_();
  186. var borderBBox = this.getScaledBBox_();
  187. var div = Blockly.WidgetDiv.DIV;
  188. picker.render(div);
  189. picker.setSelectedColor(this.getValue());
  190. // Record paletteSize after adding the palette.
  191. var paletteSize = goog.style.getSize(picker.getElement());
  192. // Flip the palette vertically if off the bottom.
  193. if (xy.y + paletteSize.height + borderBBox.height >=
  194. windowSize.height + scrollOffset.y) {
  195. xy.y -= paletteSize.height - 1;
  196. } else {
  197. xy.y += borderBBox.height - 1;
  198. }
  199. if (this.sourceBlock_.RTL) {
  200. xy.x += borderBBox.width;
  201. xy.x -= paletteSize.width;
  202. // Don't go offscreen left.
  203. if (xy.x < scrollOffset.x) {
  204. xy.x = scrollOffset.x;
  205. }
  206. } else {
  207. // Don't go offscreen right.
  208. if (xy.x > windowSize.width + scrollOffset.x - paletteSize.width) {
  209. xy.x = windowSize.width + scrollOffset.x - paletteSize.width;
  210. }
  211. }
  212. Blockly.WidgetDiv.position(xy.x, xy.y, windowSize, scrollOffset,
  213. this.sourceBlock_.RTL);
  214. // Configure event handler.
  215. var thisField = this;
  216. Blockly.FieldColour.changeEventKey_ = goog.events.listen(picker,
  217. goog.ui.ColorPicker.EventType.CHANGE,
  218. function(event) {
  219. var colour = event.target.getSelectedColor() || '#000000';
  220. Blockly.WidgetDiv.hide();
  221. if (thisField.sourceBlock_) {
  222. // Call any validation function, and allow it to override.
  223. colour = thisField.callValidator(colour);
  224. }
  225. if (colour !== null) {
  226. thisField.setValue(colour);
  227. }
  228. });
  229. };
  230. /**
  231. * Hide the colour palette.
  232. * @private
  233. */
  234. Blockly.FieldColour.widgetDispose_ = function() {
  235. if (Blockly.FieldColour.changeEventKey_) {
  236. goog.events.unlistenByKey(Blockly.FieldColour.changeEventKey_);
  237. }
  238. };