Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 1 | import '@material/web/icon/icon.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 2 | import '@material/web/switch/switch.js'; |
Renovate bot | aa5fb8e | 2024-02-25 18:10:09 +0000 | [diff] [blame^] | 3 | import '../../../../common/components/FormField.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 5 | import consoleCommonStyles from '!!raw-loader!../../../../static/css/common/console.css'; |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 6 | import {msg} from '@lit/localize'; |
| 7 | import {css, html, nothing, unsafeCSS} from 'lit'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 8 | import {createRef, ref} from 'lit/directives/ref.js'; |
| 9 | |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 10 | import {I18nLitElement} from '../../../../common/litI18nUtils.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 11 | import {SHARED_MD3_STYLES} from '../../../../common/styles/md3.js'; |
| 12 | import {kEventFlattenThreadsUpdated} from '../constants.js'; |
| 13 | |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 14 | export default class TwptThreadToolbarInject extends I18nLitElement { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 15 | static properties = { |
| 16 | options: {type: Object}, |
| 17 | }; |
| 18 | |
| 19 | static styles = [ |
| 20 | SHARED_MD3_STYLES, |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 21 | css`${unsafeCSS(consoleCommonStyles)}`, |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 22 | css` |
| 23 | :host { |
Adrià Vilanova Martínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 24 | display: block; |
| 25 | padding-top: 1rem; |
| 26 | } |
| 27 | |
| 28 | .toolbar { |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 29 | display: flex; |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 30 | flex-flow: row wrap; |
| 31 | align-items: center; |
| 32 | row-gap: 0.5rem; |
Adrià Vilanova Martínez | acb89e2 | 2023-11-16 02:36:47 +0100 | [diff] [blame] | 33 | padding: 0.5rem 0.25rem; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 34 | } |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 35 | |
| 36 | .badge-container { |
Adrià Vilanova Martínez | acb89e2 | 2023-11-16 02:36:47 +0100 | [diff] [blame] | 37 | padding-inline-end: 0.5rem; |
| 38 | border-inline-end: solid gray 1px; |
| 39 | margin-inline-end: 0.5rem; |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | .TWPT-badge { |
| 43 | --icon-size: 17px; |
| 44 | } |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 45 | `, |
| 46 | ]; |
| 47 | |
| 48 | nestedViewRef = createRef(); |
| 49 | |
| 50 | constructor() { |
| 51 | super(); |
| 52 | this.options = {}; |
| 53 | } |
| 54 | |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 55 | 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 65 | renderFlattenRepliesSwitch() { |
| 66 | if (!this.options.flattenthreads) return nothing; |
| 67 | |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 68 | 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 72 | return html` |
Renovate bot | aa5fb8e | 2024-02-25 18:10:09 +0000 | [diff] [blame^] | 73 | <twpt-form-field> |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 74 | <md-switch ${ref(this.nestedViewRef)} |
| 75 | ?selected=${!this.options?.flattenthreads_switch_enabled} |
Renovate bot | aa5fb8e | 2024-02-25 18:10:09 +0000 | [diff] [blame^] | 76 | @change=${this._flattenThreadsChanged}> |
| 77 | </md-switch> |
| 78 | <span slot="label"> |
| 79 | ${nestedViewMsg} |
| 80 | </span> |
| 81 | </twpt-form-field> |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 82 | `; |
| 83 | } |
| 84 | |
| 85 | render() { |
Adrià Vilanova Martínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 86 | // NOTE: Keep this in sync! |
| 87 | if (!this.options.flattenthreads) return nothing; |
| 88 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 89 | return html` |
Adrià Vilanova Martínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 90 | <div class="toolbar"> |
| 91 | ${this.renderBadge()} |
| 92 | ${this.renderFlattenRepliesSwitch()} |
| 93 | </div> |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 94 | `; |
| 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 | } |
| 107 | window.customElements.define( |
| 108 | 'twpt-thread-toolbar-inject', TwptThreadToolbarInject); |