Initial Commit
[ancientarts.git] / hugo / public / js / blog.js
1 /**
2 * @file blog.js
3 * @author Ken Grimes
4 * @license AGPL-3.0
5 * @copyright 2018 - Ken Grimes
6 * @summmary live edit front-end for fediblog
7 */
8 'use strict'
9
10 const apiURL = document.location.origin + '/aa/api/'
11 const baseURL = document.location.origin + '/aa/'
12
13 const logout = () => {
14 document.cookie = 'live-edit=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
15 window.location.reload(true)
16 }
17
18 const doDelete = (btn) => {
19 const element = btn.parentElement
20 const fileName = 'content/' + element.id
21 if (window.confirm(`Do you really want to delete ${element.id}?`))
22 fetch(apiURL + fileName, {credentials: 'same-origin', method: 'DELETE'}).then((resp) => {
23 if (resp.ok)
24 window.location.href = window.location.href + ".."
25 else if (resp.status === 401)
26 logout()
27 else
28 console.log(resp)
29 })
30 }
31
32 const doCancel = (btn) => {
33 const element = btn.parentElement
34 element.innerHTML = element.oldHTML
35 }
36
37 const doCommit = (btn) => {
38 const element = btn.parentElement
39 const fileName = 'content/' + element.id
40 const textArea = element.getElementsByTagName('textarea').item(0)
41 fetch(apiURL + fileName, {credentials: 'same-origin', method: 'POST', body: textArea.value}).then((resp) => {
42 if (resp.ok)
43 window.location.reload(true)
44 else if (resp.status === 401)
45 logout()
46 else
47 console.log(resp)
48 })
49 }
50
51 const doCommitPost = (btn) => {
52 const element = btn.parentElement
53 const nameBox = element.getElementsByTagName('input').item(0)
54 if (nameBox.value === '') {
55 window.alert('Filename is required')
56 return
57 }
58 const section = window.location.href.match(new RegExp(baseURL + '(.*/)'))[1]
59 const fileName = 'content/' + section + nameBox.value + '.md'
60 const textArea = element.getElementsByTagName('textarea').item(0)
61 fetch(apiURL + fileName, {credentials: 'same-origin', method: 'POST', body: textArea.value}).then((resp) => {
62 if (resp.ok)
63 window.location.reload(true)
64 else if (resp.status === 401)
65 logout()
66 else
67 console.log(resp)
68 })
69 }
70
71 const doPost = (btn) => {
72 const element = btn.parentElement
73 element.oldHTML = element.innerHTML
74 element.innerHTML =
75 '<button onclick=doCommitPost(this)>commit</button>' +
76 '<button onclick=doCancel(this)>cancel</button><br>' +
77 'Filename: <input type="text" />' +
78 '<textarea cols=80 rows=60></textarea>'
79 }
80
81 const doEdit = (btn) => {
82 const element = btn.parentElement
83 const fileName = 'content/' + element.id
84 fetch(apiURL + fileName, {credentials: 'same-origin'}).then((resp) => {
85 if (resp.status === 401)
86 logout()
87 else if (resp.ok)
88 resp.text().then((txt) => {
89 element.oldHTML = element.innerHTML
90 element.innerHTML =
91 '<button onclick=doCommit(this)>commit</button>' +
92 '<button onclick=doDelete(this)>delete</button>' +
93 '<button onclick=doCancel(this)>cancel</button><br>' +
94 '<textarea cols=80 rows=60>' +
95 txt +
96 '</textarea>'
97 })
98 else
99 console.log(resp)
100 })
101 }
102
103 const editTools = () => {
104 const article = document.body.getElementsByTagName('article').item(0)
105 article.innerHTML =
106 '<button onclick="doEdit(this)">edit</button>' +
107 '<button onclick="doPost(this)">new post</button><br>' +
108 article.innerHTML
109 }
110
111 window.onload = () => {
112 if (document.cookie.match('live-edit=true'))
113 editTools()
114 }