blob: 7cdab50d97fd048ae7c7708bd1571fea5feffcd9 [file] [log] [blame]
<!--
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">
<link rel="import" href="gr-ajax.html">
<link rel="import" href="gr-request.html">
<dom-module id="gr-change-actions">
<template>
<style>
:host {
display: block;
}
button:before {
content: attr(data-label);
}
button {
background-color: #448aff;
border: none;
border-radius: 2px;
color: #fff;
cursor: pointer;
font: inherit;
padding: .5em .75em;
}
button[loading] {
cursor: wait;
opacity: .5;
}
button[loading]:before {
content: attr(data-loading-label);
}
button[disabled] {
background-color: #555961;
opacity: .5;
}
</style>
<gr-ajax id="actionsXHR"
url="[[_computeActionsPath(changeNum, patchNum)]]"
last-response="{{_actions}}"
loading="{{_loading}}"></gr-ajax>
<div>
<template is="dom-repeat" items="[[_computeActionValues(_actions)]]" as="action">
<button title$="[[action.title]]"
disabled$="[[!action.enabled]]"
data-action-key$="[[action.__key]]"
data-label$="[[action.label]]"
data-loading-label$="[[_computeLoadingLabel(action.__key)]]"
on-tap="_handleActionTap"></button>
</template>
</div>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'gr-change-actions',
/**
* Fired when the change should be reloaded.
*
* @event reload-change
*/
properties: {
changeNum: String,
patchNum: String,
_actions: {
type: Object,
observer: '_actionsChanged',
},
_loading: {
type: Boolean,
value: true,
},
},
reload: function() {
if (!!this.changeNum && !!this.patchNum) {
this.$.actionsXHR.generateRequest();
}
},
_actionsChanged: function(actions) {
this.hidden = actions.submit == null;
},
_computeActionsPath: function(changeNum, patchNum) {
return Changes.baseURL(changeNum, patchNum) + '/actions';
},
_computeActionValues: function(actions) {
var result = [];
for (var a in actions) {
// TODO(andybons): Add the rest of the actions.
if (a != 'submit') { continue; }
actions[a].__key = a;
result.push(actions[a]);
}
return result;
},
_computeLoadingLabel: function(action) {
return {
'submit': 'Submitting...',
}[action];
},
_handleActionTap: function(e) {
e.preventDefault();
var el = Polymer.dom(e).rootTarget;
var key = el.getAttribute('data-action-key');
if (key == 'submit') {
this._submitChange('/' + key, this._actions[key]);
}
},
_submitChange: function(endpoint, action) {
var buttonEl = this.$$('[data-action-key="' + action.__key + '"]');
buttonEl.setAttribute('loading', true);
buttonEl.disabled = true;
this._send(action.method, {}, endpoint).then(
function() {
this.fire('reload-change', null, {bubbles: false});
buttonEl.setAttribute('loading', false);
buttonEl.disabled = false;
}.bind(this)).catch(function(err) {
alert('Oops. Something went wrong. Check the console and bug the ' +
'PolyGerrit team for assistance.');
buttonEl.setAttribute('loading', false);
buttonEl.disabled = false;
throw err;
});
},
_send: function(method, payload, actionEndpoint) {
var xhr = document.createElement('gr-request');
this._xhrPromise = xhr.send({
method: method,
url: Changes.baseURL(this.changeNum, this.patchNum) + actionEndpoint,
body: payload,
});
return this._xhrPromise;
},
});
})();
</script>
</dom-module>