popup.js
34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
/* *
*
* Popup generator for Stock tools
*
* (c) 2009-2017 Sebastian Bochan
*
* License: www.highcharts.com/license
*
* */
'use strict';
import H from '../parts/Globals.js';
import U from '../parts/Utilities.js';
var defined = U.defined,
isArray = U.isArray,
isObject = U.isObject,
isString = U.isString,
objectEach = U.objectEach;
var addEvent = H.addEvent,
createElement = H.createElement,
pick = H.pick,
wrap = H.wrap,
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';
// onContainerMouseDown blocks internal popup events, due to e.preventDefault.
// Related issue #4606
wrap(H.Pointer.prototype, 'onContainerMouseDown', function (proceed, e) {
var popupClass = e.target && e.target.className;
// elements is not in popup
if (!(isString(popupClass) &&
popupClass.indexOf(PREFIX + 'popup-field') >= 0)
) {
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
}
});
H.Popup = function (parentDiv, iconsURL) {
this.init(parentDiv, iconsURL);
};
H.Popup.prototype = {
/**
* Initialize the popup. Create base div and add close button.
* @private
* @param {HTMLDOMElement} - container where popup should be placed
* @param {Object} - user options
* @return {HTMLDOMElement} - return created popup's div
*/
init: function (parentDiv, iconsURL) {
// create popup div
this.container = createElement(DIV, {
className: PREFIX + 'popup'
}, null, parentDiv);
this.lang = this.getLangpack();
this.iconsURL = iconsURL;
// add close button
this.addCloseBtn();
},
/**
* Create HTML element and attach click event (close popup).
* @private
*/
addCloseBtn: function () {
var _self = this,
closeBtn;
// create close popup btn
closeBtn = createElement(DIV, {
className: PREFIX + 'popup-close'
}, null, this.container);
closeBtn.style['background-image'] = 'url(' +
this.iconsURL + 'close.svg)';
['click', 'touchstart'].forEach(function (eventName) {
addEvent(closeBtn, eventName, function () {
_self.closePopup();
});
});
},
/**
* Create two columns (divs) in HTML.
* @private
* @param {HTMLDOMElement} - container of columns
* @return {Object} - reference to two HTML columns
*/
addColsContainer: function (container) {
var rhsCol,
lhsCol;
// left column
lhsCol = createElement(DIV, {
className: PREFIX + 'popup-lhs-col'
}, null, container);
// right column
rhsCol = createElement(DIV, {
className: PREFIX + 'popup-rhs-col'
}, null, container);
// wrapper content
createElement(DIV, {
className: PREFIX + 'popup-rhs-col-wrapper'
}, null, rhsCol);
return {
lhsCol: lhsCol,
rhsCol: rhsCol
};
},
/**
* Create input with label.
* @private
* @param {String} - chain of fields i.e params.styles.fontSize
* @param {String} - indicator type
* @param {HTMLDOMElement} - container where elements should be added
* @param {String} - dafault value of input i.e period value is 14,
* extracted from defaultOptions (ADD mode) or series options (EDIT mode)
*/
addInput: function (option, type, parentDiv, value) {
var optionParamList = option.split('.'),
optionName = optionParamList[optionParamList.length - 1],
lang = this.lang,
inputName = PREFIX + type + '-' + optionName;
if (!inputName.match(indexFilter)) {
// add label
createElement(
LABEL, {
innerHTML: lang[optionName] || optionName,
htmlFor: inputName
},
null,
parentDiv
);
}
// add input
createElement(
INPUT,
{
name: inputName,
value: value[0],
type: value[1],
className: PREFIX + 'popup-field'
},
null,
parentDiv
).setAttribute(PREFIX + 'data-name', option);
},
/**
* Create button.
* @private
* @param {HTMLDOMElement} - container where elements should be added
* @param {String} - text placed as button label
* @param {String} - add | edit | remove
* @param {Function} - on click callback
* @param {HTMLDOMElement} - container where inputs are generated
* @return {HTMLDOMElement} - html button
*/
addButton: function (parentDiv, label, type, callback, fieldsDiv) {
var _self = this,
closePopup = this.closePopup,
getFields = this.getFields,
button;
button = createElement(BUTTON, {
innerHTML: label
}, null, parentDiv);
['click', 'touchstart'].forEach(function (eventName) {
addEvent(button, eventName, function () {
closePopup.call(_self);
return callback(
getFields(fieldsDiv, type)
);
});
});
return button;
},
/**
* Get values from all inputs and create JSON.
* @private
* @param {HTMLDOMElement} - container where inputs are created
* @param {String} - add | edit | remove
* @return {Object} - fields
*/
getFields: function (parentDiv, type) {
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;
fieldsOutput = {
actionType: type,
linkedTo: linkedTo && linkedTo.getAttribute('value'),
fields: { }
};
[].forEach.call(inputList, function (input) {
param = input.getAttribute(PREFIX + 'data-name');
seriesId = input.getAttribute(PREFIX + 'data-series-id');
// params
if (seriesId) {
fieldsOutput.seriesId = input.value;
} else if (param) {
fieldsOutput.fields[param] = input.value;
} else {
// type like sma / ema
fieldsOutput.type = input.value;
}
});
if (volumeTo) {
fieldsOutput.fields['params.volumeSeriesID'] = volumeTo
.getAttribute('value');
}
return fieldsOutput;
},
/**
* Reset content of the current popup and show.
* @private
* @param {Chart} - chart
* @param {Function} - on click callback
* @return {Object} - fields
*/
showPopup: function () {
var popupDiv = this.container,
toolbarClass = PREFIX + 'annotation-toolbar',
popupCloseBtn = popupDiv
.querySelectorAll('.' + PREFIX + 'popup-close')[0];
// reset content
popupDiv.innerHTML = '';
// reset toolbar styles if exists
if (popupDiv.className.indexOf(toolbarClass) >= 0) {
popupDiv.classList.remove(toolbarClass);
// reset toolbar inline styles
popupDiv.removeAttribute('style');
}
// add close button
popupDiv.appendChild(popupCloseBtn);
popupDiv.style.display = 'block';
},
/**
* Hide popup.
* @private
*/
closePopup: function () {
this.popup.container.style.display = 'none';
},
/**
* Create content and show popup.
* @private
* @param {String} - type of popup i.e indicators
* @param {Chart} - chart
* @param {Object} - options
* @param {Function} - on click callback
*/
showForm: function (type, chart, options, callback) {
this.popup = chart.navigationBindings.popup;
// show blank popup
this.showPopup();
// indicator form
if (type === 'indicators') {
this.indicators.addForm.call(this, chart, options, callback);
}
// annotation small toolbar
if (type === 'annotation-toolbar') {
this.annotations.addToolbar.call(this, chart, options, callback);
}
// annotation edit form
if (type === 'annotation-edit') {
this.annotations.addForm.call(this, chart, options, callback);
}
// flags form - add / edit
if (type === 'flag') {
this.annotations.addForm.call(this, chart, options, callback, true);
}
},
/**
* Return lang definitions for popup.
* @private
* @return {Object} - elements translations.
*/
getLangpack: function () {
return H.getOptions().lang.navigation.popup;
},
annotations: {
/**
* Create annotation simple form. It contains two buttons
* (edit / remove) and text label.
* @private
* @param {Chart} - chart
* @param {Object} - options
* @param {Function} - on click callback
*/
addToolbar: function (chart, options, callback) {
var _self = this,
lang = this.lang,
popupDiv = this.popup.container,
showForm = this.showForm,
toolbarClass = PREFIX + 'annotation-toolbar',
button;
// set small size
if (popupDiv.className.indexOf(toolbarClass) === -1) {
popupDiv.className += ' ' + toolbarClass;
}
// set position
popupDiv.style.top = chart.plotTop + 10 + 'px';
// create label
createElement(SPAN, {
innerHTML: pick(
// Advanced annotations:
lang[options.langKey] || options.langKey,
// Basic shapes:
options.shapes && options.shapes[0].type
)
}, null, popupDiv);
// add buttons
button = this.addButton(
popupDiv,
lang.removeButton || 'remove',
'remove',
callback,
popupDiv
);
button.className += ' ' + PREFIX + 'annotation-remove-button';
button.style['background-image'] = 'url(' +
this.iconsURL + 'destroy.svg)';
button = this.addButton(
popupDiv,
lang.editButton || 'edit',
'edit',
function () {
showForm.call(
_self,
'annotation-edit',
chart,
options,
callback
);
},
popupDiv
);
button.className += ' ' + PREFIX + 'annotation-edit-button';
button.style['background-image'] = 'url(' +
this.iconsURL + 'edit.svg)';
},
/**
* Create annotation simple form.
* It contains fields with param names.
* @private
* @param {Chart} - chart
* @param {Object} - options
* @param {Function} - on click callback
* @param {Boolean} - if it is a form declared for init annotation
*/
addForm: function (chart, options, callback, isInit) {
var popupDiv = this.popup.container,
lang = this.lang,
bottomRow,
lhsCol;
// create title of annotations
lhsCol = createElement('h2', {
innerHTML: lang[options.langKey] || options.langKey,
className: PREFIX + 'popup-main-title'
}, null, popupDiv);
// left column
lhsCol = createElement(DIV, {
className: PREFIX + 'popup-lhs-col ' + PREFIX + 'popup-lhs-full'
}, null, popupDiv);
bottomRow = createElement(DIV, {
className: PREFIX + 'popup-bottom-row'
}, null, popupDiv);
this.annotations.addFormFields.call(
this,
lhsCol,
chart,
'',
options,
[],
true
);
this.addButton(
bottomRow,
isInit ?
(lang.addButton || 'add') :
(lang.saveButton || 'save'),
isInit ? 'add' : 'save',
callback,
popupDiv
);
},
/**
* Create annotation's form fields.
* @private
* @param {HTMLDOMElement} - div where inputs are placed
* @param {Chart} - chart
* @param {String} - name of parent to create chain of names
* @param {Object} - options
* @param {Array} - storage - array where all items are stored
* @param {Boolean} - isRoot - recursive flag for root
*/
addFormFields: function (
parentDiv,
chart,
parentNode,
options,
storage,
isRoot
) {
var _self = this,
addFormFields = this.annotations.addFormFields,
addInput = this.addInput,
lang = this.lang,
parentFullName,
titleName;
objectEach(options, function (value, option) {
// create name like params.styles.fontSize
parentFullName = parentNode !== '' ?
parentNode + '.' + option : option;
if (isObject(value)) {
if (
// value is object of options
!isArray(value) ||
// array of objects with params. i.e labels in Fibonacci
(isArray(value) && isObject(value[0]))
) {
titleName = lang[option] || option;
if (!titleName.match(indexFilter)) {
storage.push([
true,
titleName,
parentDiv
]);
}
addFormFields.call(
_self,
parentDiv,
chart,
parentFullName,
value,
storage,
false
);
} else {
storage.push([
_self,
parentFullName,
'annotation',
parentDiv,
value
]);
}
}
});
if (isRoot) {
storage = storage.sort(function (a) {
return a[1].match(/format/g) ? -1 : 1;
});
storage.forEach(function (genInput) {
if (genInput[0] === true) {
createElement(SPAN, {
className: PREFIX + 'annotation-title',
innerHTML: genInput[1]
}, null, genInput[2]);
} else {
addInput.apply(genInput[0], genInput.splice(1));
}
});
}
}
},
indicators: {
/**
* Create indicator's form. It contains two tabs (ADD and EDIT) with
* content.
* @private
* @param {Chart} - chart
* @param {Object} - options
* @param {Function} - on click callback
*/
addForm: function (chart, options, callback) {
var tabsContainers,
indicators = this.indicators,
lang = this.lang,
buttonParentDiv;
// add tabs
this.tabs.init.call(this, chart);
// get all tabs content divs
tabsContainers = this.popup.container
.querySelectorAll('.' + PREFIX + 'tab-item-content');
// ADD tab
this.addColsContainer(tabsContainers[0]);
indicators.addIndicatorList.call(
this,
chart,
tabsContainers[0],
'add'
);
buttonParentDiv = tabsContainers[0]
.querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0];
this.addButton(
buttonParentDiv,
lang.addButton || 'add',
'add',
callback,
buttonParentDiv
);
// EDIT tab
this.addColsContainer(tabsContainers[1]);
indicators.addIndicatorList.call(
this,
chart,
tabsContainers[1],
'edit'
);
buttonParentDiv = tabsContainers[1]
.querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0];
this.addButton(
buttonParentDiv,
lang.saveButton || 'save',
'edit',
callback,
buttonParentDiv
);
this.addButton(
buttonParentDiv,
lang.removeButton || 'remove',
'remove',
callback,
buttonParentDiv
);
},
/**
* Create HTML list of all indicators (ADD mode) or added indicators
* (EDIT mode).
* @private
* @param {Chart} - chart
* @param {HTMLDOMElement} - container where list is added
* @param {String} - 'edit' or 'add' mode
*/
addIndicatorList: function (chart, parentDiv, listType) {
var _self = this,
lhsCol = parentDiv
.querySelectorAll('.' + PREFIX + 'popup-lhs-col')[0],
rhsCol = parentDiv
.querySelectorAll('.' + PREFIX + 'popup-rhs-col')[0],
isEdit = listType === 'edit',
series = isEdit ? chart.series : // EDIT mode
chart.options.plotOptions, // ADD mode
addFormFields = this.indicators.addFormFields,
rhsColWrapper,
indicatorList,
item;
// create wrapper for list
indicatorList = createElement(UL, {
className: PREFIX + 'indicator-list'
}, null, lhsCol);
rhsColWrapper = rhsCol
.querySelectorAll('.' + PREFIX + 'popup-rhs-col-wrapper')[0];
objectEach(series, function (serie, value) {
var seriesOptions = serie.options;
if (
serie.params ||
seriesOptions && seriesOptions.params
) {
var indicatorNameType = _self.indicators
.getNameType(serie, value),
indicatorType = indicatorNameType.type;
item = createElement(LI, {
className: PREFIX + 'indicator-list',
innerHTML: indicatorNameType.name
}, null, indicatorList);
['click', 'touchstart'].forEach(function (eventName) {
addEvent(item, eventName, function () {
addFormFields.call(
_self,
chart,
isEdit ? serie : series[indicatorType],
indicatorNameType.type,
rhsColWrapper
);
// add hidden input with series.id
if (isEdit && serie.options) {
createElement(INPUT, {
type: 'hidden',
name: PREFIX + 'id-' + indicatorType,
value: serie.options.id
}, null, rhsColWrapper)
.setAttribute(
PREFIX + 'data-series-id',
serie.options.id
);
}
});
});
}
});
// select first item from the list
if (indicatorList.childNodes.length > 0) {
indicatorList.childNodes[0].click();
}
},
/**
* Extract full name and type of requested indicator.
* @private
* @param {Series} - series which name is needed.
* (EDIT mode - defaultOptions.series, ADD mode - indicator series).
* @param {String} - indicator type like: sma, ema, etc.
* @return {Object} - series name and type like: sma, ema, etc.
*/
getNameType: function (series, type) {
var options = series.options,
seriesTypes = H.seriesTypes,
// add mode
seriesName = seriesTypes[type] &&
seriesTypes[type].prototype.nameBase || type.toUpperCase(),
seriesType = type;
// edit
if (options && options.type) {
seriesType = series.options.type;
seriesName = series.name;
}
return {
name: seriesName,
type: seriesType
};
},
/**
* List all series with unique ID. Its mandatory for indicators to set
* correct linking.
* @private
* @param {String} - indicator type like: sma, ema, etc.
* @param {String} - type of select i.e series or volume.
* @param {Chart} - chart
* @param {HTMLDOMElement} - element where created HTML list is added
* @param {String} selectedOption
* optional param for default value in dropdown
*/
listAllSeries: function (
type,
optionName,
chart,
parentDiv,
selectedOption
) {
var selectName = PREFIX + optionName + '-type-' + type,
lang = this.lang,
selectBox,
seriesOptions;
createElement(
LABEL, {
innerHTML: lang[optionName] || optionName,
htmlFor: selectName
},
null,
parentDiv
);
// select type
selectBox = createElement(
SELECT,
{
name: selectName,
className: PREFIX + 'popup-field'
},
null,
parentDiv
);
selectBox.setAttribute('id', PREFIX + 'select-' + optionName);
// list all series which have id - mandatory for creating indicator
chart.series.forEach(function (serie) {
seriesOptions = serie.options;
if (
!seriesOptions.params &&
seriesOptions.id &&
seriesOptions.id !== PREFIX + 'navigator-series'
) {
createElement(
OPTION,
{
innerHTML: seriesOptions.name || seriesOptions.id,
value: seriesOptions.id
},
null,
selectBox
);
}
});
if (defined(selectedOption)) {
selectBox.value = selectedOption;
}
},
/**
* Create typical inputs for chosen indicator. Fields are extracted from
* defaultOptions (ADD mode) or current indicator (ADD mode). Two extra
* fields are added:
* - hidden input - contains indicator type (required for callback)
* - select - list of series which can be linked with indicator
* @private
* @param {Chart} - chart
* @param {Series} - indicator
* @param {String} - indicator type like: sma, ema, etc.
* @param {HTMLDOMElement} - element where created HTML list is added
*/
addFormFields: function (chart, series, seriesType, rhsColWrapper) {
var fields = series.params || series.options.params,
getNameType = this.indicators.getNameType;
// reset current content
rhsColWrapper.innerHTML = '';
// create title (indicator name in the right column)
createElement(
H3,
{
className: PREFIX + 'indicator-title',
innerHTML: getNameType(series, seriesType).name
},
null,
rhsColWrapper
);
// input type
createElement(
INPUT,
{
type: 'hidden',
name: PREFIX + 'type-' + seriesType,
value: seriesType
},
null,
rhsColWrapper
);
// list all series with id
this.indicators.listAllSeries.call(
this,
seriesType,
'series',
chart,
rhsColWrapper,
series.linkedParent && fields.volumeSeriesID
);
if (fields.volumeSeriesID) {
this.indicators.listAllSeries.call(
this,
seriesType,
'volume',
chart,
rhsColWrapper,
series.linkedParent && series.linkedParent.options.id
);
}
// add param fields
this.indicators.addParamInputs.call(
this,
chart,
'params',
fields,
seriesType,
rhsColWrapper
);
},
/**
* Recurent function which lists all fields, from params object and
* create them as inputs. Each input has unique `data-name` attribute,
* which keeps chain of fields i.e params.styles.fontSize.
* @private
* @param {Chart} - chart
* @param {String} - name of parent to create chain of names
* @param {Series} - fields - params which are based for input create
* @param {String} - indicator type like: sma, ema, etc.
* @param {HTMLDOMElement} - element where created HTML list is added
*/
addParamInputs: function (chart, parentNode, fields, type, parentDiv) {
var _self = this,
addParamInputs = this.indicators.addParamInputs,
addInput = this.addInput,
parentFullName;
objectEach(fields, function (value, fieldName) {
// create name like params.styles.fontSize
parentFullName = parentNode + '.' + fieldName;
if (isObject(value)) {
addParamInputs.call(
_self,
chart,
parentFullName,
value,
type,
parentDiv
);
} else if (
// skip volume field which is created by addFormFields
parentFullName !== 'params.volumeSeriesID'
) {
addInput.call(
_self,
parentFullName,
type,
parentDiv,
[value, 'text'] // all inputs are text type
);
}
});
},
/**
* Get amount of indicators added to chart.
* @private
* @return {Number} - Amount of indicators
*/
getAmount: function () {
var series = this.series,
counter = 0;
objectEach(series, function (serie) {
var seriesOptions = serie.options;
if (
serie.params ||
seriesOptions && seriesOptions.params
) {
counter++;
}
});
return counter;
}
},
tabs: {
/**
* Init tabs. Create tab menu items, tabs containers
* @private
* @param {Chart} - reference to current chart
*/
init: function (chart) {
var tabs = this.tabs,
indicatorsCount = this.indicators.getAmount.call(chart),
firstTab; // run by default
// create menu items
firstTab = tabs.addMenuItem.call(this, 'add');
tabs.addMenuItem.call(this, 'edit', indicatorsCount);
// create tabs containers
tabs.addContentItem.call(this, 'add');
tabs.addContentItem.call(this, 'edit');
tabs.switchTabs.call(this, indicatorsCount);
// activate first tab
tabs.selectTab.call(this, firstTab, 0);
},
/**
* Create tab menu item
* @private
* @param {String} - `add` or `edit`
* @param {Number} - Disable tab when 0
* @return {HTMLDOMElement} - created HTML tab-menu element
*/
addMenuItem: function (tabName, disableTab) {
var popupDiv = this.popup.container,
className = PREFIX + 'tab-item',
lang = this.lang,
menuItem;
if (disableTab === 0) {
className += ' ' + PREFIX + 'tab-disabled';
}
// tab 1
menuItem = createElement(
SPAN,
{
innerHTML: lang[tabName + 'Button'] || tabName,
className: className
},
null,
popupDiv
);
menuItem.setAttribute(PREFIX + 'data-tab-type', tabName);
return menuItem;
},
/**
* Create tab content
* @private
* @return {HTMLDOMElement} - created HTML tab-content element
*/
addContentItem: function () {
var popupDiv = this.popup.container;
return createElement(
DIV,
{
className: PREFIX + 'tab-item-content'
},
null,
popupDiv
);
},
/**
* Add click event to each tab
* @private
* @param {Number} - Disable tab when 0
*/
switchTabs: function (disableTab) {
var _self = this,
popupDiv = this.popup.container,
tabs = popupDiv.querySelectorAll('.' + PREFIX + 'tab-item'),
dataParam;
tabs.forEach(function (tab, i) {
dataParam = tab.getAttribute(PREFIX + 'data-tab-type');
if (dataParam === 'edit' && disableTab === 0) {
return;
}
['click', 'touchstart'].forEach(function (eventName) {
addEvent(tab, eventName, function () {
// reset class on other elements
_self.tabs.deselectAll.call(_self);
_self.tabs.selectTab.call(_self, this, i);
});
});
});
},
/**
* Set tab as visible
* @private
* @param {HTMLDOMElement} - current tab
* @param {Number} - Index of tab in menu
*/
selectTab: function (tab, index) {
var allTabs = this.popup.container
.querySelectorAll('.' + PREFIX + 'tab-item-content');
tab.className += ' ' + PREFIX + 'tab-item-active';
allTabs[index].className += ' ' + PREFIX + 'tab-item-show';
},
/**
* Set all tabs as invisible.
* @private
*/
deselectAll: function () {
var popupDiv = this.popup.container,
tabs = popupDiv
.querySelectorAll('.' + PREFIX + 'tab-item'),
tabsContent = popupDiv
.querySelectorAll('.' + PREFIX + 'tab-item-content'),
i;
for (i = 0; i < tabs.length; i++) {
tabs[i].classList.remove(PREFIX + 'tab-item-active');
tabsContent[i].classList.remove(PREFIX + 'tab-item-show');
}
}
}
};
addEvent(H.NavigationBindings, 'showPopup', function (config) {
if (!this.popup) {
// Add popup to main container
this.popup = new H.Popup(
this.chart.container, (
this.chart.options.navigation.iconsURL ||
(
this.chart.options.stockTools &&
this.chart.options.stockTools.gui.iconsURL
) ||
'https://code.highcharts.com/7.2.0/gfx/stock-icons/'
)
);
}
this.popup.showForm(
config.formType,
this.chart,
config.options,
config.onSubmit
);
});
addEvent(H.NavigationBindings, 'closePopup', function () {
if (this.popup) {
this.popup.closePopup();
}
});