2022-08-05 14:26:29 +00:00
|
|
|
const PageTree = require('../page_tree')
|
|
|
|
const Dialogue = require('./dialogue')
|
|
|
|
const api = require('../api')
|
2019-09-13 13:33:10 +00:00
|
|
|
|
|
|
|
const AdminPages = {
|
|
|
|
oninit: function(vnode) {
|
|
|
|
this.error = ''
|
|
|
|
this.pages = []
|
|
|
|
|
2019-10-01 17:18:20 +00:00
|
|
|
document.title = 'Pages - Admin NFP Moe'
|
2022-07-27 08:41:18 +00:00
|
|
|
this.fetchPages(vnode)
|
|
|
|
},
|
2019-10-01 17:18:20 +00:00
|
|
|
|
2022-07-27 08:41:18 +00:00
|
|
|
fetchPages: function(vnode) {
|
|
|
|
this.loading = true
|
|
|
|
this.error = ''
|
|
|
|
|
2022-08-05 14:26:29 +00:00
|
|
|
return api.sendRequest({
|
2022-07-27 08:41:18 +00:00
|
|
|
method: 'GET',
|
|
|
|
url: '/api/auth/pages',
|
2019-09-13 13:33:10 +00:00
|
|
|
})
|
2022-07-27 08:41:18 +00:00
|
|
|
.then((result) => {
|
|
|
|
this.pages = result.tree
|
|
|
|
}, (err) => {
|
|
|
|
this.error = err.message
|
2019-09-13 13:33:10 +00:00
|
|
|
})
|
2022-07-27 08:41:18 +00:00
|
|
|
.then(() => {
|
|
|
|
this.loading = false
|
2019-09-13 13:33:10 +00:00
|
|
|
m.redraw()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2022-08-08 16:58:20 +00:00
|
|
|
confirmRemovePage: function(vnode, page) {
|
2019-09-13 13:33:10 +00:00
|
|
|
this.loading = true
|
2022-07-27 08:41:18 +00:00
|
|
|
m.redraw()
|
|
|
|
|
2022-08-05 14:26:29 +00:00
|
|
|
return api.sendRequest({
|
2022-07-27 08:41:18 +00:00
|
|
|
method: 'DELETE',
|
2022-08-12 23:33:50 +00:00
|
|
|
url: '/api/auth/pages/' + page.id,
|
2022-07-27 08:41:18 +00:00
|
|
|
})
|
|
|
|
.then(
|
2022-08-12 23:33:50 +00:00
|
|
|
() => Promise.all([
|
|
|
|
PageTree.refreshTree(),
|
|
|
|
this.fetchPages(),
|
|
|
|
]),
|
|
|
|
(err) => { this.error = err.message }
|
2022-07-27 08:41:18 +00:00
|
|
|
)
|
2022-08-12 23:33:50 +00:00
|
|
|
.then(() => {
|
|
|
|
this.loading = false
|
|
|
|
m.redraw()
|
|
|
|
})
|
2019-09-13 13:33:10 +00:00
|
|
|
},
|
|
|
|
|
2022-08-08 16:58:20 +00:00
|
|
|
askConfirmRemovePage: function(vnode, page) {
|
|
|
|
Dialogue.showDialogue(
|
|
|
|
'Delete ' + page.name,
|
|
|
|
'Are you sure you want to remove "' + page.name + '" (' + page.path + ')',
|
|
|
|
'Remove',
|
|
|
|
'alert',
|
|
|
|
'Don\'t remove',
|
|
|
|
'',
|
|
|
|
page,
|
2022-08-12 23:33:50 +00:00
|
|
|
this.confirmRemovePage.bind(this, vnode))
|
2022-08-08 16:58:20 +00:00
|
|
|
},
|
|
|
|
|
2019-09-13 13:33:10 +00:00
|
|
|
drawPage: function(vnode, page) {
|
|
|
|
return [
|
|
|
|
m('tr', [
|
2022-08-18 09:59:48 +00:00
|
|
|
m('td.nopadding', page.banner_alt_prefix
|
|
|
|
? m('a', { href: page.banner_path, target: '_blank' }, m('img', { src: page.banner_alt_prefix + '_small.avif' }))
|
|
|
|
: m.trust(' ') ),
|
|
|
|
m('td.nopadding', page.media_alt_prefix
|
|
|
|
? m('a', { href: page.media_path, target: '_blank' }, m('img', { src: page.media_alt_prefix + '_small.avif' }))
|
|
|
|
: m.trust(' ') ),
|
2019-09-13 13:33:10 +00:00
|
|
|
m('td', [
|
2019-10-02 17:40:32 +00:00
|
|
|
page.parent_id ? m('span.subpage', ' - ') : null,
|
2019-09-13 13:33:10 +00:00
|
|
|
m(m.route.Link, { href: '/admin/pages/' + page.id }, page.name),
|
|
|
|
]),
|
|
|
|
m('td', m(m.route.Link, { href: '/page/' + page.path }, '/page/' + page.path)),
|
|
|
|
m('td.right', page.updated_at.replace('T', ' ').split('.')[0]),
|
2022-08-08 16:58:20 +00:00
|
|
|
m('td.right', m('button', { onclick: this.askConfirmRemovePage.bind(this, vnode, page) }, 'Remove')),
|
2019-09-14 19:03:38 +00:00
|
|
|
]),
|
2022-07-27 08:41:18 +00:00
|
|
|
].concat(page.children ? page.children.map(AdminPages.drawPage.bind(this, vnode)) : [])
|
2019-09-13 13:33:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
view: function(vnode) {
|
|
|
|
return [
|
2022-08-08 07:22:41 +00:00
|
|
|
m('div.admin', [
|
|
|
|
m('div.inside.vertical', [
|
2022-08-05 14:26:29 +00:00
|
|
|
m('div.actions', [
|
2022-08-12 23:33:50 +00:00
|
|
|
m('div.filler'),
|
2022-08-05 14:26:29 +00:00
|
|
|
m('span', 'Actions:'),
|
|
|
|
m(m.route.Link, { href: '/admin/pages/add' }, 'Create new page'),
|
2019-09-13 13:33:10 +00:00
|
|
|
]),
|
2022-08-12 23:33:50 +00:00
|
|
|
m('h2.title', 'All pages'),
|
|
|
|
m('div.container', [
|
|
|
|
m('div.error', {
|
|
|
|
hidden: !this.error,
|
|
|
|
onclick: function() { vnode.state.error = '' },
|
|
|
|
}, this.error),
|
|
|
|
this.loading
|
|
|
|
? m('div.loading-spinner')
|
|
|
|
: m('table', [
|
|
|
|
m('thead',
|
|
|
|
m('tr', [
|
2022-08-18 09:59:48 +00:00
|
|
|
m('th', 'Banner'),
|
|
|
|
m('th', 'Cover'),
|
2022-08-12 23:33:50 +00:00
|
|
|
m('th', 'Title'),
|
|
|
|
m('th', 'Path'),
|
|
|
|
m('th.right', 'Updated'),
|
|
|
|
m('th.right', 'Actions'),
|
|
|
|
])
|
|
|
|
),
|
|
|
|
m('tbody', this.pages.map(AdminPages.drawPage.bind(this, vnode))),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]),
|
2022-08-05 14:26:29 +00:00
|
|
|
]),
|
|
|
|
]),
|
2019-09-13 13:33:10 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AdminPages
|