filebrowser/plugins/hugo.js.go

228 lines
6.7 KiB
Go
Raw Normal View History

2017-07-29 18:11:20 +00:00
package plugins
const hugoJavaScript = `'use strict';
2017-07-01 07:36:28 +00:00
(function () {
if (window.plugins === undefined || window.plugins === null) {
window.plugins = []
}
2017-07-01 07:36:28 +00:00
let regenerate = function (data, url) {
url = data.api.removePrefix(url)
return new Promise((resolve, reject) => {
let request = new window.XMLHttpRequest()
2017-07-29 18:11:20 +00:00
request.open('POST', data.store.state.baseURL + "/api/hugo" + url, true)
request.setRequestHeader('Authorization', "Bearer " + data.store.state.jwt)
request.setRequestHeader('Regenerate', 'true')
request.onload = () => {
if (request.status === 200) {
resolve()
} else {
reject(request.responseText)
}
}
request.onerror = (error) => reject(error)
request.send()
})
}
2017-07-12 14:28:35 +00:00
let newArchetype = function (data, url, type) {
url = data.api.removePrefix(url)
return new Promise((resolve, reject) => {
let request = new window.XMLHttpRequest()
2017-07-29 18:11:20 +00:00
request.open('POST', data.store.state.baseURL + "/api/hugo" + url, true)
request.setRequestHeader('Authorization',"Bearer " + data.store.state.jwt)
2017-07-12 14:28:35 +00:00
request.setRequestHeader('Archetype', encodeURIComponent(type))
request.onload = () => {
if (request.status === 200) {
resolve(request.getResponseHeader('Location'))
} else {
reject(request.responseText)
}
}
request.onerror = (error) => reject(error)
request.send()
})
}
let schedule = function (data, file, date) {
file = data.api.removePrefix(file)
return new Promise((resolve, reject) => {
let request = new window.XMLHttpRequest()
2017-07-29 18:11:20 +00:00
request.open('POST', data.store.state.baseURL + "/api/hugo" + file, true)
request.setRequestHeader('Authorization', "Bearer " + data.store.state.jwt)
2017-07-12 14:28:35 +00:00
request.setRequestHeader('Schedule', date)
request.onload = () => {
if (request.status === 200) {
resolve(request.getResponseHeader('Location'))
} else {
reject(request.responseText)
}
}
request.onerror = (error) => reject(error)
request.send()
})
}
window.plugins.push({
name: 'hugo',
credits: 'With a flavour of <a rel="noopener noreferrer" href="https://github.com/hacdias/caddy-hugo">Hugo</a>.',
header: {
visible: [
{
if: function (data, route) {
return (data.store.state.req.kind === 'editor' &&
!data.store.state.loading &&
2017-07-14 07:30:56 +00:00
data.store.state.user.allowEdit &
data.store.state.user.permissions.allowPublish)
},
click: function (event, data, route) {
event.preventDefault()
document.getElementById('save-button').click()
// TODO: wait for save to finish?
data.buttons.loading('publish')
regenerate(data, route.path)
.then(() => {
data.buttons.done('publish')
2017-07-12 15:18:13 +00:00
data.store.commit('showSuccess', 'Post published!')
data.store.commit('setReload', true)
})
.catch((error) => {
data.buttons.done('publish')
data.store.commit('showError', error)
})
},
id: 'publish-button',
icon: 'send',
name: 'Publish'
}
],
hidden: [
{
if: function (data, route) {
return (data.store.state.req.kind === 'editor' &&
!data.store.state.loading &&
data.store.state.req.metadata !== undefined &&
2017-07-14 07:30:56 +00:00
data.store.state.req.metadata !== null &&
data.store.state.user.permissions.allowPublish)
},
click: function (event, data, route) {
2017-07-12 14:28:35 +00:00
document.getElementById('save-button').click()
data.store.commit('showHover', 'schedule')
},
id: 'schedule-button',
icon: 'alarm',
name: 'Schedule'
}
]
2017-07-01 07:36:28 +00:00
},
sidebar: [
{
click: function (event, data, route) {
data.router.push({ path: '/files/settings' })
},
2017-07-18 08:10:00 +00:00
icon: 'settings',
2017-07-18 13:43:16 +00:00
name: 'Hugo Settings'
2017-07-01 07:36:28 +00:00
},
{
click: function (event, data, route) {
data.store.commit('showHover', 'new-archetype')
},
2017-07-18 07:11:42 +00:00
if: function (data, route) {
return data.store.state.user.allowNew
},
icon: 'merge_type',
2017-07-18 13:43:16 +00:00
name: 'Hugo New'
},
{
click: function (event, data, route) {
window.open(data.store.state.baseURL + '/preview/')
},
icon: 'remove_red_eye',
name: 'Preview'
}
],
prompts: [
{
name: 'new-archetype',
title: 'New file',
description: 'Create a new post based on an archetype. Your file will be created on content folder.',
inputs: [
{
type: 'text',
name: 'file',
placeholder: 'File name'
},
{
type: 'text',
name: 'archetype',
placeholder: 'Archetype'
}
],
ok: 'Create',
submit: function (event, data, route) {
event.preventDefault()
let file = event.currentTarget.querySelector('[name="file"]').value
let type = event.currentTarget.querySelector('[name="archetype"]').value
if (type === '') type = 'default'
data.store.commit('closeHovers')
newArchetype(data, '/' + file, type)
.then((url) => {
data.router.push({ path: url })
})
.catch(error => {
data.store.commit('showError', error)
})
}
2017-07-12 14:28:35 +00:00
},
{
name: 'schedule',
title: 'Schedule',
description: 'Pick a date and time to schedule the publication of this post.',
inputs: [
{
type: 'datetime-local',
name: 'date',
placeholder: 'Date'
}
],
ok: 'Schedule',
submit: function (event, data, route) {
event.preventDefault()
data.buttons.loading('schedule')
let date = event.currentTarget.querySelector('[name="date"]').value
if (date === '') {
data.buttons.done('schedule')
data.store.commit('showError', 'The date must not be empty.')
return
}
schedule(data, route.path, date)
.then(() => {
data.buttons.done('schedule')
2017-07-12 15:18:13 +00:00
data.store.commit('showSuccess', 'Post scheduled!')
2017-07-12 14:28:35 +00:00
})
.catch((error) => {
data.buttons.done('schedule')
data.store.commit('showError', error)
})
}
}
]
})
2017-07-29 18:11:20 +00:00
})()`