blob: 03ee2eb4e6e0965c2e09055db644cefef5666f2d [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ínez8a08d5a2023-04-12 23:37:31 +020033 padding-top: 0.5rem;
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010034 padding-left: 0.25rem;
35 padding-right: 0.25rem;
36 padding-bottom: 0.5rem;
37 }
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020038
39 .badge-container {
40 padding-right: 0.5rem;
41 border-right: solid gray 1px;
42 margin-right: 0.5rem;
43 }
44
45 .TWPT-badge {
46 --icon-size: 17px;
47 }
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010048 `,
49 ];
50
51 nestedViewRef = createRef();
52
53 constructor() {
54 super();
55 this.options = {};
56 }
57
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +020058 renderBadge() {
59 return html`
60 <div class="badge-container">
61 <div class="TWPT-badge">
62 <md-icon>repeat</md-icon>
63 </div>
64 </div>
65 `;
66 }
67
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010068 renderFlattenRepliesSwitch() {
69 if (!this.options.flattenthreads) return nothing;
70
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +020071 const nestedViewMsg = msg('Nested view', {
72 desc:
73 'Label for the switch which lets users enable/disable the nested view in a thread.',
74 });
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010075 return html`
Adrià Vilanova Martínez5e81b0b2023-04-13 00:04:28 +020076 <md-formfield label="${nestedViewMsg}">
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010077 <md-switch ${ref(this.nestedViewRef)}
78 ?selected=${!this.options?.flattenthreads_switch_enabled}
79 @click=${this._flattenThreadsChanged}>
80 </md-formfield>
81 `;
82 }
83
84 render() {
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020085 // NOTE: Keep this in sync!
86 if (!this.options.flattenthreads) return nothing;
87
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010088 return html`
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020089 <div class="toolbar">
90 ${this.renderBadge()}
91 ${this.renderFlattenRepliesSwitch()}
92 </div>
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010093 `;
94 }
95
96 _flattenThreadsChanged() {
97 const enabled = !this.nestedViewRef.value.selected;
98 const e = new CustomEvent(kEventFlattenThreadsUpdated, {
99 bubbles: true,
100 composed: true,
101 detail: {enabled},
102 });
103 this.dispatchEvent(e);
104 }
105}
106window.customElements.define(
107 'twpt-thread-toolbar-inject', TwptThreadToolbarInject);