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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////

import MainMenuFactory from './MainMenuFactory';
import _ from 'lodash';
import { checkMasterPassword, showQuickSearch } from '../../../static/js/Dialogs/index';
import { pgHandleItemError } from '../../../static/js/utils';
import { send_heartbeat, stop_heartbeat } from './heartbeat';
import getApiInstance from '../../../static/js/api_instance';
import usePreferences, { setupPreferenceBroadcast } from '../../../preferences/static/js/store';
import checkNodeVisibility from '../../../static/js/check_node_visibility';

define('pgadmin.browser', [
  'sources/gettext', 'sources/url_for', 'sources/pgadmin',
  'sources/csrf', 'pgadmin.authenticate.kerberos',
  'pgadmin.browser.utils', 'pgadmin.browser.messages',
  'pgadmin.browser.node', 'pgadmin.browser.collection', 'pgadmin.browser.activity',
  'pgadmin.browser.keyboard', 'sources/tree/pgadmin_tree_save_state',
], function(
  gettext, url_for, pgAdmin, csrfToken, Kerberos,
) {
  let pgBrowser = pgAdmin.Browser = pgAdmin.Browser || {};
  let select_object_msg = gettext('Please select an object in the tree view.');

  csrfToken.setPGCSRFToken(pgAdmin.csrf_token_header, pgAdmin.csrf_token);

  Kerberos.validate_kerberos_ticket();

  // Extend the browser class attributes
  _.extend(pgAdmin.Browser, {
    // The base url for browser
    URL: url_for('browser.index'),
    docker:null,
    // Reversed Engineer query for the selected database node object goes
    // here
    editor:null,
    // Left hand browser tree
    tree:null,
    // list of script to be loaded, when a certain type of node is loaded
    // It will be used to register extensions, tools, child node scripts,
    // etc.
    scripts: {},
    // Standard Widths and Height for dialogs in px
    stdW: {
      sm: 500,
      md: 700,
      lg: 900,
      default: 500,
      // If you change above values then make sure to update
      // calc method logic
      calc: (passed_width) => {
        let iw = window.innerWidth;
        if(iw > passed_width)
          return passed_width;
        else if (iw > pgAdmin.Browser.stdW.lg)
          return pgAdmin.Browser.stdW.lg;
        else if (iw > pgAdmin.Browser.stdW.md)
          return pgAdmin.Browser.stdW.md;
        else if (iw > pgAdmin.Browser.stdW.sm)
          return pgAdmin.Browser.stdW.sm;
        else
          // if avilable screen resolution is still
          // less then return the width value as it
          return iw;
      },
    },
    stdH: {
      sm: 200,
      md: 400,
      lg: 550,
      default: 550,
      // If you change above values then make sure to update
      // calc method logic
      calc: (passed_height) => {
        // We are excluding sm as it is too small for dialog
        let ih = window.innerHeight;
        if (ih > passed_height)
          return passed_height;
        else if (ih > pgAdmin.Browser.stdH.lg)
          return pgAdmin.Browser.stdH.lg;
        else if (ih > pgAdmin.Browser.stdH.md)
          return pgAdmin.Browser.stdH.md;
        else
          // if avilable screen resolution is still
          // less then return the height value as it
          return ih;
      },
    },
    // Default panels
    panels: {},
    // We also support showing dashboards, HTML file, external URL
    frames: {},
    /* Menus */
    // add_menus(...) will register all the menus
    // in this container
    all_menus_cache: {
      // All context menu goes here under certain menu types.
      // i.e. context: {'server': [...], 'server-group': [...]}
      context: {},
      // File menus
      file: {},
      // Edit menus
      edit: {},
      // Object menus
      object: {},
      // Management menus
      management: {},
      // Tools menus
      tools: {},
      // Help menus
      help: {},
    },
    MainMenus: [],
    BrowserContextMenu: [],

    menu_categories: {
      /* name, label (pair) */
      'register': {
        label: gettext('Register'),
        priority: 1,
        /* separator above this menu */
        above: false,
        below: true,
        /* icon: 'fa fa-magic', */
        single: true,
      },
      'create': {
        label: gettext('Create'),
        priority: 2,
        /* separator above this menu */
        above: false,
        below: true,
        /* icon: 'fa fa-magic', */
        single: true,
      },
    },
    // A callback to load/fetch a script when a certain node is loaded
    register_script: function(n, m, p) {
      let scripts = this.scripts;
      scripts[n] = _.isArray(scripts[n]) ? scripts[n] : [];
      scripts[n].push({'name': m, 'path': p, loaded: false});
    },
    masterpass_callback_queue: [],
    getMenuList: function(name, item, d, skipDisabled=false) {
      let obj = this;
      //This 'checkNoMenuOptionForNode' function will check if showMenu flag is present or not for selected node
      let {flag,showMenu}=MainMenuFactory.checkNoMenuOptionForNode(d);
      if(flag){
        if(showMenu===false){
          return [MainMenuFactory.createMenuItem({
            enable : false,
            label: gettext('No menu available for this object.'),
            name:'',
            priority: 1,
            category: 'create',
          })];
        }
      }else{
        let category = {
          'common': []
        };
        const nodeTypeMenus = obj.all_menus_cache[name][d._type];
        for(let key of Object.keys(nodeTypeMenus)) {
          let menuItem = nodeTypeMenus[key];
          let menuCategory = menuItem.category ?? 'common';
          category[menuCategory] = category[menuCategory] ?? [];
          category[menuCategory].push(menuItem);
        }
        let menuItemList = [];

        for(let c in category) {
          if((c in obj.menu_categories || category[c].length > 1) && c != 'common' ) {
            let allMenuItemsDisabled = true;
            category[c].forEach((mi)=> {
              mi.checkAndSetDisabled(d, item);
              if(allMenuItemsDisabled) {
                allMenuItemsDisabled = mi.isDisabled;
              }
            });

            const categoryMenuOptions = obj.menu_categories[c];
            let label = categoryMenuOptions?.label ?? c;
            let priority = categoryMenuOptions?.priority ?? 10;

            if(categoryMenuOptions?.above) {
              menuItemList.push(MainMenuFactory.getSeparator(label, priority));
            }
            if(!allMenuItemsDisabled && skipDisabled) {
              let _menuItem = MainMenuFactory.createMenuItem({
                name: c,
                label: label,
                module: c,
                category: c,
                menu_items: category[c],
                priority: priority
              });

              menuItemList.push(_menuItem);
            } else if(!skipDisabled){
              let _menuItem = MainMenuFactory.createMenuItem({
                name: c,
                label: label,
                module:c,
                category: c,
                menu_items: category[c],
                priority: priority
              });

              menuItemList.push(_menuItem);
            }
            if(categoryMenuOptions?.below) {
              menuItemList.push(MainMenuFactory.getSeparator(label, priority));
            }
          } else {
            category[c].forEach((c)=> {
              c.checkAndSetDisabled(d, item);
            });

            category[c].forEach((m)=> {
              if(!skipDisabled) {
                menuItemList.push(m);
              } else if(skipDisabled && !m.isDisabled){
                menuItemList.push(m);
              }
            });
          }
        }

        return menuItemList;
      }
    },
    // Enable/disable menu options
    enable_disable_menus: function(item) {
      let obj = this;
      let d = item ? obj.tree.itemData(item) : undefined;

      // All menus (except for the object menus) are already present.
      // They will just require to check, whether they are
      // enabled/disabled.
      pgBrowser.MainMenus.filter((m)=>m.name != 'object').forEach((menu) => {
        menu.menuItems.forEach((mitem) => {
          mitem.checkAndSetDisabled(d, item);
        });
      });

      // Create the object menu dynamically
      let objectMenu = pgBrowser.MainMenus.find((menu) => menu.name == 'object');
      if (item && obj.all_menus_cache['object']?.[d._type]) {
        let menuItemList = obj.getMenuList('object', item, d);
        objectMenu && MainMenuFactory.refreshMainMenuItems(objectMenu, menuItemList);
        let ctxMenuList = obj.getMenuList('context', item, d, true);
        obj.BrowserContextMenu = MainMenuFactory.getContextMenu(ctxMenuList);
      } else {
        objectMenu && MainMenuFactory.refreshMainMenuItems(objectMenu, [
          MainMenuFactory.createMenuItem({
            name: '',
            label: gettext('No object selected'),
            category: 'create',
            priority: 1,
            enable: false,
          })
        ]);
      }
    },
    init: function() {
      let obj=this;
      if (obj.initialized) {
        return;
      }
      obj.initialized = true;

      // Cache preferences
      usePreferences.getState().cache();
      setupPreferenceBroadcast();

      setTimeout(function() {
        obj?.editor?.setValue('-- ' + select_object_msg);
        obj?.editor?.refresh();
      }, 10);

      // Register scripts and add menus
      pgBrowser.utils.registerScripts(this);

      // Ping the server every 5 minutes
      setInterval(function() {
        getApiInstance().post(
          url_for('misc.cleanup')
        ).then(()=> {
          /*This is intentional (SonarQube)*/
        }).catch(function() {
          /*This is intentional (SonarQube)*/
        });
      }, 300000);

      obj.Events.on(
        'pgadmin:server:connected', send_heartbeat.bind(obj)
      );

      obj.Events.on(
        'pgadmin:server:disconnect', stop_heartbeat.bind(obj)
      );

      obj.check_corrupted_db_file();
      obj.Events.on('pgadmin:browser:tree:add', obj.onAddTreeNode.bind(obj));
      obj.Events.on('pgadmin:browser:tree:update', obj.onUpdateTreeNode.bind(obj));
      obj.Events.on('pgadmin:browser:tree:refresh', obj.onRefreshTreeNodeReact.bind(obj));
      obj.Events.on('pgadmin-browser:tree:loadfail', obj.onLoadFailNode.bind(obj));
      obj.bind_beforeunload();

      /* User UI activity */
      obj.log_activity(); /* The starting point */
      obj.register_to_activity_listener(document);
      obj.start_inactivity_timeout_daemon();
    },
    uiloaded: function() {
      this.set_master_password('');
      this.check_version_update();
    },
    check_corrupted_db_file: function() {
      getApiInstance().get(
        url_for('browser.check_corrupted_db_file')
      ).then(({data: res})=> {
        if(res.data.length > 0) {

          pgAdmin.Browser.notifier.alert(
            'Warning',
            'pgAdmin detected unrecoverable corruption in it\'s SQLite configuration database. ' +
            'The database has been backed up and recreated with default settings. '+
            'It may be possible to recover data such as query history manually from '+
            'the original/corrupt file using a tool such as DB Browser for SQLite if desired.'+
            '<br><br>Original file: ' + res.data + '<br>Replacement file: ' +
            res.data.substring(0, res.data.length - 14),
          );
        }
      }).catch(function(error) {
        pgAdmin.Browser.notifier.alert(error);
      });
    },
    check_master_password: function(on_resp_callback) {
      getApiInstance().get(
        url_for('browser.check_master_password')
      ).then(({data: res})=> {
        if(on_resp_callback) {
          if(res.data) {
            on_resp_callback(true);
          } else {
            on_resp_callback(false);
          }
        }
      }).catch(function(error) {
        pgAdmin.Browser.notifier.pgRespErrorNotify(error);
      });
    },

    reset_master_password: function() {
      let self = this;
      getApiInstance().delete(
        url_for('browser.set_master_password')
      ).then(({data: res})=> {
        if(!res.data) {
          self.set_master_password('');
        }
      }).catch(function(error) {
        pgAdmin.Browser.notifier.pgRespErrorNotify(error);
      });
    },

    set_master_password: function(password='',
      set_callback=()=>{/*This is intentional (SonarQube)*/},
      cancel_callback=()=>{/*This is intentional (SonarQube)*/}) {
      let data=null, self = this;

      data = {
        'password': password,
      };

      self.masterpass_callback_queue.push(set_callback);
      // Check master passowrd.
      checkMasterPassword(data, self.masterpass_callback_queue, cancel_callback);
    },

    check_version_update: function() {
      getApiInstance().get(
        url_for('misc.upgrade_check')
      ).then((res)=> {
        const data = res.data.data;
        if(data.outdated) {
          pgAdmin.Browser.notifier.warning(
            `
            ${gettext('You are currently running version %s of %s, <br/>however the current version is %s.', data.current_version, data.product_name, data.upgrade_version)}
            <br/><br/>
            ${gettext('Please click <a href="%s" target="_new" style="color:inherit">here</a> for more information.', data.download_url)}
            `,
            null
          );
        }

      }).catch(function() {
        // Suppress any errors
      });
    },

    bind_beforeunload: function() {
      window.addEventListener('beforeunload', function(e) {
        /* Can open you in new tab */
        const prefStore = usePreferences.getState();
        let tree_save_interval = prefStore.getPreferences('browser', 'browser_tree_state_save_interval'),
          confirm_on_refresh_close = prefStore.getPreferences('browser', 'confirm_on_refresh_close');

        if (!_.isUndefined(tree_save_interval) && tree_save_interval.value !== -1)
          pgAdmin.Browser.browserTreeState.save_state();

        if(!_.isUndefined(confirm_on_refresh_close) && confirm_on_refresh_close.value) {
          /* This message will not be displayed in Chrome, Firefox, Safari as they have disabled it*/
          let msg = gettext('Are you sure you want to close the %s browser?', pgBrowser.utils.app_name);
          if(e.originalEvent?.returnValue) {
            e.originalEvent.returnValue = msg;
          }
          return msg;
        }
      });
    },

    add_menu_category: function(
      id, label, priority, icon, above_separator, below_separator, single
    ) {
      this.menu_categories[id] = {
        label: label,
        priority: priority,
        icon: icon,
        above: (above_separator === true),
        below: (below_separator === true),
        single: single,
      };
    },

    // Add menus of module/extension at appropriate menu
    add_menus: function(menus) {
      let pgMenu = this.all_menus_cache;

      _.each(menus, function(m) {
        _.each(m.applies, function(a) {
          /* We do support menu type only from this list */
          if(['context', 'file', 'edit', 'object','management', 'tools', 'help'].indexOf(a) > -1){
            let _menus;

            // If current node is not visible in browser tree
            // then return from here
            if(!checkNodeVisibility(m.node)) {
              return;
            } else if(_.has(m, 'module') && !_.isUndefined(m.module)) {
              // If module to which this menu applies is not visible in
              // browser tree then also we do not display menu
              if(!checkNodeVisibility(m.module.type)) {
                return;
              }
            }

            pgMenu[a] = pgMenu[a] || {};
            if (_.isString(m.node)) {
              _menus = pgMenu[a][m.node] = pgMenu[a][m.node] || {};
            } else if (_.isString(m.category)) {
              _menus = pgMenu[a][m.category] = pgMenu[a][m.category] || {};
            }
            else {
              _menus = pgMenu[a];
            }

            let get_menuitem_obj = function(_m) {
              let enable = _m.enable;
              if(_m.enable == '') {
                enable = true;
              } else if(_.isString(_m.enable) && _m.enable.toLowerCase() == 'false') {
                enable = false;
              }

              // This is to handel quick search callback
              if(_m.name == 'mnu_quick_search_help') {
                _m.callback = () => {
                  showQuickSearch();
                };
              }

              return MainMenuFactory.createMenuItem({
                name: _m.name,
                label: _m.label,
                module: _m.module,
                category: _m.category,
                callback: typeof _m.module == 'object' && _m.module[_m.callback] && _m.callback in _m.module[_m.callback] ? _m.module[_m.callback] : _m.callback,
                priority: _m.priority,
                data: _m.data,
                url: _m.url || '#',
                target: _m.target,
                icon: _m.icon,
                enable: enable || true,
                node: _m.node,
                checked: _m.checked,
                below: _m.below,
                applies: _m.applies,
              });
            };

            if (!_.has(_menus, m.name)) {
              _menus[m.name] = get_menuitem_obj(m);

              if(m.menu_items) {
                let sub_menu_items = [];

                for(let mnu_val of m.menu_items) {
                  sub_menu_items.push(get_menuitem_obj(mnu_val));
                }
                _menus[m.name]['menu_items'] = sub_menu_items;
              }
            }
          } else  {
            console.warn(
              'Developer warning: Category \'' +
                  a +
                  '\' is not supported!\nSupported categories are: context, file, edit, object, tools, management, help'
            );
          }
        });
      });
    },
    _findTreeChildNode: function(_i, _d, _o) {
      let loaded = _o.t.wasLoad(_i),
        onLoad = function() {
          let items = _o.t.children(_i),
            i, d, n, idx = 0, size = items.length;
          for (; idx < size; idx++) {
            i = items[idx];
            d = _o.t.itemData(i);
            if (d._type === _d._type) {
              if (!_o.hasId || d._id == _d._id) {
                _o.i = i;
                _o.d = _d;
                _o.pathOfTreeItems.push({coll: false, item: i, d: _d});

                _o.success();
                return;
              }
            } else {
              n = _o.b.Nodes[d._type];
              // Are we looking at the collection node for the given node?
              if (
                n?.collection_node && d.nodes &&
                  _.indexOf(d.nodes, _d._type) != -1
              ) {
                _o.i = i;
                _o.d = null;
                _o.pathOfTreeItems.push({coll: true, item: i, d: d});

                // Set load to false when the current collection node's inode is false
                if (!_o.t.isInode(i)) {
                  _o.load = false;
                }
                _o.b._findTreeChildNode(i, _d, _o);
                return;
              }
            }
          }
          _o.notFound && typeof(_o.notFound) == 'function' &&
            _o.notFound(_d);
        };

      if (!loaded && _o.load) {
        _o.t.open(_i).then(
          () => {
            onLoad();
          },
          () => {
            let fail = _o?.o?.fail;
            if (fail && typeof(fail) == 'function') {
              fail.apply(_o.t, []);
            }
          }
        );
      } else if (loaded) {
        onLoad();
      } else {
        _o.notFound && typeof(_o.notFound) == 'function' &&
          _o.notFound(_d);
      }
    },

    onAddTreeNode: function(_data, _hierarchy, _opts) {
      let ctx = {
          b: this, // Browser
          d: null, // current parent
          hasId: true,
          i: null, // current item
          p: _.toArray(_hierarchy || {}).sort(
            function(a, b) {
              if (a.priority === b.priority) {
                return 0;
              }
              return (a.priority < b.priority ? -1 : 1);
            }
          ), // path of the parent
          pathOfTreeItems: [], // path Item
          t: this.tree, // Tree Api
          o: _opts,
        },
        traversePath = function() {
          let _ctx = this, data;

          _ctx.success = traversePath;
          if (_ctx.p.length) {
            data = _ctx.p.shift();
            // This is the parent node.
            // Replace the parent-id of the data, which could be different
            // from the given hierarchy.
            if (!_ctx.p.length) {
              data._id = _data._pid;
              _ctx.success = addItemNode;
            }
            _ctx.b._findTreeChildNode(
              _ctx.i, data, _ctx
            );
            // if parent node is null
            if (!_data._pid) {
              addItemNode.apply(_ctx, arguments);
            }
          }
          return true;
        }.bind(ctx),
        addItemNode = function() {
          // Append the new data in the tree under the current item.
          // We may need to pass it to proper collection node.
          let _ctx = this,
            first = (_ctx.i || this.t.wasLoad(_ctx.i)) &&
            this.t.first(_ctx.i),
            findChildNode = function(success, notFound) {
              let __ctx = this;
              __ctx.success = success;
              __ctx.notFound = notFound;

              __ctx.b._findTreeChildNode(__ctx.i, _data, __ctx);
            }.bind(_ctx),
            selectNode = function() {
              this.t.openPath(this.i);
              this.t.select(this.i);
              if (typeof(_ctx?.o?.success) == 'function') {
                _ctx.o.success.apply(_ctx.t, [_ctx.i, _data]);
              }
            }.bind(_ctx),
            addNode = function() {
              let __ctx = this,
                items = __ctx.t.children(__ctx.i),
                s = 0, e = items.length - 1, i,
                linearSearch = function() {
                  while (e >= s) {
                    i = items[s];
                    let d = __ctx.t.itemData(i);
                    if (d._type === 'column') {
                      if (pgAdmin.numeric_comparator(d._id, _data._id) == 1)
                        return true;
                    } else if (pgAdmin.natural_sort(d._label, _data._label) == 1)
                      return true;
                    s++;
                  }
                  //when the current element is greater than the end element
                  if (e != items.length - 1) {
                    i = items[e+1];
                    return true;
                  }
                  i = null;
                  return false;
                },
                binarySearch = function() {
                  let d, m;
                  // Binary search only outperforms Linear search for n > 44.
                  // Reference:
                  // https://en.wikipedia.org/wiki/Binary_search_algorithm#cite_note-30
                  //
                  // We will try until it's half.
                  while (e - s > 22) {
                    i = items[s];
                    d = __ctx.t.itemData(i);
                    if (d._type === 'column') {
                      if (pgAdmin.numeric_comparator(d._id, _data._id) != -1)
                        return true;
                    } else if (pgAdmin.natural_sort(d._label, _data._label) != -1)
                      return true;
                    i = items[e];
                    d = __ctx.t.itemData(i);
                    let result;
                    if (d._type === 'column') {
                      result = pgAdmin.numeric_comparator(d._id, _data._id);
                    } else {
                      result = pgAdmin.natural_sort(d._label, _data._label);
                    }
                    if (result !=1) {
                      if (e != items.length - 1) {
                        i = items[e+1];
                        return true;
                      }
                      i = null;
                      return false;
                    }
                    m = s + Math.round((e - s) / 2);
                    i = items[m];
                    d = __ctx.t.itemData(i);
                    if(d._type === 'column'){
                      result = pgAdmin.numeric_comparator(d._id, _data._id);
                    } else {
                      result = pgAdmin.natural_sort(d._label, _data._label);
                    }
                    //result will never become 0 because of remove operation
                    //which happens with updateTreeNode
                    if (result == 0)
                      return true;

                    if (result == -1) {
                      s = m + 1;
                      e--;
                    } else {
                      s++;
                      e = m - 1;
                    }
                  }
                  return linearSearch();
                };

              if (binarySearch()) {
                __ctx.t.before(i, _data).then((_item) => {
                  if (typeof(__ctx?.o?.success) == 'function') {
                    __ctx.o.success.apply(__ctx.t, [i, _data]);
                  } else {
                    __ctx.t.select(_item);
                  }
                }, () => {
                  console.warn('Failed to add before...', arguments);
                  if (typeof(__ctx.o?.fail) == 'function') {
                    __ctx.o.fail.apply(__ctx.t, [i, _data]);
                  }
                });
              } else {
                let _append = function() {
                  let ___ctx = this,
                    _parent_data = ___ctx.t.itemData(___ctx.i);

                  ___ctx.t.append(___ctx.i, _data).then(
                    (_i) => {
                      // Open the item path only if its parent is loaded
                      // or parent type is same as nodes
                      if(
                        ___ctx.t.wasLoad(___ctx.i) &&
                          _parent_data &&  _parent_data._type.search(
                          _data._type
                        ) > -1
                      ) {
                        ___ctx.t.open(___ctx.i);
                        ___ctx.t.select(_i);
                      } else if (_parent_data) {
                        // Unload the parent node so that we'll get
                        // latest data when we try to expand it
                        ___ctx.t.unload(___ctx.i).then(
                          () => {
                            ___ctx.t.open(___ctx.i);
                          }
                        );
                      }
                      if (typeof(___ctx?.o?.success) == 'function') {
                        ___ctx.o.success.apply(___ctx.t, [_i, _data]);
                      }
                    },
                    () => {
                      console.warn('Failed to append...', arguments);
                      if (typeof(___ctx?.o?.fail) == 'function') {
                        ___ctx.o.fail.apply(___ctx.t, [___ctx.i, _data]);
                      }
                    }
                  );
                }.bind(__ctx);

                if (__ctx.i && !__ctx.t.isInode(__ctx.i)) {
                  __ctx.t.setInode(__ctx.i).then(
                    () => {
                      _append();
                    }
                  );
                } else {
                  // Handle case for node without parent i.e. server-group
                  // or if parent node's inode is true.
                  _append();
                }
              }
            }.bind(_ctx);

          // Parent node do not have any children, let me unload it.
          if (!first && _ctx.t.wasLoad(_ctx.i)) {
            _ctx.t.unload(_ctx.i).then(() => {
              findChildNode(
                selectNode,
                function() {
                  let o = this && this.o;
                  if (typeof(o?.fail) == 'function') {
                    o.fail.apply(this.t, [this.i, _data]);
                  }
                }.bind(this)
              );
            },
            () => {
              let o = this && this.o;
              if (typeof(o?.fail) == 'function') {
                o.fail.apply(this.t, [this.i, _data]);
              }
            });
            return;
          }

          // We can find the collection node using _findTreeChildNode
          // indirectly.
          findChildNode(selectNode, addNode);
        }.bind(ctx);

      if (!ctx.t.wasInit() || !_data) {
        return;
      }

      traversePath();
    },

    onUpdateTreeNode: function(_old, _new, _hierarchy, _opts) {
      let ctx = {
          b: this, // Browser
          d: null, // current parent
          i: null, // current item
          hasId: true,
          p: _.toArray(_hierarchy || {}).sort(
            function(a, b) {
              if (a.priority === b.priority) {
                return 0;
              }
              return (a.priority < b.priority ? -1 : 1);
            }
          ), // path of the old object
          pathOfTreeItems: [], // path items
          t: this.tree, // Tree Api
          o: _opts,
          load: true,
          old: _old,
          new: _new,
          op: null,
        },
        errorOut = function() {
          let fail = this.o?.fail;
          if (fail && typeof(fail) == 'function') {
            fail.apply(this.t, [this.i, _new, _old]);
          }
        }.bind(ctx),
        deleteNode = function() {
          let self = this,
            pathOfTreeItems = this.pathOfTreeItems,
            findParent = function() {
              if (pathOfTreeItems.length) {
                pathOfTreeItems.pop();
                let length = pathOfTreeItems.length;
                this.i = (length && pathOfTreeItems[length - 1].item) || null;
                this.d = (length && pathOfTreeItems[length - 1].d) || null;

                // It is a collection item, let's find the node item
                if (length && pathOfTreeItems[length - 1].coll) {
                  pathOfTreeItems.pop();
                  length = pathOfTreeItems.length;
                  this.i = (length && pathOfTreeItems[length - 1].item) || null;
                  this.d = (length && pathOfTreeItems[length - 1].d) || null;
                }
              } else {
                this.i = null;
                this.d = null;
              }
            }.bind(this);

          let _item_parent = (this.i
            && this.t.hasParent(this.i)
              && this.t.parent(this.i)) || null,
            _item_grand_parent = _item_parent ?
              (this.t.hasParent(_item_parent)
              && this.t.parent(_item_parent) &&
              _item_parent.root != this.t.parent(_item_parent))
              : null;

          // Remove the current node first.
          if (
            this.i && this.d && this.old._id == this.d._id &&
              this.old._type == this.d._type
          ) {
            let _parent = this.t.parent(this.i) || null;

            // If there is no parent then just update the node
            if(this.t.isRootNode(_parent) ||
             (_parent && _parent.length == 0 && ctx.op == 'UPDATE')) {
              //Update node if browser has single child node.
              if(this.t.children().length === 1) {
                updateNode();
              } else {
                let that = this;
                this.t.remove(this.i).then(() => {
                  that.t.before(that.i, that.new).then((new_item) => {
                    that.t.select(new_item);
                  }, () => {
                    console.warn('Failed to add before..', arguments);
                  });
                });
              }
            } else {
              let postRemove = function() {
                // If item has parent but no grand parent
                if (_item_parent.path !== '/browser' && !_item_grand_parent) {
                  let parent = null;
                  // We need to search in all parent siblings (eg: server groups)
                  let parents = this.t.siblings(this.i) || [];
                  parents.push(this.i);
                  _.each(parents, function (p) {
                    let d = self.t.itemData(p);
                    // If new server group found then assign it parent
                    if(d._id == self.new._pid) {
                      parent = p;
                      self.pathOfTreeItems.push({coll: true, item: parent, d: d});
                    }
                  });

                  if (parent) {
                    this.load = true;

                    this.success = function() {
                      addItemNode();
                    }.bind(this);
                    // We can refresh the collection node, but - let's not bother about
                    // it right now.
                    this.notFound = errorOut;

                    let loaded = this.t.wasLoad(parent),
                      onLoad = function() {
                        self.i = parent;
                        self.pathOfTreeItems.push({coll: false, item: parent, d: self.d});
                        self.success();
                      };

                    if (!loaded && self.load) {
                      self.t.open(parent).then(
                        () => {
                          onLoad();
                        },
                        () => {
                          let fail = self?.o?.fail;
                          if (
                            fail && typeof(fail) == 'function'
                          ) {
                            fail.apply(self.t, []);
                          }
                        }
                      );
                    } else {
                      onLoad();
                    }
                  }
                } else {
                  // This is for rest of the nodes
                  let _parentData = this.d;
                  // Find the grand-parent, or the collection node of parent.
                  findParent();

                  if (this.i) {
                    this.load = true;

                    this.success = function() {
                      addItemNode();
                    }.bind(this);
                    // We can refresh the collection node, but - let's not bother about
                    // it right now.
                    this.notFound = errorOut;

                    // Find the new parent
                    this.b._findTreeChildNode(
                      this.i, {_id: this.new._pid, _type: _parentData._type}, this
                    );
                  } else {
                    addItemNode();
                  }
                }
              }.bind(this);

              this.t.remove(this.i).then(() => {
                findParent();
                if (_item_parent && !_item_grand_parent && _parent
                    && self.t.children(_parent).length == 0) {
                  self.t.unload(_parent).then( () => { setTimeout(postRemove); });
                }
                else {
                  setTimeout(postRemove);
                }
                return true;
              });
            }

          }
          errorOut();

        }.bind(ctx),
        findNewParent = function(_d) {
          let findParent = function() {
            let pathOfTreeItems = this.pathOfTreeItems;

            if (pathOfTreeItems.length) {
              pathOfTreeItems.pop();
              let length = pathOfTreeItems.length;
              this.i = (length && pathOfTreeItems[length - 1].item) || null;
              this.d = (length && pathOfTreeItems[length - 1].d) || null;

              // It is a collection item, let's find the node item
              if (length && pathOfTreeItems[length - 1].coll) {
                pathOfTreeItems.pop();
                length = pathOfTreeItems.length;
                this.i = (length && pathOfTreeItems[length - 1].item) || null;
                this.d = (length && pathOfTreeItems[length - 1].d) || null;
              }
            } else {
              this.i = null;
              this.d = null;
            }
          }.bind(this);

          // old parent was not found, can we find the new parent?
          if (this.i) {
            this.load = true;
            this.success = function() {
              addItemNode();
            }.bind(this);

            if (_d._type == this.old._type) {
              // We were already searching the old object under the parent.
              findParent();
              _d = this.d;
              // Find the grand parent
              findParent();
            }
            _d = this.new._pid;

            // We can refresh the collection node, but - let's not bother about
            // it right now.
            this.notFound = errorOut;

            // Find the new parent
            this.b._findTreeChildNode(this.i, _d, this);
          } else {
            addItemNode();
          }
        }.bind(ctx),
        updateNode = function() {
          if (
            this.i && this.d && this.new._type == this.d._type
          ) {
            let self = this,
              _id = this.d._id;
            if (this.new._id != this.d._id) {
              // Found the new oid, update its node_id
              let node_data = this.t.itemData(ctx.i);
              node_data._id = _id = this.new._id;
            }
            if (this.new._id == _id) {
              // Found the current
              _.extend(this.d, this.new);
              this.t.update(ctx.i, this.d);
              this.t.setLabel(ctx.i, {label: this.new.label});
              this.t.addIcon(ctx.i, {icon: this.new.icon});
              this.t.setId(ctx.i, {id: this.new.id});
              this.t.openPath(this.i);
              this.t.deselect(this.i);

              // select tree item
              self.t.select(self.i);
            }
          }
          let success = this.o?.success;
          if (success && typeof(success) == 'function') {
            success.apply(this.t, [this.i, _old, _new]);
          }
        }.bind(ctx),
        traversePath = function() {
          let _ctx = this, data;

          _ctx.success = traversePath;
          if (_ctx.p.length) {
            data = _ctx.p.shift();
            // This is the node, we can now do the required operations.
            // We should first delete the existing node, if the parent-id is
            // different.
            if (!_ctx.p.length) {
              if (_ctx.op == 'RECREATE') {
                _ctx.load = false;
                _ctx.success = deleteNode;
                _ctx.notFound = findNewParent;
              } else {
                _ctx.success = updateNode;
                _ctx.notFound = errorOut;
              }
            }
            _ctx.b._findTreeChildNode(
              _ctx.i, data, _ctx
            );
          } else if (_ctx.p.length == 1) {
            _ctx.notFound = findNewParent;
          }
          return true;
        }.bind(ctx),
        addItemNode = function() {
          let _ctx = this,
            first = (_ctx.i || this.t.wasLoad(_ctx.i)) &&
            this.t.first(_ctx.i),
            findChildNode = function(success, notFound) {
              let __ctx = this;
              __ctx.success = success;
              __ctx.notFound = notFound;

              __ctx.b._findTreeChildNode(__ctx.i, _new, __ctx);
            }.bind(_ctx),
            selectNode = function() {
              this.t.openPath(this.i);
              this.t.select(this.i);
              if (
                _ctx.o && _ctx.o.success && typeof(_ctx.o.success) == 'function'
              ) {
                _ctx.o.success.apply(_ctx.t, [_ctx.i, _new]);
              }
            }.bind(_ctx),
            addNode = function() {
              let __ctx = this,
                items = __ctx.t.children(__ctx.i),
                s = 0, e = items.length - 1, i,
                linearSearch = function() {
                  while (e >= s) {
                    i = items[s];
                    let d = __ctx.t.itemData(i);
                    if (d._type === 'column') {
                      if (pgAdmin.numeric_comparator(d._id, _new._id) == 1)
                        return true;
                    } else if (pgAdmin.natural_sort(d._label, _new._label) == 1)
                      return true;
                    s++;
                  }
                  if (e != items.length - 1) {
                    i = items[e+1];
                    return true;
                  }
                  i = null;
                  return false;
                },
                binarySearch = function() {
                  while (e - s > 22) {
                    i = items[s];
                    let d = __ctx.t.itemData(i);
                    if (d._type === 'column') {
                      if (pgAdmin.numeric_comparator(d._id, _new._id) != -1)
                        return true;
                    } else if (pgAdmin.natural_sort(d._label, _new._label) != -1)
                      return true;
                    i = items[e];
                    d = __ctx.t.itemData(i);
                    let result;
                    if (d._type === 'column') {
                      result = pgAdmin.numeric_comparator(d._id, _new._id);
                    } else {
                      result = pgAdmin.natural_sort(d._label, _new._label);
                    }
                    if (result !=1) {
                      if (e != items.length - 1) {
                        i = items[e+1];
                        return true;
                      }
                      i = null;
                      return false;
                    }
                    let m = s + Math.round((e - s) / 2);
                    i = items[m];
                    d = __ctx.t.itemData(i);
                    if(d._type === 'column'){
                      result = pgAdmin.numeric_comparator(d._id, _new._id);
                    } else {
                      result = pgAdmin.natural_sort(d._label, _new._label);
                    }
                    //result will never become 0 because of remove operation
                    //which happens with updateTreeNode
                    if (result == 0)
                      return true;

                    if (result == -1) {
                      s = m + 1;
                      e--;
                    } else {
                      s++;
                      e = m - 1;
                    }
                  }
                  return linearSearch();
                };

              if (binarySearch()) {
                __ctx.t.before(i, _new).then((new_item) => {
                  __ctx.t.openPath(new_item);
                  __ctx.t.select(new_item);
                  if (typeof(__ctx?.o?.success) == 'function') {
                    __ctx.o.success.apply(__ctx.t, [i, _old, _new]);
                  }
                }, () => {
                  console.warn('Failed to add before..', arguments);
                  if (typeof(__ctx?.o?.fail) == 'function') {
                    __ctx.o.fail.apply(__ctx.t, [i, _old, _new]);
                  }
                });
              } else {
                let _appendNode = function() {
                  __ctx.t.append(__ctx.i, _new).then(
                    (new_item) => {
                      __ctx.t.openPath(new_item);
                      __ctx.t.select(new_item);
                      if (typeof(__ctx?.o?.success) == 'function') {
                        __ctx.o.success.apply(__ctx.t, [__ctx.i, _old, _new]);
                      }
                    },
                    () => {
                      console.warn('Failed to append...', arguments);
                      if (typeof(__ctx?.o?.fail) == 'function') {
                        __ctx.o.fail.apply(__ctx.t, [__ctx.i, _old, _new]);
                      }
                    },
                  );
                };

                // If the current node's inode is false
                if (__ctx.i && !__ctx.t.isInode(__ctx.i)) {
                  __ctx.t.setInode(__ctx.i, {success: _appendNode});
                  // Open the collection node.
                  pgBrowser.tree.open(__ctx.i);
                } else {
                  // Handle case for node without parent i.e. server-group
                  // or if parent node's inode is true.
                  _appendNode();
                }

              }
            }.bind(_ctx);

          // Parent node do not have any children, let me unload it.
          if (!first && _ctx.t.wasLoad(_ctx.i)) {
            _ctx.t.unload(_ctx.i).then( () => {
              findChildNode(
                selectNode,
                function() {
                  let o = this && this.o;
                  if (typeof(o?.fail) == 'function') {
                    o.fail.apply(this.t, [this.i, _old, _new]);
                  }
                }
              );
            },
            () => {
              let o = this && this.o;
              if (typeof(o?.fail) == 'function') {
                o.fail.apply(this.t, [this.i, _old, _new]);
              }
            }
            );
            return;
          }

          // We can find the collection node using _findTreeChildNode
          // indirectly.
          findChildNode(selectNode, addNode);
        }.bind(ctx);

      if (!ctx.t.wasInit() || !_new || !_old) {
        return;
      }
      ctx.pathOfTreeItems.push(_old);
      _new._label = _new.label;
      _new.label = _.escape(_new.label);

      // We need to check if this is collection node and have
      // children count, if yes then add count with label too
      if(ctx.b.Nodes[_new._type].is_collection) {
        if ('collection_count' in _old && _old['collection_count'] > 0) {
          _new.label = _.escape(_new._label) +
            ' <span>(' + _old['collection_count'] + ')</span>';
        }
      }

      // If server icon/background changes then also we need to re-create it
      if ((
        _old._type == 'server' && _new._type == 'server' && (
          _old._pid != _new._pid || _old.icon != _new.icon
        )) || _old._pid != _new._pid || _old._id != _new._id
      ) {
        ctx.op = 'RECREATE';
        traversePath();
      } else {
        ctx.op = 'UPDATE';
        traversePath();
      }
    },

    onRefreshTreeNodeReact: function(_i, _opts) {
      this.tree.refresh(_i).then(() =>{
        if (_opts?.success) _opts.success();
      });
    },

    onRefreshTreeNode: function(_i, _opts) {
      let _d = _i && this.tree.itemData(_i),
        n = this.Nodes[_d?._type],
        ctx = {
          b: this, // Browser
          d: _d, // current parent
          i: _i, // current item
          p: null, // path of the old object
          pathOfTreeItems: [], // path items
          t: this.tree, // Tree Api
          o: _opts,
        },
        isOpen,
        idx = -1;

      this.Events.trigger('pgadmin-browser:tree:refreshing', _i, _d, n);

      if (!n) {
        _i = null;
        ctx.i = null;
        ctx.d = null;
      } else {
        isOpen = (this.tree.isInode(_i) && this.tree.isOpen(_i));
      }

      ctx.branch = ctx.t.serialize(
        _i, {}, function(i, el, d) {
          idx++;
          if (!idx || (d.inode && d.open)) {
            return {
              _id: d._id, _type: d._type, branch: d.branch, open: d.open,
            };
          }
        });

      if (!n) {
        ctx.t.destroy({
          success: function() {
            ctx.t = ctx.b.tree;
            ctx.i = null;
            ctx.b._refreshNode(ctx, ctx.branch);
          },
          error: function() {
            let fail = (_opts.o && _opts.o.fail) || _opts.fail;

            if (typeof(fail) == 'function') {
              fail();
            }
          },
        });
        return;
      }

      let api = getApiInstance();
      let fetchNodeInfo = function(__i, __d, __n) {
        let info = __n.getTreeNodeHierarchy(__i),
          url = __n.generate_url(__i, 'nodes', __d, true);

        api.get(
          url
        ).then(({data: res})=> {
          // Node information can come as result/data
          let newData = res.result || res.data;

          newData._label = newData.label;
          newData.label = _.escape(newData.label);

          ctx.t.setLabel(ctx.i, {label: newData.label});
          ctx.t.addIcon(ctx.i, {icon: newData.icon});
          ctx.t.setId(ctx.i, {id: newData.id});
          if (newData.inode)
            ctx.t.setInode(ctx.i, {inode: true});

          // This will update the tree item data.
          let itemData = ctx.t.itemData(ctx.i);
          _.extend(itemData, newData);

          if (
            __n.can_expand && typeof(__n.can_expand) == 'function'
          ) {
            if (!__n.can_expand(itemData)) {
              ctx.t.unload(ctx.i);
              return;
            }
          }
          ctx.b._refreshNode(ctx, ctx.branch);
          let success = (ctx?.o?.success) || ctx.success;
          if (success && typeof(success) == 'function') {
            success();
          }
        }).catch(function(error) {
          if (!pgHandleItemError(
            error, {item: __i, info: info}
          )) {
            if(error.response.headers['content-type'] == 'application/json') {
              let jsonResp = error.response.data ?? {};
              if(error.response.status == 410 && jsonResp.success == 0) {
                let parent = ctx.t.parent(ctx.i);

                ctx.t.remove(ctx.i, {
                  success: function() {
                    if (parent) {
                    // Try to refresh the parent on error
                      try {
                        pgBrowser.Events.trigger(
                          'pgadmin:browser:tree:refresh', parent
                        );
                      } catch (e) { console.warn(e.stack || e); }
                    }
                  },
                });
              }
            }

            pgAdmin.Browser.notifier.pgNotifier('error', error, gettext('Error retrieving details for the node.'), function (msg) {
              if (msg == 'CRYPTKEY_SET') {
                fetchNodeInfo(__i, __d, __n);
              } else {
                console.warn(arguments);
              }
            });
          }
        });
      }.bind(this);

      if (n?.collection_node) {
        let p = ctx.i = this.tree.parent(_i),
          unloadNode = function() {
            this.tree.unload(_i, {
              success: function() {
                _i = p;
                _d = ctx.d = ctx.t.itemData(ctx.i);
                n = ctx.b.Nodes[_d._type];
                _i = p;
                fetchNodeInfo(_i, _d, n);
              },
              fail: function() { console.warn(arguments); },
            });
          }.bind(this);
        if (!this.tree.isInode(_i)) {
          this.tree.setInode(_i, { success: unloadNode });
        } else {
          unloadNode();
        }
      } else if (isOpen) {
        this.tree.unload(_i, {
          success: fetchNodeInfo.bind(this, _i, _d, n),
          fail: function() {
            console.warn(arguments);
          },
        });
      } else if (!this.tree.isInode(_i) && _d.inode) {
        this.tree.setInode(_i, {
          success: fetchNodeInfo.bind(this, _i, _d, n),
          fail: function() {
            console.warn(arguments);
          },
        });
      } else {
        fetchNodeInfo(_i, _d, n);
      }
    },

    onLoadFailNode: function(_nodeData) {
      let self = this,
        isSelected = self.tree.isSelected(_nodeData);

      /** Check if master password set **/
      self.check_master_password((is_set)=>{
        if(!is_set) {
          self.set_master_password('', ()=>{
            if(isSelected) { self.tree.select(_nodeData); }
            self.tree.open(_nodeData);
          });
        }
      });
    },

    removeChildTreeNodesById: function(_parentNode, _collType, _childIds) {
      let tree_local = pgBrowser.tree, childNode, childNodeData;
      if(_parentNode && _collType) {
        let children = tree_local.children(_parentNode),
          idx = 0, size = children.length;

        _parentNode = null;

        for (; idx < size; idx++) {
          childNode = children[idx];
          childNodeData = tree_local.itemData(childNode);

          if (childNodeData._type == _collType) {
            _parentNode = childNode;
            break;
          }
        }
      }

      if (_parentNode) {
        let children = tree_local.children(_parentNode),
          idx = 0, size = children.length;

        for (; idx < size; idx++) {
          childNode = children[idx];
          childNodeData = tree_local.itemData(childNode);

          if (_childIds.indexOf(childNodeData._id) != -1) {
            pgBrowser.removeTreeNode(childNode, false, _parentNode);
          }
        }
        return true;
      }
      return false;
    },

    removeTreeNode: function(_node, _selectNext, _parentNode) {
      let tree_local = pgBrowser.tree,
        nodeToSelect = null;

      if (!_node)
        return false;

      if (_selectNext) {
        nodeToSelect = tree_local.next(_node);
        if (!nodeToSelect) {
          nodeToSelect = tree_local.prev(_node);

          if (!nodeToSelect) {
            if (!_parentNode) {
              nodeToSelect = tree_local.parent(_node);
            } else {
              nodeToSelect = _parentNode;
            }
          }
        }
        if (nodeToSelect)
          tree_local.select(nodeToSelect);
      }

      tree_local.remove(_node);
      return true;
    },

    findSiblingTreeNode: function(_node, _id) {
      let tree_local = pgBrowser.tree,
        parentNode = tree_local.parent(_node),
        siblings = tree_local.children(parentNode),
        idx = 0, nodeData, node;

      for(; idx < siblings.length; idx++) {
        node = siblings[idx];
        nodeData = tree_local.itemData(node);

        if (nodeData && nodeData._id == _id)
          return node;
      }
      return null;
    },

    findParentTreeNodeByType: function(_node, _parentType) {
      let tree_local = pgBrowser.tree,
        nodeData,
        node = _node;

      do {
        nodeData = tree_local.itemData(node);
        if (nodeData && nodeData._type == _parentType)
          return node;
        node = tree_local.hasParent(node) ? tree_local.parent(node) : null;
      } while (node);

      return null;
    },

    findChildCollectionTreeNode: function(_node, _collType) {
      let tree_local = pgBrowser.tree,
        nodeData, idx = 0,
        node,
        children = _node && tree_local.children(_node);

      if (!children?.length)
        return null;

      for(; idx < children.length; idx++) {
        node = children[idx];
        nodeData = tree_local.itemData(node);

        if (nodeData && nodeData._type == _collType)
          return node;
      }
      return null;
    },

    addChildTreeNodes: function(_treeHierarchy, _node, _type, _arrayIds, _callback) {
      let api = getApiInstance();
      let module = _type in pgBrowser.Nodes && pgBrowser.Nodes[_type],
        childTreeInfo = _arrayIds.length && _.extend(
          {}, _.mapValues(_treeHierarchy, function(_val) {
            _val.priority -= 1; return _val;
          })),
        arrayChildNodeData = [],
        fetchNodeInfo = function(__callback) {
          if (!_arrayIds.length) {
            if (__callback) {
              __callback();
            }
            return;
          }

          let childDummyInfo = {
              '_id': _arrayIds.pop(), '_type': _type, 'priority': 0,
            },
            childNodeUrl;

          childTreeInfo[_type] = childDummyInfo;
          childNodeUrl = module.generate_url(
            null, 'nodes', childDummyInfo, true, childTreeInfo
          );

          _node = _node || arguments[1];

          api.get(
            childNodeUrl
          ).then(({data: res})=> {
            if (res.success) {
              arrayChildNodeData.push(res.data);
            }
            fetchNodeInfo(_callback);
          }).catch(function(error) {
            pgAdmin.Browser.notifier.pgRespErrorNotify(error);
            fetchNodeInfo(_callback);
          });
        };


      if (!module) {
        console.warn(
          'Developer: Couldn\'t find the module for the given child: ',
          _.clone(arguments)
        );
        return;
      }

      if (pgBrowser.tree.wasLoad(_node) || pgBrowser.tree.isLeaf(_node)) {
        fetchNodeInfo(function() {
          _.each(arrayChildNodeData, function(_nodData) {
            pgBrowser.Events.trigger(
              'pgadmin:browser:tree:add', _nodData, _treeHierarchy
            );
          });

          _callback?.();
        });
      } else {
        _callback?.();
      }
    },

    _refreshNode: function(_ctx, _d) {
      let traverseNodes = function(__d) {
        let __ctx = this, idx = 0, ctx, d,
          size = (__d.branch && __d.branch.length) || 0,
          findNode = function(i_findNode, d_findNode, ctx_findNode) {
            setTimeout(
              function() {
                ctx_findNode.b._findTreeChildNode(i_findNode, d_findNode, ctx_findNode);
              }, 0
            );
          };

        for (; idx < size; idx++) {
          d = __d.branch[idx];
          let n = __ctx.b.Nodes[d._type];
          ctx = {
            b: __ctx.b,
            t: __ctx.t,
            pathOfTreeItems: [],
            i: __ctx.i,
            d: d,
            select: __ctx.select,
            hasId: n && !n.collection_node,
            o: __ctx.o,
            load: true,
          };
          ctx.success = function() {
            this.b._refreshNode(this, this.d);
          }.bind(ctx);
          findNode(__ctx.i, d, ctx);
        }
      }.bind(_ctx, _d);

      if (!_d || !_d.open)
        return;

      if (!_ctx.t.isOpen(_ctx.i)) {
        _ctx.t.open(_ctx.i, {
          unanimated: true,
          success: traverseNodes,
          fail: function() { /* Do nothing */ },
        });
      } else {
        traverseNodes();
      }

    },

    editor_shortcut_keys: {
      // Autocomplete sql command
      'Ctrl-Space': 'autocomplete',
      'Cmd-Space': 'autocomplete',

      'Alt-Up': 'goLineUp',
      'Alt-Down': 'goLineDown',

      // Move word by word left/right
      'Ctrl-Alt-Left': 'goGroupLeft',
      'Cmd-Alt-Left': 'goGroupLeft',
      'Ctrl-Alt-Right': 'goGroupRight',
      'Cmd-Alt-Right': 'goGroupRight',

      // Allow user to delete Tab(s)
      'Shift-Tab': 'indentLess',
    },
    editor_options: {
      tabSize: parseInt(pgBrowser.utils.tabSize),
      wrapCode: pgBrowser.utils.wrapCode,
      insert_pair_brackets: pgBrowser.utils.insertPairBrackets,
      brace_matching: pgBrowser.utils.braceMatching,
      indent_with_tabs: pgBrowser.utils.is_indent_with_tabs,
    },
  });

  // Use spaces instead of tab
  if (pgBrowser.utils.useSpaces == 'True') {
    pgAdmin.Browser.editor_shortcut_keys.Tab = 'insertSoftTab';
  }

  return pgAdmin.Browser;
});