Commits

Blake Winton committed 405cf12

Get the api working.

Comments (0)

Files changed (1)

packages/thunderbird-rest-api/lib/tb-rest-api.js

 
 function ApiListHandler() {
   this.handle = function(request, response) {
-    var responseBody = "<html><title>APIs</title><body>" +
-      "<form method='post' action='post-facebook-message'>" +
-      "<fieldset>" +
-        "<legend>post-facebook-message</legend>" +
-        "<table cellpadding='0' cellspacing='0' border='0'>" +
-          "<tr>" +
-            "<th><label for='subject'>Subject: </label></th>" +
-            "<td><input type='text' name='subject' id='subject' value=''/></td>" +
-          "</tr><tr>" +
-            "<th><label for='from'>From: </label></th>" +
-            "<td><input type='text' name='from' id='from' value=''/></td>" +
-          "</tr><tr>" +
-            "<th><label for='to'>To: </label></th>" +
-            "<td><input type='text' name='to' id='to' value=''/></td>" +
-          "</tr><tr>" +
-            "<th><label for='body'>Body: </label></th>" +
-            "<td><input type='text' name='body' id='body' value=''/></td>" +
-          "</tr><tr>" +
-            "<td colspan='2'>" +
-            "<input id='submit' type='submit' value='Test it out!'/>" +
-            "</td>" +
-          "</tr>" +
-        "</table>" +
-      "</fieldset>" +
-      "</form>" +
-      "</body></html>";
+    var responseBody = null;
+    if (request.path == "/api/script.js")
+      responseBody = "// Massage the form data into JSON.\n" +
+        "function submitForm(input, e) {\n" +
+        "  window.alert($(input) + ', ' + e);\n" +
+        "  e.preventDefault();\n" +
+        "  let data = {};\n" +
+        "  $(input).parents('form')\n" +
+        "          .find('input[type=\"text\"]')\n" +
+        "          .each(function(i, e) {\n" +
+        "            data[e.id] = e.value;\n" +
+        "          });\n" +
+        "  $.ajax({\n" +
+        "    type: 'POST',\n" +
+        "    url: 'post-facebook-message',\n" +
+        "    data: $.toJSON(data)," +
+        "    dataType: 'json'\n" +
+        "  });\n" +
+        "}\n";
+    else
+      responseBody = "<html><title>APIs</title><body>\n" +
+        "<form method='post' action='post-facebook-message'>\n" +
+        "<fieldset>\n" +
+          "<legend>post-facebook-message</legend>\n" +
+          "<table cellpadding='0' cellspacing='0' border='0'>\n" +
+            "<tr>\n" +
+              "<th><label for='subject'>Subject: </label></th>\n" +
+              "<td><input type='text' name='subject' id='subject'/></td>\n" +
+            "</tr><tr>\n" +
+              "<th><label for='from'>From: </label></th>\n" +
+              "<td><input type='text' name='from' id='from' value=''/></td>\n" +
+            "</tr><tr>\n" +
+              "<th><label for='to'>To: </label></th>\n" +
+              "<td><input type='text' name='to' id='to' value=''/></td>\n" +
+            "</tr><tr>\n" +
+              "<th><label for='body'>Body: </label></th>\n" +
+              "<td><input type='text' name='body' id='body' value=''/></td>\n" +
+            "</tr><tr>\n" +
+              "<td colspan='2'>\n" +
+              "<input id='submit' type='submit' value='Test it out!'\n" +
+                      "onclick='submitForm(this, event)'/>\n" +
+              "</td>\n" +
+            "</tr>\n" +
+          "</table>\n" +
+        "</fieldset>\n" +
+        "</form>\n" +
+        "</body>\n" +
+        "<script type='application/javascript;version=1.8'\n" +
+        "        src='/api/script.js'></script>\n" +
+        "<script type='application/javascript;version=1.8'\n" +
+        "        src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>\n" +
+        "<script type='application/javascript;version=1.8'\n" +
+        "        src='http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js'></script>\n" +
+        "</html>";
     response.setStatusLine(request.httpVersion, 200, "OK");
     response.setHeader("Content-Type", "text/html", false);
     response.setHeader("Content-Length", responseBody.length.toString(),
 
   server.start(port);
   server.registerPathHandler("/post-facebook-message", new FacebookPostApiHandler());
-  server.registerPathHandler("/api", new ApiListHandler());
+  api_list = new ApiListHandler();
+  server.registerPathHandler("/api", api_list);
+  server.registerPathHandler("/api/script.js", api_list);
   return server;
 };