field_clickimage.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 clickable image field.
  22. * @author toebes@extremenetworks.com (John Toebes)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.FieldClickImage');
  26. goog.require('Blockly.FieldImage');
  27. /**
  28. * Class for a clickable image.
  29. * @param {string} src The URL of the image.
  30. * @param {number} width Width of the image.
  31. * @param {number} height Height of the image.
  32. * @param {?string} opt_alt Optional alt text for when block is collapsed.
  33. * @param {?Function} opt_addHandler A function that is executed when the
  34. * image is selected.
  35. * @extends {Blockly.FieldImage}
  36. * @constructor
  37. */
  38. Blockly.FieldClickImage = function(src, width, height, opt_alt, opt_Handler, yOffset) {
  39. Blockly.FieldClickImage.superClass_.constructor.call(this,
  40. src, width, height, '');
  41. this.handler_ = opt_Handler;
  42. this.yOffset_ = yOffset;
  43. };
  44. goog.inherits(Blockly.FieldClickImage, Blockly.FieldImage);
  45. /**
  46. * Editable fields are saved by the XML renderer, non-editable fields are not.
  47. * However since we have no name, we won't be saved out.
  48. */
  49. Blockly.FieldClickImage.prototype.EDITABLE = true;
  50. /**
  51. * Mouse cursor style when over the hotspot that initiates the editor.
  52. */
  53. Blockly.FieldClickImage.prototype.CURSOR = 'default';
  54. /**
  55. * Add or remove the UI indicating if this image may be clicked or not.
  56. */
  57. Blockly.FieldClickImage.prototype.updateEditable = function() {
  58. if (this.sourceBlock_.isInFlyout || !this.EDITABLE) {
  59. Blockly.addClass_(/** @type {!Element} */ (this.fieldGroup_),
  60. 'blocklyIconGroupReadonly');
  61. } else {
  62. Blockly.removeClass_(/** @type {!Element} */ (this.fieldGroup_),
  63. 'blocklyIconGroupReadonly');
  64. }
  65. };
  66. /**
  67. * Install this field on a block.
  68. * @param {!Blockly.Block} block The block containing this field.
  69. */
  70. Blockly.FieldClickImage.prototype.init = function(block) {
  71. if (this.fieldGroup_) {
  72. // Image has already been initialized once.
  73. return;
  74. }
  75. Blockly.FieldClickImage.superClass_.init.call(this, block);
  76. // We want to use the styling of an Icon to indicate clickability
  77. Blockly.addClass_(/** @type {!Element} */ (this.fieldGroup_),
  78. 'blocklyIconGroup');
  79. //
  80. // Update the classes for this to appear editable
  81. this.updateEditable();
  82. // And bind to the mouseup so that we can get called for a click
  83. this.mouseUpWrapper_ =
  84. Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_);
  85. // Force a render.
  86. this.updateTextNode_();
  87. this.imageElement_.style.y = this.yOffset_;
  88. }
  89. /**
  90. * Clone this FieldClickImage.
  91. * @return {!Blockly.FieldClickImage} The result of calling the constructor again
  92. * with the current values of the arguments used during construction.
  93. */
  94. Blockly.FieldClickImage.prototype.clone = function() {
  95. return new Blockly.FieldClickImage(this.handler_,
  96. this.rootBlock_, this.name_, this.pos_);
  97. };
  98. /**
  99. * Take the action of the block
  100. * Note that this does swap out the dragMode_ variable because we know that
  101. * We only get invoked when we aren't actually dragging (otherwise the click
  102. * would be consumed by the drag code). Once we return, there is a small amount
  103. * of cleanup which needs to complete
  104. * @private
  105. */
  106. Blockly.FieldClickImage.prototype.showEditor_ = function(e) {
  107. if (this.handler_) {
  108. var saveDragMode = Blockly.dragMode_;
  109. Blockly.dragMode_ = 0;
  110. this.handler_(this, this.sourceBlock_, e);
  111. Blockly.dragMode_ = saveDragMode;
  112. }
  113. };