Welcome to mirror list, hosted at ThFree Co, Russian Federation.

menupopup.js « js « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fd90b81f9a515588c64b3868adb05ba02b91220 (plain)
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
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


/**
 * Get menu popup history section data.
 *
 * @param string options['itemid']           Item ID.
 * @param bool   options['hasLatestGraphs']  Link to history page with showgraph action (optional).
 *
 * @return array
 */
function getMenuPopupHistory(options) {
	var items = [],
		url = new Curl('history.php', false);

	if (!options.allowed_ui_latest_data) {
		return [];
	}

	url.setArgument('itemids[]', options.itemid);

	// latest graphs
	if (typeof options.hasLatestGraphs !== 'undefined' && options.hasLatestGraphs) {
		url.setArgument('action', 'showgraph');
		url.setArgument('to', 'now');

		url.setArgument('from', 'now-1h');
		items.push({
			label: t('Last hour graph'),
			url: url.getUrl()
		});

		url.setArgument('from', 'now-7d');
		items.push({
			label: t('Last week graph'),
			url: url.getUrl()
		});

		url.setArgument('from', 'now-1M');
		items.push({
			label: t('Last month graph'),
			url: url.getUrl()
		});
	}

	// latest values
	url.setArgument('action', 'showvalues');
	url.setArgument('from', 'now-1h');
	items.push({
		label: t('Latest values'),
		url: url.getUrl()
	});

	return [{
		label: t('History'),
		items: items
	}];
}

/**
 * Get menu popup host section data.
 *
 * @param {string} options['hostid']                  Host ID.
 * @param {array}  options['scripts']                 Host scripts (optional).
 * @param {string} options[]['name']                  Script name.
 * @param {string} options[]['scriptid']              Script ID.
 * @param {string} options[]['confirmation']          Confirmation text.
 * @param {bool}   options['showGraphs']              Link to Monitoring->Hosts->Graphs page.
 * @param {bool}   options['showDashboards']          Link to Monitoring->Hosts->Dashboards page.
 * @param {bool}   options['showWeb']		          Link to Monitoring->Hosts->Web page.
 * @param {bool}   options['showTriggers']            Link to Monitoring->Problems page.
 * @param {bool}   options['hasGoTo']                 "Go to" block in popup.
 * @param {array}  options['severities']              (optional)
 * @param {bool}   options['show_suppressed']         (optional)
 * @param {array}  options['urls']                    (optional)
 * @param {string} options['url'][]['label']
 * @param {string} options['url'][]['url']
 * @param {array}  options['tags']                    (optional)
 * @param {string} options['tags'][]['tag']
 * @param {string} options['tags'][]['value']
 * @param {number} options['tags'][]['operator']
 * @param {number} options['evaltype']                (optional)
 * @param {bool}   options['allowed_ui_inventory']    Whether user has access to inventory hosts page.
 * @param {bool}   options['allowed_ui_latest_data']  Whether user has access to latest data page.
 * @param {bool}   options['allowed_ui_problems']     Whether user has access to problems page.
 * @param {bool}   options['allowed_ui_hosts']        Whether user has access to monitoring hosts pages.
 * @param {bool}   options['allowed_ui_conf_hosts']   Whether user has access to configuration hosts page.
 * @param {Node}   trigger_element                    UI element which triggered opening of overlay dialogue.
 *
 * @return array
 */
function getMenuPopupHost(options, trigger_element) {
	const sections = [];
	const items = [];
	const configuration = [];
	let url;

	// go to section
	if (options.hasGoTo) {
		// dashboard
		if (options.allowed_ui_hosts) {
			url = new Curl('zabbix.php', false);
			url.setArgument('action', 'host.dashboard.view')
			url.setArgument('hostid', options.hostid)

			items.push({
				label: t('Dashboards'),
				disabled: !options.showDashboards,
				url: url.getUrl()
			});
		}

		// problems
		if (options.allowed_ui_problems) {
			url = new Curl('zabbix.php', false);
			url.setArgument('action', 'problem.view');
			url.setArgument('filter_name', '');
			url.setArgument('hostids[]', options.hostid);

			if ('severities' in options) {
				url.setArgument('severities[]', options.severities);
			}

			if ('show_suppressed' in options) {
				url.setArgument('show_suppressed', '1');
			}

			if ('tags' in options) {
				url.setArgument('tags', options.tags);
				url.setArgument('evaltype', options.evaltype);
			}

			items.push({
				label: t('Problems'),
				disabled: !options.showTriggers,
				url: url.getUrl()
			});
		}

		// latest data
		if (options.allowed_ui_latest_data) {
			url = new Curl('zabbix.php', false);
			url.setArgument('action', 'latest.view');

			if ('tags' in options) {
				url.setArgument('tags', options.tags);
				url.setArgument('evaltype', options.evaltype);
			}

			url.setArgument('filter_name', '');
			url.setArgument('hostids[]', options.hostid);

			items.push({
				label: t('Latest data'),
				url: url.getUrl()
			});
		}

		// graphs
		if (options.allowed_ui_hosts) {
			url = new Curl('zabbix.php', false);
			url.setArgument('action', 'charts.view')
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('filter_set', '1');

			items.push({
				label: t('Graphs'),
				disabled: !options.showGraphs,
				url: url.getUrl()
			});
		}

		// web
		if (options.allowed_ui_hosts) {
			url = new Curl('zabbix.php', false);
			url.setArgument('action', 'web.view');
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('filter_set', '1');

			items.push({
				label: t('Web'),
				disabled: !options.showWeb,
				url: url.getUrl()
			});
		}

		// inventory link
		if (options.allowed_ui_inventory) {
			url = new Curl('hostinventories.php', false);
			url.setArgument('hostid', options.hostid);

			items.push({
				label: t('Inventory'),
				url: url.getUrl()
			});
		}

		if (items.length) {
			sections.push({
				label: t('View'),
				items: items
			});
		}

		// Configuration
		if (options.allowed_ui_conf_hosts) {
			// host
			url = new Curl('zabbix.php', false);
			url.setArgument('action', 'host.edit');
			url.setArgument('hostid', options.hostid);

			configuration.push({
				label: t('Host'),
				disabled: !options.isWriteable,
				url: url.getUrl(),
				clickCallback: function(e) {
					e.preventDefault();
					jQuery(this).closest('.menu-popup').menuPopup('close', null);

					view.editHost(options.hostid);
				}
			});

			// items
			url = new Curl('items.php', false);
			url.setArgument('filter_set', '1');
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('context', 'host');

			configuration.push({
				label: t('Items'),
				disabled: !options.isWriteable,
				url: url.getUrl()
			});

			// triggers
			url = new Curl('triggers.php', false);
			url.setArgument('filter_set', '1');
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('context', 'host');

			configuration.push({
				label: t('Triggers'),
				disabled: !options.isWriteable,
				url: url.getUrl()
			});

			// graphs
			url = new Curl('graphs.php', false);
			url.setArgument('filter_set', '1');
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('context', 'host');

			configuration.push({
				label: t('Graphs'),
				disabled: !options.isWriteable,
				url: url.getUrl()
			});

			// discovery
			url = new Curl('host_discovery.php', false);
			url.setArgument('filter_set', '1');
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('context', 'host');

			configuration.push({
				label: t('Discovery'),
				disabled: !options.isWriteable,
				url: url.getUrl()
			});

			// web scenario
			url = new Curl('httpconf.php', false);
			url.setArgument('filter_set', '1');
			url.setArgument('filter_hostids[]', options.hostid);
			url.setArgument('context', 'host');

			configuration.push({
				label: t('Web'),
				disabled: !options.isWriteable,
				url: url.getUrl()
			});

			sections.push({
				label: t('Configuration'),
				items: configuration
			});
		}
	}

	// urls
	if ('urls' in options) {
		sections.push({
			label: t('Links'),
			items: getMenuPopupURLData(options.urls, trigger_element)
		});
	}

	// scripts
	if ('scripts' in options) {
		sections.push({
			label: t('Scripts'),
			items: getMenuPopupScriptData(options.scripts, trigger_element, options.hostid)
		});
	}

	return sections;
}

