Popup.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /* *
  2. *
  3. * Popup generator for Stock tools
  4. *
  5. * (c) 2009-2017 Sebastian Bochan
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. import H from '../../Core/Globals.js';
  13. import NavigationBindings from './NavigationBindings.js';
  14. import Pointer from '../../Core/Pointer.js';
  15. import U from '../../Core/Utilities.js';
  16. var addEvent = U.addEvent, createElement = U.createElement, defined = U.defined, getOptions = U.getOptions, isArray = U.isArray, isObject = U.isObject, isString = U.isString, objectEach = U.objectEach, pick = U.pick, wrap = U.wrap;
  17. var indexFilter = /\d/g, PREFIX = 'highcharts-', DIV = 'div', INPUT = 'input', LABEL = 'label', BUTTON = 'button', SELECT = 'select', OPTION = 'option', SPAN = 'span', UL = 'ul', LI = 'li', H3 = 'h3';
  18. /* eslint-disable no-invalid-this, valid-jsdoc */
  19. // onContainerMouseDown blocks internal popup events, due to e.preventDefault.
  20. // Related issue #4606
  21. wrap(Pointer.prototype, 'onContainerMouseDown', function (proceed, e) {
  22. var popupClass = e.target && e.target.className;
  23. // elements is not in popup
  24. if (!(isString(popupClass) &&
  25. popupClass.indexOf(PREFIX + 'popup-field') >= 0)) {
  26. proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  27. }
  28. });
  29. H.Popup = function (parentDiv, iconsURL) {
  30. this.init(parentDiv, iconsURL);
  31. };
  32. H.Popup.prototype = {
  33. /**
  34. * Initialize the popup. Create base div and add close button.
  35. * @private
  36. * @param {Highcharts.HTMLDOMElement} parentDiv
  37. * Container where popup should be placed
  38. * @param {string} iconsURL
  39. * Icon URL
  40. */
  41. init: function (parentDiv, iconsURL) {
  42. // create popup div
  43. this.container = createElement(DIV, {
  44. className: PREFIX + 'popup'
  45. }, null, parentDiv);
  46. this.lang = this.getLangpack();
  47. this.iconsURL = iconsURL;
  48. // add close button
  49. this.addCloseBtn();
  50. },
  51. /**
  52. * Create HTML element and attach click event (close popup).
  53. * @private
  54. */
  55. addCloseBtn: function () {
  56. var _self = this, closeBtn;
  57. // create close popup btn
  58. closeBtn = createElement(DIV, {
  59. className: PREFIX + 'popup-close'
  60. }, null, this.container);
  61. closeBtn.style['background-image'] = 'url(' +
  62. this.iconsURL + 'close.svg)';
  63. ['click', 'touchstart'].forEach(function (eventName) {
  64. addEvent(closeBtn, eventName, function () {
  65. _self.closePopup();
  66. });
  67. });
  68. },
  69. /**
  70. * Create two columns (divs) in HTML.
  71. * @private
  72. * @param {Highcharts.HTMLDOMElement} container
  73. * Container of columns
  74. * @return {Highcharts.Dictionary<Highcharts.HTMLDOMElement>}
  75. * Reference to two HTML columns (lhsCol, rhsCol)
  76. */
  77. addColsContainer: function (container) {
  78. var rhsCol, lhsCol;
  79. // left column
  80. lhsCol = createElement(DIV, {
  81. className: PREFIX + 'popup-lhs-col'
  82. }, null, container);
  83. // right column
  84. rhsCol = createElement(DIV, {
  85. className: PREFIX + 'popup-rhs-col'
  86. }, null, container);
  87. // wrapper content
  88. createElement(DIV, {
  89. className: PREFIX + 'popup-rhs-col-wrapper'
  90. }, null, rhsCol);
  91. return {
  92. lhsCol: lhsCol,
  93. rhsCol: rhsCol
  94. };
  95. },
  96. /**
  97. * Create input with label.
  98. * @private
  99. * @param {string} option
  100. * Chain of fields i.e params.styles.fontSize
  101. * @param {string} type
  102. * Indicator type
  103. * @param {Highhcharts.HTMLDOMElement}
  104. * Container where elements should be added
  105. * @param {string} value
  106. * Default value of input i.e period value is 14, extracted from
  107. * defaultOptions (ADD mode) or series options (EDIT mode)
  108. */
  109. addInput: function (option, type, parentDiv, value) {
  110. var optionParamList = option.split('.'), optionName = optionParamList[optionParamList.length - 1], lang = this.lang, inputName = PREFIX + type + '-' + optionName;
  111. if (!inputName.match(indexFilter)) {
  112. // add label
  113. createElement(LABEL, {
  114. innerHTML: lang[optionName] || optionName,
  115. htmlFor: inputName
  116. }, null, parentDiv);
  117. }
  118. // add input
  119. createElement(INPUT, {
  120. name: inputName,
  121. value: value[0],
  122. type: value[1],
  123. className: PREFIX + 'popup-field'
  124. }, null, parentDiv).setAttribute(PREFIX + 'data-name', option);
  125. },
  126. /**
  127. * Create button.
  128. * @private
  129. * @param {Highcharts.HTMLDOMElement} parentDiv
  130. * Container where elements should be added
  131. * @param {string} label
  132. * Text placed as button label
  133. * @param {string} type
  134. * add | edit | remove
  135. * @param {Function} callback
  136. * On click callback
  137. * @param {Highcharts.HTMLDOMElement} fieldsDiv
  138. * Container where inputs are generated
  139. * @return {Highcharts.HTMLDOMElement}
  140. * HTML button
  141. */
  142. addButton: function (parentDiv, label, type, callback, fieldsDiv) {
  143. var _self = this, closePopup = this.closePopup, getFields = this.getFields, button;
  144. button = createElement(BUTTON, {
  145. innerHTML: label
  146. }, null, parentDiv);
  147. ['click', 'touchstart'].forEach(function (eventName) {
  148. addEvent(button, eventName, function () {
  149. closePopup.call(_self);
  150. return callback(getFields(fieldsDiv, type));
  151. });
  152. });
  153. return button;
  154. },
  155. /**
  156. * Get values from all inputs and create JSON.
  157. * @private
  158. * @param {Highcharts.HTMLDOMElement} - container where inputs are created
  159. * @param {string} - add | edit | remove
  160. * @return {Highcharts.PopupFieldsObject} - fields
  161. */
  162. getFields: function (parentDiv, type) {
  163. var inputList = parentDiv.querySelectorAll('input'), optionSeries = '#' + PREFIX + 'select-series > option:checked', optionVolume = '#' + PREFIX + 'select-volume > option:checked', linkedTo = parentDiv.querySelectorAll(optionSeries)[0], volumeTo = parentDiv.querySelectorAll(optionVolume)[0], seriesId, param, fieldsOutput;
  164. fieldsOutput = {
  165. actionType: type,
  166. linkedTo: linkedTo && linkedTo.getAttribute('value'),
  167. fields: {}
  168. };
  169. [].forEach.call(inputList, function (input) {
  170. param = input.getAttribute(PREFIX + 'data-name');
  171. seriesId = input.getAttribute(PREFIX + 'data-series-id');
  172. // params
  173. if (seriesId) {
  174. fieldsOutput.seriesId = input.value;
  175. }
  176. else if (param) {
  177. fieldsOutput.fields[param] = input.value;
  178. }
  179. else {
  180. // type like sma / ema
  181. fieldsOutput.type = input.value;
  182. }
  183. });
  184. if (volumeTo) {
  185. fieldsOutput.fields['params.volumeSeriesID'] = volumeTo.getAttribute('value');
  186. }
  187. return fieldsOutput;
  188. },
  189. /**
  190. * Reset content of the current popup and show.
  191. * @private
  192. */
  193. showPopup: function () {
  194. var popupDiv = this.container, toolbarClass = PREFIX + 'annotation-toolbar', popupCloseBtn = popupDiv
  195. .querySelectorAll('.' + PREFIX + 'popup-close')[0];
  196. // reset content
  197. popupDiv.innerHTML = '';
  198. // reset toolbar styles if exists
  199. if (popupDiv.className.indexOf(toolbarClass) >= 0) {
  200. popupDiv.classList.remove(toolbarClass);
  201. // reset toolbar inline styles
  202. popupDiv.removeAttribute('style');
  203. }
  204. // add close button
  205. popupDiv.appendChild(popupCloseBtn);
  206. popupDiv.style.display = 'block';
  207. },
  208. /**
  209. * Hide popup.
  210. * @private
  211. */
  212. closePopup: function () {
  213. this.popup.container.style.display = 'none';
  214. },
  215. /**
  216. * Create content and show popup.
  217. * @private
  218. * @param {string} - type of popup i.e indicators
  219. * @param {Highcharts.Chart} - chart
  220. * @param {Highcharts.AnnotationsOptions} - options
  221. * @param {Function} - on click callback
  222. */
  223. showForm: function (type, chart, options, callback) {
  224. this.popup = chart.navigationBindings.popup;
  225. // show blank popup
  226. this.showPopup();
  227. // indicator form
  228. if (type === 'indicators') {
  229. this.indicators.addForm.call(this, chart, options, callback);
  230. }
  231. // annotation small toolbar
  232. if (type === 'annotation-toolbar') {
  233. this.annotations.addToolbar.call(this, chart, options, callback);
  234. }
  235. // annotation edit form
  236. if (type === 'annotation-edit') {
  237. this.annotations.addForm.call(this, chart, options, callback);
  238. }
  239. // flags form - add / edit
  240. if (type === 'flag') {
  241. this.annotations.addForm.call(this, chart, options, callback, true);
  242. }
  243. },
  244. /**
  245. * Return lang definitions for popup.
  246. * @private
  247. * @return {Highcharts.Dictionary<string>} - elements translations.
  248. */
  249. getLangpack: function () {
  250. return getOptions().lang.navigation.popup;
  251. },
  252. annotations: {
  253. /**
  254. * Create annotation simple form. It contains two buttons
  255. * (edit / remove) and text label.
  256. * @private
  257. * @param {Highcharts.Chart} - chart
  258. * @param {Highcharts.AnnotationsOptions} - options
  259. * @param {Function} - on click callback
  260. */
  261. addToolbar: function (chart, options, callback) {
  262. var _self = this, lang = this.lang, popupDiv = this.popup.container, showForm = this.showForm, toolbarClass = PREFIX + 'annotation-toolbar', button;
  263. // set small size
  264. if (popupDiv.className.indexOf(toolbarClass) === -1) {
  265. popupDiv.className += ' ' + toolbarClass;
  266. }
  267. // set position
  268. popupDiv.style.top = chart.plotTop + 10 + 'px';
  269. // create label
  270. createElement(SPAN, {
  271. innerHTML: pick(
  272. // Advanced annotations:
  273. lang[options.langKey] || options.langKey,
  274. // Basic shapes:
  275. options.shapes && options.shapes[0].type)
  276. }, null, popupDiv);
  277. // add buttons
  278. button = this.addButton(popupDiv, lang.removeButton || 'remove', 'remove', callback, popupDiv);
  279. button.className += ' ' + PREFIX + 'annotation-remove-button';
  280. button.style['background-image'] = 'url(' +
  281. this.iconsURL + 'destroy.svg)';
  282. button = this.addButton(popupDiv, lang.editButton || 'edit', 'edit', function () {
  283. showForm.call(_self, 'annotation-edit', chart, options, callback);
  284. }, popupDiv);
  285. button.className += ' ' + PREFIX + 'annotation-edit-button';
  286. button.style['background-image'] = 'url(' +
  287. this.iconsURL + 'edit.svg)';
  288. },
  289. /**
  290. * Create annotation simple form.
  291. * It contains fields with param names.
  292. * @private
  293. * @param {Highcharts.Chart} chart
  294. * Chart
  295. * @param {Object} options
  296. * Options
  297. * @param {Function} callback
  298. * On click callback
  299. * @param {boolean} [isInit]
  300. * If it is a form declared for init annotation
  301. */
  302. addForm: function (chart, options, callback, isInit) {
  303. var popupDiv = this.popup.container, lang = this.lang, bottomRow, lhsCol;
  304. // create title of annotations
  305. lhsCol = createElement('h2', {
  306. innerHTML: lang[options.langKey] || options.langKey,
  307. className: PREFIX + 'popup-main-title'
  308. }, null, popupDiv);
  309. // left column
  310. lhsCol = createElement(DIV, {
  311. className: PREFIX + 'popup-lhs-col ' + PREFIX + 'popup-lhs-full'
  312. }, null, popupDiv);
  313. bottomRow = createElement(DIV, {
  314. className: PREFIX + 'popup-bottom-row'
  315. }, null, popupDiv);
  316. this.annotations.addFormFields.call(this, lhsCol, chart, '', options, [], true);
  317. this.addButton(bottomRow, isInit ?
  318. (lang.addButton || 'add') :
  319. (lang.saveButton || 'save'), isInit ? 'add' : 'save', callback, popupDiv);
  320. },
  321. /**
  322. * Create annotation's form fields.
  323. * @private
  324. * @param {Highcharts.HTMLDOMElement} parentDiv
  325. * Div where inputs are placed
  326. * @param {Highcharts.Chart} chart
  327. * Chart
  328. * @param {string} parentNode
  329. * Name of parent to create chain of names
  330. * @param {Highcharts.AnnotationsOptions} options
  331. * Options
  332. * @param {Array<unknown>} storage
  333. * Array where all items are stored
  334. * @param {boolean} [isRoot]
  335. * Recursive flag for root
  336. */
  337. addFormFields: function (parentDiv, chart, parentNode, options, storage, isRoot) {
  338. var _self = this, addFormFields = this.annotations.addFormFields, addInput = this.addInput, lang = this.lang, parentFullName, titleName;
  339. objectEach(options, function (value, option) {
  340. // create name like params.styles.fontSize
  341. parentFullName = parentNode !== '' ?
  342. parentNode + '.' + option : option;
  343. if (isObject(value)) {
  344. if (
  345. // value is object of options
  346. !isArray(value) ||
  347. // array of objects with params. i.e labels in Fibonacci
  348. (isArray(value) && isObject(value[0]))) {
  349. titleName = lang[option] || option;
  350. if (!titleName.match(indexFilter)) {
  351. storage.push([
  352. true,
  353. titleName,
  354. parentDiv
  355. ]);
  356. }
  357. addFormFields.call(_self, parentDiv, chart, parentFullName, value, storage, false);
  358. }
  359. else {
  360. storage.push([
  361. _self,
  362. parentFullName,
  363. 'annotation',
  364. parentDiv,
  365. value
  366. ]);
  367. }
  368. }
  369. });
  370. if (isRoot) {
  371. storage = storage.sort(function (a) {
  372. return a[1].match(/format/g) ? -1 : 1;
  373. });
  374. storage.forEach(function (genInput) {
  375. if (genInput[0] === true) {
  376. createElement(SPAN, {
  377. className: PREFIX + 'annotation-title',
  378. innerHTML: genInput[1]
  379. }, null, genInput[2]);
  380. }
  381. else {
  382. addInput.apply(genInput[0], genInput.splice(1));
  383. }
  384. });
  385. }
  386. }
  387. },
  388. indicators: {
  389. /**
  390. * Create indicator's form. It contains two tabs (ADD and EDIT) with
  391. * content.
  392. * @private
  393. */
  394. addForm: function (chart, _options, callback) {
  395. var tabsContainers, indicators = this.indicators, lang = this.lang, buttonParentDiv;
  396. // add tabs
  397. this.tabs.init.call(this, chart);
  398. // get all tabs content divs
  399. tabsContainers = this.popup.container
  400. .querySelectorAll('.' + PREFIX + 'tab-item-content');
  401. // ADD tab
  402. this.addColsContainer(tabsContainers[0]);
  403. indicators.addIndicatorList.call(this, chart, tabsContainers[0], 'add');
  404. buttonParentDiv = tabsContainers[0]
  405. .querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0];
  406. this.addButton(buttonParentDiv, lang.addButton || 'add', 'add', callback, buttonParentDiv);
  407. // EDIT tab
  408. this.addColsContainer(tabsContainers[1]);
  409. indicators.addIndicatorList.call(this, chart, tabsContainers[1], 'edit');
  410. buttonParentDiv = tabsContainers[1]
  411. .querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0];
  412. this.addButton(buttonParentDiv, lang.saveButton || 'save', 'edit', callback, buttonParentDiv);
  413. this.addButton(buttonParentDiv, lang.removeButton || 'remove', 'remove', callback, buttonParentDiv);
  414. },
  415. /**
  416. * Create HTML list of all indicators (ADD mode) or added indicators
  417. * (EDIT mode).
  418. * @private
  419. */
  420. addIndicatorList: function (chart, parentDiv, listType) {
  421. var _self = this, lhsCol = parentDiv.querySelectorAll('.' + PREFIX + 'popup-lhs-col')[0], rhsCol = parentDiv.querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0], isEdit = listType === 'edit', series = (isEdit ?
  422. chart.series : // EDIT mode
  423. chart.options.plotOptions // ADD mode
  424. ), addFormFields = this.indicators.addFormFields, rhsColWrapper, indicatorList, item;
  425. // create wrapper for list
  426. indicatorList = createElement(UL, {
  427. className: PREFIX + 'indicator-list'
  428. }, null, lhsCol);
  429. rhsColWrapper = rhsCol
  430. .querySelectorAll('.' + PREFIX + 'popup-rhs-col-wrapper')[0];
  431. objectEach(series, function (serie, value) {
  432. var seriesOptions = serie.options;
  433. if (serie.params ||
  434. seriesOptions && seriesOptions.params) {
  435. var indicatorNameType = _self.indicators.getNameType(serie, value), indicatorType = indicatorNameType.type;
  436. item = createElement(LI, {
  437. className: PREFIX + 'indicator-list',
  438. innerHTML: indicatorNameType.name
  439. }, null, indicatorList);
  440. ['click', 'touchstart'].forEach(function (eventName) {
  441. addEvent(item, eventName, function () {
  442. addFormFields.call(_self, chart, isEdit ? serie : series[indicatorType], indicatorNameType.type, rhsColWrapper);
  443. // add hidden input with series.id
  444. if (isEdit && serie.options) {
  445. createElement(INPUT, {
  446. type: 'hidden',
  447. name: PREFIX + 'id-' + indicatorType,
  448. value: serie.options.id
  449. }, null, rhsColWrapper)
  450. .setAttribute(PREFIX + 'data-series-id', serie.options.id);
  451. }
  452. });
  453. });
  454. }
  455. });
  456. // select first item from the list
  457. if (indicatorList.childNodes.length > 0) {
  458. indicatorList.childNodes[0].click();
  459. }
  460. },
  461. /**
  462. * Extract full name and type of requested indicator.
  463. * @private
  464. * @param {Highcharts.Series} series
  465. * Series which name is needed. (EDIT mode - defaultOptions.series, ADD
  466. * mode - indicator series).
  467. * @param {string} - indicator type like: sma, ema, etc.
  468. * @return {Object} - series name and type like: sma, ema, etc.
  469. */
  470. getNameType: function (series, type) {
  471. var options = series.options, seriesTypes = H.seriesTypes,
  472. // add mode
  473. seriesName = seriesTypes[type] &&
  474. seriesTypes[type].prototype.nameBase || type.toUpperCase(), seriesType = type;
  475. // edit
  476. if (options && options.type) {
  477. seriesType = series.options.type;
  478. seriesName = series.name;
  479. }
  480. return {
  481. name: seriesName,
  482. type: seriesType
  483. };
  484. },
  485. /**
  486. * List all series with unique ID. Its mandatory for indicators to set
  487. * correct linking.
  488. * @private
  489. * @param {string} type
  490. * Indicator type like: sma, ema, etc.
  491. * @param {string} optionName
  492. * Type of select i.e series or volume.
  493. * @param {Highcharts.Chart} chart
  494. * Chart
  495. * @param {Highcharts.HTMLDOMElement} parentDiv
  496. * Element where created HTML list is added
  497. * @param {string} selectedOption
  498. * optional param for default value in dropdown
  499. */
  500. listAllSeries: function (type, optionName, chart, parentDiv, selectedOption) {
  501. var selectName = PREFIX + optionName + '-type-' + type, lang = this.lang, selectBox, seriesOptions;
  502. createElement(LABEL, {
  503. innerHTML: lang[optionName] || optionName,
  504. htmlFor: selectName
  505. }, null, parentDiv);
  506. // select type
  507. selectBox = createElement(SELECT, {
  508. name: selectName,
  509. className: PREFIX + 'popup-field'
  510. }, null, parentDiv);
  511. selectBox.setAttribute('id', PREFIX + 'select-' + optionName);
  512. // list all series which have id - mandatory for creating indicator
  513. chart.series.forEach(function (serie) {
  514. seriesOptions = serie.options;
  515. if (!seriesOptions.params &&
  516. seriesOptions.id &&
  517. seriesOptions.id !== PREFIX + 'navigator-series') {
  518. createElement(OPTION, {
  519. innerHTML: seriesOptions.name || seriesOptions.id,
  520. value: seriesOptions.id
  521. }, null, selectBox);
  522. }
  523. });
  524. if (defined(selectedOption)) {
  525. selectBox.value = selectedOption;
  526. }
  527. },
  528. /**
  529. * Create typical inputs for chosen indicator. Fields are extracted from
  530. * defaultOptions (ADD mode) or current indicator (ADD mode). Two extra
  531. * fields are added:
  532. * - hidden input - contains indicator type (required for callback)
  533. * - select - list of series which can be linked with indicator
  534. * @private
  535. * @param {Highcharts.Chart} chart
  536. * Chart
  537. * @param {Highcharts.Series} series
  538. * Indicator
  539. * @param {string} seriesType
  540. * Indicator type like: sma, ema, etc.
  541. * @param {Highcharts.HTMLDOMElement} rhsColWrapper
  542. * Element where created HTML list is added
  543. */
  544. addFormFields: function (chart, series, seriesType, rhsColWrapper) {
  545. var fields = series.params || series.options.params, getNameType = this.indicators.getNameType;
  546. // reset current content
  547. rhsColWrapper.innerHTML = '';
  548. // create title (indicator name in the right column)
  549. createElement(H3, {
  550. className: PREFIX + 'indicator-title',
  551. innerHTML: getNameType(series, seriesType).name
  552. }, null, rhsColWrapper);
  553. // input type
  554. createElement(INPUT, {
  555. type: 'hidden',
  556. name: PREFIX + 'type-' + seriesType,
  557. value: seriesType
  558. }, null, rhsColWrapper);
  559. // list all series with id
  560. this.indicators.listAllSeries.call(this, seriesType, 'series', chart, rhsColWrapper, series.linkedParent && fields.volumeSeriesID);
  561. if (fields.volumeSeriesID) {
  562. this.indicators.listAllSeries.call(this, seriesType, 'volume', chart, rhsColWrapper, series.linkedParent && series.linkedParent.options.id);
  563. }
  564. // add param fields
  565. this.indicators.addParamInputs.call(this, chart, 'params', fields, seriesType, rhsColWrapper);
  566. },
  567. /**
  568. * Recurent function which lists all fields, from params object and
  569. * create them as inputs. Each input has unique `data-name` attribute,
  570. * which keeps chain of fields i.e params.styles.fontSize.
  571. * @private
  572. * @param {Highcharts.Chart} chart
  573. * Chart
  574. * @param {string} parentNode
  575. * Name of parent to create chain of names
  576. * @param {Highcharts.PopupFieldsDictionary<string>} fields
  577. * Params which are based for input create
  578. * @param {string} type
  579. * Indicator type like: sma, ema, etc.
  580. * @param {Highcharts.HTMLDOMElement} parentDiv
  581. * Element where created HTML list is added
  582. */
  583. addParamInputs: function (chart, parentNode, fields, type, parentDiv) {
  584. var _self = this, addParamInputs = this.indicators.addParamInputs, addInput = this.addInput, parentFullName;
  585. objectEach(fields, function (value, fieldName) {
  586. // create name like params.styles.fontSize
  587. parentFullName = parentNode + '.' + fieldName;
  588. if (isObject(value)) {
  589. addParamInputs.call(_self, chart, parentFullName, value, type, parentDiv);
  590. }
  591. else if (
  592. // skip volume field which is created by addFormFields
  593. parentFullName !== 'params.volumeSeriesID') {
  594. addInput.call(_self, parentFullName, type, parentDiv, [value, 'text'] // all inputs are text type
  595. );
  596. }
  597. });
  598. },
  599. /**
  600. * Get amount of indicators added to chart.
  601. * @private
  602. * @return {number} - Amount of indicators
  603. */
  604. getAmount: function () {
  605. var series = this.series, counter = 0;
  606. series.forEach(function (serie) {
  607. var seriesOptions = serie.options;
  608. if (serie.params ||
  609. seriesOptions && seriesOptions.params) {
  610. counter++;
  611. }
  612. });
  613. return counter;
  614. }
  615. },
  616. tabs: {
  617. /**
  618. * Init tabs. Create tab menu items, tabs containers
  619. * @private
  620. * @param {Highcharts.Chart} chart
  621. * Reference to current chart
  622. */
  623. init: function (chart) {
  624. var tabs = this.tabs, indicatorsCount = this.indicators.getAmount.call(chart), firstTab; // run by default
  625. // create menu items
  626. firstTab = tabs.addMenuItem.call(this, 'add');
  627. tabs.addMenuItem.call(this, 'edit', indicatorsCount);
  628. // create tabs containers
  629. tabs.addContentItem.call(this, 'add');
  630. tabs.addContentItem.call(this, 'edit');
  631. tabs.switchTabs.call(this, indicatorsCount);
  632. // activate first tab
  633. tabs.selectTab.call(this, firstTab, 0);
  634. },
  635. /**
  636. * Create tab menu item
  637. * @private
  638. * @param {string} tabName
  639. * `add` or `edit`
  640. * @param {number} [disableTab]
  641. * Disable tab when 0
  642. * @return {Highcharts.HTMLDOMElement}
  643. * Created HTML tab-menu element
  644. */
  645. addMenuItem: function (tabName, disableTab) {
  646. var popupDiv = this.popup.container, className = PREFIX + 'tab-item', lang = this.lang, menuItem;
  647. if (disableTab === 0) {
  648. className += ' ' + PREFIX + 'tab-disabled';
  649. }
  650. // tab 1
  651. menuItem = createElement(SPAN, {
  652. innerHTML: lang[tabName + 'Button'] || tabName,
  653. className: className
  654. }, null, popupDiv);
  655. menuItem.setAttribute(PREFIX + 'data-tab-type', tabName);
  656. return menuItem;
  657. },
  658. /**
  659. * Create tab content
  660. * @private
  661. * @return {HTMLDOMElement} - created HTML tab-content element
  662. */
  663. addContentItem: function () {
  664. var popupDiv = this.popup.container;
  665. return createElement(DIV, {
  666. className: PREFIX + 'tab-item-content'
  667. }, null, popupDiv);
  668. },
  669. /**
  670. * Add click event to each tab
  671. * @private
  672. * @param {number} disableTab
  673. * Disable tab when 0
  674. */
  675. switchTabs: function (disableTab) {
  676. var _self = this, popupDiv = this.popup.container, tabs = popupDiv.querySelectorAll('.' + PREFIX + 'tab-item'), dataParam;
  677. tabs.forEach(function (tab, i) {
  678. dataParam = tab.getAttribute(PREFIX + 'data-tab-type');
  679. if (dataParam === 'edit' && disableTab === 0) {
  680. return;
  681. }
  682. ['click', 'touchstart'].forEach(function (eventName) {
  683. addEvent(tab, eventName, function () {
  684. // reset class on other elements
  685. _self.tabs.deselectAll.call(_self);
  686. _self.tabs.selectTab.call(_self, this, i);
  687. });
  688. });
  689. });
  690. },
  691. /**
  692. * Set tab as visible
  693. * @private
  694. * @param {globals.Element} - current tab
  695. * @param {number} - Index of tab in menu
  696. */
  697. selectTab: function (tab, index) {
  698. var allTabs = this.popup.container
  699. .querySelectorAll('.' + PREFIX + 'tab-item-content');
  700. tab.className += ' ' + PREFIX + 'tab-item-active';
  701. allTabs[index].className += ' ' + PREFIX + 'tab-item-show';
  702. },
  703. /**
  704. * Set all tabs as invisible.
  705. * @private
  706. */
  707. deselectAll: function () {
  708. var popupDiv = this.popup.container, tabs = popupDiv
  709. .querySelectorAll('.' + PREFIX + 'tab-item'), tabsContent = popupDiv
  710. .querySelectorAll('.' + PREFIX + 'tab-item-content'), i;
  711. for (i = 0; i < tabs.length; i++) {
  712. tabs[i].classList.remove(PREFIX + 'tab-item-active');
  713. tabsContent[i].classList.remove(PREFIX + 'tab-item-show');
  714. }
  715. }
  716. }
  717. };
  718. addEvent(NavigationBindings, 'showPopup', function (config) {
  719. if (!this.popup) {
  720. // Add popup to main container
  721. this.popup = new H.Popup(this.chart.container, (this.chart.options.navigation.iconsURL ||
  722. (this.chart.options.stockTools &&
  723. this.chart.options.stockTools.gui.iconsURL) ||
  724. 'https://code.highcharts.com/8.2.2/gfx/stock-icons/'));
  725. }
  726. this.popup.showForm(config.formType, this.chart, config.options, config.onSubmit);
  727. });
  728. addEvent(NavigationBindings, 'closePopup', function () {
  729. if (this.popup) {
  730. this.popup.closePopup();
  731. }
  732. });