blob: bbaa20d1e817d306bc11cd4da5ad573e8d472e3d [file] [log] [blame]
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01001import '@material/web/formfield/formfield.js';
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +02002import '@material/web/icon/icon.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01003import '@material/web/switch/switch.js';
4
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +02005import consoleCommonStyles from '!!raw-loader!../../../../static/css/common/console.css';
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +02006import {msg} from '@lit/localize';
7import {css, html, nothing, unsafeCSS} from 'lit';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01008import {createRef, ref} from 'lit/directives/ref.js';
9
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +020010import {I18nLitElement} from '../../../../common/litI18nUtils.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010011import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js';
12import {kEventFlattenThreadsUpdated} from '../constants.js';
13
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +020014export default class TwptThreadToolbarInject extends I18nLitElement {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010015 static properties = {
16 options: {type: Object},
17 };
18
19 static styles = [
20 SHARED_MD3_STYLES,
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020021 css`${unsafeCSS(consoleCommonStyles)}`,
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010022 css`
23 :host {
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020024 display: block;
25 padding-top: 1rem;
26 }
27
28 .toolbar {
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010029 display: flex;
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020030 flex-flow: row wrap;
31 align-items: center;
32 row-gap: 0.5rem;
Adrià Vilanova Martínezacb89e22023-11-16 02:36:47 +010033 padding: 0.5rem 0.25rem;
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010034 }
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020035
36 .badge-container {
Adrià Vilanova Martínezacb89e22023-11-16 02:36:47 +010037 padding-inline-end: 0.5rem;
38 border-inline-end: solid gray 1px;
39 margin-inline-end: 0.5rem;
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020040 }
41
42 .TWPT-badge {
43 --icon-size: 17px;
44 }
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010045 `,
46 ];
47
48 nestedViewRef = createRef();
49
50 constructor() {
51 super();
52 this.options = {};
53 }
54
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020055 renderBadge() {
56 return html`
57 <div class="badge-container">
58 <div class="TWPT-badge">
59 <md-icon>repeat</md-icon>
60 </div>
61 </div>
62 `;
63 }
64
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010065 renderFlattenRepliesSwitch() {
66 if (!this.options.flattenthreads) return nothing;
67
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +020068 const nestedViewMsg = msg('Nested view', {
69 desc:
70 'Label for the switch which lets users enable/disable the nested view in a thread.',
71 });
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010072 return html`
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +020073 <md-formfield label="${nestedViewMsg}">
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010074 <md-switch ${ref(this.nestedViewRef)}
75 ?selected=${!this.options?.flattenthreads_switch_enabled}
76 @click=${this._flattenThreadsChanged}>
77 </md-formfield>
78 `;
79 }
80
81 render() {
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020082 // NOTE: Keep this in sync!
83 if (!this.options.flattenthreads) return nothing;
84
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010085 return html`
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020086 <div class="toolbar">
87 ${this.renderBadge()}
88 ${this.renderFlattenRepliesSwitch()}
89 </div>
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010090 `;
91 }
92
93 _flattenThreadsChanged() {
94 const enabled = !this.nestedViewRef.value.selected;
95 const e = new CustomEvent(kEventFlattenThreadsUpdated, {
96 bubbles: true,
97 composed: true,
98 detail: {enabled},
99 });
100 this.dispatchEvent(e);
101 }
102}
103window.customElements.define(
104 'twpt-thread-toolbar-inject', TwptThreadToolbarInject);