blob: 8e1bcd9ad25ad819556556ddc753466a2bdfd634 [file] [log] [blame]
Adrià Vilanova Martínez2144f4f2023-04-12 23:16:17 +02001import '@material/web/icon/icon.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01002import '@material/web/switch/switch.js';
Renovate botaa5fb8e2024-02-25 18:10:09 +00003import '../../../../common/components/FormField.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +01004
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`
Renovate botaa5fb8e2024-02-25 18:10:09 +000073 <twpt-form-field>
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010074 <md-switch ${ref(this.nestedViewRef)}
75 ?selected=${!this.options?.flattenthreads_switch_enabled}
Renovate botaa5fb8e2024-02-25 18:10:09 +000076 @change=${this._flattenThreadsChanged}>
77 </md-switch>
78 <span slot="label">
79 ${nestedViewMsg}
80 </span>
81 </twpt-form-field>
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010082 `;
83 }
84
85 render() {
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020086 // NOTE: Keep this in sync!
87 if (!this.options.flattenthreads) return nothing;
88
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010089 return html`
Adrià Vilanova Martínez8a08d5a2023-04-12 23:37:31 +020090 <div class="toolbar">
91 ${this.renderBadge()}
92 ${this.renderFlattenRepliesSwitch()}
93 </div>
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010094 `;
95 }
96
97 _flattenThreadsChanged() {
98 const enabled = !this.nestedViewRef.value.selected;
99 const e = new CustomEvent(kEventFlattenThreadsUpdated, {
100 bubbles: true,
101 composed: true,
102 detail: {enabled},
103 });
104 this.dispatchEvent(e);
105 }
106}
107window.customElements.define(
108 'twpt-thread-toolbar-inject', TwptThreadToolbarInject);