ControlPoint.js 2.9 KB

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