NavigationBindings.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /* *
  2. *
  3. * (c) 2009-2021 Highsoft, Black Label
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Annotation from './Annotations.js';
  12. import Chart from '../../Core/Chart/Chart.js';
  13. import chartNavigationMixin from '../../Mixins/Navigation.js';
  14. import H from '../../Core/Globals.js';
  15. import U from '../../Core/Utilities.js';
  16. var addEvent = U.addEvent, attr = U.attr, extend = U.extend, format = U.format, fireEvent = U.fireEvent, isArray = U.isArray, isFunction = U.isFunction, isNumber = U.isNumber, isObject = U.isObject, merge = U.merge, objectEach = U.objectEach, pick = U.pick, setOptions = U.setOptions;
  17. /**
  18. * A config object for navigation bindings in annotations.
  19. *
  20. * @interface Highcharts.NavigationBindingsOptionsObject
  21. */ /**
  22. * ClassName of the element for a binding.
  23. * @name Highcharts.NavigationBindingsOptionsObject#className
  24. * @type {string|undefined}
  25. */ /**
  26. * Last event to be fired after last step event.
  27. * @name Highcharts.NavigationBindingsOptionsObject#end
  28. * @type {Function|undefined}
  29. */ /**
  30. * Initial event, fired on a button click.
  31. * @name Highcharts.NavigationBindingsOptionsObject#init
  32. * @type {Function|undefined}
  33. */ /**
  34. * Event fired on first click on a chart.
  35. * @name Highcharts.NavigationBindingsOptionsObject#start
  36. * @type {Function|undefined}
  37. */ /**
  38. * Last event to be fired after last step event. Array of step events to be
  39. * called sequentially after each user click.
  40. * @name Highcharts.NavigationBindingsOptionsObject#steps
  41. * @type {Array<Function>|undefined}
  42. */
  43. var doc = H.doc, win = H.win, PREFIX = 'highcharts-';
  44. /* eslint-disable no-invalid-this, valid-jsdoc */
  45. /**
  46. * IE 9-11 polyfill for Element.closest():
  47. * @private
  48. */
  49. function closestPolyfill(el, s) {
  50. var ElementProto = win.Element.prototype, elementMatches = ElementProto.matches ||
  51. ElementProto.msMatchesSelector ||
  52. ElementProto.webkitMatchesSelector, ret = null;
  53. if (ElementProto.closest) {
  54. ret = ElementProto.closest.call(el, s);
  55. }
  56. else {
  57. do {
  58. if (elementMatches.call(el, s)) {
  59. return el;
  60. }
  61. el = el.parentElement || el.parentNode;
  62. } while (el !== null && el.nodeType === 1);
  63. }
  64. return ret;
  65. }
  66. /**
  67. * @private
  68. * @interface bindingsUtils
  69. */
  70. var bindingsUtils = {
  71. /**
  72. * Update size of background (rect) in some annotations: Measure, Simple
  73. * Rect.
  74. *
  75. * @private
  76. * @function Highcharts.NavigationBindingsUtilsObject.updateRectSize
  77. *
  78. * @param {Highcharts.PointerEventObject} event
  79. * Normalized browser event
  80. *
  81. * @param {Highcharts.Annotation} annotation
  82. * Annotation to be updated
  83. */
  84. updateRectSize: function (event, annotation) {
  85. var chart = annotation.chart, options = annotation.options.typeOptions, coords = chart.pointer.getCoordinates(event), width = coords.xAxis[0].value - options.point.x, height = options.point.y - coords.yAxis[0].value;
  86. annotation.update({
  87. typeOptions: {
  88. background: {
  89. width: chart.inverted ? height : width,
  90. height: chart.inverted ? width : height
  91. }
  92. }
  93. });
  94. },
  95. /**
  96. * Get field type according to value
  97. *
  98. * @private
  99. * @function Highcharts.NavigationBindingsUtilsObject.getFieldType
  100. *
  101. * @param {'boolean'|'number'|'string'} value
  102. * Atomic type (one of: string, number, boolean)
  103. *
  104. * @return {'checkbox'|'number'|'text'}
  105. * Field type (one of: text, number, checkbox)
  106. */
  107. getFieldType: function (value) {
  108. return {
  109. 'string': 'text',
  110. 'number': 'number',
  111. 'boolean': 'checkbox'
  112. }[typeof value];
  113. }
  114. };
  115. /**
  116. * @private
  117. */
  118. var NavigationBindings = /** @class */ (function () {
  119. /* *
  120. *
  121. * Constructors
  122. *
  123. * */
  124. function NavigationBindings(chart, options) {
  125. this.boundClassNames = void 0;
  126. this.selectedButton = void 0;
  127. this.chart = chart;
  128. this.options = options;
  129. this.eventsToUnbind = [];
  130. this.container = doc.getElementsByClassName(this.options.bindingsClassName || '');
  131. }
  132. // Private properties added by bindings:
  133. // Active (selected) annotation that is editted through popup/forms
  134. // activeAnnotation: Annotation
  135. // Holder for current step, used on mouse move to update bound object
  136. // mouseMoveEvent: function () {}
  137. // Next event in `step` array to be called on chart's click
  138. // nextEvent: function () {}
  139. // Index in the `step` array of the current event
  140. // stepIndex: 0
  141. // Flag to determine if current binding has steps
  142. // steps: true|false
  143. // Bindings holder for all events
  144. // selectedButton: {}
  145. // Holder for user options, returned from `start` event, and passed on to
  146. // `step`'s' and `end`.
  147. // currentUserDetails: {}
  148. /* *
  149. *
  150. * Functions
  151. *
  152. * */
  153. /**
  154. * Initi all events conencted to NavigationBindings.
  155. *
  156. * @private
  157. * @function Highcharts.NavigationBindings#initEvents
  158. */
  159. NavigationBindings.prototype.initEvents = function () {
  160. var navigation = this, chart = navigation.chart, bindingsContainer = navigation.container, options = navigation.options;
  161. // Shorthand object for getting events for buttons:
  162. navigation.boundClassNames = {};
  163. objectEach((options.bindings || {}), function (value) {
  164. navigation.boundClassNames[value.className] = value;
  165. });
  166. // Handle multiple containers with the same class names:
  167. [].forEach.call(bindingsContainer, function (subContainer) {
  168. navigation.eventsToUnbind.push(addEvent(subContainer, 'click', function (event) {
  169. var bindings = navigation.getButtonEvents(subContainer, event);
  170. if (bindings) {
  171. navigation.bindingsButtonClick(bindings.button, bindings.events, event);
  172. }
  173. }));
  174. });
  175. objectEach(options.events || {}, function (callback, eventName) {
  176. if (isFunction(callback)) {
  177. navigation.eventsToUnbind.push(addEvent(navigation, eventName, callback, { passive: false }));
  178. }
  179. });
  180. navigation.eventsToUnbind.push(addEvent(chart.container, 'click', function (e) {
  181. if (!chart.cancelClick &&
  182. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  183. navigation.bindingsChartClick(this, e);
  184. }
  185. }));
  186. navigation.eventsToUnbind.push(addEvent(chart.container, H.isTouchDevice ? 'touchmove' : 'mousemove', function (e) {
  187. navigation.bindingsContainerMouseMove(this, e);
  188. }, H.isTouchDevice ? { passive: false } : void 0));
  189. };
  190. /**
  191. * Common chart.update() delegation, shared between bindings and exporting.
  192. *
  193. * @private
  194. * @function Highcharts.NavigationBindings#initUpdate
  195. */
  196. NavigationBindings.prototype.initUpdate = function () {
  197. var navigation = this;
  198. chartNavigationMixin.addUpdate(function (options) {
  199. navigation.update(options);
  200. }, this.chart);
  201. };
  202. /**
  203. * Hook for click on a button, method selcts/unselects buttons,
  204. * then calls `bindings.init` callback.
  205. *
  206. * @private
  207. * @function Highcharts.NavigationBindings#bindingsButtonClick
  208. *
  209. * @param {Highcharts.HTMLDOMElement} [button]
  210. * Clicked button
  211. *
  212. * @param {object} events
  213. * Events passed down from bindings (`init`, `start`, `step`, `end`)
  214. *
  215. * @param {Highcharts.PointerEventObject} clickEvent
  216. * Browser's click event
  217. */
  218. NavigationBindings.prototype.bindingsButtonClick = function (button, events, clickEvent) {
  219. var navigation = this, chart = navigation.chart;
  220. if (navigation.selectedButtonElement) {
  221. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  222. if (navigation.nextEvent) {
  223. // Remove in-progress annotations adders:
  224. if (navigation.currentUserDetails &&
  225. navigation.currentUserDetails.coll === 'annotations') {
  226. chart.removeAnnotation(navigation.currentUserDetails);
  227. }
  228. navigation.mouseMoveEvent = navigation.nextEvent = false;
  229. }
  230. }
  231. navigation.selectedButton = events;
  232. navigation.selectedButtonElement = button;
  233. fireEvent(navigation, 'selectButton', { button: button });
  234. // Call "init" event, for example to open modal window
  235. if (events.init) {
  236. events.init.call(navigation, button, clickEvent);
  237. }
  238. if (events.start || events.steps) {
  239. chart.renderer.boxWrapper.addClass(PREFIX + 'draw-mode');
  240. }
  241. };
  242. /**
  243. * Hook for click on a chart, first click on a chart calls `start` event,
  244. * then on all subsequent clicks iterate over `steps` array.
  245. * When finished, calls `end` event.
  246. *
  247. * @private
  248. * @function Highcharts.NavigationBindings#bindingsChartClick
  249. *
  250. * @param {Highcharts.Chart} chart
  251. * Chart that click was performed on.
  252. *
  253. * @param {Highcharts.PointerEventObject} clickEvent
  254. * Browser's click event.
  255. */
  256. NavigationBindings.prototype.bindingsChartClick = function (chart, clickEvent) {
  257. var navigation = this, chart = navigation.chart, selectedButton = navigation.selectedButton, svgContainer = chart.renderer.boxWrapper;
  258. // Click outside popups, should close them and deselect the annotation
  259. if (navigation.activeAnnotation &&
  260. !clickEvent.activeAnnotation &&
  261. // Element could be removed in the child action, e.g. button
  262. clickEvent.target.parentNode &&
  263. // TO DO: Polyfill for IE11?
  264. !closestPolyfill(clickEvent.target, '.' + PREFIX + 'popup')) {
  265. fireEvent(navigation, 'closePopup');
  266. }
  267. if (!selectedButton || !selectedButton.start) {
  268. return;
  269. }
  270. if (!navigation.nextEvent) {
  271. // Call init method:
  272. navigation.currentUserDetails = selectedButton.start.call(navigation, clickEvent);
  273. // If steps exists (e.g. Annotations), bind them:
  274. if (selectedButton.steps) {
  275. navigation.stepIndex = 0;
  276. navigation.steps = true;
  277. navigation.mouseMoveEvent = navigation.nextEvent =
  278. selectedButton.steps[navigation.stepIndex];
  279. }
  280. else {
  281. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  282. svgContainer.removeClass(PREFIX + 'draw-mode');
  283. navigation.steps = false;
  284. navigation.selectedButton = null;
  285. // First click is also the last one:
  286. if (selectedButton.end) {
  287. selectedButton.end.call(navigation, clickEvent, navigation.currentUserDetails);
  288. }
  289. }
  290. }
  291. else {
  292. navigation.nextEvent(clickEvent, navigation.currentUserDetails);
  293. if (navigation.steps) {
  294. navigation.stepIndex++;
  295. if (selectedButton.steps[navigation.stepIndex]) {
  296. // If we have more steps, bind them one by one:
  297. navigation.mouseMoveEvent = navigation.nextEvent =
  298. selectedButton.steps[navigation.stepIndex];
  299. }
  300. else {
  301. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  302. svgContainer.removeClass(PREFIX + 'draw-mode');
  303. // That was the last step, call end():
  304. if (selectedButton.end) {
  305. selectedButton.end.call(navigation, clickEvent, navigation.currentUserDetails);
  306. }
  307. navigation.nextEvent = false;
  308. navigation.mouseMoveEvent = false;
  309. navigation.selectedButton = null;
  310. }
  311. }
  312. }
  313. };
  314. /**
  315. * Hook for mouse move on a chart's container. It calls current step.
  316. *
  317. * @private
  318. * @function Highcharts.NavigationBindings#bindingsContainerMouseMove
  319. *
  320. * @param {Highcharts.HTMLDOMElement} container
  321. * Chart's container.
  322. *
  323. * @param {global.Event} moveEvent
  324. * Browser's move event.
  325. */
  326. NavigationBindings.prototype.bindingsContainerMouseMove = function (_container, moveEvent) {
  327. if (this.mouseMoveEvent) {
  328. this.mouseMoveEvent(moveEvent, this.currentUserDetails);
  329. }
  330. };
  331. /**
  332. * Translate fields (e.g. `params.period` or `marker.styles.color`) to
  333. * Highcharts options object (e.g. `{ params: { period } }`).
  334. *
  335. * @private
  336. * @function Highcharts.NavigationBindings#fieldsToOptions<T>
  337. *
  338. * @param {Highcharts.Dictionary<string>} fields
  339. * Fields from popup form.
  340. *
  341. * @param {T} config
  342. * Default config to be modified.
  343. *
  344. * @return {T}
  345. * Modified config
  346. */
  347. NavigationBindings.prototype.fieldsToOptions = function (fields, config) {
  348. objectEach(fields, function (value, field) {
  349. var parsedValue = parseFloat(value), path = field.split('.'), parent = config, pathLength = path.length - 1;
  350. // If it's a number (not "format" options), parse it:
  351. if (isNumber(parsedValue) &&
  352. !value.match(/px/g) &&
  353. !field.match(/format/g)) {
  354. value = parsedValue;
  355. }
  356. // Remove empty strings or values like 0
  357. if (value !== '' && value !== 'undefined') {
  358. path.forEach(function (name, index) {
  359. var nextName = pick(path[index + 1], '');
  360. if (pathLength === index) {
  361. // Last index, put value:
  362. parent[name] = value;
  363. }
  364. else if (!parent[name]) {
  365. // Create middle property:
  366. parent[name] = nextName.match(/\d/g) ? [] : {};
  367. parent = parent[name];
  368. }
  369. else {
  370. // Jump into next property
  371. parent = parent[name];
  372. }
  373. });
  374. }
  375. });
  376. return config;
  377. };
  378. /**
  379. * Shorthand method to deselect an annotation.
  380. *
  381. * @function Highcharts.NavigationBindings#deselectAnnotation
  382. */
  383. NavigationBindings.prototype.deselectAnnotation = function () {
  384. if (this.activeAnnotation) {
  385. this.activeAnnotation.setControlPointsVisibility(false);
  386. this.activeAnnotation = false;
  387. }
  388. };
  389. /**
  390. * Generates API config for popup in the same format as options for
  391. * Annotation object.
  392. *
  393. * @function Highcharts.NavigationBindings#annotationToFields
  394. *
  395. * @param {Highcharts.Annotation} annotation
  396. * Annotations object
  397. *
  398. * @return {Highcharts.Dictionary<string>}
  399. * Annotation options to be displayed in popup box
  400. */
  401. NavigationBindings.prototype.annotationToFields = function (annotation) {
  402. var options = annotation.options, editables = NavigationBindings.annotationsEditable, nestedEditables = editables.nestedOptions, getFieldType = this.utils.getFieldType, type = pick(options.type, options.shapes && options.shapes[0] &&
  403. options.shapes[0].type, options.labels && options.labels[0] &&
  404. options.labels[0].itemType, 'label'), nonEditables = NavigationBindings.annotationsNonEditable[options.langKey] || [], visualOptions = {
  405. langKey: options.langKey,
  406. type: type
  407. };
  408. /**
  409. * Nested options traversing. Method goes down to the options and copies
  410. * allowed options (with values) to new object, which is last parameter:
  411. * "parent".
  412. *
  413. * @private
  414. *
  415. * @param {*} option
  416. * Atomic type or object/array
  417. *
  418. * @param {string} key
  419. * Option name, for example "visible" or "x", "y"
  420. *
  421. * @param {object} parentEditables
  422. * Editables from NavigationBindings.annotationsEditable
  423. *
  424. * @param {object} parent
  425. * Where new options will be assigned
  426. */
  427. function traverse(option, key, parentEditables, parent) {
  428. var nextParent;
  429. if (parentEditables &&
  430. option &&
  431. nonEditables.indexOf(key) === -1 &&
  432. ((parentEditables.indexOf &&
  433. parentEditables.indexOf(key)) >= 0 ||
  434. parentEditables[key] || // nested array
  435. parentEditables === true // simple array
  436. )) {
  437. // Roots:
  438. if (isArray(option)) {
  439. parent[key] = [];
  440. option.forEach(function (arrayOption, i) {
  441. if (!isObject(arrayOption)) {
  442. // Simple arrays, e.g. [String, Number, Boolean]
  443. traverse(arrayOption, 0, nestedEditables[key], parent[key]);
  444. }
  445. else {
  446. // Advanced arrays, e.g. [Object, Object]
  447. parent[key][i] = {};
  448. objectEach(arrayOption, function (nestedOption, nestedKey) {
  449. traverse(nestedOption, nestedKey, nestedEditables[key], parent[key][i]);
  450. });
  451. }
  452. });
  453. }
  454. else if (isObject(option)) {
  455. nextParent = {};
  456. if (isArray(parent)) {
  457. parent.push(nextParent);
  458. nextParent[key] = {};
  459. nextParent = nextParent[key];
  460. }
  461. else {
  462. parent[key] = nextParent;
  463. }
  464. objectEach(option, function (nestedOption, nestedKey) {
  465. traverse(nestedOption, nestedKey, key === 0 ? parentEditables : nestedEditables[key], nextParent);
  466. });
  467. }
  468. else {
  469. // Leaf:
  470. if (key === 'format') {
  471. parent[key] = [
  472. format(option, annotation.labels[0].points[0]).toString(),
  473. 'text'
  474. ];
  475. }
  476. else if (isArray(parent)) {
  477. parent.push([option, getFieldType(option)]);
  478. }
  479. else {
  480. parent[key] = [option, getFieldType(option)];
  481. }
  482. }
  483. }
  484. }
  485. objectEach(options, function (option, key) {
  486. if (key === 'typeOptions') {
  487. visualOptions[key] = {};
  488. objectEach(options[key], function (typeOption, typeKey) {
  489. traverse(typeOption, typeKey, nestedEditables, visualOptions[key], true);
  490. });
  491. }
  492. else {
  493. traverse(option, key, editables[type], visualOptions);
  494. }
  495. });
  496. return visualOptions;
  497. };
  498. /**
  499. * Get all class names for all parents in the element. Iterates until finds
  500. * main container.
  501. *
  502. * @function Highcharts.NavigationBindings#getClickedClassNames
  503. *
  504. * @param {Highcharts.HTMLDOMElement}
  505. * Container that event is bound to.
  506. *
  507. * @param {global.Event} event
  508. * Browser's event.
  509. *
  510. * @return {Array<Array<string, Highcharts.HTMLDOMElement>>}
  511. * Array of class names with corresponding elements
  512. */
  513. NavigationBindings.prototype.getClickedClassNames = function (container, event) {
  514. var element = event.target, classNames = [], elemClassName;
  515. while (element) {
  516. elemClassName = attr(element, 'class');
  517. if (elemClassName) {
  518. classNames = classNames.concat(elemClassName
  519. .split(' ')
  520. .map(function (name) {
  521. return [
  522. name,
  523. element
  524. ];
  525. }));
  526. }
  527. element = element.parentNode;
  528. if (element === container) {
  529. return classNames;
  530. }
  531. }
  532. return classNames;
  533. };
  534. /**
  535. * Get events bound to a button. It's a custom event delegation to find all
  536. * events connected to the element.
  537. *
  538. * @private
  539. * @function Highcharts.NavigationBindings#getButtonEvents
  540. *
  541. * @param {Highcharts.HTMLDOMElement} container
  542. * Container that event is bound to.
  543. *
  544. * @param {global.Event} event
  545. * Browser's event.
  546. *
  547. * @return {object}
  548. * Object with events (init, start, steps, and end)
  549. */
  550. NavigationBindings.prototype.getButtonEvents = function (container, event) {
  551. var navigation = this, classNames = this.getClickedClassNames(container, event), bindings;
  552. classNames.forEach(function (className) {
  553. if (navigation.boundClassNames[className[0]] && !bindings) {
  554. bindings = {
  555. events: navigation.boundClassNames[className[0]],
  556. button: className[1]
  557. };
  558. }
  559. });
  560. return bindings;
  561. };
  562. /**
  563. * Bindings are just events, so the whole update process is simply
  564. * removing old events and adding new ones.
  565. *
  566. * @private
  567. * @function Highcharts.NavigationBindings#update
  568. */
  569. NavigationBindings.prototype.update = function (options) {
  570. this.options = merge(true, this.options, options);
  571. this.removeEvents();
  572. this.initEvents();
  573. };
  574. /**
  575. * Remove all events created in the navigation.
  576. *
  577. * @private
  578. * @function Highcharts.NavigationBindings#removeEvents
  579. */
  580. NavigationBindings.prototype.removeEvents = function () {
  581. this.eventsToUnbind.forEach(function (unbinder) {
  582. unbinder();
  583. });
  584. };
  585. NavigationBindings.prototype.destroy = function () {
  586. this.removeEvents();
  587. };
  588. /* *
  589. *
  590. * Static Properties
  591. *
  592. * */
  593. // Define which options from annotations should show up in edit box:
  594. NavigationBindings.annotationsEditable = {
  595. // `typeOptions` are always available
  596. // Nested and shared options:
  597. nestedOptions: {
  598. labelOptions: ['style', 'format', 'backgroundColor'],
  599. labels: ['style'],
  600. label: ['style'],
  601. style: ['fontSize', 'color'],
  602. background: ['fill', 'strokeWidth', 'stroke'],
  603. innerBackground: ['fill', 'strokeWidth', 'stroke'],
  604. outerBackground: ['fill', 'strokeWidth', 'stroke'],
  605. shapeOptions: ['fill', 'strokeWidth', 'stroke'],
  606. shapes: ['fill', 'strokeWidth', 'stroke'],
  607. line: ['strokeWidth', 'stroke'],
  608. backgroundColors: [true],
  609. connector: ['fill', 'strokeWidth', 'stroke'],
  610. crosshairX: ['strokeWidth', 'stroke'],
  611. crosshairY: ['strokeWidth', 'stroke']
  612. },
  613. // Simple shapes:
  614. circle: ['shapes'],
  615. verticalLine: [],
  616. label: ['labelOptions'],
  617. // Measure
  618. measure: ['background', 'crosshairY', 'crosshairX'],
  619. // Others:
  620. fibonacci: [],
  621. tunnel: ['background', 'line', 'height'],
  622. pitchfork: ['innerBackground', 'outerBackground'],
  623. rect: ['shapes'],
  624. // Crooked lines, elliots, arrows etc:
  625. crookedLine: [],
  626. basicAnnotation: ['shapes', 'labelOptions']
  627. };
  628. // Define non editable fields per annotation, for example Rectangle inherits
  629. // options from Measure, but crosshairs are not available
  630. NavigationBindings.annotationsNonEditable = {
  631. rectangle: ['crosshairX', 'crosshairY', 'label']
  632. };
  633. return NavigationBindings;
  634. }());
  635. /**
  636. * General utils for bindings
  637. *
  638. * @private
  639. * @name Highcharts.NavigationBindings.utils
  640. * @type {bindingsUtils}
  641. */
  642. NavigationBindings.prototype.utils = bindingsUtils;
  643. Chart.prototype.initNavigationBindings = function () {
  644. var chart = this, options = chart.options;
  645. if (options && options.navigation && options.navigation.bindings) {
  646. chart.navigationBindings = new NavigationBindings(chart, options.navigation);
  647. chart.navigationBindings.initEvents();
  648. chart.navigationBindings.initUpdate();
  649. }
  650. };
  651. addEvent(Chart, 'load', function () {
  652. this.initNavigationBindings();
  653. });
  654. addEvent(Chart, 'destroy', function () {
  655. if (this.navigationBindings) {
  656. this.navigationBindings.destroy();
  657. }
  658. });
  659. addEvent(NavigationBindings, 'deselectButton', function () {
  660. this.selectedButtonElement = null;
  661. });
  662. addEvent(Annotation, 'remove', function () {
  663. if (this.chart.navigationBindings) {
  664. this.chart.navigationBindings.deselectAnnotation();
  665. }
  666. });
  667. /**
  668. * Show edit-annotation form:
  669. * @private
  670. */
  671. function selectableAnnotation(annotationType) {
  672. var originalClick = annotationType.prototype.defaultOptions.events &&
  673. annotationType.prototype.defaultOptions.events.click;
  674. /**
  675. * @private
  676. */
  677. function selectAndshowPopup(event) {
  678. var annotation = this, navigation = annotation.chart.navigationBindings, prevAnnotation = navigation.activeAnnotation;
  679. if (originalClick) {
  680. originalClick.call(annotation, event);
  681. }
  682. if (prevAnnotation !== annotation) {
  683. // Select current:
  684. navigation.deselectAnnotation();
  685. navigation.activeAnnotation = annotation;
  686. annotation.setControlPointsVisibility(true);
  687. fireEvent(navigation, 'showPopup', {
  688. annotation: annotation,
  689. formType: 'annotation-toolbar',
  690. options: navigation.annotationToFields(annotation),
  691. onSubmit: function (data) {
  692. var config = {}, typeOptions;
  693. if (data.actionType === 'remove') {
  694. navigation.activeAnnotation = false;
  695. navigation.chart.removeAnnotation(annotation);
  696. }
  697. else {
  698. navigation.fieldsToOptions(data.fields, config);
  699. navigation.deselectAnnotation();
  700. typeOptions = config.typeOptions;
  701. if (annotation.options.type === 'measure') {
  702. // Manually disable crooshars according to
  703. // stroke width of the shape:
  704. typeOptions.crosshairY.enabled =
  705. typeOptions.crosshairY.strokeWidth !== 0;
  706. typeOptions.crosshairX.enabled =
  707. typeOptions.crosshairX.strokeWidth !== 0;
  708. }
  709. annotation.update(config);
  710. }
  711. }
  712. });
  713. }
  714. else {
  715. // Deselect current:
  716. fireEvent(navigation, 'closePopup');
  717. }
  718. // Let bubble event to chart.click:
  719. event.activeAnnotation = true;
  720. }
  721. merge(true, annotationType.prototype.defaultOptions.events, {
  722. click: selectAndshowPopup
  723. });
  724. }
  725. if (H.Annotation) {
  726. // Basic shapes:
  727. selectableAnnotation(Annotation);
  728. // Advanced annotations:
  729. objectEach(Annotation.types, function (annotationType) {
  730. selectableAnnotation(annotationType);
  731. });
  732. }
  733. setOptions({
  734. /**
  735. * @optionparent lang
  736. *
  737. * @private
  738. */
  739. lang: {
  740. /**
  741. * Configure the Popup strings in the chart. Requires the
  742. * `annotations.js` or `annotations-advanced.src.js` module to be
  743. * loaded.
  744. *
  745. * @since 7.0.0
  746. * @product highcharts highstock
  747. */
  748. navigation: {
  749. /**
  750. * Translations for all field names used in popup.
  751. *
  752. * @product highcharts highstock
  753. */
  754. popup: {
  755. simpleShapes: 'Simple shapes',
  756. lines: 'Lines',
  757. circle: 'Circle',
  758. rectangle: 'Rectangle',
  759. label: 'Label',
  760. shapeOptions: 'Shape options',
  761. typeOptions: 'Details',
  762. fill: 'Fill',
  763. format: 'Text',
  764. strokeWidth: 'Line width',
  765. stroke: 'Line color',
  766. title: 'Title',
  767. name: 'Name',
  768. labelOptions: 'Label options',
  769. labels: 'Labels',
  770. backgroundColor: 'Background color',
  771. backgroundColors: 'Background colors',
  772. borderColor: 'Border color',
  773. borderRadius: 'Border radius',
  774. borderWidth: 'Border width',
  775. style: 'Style',
  776. padding: 'Padding',
  777. fontSize: 'Font size',
  778. color: 'Color',
  779. height: 'Height',
  780. shapes: 'Shape options'
  781. }
  782. }
  783. },
  784. /**
  785. * @optionparent navigation
  786. * @product highcharts highstock
  787. *
  788. * @private
  789. */
  790. navigation: {
  791. /**
  792. * A CSS class name where all bindings will be attached to. Multiple
  793. * charts on the same page should have separate class names to prevent
  794. * duplicating events.
  795. *
  796. * Default value of versions < 7.0.4 `highcharts-bindings-wrapper`
  797. *
  798. * @since 7.0.0
  799. * @type {string}
  800. */
  801. bindingsClassName: 'highcharts-bindings-container',
  802. /**
  803. * Bindings definitions for custom HTML buttons. Each binding implements
  804. * simple event-driven interface:
  805. *
  806. * - `className`: classname used to bind event to
  807. *
  808. * - `init`: initial event, fired on button click
  809. *
  810. * - `start`: fired on first click on a chart
  811. *
  812. * - `steps`: array of sequential events fired one after another on each
  813. * of users clicks
  814. *
  815. * - `end`: last event to be called after last step event
  816. *
  817. * @type {Highcharts.Dictionary<Highcharts.NavigationBindingsOptionsObject>|*}
  818. * @sample stock/stocktools/stocktools-thresholds
  819. * Custom bindings in Highstock
  820. * @since 7.0.0
  821. * @product highcharts highstock
  822. */
  823. bindings: {
  824. /**
  825. * A circle annotation bindings. Includes `start` and one event in
  826. * `steps` array.
  827. *
  828. * @type {Highcharts.NavigationBindingsOptionsObject}
  829. * @default {"className": "highcharts-circle-annotation", "start": function() {}, "steps": [function() {}], "annotationsOptions": {}}
  830. */
  831. circleAnnotation: {
  832. /** @ignore-option */
  833. className: 'highcharts-circle-annotation',
  834. /** @ignore-option */
  835. start: function (e) {
  836. var coords = this.chart.pointer.getCoordinates(e), navigation = this.chart.options.navigation;
  837. return this.chart.addAnnotation(merge({
  838. langKey: 'circle',
  839. type: 'basicAnnotation',
  840. shapes: [{
  841. type: 'circle',
  842. point: {
  843. xAxis: 0,
  844. yAxis: 0,
  845. x: coords.xAxis[0].value,
  846. y: coords.yAxis[0].value
  847. },
  848. r: 5
  849. }]
  850. }, navigation
  851. .annotationsOptions, navigation
  852. .bindings
  853. .circleAnnotation
  854. .annotationsOptions));
  855. },
  856. /** @ignore-option */
  857. steps: [
  858. function (e, annotation) {
  859. var point = annotation.options.shapes[0].point, x = this.chart.xAxis[0].toPixels(point.x), y = this.chart.yAxis[0].toPixels(point.y), inverted = this.chart.inverted, distance = Math.max(Math.sqrt(Math.pow(inverted ? y - e.chartX : x - e.chartX, 2) +
  860. Math.pow(inverted ? x - e.chartY : y - e.chartY, 2)), 5);
  861. annotation.update({
  862. shapes: [{
  863. r: distance
  864. }]
  865. });
  866. }
  867. ]
  868. },
  869. /**
  870. * A rectangle annotation bindings. Includes `start` and one event
  871. * in `steps` array.
  872. *
  873. * @type {Highcharts.NavigationBindingsOptionsObject}
  874. * @default {"className": "highcharts-rectangle-annotation", "start": function() {}, "steps": [function() {}], "annotationsOptions": {}}
  875. */
  876. rectangleAnnotation: {
  877. /** @ignore-option */
  878. className: 'highcharts-rectangle-annotation',
  879. /** @ignore-option */
  880. start: function (e) {
  881. var coords = this.chart.pointer.getCoordinates(e), navigation = this.chart.options.navigation, x = coords.xAxis[0].value, y = coords.yAxis[0].value;
  882. return this.chart.addAnnotation(merge({
  883. langKey: 'rectangle',
  884. type: 'basicAnnotation',
  885. shapes: [{
  886. type: 'path',
  887. points: [{
  888. xAxis: 0,
  889. yAxis: 0,
  890. x: x,
  891. y: y
  892. }, {
  893. xAxis: 0,
  894. yAxis: 0,
  895. x: x,
  896. y: y
  897. }, {
  898. xAxis: 0,
  899. yAxis: 0,
  900. x: x,
  901. y: y
  902. }, {
  903. xAxis: 0,
  904. yAxis: 0,
  905. x: x,
  906. y: y
  907. }]
  908. }]
  909. }, navigation
  910. .annotationsOptions, navigation
  911. .bindings
  912. .rectangleAnnotation
  913. .annotationsOptions));
  914. },
  915. /** @ignore-option */
  916. steps: [
  917. function (e, annotation) {
  918. var points = annotation.options.shapes[0].points, coords = this.chart.pointer.getCoordinates(e), x = coords.xAxis[0].value, y = coords.yAxis[0].value;
  919. // Top right point
  920. points[1].x = x;
  921. // Bottom right point (cursor position)
  922. points[2].x = x;
  923. points[2].y = y;
  924. // Bottom left
  925. points[3].y = y;
  926. annotation.update({
  927. shapes: [{
  928. points: points
  929. }]
  930. });
  931. }
  932. ]
  933. },
  934. /**
  935. * A label annotation bindings. Includes `start` event only.
  936. *
  937. * @type {Highcharts.NavigationBindingsOptionsObject}
  938. * @default {"className": "highcharts-label-annotation", "start": function() {}, "steps": [function() {}], "annotationsOptions": {}}
  939. */
  940. labelAnnotation: {
  941. /** @ignore-option */
  942. className: 'highcharts-label-annotation',
  943. /** @ignore-option */
  944. start: function (e) {
  945. var coords = this.chart.pointer.getCoordinates(e), navigation = this.chart.options.navigation;
  946. return this.chart.addAnnotation(merge({
  947. langKey: 'label',
  948. type: 'basicAnnotation',
  949. labelOptions: {
  950. format: '{y:.2f}'
  951. },
  952. labels: [{
  953. point: {
  954. xAxis: 0,
  955. yAxis: 0,
  956. x: coords.xAxis[0].value,
  957. y: coords.yAxis[0].value
  958. },
  959. overflow: 'none',
  960. crop: true
  961. }]
  962. }, navigation
  963. .annotationsOptions, navigation
  964. .bindings
  965. .labelAnnotation
  966. .annotationsOptions));
  967. }
  968. }
  969. },
  970. /**
  971. * Path where Highcharts will look for icons. Change this to use icons
  972. * from a different server.
  973. *
  974. * @type {string}
  975. * @default https://code.highcharts.com/9.0.1/gfx/stock-icons/
  976. * @since 7.1.3
  977. * @apioption navigation.iconsURL
  978. */
  979. /**
  980. * A `showPopup` event. Fired when selecting for example an annotation.
  981. *
  982. * @type {Function}
  983. * @apioption navigation.events.showPopup
  984. */
  985. /**
  986. * A `closePopup` event. Fired when Popup should be hidden, for example
  987. * when clicking on an annotation again.
  988. *
  989. * @type {Function}
  990. * @apioption navigation.events.closePopup
  991. */
  992. /**
  993. * Event fired on a button click.
  994. *
  995. * @type {Function}
  996. * @sample highcharts/annotations/gui/
  997. * Change icon in a dropddown on event
  998. * @sample highcharts/annotations/gui-buttons/
  999. * Change button class on event
  1000. * @apioption navigation.events.selectButton
  1001. */
  1002. /**
  1003. * Event fired when button state should change, for example after
  1004. * adding an annotation.
  1005. *
  1006. * @type {Function}
  1007. * @sample highcharts/annotations/gui/
  1008. * Change icon in a dropddown on event
  1009. * @sample highcharts/annotations/gui-buttons/
  1010. * Change button class on event
  1011. * @apioption navigation.events.deselectButton
  1012. */
  1013. /**
  1014. * Events to communicate between Stock Tools and custom GUI.
  1015. *
  1016. * @since 7.0.0
  1017. * @product highcharts highstock
  1018. * @optionparent navigation.events
  1019. */
  1020. events: {},
  1021. /**
  1022. * Additional options to be merged into all annotations.
  1023. *
  1024. * @sample stock/stocktools/navigation-annotation-options
  1025. * Set red color of all line annotations
  1026. *
  1027. * @type {Highcharts.AnnotationsOptions}
  1028. * @extends annotations
  1029. * @exclude crookedLine, elliottWave, fibonacci, infinityLine,
  1030. * measure, pitchfork, tunnel, verticalLine, basicAnnotation
  1031. * @apioption navigation.annotationsOptions
  1032. */
  1033. annotationsOptions: {
  1034. animation: {
  1035. defer: 0
  1036. }
  1037. }
  1038. }
  1039. });
  1040. addEvent(NavigationBindings, 'closePopup', function () {
  1041. this.deselectAnnotation();
  1042. });
  1043. export default NavigationBindings;