navigationBindings.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /* *
  2. *
  3. * (c) 2009-2017 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.src.js';
  12. import chartNavigationMixin from '../mixins/navigation.js';
  13. import H from '../parts/Globals.js';
  14. import U from '../parts/Utilities.js';
  15. 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;
  16. /**
  17. * A config object for navigation bindings in annotations.
  18. *
  19. * @interface Highcharts.NavigationBindingsOptionsObject
  20. */ /**
  21. * ClassName of the element for a binding.
  22. * @name Highcharts.NavigationBindingsOptionsObject#className
  23. * @type {string|undefined}
  24. */ /**
  25. * Last event to be fired after last step event.
  26. * @name Highcharts.NavigationBindingsOptionsObject#end
  27. * @type {Function|undefined}
  28. */ /**
  29. * Initial event, fired on a button click.
  30. * @name Highcharts.NavigationBindingsOptionsObject#init
  31. * @type {Function|undefined}
  32. */ /**
  33. * Event fired on first click on a chart.
  34. * @name Highcharts.NavigationBindingsOptionsObject#start
  35. * @type {Function|undefined}
  36. */ /**
  37. * Last event to be fired after last step event. Array of step events to be
  38. * called sequentially after each user click.
  39. * @name Highcharts.NavigationBindingsOptionsObject#steps
  40. * @type {Array<Function>|undefined}
  41. */
  42. var doc = H.doc, win = H.win, PREFIX = 'highcharts-';
  43. /* eslint-disable no-invalid-this, valid-jsdoc */
  44. /**
  45. * IE 9-11 polyfill for Element.closest():
  46. * @private
  47. */
  48. function closestPolyfill(el, s) {
  49. var ElementProto = win.Element.prototype, elementMatches = ElementProto.matches ||
  50. ElementProto.msMatchesSelector ||
  51. ElementProto.webkitMatchesSelector, ret = null;
  52. if (ElementProto.closest) {
  53. ret = ElementProto.closest.call(el, s);
  54. }
  55. else {
  56. do {
  57. if (elementMatches.call(el, s)) {
  58. return el;
  59. }
  60. el = el.parentElement || el.parentNode;
  61. } while (el !== null && el.nodeType === 1);
  62. }
  63. return ret;
  64. }
  65. /**
  66. * @private
  67. * @interface bindingsUtils
  68. */
  69. var bindingsUtils = {
  70. /**
  71. * Update size of background (rect) in some annotations: Measure, Simple
  72. * Rect.
  73. *
  74. * @private
  75. * @function Highcharts.NavigationBindingsUtilsObject.updateRectSize
  76. *
  77. * @param {Highcharts.PointerEventObject} event
  78. * Normalized browser event
  79. *
  80. * @param {Highcharts.Annotation} annotation
  81. * Annotation to be updated
  82. */
  83. updateRectSize: function (event, annotation) {
  84. 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;
  85. annotation.update({
  86. typeOptions: {
  87. background: {
  88. width: chart.inverted ? height : width,
  89. height: chart.inverted ? width : height
  90. }
  91. }
  92. });
  93. },
  94. /**
  95. * Get field type according to value
  96. *
  97. * @private
  98. * @function Highcharts.NavigationBindingsUtilsObject.getFieldType
  99. *
  100. * @param {'boolean'|'number'|'string'} value
  101. * Atomic type (one of: string, number, boolean)
  102. *
  103. * @return {'checkbox'|'number'|'text'}
  104. * Field type (one of: text, number, checkbox)
  105. */
  106. getFieldType: function (value) {
  107. return {
  108. 'string': 'text',
  109. 'number': 'number',
  110. 'boolean': 'checkbox'
  111. }[typeof value];
  112. }
  113. };
  114. /**
  115. * @private
  116. */
  117. var NavigationBindings = /** @class */ (function () {
  118. /* *
  119. *
  120. * Constructors
  121. *
  122. * */
  123. function NavigationBindings(chart, options) {
  124. this.boundClassNames = void 0;
  125. this.selectedButton = void 0;
  126. this.chart = chart;
  127. this.options = options;
  128. this.eventsToUnbind = [];
  129. this.container = doc.getElementsByClassName(this.options.bindingsClassName || '');
  130. }
  131. // Private properties added by bindings:
  132. // Active (selected) annotation that is editted through popup/forms
  133. // activeAnnotation: Annotation
  134. // Holder for current step, used on mouse move to update bound object
  135. // mouseMoveEvent: function () {}
  136. // Next event in `step` array to be called on chart's click
  137. // nextEvent: function () {}
  138. // Index in the `step` array of the current event
  139. // stepIndex: 0
  140. // Flag to determine if current binding has steps
  141. // steps: true|false
  142. // Bindings holder for all events
  143. // selectedButton: {}
  144. // Holder for user options, returned from `start` event, and passed on to
  145. // `step`'s' and `end`.
  146. // currentUserDetails: {}
  147. /* *
  148. *
  149. * Functions
  150. *
  151. * */
  152. /**
  153. * Initi all events conencted to NavigationBindings.
  154. *
  155. * @private
  156. * @function Highcharts.NavigationBindings#initEvents
  157. */
  158. NavigationBindings.prototype.initEvents = function () {
  159. var navigation = this, chart = navigation.chart, bindingsContainer = navigation.container, options = navigation.options;
  160. // Shorthand object for getting events for buttons:
  161. navigation.boundClassNames = {};
  162. objectEach((options.bindings || {}), function (value) {
  163. navigation.boundClassNames[value.className] = value;
  164. });
  165. // Handle multiple containers with the same class names:
  166. [].forEach.call(bindingsContainer, function (subContainer) {
  167. navigation.eventsToUnbind.push(addEvent(subContainer, 'click', function (event) {
  168. var bindings = navigation.getButtonEvents(subContainer, event);
  169. if (bindings) {
  170. navigation.bindingsButtonClick(bindings.button, bindings.events, event);
  171. }
  172. }));
  173. });
  174. objectEach(options.events || {}, function (callback, eventName) {
  175. if (isFunction(callback)) {
  176. navigation.eventsToUnbind.push(addEvent(navigation, eventName, callback));
  177. }
  178. });
  179. navigation.eventsToUnbind.push(addEvent(chart.container, 'click', function (e) {
  180. if (!chart.cancelClick &&
  181. chart.isInsidePlot(e.chartX - chart.plotLeft, e.chartY - chart.plotTop)) {
  182. navigation.bindingsChartClick(this, e);
  183. }
  184. }));
  185. navigation.eventsToUnbind.push(addEvent(chart.container, H.isTouchDevice ? 'touchmove' : 'mousemove', function (e) {
  186. navigation.bindingsContainerMouseMove(this, e);
  187. }));
  188. };
  189. /**
  190. * Common chart.update() delegation, shared between bindings and exporting.
  191. *
  192. * @private
  193. * @function Highcharts.NavigationBindings#initUpdate
  194. */
  195. NavigationBindings.prototype.initUpdate = function () {
  196. var navigation = this;
  197. chartNavigationMixin.addUpdate(function (options) {
  198. navigation.update(options);
  199. }, this.chart);
  200. };
  201. /**
  202. * Hook for click on a button, method selcts/unselects buttons,
  203. * then calls `bindings.init` callback.
  204. *
  205. * @private
  206. * @function Highcharts.NavigationBindings#bindingsButtonClick
  207. *
  208. * @param {Highcharts.HTMLDOMElement} [button]
  209. * Clicked button
  210. *
  211. * @param {object} events
  212. * Events passed down from bindings (`init`, `start`, `step`, `end`)
  213. *
  214. * @param {Highcharts.PointerEventObject} clickEvent
  215. * Browser's click event
  216. */
  217. NavigationBindings.prototype.bindingsButtonClick = function (button, events, clickEvent) {
  218. var navigation = this, chart = navigation.chart;
  219. if (navigation.selectedButtonElement) {
  220. fireEvent(navigation, 'deselectButton', { button: navigation.selectedButtonElement });
  221. if (navigation.nextEvent) {
  222. // Remove in-progress annotations adders:
  223. if (navigation.currentUserDetails &&
  224. navigation.currentUserDetails.coll === 'annotations') {
  225. chart.removeAnnotation(navigation.currentUserDetails);
  226. }
  227. navigation.mouseMoveEvent = navigation.nextEvent = false;
  228. }
  229. }
  230. navigation.selectedButton = events;
  231. navigation.selectedButtonElement = button;
  232. fireEvent(navigation, 'selectButton', { button: button });
  233. // Call "init" event, for example to open modal window
  234. if (events.init) {
  235. events.init.call(navigation, button, clickEvent);
  236. }
  237. if (events.start || events.steps) {
  238. chart.renderer.boxWrapper.addClass(PREFIX + 'draw-mode');
  239. }
  240. };
  241. /**
  242. * Hook for click on a chart, first click on a chart calls `start` event,
  243. * then on all subsequent clicks iterate over `steps` array.
  244. * When finished, calls `end` event.
  245. *
  246. * @private
  247. * @function Highcharts.NavigationBindings#bindingsChartClick
  248. *
  249. * @param {Highcharts.Chart} chart
  250. * Chart that click was performed on.
  251. *
  252. * @param {Highcharts.PointerEventObject} clickEvent
  253. * Browser's click event.
  254. */
  255. NavigationBindings.prototype.bindingsChartClick = function (chart, clickEvent) {
  256. var navigation = this, chart = navigation.chart, selectedButton = navigation.selectedButton, svgContainer = chart.renderer.boxWrapper;
  257. // Click outside popups, should close them and deselect the annotation
  258. if (navigation.activeAnnotation &&
  259. !clickEvent.activeAnnotation &&
  260. // Element could be removed in the child action, e.g. button
  261. clickEvent.target.parentNode &&
  262. // TO DO: Polyfill for IE11?
  263. !closestPolyfill(clickEvent.target, '.' + PREFIX + 'popup')) {
  264. fireEvent(navigation, 'closePopup');
  265. navigation.deselectAnnotation();
  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. nonEditables.indexOf(key) === -1 &&
  431. ((parentEditables.indexOf &&
  432. parentEditables.indexOf(key)) >= 0 ||
  433. parentEditables[key] || // nested array
  434. parentEditables === true // simple array
  435. )) {
  436. // Roots:
  437. if (isArray(option)) {
  438. parent[key] = [];
  439. option.forEach(function (arrayOption, i) {
  440. if (!isObject(arrayOption)) {
  441. // Simple arrays, e.g. [String, Number, Boolean]
  442. traverse(arrayOption, 0, nestedEditables[key], parent[key]);
  443. }
  444. else {
  445. // Advanced arrays, e.g. [Object, Object]
  446. parent[key][i] = {};
  447. objectEach(arrayOption, function (nestedOption, nestedKey) {
  448. traverse(nestedOption, nestedKey, nestedEditables[key], parent[key][i]);
  449. });
  450. }
  451. });
  452. }
  453. else if (isObject(option)) {
  454. nextParent = {};
  455. if (isArray(parent)) {
  456. parent.push(nextParent);
  457. nextParent[key] = {};
  458. nextParent = nextParent[key];
  459. }
  460. else {
  461. parent[key] = nextParent;
  462. }
  463. objectEach(option, function (nestedOption, nestedKey) {
  464. traverse(nestedOption, nestedKey, key === 0 ? parentEditables : nestedEditables[key], nextParent);
  465. });
  466. }
  467. else {
  468. // Leaf:
  469. if (key === 'format') {
  470. parent[key] = [
  471. format(option, annotation.labels[0].points[0]).toString(),
  472. 'text'
  473. ];
  474. }
  475. else if (isArray(parent)) {
  476. parent.push([option, getFieldType(option)]);
  477. }
  478. else {
  479. parent[key] = [option, getFieldType(option)];
  480. }
  481. }
  482. }
  483. }
  484. objectEach(options, function (option, key) {
  485. if (key === 'typeOptions') {
  486. visualOptions[key] = {};
  487. objectEach(options[key], function (typeOption, typeKey) {
  488. traverse(typeOption, typeKey, nestedEditables, visualOptions[key], true);
  489. });
  490. }
  491. else {
  492. traverse(option, key, editables[type], visualOptions);
  493. }
  494. });
  495. return visualOptions;
  496. };
  497. /**
  498. * Get all class names for all parents in the element. Iterates until finds
  499. * main container.
  500. *
  501. * @function Highcharts.NavigationBindings#getClickedClassNames
  502. *
  503. * @param {Highcharts.HTMLDOMElement}
  504. * Container that event is bound to.
  505. *
  506. * @param {global.Event} event
  507. * Browser's event.
  508. *
  509. * @return {Array<Array<string, Highcharts.HTMLDOMElement>>}
  510. * Array of class names with corresponding elements
  511. */
  512. NavigationBindings.prototype.getClickedClassNames = function (container, event) {
  513. var element = event.target, classNames = [], elemClassName;
  514. while (element) {
  515. elemClassName = attr(element, 'class');
  516. if (elemClassName) {
  517. classNames = classNames.concat(elemClassName
  518. .split(' ')
  519. .map(function (name) {
  520. return [
  521. name,
  522. element
  523. ];
  524. }));
  525. }
  526. element = element.parentNode;
  527. if (element === container) {
  528. return classNames;
  529. }
  530. }
  531. return classNames;
  532. };
  533. /**
  534. * Get events bound to a button. It's a custom event delegation to find all
  535. * events connected to the element.
  536. *
  537. * @private
  538. * @function Highcharts.NavigationBindings#getButtonEvents
  539. *
  540. * @param {Highcharts.HTMLDOMElement} container
  541. * Container that event is bound to.
  542. *
  543. * @param {global.Event} event
  544. * Browser's event.
  545. *
  546. * @return {object}
  547. * Object with events (init, start, steps, and end)
  548. */
  549. NavigationBindings.prototype.getButtonEvents = function (container, event) {
  550. var navigation = this, classNames = this.getClickedClassNames(container, event), bindings;
  551. classNames.forEach(function (className) {
  552. if (navigation.boundClassNames[className[0]] && !bindings) {
  553. bindings = {
  554. events: navigation.boundClassNames[className[0]],
  555. button: className[1]
  556. };
  557. }
  558. });
  559. return bindings;
  560. };
  561. /**
  562. * Bindings are just events, so the whole update process is simply
  563. * removing old events and adding new ones.
  564. *
  565. * @private
  566. * @function Highcharts.NavigationBindings#update
  567. */
  568. NavigationBindings.prototype.update = function (options) {
  569. this.options = merge(true, this.options, options);
  570. this.removeEvents();
  571. this.initEvents();
  572. };
  573. /**
  574. * Remove all events created in the navigation.
  575. *
  576. * @private
  577. * @function Highcharts.NavigationBindings#removeEvents
  578. */
  579. NavigationBindings.prototype.removeEvents = function () {
  580. this.eventsToUnbind.forEach(function (unbinder) {
  581. unbinder();
  582. });
  583. };
  584. NavigationBindings.prototype.destroy = function () {
  585. this.removeEvents();
  586. };
  587. /* *
  588. *
  589. * Static Properties
  590. *
  591. * */
  592. // Define which options from annotations should show up in edit box:
  593. NavigationBindings.annotationsEditable = {
  594. // `typeOptions` are always available
  595. // Nested and shared options:
  596. nestedOptions: {
  597. labelOptions: ['style', 'format', 'backgroundColor'],
  598. labels: ['style'],
  599. label: ['style'],
  600. style: ['fontSize', 'color'],
  601. background: ['fill', 'strokeWidth', 'stroke'],
  602. innerBackground: ['fill', 'strokeWidth', 'stroke'],
  603. outerBackground: ['fill', 'strokeWidth', 'stroke'],
  604. shapeOptions: ['fill', 'strokeWidth', 'stroke'],
  605. shapes: ['fill', 'strokeWidth', 'stroke'],
  606. line: ['strokeWidth', 'stroke'],
  607. backgroundColors: [true],
  608. connector: ['fill', 'strokeWidth', 'stroke'],
  609. crosshairX: ['strokeWidth', 'stroke'],
  610. crosshairY: ['strokeWidth', 'stroke']
  611. },
  612. // Simple shapes:
  613. circle: ['shapes'],
  614. verticalLine: [],
  615. label: ['labelOptions'],
  616. // Measure
  617. measure: ['background', 'crosshairY', 'crosshairX'],
  618. // Others:
  619. fibonacci: [],
  620. tunnel: ['background', 'line', 'height'],
  621. pitchfork: ['innerBackground', 'outerBackground'],
  622. rect: ['shapes'],
  623. // Crooked lines, elliots, arrows etc:
  624. crookedLine: [],
  625. basicAnnotation: []
  626. };
  627. // Define non editable fields per annotation, for example Rectangle inherits
  628. // options from Measure, but crosshairs are not available
  629. NavigationBindings.annotationsNonEditable = {
  630. rectangle: ['crosshairX', 'crosshairY', 'label']
  631. };
  632. return NavigationBindings;
  633. }());
  634. /**
  635. * General utils for bindings
  636. *
  637. * @private
  638. * @name Highcharts.NavigationBindings.utils
  639. * @type {bindingsUtils}
  640. */
  641. NavigationBindings.prototype.utils = bindingsUtils;
  642. H.Chart.prototype.initNavigationBindings = function () {
  643. var chart = this, options = chart.options;
  644. if (options && options.navigation && options.navigation.bindings) {
  645. chart.navigationBindings = new NavigationBindings(chart, options.navigation);
  646. chart.navigationBindings.initEvents();
  647. chart.navigationBindings.initUpdate();
  648. }
  649. };
  650. addEvent(H.Chart, 'load', function () {
  651. this.initNavigationBindings();
  652. });
  653. addEvent(H.Chart, 'destroy', function () {
  654. if (this.navigationBindings) {
  655. this.navigationBindings.destroy();
  656. }
  657. });
  658. addEvent(NavigationBindings, 'deselectButton', function () {
  659. this.selectedButtonElement = null;
  660. });
  661. addEvent(Annotation, 'remove', function () {
  662. if (this.chart.navigationBindings) {
  663. this.chart.navigationBindings.deselectAnnotation();
  664. }
  665. });
  666. /**
  667. * Show edit-annotation form:
  668. * @private
  669. */
  670. function selectableAnnotation(annotationType) {
  671. var originalClick = annotationType.prototype.defaultOptions.events &&
  672. annotationType.prototype.defaultOptions.events.click;
  673. /**
  674. * @private
  675. */
  676. function selectAndshowPopup(event) {
  677. var annotation = this, navigation = annotation.chart.navigationBindings, prevAnnotation = navigation.activeAnnotation;
  678. if (originalClick) {
  679. originalClick.call(annotation, event);
  680. }
  681. if (prevAnnotation !== annotation) {
  682. // Select current:
  683. navigation.deselectAnnotation();
  684. navigation.activeAnnotation = annotation;
  685. annotation.setControlPointsVisibility(true);
  686. fireEvent(navigation, 'showPopup', {
  687. annotation: annotation,
  688. formType: 'annotation-toolbar',
  689. options: navigation.annotationToFields(annotation),
  690. onSubmit: function (data) {
  691. var config = {}, typeOptions;
  692. if (data.actionType === 'remove') {
  693. navigation.activeAnnotation = false;
  694. navigation.chart.removeAnnotation(annotation);
  695. }
  696. else {
  697. navigation.fieldsToOptions(data.fields, config);
  698. navigation.deselectAnnotation();
  699. typeOptions = config.typeOptions;
  700. if (annotation.options.type === 'measure') {
  701. // Manually disable crooshars according to
  702. // stroke width of the shape:
  703. typeOptions.crosshairY.enabled =
  704. typeOptions.crosshairY.strokeWidth !== 0;
  705. typeOptions.crosshairX.enabled =
  706. typeOptions.crosshairX.strokeWidth !== 0;
  707. }
  708. annotation.update(config);
  709. }
  710. }
  711. });
  712. }
  713. else {
  714. // Deselect current:
  715. navigation.deselectAnnotation();
  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/8.1.2/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. }
  1035. });
  1036. export default NavigationBindings;