move page-actions to a component; fix downloading

page-actions-component
Benjamin Atkin 3 years ago
parent cd467c4443
commit 810924b677

@ -10,6 +10,7 @@ import { Dropdown } from "/menu/dropdown.js"
customElements.define('m-layout', Layout) customElements.define('m-layout', Layout)
customElements.define('m-page', Page) customElements.define('m-page', Page)
customElements.define('m-page-actions', PageActions)
customElements.define( customElements.define(
'm-file-group-page', FileGroupPage 'm-file-group-page', FileGroupPage
) )
@ -41,7 +42,6 @@ class Setup {
const layout = document.createElement( const layout = document.createElement(
'm-layout' 'm-layout'
) )
layout.pageActions = new PageActions()
layout.csp = undefined layout.csp = undefined
layout.header.menu.handleLinks = true layout.header.menu.handleLinks = true
document.body.appendChild(layout) document.body.appendChild(layout)
@ -62,11 +62,9 @@ class Setup {
} }
load() { load() {
const layout = document.createElement('m-layout')
layout.pageActions = new PageActions()
if (this.registration.active) { if (this.registration.active) {
document.body.appendChild( document.body.appendChild(
layout document.createElement('m-layout')
) )
} }
} }

@ -6,6 +6,17 @@ export class Layout extends HTMLElement {
this.attachShadow({mode: 'open'}) this.attachShadow({mode: 'open'})
this.csp = "default-src 'self' 'unsafe-inline' 'unsafe-eval'" this.csp = "default-src 'self' 'unsafe-inline' 'unsafe-eval'"
this.header = document.createElement('m-header') this.header = document.createElement('m-header')
this.dialogWrap = document.createElement('div')
this.header.dialogWrap = this.dialogWrap
this.pageActions = document.createElement(
'm-page-actions'
)
this.header.pageActions = this.pageActions
this.shadowRoot.append(
this.header,
this.dialogWrap,
this.pageActions,
)
} }
connectedCallback() { connectedCallback() {
@ -29,7 +40,6 @@ export class Layout extends HTMLElement {
} }
` `
this.shadowRoot.appendChild(style) this.shadowRoot.appendChild(style)
this.header.pageActions = this.pageActions
this.header.editing = this.editing this.header.editing = this.editing
this.header.addEventListener('click-edit', () => { this.header.addEventListener('click-edit', () => {
this.editing = !this.editing this.editing = !this.editing
@ -37,11 +47,6 @@ export class Layout extends HTMLElement {
this.header.addEventListener('create-page', () => { this.header.addEventListener('create-page', () => {
this.editing = true this.editing = true
}) })
this.shadowRoot.appendChild(this.header)
this.dialogWrap = document.createElement('div')
this.header.dialogWrap = this.dialogWrap
this.pageActions.dialogWrap = this.dialogWrap
this.shadowRoot.appendChild(this.dialogWrap)
this.load() this.load()
addEventListener('hashchange', () => { addEventListener('hashchange', () => {
this.load() this.load()

@ -1,9 +1,10 @@
export class PageActions { export class PageActions extends HTMLElement {
textEn = { textEn = {
download: 'Download', download: 'Download',
rename: 'Move/Rename', rename: 'Move/Rename',
duplicate: 'Duplicate', duplicate: 'Duplicate',
delete_: 'Delete', delete_: 'Delete',
settings: 'Settings',
confirmDelete: f => ( confirmDelete: f => (
`Are you sure you want to delete ${f}?` `Are you sure you want to delete ${f}?`
), ),
@ -16,6 +17,7 @@ export class PageActions {
rename: 'Mover/Renombrar', rename: 'Mover/Renombrar',
duplicate: 'Duplicar', duplicate: 'Duplicar',
delete_: 'Borrar', delete_: 'Borrar',
settings: 'Configuración',
confirmDelete: f => ( confirmDelete: f => (
`¿Desea borrar ${f}?` `¿Desea borrar ${f}?`
), ),
@ -24,7 +26,11 @@ export class PageActions {
} }
constructor() { constructor() {
super()
this.attachShadow({mode: 'open'})
this.language = navigator.language this.language = navigator.language
this.dialogWrap = document.createElement('div')
this.shadowRoot.append(this.dialogWrap)
this.menuActions = [ this.menuActions = [
{ {
text: this.text.download, text: this.text.download,
@ -42,8 +48,21 @@ export class PageActions {
text: this.text.delete_, text: this.text.delete_,
click: this.delete_.bind(this), click: this.delete_.bind(this),
}, },
//{
// text: this.text.settings,
// click: this.settings.bind(this),
//},
] ]
} }
connectedCallback() {
const style = document.createElement('style')
style.textContent = `
m-dialog::part(footer) {
padding-top: 15px;
}
`
this.shadowRoot.appendChild(style)
}
download() { download() {
const text = localStorage.getItem(this.path) const text = localStorage.getItem(this.path)
@ -76,19 +95,19 @@ export class PageActions {
'm-forms-button-group' 'm-forms-button-group'
) )
bGroup.addPrimary(this.text.rename, () => { bGroup.addPrimary(this.text.rename, () => {
const newPath = input.value const newPath = input.value
const v = localStorage.getItem(newPath) const v = localStorage.getItem(newPath)
if (v !== null || newPath === this.path) { if (v !== null || newPath === this.path) {
if (!errorEl) { if (!errorEl) {
errorEl = document.createElement('p') errorEl = document.createElement('p')
errorEl.style.color = 'red' errorEl.style.color = 'red'
const errText = this.text.alreadyExists const errText = this.text.alreadyExists
errorEl.innerText = errText errorEl.innerText = errText
dialog.bodyEl.appendChild(errorEl) dialog.bodyEl.appendChild(errorEl)
}
return
} }
return localStorage.setItem(
}
localStorage.setItem(
newPath, newPath,
localStorage.getItem(this.path) localStorage.getItem(this.path)
) )
@ -170,6 +189,47 @@ export class PageActions {
dialog.open() dialog.open()
} }
settings() {
const dialog = document.createElement(
'm-dialog'
)
this.dialogWrap.replaceChildren(dialog)
const input = document.createElement('input')
input.value = this.path
input.style.minWidth = '300px'
dialog.bodyEl.appendChild(input)
let errorEl
const bGroup = document.createElement(
'm-forms-button-group'
)
bGroup.addPrimary(this.text.rename, () => {
const newPath = input.value
const v = localStorage.getItem(newPath)
if (v !== null || newPath === this.path) {
if (!errorEl) {
errorEl = document.createElement('p')
errorEl.style.color = 'red'
const errText = this.text.alreadyExists
errorEl.innerText = errText
dialog.bodyEl.appendChild(errorEl)
}
return
}
localStorage.setItem(
newPath,
localStorage.getItem(this.path)
)
localStorage.removeItem(this.path)
dialog.close()
location.hash = newPath
})
bGroup.addCancel(this.text.cancel, () => {
dialog.close()
})
dialog.footerEl.appendChild(bGroup)
dialog.open()
}
get language() { get language() {
return this._language return this._language
} }

@ -18,7 +18,7 @@ async function initCache() {
'/loader/builder.js', '/loader/builder.js',
'/loader/editor-build.js', '/loader/editor-build.js',
'/menu/dropdown.js', '/menu/dropdown.js',
]) //1 ]) //2
} }
self.addEventListener("install", event => { self.addEventListener("install", event => {
@ -54,4 +54,4 @@ self.addEventListener('fetch', event => {
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil(clients.claim()) event.waitUntil(clients.claim())
}) })
Loading…
Cancel
Save