diff --git a/isso/js/app/api.js b/isso/js/app/api.js index 1108c0a..573a327 100644 --- a/isso/js/app/api.js +++ b/isso/js/app/api.js @@ -183,22 +183,9 @@ define(["app/lib/promise", "app/globals"], function(Q, globals) { return deferred.promise; }; - var remote_addr = function() { - var deferred = Q.defer(); - curl("GET", endpoint + "/check-ip", null, function(rv) { - if (rv.status === 200) { - deferred.resolve(rv.body); - } else { - deferred.reject(rv.body); - } - }); - return deferred.promise; - }; - return { endpoint: endpoint, salt: salt, - remote_addr: remote_addr, create: create, modify: modify, diff --git a/isso/js/app/dom.js b/isso/js/app/dom.js index fb8f5f1..364ece0 100644 --- a/isso/js/app/dom.js +++ b/isso/js/app/dom.js @@ -91,14 +91,26 @@ define(function() { this.parentNode.removeChild(this); }; - var DOM = function(query, root) { + window.Element.prototype.show = function() { + this.style.display = "block"; + }; + + window.Element.prototype.hide = function() { + this.style.display = "none"; + }; + + var DOM = function(query, root, single) { /* jQuery-like CSS selector which returns on :param query: either a - single node, a node list or null. + single node (unless single=false), a node list or null. :param root: only queries within the given element. */ + if (typeof single === "undefined") { + single = true; + } + if (! root) { root = window.document; } @@ -109,7 +121,7 @@ define(function() { return null; } - if (elements.length === 1) { + if (elements.length === 1 && single) { return elements[0]; } diff --git a/isso/js/app/isso.js b/isso/js/app/isso.js index 3a8fd96..70b2511 100644 --- a/isso/js/app/isso.js +++ b/isso/js/app/isso.js @@ -9,38 +9,6 @@ define(["app/dom", "app/utils", "app/config", "app/api", "app/jade", "app/i18n", var el = $.htmlify(jade.render("postbox")); - if (config["avatar"]) { - // add a default identicon to not waste CPU cycles - $(".avatar > svg", el).replace(lib.identicons.blank(4, 48)); - - // on text area focus, generate identicon from IP address - $(".textarea-wrapper > .textarea", el).on("focus", function() { - if ($(".avatar svg", el).getAttribute("className") === "blank") { - $(".avatar svg", el).replace( - lib.identicons.generate(lib.pbkdf2(api.remote_addr(), api.salt, 1000, 6), 4, 48)); - } - }); - - // update identicon on email input. Listens on keyup, after 200ms the - // new identicon is generated. - var active; - $(".input-wrapper > [type=email]", el).on("keyup", function() { - if (active) { - clearTimeout(active); - } - active = setTimeout(function() { - lib.pbkdf2($(".input-wrapper > [type=email]", el).value || api.remote_addr(), api.salt, 1000, 6) - .then(function(rv) { - $(".avatar svg", el).replace(lib.identicons.generate(rv, 4, 48)); - }); - }, 200); - }, false); - - $(".input-wrapper > [type=email]", el).on("keydown", function() { - clearTimeout(active); - }, false); - } - // callback on success (e.g. to toggle the reply button) el.onsuccess = function() {}; @@ -210,6 +178,7 @@ define(["app/dom", "app/utils", "app/config", "app/api", "app/jade", "app/i18n", $("a.edit", footer).toggle("click", function(toggler) { var edit = $("a.edit", footer); + var avatar = $(".avatar", el, false)[0]; edit.textContent = i18n.translate("comment-save"); edit.insertAfter($.new("a.cancel", i18n.translate("comment-cancel"))).on("click", function() { @@ -230,9 +199,14 @@ define(["app/dom", "app/utils", "app/config", "app/api", "app/jade", "app/i18n", text.textContent = ""; text.append(textarea); }); + + if (avatar !== null) { + avatar.hide(); + } }, function(toggler) { var textarea = $(".textarea", text); + var avatar = $(".avatar", el, false)[0]; if (! toggler.canceled && textarea !== null) { if (utils.text(textarea.innerHTML).length < 3) { @@ -252,6 +226,10 @@ define(["app/dom", "app/utils", "app/config", "app/api", "app/jade", "app/i18n", text.classList.remove("textarea-wrapper"); text.classList.add("text"); + if (avatar !== null) { + avatar.show(); + } + $("a.cancel", footer).remove(); $("a.edit", footer).textContent = i18n.translate("comment-edit"); } diff --git a/isso/js/app/lib.js b/isso/js/app/lib.js index 4214131..98eaa0a 100644 --- a/isso/js/app/lib.js +++ b/isso/js/app/lib.js @@ -1,8 +1,6 @@ define(function (require) { return { editorify: require("app/lib/editor"), - identicons: require("app/lib/identicons"), - pbkdf2: require("app/lib/pbkdf2"), - sha1: require("app/lib/sha1") + identicons: require("app/lib/identicons") }; }); diff --git a/isso/js/app/lib/pbkdf2.js b/isso/js/app/lib/pbkdf2.js deleted file mode 100644 index db15c1f..0000000 --- a/isso/js/app/lib/pbkdf2.js +++ /dev/null @@ -1,201 +0,0 @@ -define(["app/lib/promise", "app/lib/sha1"], function(Q, sha1) { - /* - * JavaScript implementation of Password-Based Key Derivation Function 2 - * (PBKDF2) as defined in RFC 2898. - * Version 1.5 - * Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Parvez Anandam - * parvez@anandam.com - * http://anandam.com/pbkdf2 - * - * Distributed under the BSD license - * - * Uses Paul Johnston's excellent SHA-1 JavaScript library sha1.js: - * http://pajhome.org.uk/crypt/md5/sha1.html - * (uses the binb_sha1(), rstr2binb(), binb2str(), rstr2hex() functions from that libary) - * - * Thanks to Felix Gartsman for pointing out a bug in version 1.0 - * Thanks to Thijs Van der Schaeghe for pointing out a bug in version 1.1 - * Thanks to Richard Gautier for asking to clarify dependencies in version 1.2 - * Updated contact information from version 1.3 - * Thanks to Stuart Heinrich for pointing out updates to PAJ's SHA-1 library in version 1.4 - */ - - - /* - * The four arguments to the constructor of the PBKDF2 object are - * the password, salt, number of iterations and number of bytes in - * generated key. This follows the RFC 2898 definition: PBKDF2 (P, S, c, dkLen) - * - * The method deriveKey takes two parameters, both callback functions: - * the first is used to provide status on the computation, the second - * is called with the result of the computation (the generated key in hex). - * - * Example of use: - * - * - * - * - *
- * - */ - - var PBKDF2 = function(password, salt, num_iterations, num_bytes) - { - // Remember the password and salt - var m_bpassword = sha1.rstr2binb(password); - var m_salt = salt; - - // Total number of iterations - var m_total_iterations = num_iterations; - - // Run iterations in chunks instead of all at once, so as to not block. - // Define size of chunk here; adjust for slower or faster machines if necessary. - var m_iterations_in_chunk = 10; - - // Iteration counter - var m_iterations_done = 0; - - // Key length, as number of bytes - var m_key_length = num_bytes; - - // The hash cache - var m_hash = null; - - // The length (number of bytes) of the output of the pseudo-random function. - // Since HMAC-SHA1 is the standard, and what is used here, it's 20 bytes. - var m_hash_length = 20; - - // Number of hash-sized blocks in the derived key (called 'l' in RFC2898) - var m_total_blocks = Math.ceil(m_key_length/m_hash_length); - - // Start computation with the first block - var m_current_block = 1; - - // Used in the HMAC-SHA1 computations - var m_ipad = new Array(16); - var m_opad = new Array(16); - - // This is where the result of the iterations gets sotred - var m_buffer = new Array(0x0,0x0,0x0,0x0,0x0); - - // The result - var m_key = ""; - - // This object - var m_this_object = this; - - // The function to call with the result - var m_result_func; - - // The function to call with status after computing every chunk - var m_status_func; - - // Set up the HMAC-SHA1 computations - if (m_bpassword.length > 16) m_bpassword = sha1.binb_sha1(m_bpassword, password.length * chrsz); - for(var i = 0; i < 16; ++i) - { - m_ipad[i] = m_bpassword[i] ^ 0x36363636; - m_opad[i] = m_bpassword[i] ^ 0x5C5C5C5C; - } - - - // Starts the computation - this.deriveKey = function(status_callback, result_callback) - { - m_status_func = status_callback; - m_result_func = result_callback; - setTimeout(function() { m_this_object.do_PBKDF2_iterations() }, 0); - } - - - // The workhorse - this.do_PBKDF2_iterations = function() - { - var iterations = m_iterations_in_chunk; - if (m_total_iterations - m_iterations_done < m_iterations_in_chunk) - iterations = m_total_iterations - m_iterations_done; - - for(var i=0; i