moar syntactic sugar

This commit is contained in:
posativ 2012-12-16 13:38:24 +01:00
parent 8e2e90ed4e
commit eaaf1f4653
3 changed files with 79 additions and 58 deletions

View File

@ -1,7 +1,7 @@
all: all:
init: init:
(cd isso/js && ender build jeesh reqwest) (cd isso/js && ender build jeesh)
js: js:
cat isso/js/ender.js isso/js/isso.js > _.js cat isso/js/ender.js isso/js/isso.js > _.js

View File

@ -26,10 +26,10 @@ import sys; reload(sys)
sys.setdefaultencoding('utf-8') # we only support UTF-8 and python 2.X :-) sys.setdefaultencoding('utf-8') # we only support UTF-8 and python 2.X :-)
import io import io
import os
import json import json
import traceback import traceback
from os.path import dirname
from optparse import OptionParser, make_option, SUPPRESS_HELP from optparse import OptionParser, make_option, SUPPRESS_HELP
from itsdangerous import URLSafeTimedSerializer from itsdangerous import URLSafeTimedSerializer
@ -117,9 +117,13 @@ class Isso(object):
if code == 404: if code == 404:
try: try:
code, body, headers = wsgi.sendfile(environ['PATH_INFO'], dirname(__file__)) code, body, headers = wsgi.sendfile(environ['PATH_INFO'], os.getcwd())
except (IOError, OSError): except (IOError, OSError):
pass try:
path = environ['PATH_INFO'].rstrip('/') + '/index.html'
code, body, headers = wsgi.sendfile(path, os.getcwd())
except (IOError, OSError):
pass
if request == 'HEAD': if request == 'HEAD':
body = '' body = ''

View File

