nfp_sites/nfp_moe/app/admin/editpage.js

264 lines
7.4 KiB
JavaScript
Raw Normal View History

const FileUpload = require('./fileupload')
const PageTree = require('../page_tree')
2019-09-13 13:33:10 +00:00
const api = require('../api')
2022-07-27 08:41:18 +00:00
const Editor = require('./editor')
2019-09-13 13:33:10 +00:00
2022-07-27 08:41:18 +00:00
const EditPage = {
2019-09-13 13:33:10 +00:00
oninit: function(vnode) {
2022-07-27 08:41:18 +00:00
this.loading = false
this.showLoading = null
this.data = {
page: null,
2019-10-02 01:13:15 +00:00
}
2022-07-27 08:41:18 +00:00
this.pages = [{id: null, name: 'Frontpage'}]
this.pages = this.pages.concat(PageTree.getFlatTree())
2022-07-27 08:41:18 +00:00
this.newBanner = null
this.newMedia = null
this.editor = null
2019-10-02 01:13:15 +00:00
this.fetchPage(vnode)
},
2022-07-27 08:41:18 +00:00
onbeforeupdate: function(vnode) {
2019-10-02 01:13:15 +00:00
if (this.lastid !== m.route.param('id')) {
this.fetchPage(vnode)
}
},
fetchPage: function(vnode) {
this.lastid = m.route.param('id')
2022-07-27 08:41:18 +00:00
return this.requestPage(
api.sendRequest({
method: 'GET',
url: '/api/auth/pages/' + (this.lastid === 'add' ? '0' : this.lastid),
})
)
2022-07-27 08:41:18 +00:00
},
requestPage: function(data) {
2019-09-13 13:33:10 +00:00
this.error = ''
2022-07-27 08:41:18 +00:00
if (this.showLoading) {
clearTimeout(this.showLoading)
2019-09-13 13:33:10 +00:00
}
2022-07-27 08:41:18 +00:00
if (this.data.page) {
this.showLoading = setTimeout(() => {
this.showLoading = null
this.loading = true
2019-09-13 13:33:10 +00:00
m.redraw()
2022-07-27 08:41:18 +00:00
}, 150)
} else {
2022-07-27 08:41:18 +00:00
this.loading = true
2019-09-13 13:33:10 +00:00
}
2022-07-27 08:41:18 +00:00
data
.then((result) => {
this.data = result
if (this.data.page.id) {
document.title = 'Editing: ' + this.data.page.name + ' - Admin NFP Moe'
this.editedPath = true
} else {
document.title = 'Create Page - Admin NFP Moe'
}
}, (err) => {
this.error = err.message
})
.then(() => {
clearTimeout(this.showLoading)
this.showLoading = null
this.loading = false
m.redraw()
})
2019-09-13 13:33:10 +00:00
},
updateValue: function(name, e) {
2022-07-27 08:41:18 +00:00
this.data.page[name] = e.currentTarget.value
2019-09-13 13:33:10 +00:00
if (name === 'path') {
this.editedPath = true
} else if (name === 'name' && !this.editedPath) {
2022-07-27 08:41:18 +00:00
this.data.page.path = this.data.page.name.toLowerCase().replace(/ /g, '-')
2019-09-13 13:33:10 +00:00
}
},
updateParent: function(e) {
2022-07-27 08:41:18 +00:00
this.data.page.parent_id = Number(e.currentTarget.value) || null
2019-09-13 13:33:10 +00:00
},
2022-07-27 08:41:18 +00:00
mediaUploaded: function(type, file) {
if (type === 'banner') {
this.newBanner = file
} else {
this.newMedia = file
}
2019-09-13 13:33:10 +00:00
},
2022-07-27 08:41:18 +00:00
mediaRemoved: function(type) {
this.data.page['remove_' + type] = true
this.data.page[type + '_prefix'] = null
2019-09-14 19:03:38 +00:00
},
2019-09-13 13:33:10 +00:00
save: function(vnode, e) {
e.preventDefault()
2022-07-27 08:41:18 +00:00
if (!this.data.page.name) {
2019-09-13 13:33:10 +00:00
this.error = 'Name is missing'
2022-07-27 08:41:18 +00:00
} else if (!this.data.page.path) {
2019-09-13 13:33:10 +00:00
this.error = 'Path is missing'
2021-02-05 11:50:01 +00:00
} else {
this.error = ''
2019-09-13 13:33:10 +00:00
}
if (this.error) return
2022-07-27 08:41:18 +00:00
let formData = new FormData()
if (this.newBanner) {
formData.append('banner', this.newBanner.file)
}
if (this.newMedia) {
formData.append('media', this.newMedia.file)
}
if (this.data.page.id) {
formData.append('id', this.data.page.id)
2019-10-01 03:45:44 +00:00
}
2019-09-13 13:33:10 +00:00
2022-07-27 08:41:18 +00:00
formData.append('name', this.data.page.name)
formData.append('parent_id', this.data.page.parent_id || null)
formData.append('path', this.data.page.path)
formData.append('remove_banner', this.data.page.remove_banner ? true : false)
formData.append('remove_media', this.data.page.remove_media ? true : false)
2019-09-13 13:33:10 +00:00
this.loading = true
2022-07-27 08:41:18 +00:00
this.requestPage(
this.editor.save()
.then(body => {
formData.append('content', JSON.stringify(body))
2019-09-13 13:33:10 +00:00
return api.sendRequest({
2022-07-27 08:41:18 +00:00
method: 'PUT',
url: '/api/auth/pages/' + (this.lastid === 'add' ? '0' : this.lastid),
body: formData,
})
2019-09-13 13:33:10 +00:00
})
2022-07-27 08:41:18 +00:00
.then(data => {
if (!data.page.id) {
throw new Error('Something went wrong with saving, try again later')
} else if (this.lastid === 'add') {
this.lastid = data.page.id.toString()
m.route.set('/admin/pages/' + data.page.id)
}
return PageTree.refreshTree().then(() => {
2022-07-27 08:41:18 +00:00
this.pages = [{id: null, name: 'Frontpage'}]
this.pages = this.pages.concat(PageTree.getFlatTree())
2019-09-13 13:33:10 +00:00
2022-07-27 08:41:18 +00:00
return data
})
})
)
2019-09-13 13:33:10 +00:00
},
view: function(vnode) {
2022-07-27 08:41:18 +00:00
const bannerImage = this.data.page && this.data.page.banner_prefix
? this.data.page.banner_prefix + '_large.avif'
: null
const mediaImage = this.data.page && this.data.page.media_prefix
? this.data.page.media_prefix + '_large.avif'
: null
return [
this.loading && !this.data.page
? m('div.admin-spinner.loading-spinner')
: null,
this.data.page
? m('div.admin-wrapper', [
this.loading
? m('div.loading-spinner')
: null,
m('div.admin-actions', this.data.page.id
2019-09-13 13:33:10 +00:00
? [
m('span', 'Actions:'),
2022-07-27 08:41:18 +00:00
m(m.route.Link, { href: '/page/' + this.data.page.path }, 'View page'),
2019-09-13 13:33:10 +00:00
]
: null),
2022-07-27 08:41:18 +00:00
m('article.editarticle', [
m('header', m('h1',
(this.data.page.id ? 'Edit ' : 'Create Page ') + (this.data.page.name || '(untitled)')
)
),
2019-09-13 13:33:10 +00:00
m('div.error', {
hidden: !this.error,
2022-07-27 08:41:18 +00:00
onclick: () => { vnode.state.error = '' },
2019-09-13 13:33:10 +00:00
}, this.error),
m(FileUpload, {
2022-07-27 08:41:18 +00:00
height: 300,
onfile: this.mediaUploaded.bind(this, 'banner'),
ondelete: this.mediaRemoved.bind(this, 'banner'),
media: bannerImage,
2019-09-13 13:33:10 +00:00
}),
m(FileUpload, {
class: 'cover',
useimg: true,
2022-07-27 08:41:18 +00:00
onfile: this.mediaUploaded.bind(this, 'media'),
ondelete: this.mediaRemoved.bind(this, 'media'),
media: mediaImage,
2019-09-13 13:33:10 +00:00
}),
2022-07-27 08:41:18 +00:00
m('form.editarticle.content', {
2019-09-13 13:33:10 +00:00
onsubmit: this.save.bind(this, vnode),
}, [
m('label', 'Parent'),
m('select', {
onchange: this.updateParent.bind(this),
2022-07-27 08:41:18 +00:00
}, this.pages.filter(item => !this.data.page || item.id !== this.data.page.id).map((item) => {
return m('option', {
value: item.id || 0,
selected: item.id === this.data.page.parent_id
}, item.name)
})),
m('div.input-row', [
m('div.input-group', [
m('label', 'Name'),
m('input', {
type: 'text',
value: this.data.page.name,
oninput: this.updateValue.bind(this, 'name'),
}),
]),
m('div.input-group', [
m('label', 'Path'),
m('input', {
type: 'text',
value: this.data.page.path,
oninput: this.updateValue.bind(this, 'path'),
}),
]),
]),
2019-09-13 13:33:10 +00:00
m('label', 'Description'),
2022-07-27 08:41:18 +00:00
m(Editor, {
oncreate: (subnode) => {
this.editor = subnode.state.editor
},
contentdata: this.data.page.content,
2019-09-13 13:33:10 +00:00
}),
2022-07-27 08:41:18 +00:00
m('div', {
hidden: !this.data.page.name || !this.data.page.path
}, [
m('input', {
type: 'submit',
value: 'Save',
}),
]),
2019-09-13 13:33:10 +00:00
]),
]),
])
2022-07-27 08:41:18 +00:00
: m('div.error', {
hidden: !this.error,
onclick: () => { this.fetchPage(vnode) },
}, this.error),,
]
2019-09-13 13:33:10 +00:00
},
}
module.exports = EditPage