-
Notifications
You must be signed in to change notification settings - Fork 55
/
verticaltabs.js
1373 lines (1207 loc) · 51.3 KB
/
verticaltabs.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
/* -*- Mode: javascript; indent-tabs-mode: nil -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Vertical Tabs.
*
* The Initial Developer of the Original Code is
* Philipp von Weitershausen.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/* global require, exports:false, PageThumbs:false, CustomizableUI:false PluralForm:false*/
'use strict';
const {Cc, Ci, Cu} = require('chrome');
const {prefs} = require('sdk/simple-prefs');
const {get, set} = require('sdk/preferences/service');
const {sendPing, setDefaultPrefs, removeStylesheets, installStylesheets} = require('./utils');
const {createExposableURI} = Cc['@mozilla.org/docshell/urifixup;1'].
createInstance(Ci.nsIURIFixup);
const strings = require('./get-locale-strings').getLocaleStrings();
const ss = Cc['@mozilla.org/browser/sessionstore;1'].getService(Ci.nsISessionStore);
const utils = require('./utils');
const system = require('sdk/system');
Cu.import('resource://gre/modules/PageThumbs.jsm');
Cu.import('resource:///modules/CustomizableUI.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/PluralForm.jsm');
const NS_XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
const TAB_DROP_TYPE = 'application/x-moz-tabbrowser-tab';
// Wait these many milliseconds before resizing tabs
// after mousing out
const WAIT_BEFORE_RESIZE = 1000;
/*
* Vertical Tabs
*
* Main entry point of this add-on.
*/
function VerticalTabs(window, data, tabCenterStartup) {
this.window = window;
this.document = window.document;
this.sendPing = sendPing;
this.unloaders = [];
window.createImageBitmap(data).then((response) => {
this.newTabImage = response;
});
this.init(tabCenterStartup);
}
VerticalTabs.prototype = {
init: function (tabCenterStartup) {
let window = this.window;
let document = this.document;
this.window.VerticalTabs = this;
this.resizeTimeout = 0;
this.mouseInside = false;
let mainWindow = document.getElementById('main-window');
let tabs = document.getElementById('tabbrowser-tabs');
if (mainWindow.getAttribute('toggledon') === '') {
mainWindow.setAttribute('toggledon', 'true');
}
if (mainWindow.getAttribute('toggledon') !== 'true') {
let toolbar = document.getElementById('TabsToolbar');
this.clearFind();
tabs.removeAttribute('mouseInside');
let sidetabsbutton = utils.createElement(document, 'toolbarbutton', {
'id': 'side-tabs-button',
'label': strings.sideLabel,
'tooltiptext': strings.sideTooltip,
'class': 'toolbarbutton-1'
});
sidetabsbutton.style.MozAppearance = 'none';
sidetabsbutton.style.setProperty('-moz-image-region', 'rect(0, 16px, 16px, 0)', 'important');
let checkBrighttext = function () {
if (document.getElementById('nav-bar').getAttribute('brighttext') === 'true') {
sidetabsbutton.style.setProperty('list-style-image', 'url("resource://tabcenter/skin/tc-side-white.svg")', 'important');
} else {
sidetabsbutton.style.setProperty('list-style-image', 'url("resource://tabcenter/skin/tc-side.svg")', 'important');
}
};
checkBrighttext();
window.addEventListener('customizationchange', checkBrighttext);
sidetabsbutton.onclick = (e) => {
if (e.which !== 1) {
return;
}
mainWindow.setAttribute('toggledon', 'true');
this.unload();
set('[email protected]', Date.now().toString());
ss.setWindowValue(window, 'TCtoggledon', mainWindow.getAttribute('toggledon'));
this.init();
if (mainWindow.getAttribute('F11-fullscreen') === 'true') {
let fullscreenctls = document.getElementById('window-controls');
let navbar = document.getElementById('nav-bar');
let toggler = document.getElementById('fullscr-toggler');
let sibling = document.getElementById('navigator-toolbox').nextSibling;
toggler.removeAttribute('hidden');
window.FullScreen._updateToolbars(true);
navbar.appendChild(fullscreenctls);
document.getElementById('appcontent').insertBefore(toggler, sibling);
}
window.VerticalTabs.sendPing('tab_center_toggled_on', window);
};
toolbar.insertBefore(sidetabsbutton, null);
this.unload();
this.unloaders.push(function () {
toolbar.removeChild(sidetabsbutton);
window.removeEventListener('customizationchange', checkBrighttext);
});
return;
}
installStylesheets(window);
this.PageThumbs = PageThumbs;
this._endRemoveTab = window.gBrowser._endRemoveTab;
this.inferFromText = window.ToolbarIconColor.inferFromText;
this.receiveMessage = window.gBrowser.receiveMessage;
this.showTab = window.gBrowser.showTab;
window.gBrowser.showTab = function (aTab) {
if (aTab.hidden && !aTab.getAttribute('filtered-out')) {
aTab.removeAttribute('hidden');
this._visibleTabs = null; // invalidate cache
this.tabContainer.adjustTabstrip();
this.tabContainer._setPositionalAttributes();
let event = document.createEvent('Events');
event.initEvent('TabShow', true, false);
aTab.dispatchEvent(event);
}
};
let oldMoveTabTo = window.gBrowser.moveTabTo;
window.gBrowser.moveTabTo = function (aTab, aIndex) {
let oldPosition = aTab._tPos;
let numPinned = window.VerticalTabs.numPinnedtabs();
let reverse = tabs.getAttribute('opentabstop');
if (oldPosition === aIndex && (!reverse || numPinned === 0)) {
return;
}
// Don't allow mixing pinned and unpinned tabs.
if (aTab.pinned && !reverse) {
aIndex = Math.min(aIndex, numPinned - 1);
} else if (aTab.pinned && reverse) {
aIndex = Math.max(aIndex, this.tabs.length - numPinned);
} else if (!aTab.pinned && !reverse) {
aIndex = Math.max(aIndex, numPinned);
} else {
aIndex = Math.min(aIndex, this.tabs.length - numPinned - 1);
}
this._lastRelatedTab = null;
let wasFocused = (document.activeElement === this.mCurrentTab);
aIndex = aIndex < aTab._tPos ? aIndex : aIndex + 1;
// invalidate cache
this._visibleTabs = null;
// use .item() instead of [] because dragging to the end of the strip goes out of
// bounds: .item() returns null (so it acts like appendChild), but [] throws
this.tabContainer.insertBefore(aTab, this.tabs.item(aIndex));
for (let i = 0; i < this.tabs.length; i++) {
this.tabs[i]._tPos = i;
this.tabs[i]._selected = false;
}
// If we're in the midst of an async tab switch while calling
// moveTabTo, we can get into a case where _visuallySelected
// is set to true on two different tabs.
//
// What we want to do in moveTabTo is to remove logical selection
// from all tabs, and then re-add logical selection to mCurrentTab
// (and visual selection as well if we're not running with e10s, which
// setting _selected will do automatically).
//
// If we're running with e10s, then the visual selection will not
// be changed, which is fine, since if we weren't in the midst of a
// tab switch, the previously visually selected tab should still be
// correct, and if we are in the midst of a tab switch, then the async
// tab switcher will set the visually selected tab once the tab switch
// has completed.
this.mCurrentTab._selected = true;
if (wasFocused) {
this.mCurrentTab.focus();
}
this.tabContainer._handleTabSelect(false);
if (aTab.pinned) {
this.tabContainer._positionPinnedTabs();
}
this.tabContainer._setPositionalAttributes();
let evt = document.createEvent('UIEvents');
evt.initUIEvent('TabMove', true, false, window, oldPosition);
aTab.dispatchEvent(evt);
};
let oldPinTab = window.gBrowser.pinTab;
window.gBrowser.pinTab = function (aTab) {
if (aTab.pinned) {
return;
}
let numPinned = window.VerticalTabs.numPinnedtabs();
if (aTab.hidden) {
this.showTab(aTab);
}
let reverse = document.getAnonymousElementByAttribute(this.tabContainer, 'anonid', 'arrowscrollbox')._isRTLScrollbox;
if (reverse) {
this.moveTabTo(aTab, this.tabs.length - numPinned - 1);
} else {
this.moveTabTo(aTab, numPinned);
}
aTab.setAttribute('pinned', 'true');
this.tabContainer._unlockTabSizing();
this.tabContainer._positionPinnedTabs();
this.tabContainer.adjustTabstrip();
this.getBrowserForTab(aTab).messageManager.sendAsyncMessage('Browser:AppTab', {isAppTab: true});
let event = document.createEvent('Events');
event.initEvent('TabPinned', true, false);
aTab.dispatchEvent(event);
};
let OldPrintPreviewListenerEnter = window.PrintPreviewListener.onEnter;
let OldPrintPreviewListenerExit = window.PrintPreviewListener.onExit;
window.PrintPreviewListener.onEnter = () => {
let mainWindow = document.getElementById('main-window');
mainWindow.setAttribute('printPreview', 'true');
OldPrintPreviewListenerEnter.call(window.PrintPreviewListener);
};
window.PrintPreviewListener.onExit = () => {
mainWindow.removeAttribute('printPreview');
OldPrintPreviewListenerExit.call(window.PrintPreviewListener);
};
// change the text in the tab context box
let close_next_tabs_message = document.getElementById('context_closeTabsToTheEnd');
let previous_close_message = close_next_tabs_message.getAttribute('label');
let oldWarnAboutClosingTabs = window.gBrowser.warnAboutClosingTabs;
window.gBrowser.warnAboutClosingTabs = function (aCloseTabs, aTab) {
let tabsToClose;
switch (aCloseTabs) {
case this.closingTabsEnum.ALL:
tabsToClose = this.tabs.length - this._removingTabs.length -
window.VerticalTabs.numPinnedtabs();
break;
case this.closingTabsEnum.OTHER:
tabsToClose = window.gBrowser.visibleTabs.length - 1 - window.VerticalTabs.numPinnedtabs();
break;
case this.closingTabsEnum.TO_END:
if (!aTab){
throw new Error('Required argument missing: aTab');
}
tabsToClose = this.getTabsToTheEndFrom(aTab).length;
break;
default:
throw new Error('Invalid argument: ' + aCloseTabs);
}
if (tabsToClose <= 1) {
return true;
}
const pref = aCloseTabs === this.closingTabsEnum.ALL ?
'browser.tabs.warnOnClose' : 'browser.tabs.warnOnCloseOtherTabs';
let shouldPrompt = Services.prefs.getBoolPref(pref);
if (!shouldPrompt) {
return true;
}
let ps = Services.prompt;
// default to true: if it were false, we wouldn't get this far
let warnOnClose = {value: true};
let bundle = this.mStringBundle;
// focus the window before prompting.
// this will raise any minimized window, which will
// make it obvious which window the prompt is for and will
// solve the problem of windows "obscuring" the prompt.
// see bug #350299 for more details
window.focus();
let warningMessage =
PluralForm.get(tabsToClose, bundle.getString('tabs.closeWarningMultiple'))
.replace('#1', tabsToClose);
let buttonPressed =
ps.confirmEx(window,
bundle.getString('tabs.closeWarningTitle'),
warningMessage,
(ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0)
+ (ps.BUTTON_TITLE_CANCEL * ps.BUTTON_POS_1),
bundle.getString('tabs.closeButtonMultiple'),
null, null,
aCloseTabs === this.closingTabsEnum.ALL ?
bundle.getString('tabs.closeWarningPromptMe') : null,
warnOnClose);
let reallyClose = (buttonPressed === 0);
// don't set the pref unless they press OK and it's false
if (aCloseTabs === this.closingTabsEnum.ALL && reallyClose && !warnOnClose.value) {
Services.prefs.setBoolPref(pref, false);
}
return reallyClose;
};
let oldGetTabsToTheEndFrom = window.gBrowser.getTabsToTheEndFrom;
window.gBrowser.getTabsToTheEndFrom = (aTab) => {
let tabsToEnd = [];
let tabs = window.gBrowser.visibleTabs;
for (let i = tabs.length - 1; tabs[i] !== aTab && i >= 0; --i) {
if (!tabs[i].pinned) {
tabsToEnd.push(tabs[i]);
}
}
return tabsToEnd.reverse();
};
let oldAddTab = window.gBrowser.addTab;
window.gBrowser.addTab = function (...args) {
let numPinned = window.VerticalTabs.numPinnedtabs();
let t = oldAddTab.bind(window.gBrowser)(...args);
// opentabstop pref
// eslint-disable-next-line no-constant-condition
if (false) {
let aRelatedToCurrent;
let aReferrerURI;
if (arguments.length === 2 && typeof arguments[1] === 'object' && !(arguments[1] instanceof Ci.nsIURI)) {
let params = arguments[1];
aReferrerURI = params.referrerURI;
aRelatedToCurrent = params.relatedToCurrent;
}
// aRelatedToCurrent can be undefined or null if the tab is
//opened from an external application or bookmark
if (((aRelatedToCurrent === null || aRelatedToCurrent === undefined) ? aReferrerURI : aRelatedToCurrent) &&
Services.prefs.getBoolPref('browser.tabs.insertRelatedAfterCurrent')) {
let newTabPos = (this._lastRelatedTab || this.selectedTab)._tPos;
this.moveTabTo(t, newTabPos);
this._lastRelatedTab = t;
} else {
this.moveTabTo(t, window.gBrowser.tabs.length - numPinned - 1);
}
}
return t;
};
let reverseTabsListener = function () {
let arrowscrollbox = document.getAnonymousElementByAttribute(tabs, 'anonid', 'arrowscrollbox');
if (arrowscrollbox) {
window.VerticalTabs.reverseTabs(arrowscrollbox);
}
window.gBrowser._lastRelatedTab = null;
};
// update on changing preferences
require('sdk/simple-prefs').on('opentabstop', reverseTabsListener);
let arrowscrollbox = document.getAnonymousElementByAttribute(tabs, 'anonid', 'arrowscrollbox');
// opentabstop pref
// eslint-disable-next-line no-constant-condition
if (tabCenterStartup && arrowscrollbox && false) {
close_next_tabs_message.setAttribute('label', strings.closeTabsAbove);
arrowscrollbox._isRTLScrollbox = true;
tabs.setAttribute('opentabstop', 'true');
let i = 0;
while (window.gBrowser.tabs[0].pinned && i <= window.gBrowser.tabs.length - 1) {
window.gBrowser.moveTabTo(window.gBrowser.tabs[0], window.gBrowser.tabs.length - 1);
i++;
}
// opentabstop pref
// eslint-disable-next-line no-constant-condition
} else if (arrowscrollbox && false) {
window.VerticalTabs.reverseTabs(arrowscrollbox);
} else {
close_next_tabs_message.setAttribute('label', strings.closeTabsBelow);
}
let tabsProgressListener = {
onLocationChange: (aBrowser, aWebProgress, aRequest, aLocation, aFlags) => {
for (let tab of this.window.gBrowser.visibleTabs) {
if (tab.linkedBrowser === aBrowser) {
tab.refreshThumbAndLabel();
}
}
},
onStateChange: (aBrowser, aWebProgress, aRequest, aFlags, aStatus) => {
if ((aFlags & Ci.nsIWebProgressListener.STATE_STOP) === Ci.nsIWebProgressListener.STATE_STOP) { // eslint-disable-line no-bitwise
this.adjustCrop();
for (let tab of this.window.gBrowser.visibleTabs) {
if (tab.linkedBrowser === aBrowser && tab.refreshThumbAndLabel) {
tab.refreshThumbAndLabel();
}
}
}
}
};
window.gBrowser.addTabsProgressListener(tabsProgressListener);
window.addEventListener('animationend', (e) => {
let tab = e.target;
if (e.animationName.endsWith('tab-fade-in')) {
tab.classList.remove('tab-visible');
} else if (e.animationName.endsWith('tab-fade-out')) {
this._endRemoveTab.bind(this.window.gBrowser)(tab);
this.resizeTabs();
}
});
this._removeTab = window.gBrowser.removeTab;
window.gBrowser.removeTab = (...args) => {
this._removeTab.bind(window.gBrowser)(...args);
window.gBrowser._endRemoveTab(args[0]);
};
window.gBrowser._endRemoveTab = (aTab) => {
if (!aTab || !aTab._endRemoveArgs) {
return;
}
window.gBrowser._blurTab(aTab);
aTab.classList.add('tab-hidden');
};
window.gBrowser.receiveMessage = (...args) => {
if (args[0].target.getAttribute('anonid') === 'initialBrowser' && args[0].name === 'Browser:WindowCreated' && Services.prefs.getIntPref('browser.startup.page') !== 3) {
let tab = window.gBrowser.getTabForBrowser(window.gBrowser.selectedBrowser);
while (tab.getAttribute('pinned') === 'true') {
tab = tab.nextSibling;
}
window.gBrowser.selectedTab = tab;
}
this.receiveMessage.bind(window.gBrowser)(...args);
};
window.gBrowser.tabContainer.addEventListener('TabBarUpdated', () => {
this.clearFind('tabGroupChange');
});
window.ToolbarIconColor.inferFromText = () => {
this.inferFromText.bind(window.ToolbarIconColor)();
//use default inferFromText, then set main-window[brighttext] according to the results
if (document.getElementById('nav-bar').getAttribute('brighttext') === 'true') {
mainWindow.setAttribute('brighttext', 'true');
} else {
mainWindow.removeAttribute('brighttext');
}
};
this.thumbTimer = this.window.setInterval(() => {
tabs.selectedItem.refreshThumbAndLabel();
}, 1000);
this.unloaders.push(function () {
if (this.thumbTimer) {
this.window.clearInterval(this.thumbTimer);
this.thumbTimer = null;
}
this.window.gBrowser.removeTabsProgressListener(tabsProgressListener);
this.window.gBrowser.removeTab = this._removeTab;
this.window.gBrowser.showTab = this.showTab;
this.window.ToolbarIconColor.inferFromText = this.inferFromText;
this.window.gBrowser._endRemoveTab = this._endRemoveTab;
this.window.gBrowser.receiveMessage = this.receiveMessage;
this.window.PrintPreviewListener.onEnter = OldPrintPreviewListenerEnter;
this.window.PrintPreviewListener.onExit = OldPrintPreviewListenerExit;
this.window.gBrowser.moveTabTo = oldMoveTabTo;
this.window.gBrowser.pintab = oldPinTab;
this.window.gBrowser.addTab = oldAddTab;
this.window.gBrowser.getTabsToTheEndFrom = oldGetTabsToTheEndFrom;
window.gBrowser.warnAboutClosingTabs = oldWarnAboutClosingTabs;
if (this.document.getElementById('top-tabs-button')){
this.document.getElementById('TabsToolbar').removeChild(this.document.getElementById('top-tabs-button'));
}
close_next_tabs_message.setAttribute('label', previous_close_message);
require('sdk/simple-prefs').removeListener('opentabstop', reverseTabsListener);
});
this.rearrangeXUL();
let results = this.document.getElementById('PopupAutoCompleteRichResult');
if (results) {
results.removeAttribute('width');
}
this.tabObserver = new this.document.defaultView.MutationObserver((mutations) => {
this.tabObserver.disconnect();
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' &&
mutation.target.id === 'PopupAutoCompleteRichResult' &&
mutation.attributeName === 'width') {
results.removeAttribute('width');
} else if (mutation.type === 'attributes' && mutation.attributeName === 'overflow' && mutation.target.id === 'tabbrowser-tabs') {
if (mutation.target.getAttribute('overflow') !== 'true') {
tabs.setAttribute('overflow', 'true'); //always set overflow back to true
}
}
});
this.tabObserver.observe(tabs, {childList: true, attributes: true, subtree: true});
if (results) {
this.tabObserver.observe(results, {attributes: true});
}
});
this.tabObserver.observe(tabs, {childList: true, attributes: true, subtree: true});
if (results) {
this.tabObserver.observe(results, {attributes: true});
}
window.TabsInTitlebar.allowedBy('tabcenter', false);
this.unloaders.push(function () {
this.tabObserver.disconnect();
});
},
rearrangeXUL: function () {
const window = this.window;
const document = this.document;
// Move the bottom stuff (findbar, addonbar, etc.) in with the
// tabbrowser. That way it will share the same (horizontal)
// space as the brower. In other words, the bottom stuff no
// longer extends across the whole bottom of the window.
let mainWindow = document.getElementById('main-window');
let contentbox = document.getElementById('appcontent');
let bottom = document.getElementById('browser-bottombox');
contentbox.appendChild(bottom);
let top = document.getElementById('navigator-toolbox');
let browserPanel = document.getElementById('browser-panel');
let autocomplete = document.getElementById('PopupAutoCompleteRichResult');
let autocompleteOpen = autocomplete._openAutocompletePopup;
autocomplete._openAutocompletePopup = (aInput, aElement) => {
autocompleteOpen.bind(autocomplete)(aInput, aElement);
let rect = window.document.documentElement.getBoundingClientRect();
let popupDirection = autocomplete.style.direction;
let sidebar = document.getElementById('sidebar-box');
// Make the popup's starting margin negative so that the leading edge
// of the popup aligns with the window border.
let elementRect = aElement.getBoundingClientRect();
if (popupDirection === 'rtl') {
let offset = elementRect.right - rect.right;
let width = rect.width;
autocomplete.style.marginRight = offset + 'px';
autocomplete.style.width = width + 'px';
} else {
let offset = rect.left - elementRect.left;
let width = rect.width;
if (mainWindow.getAttribute('F11-fullscreen') !== 'true') {
if (mainWindow.getAttribute('tabspinned') !== 'true') {
offset += 45;
width -= 45;
} else {
offset += this.pinnedWidth;
width -= this.pinnedWidth;
}
}
if (sidebar.getAttribute('hidden') !== 'true') {
offset += sidebar.getBoundingClientRect().width;
width -= sidebar.getBoundingClientRect().width;
}
autocomplete.style.marginLeft = offset + 'px';
autocomplete.style.width = width + 'px';
}
};
// save the label of the first tab, the toolbox palette, and the url for later…
let tabs = document.getElementById('tabbrowser-tabs');
let label = tabs.firstChild.label;
let palette = top.palette;
let urlbar = document.getElementById('urlbar');
let url = urlbar.value;
let activeTab = window.gBrowser.mCurrentTab;
// Save the position of the tabs in the toolbar, for later restoring.
let toolbar = document.getElementById('TabsToolbar');
let tabsIndex = 0;
for (let i = 0; i < toolbar.children.length; i++) {
if (toolbar.children[i] === tabs) {
tabsIndex = i;
break;
}
}
let NewTabButton = toolbar.querySelector('#new-tab-button') || CustomizableUI.getWidget('new-tab-button').forWindow(this.window).node;
//if new tab button is not in toolbar, find it and insert it.
if (!toolbar.querySelector('#new-tab-button')) {
//save position of button for restoring later
let NewTabButtonParent = NewTabButton.parentNode;
let NewTabButtonSibling = NewTabButton.nextSibling;
toolbar.insertBefore(NewTabButton, toolbar.firstChild);
this.unloaders.push(function () {
// put the newTab button back where it belongs
NewTabButtonParent.insertBefore(NewTabButton, NewTabButtonSibling);
});
}
function restorePlacesControllers() {
// Rearranging the #navigator-toolbox (top) element results in releaseing
// the `controllers` of the #PlacesToolbar element that's inside
// #navigator-toolbox, and regenerated `controllers` doesn't contain the
// controller added in `PlacesViewBase` constructor. That breaks
// Bookmarks Toolbar and its menu items. Restore the controller to make
// them working again.
let PlacesToolbar = document.getElementById('PlacesToolbar');
if (PlacesToolbar &&
PlacesToolbar.controllers &&
PlacesToolbar.controllers.getControllerCount() === 0 &&
PlacesToolbar._placesView &&
PlacesToolbar._placesView._controller) {
PlacesToolbar.controllers.appendController(PlacesToolbar._placesView._controller);
}
}
contentbox.insertBefore(top, contentbox.firstChild);
restorePlacesControllers();
// Create a box next to the app content. It will hold the tab
// bar and the tab toolbar.
let browserbox = document.getElementById('browser');
let leftbox = utils.createElement(document, 'vbox', {'id': 'verticaltabs-box'});
let splitter = utils.createElement(document, 'vbox', {'id': 'verticaltabs-splitter'});
if (mainWindow.getAttribute('tabspinned') === '') {
mainWindow.setAttribute('tabspinned', 'true');
leftbox.setAttribute('expanded', 'true');
}
browserbox.insertBefore(leftbox, contentbox);
browserbox.insertBefore(splitter, browserbox.firstChild);
this.pinnedWidth = +mainWindow.getAttribute('tabspinnedwidth').replace('px', '') ||
+window.getComputedStyle(document.documentElement)
.getPropertyValue('--pinned-width').replace('px', '');
document.documentElement.style.setProperty('--pinned-width', `${this.pinnedWidth}px`);
splitter.addEventListener('mousedown', (event) => {
if (event.which !== 1) {
return;
}
if (this.pinnedWidth > document.width / 2) {
this.pinnedWidth = document.width / 2;
}
let initialX = event.screenX - this.pinnedWidth;
let mousemove = (event) => {
let xDelta = event.screenX - initialX;
this.pinnedWidth = Math.min(xDelta, document.width / 2);
if (this.pinnedWidth < 30) {
this.pinnedWidth = 30;
}
document.documentElement.style.setProperty('--pinned-width', `${this.pinnedWidth}px`);
mainWindow.setAttribute('tabspinnedwidth', `${this.pinnedWidth}px`);
ss.setWindowValue(window, 'TCtabspinnedwidth', mainWindow.getAttribute('tabspinnedwidth'));
this.resizeFindInput();
this.resizeTabs();
};
let mouseup = (event) => {
document.removeEventListener('mousemove', mousemove);
document.removeEventListener('mouseup', mouseup);
};
document.addEventListener('mousemove', mousemove);
document.addEventListener('mouseup', mouseup);
});
// Move the tabs next to the app content, make them vertical,
// and restore their width from previous session
tabs.setAttribute('vertical', true);
tabs.setAttribute('overflow', 'true');
leftbox.insertBefore(tabs, leftbox.firstChild);
//remove extra #newtab-popup before they get added again in the tabs constructor
if (NewTabButton) {
while (NewTabButton.children.length > 1) {
NewTabButton.firstChild.remove();
}
}
tabs.orient = 'vertical';
tabs.mTabstrip.orient = 'vertical';
tabs.tabbox.orient = 'horizontal'; // probably not necessary
// And restore the label, palette and url here.
tabs.firstChild.label = label;
top.palette = palette;
urlbar.value = url;
window.gBrowser.tabContainer.selectedIndex = tabs.getIndexOfItem(activeTab);
// Move the tabs toolbar into the tab strip
toolbar.setAttribute('collapsed', 'false'); // no more vanishing new tab toolbar
toolbar._toolbox = null; // reset value set by constructor
toolbar.setAttribute('toolboxid', 'navigator-toolbox');
let pin_button = utils.createElement(document, 'toolbarbutton', {
'id': 'pin-button'
});
pin_button.onclick = function (event) {
if (event.which !== 1) {
return;
}
let newstate = mainWindow.getAttribute('tabspinned') === 'true' ? 'false' : 'true';
mainWindow.setAttribute('tabspinned', newstate);
ss.setWindowValue(window, 'TCtabspinned', newstate);
if (newstate === 'true') {
window.VerticalTabs.sendPing('tab_center_pinned', window);
pin_button.setAttribute('tooltiptext', `${strings.sidebarShrink}`);
} else {
window.VerticalTabs.sendPing('tab_center_unpinned', window);
pin_button.setAttribute('tooltiptext', `${strings.sidebarOpen}`);
document.getElementById('verticaltabs-box').removeAttribute('search_expanded');
}
window.VerticalTabs.resizeFindInput();
window.VerticalTabs.resizeTabs();
};
let tooltiptext = mainWindow.getAttribute('tabspinned') === 'true' ? strings.sidebarShrink : strings.sidebarOpen;
pin_button.setAttribute('tooltiptext', tooltiptext);
toolbar.appendChild(pin_button);
leftbox.insertBefore(toolbar, leftbox.firstChild);
let find_input = utils.createElement(document, 'textbox', {
'id': 'find-input',
'class': 'searchbar-textbox'
});
let search_icon = utils.createElement(document, 'image', {
'id': 'tabs-search'
});
find_input.appendChild(search_icon);
search_icon.addEventListener('click', function (e) {
find_input.focus();
});
find_input.addEventListener('input', () => {
this.search_engaged = true;
this.filtertabs.call(this);
});
this.window.addEventListener('keyup', (e) => {
if(e.keyCode === 27) {
this.clearFind();
}
});
find_input.onfocus = () => { this.sendPing('tab_center_search_focus', window);};
find_input.onblur = () => {
let details = {search_engaged: this.search_engaged};
this.sendPing('tab_center_search_blur', window, details);
this.search_engaged = false;
};
this.search_engaged = false;
//build button to toggle Tab Center on/off
let toptabsbutton = utils.createElement(document, 'toolbarbutton', {
'id': 'top-tabs-button',
'label': strings.topLabel,
'tooltiptext': strings.topTooltip
});
toptabsbutton.onclick = (e) => {
if (e.which !== 1) {
return;
}
mainWindow.setAttribute('toggledon', 'false');
set('[email protected]', Date.now().toString());
ss.setWindowValue(window, 'TCtoggledon', mainWindow.getAttribute('toggledon'));
window.VerticalTabs.sendPing('tab_center_toggled_off', window);
this.init();
if (mainWindow.getAttribute('F11-fullscreen') === 'true'){
window.FullScreen._updateToolbars(true);
window.FullScreen._isChromeCollapsed = false;
window.FullScreen.hideNavToolbox();
document.getElementById('TabsToolbar').appendChild(document.getElementById('window-controls'));
}
};
leftbox.contextMenuOpen = false;
let contextMenuHidden = (event) => {
//don't catch close events from tooltips
if (event.originalTarget.tagName === 'xul:menupopup' || event.originalTarget.tagName === 'menupopup') {
leftbox.contextMenuOpen = false;
// give user time to move mouse back in after closing context menu,
// also allow for event to finish before checking for this.mouseInside
window.setTimeout(() => {
exit();
}, 200);
}
};
document.addEventListener('popuphidden', contextMenuHidden);
leftbox.addEventListener('contextmenu', function (event) {
if (event.target.tagName === 'tab' || event.target.id === 'new-tab-button' || event.target.id === 'pin-button' || event.target.id === 'find-input') {
this.contextMenuOpen = true;
}
});
document.getElementById('filler-tab').addEventListener('click', this.clearFind.bind(this));
let spacer = utils.createElement(document, 'spacer', {'id': 'new-tab-spacer'});
toolbar.insertBefore(find_input, pin_button);
toolbar.insertBefore(spacer, pin_button);
toolbar.insertBefore(toptabsbutton, toolbar.lastChild);
this.resizeFindInput();
//remove option to movetopanel or removefromtoolbar from the new-tab-button
let oldOnViewToolbarsPopupShowing = window.onViewToolbarsPopupShowing;
window.onViewToolbarsPopupShowing = function (aEvent, aInsertPoint) {
oldOnViewToolbarsPopupShowing(aEvent, aInsertPoint);
if (aEvent.explicitOriginalTarget.id === 'new-tab-button') {
aEvent.target.querySelector('.customize-context-moveToPanel').setAttribute('disabled', true);
aEvent.target.querySelector('.customize-context-removeFromToolbar').setAttribute('disabled', true);
}
};
let enterTimeout = -1;
let exit = (event) => {
if (!this.mouseInside) {
let arrowscrollbox = this.document.getAnonymousElementByAttribute(tabs, 'anonid', 'arrowscrollbox');
let scrollbox = this.document.getAnonymousElementByAttribute(arrowscrollbox, 'anonid', 'scrollbox');
let scrolltop = scrollbox.scrollTop;
if (enterTimeout > 0) {
window.clearTimeout(enterTimeout);
enterTimeout = -1;
}
if (mainWindow.getAttribute('tabspinned') !== 'true' && leftbox.getAttribute('search_expanded') !== 'true' && !leftbox.contextMenuOpen) {
arrowscrollbox.skipNextScroll = true;
leftbox.removeAttribute('expanded');
this.clearFind();
this.adjustCrop();
let tabsPopup = document.getElementById('alltabs-popup');
if (tabsPopup.state === 'open') {
tabsPopup.hidePopup();
}
}
tabs.removeAttribute('mouseInside');
scrollbox.scrollTop = scrolltop;
}
};
let enter = (event) => {
let shouldExpand = tabs.getAttribute('mouseInside') !== 'true' &&
leftbox.getAttribute('expanded') !== 'true';
let arrowscrollbox = this.document.getAnonymousElementByAttribute(tabs, 'anonid', 'arrowscrollbox');
if (shouldExpand) {
arrowscrollbox.skipNextScroll = true;
this.recordExpansion();
if (event.type === 'mouseenter') {
enterTimeout = window.setTimeout(() => {
leftbox.setAttribute('expanded', 'true');
this.adjustCrop();
}, 300);
} else if (event.pageX <= 4) {
if (enterTimeout > 0) {
window.clearTimeout(enterTimeout);
enterTimeout = -1;
}
leftbox.setAttribute('expanded', 'true');
this.adjustCrop();
}
}
this.mouseEntered();
};
let pauseBeforeExit = () => {
this.mouseExited();
window.setTimeout(() => {
exit();
}, 200);
};
tabs.ondragleave = function (e) {
if (!e.relatedTarget || !e.relatedTarget.closest('#tabbrowser-tabs')) {
if (tabs.getAttribute('movingtab') === 'true') {
let scrollbox = document.getAnonymousElementByAttribute(tabs, 'anonid', 'arrowscrollbox');
let scrollbuttonDown = document.getAnonymousElementByAttribute(scrollbox, 'anonid', 'scrollbutton-down');
let scrollbuttonUp = document.getAnonymousElementByAttribute(scrollbox, 'anonid', 'scrollbutton-up');
scrollbuttonUp.onmouseout();
scrollbuttonDown.onmouseout();
}
}
};
leftbox.addEventListener('mouseenter', enter);
leftbox.addEventListener('mousemove', enter);
leftbox.addEventListener('mouseleave', pauseBeforeExit);
leftbox.addEventListener('mousedown', (event) => {
// Don't register clicks on stuff in the leftbar if it's not open.
if (event.mozInputSource === Ci.nsIDOMMouseEvent.MOZ_SOURCE_TOUCH &&
leftbox.getAttribute('expanded') !== 'true') {
event.stopPropagation();
}
}, true);
tabs.addEventListener('TabOpen', this, false);
tabs.addEventListener('TabClose', this, false);
window.setTimeout(() => {
if (mainWindow.getAttribute('tabspinned') === 'true') {
leftbox.setAttribute('expanded', 'true');
}
for (let i = 0; i < tabs.childNodes.length; i++) {
this.initTab(tabs.childNodes[i]);
}
}, 150);
function checkCompactTheme() {
if(window.getComputedStyle(document.documentElement).getPropertyValue('--lwt-header-image').indexOf('browser/defaultthemes/compact') >= 0) {
mainWindow.setAttribute('compact-theme', true);
}
}
function checkDevTheme() {
if (/devedition/.test(mainWindow.style.backgroundImage)) {
mainWindow.setAttribute('devedition-theme', 'true');
} else {
mainWindow.removeAttribute('devedition-theme');
}
}
checkDevTheme();
checkCompactTheme();
let beforeListener = function () {
browserPanel.insertBefore(top, browserPanel.firstChild);
restorePlacesControllers();
browserPanel.insertBefore(bottom, document.getElementById('fullscreen-warning').nextSibling);
top.palette = palette;
};
window.addEventListener('beforecustomization', beforeListener);
let changeListener = () => {
setDefaultPrefs();
};
window.addEventListener('customizationchange', changeListener);
let afterListener = function () {
contentbox.insertBefore(top, contentbox.firstChild);
restorePlacesControllers();
contentbox.appendChild(bottom);
top.palette = palette;
checkDevTheme();
checkCompactTheme();