blob: 7b85618a77853ec2b1fb0c4b1c61c0b1e09101ec [file] [log] [blame]
Adrià Vilanova Martíneze5263f12022-05-29 19:22:13 +02001import {css, html, LitElement} from 'lit';
2
3import {isoLangs} from '../../../common/consts.js';
4import {msg} from '../../../common/i18n.js';
5import {DIALOG_STYLES} from '../../shared/dialog-styles.js';
6import {SHARED_STYLES} from '../../shared/shared-styles.js';
7
8const ALL_LANGUAGES =
9 Object.entries(isoLangs)
10 .map(entry => {
11 let lang = entry[1];
12 lang.code = entry[0];
13 return lang;
14 })
15 .sort((a, b) => a.name < b.name ? -1 : (a.name > b.name ? 1 : 0));
16
17export class AddLanguageDialog extends LitElement {
18 static properties = {
19 languages: {type: Object},
20 };
21
22 static get styles() {
23 return [
24 SHARED_STYLES,
25 DIALOG_STYLES,
26 css`
27 dialog {
28 max-height: 430px;
29 width: 430px;
30 }
31
32 #language_label {
33 font-size: 12px;
34 }
35
36 select {
37 width: 100%;
38 }
39
40 .action_buttons {
41 border-top: none;
42 padding-top: 0;
43 }
44 `,
45 ];
46 }
47
48 constructor() {
49 super();
50 this.addEventListener('show-add-language-dialog', this.showDialog);
51 }
52
53 render() {
54 let languageCodes = Object.values(this.languages ?? {});
55 let languages = ALL_LANGUAGES
56 .filter(lang => {
57 return !languageCodes.includes(lang.code);
58 })
59 .map(lang => {
60 return html`
61 <option value=${lang.code}>
62 ${lang?.name} (${lang?.nativeName})
63 </option>
64 `;
65 });
66
67 return html`
68 <dialog>
69 <div class="scrollable">
70 <h3>${msg('options_addlanguage')}</h3>
71 <div class="content_area">
72 <label id="language_label" for="select_language">
73 ${msg('options_language_label')}
74 </label>
75 <select id="select_language">${languages}</select>
76 </div>
77 </div>
78 <div class="action_buttons">
79 <button @click=${this.closeDialog}>
80 ${msg('options_cancel')}
81 </button>
82 <button @click=${this.addLanguage}>
83 ${msg('options_addlanguage_addbutton')}
84 </button>
85 </div>
86 </dialog>
87 `;
88 }
89
90 showDialog() {
91 let dialog = this.renderRoot.querySelector('dialog');
92 dialog.showModal();
93 }
94
95 closeDialog() {
96 this.renderRoot.querySelector('dialog').close();
97 }
98
99 addLanguage() {
100 let languageCodes = Object.values(this.languages ?? {});
101 let select = this.renderRoot.querySelector('#select_language');
102
103 let newLang = select.value;
104 languageCodes.push(newLang);
105 let translateinto = Object.assign({}, languageCodes);
106 chrome.storage.sync.set({translateinto}, () => {
107 select.selectedIndex = 0;
108 this.closeDialog();
109 });
110 }
111}
112customElements.define('add-language-dialog', AddLanguageDialog);