From 261f2fe18ee397ba9e92f8701e77a31e810ad118 Mon Sep 17 00:00:00 2001 From: posativ Date: Sun, 16 Dec 2012 23:29:07 +0100 Subject: [PATCH] introduce namespaces for JS and trying to escape the pyramid of doom --- Makefile | 2 +- isso/js/client.js | 216 ++++++++++++++++++++++++++++++++++++ isso/js/isso.js | 249 +++--------------------------------------- isso/static/post.html | 1 + 4 files changed, 236 insertions(+), 232 deletions(-) create mode 100644 isso/js/client.js diff --git a/Makefile b/Makefile index a14c98b..4372158 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,6 @@ init: rm -rf $(ISSO)/promise.js js: - cat $(ISSO)/ender.js $(ISSO)/isso.js $(ISSO)/utils.js > $(ISSO)/_.js + cat $(ISSO)/ender.js $(ISSO)/isso.js $(ISSO)/utils.js $(ISSO)/client.js > $(ISSO)/_.js yuicompressor --type js --charset utf-8 $(ISSO)/_.js -o $(ISSO)/embed.js rm $(ISSO)/_.js diff --git a/isso/js/client.js b/isso/js/client.js new file mode 100644 index 0000000..92dfc35 --- /dev/null +++ b/isso/js/client.js @@ -0,0 +1,216 @@ +/* Copyright 2012, Martin Zimmermann . All rights reserved. + * License: BSD Style, 2 clauses. See isso/__init__.py. + */ + + var isso = isso || {}; + + +function form(id, defaults, func) { + /* + 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 func: function, when the user submits the form + */ + + var rv = $(brew([ + 'div', {'class': 'issoform', 'id': 'issoform' + (id ? ('_' + id) : '')}, + ['div', + ['input', {'type': 'text', 'name': 'author', 'id': 'author', 'value': defaults.author || "", 'placeholder': "Name"}]], + ['div', + ['input', {'type': 'email', 'name': 'email', 'id': 'email', 'value': defaults.email || "", 'placeholder': "Email"}]], + ['div', + ['input', {'type': 'url', 'name': 'website', 'id': 'website', 'value': defaults.website || "", 'placeholder': "Website"}]], + ['div', + ['textarea', defaults.text || "", {'rows': '10', 'name': 'text', 'id': 'comment', 'placeholder': "Comment"}]], + ['div', + ['input', {'type': 'submit', 'value': 'Add Comment'}]], + ])); + + $('input[type="submit"]', rv).on('click', function(event) { + func(rv, id); + event.stop(); + }); + return rv; +}; + + +function extract(form, parent) { + return { + text: $('textarea[id="comment"]', form).val() || null, + author: $('input[id="author"]', form).val() || null, + email: $('input[id="email"]', form).val() || null, + website: $('input[id="website"]', form).val() || null, + parent: parent + }; +} + + +function edit() { + +}; + + +function commit(form, parent) { + isso.create(extract(form, parent), function(status, rv) { + if (status == 201 || status == 202) { + insert(JSON.parse(rv)); + } + }); +}; + + +var insert = function insert(post) { + /* + Insert a comment into #isso_thread. + + :param post: JSON from API call + */ + + var path = encodeURIComponent(window.location.pathname), + date = new Date(parseInt(post['created']) * 1000); + + // create