Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 1 | import '@polymer/paper-button/paper-button.js'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 2 | import './add-language-dialog'; |
| 3 | |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 4 | import {css, html, LitElement} from 'lit'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 5 | import {customElement, property} from 'lit/decorators.js'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 6 | import {map} from 'lit/directives/map.js'; |
| 7 | |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 8 | import {isoLangs} from '../../../common/consts'; |
| 9 | import {msg} from '../../../common/i18n'; |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 10 | import {TargetLangs} from '../../../common/options'; |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 11 | import {SHARED_STYLES} from '../../shared/shared-styles'; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 12 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 13 | @customElement('languages-editor') |
| 14 | export default class LanguagesEditor extends LitElement { |
| 15 | @property({type: Object}) languages: TargetLangs; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 16 | |
| 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ínez | b9180ef | 2022-05-29 20:26:43 +0200 | [diff] [blame] | 25 | background-color: #fafafa; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 26 | 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ínez | b9180ef | 2022-05-29 20:26:43 +0200 | [diff] [blame] | 41 | background-color: white; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 42 | -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ínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | #languages li .movebtn { |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 55 | font-size: 15px; |
| 56 | } |
| 57 | |
| 58 | #languages li .movebtn:not([disabled]) { |
| 59 | color: #1f649d; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #languages li :is(.delete, .movebtn) { |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 63 | 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ínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 72 | #languages li paper-button:is(:hover, :focus) { |
| 73 | background: rgba(0, 0, 0, 0.06); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 74 | } |
| 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ínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 92 | render() { |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 93 | const languageCodes = Object.values(this.languages ?? {}); |
| 94 | const languageList = map(languageCodes, (lang, i) => { |
| 95 | const moveBtns = []; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 96 | if (i != 0) { |
| 97 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 98 | <paper-button |
| 99 | class="movebtn" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 100 | @click=${() => this.swapLanguages(i, i - 1)}> |
| 101 | ↑ |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 102 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 103 | `); |
| 104 | } else { |
| 105 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 106 | <paper-button class="movebtn" disabled> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 107 | ↑ |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 108 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 109 | `); |
| 110 | } |
| 111 | if (i != languageCodes.length - 1) { |
| 112 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 113 | <paper-button |
| 114 | class="movebtn" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 115 | @click=${() => this.swapLanguages(i, i + 1)}> |
| 116 | ↓ |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 117 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 118 | `); |
| 119 | } else { |
| 120 | moveBtns.push(html` |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 121 | <paper-button class="movebtn" disabled> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 122 | ↓ |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 123 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 124 | `); |
| 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ínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 133 | <paper-button |
| 134 | class="delete" |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 135 | @click=${() => this.deleteLanguage(lang)}> |
| 136 | × |
Adrià Vilanova Martínez | 33ee9cd | 2022-06-01 01:06:53 +0200 | [diff] [blame] | 137 | </paper-button> |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 138 | </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ínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 162 | save(languageCodes: string[]) { |
| 163 | const translateinto = Object.assign({}, languageCodes); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 164 | chrome.storage.sync.set({translateinto}); |
| 165 | } |
| 166 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 167 | deleteLanguage(deleteLang: string) { |
| 168 | const languageCodes = |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 169 | Object.values(this.languages ?? {}).filter(lang => lang != deleteLang); |
| 170 | this.save(languageCodes); |
| 171 | } |
| 172 | |
Adrià Vilanova Martínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 173 | swapLanguages(i: number, j: number) { |
| 174 | const languageCodes = Object.values(this.languages ?? {}); |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 175 | 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ínez | 2b50e91 | 2022-06-01 00:05:40 +0200 | [diff] [blame] | 181 | const tmp = languageCodes[j]; |
Adrià Vilanova Martínez | e5263f1 | 2022-05-29 19:22:13 +0200 | [diff] [blame] | 182 | languageCodes[j] = languageCodes[i]; |
| 183 | languageCodes[i] = tmp; |
| 184 | this.save(languageCodes); |
| 185 | } |
| 186 | } |