1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | import { ArrayExt, each } from '@lumino/algorithm';
|
11 |
|
12 | import { IDisposable } from '@lumino/disposable';
|
13 |
|
14 | import { ElementExt } from '@lumino/domutils';
|
15 |
|
16 | import { Drag } from '@lumino/dragdrop';
|
17 |
|
18 | import { Message, MessageLoop } from '@lumino/messaging';
|
19 |
|
20 | import { ISignal, Signal } from '@lumino/signaling';
|
21 |
|
22 | import {
|
23 | ElementARIAAttrs,
|
24 | ElementDataset,
|
25 | ElementInlineStyle,
|
26 | h,
|
27 | VirtualDOM,
|
28 | VirtualElement
|
29 | } from '@lumino/virtualdom';
|
30 |
|
31 | import { Title } from './title';
|
32 |
|
33 | import { Widget } from './widget';
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export class TabBar<T> extends Widget {
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | constructor(options: TabBar.IOptions<T> = {}) {
|
50 | super({ node: Private.createNode() });
|
51 | this.addClass('lm-TabBar');
|
52 |
|
53 | this.addClass('p-TabBar');
|
54 |
|
55 | this.contentNode.setAttribute('role', 'tablist');
|
56 | this.setFlag(Widget.Flag.DisallowLayout);
|
57 | this._document = options.document || document;
|
58 | this.tabsMovable = options.tabsMovable || false;
|
59 | this.titlesEditable = options.titlesEditable || false;
|
60 | this.allowDeselect = options.allowDeselect || false;
|
61 | this.addButtonEnabled = options.addButtonEnabled || false;
|
62 | this.insertBehavior = options.insertBehavior || 'select-tab-if-needed';
|
63 | this.name = options.name || '';
|
64 | this.orientation = options.orientation || 'horizontal';
|
65 | this.removeBehavior = options.removeBehavior || 'select-tab-after';
|
66 | this.renderer = options.renderer || TabBar.defaultRenderer;
|
67 | }
|
68 |
|
69 | |
70 |
|
71 |
|
72 | dispose(): void {
|
73 | this._releaseMouse();
|
74 | this._titles.length = 0;
|
75 | this._previousTitle = null;
|
76 | super.dispose();
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | get currentChanged(): ISignal<this, TabBar.ICurrentChangedArgs<T>> {
|
91 | return this._currentChanged;
|
92 | }
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | get tabMoved(): ISignal<this, TabBar.ITabMovedArgs<T>> {
|
103 | return this._tabMoved;
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | get tabActivateRequested(): ISignal<
|
116 | this,
|
117 | TabBar.ITabActivateRequestedArgs<T>
|
118 | > {
|
119 | return this._tabActivateRequested;
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 | get addRequested(): ISignal<this, void> {
|
126 | return this._addRequested;
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | get tabCloseRequested(): ISignal<this, TabBar.ITabCloseRequestedArgs<T>> {
|
136 | return this._tabCloseRequested;
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | get tabDetachRequested(): ISignal<this, TabBar.ITabDetachRequestedArgs<T>> {
|
152 | return this._tabDetachRequested;
|
153 | }
|
154 |
|
155 | |
156 |
|
157 |
|
158 | readonly renderer: TabBar.IRenderer<T>;
|
159 |
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 | get document(): Document | ShadowRoot {
|
166 | return this._document;
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | tabsMovable: boolean;
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 | get titlesEditable(): boolean {
|
182 | return this._titlesEditable;
|
183 | }
|
184 |
|
185 | |
186 |
|
187 |
|
188 |
|
189 | set titlesEditable(value: boolean) {
|
190 | this._titlesEditable = value;
|
191 | }
|
192 |
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | allowDeselect: boolean;
|
200 |
|
201 | |
202 |
|
203 |
|
204 | insertBehavior: TabBar.InsertBehavior;
|
205 |
|
206 | |
207 |
|
208 |
|
209 | removeBehavior: TabBar.RemoveBehavior;
|
210 |
|
211 | |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | get currentTitle(): Title<T> | null {
|
218 | return this._titles[this._currentIndex] || null;
|
219 | }
|
220 |
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | set currentTitle(value: Title<T> | null) {
|
228 | this.currentIndex = value ? this._titles.indexOf(value) : -1;
|
229 | }
|
230 |
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | get currentIndex(): number {
|
238 | return this._currentIndex;
|
239 | }
|
240 |
|
241 | |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | set currentIndex(value: number) {
|
248 |
|
249 | if (value < 0 || value >= this._titles.length) {
|
250 | value = -1;
|
251 | }
|
252 |
|
253 |
|
254 | if (this._currentIndex === value) {
|
255 | return;
|
256 | }
|
257 |
|
258 |
|
259 | let pi = this._currentIndex;
|
260 | let pt = this._titles[pi] || null;
|
261 |
|
262 |
|
263 | let ci = value;
|
264 | let ct = this._titles[ci] || null;
|
265 |
|
266 |
|
267 | this._currentIndex = ci;
|
268 | this._previousTitle = pt;
|
269 |
|
270 |
|
271 | this.update();
|
272 |
|
273 |
|
274 | this._currentChanged.emit({
|
275 | previousIndex: pi,
|
276 | previousTitle: pt,
|
277 | currentIndex: ci,
|
278 | currentTitle: ct
|
279 | });
|
280 | }
|
281 |
|
282 | |
283 |
|
284 |
|
285 | get name(): string {
|
286 | return this._name;
|
287 | }
|
288 |
|
289 | |
290 |
|
291 |
|
292 | set name(value: string) {
|
293 | this._name = value;
|
294 | if (value) {
|
295 | this.contentNode.setAttribute('aria-label', value);
|
296 | } else {
|
297 | this.contentNode.removeAttribute('aria-label');
|
298 | }
|
299 | }
|
300 |
|
301 | |
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | get orientation(): TabBar.Orientation {
|
308 | return this._orientation;
|
309 | }
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | set orientation(value: TabBar.Orientation) {
|
318 |
|
319 | if (this._orientation === value) {
|
320 | return;
|
321 | }
|
322 |
|
323 |
|
324 | this._releaseMouse();
|
325 |
|
326 |
|
327 | this._orientation = value;
|
328 | this.dataset['orientation'] = value;
|
329 | this.contentNode.setAttribute('aria-orientation', value);
|
330 | }
|
331 |
|
332 | |
333 |
|
334 |
|
335 | get addButtonEnabled(): boolean {
|
336 | return this._addButtonEnabled;
|
337 | }
|
338 |
|
339 | |
340 |
|
341 |
|
342 | set addButtonEnabled(value: boolean) {
|
343 |
|
344 | if (this._addButtonEnabled === value) {
|
345 | return;
|
346 | }
|
347 |
|
348 | this._addButtonEnabled = value;
|
349 | if (value) {
|
350 | this.addButtonNode.classList.remove('lm-mod-hidden');
|
351 | } else {
|
352 | this.addButtonNode.classList.add('lm-mod-hidden');
|
353 | }
|
354 | }
|
355 |
|
356 | |
357 |
|
358 |
|
359 | get titles(): ReadonlyArray<Title<T>> {
|
360 | return this._titles;
|
361 | }
|
362 |
|
363 | |
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | get contentNode(): HTMLUListElement {
|
372 | return this.node.getElementsByClassName(
|
373 | 'lm-TabBar-content'
|
374 | )[0] as HTMLUListElement;
|
375 | }
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | get addButtonNode(): HTMLDivElement {
|
386 | return this.node.getElementsByClassName(
|
387 | 'lm-TabBar-addButton'
|
388 | )[0] as HTMLDivElement;
|
389 | }
|
390 |
|
391 | |
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 | addTab(value: Title<T> | Title.IOptions<T>): Title<T> {
|
403 | return this.insertTab(this._titles.length, value);
|
404 | }
|
405 |
|
406 | |
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | insertTab(index: number, value: Title<T> | Title.IOptions<T>): Title<T> {
|
422 |
|
423 | this._releaseMouse();
|
424 |
|
425 |
|
426 | let title = Private.asTitle(value);
|
427 |
|
428 |
|
429 | let i = this._titles.indexOf(title);
|
430 |
|
431 |
|
432 | let j = Math.max(0, Math.min(index, this._titles.length));
|
433 |
|
434 |
|
435 | if (i === -1) {
|
436 |
|
437 | ArrayExt.insert(this._titles, j, title);
|
438 |
|
439 |
|
440 | title.changed.connect(this._onTitleChanged, this);
|
441 |
|
442 |
|
443 | this.update();
|
444 |
|
445 |
|
446 | this._adjustCurrentForInsert(j, title);
|
447 |
|
448 |
|
449 | return title;
|
450 | }
|
451 |
|
452 |
|
453 |
|
454 |
|
455 | if (j === this._titles.length) {
|
456 | j--;
|
457 | }
|
458 |
|
459 |
|
460 | if (i === j) {
|
461 | return title;
|
462 | }
|
463 |
|
464 |
|
465 | ArrayExt.move(this._titles, i, j);
|
466 |
|
467 |
|
468 | this.update();
|
469 |
|
470 |
|
471 | this._adjustCurrentForMove(i, j);
|
472 |
|
473 |
|
474 | return title;
|
475 | }
|
476 |
|
477 | |
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 | removeTab(title: Title<T>): void {
|
486 | this.removeTabAt(this._titles.indexOf(title));
|
487 | }
|
488 |
|
489 | |
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 | removeTabAt(index: number): void {
|
498 |
|
499 | this._releaseMouse();
|
500 |
|
501 |
|
502 | let title = ArrayExt.removeAt(this._titles, index);
|
503 |
|
504 |
|
505 | if (!title) {
|
506 | return;
|
507 | }
|
508 |
|
509 |
|
510 | title.changed.disconnect(this._onTitleChanged, this);
|
511 |
|
512 |
|
513 | if (title === this._previousTitle) {
|
514 | this._previousTitle = null;
|
515 | }
|
516 |
|
517 |
|
518 | this.update();
|
519 |
|
520 |
|
521 | this._adjustCurrentForRemove(index, title);
|
522 | }
|
523 |
|
524 | |
525 |
|
526 |
|
527 | clearTabs(): void {
|
528 |
|
529 | if (this._titles.length === 0) {
|
530 | return;
|
531 | }
|
532 |
|
533 |
|
534 | this._releaseMouse();
|
535 |
|
536 |
|
537 | for (let title of this._titles) {
|
538 | title.changed.disconnect(this._onTitleChanged, this);
|
539 | }
|
540 |
|
541 |
|
542 | let pi = this.currentIndex;
|
543 | let pt = this.currentTitle;
|
544 |
|
545 |
|
546 | this._currentIndex = -1;
|
547 | this._previousTitle = null;
|
548 |
|
549 |
|
550 | this._titles.length = 0;
|
551 |
|
552 |
|
553 | this.update();
|
554 |
|
555 |
|
556 | if (pi === -1) {
|
557 | return;
|
558 | }
|
559 |
|
560 |
|
561 | this._currentChanged.emit({
|
562 | previousIndex: pi,
|
563 | previousTitle: pt,
|
564 | currentIndex: -1,
|
565 | currentTitle: null
|
566 | });
|
567 | }
|
568 |
|
569 | |
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 | releaseMouse(): void {
|
577 | this._releaseMouse();
|
578 | }
|
579 |
|
580 | |
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 | handleEvent(event: Event): void {
|
592 | switch (event.type) {
|
593 | case 'mousedown':
|
594 | this._evtMouseDown(event as MouseEvent);
|
595 | break;
|
596 | case 'mousemove':
|
597 | this._evtMouseMove(event as MouseEvent);
|
598 | break;
|
599 | case 'mouseup':
|
600 | this._evtMouseUp(event as MouseEvent);
|
601 | break;
|
602 | case 'pointerdown':
|
603 | this._evtMouseDown(event as MouseEvent);
|
604 | break;
|
605 | case 'pointermove':
|
606 | this._evtMouseMove(event as MouseEvent);
|
607 | break;
|
608 | case 'pointerup':
|
609 | this._evtMouseUp(event as MouseEvent);
|
610 | break;
|
611 | case 'dblclick':
|
612 | this._evtDblClick(event as MouseEvent);
|
613 | break;
|
614 | case 'keydown':
|
615 | this._evtKeyDown(event as KeyboardEvent);
|
616 | break;
|
617 | case 'contextmenu':
|
618 | event.preventDefault();
|
619 | event.stopPropagation();
|
620 | break;
|
621 | }
|
622 | }
|
623 |
|
624 | |
625 |
|
626 |
|
627 | protected onBeforeAttach(msg: Message): void {
|
628 | this.node.addEventListener('mousedown', this);
|
629 | this.node.addEventListener('pointerdown', this);
|
630 | this.node.addEventListener('dblclick', this);
|
631 | }
|
632 |
|
633 | |
634 |
|
635 |
|
636 | protected onAfterDetach(msg: Message): void {
|
637 | this.node.removeEventListener('mousedown', this);
|
638 | this.node.removeEventListener('pointerdown', this);
|
639 | this.node.removeEventListener('dblclick', this);
|
640 | this._releaseMouse();
|
641 | }
|
642 |
|
643 | |
644 |
|
645 |
|
646 | protected onUpdateRequest(msg: Message): void {
|
647 | let titles = this._titles;
|
648 | let renderer = this.renderer;
|
649 | let currentTitle = this.currentTitle;
|
650 | let content = new Array<VirtualElement>(titles.length);
|
651 | for (let i = 0, n = titles.length; i < n; ++i) {
|
652 | let title = titles[i];
|
653 | let current = title === currentTitle;
|
654 | let zIndex = current ? n : n - i - 1;
|
655 | content[i] = renderer.renderTab({ title, current, zIndex });
|
656 | }
|
657 | VirtualDOM.render(content, this.contentNode);
|
658 | }
|
659 |
|
660 | |
661 |
|
662 |
|
663 | private _evtDblClick(event: MouseEvent): void {
|
664 |
|
665 | if (!this.titlesEditable) {
|
666 | return;
|
667 | }
|
668 |
|
669 | let tabs = this.contentNode.children;
|
670 |
|
671 |
|
672 | let index = ArrayExt.findFirstIndex(tabs, tab => {
|
673 | return ElementExt.hitTest(tab, event.clientX, event.clientY);
|
674 | });
|
675 |
|
676 |
|
677 | if (index === -1) {
|
678 | return;
|
679 | }
|
680 |
|
681 | let title = this.titles[index];
|
682 | let label = tabs[index].querySelector('.lm-TabBar-tabLabel') as HTMLElement;
|
683 | if (label && label.contains(event.target as HTMLElement)) {
|
684 | let value = title.label || '';
|
685 |
|
686 |
|
687 | let oldValue = label.innerHTML;
|
688 | label.innerHTML = '';
|
689 |
|
690 | let input = document.createElement('input');
|
691 | input.classList.add('lm-TabBar-tabInput');
|
692 | input.value = value;
|
693 | label.appendChild(input);
|
694 |
|
695 | let onblur = () => {
|
696 | input.removeEventListener('blur', onblur);
|
697 | label.innerHTML = oldValue;
|
698 | };
|
699 |
|
700 | input.addEventListener('dblclick', (event: Event) =>
|
701 | event.stopPropagation()
|
702 | );
|
703 | input.addEventListener('blur', onblur);
|
704 | input.addEventListener('keydown', (event: KeyboardEvent) => {
|
705 | if (event.key === 'Enter') {
|
706 | if (input.value !== '') {
|
707 | title.label = title.caption = input.value;
|
708 | }
|
709 | onblur();
|
710 | } else if (event.key === 'Escape') {
|
711 | onblur();
|
712 | }
|
713 | });
|
714 | input.select();
|
715 | input.focus();
|
716 |
|
717 | if (label.children.length > 0) {
|
718 | (label.children[0] as HTMLElement).focus();
|
719 | }
|
720 | }
|
721 | }
|
722 |
|
723 | |
724 |
|
725 |
|
726 | private _evtKeyDown(event: KeyboardEvent): void {
|
727 |
|
728 | event.preventDefault();
|
729 | event.stopPropagation();
|
730 |
|
731 |
|
732 | if (event.keyCode === 27) {
|
733 | this._releaseMouse();
|
734 | }
|
735 | }
|
736 |
|
737 | |
738 |
|
739 |
|
740 | private _evtMouseDown(event: MouseEvent): void {
|
741 |
|
742 | if (event.button !== 0 && event.button !== 1) {
|
743 | return;
|
744 | }
|
745 |
|
746 |
|
747 | if (this._dragData) {
|
748 | return;
|
749 | }
|
750 |
|
751 |
|
752 | let addButtonClicked =
|
753 | this.addButtonEnabled &&
|
754 | this.addButtonNode.contains(event.target as HTMLElement);
|
755 |
|
756 |
|
757 | let tabs = this.contentNode.children;
|
758 |
|
759 |
|
760 | let index = ArrayExt.findFirstIndex(tabs, tab => {
|
761 | return ElementExt.hitTest(tab, event.clientX, event.clientY);
|
762 | });
|
763 |
|
764 |
|
765 | if (index === -1 && !addButtonClicked) {
|
766 | return;
|
767 | }
|
768 |
|
769 |
|
770 | event.preventDefault();
|
771 | event.stopPropagation();
|
772 |
|
773 |
|
774 | this._dragData = {
|
775 | tab: tabs[index] as HTMLElement,
|
776 | index: index,
|
777 | pressX: event.clientX,
|
778 | pressY: event.clientY,
|
779 | tabPos: -1,
|
780 | tabSize: -1,
|
781 | tabPressPos: -1,
|
782 | targetIndex: -1,
|
783 | tabLayout: null,
|
784 | contentRect: null,
|
785 | override: null,
|
786 | dragActive: false,
|
787 | dragAborted: false,
|
788 | detachRequested: false
|
789 | };
|
790 |
|
791 |
|
792 | this.document.addEventListener('mouseup', this, true);
|
793 | this.document.addEventListener('pointerup', this, true);
|
794 |
|
795 |
|
796 | if (event.button === 1 || addButtonClicked) {
|
797 | return;
|
798 | }
|
799 |
|
800 |
|
801 | let icon = tabs[index].querySelector(this.renderer.closeIconSelector);
|
802 | if (icon && icon.contains(event.target as HTMLElement)) {
|
803 | return;
|
804 | }
|
805 |
|
806 |
|
807 | if (this.tabsMovable) {
|
808 | this.document.addEventListener('mousemove', this, true);
|
809 | this.document.addEventListener('pointermove', this, true);
|
810 | this.document.addEventListener('keydown', this, true);
|
811 | this.document.addEventListener('contextmenu', this, true);
|
812 | }
|
813 |
|
814 |
|
815 | if (this.allowDeselect && this.currentIndex === index) {
|
816 | this.currentIndex = -1;
|
817 | } else {
|
818 | this.currentIndex = index;
|
819 | }
|
820 |
|
821 |
|
822 | if (this.currentIndex === -1) {
|
823 | return;
|
824 | }
|
825 |
|
826 |
|
827 | this._tabActivateRequested.emit({
|
828 | index: this.currentIndex,
|
829 | title: this.currentTitle!
|
830 | });
|
831 | }
|
832 |
|
833 | |
834 |
|
835 |
|
836 | private _evtMouseMove(event: MouseEvent): void {
|
837 |
|
838 | let data = this._dragData;
|
839 | if (!data) {
|
840 | return;
|
841 | }
|
842 |
|
843 |
|
844 | event.preventDefault();
|
845 | event.stopPropagation();
|
846 |
|
847 |
|
848 | let tabs = this.contentNode.children;
|
849 |
|
850 |
|
851 | if (!data.dragActive && !Private.dragExceeded(data, event)) {
|
852 | return;
|
853 | }
|
854 |
|
855 |
|
856 | if (!data.dragActive) {
|
857 |
|
858 | let tabRect = data.tab.getBoundingClientRect();
|
859 | if (this._orientation === 'horizontal') {
|
860 | data.tabPos = data.tab.offsetLeft;
|
861 | data.tabSize = tabRect.width;
|
862 | data.tabPressPos = data.pressX - tabRect.left;
|
863 | } else {
|
864 | data.tabPos = data.tab.offsetTop;
|
865 | data.tabSize = tabRect.height;
|
866 | data.tabPressPos = data.pressY - tabRect.top;
|
867 | }
|
868 | data.tabLayout = Private.snapTabLayout(tabs, this._orientation);
|
869 | data.contentRect = this.contentNode.getBoundingClientRect();
|
870 | data.override = Drag.overrideCursor('default');
|
871 |
|
872 |
|
873 | data.tab.classList.add('lm-mod-dragging');
|
874 | this.addClass('lm-mod-dragging');
|
875 |
|
876 | data.tab.classList.add('p-mod-dragging');
|
877 | this.addClass('p-mod-dragging');
|
878 |
|
879 |
|
880 |
|
881 | data.dragActive = true;
|
882 | }
|
883 |
|
884 |
|
885 | if (!data.detachRequested && Private.detachExceeded(data, event)) {
|
886 |
|
887 | data.detachRequested = true;
|
888 |
|
889 |
|
890 | let index = data.index;
|
891 | let clientX = event.clientX;
|
892 | let clientY = event.clientY;
|
893 | let tab = tabs[index] as HTMLElement;
|
894 | let title = this._titles[index];
|
895 |
|
896 |
|
897 | this._tabDetachRequested.emit({ index, title, tab, clientX, clientY });
|
898 |
|
899 |
|
900 | if (data.dragAborted) {
|
901 | return;
|
902 | }
|
903 | }
|
904 |
|
905 |
|
906 | Private.layoutTabs(tabs, data, event, this._orientation);
|
907 | }
|
908 |
|
909 | |
910 |
|
911 |
|
912 | private _evtMouseUp(event: MouseEvent): void {
|
913 |
|
914 | if (event.button !== 0 && event.button !== 1) {
|
915 | return;
|
916 | }
|
917 |
|
918 |
|
919 | const data = this._dragData;
|
920 | if (!data) {
|
921 | return;
|
922 | }
|
923 |
|
924 |
|
925 | event.preventDefault();
|
926 | event.stopPropagation();
|
927 |
|
928 |
|
929 | this.document.removeEventListener('mousemove', this, true);
|
930 | this.document.removeEventListener('mouseup', this, true);
|
931 | this.document.removeEventListener('pointermove', this, true);
|
932 | this.document.removeEventListener('pointerup', this, true);
|
933 | this.document.removeEventListener('keydown', this, true);
|
934 | this.document.removeEventListener('contextmenu', this, true);
|
935 |
|
936 |
|
937 | if (!data.dragActive) {
|
938 |
|
939 | this._dragData = null;
|
940 |
|
941 |
|
942 | let addButtonClicked =
|
943 | this.addButtonEnabled &&
|
944 | this.addButtonNode.contains(event.target as HTMLElement);
|
945 | if (addButtonClicked) {
|
946 | this._addRequested.emit(undefined);
|
947 | return;
|
948 | }
|
949 |
|
950 |
|
951 | let tabs = this.contentNode.children;
|
952 |
|
953 |
|
954 | let index = ArrayExt.findFirstIndex(tabs, tab => {
|
955 | return ElementExt.hitTest(tab, event.clientX, event.clientY);
|
956 | });
|
957 |
|
958 |
|
959 | if (index !== data.index) {
|
960 | return;
|
961 | }
|
962 |
|
963 |
|
964 | let title = this._titles[index];
|
965 | if (!title.closable) {
|
966 | return;
|
967 | }
|
968 |
|
969 |
|
970 | if (event.button === 1) {
|
971 | this._tabCloseRequested.emit({ index, title });
|
972 | return;
|
973 | }
|
974 |
|
975 |
|
976 | let icon = tabs[index].querySelector(this.renderer.closeIconSelector);
|
977 | if (icon && icon.contains(event.target as HTMLElement)) {
|
978 | this._tabCloseRequested.emit({ index, title });
|
979 | return;
|
980 | }
|
981 |
|
982 |
|
983 | return;
|
984 | }
|
985 |
|
986 |
|
987 | if (event.button !== 0) {
|
988 | return;
|
989 | }
|
990 |
|
991 |
|
992 | Private.finalizeTabPosition(data, this._orientation);
|
993 |
|
994 |
|
995 | data.tab.classList.remove('lm-mod-dragging');
|
996 |
|
997 | data.tab.classList.remove('p-mod-dragging');
|
998 |
|
999 |
|
1000 |
|
1001 | let duration = Private.parseTransitionDuration(data.tab);
|
1002 |
|
1003 |
|
1004 | setTimeout(() => {
|
1005 |
|
1006 | if (data.dragAborted) {
|
1007 | return;
|
1008 | }
|
1009 |
|
1010 |
|
1011 | this._dragData = null;
|
1012 |
|
1013 |
|
1014 | Private.resetTabPositions(this.contentNode.children, this._orientation);
|
1015 |
|
1016 |
|
1017 | data.override!.dispose();
|
1018 |
|
1019 |
|
1020 | this.removeClass('lm-mod-dragging');
|
1021 |
|
1022 | this.removeClass('p-mod-dragging');
|
1023 |
|
1024 |
|
1025 |
|
1026 | let i = data.index;
|
1027 | let j = data.targetIndex;
|
1028 | if (j === -1 || i === j) {
|
1029 | return;
|
1030 | }
|
1031 |
|
1032 |
|
1033 | ArrayExt.move(this._titles, i, j);
|
1034 |
|
1035 |
|
1036 | this._adjustCurrentForMove(i, j);
|
1037 |
|
1038 |
|
1039 | this._tabMoved.emit({
|
1040 | fromIndex: i,
|
1041 | toIndex: j,
|
1042 | title: this._titles[j]
|
1043 | });
|
1044 |
|
1045 |
|
1046 | MessageLoop.sendMessage(this, Widget.Msg.UpdateRequest);
|
1047 | }, duration);
|
1048 | }
|
1049 |
|
1050 | |
1051 |
|
1052 |
|
1053 | private _releaseMouse(): void {
|
1054 |
|
1055 | let data = this._dragData;
|
1056 | if (!data) {
|
1057 | return;
|
1058 | }
|
1059 |
|
1060 |
|
1061 | this._dragData = null;
|
1062 |
|
1063 |
|
1064 | this.document.removeEventListener('mousemove', this, true);
|
1065 | this.document.removeEventListener('mouseup', this, true);
|
1066 | this.document.removeEventListener('pointermove', this, true);
|
1067 | this.document.removeEventListener('pointerup', this, true);
|
1068 | this.document.removeEventListener('keydown', this, true);
|
1069 | this.document.removeEventListener('contextmenu', this, true);
|
1070 |
|
1071 |
|
1072 |
|
1073 | data.dragAborted = true;
|
1074 |
|
1075 |
|
1076 | if (!data.dragActive) {
|
1077 | return;
|
1078 | }
|
1079 |
|
1080 |
|
1081 | Private.resetTabPositions(this.contentNode.children, this._orientation);
|
1082 |
|
1083 |
|
1084 | data.override!.dispose();
|
1085 |
|
1086 |
|
1087 | data.tab.classList.remove('lm-mod-dragging');
|
1088 | this.removeClass('lm-mod-dragging');
|
1089 |
|
1090 | data.tab.classList.remove('p-mod-dragging');
|
1091 | this.removeClass('p-mod-dragging');
|
1092 |
|
1093 | }
|
1094 |
|
1095 | |
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 | private _adjustCurrentForInsert(i: number, title: Title<T>): void {
|
1102 |
|
1103 | let ct = this.currentTitle;
|
1104 | let ci = this._currentIndex;
|
1105 | let bh = this.insertBehavior;
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 | if (bh === 'select-tab' || (bh === 'select-tab-if-needed' && ci === -1)) {
|
1112 | this._currentIndex = i;
|
1113 | this._previousTitle = ct;
|
1114 | this._currentChanged.emit({
|
1115 | previousIndex: ci,
|
1116 | previousTitle: ct,
|
1117 | currentIndex: i,
|
1118 | currentTitle: title
|
1119 | });
|
1120 | return;
|
1121 | }
|
1122 |
|
1123 |
|
1124 | if (ci >= i) {
|
1125 | this._currentIndex++;
|
1126 | }
|
1127 | }
|
1128 |
|
1129 | |
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 | private _adjustCurrentForMove(i: number, j: number): void {
|
1136 | if (this._currentIndex === i) {
|
1137 | this._currentIndex = j;
|
1138 | } else if (this._currentIndex < i && this._currentIndex >= j) {
|
1139 | this._currentIndex++;
|
1140 | } else if (this._currentIndex > i && this._currentIndex <= j) {
|
1141 | this._currentIndex--;
|
1142 | }
|
1143 | }
|
1144 |
|
1145 | |
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 | private _adjustCurrentForRemove(i: number, title: Title<T>): void {
|
1152 |
|
1153 | let ci = this._currentIndex;
|
1154 | let bh = this.removeBehavior;
|
1155 |
|
1156 |
|
1157 | if (ci !== i) {
|
1158 | if (ci > i) {
|
1159 | this._currentIndex--;
|
1160 | }
|
1161 | return;
|
1162 | }
|
1163 |
|
1164 |
|
1165 |
|
1166 |
|
1167 | if (this._titles.length === 0) {
|
1168 | this._currentIndex = -1;
|
1169 | this._currentChanged.emit({
|
1170 | previousIndex: i,
|
1171 | previousTitle: title,
|
1172 | currentIndex: -1,
|
1173 | currentTitle: null
|
1174 | });
|
1175 | return;
|
1176 | }
|
1177 |
|
1178 |
|
1179 | if (bh === 'select-tab-after') {
|
1180 | this._currentIndex = Math.min(i, this._titles.length - 1);
|
1181 | this._currentChanged.emit({
|
1182 | previousIndex: i,
|
1183 | previousTitle: title,
|
1184 | currentIndex: this._currentIndex,
|
1185 | currentTitle: this.currentTitle
|
1186 | });
|
1187 | return;
|
1188 | }
|
1189 |
|
1190 |
|
1191 | if (bh === 'select-tab-before') {
|
1192 | this._currentIndex = Math.max(0, i - 1);
|
1193 | this._currentChanged.emit({
|
1194 | previousIndex: i,
|
1195 | previousTitle: title,
|
1196 | currentIndex: this._currentIndex,
|
1197 | currentTitle: this.currentTitle
|
1198 | });
|
1199 | return;
|
1200 | }
|
1201 |
|
1202 |
|
1203 | if (bh === 'select-previous-tab') {
|
1204 | if (this._previousTitle) {
|
1205 | this._currentIndex = this._titles.indexOf(this._previousTitle);
|
1206 | this._previousTitle = null;
|
1207 | } else {
|
1208 | this._currentIndex = Math.min(i, this._titles.length - 1);
|
1209 | }
|
1210 | this._currentChanged.emit({
|
1211 | previousIndex: i,
|
1212 | previousTitle: title,
|
1213 | currentIndex: this._currentIndex,
|
1214 | currentTitle: this.currentTitle
|
1215 | });
|
1216 | return;
|
1217 | }
|
1218 |
|
1219 |
|
1220 | this._currentIndex = -1;
|
1221 | this._currentChanged.emit({
|
1222 | previousIndex: i,
|
1223 | previousTitle: title,
|
1224 | currentIndex: -1,
|
1225 | currentTitle: null
|
1226 | });
|
1227 | }
|
1228 |
|
1229 | |
1230 |
|
1231 |
|
1232 | private _onTitleChanged(sender: Title<T>): void {
|
1233 | this.update();
|
1234 | }
|
1235 |
|
1236 | private _name: string;
|
1237 | private _currentIndex = -1;
|
1238 | private _titles: Title<T>[] = [];
|
1239 | private _orientation: TabBar.Orientation;
|
1240 | private _document: Document | ShadowRoot;
|
1241 | private _titlesEditable: boolean = false;
|
1242 | private _previousTitle: Title<T> | null = null;
|
1243 | private _dragData: Private.IDragData | null = null;
|
1244 | private _addButtonEnabled: boolean = false;
|
1245 | private _tabMoved = new Signal<this, TabBar.ITabMovedArgs<T>>(this);
|
1246 | private _currentChanged = new Signal<this, TabBar.ICurrentChangedArgs<T>>(
|
1247 | this
|
1248 | );
|
1249 | private _addRequested = new Signal<this, void>(this);
|
1250 | private _tabCloseRequested = new Signal<
|
1251 | this,
|
1252 | TabBar.ITabCloseRequestedArgs<T>
|
1253 | >(this);
|
1254 | private _tabDetachRequested = new Signal<
|
1255 | this,
|
1256 | TabBar.ITabDetachRequestedArgs<T>
|
1257 | >(this);
|
1258 | private _tabActivateRequested = new Signal<
|
1259 | this,
|
1260 | TabBar.ITabActivateRequestedArgs<T>
|
1261 | >(this);
|
1262 | }
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 | export namespace TabBar {
|
1268 | |
1269 |
|
1270 |
|
1271 | export type Orientation =
|
1272 | | |
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 | 'horizontal'
|
1278 |
|
1279 | |
1280 |
|
1281 |
|
1282 |
|
1283 |
|
1284 | | 'vertical';
|
1285 |
|
1286 | |
1287 |
|
1288 |
|
1289 | export type InsertBehavior =
|
1290 | | |
1291 |
|
1292 |
|
1293 | 'none'
|
1294 |
|
1295 | |
1296 |
|
1297 |
|
1298 | | 'select-tab'
|
1299 |
|
1300 | |
1301 |
|
1302 |
|
1303 | | 'select-tab-if-needed';
|
1304 |
|
1305 | |
1306 |
|
1307 |
|
1308 | export type RemoveBehavior =
|
1309 | | |
1310 |
|
1311 |
|
1312 | 'none'
|
1313 |
|
1314 | |
1315 |
|
1316 |
|
1317 | | 'select-tab-after'
|
1318 |
|
1319 | |
1320 |
|
1321 |
|
1322 | | 'select-tab-before'
|
1323 |
|
1324 | |
1325 |
|
1326 |
|
1327 | | 'select-previous-tab';
|
1328 |
|
1329 | |
1330 |
|
1331 |
|
1332 | export interface IOptions<T> {
|
1333 | |
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 | document?: Document | ShadowRoot;
|
1340 |
|
1341 | |
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 | name?: string;
|
1347 |
|
1348 | |
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 | orientation?: TabBar.Orientation;
|
1354 |
|
1355 | |
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 | tabsMovable?: boolean;
|
1361 |
|
1362 | |
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 | allowDeselect?: boolean;
|
1368 |
|
1369 | |
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 | titlesEditable?: boolean;
|
1375 |
|
1376 | |
1377 |
|
1378 |
|
1379 |
|
1380 |
|
1381 | addButtonEnabled?: boolean;
|
1382 |
|
1383 | |
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 | insertBehavior?: TabBar.InsertBehavior;
|
1389 |
|
1390 | |
1391 |
|
1392 |
|
1393 |
|
1394 |
|
1395 | removeBehavior?: TabBar.RemoveBehavior;
|
1396 |
|
1397 | |
1398 |
|
1399 |
|
1400 |
|
1401 |
|
1402 | renderer?: IRenderer<T>;
|
1403 | }
|
1404 |
|
1405 | |
1406 |
|
1407 |
|
1408 | export interface ICurrentChangedArgs<T> {
|
1409 | |
1410 |
|
1411 |
|
1412 | readonly previousIndex: number;
|
1413 |
|
1414 | |
1415 |
|
1416 |
|
1417 | readonly previousTitle: Title<T> | null;
|
1418 |
|
1419 | |
1420 |
|
1421 |
|
1422 | readonly currentIndex: number;
|
1423 |
|
1424 | |
1425 |
|
1426 |
|
1427 | readonly currentTitle: Title<T> | null;
|
1428 | }
|
1429 |
|
1430 | |
1431 |
|
1432 |
|
1433 | export interface ITabMovedArgs<T> {
|
1434 | |
1435 |
|
1436 |
|
1437 | readonly fromIndex: number;
|
1438 |
|
1439 | |
1440 |
|
1441 |
|
1442 | readonly toIndex: number;
|
1443 |
|
1444 | |
1445 |
|
1446 |
|
1447 | readonly title: Title<T>;
|
1448 | }
|
1449 |
|
1450 | |
1451 |
|
1452 |
|
1453 | export interface ITabActivateRequestedArgs<T> {
|
1454 | |
1455 |
|
1456 |
|
1457 | readonly index: number;
|
1458 |
|
1459 | |
1460 |
|
1461 |
|
1462 | readonly title: Title<T>;
|
1463 | }
|
1464 |
|
1465 | |
1466 |
|
1467 |
|
1468 | export interface ITabCloseRequestedArgs<T> {
|
1469 | |
1470 |
|
1471 |
|
1472 | readonly index: number;
|
1473 |
|
1474 | |
1475 |
|
1476 |
|
1477 | readonly title: Title<T>;
|
1478 | }
|
1479 |
|
1480 | |
1481 |
|
1482 |
|
1483 | export interface ITabDetachRequestedArgs<T> {
|
1484 | |
1485 |
|
1486 |
|
1487 | readonly index: number;
|
1488 |
|
1489 | |
1490 |
|
1491 |
|
1492 | readonly title: Title<T>;
|
1493 |
|
1494 | |
1495 |
|
1496 |
|
1497 | readonly tab: HTMLElement;
|
1498 |
|
1499 | |
1500 |
|
1501 |
|
1502 | readonly clientX: number;
|
1503 |
|
1504 | |
1505 |
|
1506 |
|
1507 | readonly clientY: number;
|
1508 | }
|
1509 |
|
1510 | |
1511 |
|
1512 |
|
1513 | export interface IRenderData<T> {
|
1514 | |
1515 |
|
1516 |
|
1517 | readonly title: Title<T>;
|
1518 |
|
1519 | |
1520 |
|
1521 |
|
1522 | readonly current: boolean;
|
1523 |
|
1524 | |
1525 |
|
1526 |
|
1527 | readonly zIndex: number;
|
1528 | }
|
1529 |
|
1530 | |
1531 |
|
1532 |
|
1533 | export interface IRenderer<T> {
|
1534 | |
1535 |
|
1536 |
|
1537 | readonly closeIconSelector: string;
|
1538 |
|
1539 | |
1540 |
|
1541 |
|
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 | renderTab(data: IRenderData<T>): VirtualElement;
|
1547 | }
|
1548 |
|
1549 | |
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 | export class Renderer implements IRenderer<any> {
|
1556 | |
1557 |
|
1558 |
|
1559 | readonly closeIconSelector = '.lm-TabBar-tabCloseIcon';
|
1560 |
|
1561 | |
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 | renderTab(data: IRenderData<any>): VirtualElement {
|
1569 | let title = data.title.caption;
|
1570 | let key = this.createTabKey(data);
|
1571 | let id = key;
|
1572 | let style = this.createTabStyle(data);
|
1573 | let className = this.createTabClass(data);
|
1574 | let dataset = this.createTabDataset(data);
|
1575 | let aria = this.createTabARIA(data);
|
1576 | if (data.title.closable) {
|
1577 | return h.li(
|
1578 | { id, key, className, title, style, dataset, ...aria },
|
1579 | this.renderIcon(data),
|
1580 | this.renderLabel(data),
|
1581 | this.renderCloseIcon(data)
|
1582 | );
|
1583 | } else {
|
1584 | return h.li(
|
1585 | { id, key, className, title, style, dataset, ...aria },
|
1586 | this.renderIcon(data),
|
1587 | this.renderLabel(data)
|
1588 | );
|
1589 | }
|
1590 | }
|
1591 |
|
1592 | |
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 | renderIcon(data: IRenderData<any>): VirtualElement {
|
1600 | const { title } = data;
|
1601 | let className = this.createIconClass(data);
|
1602 |
|
1603 |
|
1604 | if (typeof title.icon === 'string') {
|
1605 | return h.div({ className }, title.iconLabel);
|
1606 | }
|
1607 |
|
1608 |
|
1609 |
|
1610 | return h.div({ className }, title.icon!, title.iconLabel);
|
1611 | }
|
1612 |
|
1613 | |
1614 |
|
1615 |
|
1616 |
|
1617 |
|
1618 |
|
1619 |
|
1620 | renderLabel(data: IRenderData<any>): VirtualElement {
|
1621 | return h.div(
|
1622 | {
|
1623 | className:
|
1624 | 'lm-TabBar-tabLabel' +
|
1625 |
|
1626 | ' p-TabBar-tabLabel'
|
1627 |
|
1628 | },
|
1629 | data.title.label
|
1630 | );
|
1631 | }
|
1632 |
|
1633 | |
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 | renderCloseIcon(data: IRenderData<any>): VirtualElement {
|
1641 | return h.div({
|
1642 | className:
|
1643 | 'lm-TabBar-tabCloseIcon' +
|
1644 |
|
1645 | ' p-TabBar-tabCloseIcon'
|
1646 |
|
1647 | });
|
1648 | }
|
1649 |
|
1650 | |
1651 |
|
1652 |
|
1653 |
|
1654 |
|
1655 |
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 |
|
1662 | createTabKey(data: IRenderData<any>): string {
|
1663 | let key = this._tabKeys.get(data.title);
|
1664 | if (key === undefined) {
|
1665 | key = `tab-key-${this._tabID++}`;
|
1666 | this._tabKeys.set(data.title, key);
|
1667 | }
|
1668 | return key;
|
1669 | }
|
1670 |
|
1671 | |
1672 |
|
1673 |
|
1674 |
|
1675 |
|
1676 |
|
1677 |
|
1678 | createTabStyle(data: IRenderData<any>): ElementInlineStyle {
|
1679 | return { zIndex: `${data.zIndex}` };
|
1680 | }
|
1681 |
|
1682 | |
1683 |
|
1684 |
|
1685 |
|
1686 |
|
1687 |
|
1688 |
|
1689 | createTabClass(data: IRenderData<any>): string {
|
1690 | let name = 'lm-TabBar-tab';
|
1691 |
|
1692 | name += ' p-TabBar-tab';
|
1693 |
|
1694 | if (data.title.className) {
|
1695 | name += ` ${data.title.className}`;
|
1696 | }
|
1697 | if (data.title.closable) {
|
1698 | name += ' lm-mod-closable';
|
1699 |
|
1700 | name += ' p-mod-closable';
|
1701 |
|
1702 | }
|
1703 | if (data.current) {
|
1704 | name += ' lm-mod-current';
|
1705 |
|
1706 | name += ' p-mod-current';
|
1707 |
|
1708 | }
|
1709 | return name;
|
1710 | }
|
1711 |
|
1712 | |
1713 |
|
1714 |
|
1715 |
|
1716 |
|
1717 |
|
1718 |
|
1719 | createTabDataset(data: IRenderData<any>): ElementDataset {
|
1720 | return data.title.dataset;
|
1721 | }
|
1722 |
|
1723 | |
1724 |
|
1725 |
|
1726 |
|
1727 |
|
1728 |
|
1729 |
|
1730 | createTabARIA(data: IRenderData<any>): ElementARIAAttrs {
|
1731 | return { role: 'tab', 'aria-selected': data.current.toString() };
|
1732 | }
|
1733 |
|
1734 | |
1735 |
|
1736 |
|
1737 |
|
1738 |
|
1739 |
|
1740 |
|
1741 | createIconClass(data: IRenderData<any>): string {
|
1742 | let name = 'lm-TabBar-tabIcon';
|
1743 |
|
1744 | name += ' p-TabBar-tabIcon';
|
1745 |
|
1746 | let extra = data.title.iconClass;
|
1747 | return extra ? `${name} ${extra}` : name;
|
1748 | }
|
1749 |
|
1750 | private _tabID = 0;
|
1751 | private _tabKeys = new WeakMap<Title<any>, string>();
|
1752 | }
|
1753 |
|
1754 | |
1755 |
|
1756 |
|
1757 | export const defaultRenderer = new Renderer();
|
1758 |
|
1759 | |
1760 |
|
1761 |
|
1762 | export const addButtonSelector = '.lm-TabBar-addButton';
|
1763 | }
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 | namespace Private {
|
1769 | |
1770 |
|
1771 |
|
1772 | export const DRAG_THRESHOLD = 5;
|
1773 |
|
1774 | |
1775 |
|
1776 |
|
1777 | export const DETACH_THRESHOLD = 20;
|
1778 |
|
1779 | |
1780 |
|
1781 |
|
1782 | export interface IDragData {
|
1783 | |
1784 |
|
1785 |
|
1786 | tab: HTMLElement;
|
1787 |
|
1788 | |
1789 |
|
1790 |
|
1791 | index: number;
|
1792 |
|
1793 | |
1794 |
|
1795 |
|
1796 | pressX: number;
|
1797 |
|
1798 | |
1799 |
|
1800 |
|
1801 | pressY: number;
|
1802 |
|
1803 | |
1804 |
|
1805 |
|
1806 |
|
1807 |
|
1808 | tabPos: number;
|
1809 |
|
1810 | |
1811 |
|
1812 |
|
1813 |
|
1814 |
|
1815 | tabSize: number;
|
1816 |
|
1817 | |
1818 |
|
1819 |
|
1820 |
|
1821 |
|
1822 | tabPressPos: number;
|
1823 |
|
1824 | |
1825 |
|
1826 |
|
1827 |
|
1828 |
|
1829 | targetIndex: number;
|
1830 |
|
1831 | |
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 | tabLayout: ITabLayout[] | null;
|
1837 |
|
1838 | |
1839 |
|
1840 |
|
1841 |
|
1842 |
|
1843 | contentRect: ClientRect | null;
|
1844 |
|
1845 | |
1846 |
|
1847 |
|
1848 |
|
1849 |
|
1850 | override: IDisposable | null;
|
1851 |
|
1852 | |
1853 |
|
1854 |
|
1855 | dragActive: boolean;
|
1856 |
|
1857 | |
1858 |
|
1859 |
|
1860 | dragAborted: boolean;
|
1861 |
|
1862 | |
1863 |
|
1864 |
|
1865 | detachRequested: boolean;
|
1866 | }
|
1867 |
|
1868 | |
1869 |
|
1870 |
|
1871 | export interface ITabLayout {
|
1872 | |
1873 |
|
1874 |
|
1875 | margin: number;
|
1876 |
|
1877 | |
1878 |
|
1879 |
|
1880 | pos: number;
|
1881 |
|
1882 | |
1883 |
|
1884 |
|
1885 | size: number;
|
1886 | }
|
1887 |
|
1888 | |
1889 |
|
1890 |
|
1891 | export function createNode(): HTMLDivElement {
|
1892 | let node = document.createElement('div');
|
1893 | let content = document.createElement('ul');
|
1894 | content.setAttribute('role', 'tablist');
|
1895 | content.className = 'lm-TabBar-content';
|
1896 |
|
1897 | content.classList.add('p-TabBar-content');
|
1898 |
|
1899 | node.appendChild(content);
|
1900 |
|
1901 | let add = document.createElement('div');
|
1902 | add.className = 'lm-TabBar-addButton lm-mod-hidden';
|
1903 | node.appendChild(add);
|
1904 | return node;
|
1905 | }
|
1906 |
|
1907 | |
1908 |
|
1909 |
|
1910 | export function asTitle<T>(value: Title<T> | Title.IOptions<T>): Title<T> {
|
1911 | return value instanceof Title ? value : new Title<T>(value);
|
1912 | }
|
1913 |
|
1914 | |
1915 |
|
1916 |
|
1917 | export function parseTransitionDuration(tab: HTMLElement): number {
|
1918 | let style = window.getComputedStyle(tab);
|
1919 | return 1000 * (parseFloat(style.transitionDuration!) || 0);
|
1920 | }
|
1921 |
|
1922 | |
1923 |
|
1924 |
|
1925 | export function snapTabLayout(
|
1926 | tabs: HTMLCollection,
|
1927 | orientation: TabBar.Orientation
|
1928 | ): ITabLayout[] {
|
1929 | let layout = new Array<ITabLayout>(tabs.length);
|
1930 | for (let i = 0, n = tabs.length; i < n; ++i) {
|
1931 | let node = tabs[i] as HTMLElement;
|
1932 | let style = window.getComputedStyle(node);
|
1933 | if (orientation === 'horizontal') {
|
1934 | layout[i] = {
|
1935 | pos: node.offsetLeft,
|
1936 | size: node.offsetWidth,
|
1937 | margin: parseFloat(style.marginLeft!) || 0
|
1938 | };
|
1939 | } else {
|
1940 | layout[i] = {
|
1941 | pos: node.offsetTop,
|
1942 | size: node.offsetHeight,
|
1943 | margin: parseFloat(style.marginTop!) || 0
|
1944 | };
|
1945 | }
|
1946 | }
|
1947 | return layout;
|
1948 | }
|
1949 |
|
1950 | |
1951 |
|
1952 |
|
1953 | export function dragExceeded(data: IDragData, event: MouseEvent): boolean {
|
1954 | let dx = Math.abs(event.clientX - data.pressX);
|
1955 | let dy = Math.abs(event.clientY - data.pressY);
|
1956 | return dx >= DRAG_THRESHOLD || dy >= DRAG_THRESHOLD;
|
1957 | }
|
1958 |
|
1959 | |
1960 |
|
1961 |
|
1962 | export function detachExceeded(data: IDragData, event: MouseEvent): boolean {
|
1963 | let rect = data.contentRect!;
|
1964 | return (
|
1965 | event.clientX < rect.left - DETACH_THRESHOLD ||
|
1966 | event.clientX >= rect.right + DETACH_THRESHOLD ||
|
1967 | event.clientY < rect.top - DETACH_THRESHOLD ||
|
1968 | event.clientY >= rect.bottom + DETACH_THRESHOLD
|
1969 | );
|
1970 | }
|
1971 |
|
1972 | |
1973 |
|
1974 |
|
1975 | export function layoutTabs(
|
1976 | tabs: HTMLCollection,
|
1977 | data: IDragData,
|
1978 | event: MouseEvent,
|
1979 | orientation: TabBar.Orientation
|
1980 | ): void {
|
1981 |
|
1982 | let pressPos: number;
|
1983 | let localPos: number;
|
1984 | let clientPos: number;
|
1985 | let clientSize: number;
|
1986 | if (orientation === 'horizontal') {
|
1987 | pressPos = data.pressX;
|
1988 | localPos = event.clientX - data.contentRect!.left;
|
1989 | clientPos = event.clientX;
|
1990 | clientSize = data.contentRect!.width;
|
1991 | } else {
|
1992 | pressPos = data.pressY;
|
1993 | localPos = event.clientY - data.contentRect!.top;
|
1994 | clientPos = event.clientY;
|
1995 | clientSize = data.contentRect!.height;
|
1996 | }
|
1997 |
|
1998 |
|
1999 | let targetIndex = data.index;
|
2000 | let targetPos = localPos - data.tabPressPos;
|
2001 | let targetEnd = targetPos + data.tabSize;
|
2002 |
|
2003 |
|
2004 | for (let i = 0, n = tabs.length; i < n; ++i) {
|
2005 | let pxPos: string;
|
2006 | let layout = data.tabLayout![i];
|
2007 | let threshold = layout.pos + (layout.size >> 1);
|
2008 | if (i < data.index && targetPos < threshold) {
|
2009 | pxPos = `${data.tabSize + data.tabLayout![i + 1].margin}px`;
|
2010 | targetIndex = Math.min(targetIndex, i);
|
2011 | } else if (i > data.index && targetEnd > threshold) {
|
2012 | pxPos = `${-data.tabSize - layout.margin}px`;
|
2013 | targetIndex = Math.max(targetIndex, i);
|
2014 | } else if (i === data.index) {
|
2015 | let ideal = clientPos - pressPos;
|
2016 | let limit = clientSize - (data.tabPos + data.tabSize);
|
2017 | pxPos = `${Math.max(-data.tabPos, Math.min(ideal, limit))}px`;
|
2018 | } else {
|
2019 | pxPos = '';
|
2020 | }
|
2021 | if (orientation === 'horizontal') {
|
2022 | (tabs[i] as HTMLElement).style.left = pxPos;
|
2023 | } else {
|
2024 | (tabs[i] as HTMLElement).style.top = pxPos;
|
2025 | }
|
2026 | }
|
2027 |
|
2028 |
|
2029 | data.targetIndex = targetIndex;
|
2030 | }
|
2031 |
|
2032 | |
2033 |
|
2034 |
|
2035 | export function finalizeTabPosition(
|
2036 | data: IDragData,
|
2037 | orientation: TabBar.Orientation
|
2038 | ): void {
|
2039 |
|
2040 | let clientSize: number;
|
2041 | if (orientation === 'horizontal') {
|
2042 | clientSize = data.contentRect!.width;
|
2043 | } else {
|
2044 | clientSize = data.contentRect!.height;
|
2045 | }
|
2046 |
|
2047 |
|
2048 | let ideal: number;
|
2049 | if (data.targetIndex === data.index) {
|
2050 | ideal = 0;
|
2051 | } else if (data.targetIndex > data.index) {
|
2052 | let tgt = data.tabLayout![data.targetIndex];
|
2053 | ideal = tgt.pos + tgt.size - data.tabSize - data.tabPos;
|
2054 | } else {
|
2055 | let tgt = data.tabLayout![data.targetIndex];
|
2056 | ideal = tgt.pos - data.tabPos;
|
2057 | }
|
2058 |
|
2059 |
|
2060 | let limit = clientSize - (data.tabPos + data.tabSize);
|
2061 | let final = Math.max(-data.tabPos, Math.min(ideal, limit));
|
2062 |
|
2063 |
|
2064 | if (orientation === 'horizontal') {
|
2065 | data.tab.style.left = `${final}px`;
|
2066 | } else {
|
2067 | data.tab.style.top = `${final}px`;
|
2068 | }
|
2069 | }
|
2070 |
|
2071 | |
2072 |
|
2073 |
|
2074 | export function resetTabPositions(
|
2075 | tabs: HTMLCollection,
|
2076 | orientation: TabBar.Orientation
|
2077 | ): void {
|
2078 | each(tabs, tab => {
|
2079 | if (orientation === 'horizontal') {
|
2080 | (tab as HTMLElement).style.left = '';
|
2081 | } else {
|
2082 | (tab as HTMLElement).style.top = '';
|
2083 | }
|
2084 | });
|
2085 | }
|
2086 | }
|