Move triggering thread creation to gr-diff-host

This is a step towards allowing code using gr-diff pick their own
comment widget. The threads are still created by
gr-diff-comment-thread-group in this commit - only the triggering is
moved. But that will change in a future CL.

Change-Id: If42b07df11422b69e7e1421c15e923d208012b47
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
index 6f61fb9..19fb308 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
@@ -177,7 +177,7 @@
     },
 
     listeners: {
-      'draft-interaction': '_handleDraftInteraction',
+      'create-comment': '_handleCreateComment',
     },
 
     observers: [
@@ -445,11 +445,35 @@
           this.patchRange);
     },
 
-    _handleDraftInteraction() {
+    /** @param {CustomEvent} e */
+    _handleCreateComment(e) {
+      const {threadGroupEl, lineNum, side, range} = e.detail;
+      const threadEl = this._getOrCreateThread(threadGroupEl, side, range);
+      threadEl.addOrEditDraft(lineNum, range);
       this.$.reporting.recordDraftInteraction();
     },
 
     /**
+     * Gets or creates a comment thread from a specific thread group.
+     * May include a range, if the comment is a range comment.
+     *
+     * @param {!Object} threadGroupEl
+     * @param {string} commentSide
+     * @param {!Object=} range
+     * @return {!Object}
+     */
+    _getOrCreateThread(threadGroupEl, commentSide, range=undefined) {
+      let threadEl = threadGroupEl.getThread(commentSide, range);
+
+      if (!threadEl) {
+        threadGroupEl.addNewThread(commentSide, range);
+        Polymer.dom.flush();
+        threadEl = threadGroupEl.getThread(commentSide, range);
+      }
+      return threadEl;
+    },
+
+    /**
      * Take a diff that was loaded with a ignore-whitespace other than
      * IGNORE_NONE, and convert delta chunks labeled as common into shared
      * chunks.
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html
index f83253e..3374686 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html
+++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html
@@ -776,6 +776,29 @@
       });
     });
 
+    test('_getOrCreateThread', () => {
+      const threadGroupEl =
+          document.createElement('gr-diff-comment-thread-group');
+      const commentSide = 'left';
+
+      assert.isOk(element._getOrCreateThread(threadGroupEl,
+          commentSide));
+
+      // Try to fetch a thread with a different range.
+      range = {
+        startLine: 1,
+        startChar: 1,
+        endLine: 1,
+        endChar: 3,
+      };
+
+      assert.isOk(element._getOrCreateThread(
+          threadGroupEl, commentSide, range));
+      const threadCount = Polymer.dom(threadGroupEl.root).
+            querySelectorAll('gr-diff-comment-thread').length;
+      assert.equal(threadCount, 2);
+    });
+
     suite('_translateChunksToIgnore', () => {
       let content;