Cleanup gr-change-view (remove gr-ajax use)

Bug: Issue 3988
Change-Id: I9a0134ef96ee48a54cac85bd92e3dea88cb7c76f
diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.html b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.html
index 00fb2c9..8b77489 100644
--- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.html
+++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.html
@@ -18,7 +18,6 @@
 <link rel="import" href="../../../behaviors/keyboard-shortcut-behavior.html">
 <link rel="import" href="../../../behaviors/rest-client-behavior.html">
 <link rel="import" href="../../shared/gr-account-link/gr-account-link.html">
-<link rel="import" href="../../shared/gr-ajax/gr-ajax.html">
 <link rel="import" href="../../shared/gr-button/gr-button.html">
 <link rel="import" href="../../shared/gr-change-star/gr-change-star.html">
 <link rel="import" href="../../shared/gr-date-formatter/gr-date-formatter.html">
@@ -218,22 +217,6 @@
         }
       }
     </style>
-    <gr-ajax id="detailXHR"
-        url="[[_computeDetailPath(_changeNum)]]"
-        params="[[_computeDetailQueryParams()]]"
-        last-response="{{_change}}"
-        loading="{{_loading}}"></gr-ajax>
-    <gr-ajax id="commentsXHR"
-        url="[[_computeCommentsPath(_changeNum)]]"
-        last-response="{{_comments}}"></gr-ajax>
-    <gr-ajax id="commitInfoXHR"
-        url="[[_computeCommitInfoPath(_changeNum, _patchNum)]]"
-        last-response="{{_commitInfo}}"></gr-ajax>
-    <!-- TODO(andybons): Cache the project config. -->
-    <gr-ajax id="configXHR"
-        auto
-        url="[[_computeProjectConfigPath(_change.project)]]"
-        last-response="{{_projectConfig}}"></gr-ajax>
     <div class="container loading" hidden$="{{!_loading}}">Loading...</div>
     <div class="container" hidden$="{{_loading}}">
       <div class="headerContainer">
diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js
index 9af6aa9..0709f62 100644
--- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js
+++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.js
@@ -225,31 +225,6 @@
       return '(' + status.toLowerCase() + ')';
     },
 
-    _computeDetailPath: function(changeNum) {
-      return '/changes/' + changeNum + '/detail';
-    },
-
-    _computeCommitInfoPath: function(changeNum, patchNum) {
-      return this.changeBaseURL(changeNum, patchNum) + '/commit?links';
-    },
-
-    _computeCommentsPath: function(changeNum) {
-      return '/changes/' + changeNum + '/comments';
-    },
-
-    _computeProjectConfigPath: function(project) {
-      return '/projects/' + encodeURIComponent(project) + '/config';
-    },
-
-    _computeDetailQueryParams: function() {
-      var options = this.listChangesOptionsToHex(
-          this.ListChangesOption.ALL_REVISIONS,
-          this.ListChangesOption.CHANGE_ACTIONS,
-          this.ListChangesOption.DOWNLOAD_COMMANDS
-      );
-      return {O: options};
-    },
-
     _computeLatestPatchNum: function(change) {
       return change.revisions[change.current_revision]._number;
     },
@@ -345,13 +320,44 @@
 
     _getDiffDrafts: function() {
       return this.$.restAPI.getDiffDrafts(this._changeNum).then(
-          function(drafts) { return this._diffDrafts = drafts; }.bind(this));
+          function(drafts) {
+            return this._diffDrafts = drafts;
+          }.bind(this));
     },
 
     _getLoggedIn: function() {
       return this.$.restAPI.getLoggedIn();
     },
 
