Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 1 | import '@polymer/paper-button/paper-button.js'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 2 | import './add-language-dialog'; |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 3 | import '../framework/ha-svg-icon'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 4 | |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 5 | import {mdiArrowDown, mdiArrowUp, mdiClose} from '@mdi/js'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 6 | import {css, html, LitElement} from 'lit'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 7 | import {customElement, property} from 'lit/decorators.js'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 8 | import {map} from 'lit/directives/map.js'; |
| 9 | |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 10 | import {isoLangs} from '../../../common/consts'; |
| 11 | import {msg} from '../../../common/i18n'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 12 | import {TargetLangs} from '../../../common/options'; |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 13 | import {SHARED_STYLES} from '../../shared/shared-styles'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 14 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 15 | @customElement('languages-editor') |
| 16 | export default class LanguagesEditor extends LitElement { |
| 17 | @property({type: Object}) languages: TargetLangs; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 18 | |
| 19 | static get styles() { |
| 20 | return [ |
| 21 | SHARED_STYLES, |
| 22 | css` |
| 23 | #languages_container { |
| 24 | width: 300px; |
| 25 | height: 250px; |
| 26 | border: 1px solid #ccc; |
Adrià Vilanova Martínez | b9180ef | 2022-05-29 20:26:43 +0200 | [diff] [blame] | 27 | background-color: #fafafa; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 28 | overflow: auto; |
| 29 | } |
| 30 | |
| 31 | #languages { |
| 32 | list-style: none; |
| 33 | margin: 0; |
| 34 | padding: 0; |
| 35 | } |
| 36 | |
| 37 | #languages li { |
| 38 | display: flex; |
| 39 | flex-direction: row; |
| 40 | align-items: center; |
| 41 | padding: 15px; |
| 42 | border-bottom: 1px dashed #ddd; |
Adrià Vilanova Martínez | b9180ef | 2022-05-29 20:26:43 +0200 | [diff] [blame] | 43 | background-color: white; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 44 | -webkit-user-select: none; |
| 45 | } |
| 46 | |
| 47 | #languages li .label { |
| 48 | flex-grow: 1; |
| 49 | } |
| 50 | |
| 51 | #languages li .delete { |
| 52 | font-size: 18px; |
| 53 | color: red; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | #languages li .movebtn { |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 57 | font-size: 15px; |
| 58 | } |
| 59 | |
| 60 | #languages li .movebtn:not([disabled]) { |
| 61 | color: #1f649d; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #languages li :is(.delete, .movebtn) { |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 65 | min-width: 28px; |
| 66 | width: 28px; |
| 67 | min-height: 28px; |
| 68 | height: 28px; |
| 69 | padding: 4px; |
| 70 | margin: 0; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 71 | } |
| 72 | |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 73 | #languages li ha-svg-icon { |
| 74 | --mdc-icon-size: 16px; |
| 75 | } |
| 76 | |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 77 | #languages li paper-button:is(:hover, :focus) { |
| 78 | background: rgba(0, 0, 0, 0.06); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #languages_footer { |
| 82 | width: 300px; |
| 83 | height: 35px; |
| 84 | background-color: #fff; |
| 85 | border: 1px solid #ccc; |
| 86 | border-top: 0; |
| 87 | } |
| 88 | |
| 89 | #languages_add { |
| 90 | margin-inline-start: 4px; |
| 91 | margin-top: 4px; |
| 92 | } |
| 93 | `, |
| 94 | ]; |
| 95 | } |
| 96 | |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 97 | render() { |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 98 | const languageCodes = Object.values(this.languages ?? {}); |
| 99 | const languageList = map(languageCodes, (lang, i) => { |
| 100 | const moveBtns = []; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 101 | if (i != 0) { |
| 102 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 103 | <paper-button |
| 104 | class="movebtn" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 105 | @click=${() => this.swapLanguages(i, i - 1)}> |
Adrià Vilanova Martínez | c15219e | 2024-05-28 22:37:10 +0200 | [diff] [blame] | 106 | <ha-svg-icon |
| 107 | .path=${mdiArrowUp} |
| 108 | aria-label="${msg('options_language_moveup')}"> |
| 109 | </ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 110 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 111 | `); |
| 112 | } else { |
| 113 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 114 | <paper-button class="movebtn" disabled> |
Adrià Vilanova Martínez | c15219e | 2024-05-28 22:37:10 +0200 | [diff] [blame] | 115 | <ha-svg-icon |
| 116 | .path=${mdiArrowUp} |
| 117 | aria-label="${msg('options_language_moveup')}"> |
| 118 | </ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 119 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 120 | `); |
| 121 | } |
| 122 | if (i != languageCodes.length - 1) { |
| 123 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 124 | <paper-button |
| 125 | class="movebtn" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 126 | @click=${() => this.swapLanguages(i, i + 1)}> |
Adrià Vilanova Martínez | c15219e | 2024-05-28 22:37:10 +0200 | [diff] [blame] | 127 | <ha-svg-icon |
| 128 | .path=${mdiArrowDown} |
| 129 | aria-label="${msg('options_language_movedown')}"> |
| 130 | </ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 131 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 132 | `); |
| 133 | } else { |
| 134 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 135 | <paper-button class="movebtn" disabled> |
Adrià Vilanova Martínez | c15219e | 2024-05-28 22:37:10 +0200 | [diff] [blame] | 136 | <ha-svg-icon |
| 137 | .path=${mdiArrowDown} |
| 138 | aria-label="${msg('options_language_movedown')}"> |
| 139 | </ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 140 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 141 | `); |
| 142 | } |
| 143 | |
| 144 | return html` |
| 145 | <li data-id=${lang}> |
| 146 | <span class="label"> |
| 147 | ${isoLangs?.[lang]?.['name']} (${isoLangs?.[lang]?.nativeName}) |
| 148 | </span> |
| 149 | ${moveBtns} |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 150 | <paper-button |
| 151 | class="delete" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 152 | @click=${() => this.deleteLanguage(lang)}> |
Adrià Vilanova Martínez | c15219e | 2024-05-28 22:37:10 +0200 | [diff] [blame] | 153 | <ha-svg-icon |
| 154 | .path=${mdiClose} |
| 155 | aria-label="${msg('options_language_remove')}"> |
| 156 | </ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 157 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 158 | </li> |
| 159 | `; |
| 160 | }); |
| 161 | |
| 162 | return html` |
| 163 | <div id="languages_container"> |
| 164 | <ul id="languages">${languageList}</ul> |
| 165 | </div> |
| 166 | <div id="languages_footer"> |
| 167 | <button @click=${this.showAddLanguageDialog} id="languages_add"> |
| 168 | ${msg('options_addlanguage_addbutton')} |
| 169 | </button> |
| 170 | </div> |
| 171 | |
| 172 | <add-language-dialog .languages=${this.languages}></add-language-dialog> |
| 173 | `; |
| 174 | } |
| 175 | |
| 176 | showAddLanguageDialog() { |
| 177 | const e = new CustomEvent( |
| 178 | 'show-add-language-dialog', {bubbles: true, composed: true}); |
| 179 | this.renderRoot.querySelector('add-language-dialog').dispatchEvent(e); |
| 180 | } |
| 181 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 182 | save(languageCodes: string[]) { |
| 183 | const translateinto = Object.assign({}, languageCodes); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 184 | chrome.storage.sync.set({translateinto}); |
| 185 | } |
| 186 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 187 | deleteLanguage(deleteLang: string) { |
| 188 | const languageCodes = |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 189 | Object.values(this.languages ?? {}).filter(lang => lang != deleteLang); |
| 190 | this.save(languageCodes); |
| 191 | } |
| 192 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 193 | swapLanguages(i: number, j: number) { |
| 194 | const languageCodes = Object.values(this.languages ?? {}); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 195 | if (i >= languageCodes.length || j >= languageCodes.length || i < 0 || |
| 196 | j < 0) { |
| 197 | console.error( |
| 198 | 'Can\'t swap languages because the indexes are out of the range.'); |
| 199 | return; |
| 200 | } |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 201 | const tmp = languageCodes[j]; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 202 | languageCodes[j] = languageCodes[i]; |
| 203 | languageCodes[i] = tmp; |
| 204 | this.save(languageCodes); |
| 205 | } |
| 206 | } |