blob: d6f3e52bd52506bda64f745fe47e6d79e5270244 [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)}>
avm99963874d3f62022-06-04 00:32:41 +0200106 <ha-svg-icon .path=${mdiArrowUp}></ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200107 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200108 `);
109 } else {
110 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200111 <paper-button class="movebtn" disabled>
avm99963874d3f62022-06-04 00:32:41 +0200112 <ha-svg-icon .path=${mdiArrowUp}></ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200113 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200114 `);
115 }
116 if (i != languageCodes.length - 1) {
117 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200118 <paper-button
119 class="movebtn"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200120 @click=${() => this.swapLanguages(i, i + 1)}>
avm99963874d3f62022-06-04 00:32:41 +0200121 <ha-svg-icon .path=${mdiArrowDown}></ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200122 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200123 `);
124 } else {
125 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200126 <paper-button class="movebtn" disabled>
avm99963874d3f62022-06-04 00:32:41 +0200127 <ha-svg-icon .path=${mdiArrowDown}></ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200128 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200129 `);
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ínez33ee9cd2022-06-01 01:06:53 +0200138 <paper-button
139 class="delete"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200140 @click=${() => this.deleteLanguage(lang)}>
avm99963874d3f62022-06-04 00:32:41 +0200141 <ha-svg-icon .path=${mdiClose}></ha-svg-icon>
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200142 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200143 </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ínez2b50e912022-06-01 00:05:40 +0200167 save(languageCodes: string[]) {
168 const translateinto = Object.assign({}, languageCodes);
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200169 chrome.storage.sync.set({translateinto});
170 }
171
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200172 deleteLanguage(deleteLang: string) {
173 const languageCodes =
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200174 Object.values(this.languages ?? {}).filter(lang => lang != deleteLang);
175 this.save(languageCodes);
176 }
177
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200178 swapLanguages(i: number, j: number) {
179 const languageCodes = Object.values(this.languages ?? {});
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200180 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ínez2b50e912022-06-01 00:05:40 +0200186 const tmp = languageCodes[j];
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200187 languageCodes[j] = languageCodes[i];
188 languageCodes[i] = tmp;
189 this.save(languageCodes);
190 }
191}