Refactor: Move changes.js into a Polymer Behavior

This is the idiomatic Polymer way of sharing common functionality.
It also gets rid of the need to pull in changes.js in each test
that requires it.

Change-Id: I076d39ef69c12612a636c0c9b0b52ccdd58791f5
This commit is contained in:
Andrew Bonventre
2016-02-01 13:42:02 -08:00
parent e8f75dbb0f
commit 8949944b03
20 changed files with 163 additions and 136 deletions

View File

@@ -0,0 +1,106 @@
<!--
Copyright (C) 2016 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="../bower_components/polymer/polymer.html">
<script>
(function(window) {
'use strict';
var RESTClientBehavior = {
ChangeDiffType: {
ADDED: 'ADDED',
COPIED: 'COPIED',
DELETED: 'DELETED',
MODIFIED: 'MODIFIED',
RENAMED: 'RENAMED',
REWRITE: 'REWRITE',
},
ChangeStatus: {
NEW: 'NEW',
MERGED: 'MERGED',
ABANDONED: 'ABANDONED',
DRAFT: 'DRAFT',
},
// Must be kept in sync with the ListChangesOption enum and protobuf.
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
},
listChangesOptionsToHex: function() {
var v = 0;
for (var i = 0; i < arguments.length; i++) {
v |= 1 << arguments[i];
}
return v.toString(16);
},
changeBaseURL: function(changeNum, patchNum) {
var v = '/changes/' + changeNum;
if (patchNum) {
v += '/revisions/' + patchNum;
}
return v;
},
};
window.Gerrit = window.Gerrit || {};
window.Gerrit.RESTClientBehavior = RESTClientBehavior;
})(window);
</script>

View File

