-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
1621 lines (1493 loc) · 73.4 KB
/
bootstrap.js
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
/*
* This file is part of Tab Tree,
* Copyright (C) 2015 Sergey Zelentsov <[email protected]>
*/
'use strict';
/* jshint moz:true */
/* global Components, CustomizableUI, Services, SessionStore, APP_SHUTDOWN */
//const {classes: Cc, interfaces: Ci, utils: Cu} = Components; // WebStorm inspector doesn't understand destructuring assignment
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource:///modules/CustomizableUI.jsm");
var ssHack = Cu.import("resource:///modules/sessionstore/SessionStore.jsm");
var ssOrig;
const ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
const sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService);
var stringBundle = Services.strings.createBundle('chrome://tabtree/locale/global.properties?' + Math.random()); // Randomize URI to work around bug 719376
var menuVisible;
//noinspection JSUnusedGlobalSymbols
function startup(data, reason)
{
let uri = Services.io.newURI("chrome://tabtree/skin/tree.css", null, null);
sss.loadAndRegisterSheet(uri, sss.AUTHOR_SHEET);
uri = Services.io.newURI("chrome://tabtree/skin/toolbox.css", null, null);
sss.loadAndRegisterSheet(uri, sss.AUTHOR_SHEET);
// // Why do we use Proxy here? Let's see the chain how SS works:
// // <window onload="gBrowserInit.onLoad()" /> ->
// // -> Services.obs.notifyObservers(window, "browser-window-before-show", ""); ->
// // -> SessionStore.jsm ->
// // -> OBSERVING.forEach(function(aTopic) { Services.obs.addObserver(this, aTopic, true); }, this); ->
// // -> case "browser-window-before-show": this.onBeforeBrowserWindowShown(aSubject); ->
// // -> SessionStoreInternal.onLoad(aWindow); ->
// // (1) -> Services.obs.notifyObservers(null, NOTIFY_WINDOWS_RESTORED, "");
// // (2) -> or just end
// // UPD: Firefox 41+ splits SessionStoreInternal.onLoad in two - onLoad and initializeWindow
// // Here we dispatch our new event 'tt-TabsLoad'
if (ssHack.SessionStoreInternal.initializeWindow) { // Fix for Firefox 41+
ssOrig = ssHack.SessionStoreInternal.initializeWindow;
ssHack.SessionStoreInternal.initializeWindow = new Proxy(ssHack.SessionStoreInternal.initializeWindow, {
apply: function (target, thisArg, argumentsList) {
target.apply(thisArg, argumentsList); // returns nothing
let aWindow = argumentsList[0];
let event = new Event('tt-TabsLoad'); // we just added our event after this function is executed
aWindow.dispatchEvent(event);
}
});
} else { // to support Firefox before version 41
ssOrig = ssHack.SessionStoreInternal.onLoad;
ssHack.SessionStoreInternal.onLoad = new Proxy(ssHack.SessionStoreInternal.onLoad, {
apply: function (target, thisArg, argumentsList) {
target.apply(thisArg, argumentsList); // returns nothing
let aWindow = argumentsList[0];
let event = new Event('tt-TabsLoad'); // we just added our event after this function is executed
aWindow.dispatchEvent(event);
}
});
}
Services.prefs.getDefaultBranch(null).setBoolPref('extensions.tabtree.treelines', true); // setting default pref
Services.prefs.getDefaultBranch(null).setIntPref('extensions.tabtree.highlight-unloaded-tabs', 0); // setting default pref
Services.prefs.getDefaultBranch(null).setBoolPref('extensions.tabtree.dblclick', false); // setting default pref
Services.prefs.getDefaultBranch(null).setIntPref('extensions.tabtree.delay', 0); // setting default pref
Services.prefs.getDefaultBranch(null).setIntPref('extensions.tabtree.position', 0); // setting default pref // 0 - Left, 1 - Right
// migration code :
try {
Services.prefs.setBoolPref('extensions.tabtree.treelines', Services.prefs.getBoolPref('extensions.tabstree.treelines'));
Services.prefs.setIntPref('extensions.tabtree.highlight-unloaded-tabs', Services.prefs.getIntPref('extensions.tabstree.highlight-unloaded-tabs'));
Services.prefs.setBoolPref('extensions.tabtree.dblclick', Services.prefs.getBoolPref('extensions.tabstree.dblclick'));
Services.prefs.setIntPref('extensions.tabtree.delay', Services.prefs.getIntPref('extensions.tabstree.delay'));
Services.prefs.setIntPref('extensions.tabtree.position', Services.prefs.getIntPref('extensions.tabstree.position'));
Services.prefs.deleteBranch('extensions.tabstree.');
} catch (e) {
}
// - end migration code // don't forget to delete when v1.1.0 isn't in use anymore
windowListener.register();
}
//noinspection JSUnusedGlobalSymbols
function shutdown(aData, aReason)
{
if (aReason == APP_SHUTDOWN) return;
let uri = Services.io.newURI("chrome://tabtree/skin/tree.css", null, null);
if( sss.sheetRegistered(uri, sss.AUTHOR_SHEET) ) {
sss.unregisterSheet(uri, sss.AUTHOR_SHEET);
}
uri = Services.io.newURI("chrome://tabtree/skin/toolbox.css", null, null);
if( sss.sheetRegistered(uri, sss.AUTHOR_SHEET) ) {
sss.unregisterSheet(uri, sss.AUTHOR_SHEET);
}
if (ssHack.SessionStoreInternal.initializeWindow) { // Fix for Firefox 41+
ssHack.SessionStoreInternal.initializeWindow = ssOrig;
} else { // to support Firefox before version 41
ssHack.SessionStoreInternal.onLoad = ssOrig;
}
CustomizableUI.setToolbarVisibility('toolbar-menubar', menuVisible); // restoring menu visibility(in all windows)
windowListener.unregister();
}
//noinspection JSUnusedGlobalSymbols
function install(aData, aReason) { }
//noinspection JSUnusedGlobalSymbols
function uninstall(aData, aReason) { }
//noinspection JSUnusedGlobalSymbols
var windowListener = {
onOpenWindow: function (aXULWindow) {
// In Gecko 7.0 nsIDOMWindow2 has been merged into nsIDOMWindow interface.
// In Gecko 8.0 nsIDOMStorageWindow and nsIDOMWindowInternal have been merged into nsIDOMWindow interface.
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
aDOMWindow.addEventListener('tt-TabsLoad', function onTabsLoad(event) {
aDOMWindow.removeEventListener('tt-TabsLoad', onTabsLoad, false);
windowListener.loadIntoWindow(aDOMWindow);
}, false);
},
onCloseWindow: function (aXULWindow) {
// In Gecko 7.0 nsIDOMWindow2 has been merged into nsIDOMWindow interface.
// In Gecko 8.0 nsIDOMStorageWindow and nsIDOMWindowInternal have been merged into nsIDOMWindow interface.
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
if (!aDOMWindow) {
return;
}
let browser = aDOMWindow.document.querySelector('#browser');
if (!browser) {
return;
}
let sidebar = aDOMWindow.document.querySelector('#tt-sidebar');
ss.setWindowValue(aDOMWindow, 'tt-width', sidebar.width); // Remember the width of 'tt-sidebar'
// Remember the first visible row of the <tree id="tt">:
ss.setWindowValue(aDOMWindow, 'tt-first-visible-row', aDOMWindow.document.querySelector('#tt').treeBoxObject.getFirstVisibleRow().toString());
Services.prefs.removeObserver('extensions.tabtree.treelines', aDOMWindow.tt.toRemove.prefsObserver); // it can also be removed in 'unloadFromWindow'
},
onWindowTitleChange: function (aXULWindow, aNewTitle) {},
register: function () {
// Load into any existing windows
let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
let aXULWindow = XULWindows.getNext();
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
windowListener.loadIntoWindow(aDOMWindow);
}
// Listen to new windows
Services.wm.addListener(windowListener);
},
unregister: function () {
// Unload from any existing windows
let XULWindows = Services.wm.getXULWindowEnumerator(null);
while (XULWindows.hasMoreElements()) {
let aXULWindow = XULWindows.getNext();
let aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
windowListener.unloadFromWindow(aDOMWindow, aXULWindow);
}
//Stop listening so future added windows don't get this attached
Services.wm.removeListener(windowListener);
},
unloadFromWindow: function (aDOMWindow, aXULWindow) {
if (!aDOMWindow) {
return;
}
let browser = aDOMWindow.document.querySelector('#browser');
if (!browser) {
return;
}
let splitter = aDOMWindow.document.querySelector('#tt-splitter');
if (splitter) {
let sidebar = aDOMWindow.document.querySelector('#tt-sidebar');
ss.deleteWindowValue(aDOMWindow, 'tt-width', sidebar.width); // Restore the width of 'tt-sidebar' to 200px
splitter.parentNode.removeChild(splitter);
sidebar.parentNode.removeChild(sidebar);
let fullscrToggler = aDOMWindow.document.querySelector('#tt-fullscr-toggler');
fullscrToggler.parentNode.removeChild(fullscrToggler);
let panel = aDOMWindow.document.querySelector('#tt-panel');
panel.parentNode.removeChild(panel);
}
Object.keys(aDOMWindow.tt.toRestore.g).forEach( (x)=>{aDOMWindow.gBrowser[x] = aDOMWindow.tt.toRestore.g[x];} );
Object.keys(aDOMWindow.tt.toRestore.TabContextMenu).forEach( (x)=>{aDOMWindow.TabContextMenu[x] = aDOMWindow.tt.toRestore.TabContextMenu[x];} ); // only 1 at the moment - 'updateContextMenu'
aDOMWindow.gBrowser.tabContainer.removeEventListener("TabMove", aDOMWindow.tt.toRemove.eventListeners.onTabMove, false);
aDOMWindow.gBrowser.tabContainer.removeEventListener("TabSelect", aDOMWindow.tt.toRemove.eventListeners.onTabSelect, false);
aDOMWindow.gBrowser.tabContainer.removeEventListener("TabAttrModified", aDOMWindow.tt.toRemove.eventListeners.onTabAttrModified, false);
aDOMWindow.gBrowser.removeTabsProgressListener(aDOMWindow.tt.toRemove.tabsProgressListener);
aDOMWindow.removeEventListener("sizemodechange", aDOMWindow.tt.toRemove.eventListeners.onSizemodechange, false);
// Restore default title bar buttons position (Minimize, Restore/Maximize, Close):
let titlebarButtonboxContainer = aDOMWindow.document.querySelector('#titlebar-buttonbox-container');
let titlebarContent = aDOMWindow.document.querySelector('#titlebar-content');
titlebarContent.appendChild(titlebarButtonboxContainer);
if (aDOMWindow.tt.toRestore.tabsintitlebar) { // restoring 'tabsintitlebar' attr
aDOMWindow.document.documentElement.setAttribute("tabsintitlebar", "true"); // hide native titlebar
} else {
aDOMWindow.document.documentElement.removeAttribute("tabsintitlebar"); // show native titlebar
}
aDOMWindow.TabsInTitlebar._update(true); // It is needed to recalculate negative 'margin-bottom' for 'titlebar' and 'margin-bottom' for 'titlebarContainer'
Services.prefs.removeObserver('extensions.tabtree.treelines', aDOMWindow.tt.toRemove.prefsObserver); // it could be already removed in 'onCloseWindow'
delete aDOMWindow.tt;
},
loadIntoWindow: function(aDOMWindow) {
if (!aDOMWindow) {
return;
}
let browser = aDOMWindow.document.querySelector('#browser');
if (!browser) {
return;
}
let g = aDOMWindow.gBrowser;
aDOMWindow.tt = {
toRemove: {eventListeners: {}, prefsObserver: null, tabsProgressListener: null},
toRestore: {g: {}, TabContextMenu: {}, tabsintitlebar: true}
};
if (menuVisible === undefined) { // we have to remember menu visibility only once
menuVisible = aDOMWindow.document.querySelector('#toolbar-menubar').getAttribute('autohide') != 'true'; // remember menu visibility until restart
CustomizableUI.setToolbarVisibility('toolbar-menubar', false); // hiding menu(in all windows) in case there are users who don't know how to do that
}
// remember 'tabsintitlebar' attr before beginning to interact with it // default is 'true':
aDOMWindow.tt.toRestore.tabsintitlebar = aDOMWindow.document.documentElement.getAttribute('tabsintitlebar')=='true';
//////////////////// TITLE BAR STANDARD BUTTONS (Minimize, Restore/Maximize, Close) ////////////////////////////
// We can't use 'window.load' event here, because it always shows windowState==='STATE_NORMAL' even when the actual state is 'STATE_MAXIMIZED'
let navBar = aDOMWindow.document.querySelector('#nav-bar');
let titlebarButtonboxContainer = aDOMWindow.document.querySelector('#titlebar-buttonbox-container');
//let titlebarContent = aDOMWindow.document.querySelector('#titlebar-content'); // already here
let windowControls = aDOMWindow.document.querySelector('#window-controls');
switch (aDOMWindow.windowState) {
case aDOMWindow.STATE_MAXIMIZED:
navBar.appendChild(titlebarButtonboxContainer);
aDOMWindow.document.documentElement.setAttribute("tabsintitlebar", "true"); // hide native titlebar
aDOMWindow.updateTitlebarDisplay();
break;
case aDOMWindow.STATE_NORMAL:
aDOMWindow.document.documentElement.removeAttribute("tabsintitlebar"); // show native titlebar
aDOMWindow.updateTitlebarDisplay();
//titlebarContent.appendChild(titlebarButtonboxContainer); // already here
break;
case aDOMWindow.STATE_FULLSCREEN:
navBar.appendChild(windowControls);
break;
}
//////////////////// END TITLE BAR STANDARD BUTTONS (Minimize, Restore/Maximize, Close) ////////////////////////
let propsToSet;
// for "Left" position:
// <vbox id="tt-fullscr-toggler"></vbox>
// <vbox id="tt-sidebar" width="200">
// <toolbox></toolbox>
// <tree id="tt" flex="1" seltype="single" context="tabContextMenu" treelines="true" hidecolumnpicker="true"></tree>
// </vbox>
// <splitter id="tt-splitter" />
// for "Right" position:
// <splitter id="tt-splitter" />
// <vbox id="tt-fullscr-toggler"></vbox>
// <vbox id="tt-sidebar" width="200">
// <toolbox></toolbox>
// <tree id="tt" flex="1" seltype="single" context="tabContextMenu" treelines="true" hidecolumnpicker="true"></tree>
// </vbox>
////////////////////////////////////////// VBOX tt-fullscr-toggler /////////////////////////////////////////////
// <vbox id="tt-fullscr-toggler"></vbox> // I am just copying what firefox does for its 'fullscr-toggler'
let fullscrToggler = aDOMWindow.document.createElement('vbox');
fullscrToggler.setAttribute('id', 'tt-fullscr-toggler');
// added later
//////////////////////////////////////// END VBOX tt-fullscr-toggler ///////////////////////////////////////////
//////////////////// VBOX ///////////////////////////////////////////////////////////////////////
let sidebar = aDOMWindow.document.createElement('vbox');
propsToSet = {
id: 'tt-sidebar',
width: ss.getWindowValue(aDOMWindow, 'tt-width') || '200'
//persist: 'width' // It seems 'persist' attr doesn't work in bootstrap addons, I'll use SS instead
};
Object.keys(propsToSet).forEach( (p)=>{sidebar.setAttribute(p, propsToSet[p])} );
// added later
//////////////////// END VBOX ///////////////////////////////////////////////////////////////////////
//////////////////// SPLITTER ///////////////////////////////////////////////////////////////////////
let splitter = aDOMWindow.document.createElement('splitter');
propsToSet = {
id: 'tt-splitter'
//class: 'sidebar-splitter' // "I'm just copying what mozilla does for their social sidebar splitter"
// "I left it out, but you can leave it in to see how you can style the splitter"
};
Object.keys(propsToSet).forEach( (p)=>{splitter.setAttribute(p, propsToSet[p]);} );
// added later
//////////////////// END SPLITTER ///////////////////////////////////////////////////////////////////////
if (Services.prefs.getIntPref('extensions.tabtree.position') == 1) {
browser.appendChild(fullscrToggler);
browser.appendChild(splitter);
browser.appendChild(sidebar);
} else {
browser.insertBefore(fullscrToggler, aDOMWindow.document.querySelector('#appcontent')); // don't forget to remove
browser.insertBefore(sidebar, aDOMWindow.document.querySelector('#appcontent')); // don't forget to remove
browser.insertBefore(splitter, aDOMWindow.document.querySelector('#appcontent'));
}
//////////////////// QUICK SEARCH BOX ////////////////////////////////////////////////////////////////////////
let quickSearchBox = aDOMWindow.document.createElement('textbox');
propsToSet = {
id: 'tt-quicksearchbox',
placeholder: stringBundle.GetStringFromName('tabs_quick_search') || 'Tabs quick search...' // the latter is just in case
};
Object.keys(propsToSet).forEach( (p)=>{quickSearchBox.setAttribute(p, propsToSet[p]);} );
sidebar.appendChild(quickSearchBox);
//////////////////// END QUICK SEARCH BOX /////////////////////////////////////////////////////////////////
//////////////////// DROP INDICATOR ////////////////////////////////////////////////////////////////////////
let ind = aDOMWindow.document.getAnonymousElementByAttribute(aDOMWindow.gBrowser.tabContainer, 'anonid', 'tab-drop-indicator').cloneNode(true);
ind.removeAttribute('anonid');
ind.id = 'tt-drop-indicator';
ind.collapsed = true;
ind.style.marginTop = '-8px'; // needed for flipped arrow
let hboxForDropIndicator = aDOMWindow.document.createElement('hbox');
hboxForDropIndicator.align = 'start'; // just copying what mozilla does, but 'start' instead of 'end'
hboxForDropIndicator.appendChild(ind);
//////////////////// END DROP INDICATOR /////////////////////////////////////////////////////////////////
//////////////////// TOOLBOX /////////////////////////////////////////////////////////////////
/*
<toolbox>
<toolbar id="tt-toolbar">
<hbox align="start">
<img id="tt-drop-indicator" style="margin-top:-8px"/>
</hbox>
<toolbarbutton />
<toolbarbutton />
<toolbarbutton />
<toolbarbutton />
...
</toolbar>
</toolbox>
*/
let toolbox = aDOMWindow.document.createElement('toolbox');
let toolbar = aDOMWindow.document.createElement('toolbar');
toolbar.setAttribute('id', 'tt-toolbar');
toolbar.appendChild(hboxForDropIndicator);
toolbox.appendChild(toolbar);
sidebar.appendChild(toolbox);
//////////////////// END TOOLBOX /////////////////////////////////////////////////////////////////
//////////////////// TREE ///////////////////////////////////////////////////////////////////////
/*
<tree id="tt" flex="1" seltype="single" context="tabContextMenu" treelines="true" hidecolumnpicker="true"> // approximately
<treecols>
<treecol id="namecol" label="Name" primary="true" flex="1"/>
</treecols>
<treechildren id="tt-treechildren"/>
</tree>
*/
let tree = aDOMWindow.document.createElement('tree'); // <tree>
propsToSet = {
id: 'tt',
flex: '1',
seltype: 'single',
context: 'tabContextMenu',
treelines: Services.prefs.getBoolPref('extensions.tabtree.treelines').toString(),
hidecolumnpicker: 'true'
};
Object.keys(propsToSet).forEach( (p)=>{tree.setAttribute(p, propsToSet[p]);} );
let treecols = aDOMWindow.document.createElement('treecols'); // <treecols>
let treecol = aDOMWindow.document.createElement('treecol'); // <treecol>
propsToSet = {
id: 'tt-col',
flex: '1',
primary: 'true',
hideheader: 'true'
};
Object.keys(propsToSet).forEach( (p)=>{treecol.setAttribute(p, propsToSet[p]);} );
let treechildren = aDOMWindow.document.createElement('treechildren'); // <treechildren id="tt-treechildren">
treechildren.setAttribute('id', 'tt-treechildren');
treecols.appendChild(treecol);
tree.appendChild(treecols);
tree.appendChild(treechildren);
sidebar.appendChild(tree);
//////////////////// END TREE /////////////////////////////////////////////////////////////////
//////////////////// PANEL /////////////////////////////////////////////////////////////////////
let panel = aDOMWindow.document.createElement('panel');
panel.setAttribute('id', 'tt-panel');
panel.setAttribute('style', 'opacity: 0.8');
aDOMWindow.document.querySelector('#mainPopupSet').appendChild(panel); // don't forget to delete
//////////////////// END PANEL /////////////////////////////////////////////////////////////////
//////////////////// FEEDBACK TREE /////////////////////////////////////////////////////////////////////
let treeFeedback = aDOMWindow.document.createElement('tree');
/*
* <tree id="tt-tree-feedback" flex="1" seltype="single" treelines="true" seltype="single">
* <treecols>
* <treecol primary="true" flex="1"/>
* </treecols>
* <treechildren/>
* </tree>
*/
treeFeedback.setAttribute('id', 'tt-tree-feedback');
treeFeedback.setAttribute('flex', '1');
treeFeedback.setAttribute('seltype', 'single');
treeFeedback.setAttribute('treelines', Services.prefs.getBoolPref('extensions.tabtree.treelines').toString());
treeFeedback.setAttribute('hidecolumnpicker', 'true');
let treecolsFeedback = aDOMWindow.document.createElement('treecols');
let treecolFeedback = aDOMWindow.document.createElement('treecol');
treecolFeedback.setAttribute('flex', '1');
treecolFeedback.setAttribute('primary', 'true');
treecolFeedback.setAttribute('hideheader', 'true');
let treechildrenFeedback = aDOMWindow.document.createElement('treechildren');
treechildrenFeedback.setAttribute('id', 'tt-treechildren-feedback');
treecolsFeedback.appendChild(treecolFeedback);
treeFeedback.appendChild(treecolsFeedback);
treeFeedback.appendChild(treechildrenFeedback);
panel.appendChild(treeFeedback);
//////////////////// END FEEDBACK TREE /////////////////////////////////////////////////////////////////
/////////////////////////// PSEUDO-ANIMATED PNG ////////////////////////////////////////////////////////////////
// my way to force Firefox to cache images. Otherwise they would be loaded upon the first request (a tab load/refresh) and it wouldn't look smooth:
// I can't use a real animated PNG with <tree> element because it causes abnormally high CPU load
let pngsConnecting = aDOMWindow.document.createElement('hbox');
let pngsLoading = aDOMWindow.document.createElement('hbox');
for (let i=1; i<=18; ++i) { // 18 frames for each animated png
let pngConnecting = aDOMWindow.document.createElement('image');
let pngLoading = aDOMWindow.document.createElement('image');
pngConnecting.setAttribute('collapsed', 'true');
pngLoading.setAttribute('collapsed', 'true');
pngConnecting.setAttribute('src', 'chrome://tabtree/skin/connecting-F'+i+'.png');
pngLoading.setAttribute('src', 'chrome://tabtree/skin/loading-F'+i+'.png');
pngsConnecting.appendChild(pngConnecting);
pngsLoading.appendChild(pngLoading);
}
sidebar.appendChild(pngsConnecting);
sidebar.appendChild(pngsLoading);
/////////////////////// END PSEUDO-ANIMATED PNG ////////////////////////////////////////////////////////////////
//////////////////////////////// here we could load something before all tabs have been loaded and restored by SS ////////////////////////////////
let tt = {
DROP_BEFORE: -1,
DROP_AFTER: 1,
get nPinned() {
let c;
for (c=0; c<g.tabs.length; ++c) {
if (!g.tabs[c].pinned) {
break;
}
}
return c;
},
hasAnyChildren: function(tPos) {
let l = parseInt(ss.getTabValue(g.tabs[tPos], 'ttLevel'));
return !!( g.tabs[tPos + 1] && l + 1 == parseInt(ss.getTabValue(g.tabs[tPos + 1], 'ttLevel')) );
}, // hasAnyChildren(tPos)
hasAnySiblings: function(tPos) {
let level = parseInt(ss.getTabValue(g.tabs[tPos], 'ttLevel'));
for (let i = tPos-1; i>=0; --i) {
if (!g.tabs[i]) break;
if ( parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) == level ) return true;
if ( parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) < level ) break;
}
for (let i = tPos+1; i<g.tabs.length; ++i) {
if (!g.tabs[i]) break;
if ( parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) == level ) return true;
if ( parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) < level ) break;
}
return false;
}, // hasAnySiblings(tPos)
parentTab: function(aTab) {
// no checking at all
for (let i=aTab._tPos-1; i>=0; --i) {
if (this.levelInt(i)<this.levelInt(aTab)) return g.tabs[i];
}
return undefined;
},
levelInt: function(aTabOrPos) {
if (aTabOrPos instanceof Ci.nsIDOMElement) { // if it isn't a number
return parseInt(ss.getTabValue(aTabOrPos, 'ttLevel'));
} else {
return parseInt(ss.getTabValue(g.tabs[aTabOrPos], 'ttLevel'));
}
}, // levelInt(aTabOrPos) //
setLevel: function(aTabOrPos, level) {
if (aTabOrPos instanceof Ci.nsIDOMElement) { // if it isn't a number
ss.setTabValue(aTabOrPos, 'ttLevel', level.toString());
} else {
ss.setTabValue(g.tabs[aTabOrPos], 'ttLevel', level.toString());
}
},
shiftRight: function(aTab) {
if (aTab instanceof Ci.nsIDOMElement) {
ss.setTabValue(aTab, 'ttLevel', (parseInt(ss.getTabValue(aTab, 'ttLevel'))+1).toString() );
} else {
ss.setTabValue(g.tabs[aTab], 'ttLevel', (parseInt(ss.getTabValue(g.tabs[aTab], 'ttLevel'))+1).toString() );
}
},
moveTabToPlus: function(aTab, tPosTo, mode) {
if (aTab.pinned) { // if a pinned tab is being moved from 'toolbar' to 'tree', then unpin it before moving
g.unpinTab(aTab);
this.moveTabToPlus(aTab, tPosTo, mode); // recursion
return;
}
if (mode===tree.view.DROP_ON) {
ss.setTabValue(aTab, 'ttLevel', (parseInt(ss.getTabValue(g.tabs[tPosTo], 'ttLevel'))+1).toString());
for (let i=tPosTo+1; i<g.tabs.length+1; ++i) { // +1 on purpose in order to correctly process adding the tab to the very last position
// !g.tabs[i] is in order to correctly process adding the tab to the very last postition also
if ( !g.tabs[i] || parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) <= parseInt(ss.getTabValue(g.tabs[tPosTo], 'ttLevel')) ) {
if (aTab._tPos > i) {
g.moveTabTo(aTab, i);
} else if (aTab._tPos < i) {
g.moveTabTo(aTab, i-1);
}
break;
}
}
} else if (mode===tree.view.DROP_BEFORE) {
ss.setTabValue(aTab, 'ttLevel', ss.getTabValue(g.tabs[tPosTo], 'ttLevel') );
if (aTab._tPos > tPosTo) {
g.moveTabTo(aTab, tPosTo);
} else if (aTab._tPos < tPosTo) {
g.moveTabTo(aTab, tPosTo-1);
}
} else if (mode===tree.view.DROP_AFTER) {
if ( this.hasAnyChildren(tPosTo) ) {
ss.setTabValue(aTab, 'ttLevel', (parseInt(ss.getTabValue(g.tabs[tPosTo], 'ttLevel'))+1).toString());
} else {
ss.setTabValue(aTab, 'ttLevel', ss.getTabValue(g.tabs[tPosTo], 'ttLevel') );
}
if (aTab._tPos > tPosTo) {
g.moveTabTo(aTab, tPosTo+1);
} else if (aTab._tPos < tPosTo) {
g.moveTabTo(aTab, tPosTo);
}
}
}, // moveTabToPlus: function(aTab, tPosTo, mode) {
moveBranchToPlus: function(aTab, tPosTo, mode) {
let tPos = aTab._tPos;
let baseSourceLevel = this.levelInt(aTab);
if (mode===tree.view.DROP_ON) {
let baseLevelDiff = this.levelInt(tPos) - (this.levelInt(tPosTo)+1);
for (let i=tPosTo+1; i<g.tabs.length+1; ++i) { // +1 on purpose in order to correctly process adding the tab to the very last position
// !g.tabs[i] is in order to correctly process adding the tab to the very last postition also
if ( !g.tabs[i] || this.levelInt(i)<=this.levelInt(tPosTo) ) { // skip already existing children in the destination tab
if (tPos>=i) {
let lastDescendantPos;
for (lastDescendantPos=tPos+1; lastDescendantPos<g.tabs.length+1; ++lastDescendantPos) { // length+1 on purpose
if (!g.tabs[lastDescendantPos] || this.levelInt(lastDescendantPos)<=baseSourceLevel) {
lastDescendantPos = lastDescendantPos-1;
break;
}
}
while (this.levelInt(lastDescendantPos)>baseSourceLevel) {
this.setLevel(lastDescendantPos, this.levelInt(lastDescendantPos)-baseLevelDiff);
g.moveTabTo(g.tabs[lastDescendantPos], i);
}
this.setLevel(lastDescendantPos, this.levelInt(lastDescendantPos)-baseLevelDiff); // It must be 'aTab' tab
g.moveTabTo(g.tabs[lastDescendantPos], i);
} else if (tPos<i) {
this.setLevel(tPos, this.levelInt(tPos)-baseLevelDiff);
g.moveTabTo(g.tabs[tPos], i-1);
while (this.levelInt(tPos)>baseSourceLevel) {
this.setLevel(tPos, this.levelInt(tPos)-baseLevelDiff);
g.moveTabTo(g.tabs[tPos], i-1);
}
}
break;
}
}
} else if (mode===tree.view.DROP_BEFORE) {
let baseLevelDiff = this.levelInt(tPos) - this.levelInt(tPosTo);
if (tPos<tPosTo) {
this.setLevel(tPos, this.levelInt(tPos)-baseLevelDiff);
g.moveTabTo(g.tabs[tPos], tPosTo-1);
while (this.levelInt(tPos)>baseSourceLevel) {
this.setLevel(tPos, this.levelInt(tPos)-baseLevelDiff);
g.moveTabTo(g.tabs[tPos], tPosTo-1);
}
} else if (tPos>tPosTo) {
let lastDescendantPos;
for (let i=tPos+1; i<g.tabs.length+1; ++i) { // length+1 on purpose
if (!g.tabs[i] || this.levelInt(i)<=baseSourceLevel) {
lastDescendantPos = i-1;
break;
}
}
while (this.levelInt(lastDescendantPos)>baseSourceLevel) {
this.setLevel(lastDescendantPos, this.levelInt(lastDescendantPos)-baseLevelDiff);
g.moveTabTo(g.tabs[lastDescendantPos], tPosTo);
}
this.setLevel(lastDescendantPos, this.levelInt(lastDescendantPos)-baseLevelDiff); // It must be 'aTab' tab
g.moveTabTo(g.tabs[lastDescendantPos], tPosTo);
}
} else if (mode===tree.view.DROP_AFTER) {
let baseLevelDiff = this.levelInt(tPos) - this.levelInt(tPosTo);
if (this.hasAnyChildren(tPosTo)) {
--baseLevelDiff;
}
if (tPos<tPosTo) {
this.setLevel(tPos, this.levelInt(tPos)-baseLevelDiff);
g.moveTabTo(g.tabs[tPos], tPosTo);
while (this.levelInt(tPos)>baseSourceLevel) {
this.setLevel(tPos, this.levelInt(tPos)-baseLevelDiff);
g.moveTabTo(g.tabs[tPos], tPosTo);
}
} else if (tPos>tPosTo) {
let lastDescendantPos;
for (let i=tPos+1; i<g.tabs.length+1; ++i) { // length+1 on purpose
if (!g.tabs[i] || this.levelInt(i)<=baseSourceLevel) {
lastDescendantPos = i-1;
break;
}
}
while (this.levelInt(lastDescendantPos)>baseSourceLevel) {
this.setLevel(lastDescendantPos, this.levelInt(lastDescendantPos)-baseLevelDiff);
g.moveTabTo(g.tabs[lastDescendantPos], tPosTo+1);
}
this.setLevel(lastDescendantPos, this.levelInt(lastDescendantPos)-baseLevelDiff); // It must be 'aTab' tab
g.moveTabTo(g.tabs[lastDescendantPos], tPosTo+1);
}
}
}, // moveBranchToPlus: function(aTab, tPosTo, mode) {
lastDescendantPos: function(aTabOrPos) {
let ret;
let tPos = (aTabOrPos instanceof Ci.nsIDOMElement) ? aTabOrPos._tPos : aTabOrPos;
for (ret = tPos + 1; ret < g.tabs.length + 1; ++ret) { // length+1 on purpose
if (!g.tabs[ret] || this.levelInt(ret) <= this.levelInt(tPos)) {
ret = ret - 1;
break;
}
}
return ret;
},
movePinnedToPlus: function(aTab, tPosTo, mode) {
if (mode === this.DROP_BEFORE) {
if (aTab._tPos > tPosTo) {
g.moveTabTo(aTab, tPosTo);
} else if (aTab._tPos < tPosTo) {
g.moveTabTo(aTab, tPosTo-1);
}
} else if (mode === this.DROP_AFTER) {
if (aTab._tPos > tPosTo) {
g.moveTabTo(aTab, tPosTo+1);
} else if (aTab._tPos < tPosTo) {
g.moveTabTo(aTab, tPosTo);
}
}
this.redrawToolbarbuttons();
},
redrawToolbarbuttons: function() { // It's better to redraw all toolbarbuttons every time then add one toolbarbutton at a time. There were bugs when dragging and dropping them very fast
// reusing existing toolbarbuttons
let n = toolbar.childNodes.length - 1; // -1 for the arrow hbox
let max = Math.max(this.nPinned, n);
let min = Math.min(this.nPinned, n);
for (let i=0; i<max; ++i) {
let toolbarbtn;
if (i<min) { // reusing existing toolbarbuttons here
toolbarbtn = toolbar.childNodes[i+1]; // +1 for the arrow hbox
} else if (this.nPinned > n) { // we added a new pinned tab(tabs)
toolbarbtn = aDOMWindow.document.createElement('toolbarbutton');
toolbar.appendChild(toolbarbtn);
} else if (this.nPinned < n) { // we removed a pinned tab(tabs)
toolbarbtn = toolbar.childNodes[i+1]; // +1 for the arrow hbox
toolbar.removeChild(toolbarbtn);
continue;
}
toolbarbtn.setAttribute('tooltiptext', g.tabs[i].label);
toolbarbtn.setAttribute('type', 'radio');
toolbarbtn.setAttribute('group', 'RadioGroup');
toolbarbtn.setAttribute('context', 'tabContextMenu');
toolbarbtn.checked = g.tabs[i].selected;
let image = aDOMWindow.document.getAnonymousNodes(toolbarbtn)[0]; // there are sites with at least 32px*32px images therefore buttons would have become huge
image.setAttribute('height', '16px'); // we reduce such big images
image.setAttribute('width', '16px'); // also there are cases where the image is 60px*20px ('chrome://browser/skin/search-indicator.png' for example)
if (g.tabs[i].hasAttribute('progress') && g.tabs[i].hasAttribute('busy')) {
toolbarbtn.setAttribute('image', 'chrome://browser/skin/tabbrowser/loading.png');
} else if (g.tabs[i].hasAttribute('busy')) {
toolbarbtn.setAttribute('image', 'chrome://browser/skin/tabbrowser/connecting.png');
} else {
toolbarbtn.setAttribute('image', g.tabs[i].image || 'chrome://mozapps/skin/places/defaultFavicon.png');
}
}
g.mCurrentTab.pinned ? tree.view.selection.clearSelection() : tree.view.selection.select(g.mCurrentTab._tPos - tt.nPinned); // NEW
}, // redrawToolbarbuttons: function() {
quickSearch: function(aText, tPos) {
// I assume that this method is never invoked with aText=''
let url = g.browsers[tPos]._userTypedValue || g.browsers[tPos].contentDocument.URL || '';
let txt = aText.toLowerCase();
if (g.tabs[tPos].label.toLowerCase().indexOf(txt)!=-1 || url.toLowerCase().indexOf(txt)!=-1) { // 'url.toLowerCase()' may be replaced by 'url'
return true;
}
}
}; // let tt = {
treechildren.addEventListener('dragstart', function(event) { // if the event was attached to 'tree' then the popup would be shown while you scrolling
let tab = g.tabs[tree.currentIndex+tt.nPinned];
event.dataTransfer.mozSetDataAt(aDOMWindow.TAB_DROP_TYPE, tab, 0);
// "We must not set text/x-moz-url or text/plain data here,"
// "otherwise trying to detach the tab by dropping it on the desktop"
// "may result in an "internet shortcut" // from tabbrowser.xml
event.dataTransfer.mozSetDataAt("text/x-moz-text-internal", tab.linkedBrowser.currentURI.spec, 0);
if (1 || tt.hasAnyChildren(tab._tPos)) { // remove "1" to use default feedback image
panel.addEventListener('popupshown', function onPopupShown() {
panel.removeEventListener('popupshown', onPopupShown);
//noinspection JSUnusedGlobalSymbols
treeFeedback.treeBoxObject.view = {
numStart: tab._tPos,
numEnd: tt.lastDescendantPos(tab._tPos),
treeBox: null,
selection: null,
setTree: function (treeBox) {
this.treeBox = treeBox;
},
get rowCount() {
return this.numEnd - this.numStart + 1;
},
getCellText: function (row, column) {
let tPos = row + this.numStart;
return ' ' + g.tabs[tPos].label;
},
getImageSrc: function (row, column) {
let tPos = row + this.numStart;
return g.tabs[tPos].image;
}, // or null to hide icons or /g.getIcon(g.tabs[row])/
isContainer: function (row) {
return true;
}, // drop can be performed only on containers
isContainerOpen: function (row) {
return true;
},
isContainerEmpty: function (row) {
let tPos = row + this.numStart;
return !tt.hasAnyChildren(tPos);
},
getLevel: function (row) {
let tPos = row + this.numStart;
return parseInt(ss.getTabValue(g.tabs[tPos], 'ttLevel'));
},
isSeparator: function (row) {
return false;
},
isSorted: function () {
return false;
},
isEditable: function (row, column) {
return false;
},
getParentIndex: function (row) {
if (this.getLevel(row) == 0) return -1;
for (let t = row - 1; t >= 0; --t) {
if (this.getLevel(t) < this.getLevel(row)) return t; // && this.isContainerEmpty(t)
}
return -1;
},
hasNextSibling: function (row, after) {
let thisLevel = this.getLevel(row);
for (let t = after + 1; t < this.rowCount; t++) {
let nextLevel = this.getLevel(t);
if (nextLevel == thisLevel) return true;
if (nextLevel < thisLevel) break;
}
return false;
},
toggleOpenState: function (row) {
//this.treeBox.invalidateRow(row);
}
};
let borderTopWidth = parseInt( aDOMWindow.getComputedStyle(treeFeedback).getPropertyValue('border-top-width') );
let borderBottomWidth = parseInt( aDOMWindow.getComputedStyle(treeFeedback).getPropertyValue('border-bottom-width') );
let treeFeedbackHeight = treeFeedback.treeBoxObject.rowHeight * treeFeedback.treeBoxObject.view.rowCount;
panel.height = treeFeedbackHeight + borderTopWidth + borderBottomWidth;
}); // treeFeedback.treeBoxObject.view = {
panel.width = aDOMWindow.document.querySelector('#tt-sidebar').width;
let borderLeftWidth = parseInt( aDOMWindow.getComputedStyle(tree).getPropertyValue('border-left-width') );
let marginLeft = parseInt( aDOMWindow.getComputedStyle(tree).getPropertyValue('margin-left') );
event.dataTransfer.setDragImage(panel, event.clientX-(tree.boxObject.x-borderLeftWidth-marginLeft)+1, -20); // I don't know why "+1"
}
// uncomment if you always want to highlight 'gBrowser.mCurrentTab':
//g.mCurrentTab.pinned ? tree.view.selection.clearSelection() : tree.view.selection.select(g.mCurrentTab._tPos - tt.nPinned); // NEW
event.stopPropagation();
}, false); // tree.addEventListener('dragstart', function(event) {
tree.addEventListener('dragend', function(event) {
if (event.dataTransfer.dropEffect == 'none') { // the drag was cancelled
g.mCurrentTab.pinned ? tree.view.selection.clearSelection() : tree.view.selection.select(g.mCurrentTab._tPos - tt.nPinned); // NEW
}
}, false);
for (let i=0; i<g.tabs.length; ++i) {
if ( ss.getTabValue(g.tabs[i], 'ttLevel') === '' ) {
ss.setTabValue(g.tabs[i], 'ttLevel', '0');
}
}
aDOMWindow.tt.toRestore.g.addTab = g.addTab;
g.addTab = new Proxy(g.addTab, {
apply: function(target, thisArg, argumentsList) {
// altering params.relatedToCurrent argument in order to ignore about:config insertRelatedAfterCurrent option:
if (argumentsList.length == 2 && typeof argumentsList[1] == "object" && !(argumentsList[1] instanceof Ci.nsIURI)) {
argumentsList[1].relatedToCurrent = false;
argumentsList[1].skipAnimation = true; // I believe after disabling animation tabs are added a little bit faster
// But I can't see the difference with the naked eye
}
if (argumentsList.length>=2 && argumentsList[1].referrerURI) { // undo close tab hasn't got argumentsList[1]
g.tabContainer.addEventListener('TabOpen', function onPreAddTabWithRef(event) {
g.tabContainer.removeEventListener('TabOpen', onPreAddTabWithRef, true);
let tab = event.target;
let oldTab = g.selectedTab;
if (oldTab.pinned) {
ss.setTabValue(tab, 'ttLevel', '0');
tree.treeBoxObject.rowCountChanged(g.tabs.length-1 - tt.nPinned, 1); // our new tab is at index g.tabs.length-1
} else {
ss.setTabValue(tab, 'ttLevel', (parseInt(ss.getTabValue(oldTab, 'ttLevel')) + 1).toString());
let i;
for (i = oldTab._tPos + 1; i < g.tabs.length - 1; ++i) { // the last is our new tab
if (parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) <= parseInt(ss.getTabValue(oldTab, 'ttLevel'))) {
g.moveTabTo(tab, i);
break;
}
}
tree.treeBoxObject.rowCountChanged(i - tt.nPinned, 1);
// now we need to do something with a selected tree row(it has moved due to a newly added tab, it is not obvious why)
// it only needed if we opened a new tab in background and not from pinned tab:
tree.view.selection.select(oldTab._tPos - tt.nPinned);
}
tree.treeBoxObject.ensureRowIsVisible(tab._tPos - tt.nPinned);
}, true);
} else if (argumentsList.length>=2 && !argumentsList[1].referrerURI) { // new tab button or dropping links on the native tabbar
g.tabContainer.addEventListener('TabOpen', function onPreAddTabWithoutRef(event) {
g.tabContainer.removeEventListener('TabOpen', onPreAddTabWithoutRef, true);
if ( ss.getTabValue(event.target, 'ttLevel') === '' ) { // despite MDN it returns '' instead of undefined
ss.setTabValue(event.target, 'ttLevel', '0');
}
tree.treeBoxObject.rowCountChanged(event.target._tPos-tt.nPinned, 1);
}, true);
} else { // undo close tab
g.tabContainer.ttUndoingCloseTab = true; // for 'TabSelected' event handler in order not to fire when it is unnecessary
g.tabContainer.addEventListener('TabOpen', function onPreAddUndoCloseTab(event) {
g.tabContainer.removeEventListener('TabOpen', onPreAddUndoCloseTab, true);
aDOMWindow.document.addEventListener('SSTabRestoring', function onSSing(event) {
aDOMWindow.document.removeEventListener('SSTabRestoring', onSSing, true);
let tab = event.originalTarget; // the tab being restored
if (tab.pinned) {
tt.redrawToolbarbuttons();
} else {
tree.treeBoxObject.rowCountChanged(tab._tPos - tt.nPinned, 1);
// refresh the twisty (commented out while the twisty is disabled):
//let pTab = tt.parentTab(tab);
//if (pTab) {
// tree.treeBoxObject.invalidateRow(pTab._tPos - tt.nPinned);
//}
if (ss.getTabValue(tab, 'ttSS')) { // if tab had direct children, then make them children again
let arr = JSON.parse(ss.getTabValue(tab, 'ttSS'));
tt.shiftRight(tab._tPos + 1);
for (let i = tab._tPos + 2; i < g.tabs.length; ++i) { // +2 on purpose
if (tt.levelInt(i) <= tt.levelInt(tab)) {
break;
}
if (tt.levelInt(i) == tt.levelInt(tab) + 1) {
if (arr.indexOf(g.tabs[i].linkedPanel) == -1) {
tt.shiftRight(i);
}
} else {
tt.shiftRight(i);
}
}
ss.deleteTabValue(tab, 'ttSS');
}
tree.view.selection.select(tab._tPos - tt.nPinned); // after 'rowCountChanged' the selected row is moved 1 position ahead
tree.treeBoxObject.ensureRowIsVisible(tab._tPos - tt.nPinned);
}
}, true);
}, true);
}
return target.apply(thisArg, argumentsList);
}
}); // don't forget to restore
aDOMWindow.tt.toRestore.g._endRemoveTab = g._endRemoveTab;
g._endRemoveTab = new Proxy(g._endRemoveTab, {
apply: function(target, thisArg, argumentsList) {
let tPos = argumentsList[0]._tPos;
let tab = argumentsList[0];
let pinned = false;
if (tab.pinned) { // if we are closing a pinned tab then remember it
pinned = true;
} else if ( tt.hasAnyChildren(tPos) ) { // if we are closing a parent then make the first child a new parent
// for SS we need to save the direct children
let arr = [];
for (let i=tPos+1; i<g.tabs.length; ++i) {
if ( tt.levelInt(i) <= tt.levelInt(tPos) ) {
break;
}
if ( tt.levelInt(i) == tt.levelInt(tPos)+1 ) {
arr.push(g.tabs[i].linkedPanel);
}
}
ss.setTabValue(tab, 'ttSS', JSON.stringify(arr));
// end for SS
for (let i=tPos+2; i<g.tabs.length; ++i) {
if ( g.tabs[i] && parseInt(ss.getTabValue(g.tabs[i], 'ttLevel')) > parseInt(ss.getTabValue(g.tabs[tPos+1], 'ttLevel')) ) {
ss.setTabValue(g.tabs[i], 'ttLevel', (parseInt(ss.getTabValue(g.tabs[i], 'ttLevel'))-1).toString());
} else {
break;
}
}
ss.setTabValue(g.tabs[tPos+1], 'ttLevel', (parseInt(ss.getTabValue(g.tabs[tPos+1], 'ttLevel'))-1).toString());
} else if ( parseInt(ss.getTabValue(tab, 'ttLevel'))>=1 && !tt.hasAnySiblings(tPos) ) { // closing the last child, the first condition may be omitted, for now
let pTab = tt.parentTab(tab);
tree.treeBoxObject.invalidateRow(pTab._tPos-tt.nPinned);
}
target.apply(thisArg, argumentsList); // returns nothing // after this, "tab.pinned" is always 'false' therefore we use "pinned" which we prepared early
if (pinned) {
tt.redrawToolbarbuttons();
} else {
tree.treeBoxObject.rowCountChanged(tPos - tt.nPinned, -1);
g.mCurrentTab.pinned ? tree.view.selection.clearSelection() : tree.view.selection.select(g.mCurrentTab._tPos - tt.nPinned); // NEW
}
}
}); // don't forget to restore
//noinspection JSUnusedGlobalSymbols
let view = {
treeBox: null,
selection: null,
setTree: function(treeBox) { this.treeBox = treeBox; },
get rowCount() {
return g.tabs.length-tt.nPinned;
},
getCellText: function(row, column) {
let tPos = row+tt.nPinned;
return ' ' + g.tabs[tPos].label;
},
getImageSrc: function(row, column) {
// Notice that when 'busy' attribute has already been removed the favicon can still be not loaded
let tPos = row+tt.nPinned;
let tab = g.tabs[tPos];
if ('ttThrobC' in tab) {
if (tab.hasAttribute('progress') && tab.hasAttribute('busy')) {
return 'chrome://tabtree/skin/loading-F' + tab.ttThrobC + '.png';
} else if (tab.hasAttribute('busy')) {
return 'chrome://tabtree/skin/connecting-F' + tab.ttThrobC + '.png';
} else {
// we can also clear this Interval in 'TabAttrModified' event listener
aDOMWindow.clearInterval(tab.ttThrob);
delete tab.ttThrobC;
delete tab.ttThrob;
}
}
// Firefox uses <xul:image> instead of <img>. <xul:image> doesn't have '.complete' property
// And I don't know any other way to determine whether an image was loaded or not.
// If I just wrote "return g.tabs[tPos].image;" then there would be a small period of time when
// a page is already loaded (and attribute 'busy' removed), but the favicon is still loading and
// in that period of time a row wouldn't have any icon and for user it would look like a tab title jumping to the left for a split of a second
let im = new aDOMWindow.Image();
im.src = tab.image;
if (im.complete) {
return tab.image;
} else {
return 'chrome://mozapps/skin/places/defaultFavicon.png';
// or 'chrome://tabtree/skin/completelyTransparent.png' // it would look exactly like what Firefox does for its default tabs
}