/**
 * Get menu popup submap map element section data.
 *
 * @param {array}  options['sysmapid']
 * @param {int}    options['severity_min']    (optional)
 * @param {int}    options['is_widget']       (optional)
 * @param {array}  options['urls']            (optional)
 * @param {string} options['url'][]['label']
 * @param {string} options['url'][]['url']
 *
 * @return array
 */
function getMenuPopupMapElementSubmap(options) {
	const sections = [];
	const item = {label: t('Submap')};

	if (options.unique_id !== undefined) {
		item.clickCallback = ()=> {
			ZABBIX.Dashboard.getDashboardPages().forEach((page) => {
				const widget = page.getWidget(options.unique_id);

				if (widget !== null) {
					widget.navigateToSubmap(options.sysmapid);
				}
			});
		};
	}
	else {
		if (!options.allowed_ui_maps) {
			return [];
		}

		const submap_url = new Curl('zabbix.php', false);
		submap_url.setArgument('action', 'map.view');
		submap_url.setArgument('sysmapid', options.sysmapid);
		if (typeof options.severity_min !== 'undefined') {
			submap_url.setArgument('severity_min', options.severity_min);
		}
		item.url = submap_url.getUrl();
	}

	sections.push({
		label: t('Go to'),
		items: [item]
	});

	// urls
	if (typeof options.urls !== 'undefined') {
		sections.push({
			label: t('Links'),
			items: options.urls
		});
	}

	return sections;
}

/**
 * Get menu popup host group map element section data.
 *
 * @param {string} options['groupid']
 * @param {array}  options['severities']         (optional)
 * @param {bool}   options['show_suppressed']    (optional)
 * @param {array}  options['urls']               (optional)
 * @param {string} options['url'][]['label']
 * @param {string} options['url'][]['url']
 * @param {array}  options['tags']               (optional)
 * @param {string} options['tags'][]['tag']
 * @param {string} options['tags'][]['value']
 * @param {number} options['tags'][]['operator']
 * @param {number} options['evaltype']           (optional)
 *
 * @return array
 */
function getMenuPopupMapElementGroup(options) {
	if (!options.allowed_ui_problems) {
		return [];
	}

	var sections = [],
		problems_url = new Curl('zabbix.php', false);

	problems_url.setArgument('action', 'problem.view');
	problems_url.setArgument('filter_name', '');
	problems_url.setArgument('groupids[]', options.groupid);
	if (typeof options.severities !== 'undefined') {
		problems_url.setArgument('severities[]', options.severities);
	}
	if (typeof options.show_suppressed !== 'undefined' && options.show_suppressed) {
		problems_url.setArgument('show_suppressed', '1');
	}
	if (typeof options.tags !== 'undefined') {
		problems_url.setArgument('tags', options.tags);
		problems_url.setArgument('evaltype', options.evaltype);
	}

	sections.push({
		label: t('Go to'),
		items: [{
			label: t('Problems'),
			url: problems_url.getUrl()
		}]
	});

	// urls
	if (typeof options.urls !== 'undefined') {
		sections.push({
			label: t('Links'),
			items: options.urls
		});
	}

	return sections;
}

/**
 * Get menu popup trigger map element section data.
 *
 * @param {array}  options['triggerids']
 * @param {array}  options['severities']       (optional)
 * @param {bool}   options['show_suppressed']  (optional)
 * @param {array}  options['urls']             (optional)
 * @param {string} options['url'][]['label']
 * @param {string} options['url'][]['url']
 *
 * @return array
 */
