ControlPoint.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import H from './../parts/Globals.js';
  2. import U from './../parts/Utilities.js';
  3. var extend = U.extend,
  4. pick = U.pick;
  5. import eventEmitterMixin from './eventEmitterMixin.js';
  6. /**
  7. * A control point class which is a connection between controllable
  8. * transform methods and a user actions.
  9. *
  10. * @constructor
  11. * @mixes eventEmitterMixin
  12. * @memberOf Annotation
  13. *
  14. * @param {Highcharts.Chart} chart a chart instance
  15. * @param {Object} target a controllable instance which is a target for
  16. * a control point
  17. * @param {Annotation.ControlPoint.Options} options an options object
  18. * @param {number} [index]
  19. */
  20. function ControlPoint(chart, target, options, index) {
  21. this.chart = chart;
  22. this.target = target;
  23. this.options = options;
  24. this.index = pick(options.index, index);
  25. }
  26. /**
  27. * @typedef {Object} Annotation.ControlPoint.Position
  28. * @property {number} x
  29. * @property {number} y
  30. */
  31. /**
  32. * @callback Annotation.ControlPoint.Positioner
  33. * @param {Object} e event
  34. * @param {Controllable} target
  35. * @return {Annotation.ControlPoint.Position} position
  36. */
  37. /**
  38. * @typedef {Object} Annotation.ControlPoint.Options
  39. * @property {string} symbol
  40. * @property {number} width
  41. * @property {number} height
  42. * @property {Object} style
  43. * @property {boolean} visible
  44. * @property {Annotation.ControlPoint.Positioner} positioner
  45. * @property {Object} events
  46. */
  47. extend(
  48. ControlPoint.prototype,
  49. eventEmitterMixin
  50. );
  51. /**
  52. * List of events for `anntation.options.events` that should not be
  53. * added to `annotation.graphic` but to the `annotation`.
  54. *
  55. * @type {Array<string>}
  56. */
  57. ControlPoint.prototype.nonDOMEvents = ['drag'];
  58. /**
  59. * Set the visibility.
  60. *
  61. * @param {boolean} [visible]
  62. **/
  63. ControlPoint.prototype.setVisibility = function (visible) {
  64. this.graphic.attr('visibility', visible ? 'visible' : 'hidden');
  65. this.options.visible = visible;
  66. };
  67. /**
  68. * Render the control point.
  69. */
  70. ControlPoint.prototype.render = function () {
  71. var chart = this.chart,
  72. options = this.options;
  73. this.graphic = chart.renderer
  74. .symbol(
  75. options.symbol,
  76. 0,
  77. 0,
  78. options.width,
  79. options.height
  80. )
  81. .add(chart.controlPointsGroup)
  82. .css(options.style);
  83. this.setVisibility(options.visible);
  84. this.addEvents();
  85. };
  86. /**
  87. * Redraw the control point.
  88. *
  89. * @param {boolean} [animation]
  90. */
  91. ControlPoint.prototype.redraw = function (animation) {
  92. this.graphic[animation ? 'animate' : 'attr'](
  93. this.options.positioner.call(this, this.target)
  94. );
  95. };
  96. /**
  97. * Destroy the control point.
  98. */
  99. ControlPoint.prototype.destroy = function () {
  100. eventEmitterMixin.destroy.call(this);
  101. if (this.graphic) {
  102. this.graphic = this.graphic.destroy();
  103. }
  104. this.chart = null;
  105. this.target = null;
  106. this.options = null;
  107. };
  108. /**
  109. * Update the control point.
  110. */
  111. ControlPoint.prototype.update = function (userOptions) {
  112. var chart = this.chart,
  113. target = this.target,
  114. index = this.index,
  115. options = H.merge(true, this.options, userOptions);
  116. this.destroy();
  117. this.constructor(chart, target, options, index);
  118. this.render(chart.controlPointsGroup);
  119. this.redraw();
  120. };
  121. export default ControlPoint;