Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
with
109 additions
and 3 deletions.
- +1 −1 couchapp.json
- +6 −1 rewrites.json
- +3 −1 templates/post.html
- +57 −0 templates/uploadform.html
- +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" | ||
} |
@@ -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] | ||
} |