blob: b792f5a1353209eb18fc3b58da581d14da6d5209 [file] [log] [blame]
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +02001import '@polymer/paper-button/paper-button.js';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02002import './add-language-dialog';
avm99963874d3f62022-06-04 00:32:41 +02003import '../framework/ha-svg-icon';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02004
avm99963874d3f62022-06-04 00:32:41 +02005import {mdiArrowDown, mdiArrowUp, mdiClose} from '@mdi/js';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02006import {css, html, LitElement} from 'lit';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02007import {customElement, property} from 'lit/decorators.js';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02008import {map} from 'lit/directives/map.js';
9
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020010import {isoLangs} from '../../../common/consts';
11import {msg} from '../../../common/i18n';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020012import {TargetLangs} from '../../../common/options';
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020013import {SHARED_STYLES} from '../../shared/shared-styles';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020014
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020015@customElement('languages-editor')
16export default class LanguagesEditor extends LitElement {
17 @property({type: Object}) languages: TargetLangs;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020018
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ínezb9180ef2022-05-29 20:26:43 +020027 background-color: #fafafa;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020028 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ínezb9180ef2022-05-29 20:26:43 +020043 background-color: white;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020044 -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íneze5263f12022-05-29 19:22:13 +020054 }
55
56 #languages li .movebtn {
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020057 font-size: 15px;
58 }
59
60 #languages li .movebtn:not([disabled]) {
61 color: #1f649d;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020062 }
63
64 #languages li :is(.delete, .movebtn) {
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020065 min-width: 28px;
66 width: 28px;
67 min-height: 28px;
68 height: 28px;
69 padding: 4px;
70 margin: 0;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020071 }
72
avm99963874d3f62022-06-04 00:32:41 +020073 #languages li ha-svg-icon {
74 --mdc-icon-size: 16px;
75 }
76
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020077 #languages li paper-button:is(:hover, :focus) {
78 background: rgba(0, 0, 0, 0.06);
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020079 }
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íneze5263f12022-05-29 19:22:13 +020097 render() {
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020098 const languageCodes = Object.values(this.languages ?? {});
99 const languageList = map(languageCodes, (lang, i) => {
100 const moveBtns = [];
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200101 if (i != 0) {
102 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200103 <paper-button
104 class="movebtn"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200105 @click=${() => this.swapLanguages(i, i - 1)}>
Adrià Vilanova Martínezc15219e2024-05-28 22:37:10 +0200106 <ha-svg-icon
107 .path=${mdiArrowUp}
108 aria-label="${msg('options_language_moveup')}">
109 </ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200110 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200111 `);
112 } else {
113 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200114 <paper-button class="movebtn" disabled>
Adrià Vilanova Martínezc15219e2024-05-28 22:37:10 +0200115 <ha-svg-icon
116 .path=${mdiArrowUp}
117 aria-label="${msg('options_language_moveup')}">
118 </ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200119 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200120 `);
121 }
122 if (i != languageCodes.length - 1) {
123 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200124 <paper-button
125 class="movebtn"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200126 @click=${() => this.swapLanguages(i, i + 1)}>
Adrià Vilanova Martínezc15219e2024-05-28 22:37:10 +0200127 <ha-svg-icon
128 .path=${mdiArrowDown}
129 aria-label="${msg('options_language_movedown')}">
130 </ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200131 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200132 `);
133 } else {
134 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200135 <paper-button class="movebtn" disabled>
Adrià Vilanova Martínezc15219e2024-05-28 22:37:10 +0200136 <ha-svg-icon
137 .path=${mdiArrowDown}
138 aria-label="${msg('options_language_movedown')}">
139 </ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200140 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200141 `);
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ínez33ee9cd2022-06-01 01:06:53 +0200150 <paper-button
151 class="delete"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200152 @click=${() => this.deleteLanguage(lang)}>
Adrià Vilanova Martínezc15219e2024-05-28 22:37:10 +0200153 <ha-svg-icon
154 .path=${mdiClose}
155 aria-label="${msg('options_language_remove')}">
156 </ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200157 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200158 </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ínez2b50e912022-06-01 00:05:40 +0200182 save(languageCodes: string[]) {
183 const translateinto = Object.assign({}, languageCodes);
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200184 chrome.storage.sync.set({translateinto});
185 }
186
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200187 deleteLanguage(deleteLang: string) {
188 const languageCodes =
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200189 Object.values(this.languages ?? {}).filter(lang => lang != deleteLang);
190 this.save(languageCodes);
191 }
192
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200193 swapLanguages(i: number, j: number) {
194 const languageCodes = Object.values(this.languages ?? {});
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200195 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ínez2b50e912022-06-01 00:05:40 +0200201 const tmp = languageCodes[j];
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200202 languageCodes[j] = languageCodes[i];
203 languageCodes[i] = tmp;
204 this.save(languageCodes);
205 }
206}