+    _getProjectConfig: function() {
+      return this.$.restAPI.getProjectConfig(this._change.project).then(
+          function(config) {
+            this._projectConfig = config;
+          }.bind(this));
+    },
+
+    _getChangeDetail: function() {
+      return this.$.restAPI.getChangeDetail(this._changeNum).then(
+          function(change) {
+            this._change = change;
+          }.bind(this));
+    },
+
+    _getComments: function() {
+      return this.$.restAPI.getDiffComments(this._changeNum).then(
+          function(comments) {
+            this._comments = comments;
+          }.bind(this));
+    },
+
+    _getCommitInfo: function() {
+      return this.$.restAPI.getChangeCommitInfo(
+          this._changeNum, this._patchNum).then(
+              function(commitInfo) {
+                this._commitInfo = commitInfo;
+              }.bind(this));
+    },
+
     _reloadDiffDrafts: function() {
       this._diffDrafts = {};
       this._getDiffDrafts().then(function() {
@@ -362,24 +368,31 @@
     },
 
     _reload: function() {
+      this._loading = true;
+
       this._getLoggedIn().then(function(loggedIn) {
         if (!loggedIn) { return; }
 
         this._reloadDiffDrafts();
       }.bind(this));
 
-      var detailCompletes = this.$.detailXHR.generateRequest().completes;
-      this.$.commentsXHR.generateRequest();
+      var detailCompletes = this._getChangeDetail().then(function() {
+        this._loading = false;
+      }.bind(this));
+      this._getComments();
 
       var reloadPatchNumDependentResources = function() {
         return Promise.all([
-          this.$.commitInfoXHR.generateRequest().completes,
+          this._getCommitInfo(),
           this.$.actions.reload(),
           this.$.fileList.reload(),
         ]);
       }.bind(this);
       var reloadDetailDependentResources = function() {
-        return this.$.relatedChanges.reload();
+        return Promise.all([
+          this.$.relatedChanges.reload(),
+          this._getProjectConfig(),
+        ]);
       }.bind(this);
 
       this._resetHeaderEl();
diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html
index 6d4d4b0..ee08ba1 100644
--- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html
+++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_test.html
@@ -35,27 +35,12 @@
 <script>
   suite('gr-change-view tests', function() {
     var element;
-    var server;
 
     setup(function() {
+      stub('gr-rest-api-interface', {
+        getAccount: function() { return Promise.resolve(null); },
+      });
       element = fixture('basic');
-      element.$.configXHR.auto = false;
-
-      server = sinon.fakeServer.create();
-      // Eat any requests made by elements in this suite.
-      server.respondWith(
-        'GET',
-        /\/changes\/(.*)/,
-        [
-          200,
-          {'Content-Type': 'application/json'},
-          ')]}\'\n{}',
-        ]
-      );
-    });
-
-    teardown(function() {
-      server.restore();
     });
 
     test('keyboard shortcuts', function() {
@@ -66,7 +51,6 @@
           'Should navigate to /');
       showStub.restore();
 
-
       MockInteractions.pressAndReleaseKeyOn(element, 65);  // 'a'
       var overlayEl = element.$.replyOverlay;
       assert.isFalse(overlayEl.opened);
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
index 477fafe..ee0a7db 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-api-interface.js
@@ -17,6 +17,54 @@
   var JSON_PREFIX = ')]}\'';
   var PARENT_PATCH_NUM = 'PARENT';
 
+  // Must be kept in sync with the ListChangesOption enum and protobuf.
+  var ListChangesOption = {
+    LABELS: 0,
+    DETAILED_LABELS: 8,
+
+    // Return information on the current patch set of the change.
+    CURRENT_REVISION: 1,
+    ALL_REVISIONS: 2,
+
+    // If revisions are included, parse the commit object.
+    CURRENT_COMMIT: 3,
+    ALL_COMMITS: 4,
+
+    // If a patch set is included, include the files of the patch set.
+    CURRENT_FILES: 5,
+    ALL_FILES: 6,
+
+    // If accounts are included, include detailed account info.
+    DETAILED_ACCOUNTS: 7,
+
+    // Include messages associated with the change.
+    MESSAGES: 9,
+
+    // Include allowed actions client could perform.
+    CURRENT_ACTIONS: 10,
+
+    // Set the reviewed boolean for the caller.
+    REVIEWED: 11,
+
+    // Include download commands for the caller.
+    DOWNLOAD_COMMANDS: 13,
+
+    // Include patch set weblinks.
+    WEB_LINKS: 14,
+
+    // Include consistency check results.
+    CHECK: 15,
+
+    // Include allowed change actions client could perform.
+    CHANGE_ACTIONS: 16,
+
+    // Include a copy of commit messages including review footers.
+    COMMIT_FOOTERS: 17,
+
+    // Include push certificate information along with any patch sets.
+    PUSH_CERTIFICATES: 18
+  };
+
   Polymer({
     is: 'gr-rest-api-interface',
 
@@ -86,6 +134,11 @@
       return this._fetchSharedCacheURL('/config/server/info');
     },
 
+    getProjectConfig: function(project) {
+      return this._fetchSharedCacheURL(
+          '/projects/' + encodeURIComponent(project) + '/config');
+    },
+
     getVersion: function() {
       return this._fetchSharedCacheURL('/config/server/version');
     },
@@ -135,6 +188,23 @@
       return this._sharedFetchPromises[url];
     },
 
+    getChangeDetail: function(changeNum, opt_cancelCondition) {
+      var options = this._listChangesOptionsToHex(
+          ListChangesOption.ALL_REVISIONS,
+          ListChangesOption.CHANGE_ACTIONS,
+          ListChangesOption.DOWNLOAD_COMMANDS
+      );
+      return this.fetchJSON(
+          this._changeBaseURL(changeNum) + '/detail',
+          opt_cancelCondition,
+          {O: options});
+    },
+
+    getChangeCommitInfo: function(changeNum, patchNum) {
+      return this.fetchJSON(
+          this._changeBaseURL(changeNum, patchNum) + '/commit?links');
+    },
+
     getChangeFiles: function(changeNum, patchNum) {
       return this.fetchJSON(
           this._changeBaseURL(changeNum, patchNum) + '/files');
@@ -215,7 +285,7 @@
         opt_patchNum, opt_path) {
       if (!opt_basePatchNum && !opt_patchNum && !opt_path) {
         return this.fetchJSON(
-            this._getDiffCommentsFetchURL(changeNum, '/drafts'));
+            this._getDiffCommentsFetchURL(changeNum, endpoint));
       }
 
       function onlyParent(c) { return c.side == PARENT_PATCH_NUM; }
@@ -262,6 +332,14 @@
       return v;
     },
 
+    _listChangesOptionsToHex: function() {
+      var v = 0;
+      for (var i = 0; i < arguments.length; i++) {
+        v |= 1 << arguments[i];
+      }
+      return v.toString(16);
+    },
+
     _getCookie: function(name) {
       var key = name + '=';
       var cookies = document.cookie.split(';');