nmbug.js: Fill in the POST /mid/<message_id> handling
authorW. Trevor King <wking@tremily.us>
Sun, 21 Sep 2014 13:41:39 +0000 (06:41 -0700)
committerW. Trevor King <wking@tremily.us>
Sun, 21 Sep 2014 13:41:39 +0000 (06:41 -0700)
This is currently only (un)setting one tag per request.  If we want, a
later optimization could queue changes and push them all at once (e.g.
after a user pushes a submit button).  It would also be nice to update
the UI based on the list of returned tags, so we hear about changes
made by parallel parties.

nmbug.js

index 669272da4ab25e58d03008d2d3d3bf53343d79ff..4281cdb8f4e4a6574b2d131181d48f0b4c474048 100644 (file)
--- a/nmbug.js
+++ b/nmbug.js
@@ -115,7 +115,33 @@ nmbug = {
                dialog.show();
        },
        _toggle_tag: function (message_id, tag, li) {
-               alert('toggle ' + tag + ' for ' + message_id);
+               var prefix;
+               if (li.style.backgroundColor == 'lime') {
+                       prefix = '-';  /* unset */
+                       li.style.backgroundColor = null;
+               } else {
+                       prefix = '+';  /* set */
+                       li.style.backgroundColor = 'lime';
+               }
+               var url = [
+                       nmbug_server,
+                       'mid',
+                       encodeURIComponent(message_id),
+                       ].join('/');
+               console.log('nmbug: alter tags via ' + url);
+               var request = new XMLHttpRequest();
+               request.onload = function () {
+                       if (this.status == 200) {
+                               var tags = JSON.parse(this.response);
+                               console.log('nmbug: got tags', tags);
+                       } else {
+                               throw 'Error posting to ' + url + ' (status ' + this.status + ')';
+                       }
+               };
+               request.open('post', url, true);
+               request.setRequestHeader(
+                       'Content-Type', 'application/json; charset=UTF-8');
+               request.send(JSON.stringify([prefix + tag]));
        },
 };