From db32d1c7f0c408db6bd04226c6163a0ef3eb0135 Mon Sep 17 00:00:00 2001 From: posativ Date: Thu, 25 Oct 2012 13:24:44 +0200 Subject: [PATCH] refactor JS and add YUI compression. --- Makefile | 9 + isso/__init__.py | 3 +- isso/js/isso.js | 284 ++++ isso/static/ender.js | 3134 ----------------------------------------- isso/static/post.html | 229 +-- 5 files changed, 298 insertions(+), 3361 deletions(-) create mode 100644 Makefile create mode 100644 isso/js/isso.js delete mode 100644 isso/static/ender.js diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3c6fb5c --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: + +init: + (cd isso/js && ender build jeesh reqwest) + +js: + cat isso/js/ender.js isso/js/isso.js > _.js + yuicompressor --type js --charset utf-8 _.js -o isso/js/embed.js + rm _.js diff --git a/isso/__init__.py b/isso/__init__.py index 728a05e..23642fb 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -143,5 +143,6 @@ def main(): migrate.disqus(app.db, fp.read()) else: app = SharedDataMiddleware(app, { - '/static': join(dirname(__file__), 'static')}) + '/static': join(dirname(__file__), 'static'), + '/js': join(dirname(__file__), 'js')}) run_simple('127.0.0.1', 8000, app, use_reloader=True) diff --git a/isso/js/isso.js b/isso/js/isso.js new file mode 100644 index 0000000..082044d --- /dev/null +++ b/isso/js/isso.js @@ -0,0 +1,284 @@ +/* Isso – Ich schrei sonst! + * + * Copyright 2012, Martin Zimmermann . All rights reserved. + * License: BSD Style, 2 clauses. See isso/__init__.py. + * + * + * Code requires Bean, Bonzo, Qwery, domReady (all are part of jeesh) and + * reqwest. To ease integration with websites, all classes are prefixed + * with `isso`. + */ + + +/* utility functions -- JS Y U SO STUPID? + * + * read(cookie): return `cookie` string if set + * zfill(argument, i): zero fill `argument` with `i` zeros + */ + + +function read(cookie){ + return(document.cookie.match('(^|; )' + cookie + '=([^;]*)') || 0)[2] +}; + + +function zfill(arg, i) { + var res = String(arg); + if (res.length < i) { + for (var j = 0; j <= (i - res.length); j++) { + res = '0' + res; + }; + }; + + return res; +}; + + +/* + * isso specific helpers to create, modify, delete and receive comments + */ + +function isso(method, path, data, success, error) { + $.ajax({ + url: path, + method: method, + type: 'json', + headers: { + 'Content-Type': 'application/json' + }, + data: JSON.stringify(data), + error: function(rv) { + error(rv) || alert("Mööp."); + }, + success: function(rv) { + success(rv); + } + }); +}; + + +function verify(data) { + return data['text'] == null ? false : true +}; + + +function create(data, success) { + + if (!verify(data)) { + return; + } + + isso('POST', '/1.0/' + encodeURIComponent(window.location.pathname) + '/new', data, success); +}; + + +function modify(id, data, success) { + if (!verify(data)) { + return; + } + + isso('PUT', '/1.0/' + encodeURIComponent(window.location.pathname) + '/' + id, data, success); +}; + + +function form(id, appendfunc, eventfunc) { + /* + Returns HTML for form and registers submit call. + + Synopsis: `isso_N` is the comment with the id N. `issoform` is a new + form to write an answer to the article or answer to a comment using + `issoform_N` where N is the id to respond to. + + :param id: comment id + :param returnfunc: function, that takes one argument (the HTML to display the form) + :param eventfunc: function, when the user submits the form + */ + + var formid = 'issoform' + (id ? ('_' + id) : ''), form = + '
' + + '
' + + ' ' + + '
' + + '
' + + ' ' + + '
' + + '
' + + '' + + '
' + + '
' + + ' ' + + '
' + + '
' + + '' + + '
' + + '
' + + appendfunc(form); + $('#' + formid + ' ' + 'input[type="submit"]').on('click', eventfunc); +}; + + +function insert(thread, post) { + /* + Insert a comment into #isso_thread. + + :param thread: XXX remove this + :param post: JSON from API call + */ + + // pythonic strftime + var format = function(date, lang, fmt) { + + var months = {'de': [ + 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', + 'August', 'September', 'Oktober', 'November', 'Dezember'], + 'en': [ + 'January', 'February', 'March', 'April', 'May', 'June', 'July', + 'August', 'September', 'October', 'November', 'December'], + }; + + var conversions = [ + ['%Y', date.getFullYear()], ['%m', zfill(date.getMonth(), 2)], + ['%B', months[lang][date.getMonth() - 1]], + ['%d', zfill(date.getDate(), 2)], ['%H', zfill(date.getHours(), 2)], + ['%H', zfill(date.getHours(), 2)], ['%M', zfill(date.getMinutes(), 2)], + ]; + + conversions.map(function(item) { fmt = fmt.replace(item[0], item[1]) }); + return fmt; + }; + + var author = post['author'] || 'Anonymous'; + if (post['website']) { + author = '' + author + ''; + } + + var date = new Date(parseInt(post['created']) * 1000); + + // create