function getMenuPopupMapElementTrigger(options) {
	const sections = [];
	const items = [];
	let url;

	if (options.allowed_ui_problems) {
		url = new Curl('zabbix.php', false);
		url.setArgument('action', 'problem.view');
		url.setArgument('filter_name', '');
		url.setArgument('triggerids', options.triggers.map((value) => value.triggerid));

		if ('severities' in options) {
			url.setArgument('severities[]', options.severities);
		}

		if ('show_suppressed' in options) {
			url.setArgument('show_suppressed', '1');
		}

		items.push({
			label: t('Problems'),
			disabled: !options.showEvents,
			url: url.getUrl()
		});
	}

	// items problems
	if (options.allowed_ui_latest_data && options.items.length) {
		const history = [];

		for (const item of options.items) {
			url = new Curl('history.php', false);
			url.setArgument('action', item.params.action);
			url.setArgument('itemids[]', item.params.itemid);

			history.push({
				label: item.name,
				url: url.getUrl()
			});
		};

		items.push({
			label: t('History'),
			items: history
		});
	}

	if (items.length) {
		sections.push({
			label: t('View'),
			items: items
		});
	}

	// configuration
	if (options.allowed_ui_conf_hosts) {
		const config_urls = [];
		const trigger_urls = [];
		const item_urls = [];

		for (const value of options.triggers) {
			url = new Curl('triggers.php', false);
			url.setArgument('form', 'update');
			url.setArgument('triggerid', value.triggerid);
			url.setArgument('context', 'host');

			trigger_urls.push({
				label: value.description,
				url: url.getUrl()
			});
		}

		config_urls.push({
			label: t('Triggers'),
			items: trigger_urls
		});

		if (options.items.length) {
			for (const item of options.items) {
				url = new Curl('items.php', false);
				url.setArgument('form', 'update');
				url.setArgument('itemid', item.params.itemid);
				url.setArgument('context', 'host');

				item_urls.push({
					label: item.name,
					disabled: item.params.is_webitem,
					url: url.getUrl()
				});
			};

			config_urls.push({
				label: t('Items'),
				items: item_urls
			});
		}

		sections.push({
			label: t('Configuration'),
			items: config_urls
		});
	}

	// urls
	if ('urls' in options) {
		sections.push({
			label: t('Links'),
			items: options.urls
		});
	}

	return sections;
}

/**
 * Get menu popup image map element section data.
 *
 * @param {array}  options['urls']             (optional)
 * @param {string} options['url'][]['label']
 * @param {string} options['url'][]['url']
 *
 * @return array
 */
function getMenuPopupMapElementImage(options) {
	// urls
	if (typeof options.urls !== 'undefined') {
		return [{
			label: t('Links'),
			items: options.urls
		}];
	}

	return [];
}

/**
 * Get menu popup dashboard actions data.
 *
 * @param {string} options['dashboardid']
 * @param {bool}   options['editable']
 * @param {bool}   options['has_related_reports']
 * @param {bool}   options['can_edit_dashboards']
 * @param {bool}   options['can_view_reports']
 * @param {bool}   options['can_create_reports']
 * @param {object} trigger_element                   UI element which triggered opening of overlay dialogue.
 *
 * @return array
 */
function getMenuPopupDashboard(options, trigger_element) {
	const sections = [];
	const parameters = {dashboardid: options.dashboardid};

	// Dashboard actions.
	if (options.can_edit_dashboards) {
		const url_create = new Curl('zabbix.php', false);
		url_create.setArgument('action', 'dashboard.view');
		url_create.setArgument('new', '1');

		const url_clone = new Curl('zabbix.php', false);
		url_clone.setArgument('action', 'dashboard.view');
		url_clone.setArgument('source_dashboardid', options.dashboardid);

		const url_delete = new Curl('zabbix.php', false);
		url_delete.setArgument('action', 'dashboard.delete');
		url_delete.setArgument('dashboardids', [options.dashboardid]);

		sections.push({
			label: t('Actions'),
			items: [
				{
					label: t('Sharing'),
					clickCallback: function () {
						jQuery(this).closest('.menu-popup').menuPopup('close', null);

						PopUp('popup.dashboard.share.edit', parameters, {
							dialogueid: 'dashboard_share_edit',
							dialogue_class: 'modal-popup-generic',
							trigger_element
						});
					},
					disabled: !options.editable
				},
				{
					label: t('Create new'),
					url: url_create.getUrl()
				},
				{
					label: t('Clone'),
					url: url_clone.getUrl()
				},
				{
					label: t('Delete'),
					clickCallback: function () {
						jQuery(this).closest('.menu-popup').menuPopup('close', null);

						if (!confirm(t('Delete dashboard?'))) {
							return false;
						}

						redirect(url_delete.getUrl(), 'post', 'sid', true, true);
					},
					disabled: !options.editable
				}
			]
		});
	}

	// Report actions.
	if (options.can_view_reports) {
		const report_actions = [
			{
				label: t('View related reports'),
				clickCallback: function () {
					jQuery(this).closest('.menu-popup').menuPopup('close', null);

					PopUp('popup.scheduledreport.list', parameters, {trigger_element});
				},
				disabled: !options.has_related_reports
			}
		];

		if (options.can_create_reports) {
			report_actions.unshift({
				label: t('Create new report'),
				clickCallback: function () {
					jQuery(this).closest('.menu-popup').menuPopup('close', null);

					PopUp('popup.scheduledreport.edit', parameters, {trigger_element});
				}
			});
		}

		sections.push({
			label: options.can_edit_dashboards ? null : t('Actions'),
			items: report_actions
		})
	}

	return sections;
}

/**
 * Get menu popup trigger section data.
 *
 * @param {string} options['triggerid']               Trigger ID.
 * @param {string} options['eventid']                 (optional) Required for Acknowledge section.
 * @param {object} options['items']                   Link to trigger item history page (optional).
 * @param {string} options['items'][]['name']         Item name.
 * @param {object} options['items'][]['params']       Item URL parameters ("name" => "value").
 * @param {bool}   options['acknowledge']             (optional) Whether to show Acknowledge section.
 * @param {object} options['configuration']           Link to trigger configuration page (optional).
 * @param {bool}   options['showEvents']              Show Problems item enabled. Default: false.
 * @param {string} options['url']                     Trigger URL link (optional).
 * @param {object} trigger_element                      UI element which triggered opening of overlay dialogue.
 *
 * @return array
 */
