@ -1,14 +1,17 @@
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 } ? `
) ,
) ,
cancel : 'Cancel' ,
cancel : 'Cancel' ,
alreadyExists : 'There is already a page with that name.' ,
alreadyExists : 'There is already a page with that name.' ,
save : 'Guardar' ,
close : 'Close' ,
}
}
textEs = {
textEs = {
@ -16,16 +19,36 @@ 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 } ? `
) ,
) ,
cancel : 'Cancelar' ,
cancel : 'Cancelar' ,
alreadyExists : 'Ya existe una página con ese nombre.' ,
alreadyExists : 'Ya existe una página con ese nombre.' ,
save : 'Guardar' ,
close : 'Cerrar' ,
}
}
constructor ( ) {
constructor ( ) {
super ( )
this . attachShadow ( { mode : 'open' } )
this . language = navigator . language
this . language = navigator . language
this . menuActions = [
this . dialogWrap = document . createElement ( 'div' )
this . shadowRoot . append ( this . dialogWrap )
}
connectedCallback ( ) {
const style = document . createElement ( 'style' )
style . textContent = `
m - dialog : : part ( footer ) {
padding - top : 15 px ;
}
`
this . shadowRoot . appendChild ( style )
}
get menuActions ( ) {
const baseActions = [
{
{
text : this . text . download ,
text : this . text . download ,
click : this . download . bind ( this ) ,
click : this . download . bind ( this ) ,
@ -43,10 +66,21 @@ export class PageActions {
click : this . delete _ . bind ( this ) ,
click : this . delete _ . bind ( this ) ,
} ,
} ,
]
]
if ( this . page . isGroup ) {
return [
... baseActions ,
{
text : this . text . settings ,
click : this . settings . bind ( this ) ,
} ,
]
} else {
return baseActions
}
}
}
download ( ) {
download ( ) {
const text = localStorage . getItem ( this . path )
const text = this . s torage. getItem ( this . path )
const sp = this . path . split ( '/' )
const sp = this . path . split ( '/' )
const filename = sp [ sp . length - 1 ]
const filename = sp [ sp . length - 1 ]
const el = document . createElement ( 'a' )
const el = document . createElement ( 'a' )
@ -76,23 +110,48 @@ 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 = this . storage . 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
const sKeyOld = 'settings/page:' + this . path
}
const sKeyNew = 'settings/page:' + newPath
localStorage . setItem (
const settingsJson = this . storage . getItem ( sKeyOld )
if ( settingsJson ? ? true === true ) {
this . storage . setItem ( sKeyNew , settingsJson )
this . storage . removeItem ( sKeyOld )
let settingsData
try {
settingsData = JSON . parse ( settingsJson )
} catch ( err ) {
settingsData = { }
}
if ( settingsData ? . connections ) {
for ( const dir of [ 'outbound' , 'inbound' ] ) {
const otherDir = (
dir === 'outbound' ? 'inbound' : 'outbound'
)
this . applyInverseRename (
settingsData , dir , otherDir , this . path , newPath
)
}
}
} else {
this . storage . removeItem ( sKeyNew )
}
this . storage . setItem (
newPath ,
newPath ,
localStorage . getItem ( this . path )
this . s torage. getItem ( this . path )
)
)
localStorage . removeItem ( this . path )
this . s torage. removeItem ( this . path )
dialog . close ( )
dialog . close ( )
location . hash = newPath
location . hash = newPath
} )
} )
@ -119,7 +178,7 @@ export class PageActions {
const btnText = this . text . duplicate
const btnText = this . text . duplicate
bGroup . addPrimary ( btnText , ( ) => {
bGroup . addPrimary ( btnText , ( ) => {
const newPath = input . value
const newPath = input . value
const v = localS torage. getItem ( newPath )
const v = this . s torage. 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' )
@ -130,9 +189,13 @@ export class PageActions {
}
}
return
return
}
}
localS torage. setItem (
this . s torage. setItem (
newPath ,
newPath ,
localStorage . getItem ( this . path )
this . storage . getItem ( this . path )
)
this . storage . setItem (
'settings/page:' + newPath ,
this . storage . getItem ( this . path ) ? ? '{}'
)
)
dialog . close ( )
dialog . close ( )
location . hash = newPath
location . hash = newPath
@ -159,7 +222,10 @@ export class PageActions {
'm-forms-button-group'
'm-forms-button-group'
)
)
bGroup . addPrimary ( this . text . delete _ , ( ) => {
bGroup . addPrimary ( this . text . delete _ , ( ) => {
localStorage . removeItem ( this . path )
this . storage . removeItem ( this . path )
this . storage . removeItem (
'settings/page:' + this . path
)
location . hash = '/'
location . hash = '/'
dialog . close ( )
dialog . close ( )
} )
} )
@ -170,6 +236,132 @@ export class PageActions {
dialog . open ( )
dialog . open ( )
}
}
settings ( ) {
const dialog = document . createElement (
'm-dialog'
)
this . dialogWrap . replaceChildren ( dialog )
const settingsEl = document . createElement (
'm-settings-page-settings'
)
settingsEl . cspProfiles = this . cspProfiles
settingsEl . path = this . path
settingsEl . checkExists = path => (
this . storage . getItem ( path ) !== null
)
settingsEl . data = this . page . settings
const h = document . createElement ( 'h2' )
h . innerText = this . text . settings
dialog . headerEl . append ( h )
dialog . bodyEl . append ( settingsEl )
const bGroup = document . createElement (
'm-forms-button-group'
)
bGroup . addPrimary ( this . text . save , ( ) => {
const settingsData = settingsEl . data
this . page . settings = settingsData
for ( const dir of [ 'outbound' , 'inbound' ] ) {
const otherDir = (
dir === 'outbound' ? 'inbound' : 'outbound'
)
this . applyInverseSettings (
settingsData , dir , otherDir
)
}
this . applyTemplateSettings ( )
dialog . close ( )
this . dispatchEvent ( new CustomEvent (
'settings-change' , { bubbles : true , composed : true }
) )
} )
bGroup . addCancel ( this . text . cancel , ( ) => {
dialog . close ( )
} )
dialog . footerEl . appendChild ( bGroup )
dialog . open ( )
}
applyInverseSettings ( settingsData , dir , otherDir ) {
const selfEntries = Object . entries (
settingsData . connections [ dir ]
)
for ( const [ path , access ] of selfEntries ) {
const key = 'settings/page:' + path
let val = this . storage . getItem ( key )
try {
if ( val !== null ) {
val = JSON . parse ( val )
}
} catch ( err ) {
// ignore
}
const data = val ? ? { }
data . connections = data . connections ? ? { }
data . connections [ otherDir ] = (
data . connections [ otherDir ] ? ? { }
)
data . connections [ otherDir ] [ this . path ] = access
this . storage . setItem (
key , JSON . stringify ( data )
)
}
}
applyInverseRename (
settingsData , dir , otherDir , oldPath , newPath
) {
const selfEntries = Object . entries (
settingsData . connections [ dir ] ? ? { }
)
for ( const [ path , access ] of selfEntries ) {
const key = 'settings/page:' + path
let val = this . storage . getItem ( key )
try {
if ( val !== null ) {
val = JSON . parse ( val )
}
} catch ( err ) {
// ignore
}
const data = val ? ? { }
data . connections = data . connections ? ? { }
data . connections [ otherDir ] = (
data . connections [ otherDir ] ? ? { }
)
const accessValue = data . connections [ otherDir ] [ oldPath ]
data . connections [ otherDir ] [ newPath ] = accessValue
data . connections [ otherDir ] [ oldPath ] = undefined
this . storage . setItem (
key , JSON . stringify ( data )
)
}
}
applyTemplateSettings ( ) {
const templates = { }
for ( const key of this . storage . keys ( ) ) {
try {
if ( key . startsWith ( 'settings/page:' ) ) {
const data = JSON . parse (
this . storage . getItem ( key )
)
if ( data . template ) {
const name = key . slice (
'settings/page:' . length
)
templates [ name ] = { name }
}
}
} catch ( err ) {
// do nothing
}
}
this . storage . setItem (
'settings:templates' ,
JSON . stringify ( { templates } ) ,
)
}
get language ( ) {
get language ( ) {
return this . _language
return this . _language
}
}