blob: c48a3b632a1bcae2249cc902570ad75090a7a577 [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';
3
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02004import {css, html, LitElement} from 'lit';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02005import {customElement, property} from 'lit/decorators.js';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02006import {map} from 'lit/directives/map.js';
7
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02008import {isoLangs} from '../../../common/consts';
9import {msg} from '../../../common/i18n';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020010import {TargetLangs} from '../../../common/options';
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020011import {SHARED_STYLES} from '../../shared/shared-styles';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020012
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020013@customElement('languages-editor')
14export default class LanguagesEditor extends LitElement {
15 @property({type: Object}) languages: TargetLangs;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020016
17 static get styles() {
18 return [
19 SHARED_STYLES,
20 css`
21 #languages_container {
22 width: 300px;
23 height: 250px;
24 border: 1px solid #ccc;
Adrià Vilanova Martínezb9180ef2022-05-29 20:26:43 +020025 background-color: #fafafa;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020026 overflow: auto;
27 }
28
29 #languages {
30 list-style: none;
31 margin: 0;
32 padding: 0;
33 }
34
35 #languages li {
36 display: flex;
37 flex-direction: row;
38 align-items: center;
39 padding: 15px;
40 border-bottom: 1px dashed #ddd;
Adrià Vilanova Martínezb9180ef2022-05-29 20:26:43 +020041 background-color: white;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020042 -webkit-user-select: none;
43 }
44
45 #languages li .label {
46 flex-grow: 1;
47 }
48
49 #languages li .delete {
50 font-size: 18px;
51 color: red;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020052 }
53
54 #languages li .movebtn {
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020055 font-size: 15px;
56 }
57
58 #languages li .movebtn:not([disabled]) {
59 color: #1f649d;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020060 }
61
62 #languages li :is(.delete, .movebtn) {
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020063 font-family: inherit!important;
64 min-width: 28px;
65 width: 28px;
66 min-height: 28px;
67 height: 28px;
68 padding: 4px;
69 margin: 0;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020070 }
71
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020072 #languages li paper-button:is(:hover, :focus) {
73 background: rgba(0, 0, 0, 0.06);
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020074 }
75
76 #languages_footer {
77 width: 300px;
78 height: 35px;
79 background-color: #fff;
80 border: 1px solid #ccc;
81 border-top: 0;
82 }
83
84 #languages_add {
85 margin-inline-start: 4px;
86 margin-top: 4px;
87 }
88 `,
89 ];
90 }
91
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020092 render() {
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020093 const languageCodes = Object.values(this.languages ?? {});
94 const languageList = map(languageCodes, (lang, i) => {
95 const moveBtns = [];
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020096 if (i != 0) {
97 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +020098 <paper-button
99 class="movebtn"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200100 @click=${() => this.swapLanguages(i, i - 1)}>
101
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200102 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200103 `);
104 } else {
105 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200106 <paper-button class="movebtn" disabled>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200107
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200108 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200109 `);
110 }
111 if (i != languageCodes.length - 1) {
112 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200113 <paper-button
114 class="movebtn"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200115 @click=${() => this.swapLanguages(i, i + 1)}>
116
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200117 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200118 `);
119 } else {
120 moveBtns.push(html`
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200121 <paper-button class="movebtn" disabled>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200122
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200123 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200124 `);
125 }
126
127 return html`
128 <li data-id=${lang}>
129 <span class="label">
130 ${isoLangs?.[lang]?.['name']} (${isoLangs?.[lang]?.nativeName})
131 </span>
132 ${moveBtns}
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200133 <paper-button
134 class="delete"
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200135 @click=${() => this.deleteLanguage(lang)}>
136 ×
Adrià Vilanova Martínez33ee9cd2022-06-01 01:06:53 +0200137 </paper-button>
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200138 </li>
139 `;
140 });
141
142 return html`
143 <div id="languages_container">
144 <ul id="languages">${languageList}</ul>
145 </div>
146 <div id="languages_footer">
147 <button @click=${this.showAddLanguageDialog} id="languages_add">
148 ${msg('options_addlanguage_addbutton')}
149 </button>
150 </div>
151
152 <add-language-dialog .languages=${this.languages}></add-language-dialog>
153 `;
154 }
155
156 showAddLanguageDialog() {
157 const e = new CustomEvent(
158 'show-add-language-dialog', {bubbles: true, composed: true});
159 this.renderRoot.querySelector('add-language-dialog').dispatchEvent(e);
160 }
161
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200162 save(languageCodes: string[]) {
163 const translateinto = Object.assign({}, languageCodes);
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200164 chrome.storage.sync.set({translateinto});
165 }
166
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200167 deleteLanguage(deleteLang: string) {
168 const languageCodes =
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200169 Object.values(this.languages ?? {}).filter(lang => lang != deleteLang);
170 this.save(languageCodes);
171 }
172
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200173 swapLanguages(i: number, j: number) {
174 const languageCodes = Object.values(this.languages ?? {});
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200175 if (i >= languageCodes.length || j >= languageCodes.length || i < 0 ||
176 j < 0) {
177 console.error(
178 'Can\'t swap languages because the indexes are out of the range.');
179 return;
180 }
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200181 const tmp = languageCodes[j];
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200182 languageCodes[j] = languageCodes[i];
183 languageCodes[i] = tmp;
184 this.save(languageCodes);
185 }
186}