function getMenuPopupTrigger(options, trigger_element) {
	const sections = [];
	const items = [];
	let url;

	if (options.allowed_ui_problems) {
		// events
		url = new Curl('zabbix.php', false);
		url.setArgument('action', 'problem.view');
		url.setArgument('filter_name', '');
		url.setArgument('triggerids[]', options.triggerid);

		items.push({
			label: t('Problems'),
			disabled: !options.showEvents,
			url: url.getUrl()
		});
	}

	// acknowledge
	if ('acknowledge' in options) {
		items.push({
			label: t('Acknowledge'),
			clickCallback: function() {
				jQuery(this).closest('.menu-popup-top').menuPopup('close', null);

				acknowledgePopUp({eventids: [options.eventid]}, trigger_element);
			}
		});
	}

	// items problems
	if (options.allowed_ui_latest_data && options.items.length) {
		const history = [];

		for (const item of options.items) {
			url = new Curl('history.php', false);
			url.setArgument('action', item.params.action);
			url.setArgument('itemids[]', item.params.itemid);

			history.push({
				label: item.name,
				url: url.getUrl()
			});
		};

		items.push({
			label: t('History'),
			items: history
		});
	}

	if (items.length) {
		sections.push({
			label: t('View'),
			items: items
		});
	}

	// configuration
	if (options.allowed_ui_conf_hosts) {
		const config_urls = [];
		const item_urls = [];

		url = new Curl('triggers.php', false);
		url.setArgument('form', 'update');
		url.setArgument('triggerid', options.triggerid);
		url.setArgument('context', 'host');

		config_urls.push({
			label: t('Trigger'),
			url: url.getUrl()
		});

		if (options.items.length) {
			for (const item of options.items) {
				url = new Curl('items.php', false);
				url.setArgument('form', 'update');
				url.setArgument('itemid', item.params.itemid);
				url.setArgument('context', 'host');

				item_urls.push({
					label: item.name,
					disabled: item.params.is_webitem,
					url: url.getUrl()
				});
			}

			config_urls.push({
				label: t('Items'),
				items: item_urls
			});
		}

		sections.push({
			label: t('Configuration'),
			items: config_urls
		});
	}

	// urls
	if ('urls' in options) {
		sections.push({
			label: t('Links'),
			items: getMenuPopupURLData(options.urls, trigger_element)
		});
	}

	// scripts
	if ('scripts' in options) {
		sections.push({
			label: t('Scripts'),
			items: getMenuPopupScriptData(options.scripts, trigger_element, null, options.eventid)
		});
	}

	return sections;
}

/**
 * Get menu popup latest data item log section data.
 *
 * @param string options['itemid']
 * @param string options['hostid']
 * @param bool   options['showGraph']                   Link to Monitoring->Items->Graphs page.
 * @param bool   options['history']                     Is history available.
 * @param bool   options['trends']                      Are trends available.
 * @param bool   options['allowed_ui_conf_hosts']       Whether user has access to configuration hosts pages.
 * @param bool   options['isWriteable']                 Whether user has read and write access to host and its items.
 *
 * @return array
 */
function getMenuPopupItem(options) {
	const sections = [];
	const actions = [];
	const items = [];
	let url;

	// latest data link
	if (options.allowed_ui_latest_data) {
		url = new Curl('zabbix.php', false);
		url.setArgument('action', 'latest.view');
		url.setArgument('hostids[]', options.hostid);
		url.setArgument('name', options.name);
		url.setArgument('filter_name', '');

		items.push({
			label: t('Latest data'),
			url: url.getUrl()
		});
	}

	url = new Curl('history.php', false);
	url.setArgument('action', 'showgraph');
	url.setArgument('itemids[]', options.itemid);

	items.push({
		label: t('Graph'),
		url: url.getUrl(),
		disabled: !options.showGraph
	});

	url = new Curl('history.php', false);
	url.setArgument('action', 'showvalues');
	url.setArgument('itemids[]', options.itemid);

	items.push({
		label: t('Values'),
		url: url.getUrl(),
		disabled: !options.history && !options.trends
	});

	url = new Curl('history.php', false);
	url.setArgument('action', 'showlatest');
	url.setArgument('itemids[]', options.itemid);

	items.push({
		label: t('500 latest values'),
		url: url.getUrl(),
		disabled: !options.history && !options.trends
	});

	sections.push({
		label: t('View'),
		items: items
	});

	if (options.allowed_ui_conf_hosts) {
		const config_urls = [];
		const config_triggers = {
			label: t('Triggers'),
			disabled: options.triggers.length === 0
		};

		if (options.isWriteable) {
			url = new Curl('items.php', false);
			url.setArgument('form', 'update');
			url.setArgument('hostid', options.hostid);
			url.setArgument('itemid', options.itemid);
			url.setArgument('backurl', options.backurl);
			url.setArgument('context', options.context);

			config_urls.push({
				label: t('Item'),
				url: url.getUrl()
			});
		}

		if (options.triggers.length) {
			const trigger_items = [];

			for (const value of options.triggers) {
				url = new Curl('triggers.php', false);
				url.setArgument('form', 'update');
				url.setArgument('triggerid', value.triggerid);
				url.setArgument('backurl', options.backurl);
				url.setArgument('context', options.context);

				trigger_items.push({
					label: value.description,
					url: url.getUrl()
				});
			}

			config_triggers.items = trigger_items;
		}

		config_urls.push(config_triggers);

		url = new Curl('triggers.php', false);
		url.setArgument('form', 'create');
		url.setArgument('hostid', options.hostid);
		url.setArgument('description', options.name);
		url.setArgument('expression', 'func(/' + options.host + '/' + options.key + ')');
		url.setArgument('backurl', options.backurl);
		url.setArgument('context', options.context);

		config_urls.push({
			label: t('Create trigger'),
			url: url.getUrl()
		});

		url = new Curl('items.php', false);
		url.setArgument('form', 'create');
		url.setArgument('hostid', options.hostid);
		url.setArgument('type', 18); // ITEM_TYPE_DEPENDENT
		url.setArgument('master_itemid', options.itemid);
		url.setArgument('backurl', options.backurl);
		url.setArgument('context', options.context);

		config_urls.push({
			label: t('Create dependent item'),
			url: url.getUrl(),
			disabled: options.isDiscovery
		});

		url = new Curl('host_discovery.php', false);
		url.setArgument('form', 'create');
		url.setArgument('hostid', options.hostid);
		url.setArgument('type', 18); // ITEM_TYPE_DEPENDENT
		url.setArgument('master_itemid', options.itemid);
		url.setArgument('backurl', options.backurl);
		url.setArgument('context', options.context);

		config_urls.push({
			label: t('Create dependent discovery rule'),
			url: url.getUrl(),
			disabled: options.isDiscovery
		});

		sections.push({
			label: t('Configuration'),
			items: config_urls
		});
	}

	const execute = {
		label: t('Execute now'),
		disabled: !options.isExecutable
	};

	if (options.isExecutable) {
		execute.clickCallback = function () {
			jQuery(this).closest('.menu-popup').menuPopup('close', null);

			view.checkNow(options.itemid);
		};
	}

	actions.push(execute);

	sections.push({
		label: t('Actions'),
		items: actions
	});

	return sections;
}

