Permalink
Browse files

v0.3.0: publish blog posts

The version includes:

* API path: POST /post/:postid/publish
* HTML partial template for a upload form
* server-side update handler to attach static HTML to blog post doc
  • Loading branch information...
llabball committed Jan 26, 2014
1 parent 6c0036d commit 3e55abdd15e747cf91faa0aa09c04171147ac470
Showing with 109 additions and 3 deletions.
  1. +1 −1 couchapp.json
  2. +6 −1 rewrites.json
  3. +3 −1 templates/post.html
  4. +57 −0 templates/uploadform.html
  5. +42 −0 updates/publish.js
@@ -1,5 +1,5 @@
{
"name": "StaticCouchBlog",
"description": "A web blog system to create, edit, preview and publish posts. The application is performance orienteted for the reads - it serves them finally as static files.",
"version": "0.2.0"
"version": "0.3.0"
}
@@ -8,5 +8,10 @@
"to": "_show/preview/:pid",
"from": "post/:pid/preview",
"method": "GET"
}
},
{
"to": "_update/publish/:pid",
"from": "post/:pid/publish",
"method": "POST"
}
]
@@ -11,6 +11,8 @@ <h1>{{title}}</h1>
<p><strong>{{teaser}}</strong></p>
{{{content}}}
{{>uploadform}}
</body>
</html>
@@ -0,0 +1,57 @@
<div id="uploadform">
<hr/>
<div id="note"></div>
<input type="submit" id="publish" value="publish" onclick="publishPost()">
<script type="text/javascript">
function upload (url, data, errorcallback, successcallback) {
var r = new XMLHttpRequest()
r.open('POST', url, true)
r.setRequestHeader('Content-Type', 'text/plain')
r.onreadystatechange = function () {
if (r.readyState === 4) {
if(r.status >= 400) {
if (errorcallback) errorcallback(r)
return
} else {
if (successcallback) successcallback(r)
}
}
}
r.send(data)
}
function setButtonValue (value, disabled, message) {
var button = document.querySelector('#publish')
, note = document.querySelector('#note')
button.value = value
note.innerHTML = message || ''
if (!disabled)
button.removeAttribute('disabled')
else
button.setAttribute('disabled', 'disabled')
}
function publishPost () {
var html = document.documentElement.cloneNode(true)
, form = html.querySelector('#uploadform')
, onerror, onsuccess
onerror = function (r) {
setButtonValue('fail. Try Again?', false, JSON.parse(r.response).reason)
}
onsuccess = function (r) {
window.location.href = './'
}
setButtonValue('uploading attachment...', true)
form.parentNode.removeChild(form)
upload('./publish?rev={{rev}}', btoa(html.outerHTML), onerror, onsuccess)
}
</script>
</div>
@@ -0,0 +1,42 @@
function (doc, req) {
var rev = req.query.rev
, html = req.body
, resp = {}
, error = false
, newdoc
if(!rev) {
error = true
resp.code = 412
resp.body = {error: 'no_revision', reason: 'missing rev query param'}
}
if(!doc) {
error = true
resp.code = 404
resp.body = {error: 'not_found', reason: 'missing'}
}
if(!html || html.length < 1) {
error = true
resp.code = 412
resp.body = {error: 'not_data', reason: 'missing HTML content'}
}
if(!error) {
newdoc = doc
newdoc.publish_at = new Date().toISOString()
newdoc._rev = rev
newdoc._attachments['static.html'] = {
'content_type':'text\/html',
'data': html
}
resp.code = 202
resp.body = {stat: 'ok', reason: 'Post successfully published. Visit /post/' + doc._id}
} else {
newdoc = null
}
resp.body = JSON.stringify(resp.body)
return [newdoc, resp]
}

0 comments on commit 3e55abd

Please sign in to comment.