@@ -26,7 +26,6 @@ limitations under the License.
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/app.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/util.js"></script>
<dom-module id="gr-app">

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-request.html">
@@ -114,6 +115,8 @@ limitations under the License.
},
},
behaviors: [ Gerrit.RESTClientBehavior ],
observers: [
'_actionsChanged(actions, _revisionActions)',
],
@@ -132,7 +135,7 @@ limitations under the License.
},
_computeRevisionActionsPath: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/actions';
return this.changeBaseURL(changeNum, patchNum) + '/actions';
},
_getValuesFor: function(obj) {
@@ -208,7 +211,7 @@ limitations under the License.
var xhr = document.createElement('gr-request');
this._xhrPromise = xhr.send({
method: method,
url: Changes.baseURL(this.changeNum,
url: this.changeBaseURL(this.changeNum,
revisionAction ? this.patchNum : null) + actionEndpoint,
body: payload,
});

View File

@@ -16,6 +16,7 @@ limitations under the License.
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../styles/gr-change-list-styles.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-account-link.html">
<link rel="import" href="gr-change-star.html">
<link rel="import" href="gr-date-formatter.html">
@@ -107,6 +108,8 @@ limitations under the License.
},
},
behaviors: [ Gerrit.RESTClientBehavior ],
_computeChangeURL: function(changeNum) {
if (!changeNum) { return ''; }
return '/c/' + changeNum + '/';
@@ -116,13 +119,13 @@ limitations under the License.
if (change.mergeable != null && change.mergeable == false) {
return 'Merge Conflict';
}
if (change.status == Changes.Status.MERGED) {
if (change.status == this.ChangeStatus.MERGED) {
return 'Merged';
}
if (change.status == Changes.Status.DRAFT) {
if (change.status == this.ChangeStatus.DRAFT) {
return 'Draft';
}
if (change.status == Changes.Status.ABANDONED) {
if (change.status == this.ChangeStatus.ABANDONED) {
return 'Abandoned';
}
return '';

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-change-list.html">
@@ -135,6 +136,8 @@ limitations under the License.
},
},
behaviors: [ Gerrit.RESTClientBehavior ],
attached: function() {
this.fire('title-change', {title: this._query});
},
@@ -155,9 +158,9 @@ limitations under the License.
},
_computeQueryParams: function(query, offset) {
var options = Changes.listChangesOptionsToHex(
Changes.ListChangesOption.LABELS,
Changes.ListChangesOption.DETAILED_ACCOUNTS
var options = this.listChangesOptionsToHex(
this.ListChangesOption.LABELS,
this.ListChangesOption.DETAILED_ACCOUNTS
);
var obj = {
n: DEFAULT_NUM_CHANGES, // Number of results to return.

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-account-link.html">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-change-actions.html">
@@ -322,6 +323,8 @@ limitations under the License.
}
},
behaviors: [ Gerrit.RESTClientBehavior ],
ready: function() {
app.accountReady.then(function() {
this._loggedIn = app.loggedIn;
@@ -436,7 +439,7 @@ limitations under the License.
},
_computeChangeStatus: function(status) {
if (status == Changes.Status.NEW) {
if (status == this.ChangeStatus.NEW) {
return '';
}
return '(' + status.toLowerCase() + ')';
@@ -447,7 +450,7 @@ limitations under the License.
},
_computeCommitInfoPath: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/commit?links';
return this.changeBaseURL(changeNum, patchNum) + '/commit?links';
},
_computeCommentsPath: function(changeNum) {
@@ -459,9 +462,9 @@ limitations under the License.
},
_computeDetailQueryParams: function() {
var options = Changes.listChangesOptionsToHex(
Changes.ListChangesOption.ALL_REVISIONS,
Changes.ListChangesOption.CHANGE_ACTIONS
var options = this.listChangesOptionsToHex(
this.ListChangesOption.ALL_REVISIONS,
this.ListChangesOption.CHANGE_ACTIONS
);
return { O: options };
},

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<dom-module id="gr-dashboard-view">
<template>
@@ -62,10 +63,6 @@ limitations under the License.
* @event title-change
*/
attached: function() {
this.fire('title-change', {title: 'My Reviews'});
},
properties: {
viewState: Object,
@@ -88,11 +85,17 @@ limitations under the License.
},
},
behaviors: [ Gerrit.RESTClientBehavior ],
attached: function() {
this.fire('title-change', {title: 'My Reviews'});
},
_computeQueryParams: function() {
var options = Changes.listChangesOptionsToHex(
Changes.ListChangesOption.LABELS,
Changes.ListChangesOption.DETAILED_ACCOUNTS,
Changes.ListChangesOption.REVIEWED
var options = this.listChangesOptionsToHex(
this.ListChangesOption.LABELS,
this.ListChangesOption.DETAILED_ACCOUNTS,
this.ListChangesOption.REVIEWED
);
return {
O: options,

View File

@@ -16,6 +16,7 @@ limitations under the License.
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-dropdown/iron-dropdown.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-diff.html">
@@ -180,6 +181,8 @@ limitations under the License.
}.bind(this));
},
behaviors: [ Gerrit.RESTClientBehavior ],
attached: function() {
if (this._path) {
this.fire('title-change', {title: this._path});
@@ -290,13 +293,13 @@ limitations under the License.
},
_computeChangeDetailQueryParams: function() {
return { O: Changes.listChangesOptionsToHex(
Changes.ListChangesOption.ALL_REVISIONS
return { O: this.listChangesOptionsToHex(
this.ListChangesOption.ALL_REVISIONS
)};
},
_computeFilesPath: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/files';
return this.changeBaseURL(changeNum, patchNum) + '/files';
},
_computeProjectConfigPath: function(project) {

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-diff-preferences.html">
<link rel="import" href="gr-diff-side.html">
@@ -196,6 +197,8 @@ limitations under the License.
_diffPreferencesPromise: Object, // Used for testing.
},
behaviors: [ Gerrit.RESTClientBehavior ],
observers: [
'_prefsChanged(prefs.*)',
],
@@ -306,16 +309,16 @@ limitations under the License.
},
_computeDiffPath: function(changeNum, patchNum, path) {
return Changes.baseURL(changeNum, patchNum) + '/files/' +
return this.changeBaseURL(changeNum, patchNum) + '/files/' +
encodeURIComponent(path) + '/diff';
},
_computeCommentsPath: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/comments';
return this.changeBaseURL(changeNum, patchNum) + '/comments';
},
_computeDraftsPath: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/drafts';
return this.changeBaseURL(changeNum, patchNum) + '/drafts';
},
_computeDiffQueryParams: function(basePatchNum) {

View File

@@ -15,6 +15,7 @@ limitations under the License.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-ajax.html">
<dom-module id="gr-file-list">
@@ -119,6 +120,8 @@ limitations under the License.
}
},
behaviors: [ Gerrit.RESTClientBehavior ],
attached: function() {
document.body.addEventListener('keydown', this._boundKeyHandler);
},
@@ -141,7 +144,7 @@ limitations under the License.
},
_computeFilesURL: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/files';
return this.changeBaseURL(changeNum, patchNum) + '/files';
},
_computeCommentsString: function(comments, patchNum, path) {
@@ -155,7 +158,7 @@ limitations under the License.
},
_computeDraftsURL: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/drafts';
return this.changeBaseURL(changeNum, patchNum) + '/drafts';
},
_computeDraftsString: function(drafts, path) {

View File

@@ -18,6 +18,7 @@ limitations under the License.
<link rel="import" href="../bower_components/iron-autogrow-textarea/iron-autogrow-textarea.html">
<link rel="import" href="../bower_components/iron-dropdown/iron-dropdown.html">
<link rel="import" href="../bower_components/iron-selector/iron-selector.html">
<link rel="import" href="../behaviors/rest-client-behavior.html">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-request.html">
@@ -193,6 +194,8 @@ limitations under the License.
_xhrPromise: Object, // Used for testing.
},
behaviors: [ Gerrit.RESTClientBehavior ],
get opened() {
return this.$.dropdown.opened;
},
@@ -312,7 +315,7 @@ limitations under the License.
var xhr = document.createElement('gr-request');
this._xhrPromise = xhr.send({
method: 'POST',
url: Changes.baseURL(this.changeNum, this.patchNum) + '/review',
url: this.changeBaseURL(this.changeNum, this.patchNum) + '/review',
body: payload,
});