/**
 * Get menu structure for item prototypes.
 *
 * @param array  options['name']
 * @param string options['backurl']                             Url from where the popup menu was called.
 * @param array  options['key']                                 Item prototype key.
 * @param array  options['host']                                Host name.
 * @param array  options['itemid']
 * @param array  options['trigger_prototypes']                  (optional)
 * @param string options['trigger_prototypes'][n]['triggerid']
 * @param string options['trigger_prototypes'][n]['name']
 * @param array  options['parent_discoveryid']
 * @param string options['context']                             Additional parameter in URL to identify main section.
 *
 * @return array
 */
function getMenuPopupItemPrototype(options) {
	const sections = [];
	const config_urls = [];
	let config_triggers = {
		label: t('Trigger prototypes'),
		disabled: true
	};

	let url;

	url = new Curl('disc_prototypes.php', false);
	url.setArgument('form', 'update');
	url.setArgument('parent_discoveryid', options.parent_discoveryid);
	url.setArgument('itemid', options.itemid);
	url.setArgument('context', options.context);

	config_urls.push({
		label: t('Item prototype'),
		url: url.getUrl()
	});

	if (options.trigger_prototypes.length) {
		const trigger_prototypes = [];

		for (const value of options.trigger_prototypes) {
			url = new Curl('trigger_prototypes.php', false);
			url.setArgument('form', 'update');
			url.setArgument('parent_discoveryid', options.parent_discoveryid);
			url.setArgument('triggerid', value.triggerid)
			url.setArgument('context', options.context);
			url.setArgument('backurl', options.backurl);

			trigger_prototypes.push({
				label: value.description,
				url: url.getUrl()
			});
		}

		if (trigger_prototypes.length) {
			config_triggers = {...config_triggers, ...{items: trigger_prototypes, disabled: false}};
		}
	}

	config_urls.push(config_triggers);

	url = new Curl('trigger_prototypes.php', false);
	url.setArgument('parent_discoveryid', options.parent_discoveryid);
	url.setArgument('form', 'create');
	url.setArgument('description', options.name);
	url.setArgument('expression', 'func(/' + options.host + '/' + options.key + ')');
	url.setArgument('context', options.context);
	url.setArgument('backurl', options.backurl);

	config_urls.push({
		label: t('Create trigger prototype'),
		url: url.getUrl()
	});

	url = new Curl('disc_prototypes.php', false);
	url.setArgument('form', 'create');
	url.setArgument('parent_discoveryid', options.parent_discoveryid);
	url.setArgument('type', 18);	// ITEM_TYPE_DEPENDENT
	url.setArgument('master_itemid', options.itemid);
	url.setArgument('context', options.context);

	config_urls.push({
		label: t('Create dependent item'),
		url: url.getUrl()
	});

	sections.push({
		label: t('Configuration'),
		items: config_urls
	});

	return sections;
}

/**
 * Get dropdown section data.
 *
 * @param {array}  options
 * @param {object} trigger_elem  UI element that was clicked to open overlay dialogue.
 *
 * @returns array
 */
function getMenuPopupDropdown(options, trigger_elem) {
	var items = [];

	jQuery.each(options.items, function(i, item) {
		var row = {
			label: item.label,
			url: item.url || 'javascript:void(0);'
		};

		if (item.class) {
			row.class = item.class;
		}

		if (options.toggle_class) {
			row.clickCallback = () => {
				jQuery(trigger_elem)
					.removeClass()
					.addClass(['btn-alt', options.toggle_class, item.class].join(' '));

				jQuery('input[type=hidden]', jQuery(trigger_elem).parent())
					.val(item.value)
					.trigger('change');
			}
		}
		else if (options.submit_form) {
			row.url = 'javascript:void(0);';
			row.clickCallback = () => {
				var $_form = trigger_elem.closest('form');

				if (!$_form.data("action")) {
					$_form.data("action", $_form.attr("action"));
				}

				$_form.attr("action", item.url);
				$_form.submit();
			}
		}

		items.push(row);
	});

	return [{
		items: items
	}];
}

/**
 * Get menu popup submenu section data.
 *
 * @param object options['submenu']                                    List of menu sections.
 * @param object options['submenu'][section]                           An individual section definition.
 * @param string options['submenu'][section]['label']                  Non-clickable section label.
 * @param object options['submenu'][section]['items']                  List of menu items of the section.
 * @param string options['submenu'][section]['items'][url]             Menu item label for the given url.
 * @param object options['submenu'][section]['items'][key]             Menu item with a submenu.
 * @param object options['submenu'][section]['items'][key]['label']    Non-clickable subsection label.
 * @param object options['submenu'][section]['items'][key]['items']    List of menu items of the subsection.
 * @param object options['submenu'][section]['items'][key]['items'][]  More levels of submenu.
 *
 * @returns array
 */
function getMenuPopupSubmenu(options) {
	var transform = function(sections) {
			var result = [];

			for (var key in sections) {
				if (typeof sections[key] === 'object') {
					var item = {};
					for (var item_key in sections[key]) {
						if (item_key === 'items') {
							item[item_key] = transform(sections[key][item_key]);
						}
						else {
							item[item_key] = sections[key][item_key];
						}
					}
					result.push(item);
				}
				else {
					result.push({
						'label': sections[key],
						'url': key
					});
				}
			}

			return result;
		};

	return transform(options.submenu);
}

/**
 * Get data for the "Insert expression" menu in the trigger expression constructor.
 *
 * @return array
 */
function getMenuPopupTriggerMacro(options) {
	var items = [],
		expressions = [
			{
				label: t('Trigger status "OK"'),
				string: '{TRIGGER.VALUE}=0'
			},
			{
				label: t('Trigger status "Problem"'),
				string: '{TRIGGER.VALUE}=1'
			}
		];

	jQuery.each(expressions, function(key, expression) {
		items[items.length] = {
			label: expression.label,
			clickCallback: function() {
				var expressionInput = jQuery('#expr_temp');

				if (expressionInput.val().length > 0 && !confirm(t('Do you wish to replace the conditional expression?'))) {
					return false;
				}

				expressionInput.val(expression.string);

				jQuery(this).closest('.menu-popup').menuPopup('close', null);
			}
		};
	});

	return [{
		label: t('Insert expression'),
		items: items
	}];
}

