README.rst: Unwind 'If ..., returns a 404' -> 'Returns a 404 if ...'
[nmhive.git] / nmbug.js
index ec33a362ea6dad0f12b51ece367900e57803e3c2..77b27c3edafbd55d5b3e624fdcea2c6d04ba881a 100644 (file)
--- a/nmbug.js
+++ b/nmbug.js
@@ -1,3 +1,29 @@
+/*
+ Copyright (C) 2014 W. Trevor King <wking@tremily.us>
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+ */
+
 var nmbug_server = 'http://localhost:5000';
 
 nmbug = {
@@ -52,68 +78,48 @@ nmbug = {
                request.send();
        },
        _edit_tags: function (frame, available_tags, message_id, tags) {
-               if (frame.document.createElement('dialog').show) {
+               var have_dialog_polyfill;
+               try {
+                       have_dialog_polyfill = frame.dialogPolyfill !== undefined;
+               } catch (error) {
+                       if (error.name == 'ReferenceError') {
+                               have_dialog_polyfill = false;
+                       }
+               }
+               if (frame.document.createElement('dialog').show || have_dialog_polyfill) {
                        this._x_edit_tags(frame, available_tags, message_id, tags);
                } else {
-                       var _this = this;
-                       var needed = [];
-
-                       function basename (element) {
-                               var url;
-                               if (!element.tagName) {
-                                       return;
-                               } else if (element.tagName.toLowerCase() == 'script') {
-                                       url = element.src;
-                               } else if (element.tagName.toLowerCase() == 'link') {
-                                       url = element.href;
-                               } else {
-                                       return;
-                               }
-                               return url.replace(/.*\//, '');
-                       }
+                       var script = frame.document.createElement('script');
+                       script.type = 'text/javascript';
+                       script.src = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.js';
+                       script.async = false;
+                       console.log('nmbug: loading dialog-polyfill.js');
+                       frame.document.head.appendChild(script);
 
-                       function onload () {
-                               var name = basename(this);
-                               console.log('nmbug: loaded ' + name, this);
-                               var index = needed.indexOf(name);
-                               if (index !== -1) {
-                                       needed.splice(index, 1);
-                               }
-                               if (needed.length == 0) {
-                                       _this._x_edit_tags(frame, available_tags, message_id, tags);
-                               }
-                       }
+                       var link = frame.document.createElement('link');
+                       link.rel = 'stylesheet';
+                       link.type = 'text/css';
+                       link.href = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.css';
+                       link.async = false;
+                       console.log('nmbug: loading dialog-polyfill.css');
+                       frame.document.head.appendChild(link);
 
-                       function has_header (name) {
-                               var nodes = frame.document.head.childNodes;
-                               for (var i = 0; i < nodes.length; i++) {
-                                       if (basename(nodes[i]) == name) {
-                                               return true;
+                       var _this = this;
+                       function edit_tags_after_dialog_polyfill () {
+                               try {
+                                       have_dialog_polyfill = frame.dialogPolyfill !== undefined;
+                                       console.log('have dialogPolyfill');
+                                       window.setTimeout(
+                                                       _this._x_edit_tags.bind(_this), 200,
+                                                       frame, available_tags, message_id, tags);
+                               } catch (error) {
+                                       if (error.name == 'ReferenceError') {
+                                               console.log('waiting for dialogPolyfill');
+                                               window.setTimeout(edit_tags_after_dialog_polyfill, 200);
                                        }
                                }
-                               return false;
-                       }
-
-                       if (!has_header('dialog-polyfill.js')) {
-                               needed.push('dialog-polyfill.js');
-                               var script = frame.document.createElement('script');
-                               script.type = 'text/javascript';
-                               script.src = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.js';
-                               script.onload = onload;
-                               console.log('nmbug: loading dialog-polyfill.js');
-                               frame.document.head.appendChild(script);
-                       }
-
-                       if (!has_header('dialog-polyfill.css')) {
-                               needed.push('dialog-polyfill.css');
-                               var link = frame.document.createElement('link');
-                               link.rel = 'stylesheet';
-                               link.type = 'text/css';
-                               link.href = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.css';
-                               link.onload = onload;
-                               console.log('nmbug: loading dialog-polyfill.css');
-                               frame.document.head.appendChild(link);
                        }
+                       edit_tags_after_dialog_polyfill();
                }
        },
        _x_edit_tags: function (frame, available_tags, message_id, tags) {
@@ -125,23 +131,26 @@ nmbug = {
                dialog.style.border = '1px solid rgba(0, 0, 0, 0.3)';
                dialog.style.borderRadius = '6px';
                dialog.style.boxShadow = '0 3px 7px rgba(0, 0, 0, 0.3)';
+               dialog.style.marginLeft = '10em';
+               dialog.style.marginRight = '10em';
 
                var content = frame.document.createElement('p');
                content.innerHTML = 'Edit tags for ' + message_id;
                dialog.appendChild(content);
 
-               var ul = frame.document.createElement('ul');
-               dialog.appendChild(ul);
+               var tag_list = frame.document.createElement('p');
+               dialog.appendChild(tag_list);
                for (var i = 0; i < available_tags.length; i++) {
-                       var li = frame.document.createElement('li');
-                       li.innerHTML = available_tags[i];
-                       li.style.cursor = 'pointer';
+                       var tag = frame.document.createElement('a');
+                       tag.innerHTML = available_tags[i];
+                       tag.style.cursor = 'pointer';
                        if (tags.indexOf(available_tags[i]) >= 0) {
-                               li.style.backgroundColor = 'lime';
+                               tag.style.backgroundColor = 'lime';
                        }
-                       li.onclick = this._toggle_tag.bind(
-                               this, message_id, available_tags[i], li);
-                       ul.appendChild(li);
+                       tag.onclick = this._toggle_tag.bind(
+                               this, message_id, available_tags[i], tag);
+                       tag_list.appendChild(tag);
+                       tag_list.appendChild(frame.document.createTextNode(' '));
                }
                var close = frame.document.createElement('button');
                close.innerHTML = 'Close';
@@ -150,18 +159,18 @@ nmbug = {
                };
                dialog.appendChild(close);
 
-               frame.document.body.appendChild(dialog);
+               frame.document.body.insertBefore(dialog, frame.document.body.firstChild);
 
                dialog.show();
        },
-       _toggle_tag: function (message_id, tag, li) {
+       _toggle_tag: function (message_id, tag, element) {
                var prefix;
-               if (li.style.backgroundColor == 'lime') {
+               if (element.style.backgroundColor == 'lime') {
                        prefix = '-';  /* unset */
-                       li.style.backgroundColor = null;
+                       element.style.backgroundColor = null;
                } else {
                        prefix = '+';  /* set */
-                       li.style.backgroundColor = 'lime';
+                       element.style.backgroundColor = 'lime';
                }
                var url = [
                        nmbug_server,