View File

@@ -1,97 +0,0 @@
// Copyright (C) 2015 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.
'use strict';
var Changes = Changes || {};
Changes.DiffType = {
ADDED: 'ADDED',
COPIED: 'COPIED',
DELETED: 'DELETED',
MODIFIED: 'MODIFIED',
RENAMED: 'RENAMED',
REWRITE: 'REWRITE',
};
Changes.Status = {
NEW: 'NEW',
MERGED: 'MERGED',
ABANDONED: 'ABANDONED',
DRAFT: 'DRAFT',
};
// Must be kept in sync with the ListChangesOption enum and protobuf.
Changes.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
};
Changes.listChangesOptionsToHex = function() {
var v = 0;
for (var i = 0; i < arguments.length; i++) {
v |= 1 << arguments[i];
}
return v.toString(16);
};
Changes.baseURL = function(changeNum, patchNum) {
var v = '/changes/' + changeNum;
if (patchNum) {
v += '/revisions/' + patchNum;
}
return v;
};

View File

@@ -20,7 +20,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/util.js"></script>
<link rel="import" href="../bower_components/iron-test-helpers/iron-test-helpers.html">

View File

@@ -20,7 +20,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/util.js"></script>

View File

@@ -22,7 +22,6 @@ limitations under the License.
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/util.js"></script>
<link rel="import" href="../bower_components/iron-test-helpers/iron-test-helpers.html">

View File

@@ -21,7 +21,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/util.js"></script>

View File

@@ -20,7 +20,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/util.js"></script>

View File

@@ -21,7 +21,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/util.js"></script>

View File

@@ -21,7 +21,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/util.js"></script>

View File

@@ -20,7 +20,6 @@ limitations under the License.
<script src="../bower_components/webcomponentsjs/webcomponents.min.js"></script>
<script src="../bower_components/web-component-tester/browser.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/fake-app.js"></script>
<script src="../scripts/util.js"></script>