Fix race between comment saving and reply dialog

In some cases, the reply dialog could be opened before all comment
drafts have been saved, causing the draft to not appear in the dialog.

This change maintains an array corresponding to each draft request and
refers to it before opening the reply dialog. If the array is populated,
a non-blocking alert is shown with the text 'Try again when all comments
have saved.'

Bug: Issue 5369
Change-Id: Ieb73e7d7b4f66daff6cc2278a84c2195b7d0e541
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
index 63e3574..eea27db 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
@@ -16,6 +16,8 @@
 
   var COMMIT_MESSAGE_PATH = '/COMMIT_MSG';
 
+  var COMMENT_SAVE = 'Try again when all comments have saved.';
+
   var DiffSides = {
     LEFT: 'left',
     RIGHT: 'right',
@@ -32,6 +34,12 @@
      * @event title-change
      */
 
+    /**
+     * Fired when user tries to navigate away while comments are pending save.
+     *
+     * @event show-alert
+     */
+
     properties: {
       /**
        * URL params passed from the router.
@@ -82,7 +90,7 @@
       _filesWeblinks: Object,
 
       /**
-       * Map of paths in the current chnage and patch range that have comments
+       * Map of paths in the current change and patch range that have comments
        * or drafts or robot comments.
        */
       _commentMap: Object,
@@ -330,6 +338,11 @@
       if (this.modifierPressed(e)) { return; }
 
       if (!this._loggedIn) { return; }
+      if (this.$.restAPI.hasPendingDiffDrafts()) {
+        this.dispatchEvent(new CustomEvent('show-alert',
+            {detail: {message: COMMENT_SAVE}, bubbles: true}));
+        return;
+      }
 
       this.set('changeViewState.showReplyDialog', true);
       e.preventDefault();