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 | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 33 | padding-top: 0.5rem; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 34 | padding-left: 0.25rem; |
| 35 | padding-right: 0.25rem; |
| 36 | padding-bottom: 0.5rem; |
| 37 | } |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 38 | |
| 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 48 | `, |
| 49 | ]; |
| 50 | |
| 51 | nestedViewRef = createRef(); |
| 52 | |
| 53 | constructor() { |
| 54 | super(); |
| 55 | this.options = {}; |
| 56 | } |
| 57 | |
Adrià Vilanova Martínez | 2144f4f | 2023-04-12 23:16:17 +0200 | [diff] [blame] | 58 | 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 68 | renderFlattenRepliesSwitch() { |
| 69 | if (!this.options.flattenthreads) return nothing; |
| 70 | |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 71 | 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ínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 75 | return html` |
Adrià Vilanova Martínez | 5e81b0b | 2023-04-13 00:04:28 +0200 | [diff] [blame] | 76 | <md-formfield label="${nestedViewMsg}"> |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 77 | <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ínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 85 | // NOTE: Keep this in sync! |
| 86 | if (!this.options.flattenthreads) return nothing; |
| 87 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 88 | return html` |
Adrià Vilanova Martínez | 8a08d5a | 2023-04-12 23:37:31 +0200 | [diff] [blame] | 89 | <div class="toolbar"> |
| 90 | ${this.renderBadge()} |
| 91 | ${this.renderFlattenRepliesSwitch()} |
| 92 | </div> |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 93 | `; |
| 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 | } |
| 106 | window.customElements.define( |
| 107 | 'twpt-thread-toolbar-inject', TwptThreadToolbarInject); |