@ -12,20 +12,21 @@
/* utility functions -- JS Y U SO STUPID? /* utility functions -- JS Y U SO STUPID?
* *
* read(cookie): return `cookie` string if set * read(cookie): return `cookie` string if set
* zfill(argument, i): zero fill `argument` with `i` zeros * format(date): human-readable date formatting
* brew(array): similar to DOMinate essentials
*/ */
// var prefix = "/comments"; // var prefix = "/comments";
var prefix = ""; var prefix = "";
function read(cookie){ function read(cookie) {
return(document.cookie.match('(^|; )' + cookie + '=([^;]*)') || 0)[2] return (document.cookie.match('(^|; )' + cookie + '=([^;]*)') || 0)[2]
}; };
function format(date){ function format(date) {
/*! /*!
* JavaScript Pretty Date * JavaScript Pretty Date
* Copyright (c) 2011 John Resig (ejohn.org) * Copyright (c) 2011 John Resig (ejohn.org)
@ -34,20 +35,49 @@ function format(date){
var diff = (((new Date()).getTime() - date.getTime()) / 1000), var diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400); day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ) if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31)
return; return;
return day_diff == 0 && ( return day_diff == 0 && (
diff < 60 && "just now" || diff < 60 && "just now" ||
diff < 120 && "1 minute ago" || diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" || diff < 3600 && Math.floor(diff / 60) + " minutes ago" ||
diff < 7200 && "1 hour ago" || diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") || diff < 86400 && Math.floor(diff / 3600) + " hours ago") ||
day_diff == 1 && "Yesterday" || day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" || day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"; day_diff < 31 && Math.ceil(day_diff / 7) + " weeks ago";
} }
function brew(arr) {
/*
* Element creation utility. Similar to DOMinate, but with a slightly different syntax:
* brew([TAG, {any: attribute, ...}, 'Hello World', ' Foo Bar', ['TAG', 'Hello World'], ...])
* --> <TAG any="attribute">Hello World Foo Bar<TAG>Hello World</TAG></TAG>
*/
var rv = document.createElement(arr[0]);
for (var i = 1; i < arr.length; i++) {
if (arr[i] instanceof Array) {
rv.appendChild(brew(arr[i]));
} else if (typeof(arr[i]) == "string") {
rv.appendChild(document.createTextNode(arr[i]));
} else {
attrs = arr[i] || {};
for (var k in attrs) {
if (!attrs.hasOwnProperty(k)) continue;
rv.setAttribute(k, attrs[k]);
}
}
};
return rv;
}
/* /*
* isso specific helpers to create, modify, delete and receive comments * isso specific helpers to create, modify, delete and receive comments
*/ */
@ -91,24 +121,21 @@ function form(id, appendfunc, eventfunc) {
:param eventfunc: function, when the user submits the form :param eventfunc: function, when the user submits the form
*/ */
var formid = 'issoform' + (id ? ('_' + id) : ''), form = var formid = 'issoform' + (id ? ('_' + id) : ''), form = brew([
'<div class="issoform" id="' + formid + '">' + 'div', {'class': 'issoform', 'id': formid},
'<div>' + ['div',
' <input type="text" name="author" id="author" value="" placeholder="Name">' + ['input', {'type': 'text', 'name': 'author', 'id': 'author', 'value': '', 'placeholder': "Name"}]],
'</div>' + ['div',
'<div>' + ['input', {'type': 'email', 'name': 'email', 'id': 'email', 'value': '', 'placeholder': "Email"}]],
' <input type="email" name="email" id="email" value="" placeholder="Email">' + ['div',
'</div>' + ['input', {'type': 'email', 'name': 'email', 'id': 'email', 'value': '', 'placeholder': "Email"}]],
'<div>' + ['div',
'<input type="url" name="website" id="website" value="" placeholder="Website URL">' + ['input', {'type': 'url', 'name': 'website', 'id': 'website', 'value': '', 'placeholder': "website URL"}]],
'</div>' + ['div',
'<div>' + ['textarea', {'rows': '10', 'name': 'text', 'id': 'comment', 'placeholder': "Comment"}]],
' <textarea rows="10" name="text" id="comment" placeholder="Comment"></textarea>' + ['div',
'</div>' + ['input', {'type': 'submit', 'value': 'Add Comment'}]],
'<div>' + ]);
'<input type="submit" name="submit" value="Add Comment">' +
'</div>' +
'</div>'
appendfunc(form); appendfunc(form);
$('#' + formid + ' ' + 'input[type="submit"]').on('click', eventfunc); $('#' + formid + ' ' + 'input[type="submit"]').on('click', eventfunc);
@ -130,19 +157,16 @@ function insert(post) {
$('#isso_' + post['parent']).append('<ul></ul>'); $('#isso_' + post['parent']).append('<ul></ul>');
$(post['parent'] ? '#isso_' + post['parent'] + ' > ul:last-child' : '#isso_thread > ul') $(post['parent'] ? '#isso_' + post['parent'] + ' > ul:last-child' : '#isso_thread > ul')
.append( .append(brew([
'<article class="isso" id="isso_' + post['id'] + '">' + 'article', {'class': 'isso', 'id': 'isso_' + post['id']},
' <header></header>' + ['header'], ['div'], ['footer']
' <div></div>' + ]));
' <footer></footer>' +
'</article>'
);
var node = $('#isso_' + post['id']), var node = $('#isso_' + post['id']),
author = post['author'] || 'Anonymous'; author = post['author'] || 'Anonymous';
if (post['website']) if (post['website'])
author = '<a href="' + post['website'] + '" rel="nofollow">' + author + '</a>'; author = brew(['a', {'href': post['website'], 'rel': 'nofollow'}]);
// deleted // deleted
if (post['mode'] == 4) { if (post['mode'] == 4) {
@ -154,28 +178,22 @@ function insert(post) {
$('header', node).append('<span class="author">' + author + '</span>'); $('header', node).append('<span class="author">' + author + '</span>');
if (post['mode'] == 2 ) if (post['mode'] == 2 )
$('helpers', node).append('<span class="note">Kommentar muss noch freigeschaltet werden</span>'); $('header', node).append(brew(['span', {'class': 'note'}, 'Kommentar muss noch freigeschaltet werden']));
$('div', node).html(post['text']); $('div', node).html(post['text']);
$('footer', node).append( $('footer', node).append(brew([
'<a href="#">Antworten</a>' + 'a', {'href': '#'}, 'Antworten',
'<a href="#isso_' + post['id'] + '">#' + post['id'] + '</a>' + ])).append(brew([
'<time datetime="' + date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + 'a', {'href': '#isso_' + post['id']}, '#' + post['id'],
date.getUTCDate() + '">' + format(date) + ])).append(brew([
'</time>' 'time', {'datetime': date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate()}, format(date)
) ]));
// pending notification
if (post['mode'] == 2 ) {
$('#isso_' + post['id'] + ' header')
.append('<span class="note">Kommentar muss noch freigeschaltet werden</span>');
}
if (read(path + '-' + post['id'])) { if (read(path + '-' + post['id'])) {
$('#isso_' + post['id'] + '> footer > a:first-child') $('#isso_' + post['id'] + '> footer > a:first-child')
.after('<a class="delete" href="#">Löschen</a>') .after(brew(['a', {'class': 'delete', 'href': '#'}, 'Löschen']))
.after('<a class="edit" href="#">Bearbeiten</a>'); .after(brew(['a', {'class': 'edit', 'href': '#'}, 'Bearbeiten']));
// DELETE // DELETE
$('#isso_' + post['id'] + ' > footer .delete').on('click', function(event) { $('#isso_' + post['id'] + ' > footer .delete').on('click', function(event) {
@ -289,7 +307,6 @@ function initialize(thread) {
function fetch(thread) { function fetch(thread) {
console.log(window.location.pathname);
$.ajax('GET', prefix + '/1.0/' + encodeURIComponent(window.location.pathname), $.ajax('GET', prefix + '/1.0/' + encodeURIComponent(window.location.pathname),
{}, {'Content-Type': 'application/json'}).then(function(status, rv) { {}, {'Content-Type': 'application/json'}).then(function(status, rv) {