blob: 155d4e5dd94c097a413b6ec1c5579304d9afc6ce [file] [log] [blame]
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02001import {css, html, LitElement} from 'lit';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02002import {customElement, property} from 'lit/decorators.js';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02003
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02004import {IsoLang, isoLangs} from '../../../common/consts';
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02005import {msg} from '../../../common/i18n';
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +02006import {TargetLangs} from '../../../common/options';
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02007import {DIALOG_STYLES} from '../../shared/dialog-styles';
8import {SHARED_STYLES} from '../../shared/shared-styles';
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02009
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020010interface IsoLangWCode extends IsoLang {
11 code: string;
12}
13
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020014const ALL_LANGUAGES =
15 Object.entries(isoLangs)
16 .map(entry => {
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020017 const lang: IsoLangWCode = Object.assign(entry[1], {code: entry[0]});
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020018 return lang;
19 })
20 .sort((a, b) => a.name < b.name ? -1 : (a.name > b.name ? 1 : 0));
21
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020022@customElement('add-language-dialog')
23export default class AddLanguageDialog extends LitElement {
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020024 static properties = {
25 languages: {type: Object},
26 };
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020027 @property({type: Object}) languages: TargetLangs;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020028
29 static get styles() {
30 return [
31 SHARED_STYLES,
32 DIALOG_STYLES,
33 css`
34 dialog {
35 max-height: 430px;
36 width: 430px;
37 }
38
39 #language_label {
40 font-size: 12px;
41 }
42
43 select {
44 width: 100%;
45 }
46
47 .action_buttons {
48 border-top: none;
49 padding-top: 0;
50 }
51 `,
52 ];
53 }
54
55 constructor() {
56 super();
57 this.addEventListener('show-add-language-dialog', this.showDialog);
58 }
59
60 render() {
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020061 const languageCodes = Object.values(this.languages ?? {});
62 const languages = ALL_LANGUAGES
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020063 .filter(lang => {
64 return !languageCodes.includes(lang.code);
65 })
66 .map(lang => {
67 return html`
68 <option value=${lang.code}>
69 ${lang?.name} (${lang?.nativeName})
70 </option>
71 `;
72 });
73
74 return html`
75 <dialog>
76 <div class="scrollable">
77 <h3>${msg('options_addlanguage')}</h3>
78 <div class="content_area">
79 <label id="language_label" for="select_language">
80 ${msg('options_language_label')}
81 </label>
82 <select id="select_language">${languages}</select>
83 </div>
84 </div>
85 <div class="action_buttons">
86 <button @click=${this.closeDialog}>
87 ${msg('options_cancel')}
88 </button>
89 <button @click=${this.addLanguage}>
90 ${msg('options_addlanguage_addbutton')}
91 </button>
92 </div>
93 </dialog>
94 `;
95 }
96
97 showDialog() {
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +020098 const dialog = this.renderRoot.querySelector('dialog');
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +020099 dialog.showModal();
100 }
101
102 closeDialog() {
103 this.renderRoot.querySelector('dialog').close();
104 }
105
106 addLanguage() {
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200107 const languageCodes = Object.values(this.languages ?? {});
108 const select = this.renderRoot.querySelector('#select_language') as HTMLSelectElement;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200109
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200110 const newLang = select.value;
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200111 languageCodes.push(newLang);
Adrià Vilanova Martínez2b50e912022-06-01 00:05:40 +0200112 const translateinto = Object.assign({}, languageCodes);
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +0200113 chrome.storage.sync.set({translateinto}, () => {
114 select.selectedIndex = 0;
115 this.closeDialog();
116 });
117 }
118}