nfp_sites/app/admin/editarticle.js

363 lines
11 KiB
JavaScript
Raw Normal View History

2019-09-13 13:33:10 +00:00
const Authentication = require('../authentication')
const FileUpload = require('../widgets/fileupload')
2019-10-02 18:47:20 +00:00
const Staff = require('../api/staff')
2019-09-13 13:33:10 +00:00
const Froala = require('./froala')
2019-10-01 11:35:00 +00:00
const Page = require('../api/page')
const File = require('../api/file')
2019-09-14 19:03:38 +00:00
const Fileinfo = require('../widgets/fileinfo')
2019-10-01 11:35:00 +00:00
const Article = require('../api/article')
2022-07-20 00:33:06 +00:00
const common = require('../api/common')
2019-09-13 13:33:10 +00:00
const EditArticle = {
oninit: function(vnode) {
2022-07-20 00:33:06 +00:00
this.editor = null
this.loading = false
this.showLoading = null
this.data = {
article: null,
files: [],
staff: [],
2019-09-14 19:03:38 +00:00
}
2022-07-20 00:33:06 +00:00
this.pages = [{id: null, name: 'Frontpage'}]
this.addPageTree('', Page.Tree)
this.newBanner = null
this.newMedia = null
2019-09-14 19:03:38 +00:00
this.fetchArticle(vnode)
},
2022-07-20 00:33:06 +00:00
addPageTree: function(prefix, branches) {
branches.forEach((page) => {
this.pages.push({ id: page.id, name: prefix + page.name })
if (page.children && page.children.length) {
this.addPageTree(page.name + ' -> ', page.children)
}
})
},
onbeforeupdate: function(vnode) {
2019-09-14 19:03:38 +00:00
if (this.lastid !== m.route.param('id')) {
this.fetchArticle(vnode)
}
},
fetchArticle: function(vnode) {
this.lastid = m.route.param('id')
2019-09-13 13:33:10 +00:00
this.error = ''
2022-07-20 00:33:06 +00:00
if (this.showLoading) {
clearTimeout(this.showLoading)
2019-09-13 13:33:10 +00:00
}
2022-07-20 00:33:06 +00:00
if (this.data.article) {
this.showLoading = setTimeout(() => {
this.showLoading = null
this.loading = true
2019-09-13 13:33:10 +00:00
m.redraw()
2022-07-20 00:33:06 +00:00
}, 150)
} else {
2022-07-20 00:33:06 +00:00
this.loading = true
2019-09-13 13:33:10 +00:00
}
2022-07-20 00:33:06 +00:00
return common.sendRequest({
method: 'GET',
url: '/api/auth/articles/' + this.lastid,
})
.then((result) => {
console.log('result', result)
this.data = result
if (this.data.article) {
this.data.article.publish_at = new Date(this.data.article.publish_at)
document.title = 'Editing: ' + this.data.article.name + ' - Admin NFP Moe'
} else {
document.title = 'Create Article - Admin NFP Moe'
}
}, (err) => {
this.error = err.message
})
.then(() => {
clearTimeout(this.showLoading)
this.showLoading = null
this.loading = false
m.redraw()
})
2019-10-02 00:16:11 +00:00
},
2019-09-13 13:33:10 +00:00
updateValue: function(name, e) {
2019-10-02 00:16:11 +00:00
if (name === 'is_featured') {
2022-07-20 00:33:06 +00:00
this.data.article[name] = e.currentTarget.checked
2019-10-02 00:16:11 +00:00
} else {
2022-07-20 00:33:06 +00:00
this.data.article[name] = e.currentTarget.value
2019-10-02 00:16:11 +00:00
}
2019-09-13 13:33:10 +00:00
if (name === 'path') {
this.editedPath = true
} else if (name === 'name' && !this.editedPath) {
2022-07-20 00:33:06 +00:00
this.data.article.path = this.data.article.name.toLowerCase().replace(/ /g, '-')
2019-09-13 13:33:10 +00:00
}
},
updateParent: function(e) {
2022-07-20 00:33:06 +00:00
this.data.article.page_id = Number(e.currentTarget.value) || null
2019-09-13 13:33:10 +00:00
},
2019-10-02 18:47:20 +00:00
updateStaffer: function(e) {
2022-07-20 00:33:06 +00:00
this.data.article.admin_id = Number(e.currentTarget.value)
2019-10-02 18:47:20 +00:00
},
2022-07-20 00:33:06 +00:00
mediaUploaded: function(type, file) {
if (type === 'banner') {
this.newBanner = file
} else {
this.newMedia = file
}
2019-09-13 13:33:10 +00:00
},
2019-09-14 19:03:38 +00:00
mediaRemoved: function(type) {
2022-07-20 00:33:06 +00:00
this.data.article[type] = 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-20 00:33:06 +00:00
console.log(this.data)
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.article.id) {
formData.append('id', this.data.article.id)
}
formData.append('admin_id', this.data.article.admin_id)
formData.append('name', this.data.article.name)
formData.append('content', this.data.article.content)
formData.append('is_featured', this.data.article.is_featured)
formData.append('path', this.data.article.path)
formData.append('page_id', this.data.article.page_id)
formData.append('publish_at', this.data.article.publish_at)
this.loading = true
common.sendRequest({
method: 'PUT',
url: '/api/auth/articles/' + this.lastid,
body: formData,
})
.then((result) => {
console.log('result', result)
}, (err) => {
this.error = err.message
})
.then(() => {
this.loading = false
m.redraw()
})
/*e.preventDefault()
if (!this.data.article.name) {
2019-09-13 13:33:10 +00:00
this.error = 'Name is missing'
2022-07-20 00:33:06 +00:00
} else if (!this.data.article.path) {
2019-09-13 13:33:10 +00:00
this.error = 'Path is missing'
2019-09-14 19:03:38 +00:00
} else {
this.error = ''
2019-09-13 13:33:10 +00:00
}
if (this.error) return
2022-07-20 00:33:06 +00:00
this.data.article.description = vnode.state.froala && vnode.state.froala.html.get() || this.data.article.description
if (this.data.article.description) {
this.data.article.description = this.data.article.description.replace(/<p[^>]+data-f-id="pbf"[^>]+>[^>]+>[^>]+>[^>]+>/, '')
2019-10-01 03:45:44 +00:00
}
2019-09-13 13:33:10 +00:00
this.loading = true
let promise
2022-07-20 00:33:06 +00:00
if (this.data.article.id) {
promise = Article.updateArticle(this.data.article.id, {
name: this.data.article.name,
path: this.data.article.path,
page_id: this.data.article.page_id,
description: this.data.article.description,
banner_id: this.data.article.banner && this.data.article.banner.id,
media_id: this.data.article.media && this.data.article.media.id,
publish_at: new Date(this.data.article.publish_at),
is_featured: this.data.article.is_featured,
staff_id: this.data.article.staff_id,
2019-09-13 13:33:10 +00:00
})
} else {
2019-10-01 11:35:00 +00:00
promise = Article.createArticle({
2022-07-20 00:33:06 +00:00
name: this.data.article.name,
path: this.data.article.path,
page_id: this.data.article.page_id,
description: this.data.article.description,
banner_id: this.data.article.banner && this.data.article.banner.id,
media_id: this.data.article.media && this.data.article.media.id,
publish_at: new Date(this.data.article.publish_at),
is_featured: this.data.article.is_featured,
staff_id: this.data.article.staff_id,
2019-09-13 13:33:10 +00:00
})
}
promise.then(function(res) {
if (vnode.state.article.id) {
res.media = vnode.state.article.media
res.banner = vnode.state.article.banner
2019-09-14 19:03:38 +00:00
res.files = vnode.state.article.files
2019-09-13 13:33:10 +00:00
vnode.state.article = res
2019-10-02 00:16:11 +00:00
EditArticle.parsePublishedAt(vnode, null)
2019-09-13 13:33:10 +00:00
} else {
m.route.set('/admin/articles/' + res.id)
}
})
.catch(function(err) {
vnode.state.error = err.message
})
.then(function() {
vnode.state.loading = false
m.redraw()
2022-07-20 00:33:06 +00:00
})*/
2019-09-13 13:33:10 +00:00
},
2022-07-20 00:33:06 +00:00
uploadFile: function(vnode, e) {
2019-09-14 19:03:38 +00:00
2019-10-02 18:47:20 +00:00
},
2019-09-13 13:33:10 +00:00
view: function(vnode) {
2022-07-20 00:33:06 +00:00
const showPublish = this.data.article
? this.data.article.publish_at > new Date()
: false
return [
2019-09-13 13:33:10 +00:00
this.loading ?
m('div.loading-spinner')
2022-07-20 00:33:06 +00:00
: null,
this.data.article
? m('div.admin-wrapper', [
m('div.admin-actions', this.data.article.id
2019-09-13 13:33:10 +00:00
? [
m('span', 'Actions:'),
2022-07-20 00:33:06 +00:00
m(m.route.Link, { href: '/article/' + this.data.article.path }, 'View article'),
2019-09-13 13:33:10 +00:00
]
: null),
m('article.editarticle', [
2022-07-20 00:33:06 +00:00
m('header', m('h1', this.creating ? 'Create Article' : 'Edit ' + (this.data.article.name || '(untitled)'))),
2019-09-13 13:33:10 +00:00
m('div.error', {
hidden: !this.error,
2022-07-20 00:33:06 +00:00
onclick: () => { vnode.state.error = '' },
2019-09-13 13:33:10 +00:00
}, this.error),
m(FileUpload, {
height: 300,
2022-07-20 00:33:06 +00:00
onfile: this.mediaUploaded.bind(this, 'banner'),
2019-09-14 19:03:38 +00:00
ondelete: this.mediaRemoved.bind(this, 'banner'),
2022-07-20 00:33:06 +00:00
media: this.data.article && this.data.article.banner,
2019-09-13 13:33:10 +00:00
}),
m(FileUpload, {
class: 'cover',
useimg: true,
2022-07-20 00:33:06 +00:00
onfile: this.mediaUploaded.bind(this, 'media'),
2019-09-14 19:03:38 +00:00
ondelete: this.mediaRemoved.bind(this, 'media'),
2022-07-20 00:33:06 +00:00
media: this.data.article && this.data.article.media,
2019-09-13 13:33:10 +00:00
}),
m('form.editarticle.content', {
onsubmit: this.save.bind(this, vnode),
}, [
m('label', 'Parent'),
m('select', {
onchange: this.updateParent.bind(this),
2022-07-20 00:33:06 +00:00
}, this.pages.map((item) => {
return m('option', {
value: item.id || 0,
selected: item.id === this.data.article.page_id
}, item.name)
})),
2019-09-13 13:33:10 +00:00
m('label', 'Name'),
m('input', {
type: 'text',
2022-07-20 00:33:06 +00:00
value: this.data.article.name,
2019-09-13 13:33:10 +00:00
oninput: this.updateValue.bind(this, 'name'),
}),
2019-10-02 18:47:20 +00:00
m('label.slim', 'Path'),
m('input.slim', {
type: 'text',
2022-07-20 00:33:06 +00:00
value: this.data.article.path,
2019-10-02 18:47:20 +00:00
oninput: this.updateValue.bind(this, 'path'),
}),
2019-09-13 13:33:10 +00:00
m('label', 'Description'),
(
this.loadedFroala ?
m('div', {
2022-07-20 00:33:06 +00:00
oncreate: (div) => {
console.log(div.dom)
2019-09-13 13:33:10 +00:00
},
})
: null
),
2019-10-02 18:47:20 +00:00
m('label', 'Published at'),
2019-09-13 13:33:10 +00:00
m('input', {
2019-10-02 00:16:11 +00:00
type: 'datetime-local',
2022-07-20 00:33:06 +00:00
value: this.data.article.publish_at,
oninput: this.updateValue.bind(this, 'publish_at'),
2019-10-02 00:16:11 +00:00
}),
2019-10-02 18:47:20 +00:00
m('label', 'Published by'),
m('select', {
2022-07-20 00:33:06 +00:00
onchange: this.updateStaffer.bind(this),
},
this.data.staff.map((item) => {
return m('option', {
value: item.id,
selected: item.id === this.data.article.staff_id
}, item.name)
})
),
2019-10-02 00:16:11 +00:00
m('label', 'Make featured'),
m('input', {
type: 'checkbox',
2022-07-20 00:33:06 +00:00
checked: this.data.article.is_featured,
2019-10-02 00:16:11 +00:00
oninput: this.updateValue.bind(this, 'is_featured'),
2019-09-13 13:33:10 +00:00
}),
m('div', [
m('input', {
type: 'submit',
value: 'Save',
}),
showPublish
2022-07-20 00:33:06 +00:00
? m('button.submit', {
onclick: () => {
this.data.article.publish_at = new Date().toISOString()
}
}, 'Publish')
: null,
]),
2019-09-13 13:33:10 +00:00
]),
2022-07-20 00:33:06 +00:00
this.data.files.length
2019-09-14 19:03:38 +00:00
? m('files', [
m('h4', 'Files'),
2022-07-20 00:33:06 +00:00
this.data.files.map((file) => {
return m(Fileinfo, { file: file })
}),
2019-09-14 19:03:38 +00:00
])
: null,
2022-07-20 00:33:06 +00:00
this.data.article.id
2019-09-14 19:03:38 +00:00
? m('div.fileupload', [
'Add file',
m('input', {
accept: '*',
type: 'file',
onchange: this.uploadFile.bind(this, vnode),
}),
(vnode.state.loadingFile ? m('div.loading-spinner') : null),
])
: null,
2019-09-13 13:33:10 +00:00
]),
])
2022-07-20 00:33:06 +00:00
: null,
]
2019-09-13 13:33:10 +00:00
},
}
module.exports = EditArticle