Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 1 | import {css, html, LitElement} from 'lit'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 2 | import {customElement, property} from 'lit/decorators.js'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 3 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 4 | import {IsoLang, isoLangs} from '../../../common/consts'; |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 5 | import {msg} from '../../../common/i18n'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 6 | import {TargetLangs} from '../../../common/options'; |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 7 | import {DIALOG_STYLES} from '../../shared/dialog-styles'; |
| 8 | import {SHARED_STYLES} from '../../shared/shared-styles'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 9 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 10 | interface IsoLangWCode extends IsoLang { |
| 11 | code: string; |
| 12 | } |
| 13 | |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 14 | const ALL_LANGUAGES = |
| 15 | Object.entries(isoLangs) |
| 16 | .map(entry => { |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 17 | const lang: IsoLangWCode = Object.assign(entry[1], {code: entry[0]}); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 18 | return lang; |
| 19 | }) |
| 20 | .sort((a, b) => a.name < b.name ? -1 : (a.name > b.name ? 1 : 0)); |
| 21 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 22 | @customElement('add-language-dialog') |
| 23 | export default class AddLanguageDialog extends LitElement { |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 24 | static properties = { |
| 25 | languages: {type: Object}, |
| 26 | }; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 27 | @property({type: Object}) languages: TargetLangs; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 28 | |
| 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ínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 61 | const languageCodes = Object.values(this.languages ?? {}); |
| 62 | const languages = ALL_LANGUAGES |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 63 | .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ínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 98 | const dialog = this.renderRoot.querySelector('dialog'); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 99 | dialog.showModal(); |
| 100 | } |
| 101 | |
| 102 | closeDialog() { |
| 103 | this.renderRoot.querySelector('dialog').close(); |
| 104 | } |
| 105 | |
| 106 | addLanguage() { |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 107 | const languageCodes = Object.values(this.languages ?? {}); |
| 108 | const select = this.renderRoot.querySelector('#select_language') as HTMLSelectElement; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 109 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 110 | const newLang = select.value; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 111 | languageCodes.push(newLang); |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 112 | const translateinto = Object.assign({}, languageCodes); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 113 | chrome.storage.sync.set({translateinto}, () => { |
| 114 | select.selectedIndex = 0; |
| 115 | this.closeDialog(); |
| 116 | }); |
| 117 | } |
| 118 | } |