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)}> |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 106 | <ha-svg-icon .path=${mdiArrowUp}></ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 107 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 108 | `); |
| 109 | } else { |
| 110 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 111 | <paper-button class="movebtn" disabled> |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 112 | <ha-svg-icon .path=${mdiArrowUp}></ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 113 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 114 | `); |
| 115 | } |
| 116 | if (i != languageCodes.length - 1) { |
| 117 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 118 | <paper-button |
| 119 | class="movebtn" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 120 | @click=${() => this.swapLanguages(i, i + 1)}> |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 121 | <ha-svg-icon .path=${mdiArrowDown}></ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 122 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 123 | `); |
| 124 | } else { |
| 125 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 126 | <paper-button class="movebtn" disabled> |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 127 | <ha-svg-icon .path=${mdiArrowDown}></ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 128 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 129 | `); |
| 130 | } |
| 131 | |
| 132 | return html` |
| 133 | <li data-id=${lang}> |
| 134 | <span class="label"> |
| 135 | ${isoLangs?.[lang]?.['name']} (${isoLangs?.[lang]?.nativeName}) |
| 136 | </span> |
| 137 | ${moveBtns} |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 138 | <paper-button |
| 139 | class="delete" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 140 | @click=${() => this.deleteLanguage(lang)}> |
avm99963 | 874d3f6 | 2022-06-04 00:32:41 +0200 | [diff] [blame] | 141 | <ha-svg-icon .path=${mdiClose}></ha-svg-icon> |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 142 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 143 | </li> |
| 144 | `; |
| 145 | }); |
| 146 | |
| 147 | return html` |
| 148 | <div id="languages_container"> |
| 149 | <ul id="languages">${languageList}</ul> |
| 150 | </div> |
| 151 | <div id="languages_footer"> |
| 152 | <button @click=${this.showAddLanguageDialog} id="languages_add"> |
| 153 | ${msg('options_addlanguage_addbutton')} |
| 154 | </button> |
| 155 | </div> |
| 156 | |
| 157 | <add-language-dialog .languages=${this.languages}></add-language-dialog> |
| 158 | `; |
| 159 | } |
| 160 | |
| 161 | showAddLanguageDialog() { |
| 162 | const e = new CustomEvent( |
| 163 | 'show-add-language-dialog', {bubbles: true, composed: true}); |
| 164 | this.renderRoot.querySelector('add-language-dialog').dispatchEvent(e); |
| 165 | } |
| 166 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 167 | save(languageCodes: string[]) { |
| 168 | const translateinto = Object.assign({}, languageCodes); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 169 | chrome.storage.sync.set({translateinto}); |
| 170 | } |
| 171 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 172 | deleteLanguage(deleteLang: string) { |
| 173 | const languageCodes = |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 174 | Object.values(this.languages ?? {}).filter(lang => lang != deleteLang); |
| 175 | this.save(languageCodes); |
| 176 | } |
| 177 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 178 | swapLanguages(i: number, j: number) { |
| 179 | const languageCodes = Object.values(this.languages ?? {}); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 180 | if (i >= languageCodes.length || j >= languageCodes.length || i < 0 || |
| 181 | j < 0) { |
| 182 | console.error( |
| 183 | 'Can\'t swap languages because the indexes are out of the range.'); |
| 184 | return; |
| 185 | } |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 186 | const tmp = languageCodes[j]; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 187 | languageCodes[j] = languageCodes[i]; |
| 188 | languageCodes[i] = tmp; |
| 189 | this.save(languageCodes); |
| 190 | } |
| 191 | } |