Enable display of images in the comments in the new UI

The script used to display images in the comment section of the change
view did only work in the old UI.

This change adds a Polymer-based component that enables the display in
the new UI as well.

Change-Id: Id0c8f80f251c097ff85692d395e71af9b60cddea
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/HttpModule.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/HttpModule.java
index 634a7fe..627cfd8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/HttpModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/HttpModule.java
@@ -39,6 +39,10 @@
       serveRegex("^" + ImageServlet.PATH_PREFIX + "(.+)?$").with(ImageServlet.class);
     }
 
+    // GWT only
     DynamicSet.bind(binder(), WebUiPlugin.class).toInstance(new JavaScriptPlugin("imagare.js"));
+
+    // Polymer only
+    DynamicSet.bind(binder(), WebUiPlugin.class).toInstance(new JavaScriptPlugin("imagare.html"));
   }
 }
diff --git a/src/main/resources/static/gr-imagare-inline.html b/src/main/resources/static/gr-imagare-inline.html
new file mode 100644
index 0000000..9b698fa
--- /dev/null
+++ b/src/main/resources/static/gr-imagare-inline.html
@@ -0,0 +1,19 @@
+ <!--
+@license
+Copyright (C) 2019 The Android Open Source Project
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<dom-module id="gr-imagare-inline">
+  <template>
+  </template>
+  <script src="gr-imagare-inline.js"></script>
+</dom-module>
diff --git a/src/main/resources/static/gr-imagare-inline.js b/src/main/resources/static/gr-imagare-inline.js
new file mode 100644
index 0000000..3001252
--- /dev/null
+++ b/src/main/resources/static/gr-imagare-inline.js
@@ -0,0 +1,189 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+(function () {
+  'use strict';
+
+  const LINK_DECORATIONS = {
+    NONE: 1,
+    INLINE: 2,
+    TOOLTIP: 3,
+  };
+
+  Polymer({
+    is: 'gr-imagare-inline',
+
+    properties: {
+      _expandedObserver: MutationObserver,
+      _messageAddedObserver: MutationObserver,
+      _messages: Object,
+      _link_decoration: Number,
+      _pattern: String,
+      _decorator_fn: Function,
+    },
+
+    attached() {
+      this._getAccountPrefs().then(() => {
+        if (this._link_decoration === LINK_DECORATIONS.NONE) {
+          return;
+        }
+
+        this._expandedObserver = new MutationObserver(mutations => {
+          mutations.forEach(mut => {
+            if (!mut.target.classList.contains('expanded')){
+              return;
+            }
+            let links = this._getLinksFromMessage(mut.target);
+
+            if (!links) {
+              return;
+            }
+
+            for (const link of links) {
+              this._decorator_fn(link);
+            }
+          });
+        });
+
+        this._messageAddedObserver = new MutationObserver(mutations => {
+          mutations.forEach(mut => {
+            mut.addedNodes.forEach(node => {
+              if (node.tagName === "GR-MESSAGE") {
+                this._addExpandedObservers(node);
+              }
+            });
+          });
+        });
+
+        this._messageAddedObserver.observe(
+          document.getElementsByTagName('gr-messages-list')[0],
+          {
+            childList: true,
+          });
+
+        this._addObserversToMessages();
+      });
+    },
+
+    detached() {
+      this._expandedObserver.disconnect();
+      this._messageAddedObserver.disconnect();
+    },
+
+    _addObserversToMessages() {
+      this._messages = this._getMessages();
+
+      if (!this._messages) {
+        return;
+      }
+
+      for (const message of this._messages) {
+        this._addExpandedObservers(message);
+      }
+    },
+
+    _addExpandedObservers(message) {
+      this._expandedObserver.observe(message, {
+        attributes: true,
+        attributeOldValue: true,
+        attributFilter: ['class'],
+      });
+    },
+
+    _getAccountPrefs() {
+      return this.plugin.restApi('/accounts/self/imagare~preference')
+        .get('')
+        .then(prefs => {
+          if (!prefs || !prefs.link_decoration) {
+            this._link_decoration = LINK_DECORATIONS.NONE;
+            this._pattern = '.*';
+          } else {
+            this._link_decoration = LINK_DECORATIONS[prefs.link_decoration.toUpperCase()];
+            this._pattern = prefs.pattern || '.*';
+          }
+
+          switch (this._link_decoration) {
+            case LINK_DECORATIONS.INLINE:
+              this._decorator_fn = this._insertImage.bind(this);
+              break;
+            case LINK_DECORATIONS.TOOLTIP:
+              this._decorator_fn = this._addTooltip.bind(this);
+              break;
+            case LINK_DECORATIONS.NONE:
+            default:
+              this._decorator_fn = () => {};
+          }
+        });
+    },
+
+    _getMessages() {
+      let messageList = document.getElementsByTagName('gr-messages-list')[0];
+      if (messageList) {
+        return messageList.getElementsByTagName('gr-message');
+      }
+    },
+
+    _getLinksFromMessage(message) {
+      let links = [];
+      let linkedTexts = message.getElementsByTagName('gr-linked-text');
+      for (const e of linkedTexts) {
+        let aTags = e.getElementsByTagName('a');
+        if (aTags && aTags.length > 0){
+          for (const a of aTags){
+            if (a.getElementsByTagName('img').length > 0) {
+              continue;
+            }
+            if (!a.href.match(this._pattern)) {
+              continue;
+            }
+
+            links = links.concat(a);
+          }
+        }
+      }
+      return links.length > 0 ? links : null;
+    },
+
+    _createImage(url) {
+      let img = document.createElement('img');
+      img.setAttribute("src", url);
+      img.setAttribute("style", "max-width: 100%; height: auto;");
+
+      return img;
+    },
+
+    _insertImage(link) {
+      if (!link) {
+        return;
+      }
+
+      link.replaceWith(this._createImage(link.href));
+    },
+
+    _addTooltip(link) {
+      if (!link) {
+        return;
+      }
+
+      link.onmouseover = (event) => {
+        let img = this._createImage(link.href);
+        img.onmouseout = (event) => {
+          event.target.replaceWith(link);
+        }
+
+        event.target.replaceWith(img);
+      }
+    },
+  });
+})();
diff --git a/src/main/resources/static/imagare.html b/src/main/resources/static/imagare.html
new file mode 100644
index 0000000..42ea7ae
--- /dev/null
+++ b/src/main/resources/static/imagare.html
@@ -0,0 +1,28 @@
+<!--
+@license
+Copyright (C) 2019 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<link rel="import" href="./gr-imagare-inline.html">
+
+<dom-module id="imagare">
+  <script>
+    Gerrit.install(plugin => {
+      if (!window.Polymer) { return; }
+
+      plugin.registerCustomComponent('change-view-integration', 'gr-imagare-inline');
+    });
+  </script>
+</dom-module>
diff --git a/src/main/resources/static/imagare.js b/src/main/resources/static/imagare.js
index 91bec6f..779f1b5 100644
--- a/src/main/resources/static/imagare.js
+++ b/src/main/resources/static/imagare.js
@@ -13,6 +13,11 @@
 // limitations under the License.
 
 Gerrit.install(function(self) {
+
+    if (window.Polymer) { return; }
+
+    // The code below is only used by the GWT-UI
+
     function onComment(e) {
       var prefs = getPrefsFromCookie();
       if (prefs !== null) {