/**
 * Build script menu tree.
 *
 * @param {array} scripts          Script names and nenu paths.
 * @param {Node}  trigger_element  UI element which triggered opening of overlay dialogue.
 * @param {array} hostid           Host ID.
 * @param {array} eventid          Event ID.
 *
 * @return {array}
 */
function getMenuPopupScriptData(scripts, trigger_element, hostid, eventid) {
	let tree = {};

	// Parse scripts and create tree.
	for (let key in scripts) {
		const script = scripts[key];

		if (typeof script.scriptid !== 'undefined') {
			const items = (script.menu_path.length > 0) ? splitPath(script.menu_path) : [];

			appendTreeItem(tree, script.name, items, {
				scriptid: script.scriptid,
				confirmation: script.confirmation,
				hostid: hostid,
				eventid: eventid
			});
		}
	}

	return getMenuPopupScriptItems(tree, trigger_element);
}

/**
 * Build URL menu tree.
 *
 * @param {array} urls             URL names and nenu paths.
 * @param {Node}  trigger_element  UI element which triggered opening of overlay dialogue.
 *
 * @return {array}
 */
function getMenuPopupURLData(urls, trigger_element) {
	let tree = {};

	// Parse URLs and create tree.
	for (let key in urls) {
		const url = urls[key];

		if (typeof url.menu_path !== 'undefined') {
			const items = (url.menu_path.length > 0) ? splitPath(url.menu_path) : [];

			appendTreeItem(tree, url.label, items, {
				url: url.url,
				target: url.target,
				confirmation: url.confirmation,
				rel: url.rel,
			});
		}
	}

	return getMenuPopupURLItems(tree, trigger_element);
}

/**
 * Add a menu item to tree.
 *
 * @param {object} tree   Menu tree object to where menu items will be added.
 * @param {string} name   Menu element label (name).
 * @param {array}  items  List of menu items to add.
 * @param {object} params Additional menu item parameters like URL, target, clickcallback etc.
 */
function appendTreeItem(tree, name, items, params) {
	if (items.length > 0) {
		const item = items.shift();

		if (typeof tree[item] === 'undefined') {
			tree[item] = {
				name: item,
				items: {}
			};
		}

		appendTreeItem(tree[item].items, name, items, params);
	}
	else {
		tree['/' + name] = {
			name: name,
			params: params,
			items: {}
		};
	}
}

/**
 * Build URL menu items from tree.
 *
 * @param {object} tree        Menu tree object to where menu items are.
 * @param {Node}   trigger_elm UI element which triggered opening of overlay dialogue.
 *
 * @return {array}
 */
function getMenuPopupURLItems(tree, trigger_elm) {
	let items = [];

	if (objectSize(tree) > 0) {
		Object.values(tree).map((data) => {
			const item = {label: data.name};

			if (typeof data.items !== 'undefined' && objectSize(data.items) > 0) {
				item.items = getMenuPopupURLItems(data.items, trigger_elm);
			}

			if (typeof data.params !== 'undefined') {
				item.url = data.params.url;
				item.target = data.params.target;
				item.rel = data.params.rel;

				if (data.params.confirmation !== '') {
					item.clickCallback = function(e) {
						return confirm(data.params.confirmation);
					}
				}
			}

			items[items.length] = item;
		});
	}

	return items;
}

/**
 * Build script menu items from tree.
 *
 * @param {object} tree        Menu tree object to where menu items are.
 * @param {Node}   trigger_elm UI element which triggered opening of overlay dialogue.
 *
 * @return {array}
 */
function getMenuPopupScriptItems(tree, trigger_elm) {
	let items = [];

	if (objectSize(tree) > 0) {
		Object.values(tree).map((data) => {
			const item = {label: data.name};

			if (typeof data.items !== 'undefined' && objectSize(data.items) > 0) {
				item.items = getMenuPopupScriptItems(data.items, trigger_elm);
			}

			if (typeof data.params !== 'undefined' && typeof data.params.scriptid !== 'undefined') {
				item.clickCallback = function(e) {
					jQuery(this)
						.closest('.menu-popup-top')
						.menuPopup('close', trigger_elm, false);
					executeScript(data.params.scriptid, data.params.confirmation, trigger_elm, data.params.hostid,
						data.params.eventid
					);
					cancelEvent(e);
				};
			}

			items[items.length] = item;
		});
	}

	return items;
}

