Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 1 | import '@material/web/formfield/formfield.js'; |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 2 | import '@material/web/icon/icon.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 3 | import '@material/web/switch/switch.js'; |
| 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` |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 73 | <md-formfield label="${nestedViewMsg}"> |
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} |
| 76 | @click=${this._flattenThreadsChanged}> |
| 77 | </md-formfield> |
| 78 | `; |
| 79 | } |
| 80 | |
| 81 | render() { |
Adrià Vilanova Martínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 82 | // NOTE: Keep this in sync! |
| 83 | if (!this.options.flattenthreads) return nothing; |
| 84 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 85 | return html` |
Adrià Vilanova Martínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 86 | <div class="toolbar"> |
| 87 | ${this.renderBadge()} |
| 88 | ${this.renderFlattenRepliesSwitch()} |
| 89 | </div> |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 90 | `; |
| 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 | } |
| 103 | window.customElements.define( |
| 104 | 'twpt-thread-toolbar-inject', TwptThreadToolbarInject); |