You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.6 KiB
JavaScript

3 years ago
export class FileGroup extends HTMLElement {
textEn = {}
3 years ago
textEs = {}
3 years ago
constructor() {
super()
this.fileCount = {value: 0}
3 years ago
this.language = navigator.language
this.attachShadow({mode: 'open'})
this.headerEl = document.createElement('div')
this.headerEl.classList.add('header')
this.contentEl = document.createElement('div')
this.contentEl.classList.add('content')
this.shadowRoot.appendChild(this.headerEl)
this.shadowRoot.appendChild(this.contentEl)
const bGroup = document.createElement(
'm-forms-button-group'
)
this.shadowRoot.appendChild(bGroup)
this.contentEl.addEventListener(
'click-add-above',
e => {
const el = document.createElement(
'm-editor-file-view'
)
el.fileCount = this.fileCount
el.codeMirror = this.codeMirror
e.target.insertAdjacentElement(
'beforebegin', el
)
this.fileCount.value += 1
},
)
this.contentEl.addEventListener(
'click-add-below',
e => {
const el = document.createElement(
'm-editor-file-view'
)
el.fileCount = this.fileCount
el.codeMirror = this.codeMirror
e.target.insertAdjacentElement(
'afterend', el
)
this.fileCount.value += 1
},
)
3 years ago
}
connectedCallback() {
const style = document.createElement('style')
style.textContent = `
:host {
display: flex;
flex-direction: column;
align-items: stretch;
}
div.header {
display: flex;
flex-direction: row;
}
div.files {
display: flex;
flex-direction: column;
flex-grow: 1;
overflow-y: auto;
}
`
this.shadowRoot.appendChild(style)
if (this.contentEl.childNodes.length === 0) {
this.addFile()
}
}
3 years ago
addFile({name, data, collapsed} = {}) {
3 years ago
const el = document.createElement('m-editor-file-view')
el.fileCount = this.fileCount
el.codeMirror = this.codeMirror
3 years ago
if (name !== undefined) {
el.name = name
}
if (data !== undefined) {
el.data = data
}
3 years ago
if (collapsed !== undefined) {
el.collapsed = collapsed
}
3 years ago
this.contentEl.appendChild(el)
this.fileCount.value += 1
3 years ago
return el
}
get language() {
return this._language
}
set language(language) {
this._language = language
this.text = this.langEs ? this.textEs : this.textEn
}
get langEs() {
return /^es\b/.test(this.language)
}
get files() {
return [...this.contentEl.children]
}
}