jQuery(function($) {

	/**
	 * Menu popup.
	 *
	 * @param array  sections              Menu sections.
	 * @param string sections[n]['label']  Section title (optional).
	 * @param array  sections[n]['items']  Section menu data (see createMenuItem() for available options).
	 * @param object event                 Menu popup call event.
	 * @param object options               Menu popup options (optional).
	 * @param object options['class']      Menu popup additional class name (optional).
	 * @param object options['position']   Menu popup position object (optional).
	 *
	 * @see createMenuItem()
	 */
	$.fn.menuPopup = function(method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		}
		else {
			return methods.init.apply(this, arguments);
		}
	};

	/**
	 * Created popup menu item nodes and append to $menu_popup.
	 *
	 * @param {object} $menu_popup    jQuery node of popup menu.
	 * @param {array} sections        Array of menu popup sections.
	 */
	function addMenuPopupItems($menu_popup, sections) {
		// Create menu sections.
		$.each(sections, function(i, section) {
			// Add a separator between menu item sections.
			if (i > 0) {
				$menu_popup.append($('<li>').append($('<div>')));
			}

			var section_label = null;

			if (typeof section.label === 'string' && section.label.length) {
				section_label = section.label;
			}

			// Add menu item section label, if provided.
			if (section_label !== null) {
				$menu_popup.append($('<li>').append($('<h3>').text(section_label)));
			}

			// Add individual menu items of the section.
			$.each(section.items, function(i, item) {
				item = $.extend({}, item);
				if (sections.length > 1 && section_label !== null) {
					item.ariaLabel = section_label + ', ' + item['label'];
				}
				$menu_popup.append(createMenuItem(item));
			});
		});

		if (sections.length == 1) {
			if (typeof sections[0].label === 'string' && sections[0].label.length) {
				$menu_popup.attr({'aria-label': sections[0].label});
			}
		}
	}

	var defaultOptions = {
		closeCallback: function(){},
		background_layer: true
	};

	var methods = {
		init: function(sections, event, options) {
			// Don't display empty menu.
			if (!sections.length || !sections[0]['items'].length) {
				return;
			}

			var $opener = $(this);

			options = $.extend({
				position: {
					/*
					 * Please note that click event is also triggered by hitting spacebar on the keyboard,
					 * in which case the number of mouse clicks (stored in event.originalEvent.detail) will be zero.
					 */
					of: (['click', 'mouseup', 'mousedown'].includes(event.type) && event.originalEvent.detail)
						? event
						: event.target,
					my: 'left top',
					at: 'left bottom',
					using: (pos, data) => {
						let max_left = data.horizontal === 'left'
							? document.querySelector('.wrapper').clientWidth
							: document.querySelector('.wrapper').clientWidth - data.element.width;

						pos.top = Math.max(0, pos.top);
						pos.left = Math.max(0, Math.min(max_left, pos.left));

						data.element.element[0].style.top = `${pos.top}px`;
						data.element.element[0].style.left = `${pos.left}px`;
					}
				}
			}, defaultOptions, options || {});

			// Close other action menus and prevent focus jumping before opening a new popup.
			$('.menu-popup-top').menuPopup('close', null, false);

			$opener.attr('aria-expanded', 'true');

			var $menu_popup = $('<ul>', {
					'role': 'menu',
					'class': 'menu-popup menu-popup-top',
					'tabindex': 0
				});

			// Add custom class, if specified.
			if ('class' in options) {
				$menu_popup.addClass(options.class);
			}

			$opener.data({
				sections: sections,
				menu_popup: $menu_popup
			});
			addMenuPopupItems($menu_popup, sections);

			$menu_popup.data('menu_popup', options);

			if (options.background_layer) {
				$('.wrapper').append($('<div>', {class: 'menu-popup-overlay'}));
			}

			$('.wrapper').append($menu_popup);

			// Position the menu (before hiding).
			$menu_popup.position(options.position);

			// Hide all action menu sub-levels, including the topmost, for fade effect to work.
			$menu_popup.add('.menu-popup', $menu_popup).hide();

			// Position and display the menu.
			$menu_popup.fadeIn(50);

			addToOverlaysStack('menu-popup', event.target, 'menu-popup');

			// Need to be postponed.
			setTimeout(function() {
				$(document)
					.on('click dragstart contextmenu', {menu: $menu_popup, opener: $opener},
						menuPopupDocumentCloseHandler
					)
					.on('keydown', {menu: $menu_popup}, menuPopupKeyDownHandler);
			});

			$menu_popup.focus();
		},

		close: function(trigger_elem, return_focus) {
			var menu_popup = $(this),
				options = $(menu_popup).data('menu_popup') || {};

			if (!menu_popup.is(trigger_elem) && menu_popup.has(trigger_elem).length === 0) {
				$('[aria-expanded="true"]', trigger_elem).attr({'aria-expanded': 'false'});
				menu_popup.fadeOut(0);

				$('.highlighted', menu_popup).removeClass('highlighted');
				$('[aria-expanded="true"]', menu_popup).attr({'aria-expanded': 'false'});

				$(document)
					.off('click dragstart contextmenu', menuPopupDocumentCloseHandler)
					.off('keydown', menuPopupKeyDownHandler);

				var overlay = removeFromOverlaysStack('menu-popup', return_focus);

				if (overlay && typeof overlay['element'] !== undefined) {
					// Remove expanded attribute of the original opener.
					$(overlay['element']).attr({'aria-expanded': 'false'});
				}

				if (options.background_layer) {
					menu_popup.prev().remove();
				}

				menu_popup.remove();

				// Call menu close callback function.
				typeof options.closeCallback === 'function' && options.closeCallback.apply();
			}
		},

		/**
		 * Refresh popup menu, call refreshCallback for every item if defined. Refresh recreate item dom nodes.
		 */
		refresh: function(widget) {
			var $opener = $(this),
				sections = $opener.data('sections'),
				$menu_popup = $opener.data('menu_popup');

			$menu_popup.empty();
			sections.forEach(
				section => section.items && section.items.forEach(
					item => item.refreshCallback && item.refreshCallback.call(item, widget)
			));
			addMenuPopupItems($menu_popup, sections);
		}
	};

	/**
	 * Expends hovered/selected context menu item.
	 */
	$.fn.actionMenuItemExpand = function() {
		var li = $(this),
			pos = li.position(),
			menu = li.closest('.menu-popup');

		for (var item = $('li:first-child', menu); item.length > 0; item = item.next()) {
			if (item[0] == li[0]) {
				$('>a', li[0]).addClass('highlighted');

				if (!$('ul', item[0]).is(':visible')) {
					const $submenu = $('ul:first', item[0]);

					if ($submenu.length) {
						const position = {
							'top': pos.top - 6,
							'left': pos.left + li.outerWidth() + 14,
						};

						$submenu
							.css('display' ,'block')
							.prev('[role="menuitem"]').attr({'aria-expanded': 'true'});

						let max_relative_left = $(window).outerWidth(true) - $submenu.outerWidth(true)
							- menu[0].getBoundingClientRect().left - 14 * 2;

						position.top = Math.max(0, position.top);
						position.left = Math.max(0, Math.min(max_relative_left, position.left));

						$submenu.css(position);
					}
				}
			}
			else {
				// Remove activity from item that has been selected by keyboard and now is deselected using mouse.
				if ($('>a', item[0]).hasClass('highlighted')) {
					$('>a', item[0]).removeClass('highlighted').blur();
				}

				// Closes all other submenus from this level, if they were open.
				if ($('ul', item[0]).is(':visible')) {
					$('ul', item[0]).prev('[role="menuitem"]').removeClass('highlighted');
					$('ul', item[0]).prev('[role="menuitem"]').attr({'aria-expanded': 'false'});
					$('ul', item[0]).css({'display': 'none'});
				}
			}
		}

		return this;
	};

	/**
	 * Collapses context menu item that has lost focus or is not selected anymore.
	 */
	$.fn.actionMenuItemCollapse = function() {
		// Remove style and close sub-menus in deeper levels.
		var parent_menu = $(this).closest('.menu-popup');
		$('.highlighted', parent_menu).removeClass('highlighted');
		$('[aria-expanded]', parent_menu).attr({'aria-expanded': 'false'});
		$('.menu-popup', parent_menu).css({'display': 'none'});

		// Close actual menu level.
		parent_menu.not('.menu-popup-top').css({'display': 'none'});
		parent_menu.prev('[role="menuitem"]').attr({'aria-expanded': 'false'});

		return this;
	};

	function menuPopupDocumentCloseHandler(event) {
		$(event.data.menu[0]).menuPopup('close', event.data.opener);
	}

	function menuPopupKeyDownHandler(event) {
		var link_selector = '.menu-popup-item',
			menu_popup = $(event.data.menu[0]),
			level = menu_popup,
			selected,
			items;

		// Find active menu level.
		while ($('[aria-expanded="true"]:visible', level).length) {
			level = $('[aria-expanded="true"]:visible:first', level.get(0)).next('[role="menu"]');
		}

		// Find active menu items.
		items = $('>li', level).filter(function() {
			return $(this).has('.menu-popup-item').length;
		});

		// Find an element that was selected when key was pressed.
		if ($('.menu-popup-item.highlighted', level).length) {
			selected = $(link_selector + '.highlighted', level).closest('li');
		}
		else if ($('.menu-popup-item', level).filter(function() {
			return this == document.activeElement;
		}).length) {
			selected = $(document.activeElement).closest('li');
		}

		// Perform action based on keydown event.
		switch (event.which) {
			case 37: // arrow left
				if (typeof selected !== 'undefined' && selected.has('.menu-popup')) {
					if (level != menu_popup) {
						selected.actionMenuItemCollapse();

						// Must focus previous element, otherwise screen reader will exit menu.
						selected.closest('.menu-popup').prev('[role="menuitem"]').addClass('highlighted').focus();
					}
				}
				break;

			case 38: // arrow up
				if (typeof selected === 'undefined') {
					$(link_selector + ':last', level).addClass('highlighted').focus();
				}
				else {
					var prev = items[items.index(selected) - 1];
					if (typeof prev === 'undefined') {
						prev = items[items.length - 1];
					}

					$(link_selector, selected).removeClass('highlighted');
					$(link_selector + ':first', prev).addClass('highlighted').focus();
				}

				// Prevent page scrolling.
				event.preventDefault();
				break;

			case 39: // arrow right
				if (typeof selected !== 'undefined' && selected.has('.menu-popup')) {
					selected.actionMenuItemExpand();
					$('ul > li ' + link_selector + ':first', selected).addClass('highlighted').focus();
				}
				break;

			case 40: // arrow down
				if (typeof selected === 'undefined') {
					$(link_selector + ':first', items[0]).addClass('highlighted').focus();
				}
				else {
					var next = items[items.index(selected) + 1];
					if (typeof next === 'undefined') {
						next = items[0];
					}

					$(link_selector, selected).removeClass('highlighted');
					$(link_selector + ':first', next).addClass('highlighted').focus();
				}

				// Prevent page scrolling.
				event.preventDefault();
				break;

			case 27: // ESC
				$(menu_popup).menuPopup('close', null);
				break;

			case 13: // Enter
				if (typeof selected !== 'undefined') {
					$('>' + link_selector, selected)[0].click();
				}
				break;

			case 9: // Tab
				event.preventDefault();
				break;
		}

		return false;
	}

	/**
	 * Create menu item.
	 *
	 * @param string options['label']          Link label.
	 * @param string options['ariaLabel']	   Aria-label text.
	 * @param string options['url']            Link url.
	 * @param string options['css']            Item class.
	 * @param array  options['data']           Item data ("key" => "value").
	 * @param array  options['items']          Item sub menu.
	 * @param {bool} options['disabled']       Item disable status.
	 * @param object options['clickCallback']  Item click callback.
	 *
	 * @return object
	 */
	function createMenuItem(options) {
		options = $.extend({
			ariaLabel: options.label,
			selected: false,
			disabled: false,
			class: false
		}, options);

		var item = $('<li>'),
			link = $('<a>', {
				role: 'menuitem',
				tabindex: '-1',
				'aria-label': options.selected ? sprintf(t('S_SELECTED_SR'), options.ariaLabel) : options.ariaLabel
			}).data('aria-label', options.ariaLabel);

		if (typeof options.label !== 'undefined') {
			link.text(options.label);

			if (typeof options.items !== 'undefined' && options.items.length > 0) {
				// if submenu exists
				link.append($('<span>', {'class': 'arrow-right'}));
			}
		}

		if (typeof options.data !== 'undefined' && objectSize(options.data) > 0) {
			$.each(options.data, function(key, value) {
				link.data(key, value);
			});
		}

		link.addClass('menu-popup-item');

		if (options.disabled) {
			link.addClass('disabled');
		}
		else {
			if (typeof options.url !== 'undefined') {
				link.attr('href', options.url);

				if ('target' in options) {
					link.attr('target', options.target);
				}

				if ('rel' in options) {
					link.attr('rel', options.rel);
				}
			}

			if (typeof options.clickCallback !== 'undefined') {
				link.on('click', options.clickCallback);
			}
		}

		if (options.selected) {
			link.addClass('selected');
		}

		if (options.class) {
			link.addClass(options.class);
		}

		if ('dataAttributes' in options) {
			$.each(options.dataAttributes, function(key, value) {
				link.attr((key.substr(0, 5) === 'data-') ? key : 'data-' + key, value);
			});
		}

		if (typeof options.items !== 'undefined' && options.items.length > 0) {
			link.attr({
				'aria-haspopup': 'true',
				'aria-expanded': 'false',
				'aria-hidden': 'true'
			});
			link.on('click', function(e) {
				e.stopPropagation();
			});
		}

		item.append(link);

		if (typeof options.items !== 'undefined' && options.items.length > 0) {
			var menu = $('<ul>', {
					class : 'menu-popup',
					role: 'menu'
				})
				.on('mouseenter', function(e) {
					// Prevent 'mouseenter' event in parent item, that would call actionMenuItemExpand() for parent.
					e.stopPropagation();
				});

			$.each(options.items, function(i, item) {
				menu.append(createMenuItem(item));
			});

			item.append(menu);
		}

		item.on('mouseenter', function(e) {
			e.stopPropagation();
			$(this).actionMenuItemExpand();
		});

		return item;
	}
});