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