NavigationBindings.js 45 KB

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