blob: 26cd06a8ce7f63a2863618aacc140963e6374625 [file] [log] [blame]
<!-- The <commit-sk> custom element declaration.
Displays a single commit in a uniform manner.
Attributes:
hash - The git commit hash to display.
commit - The serialized types.Commit that corresponds to the given hash.
repo - The URL of the underlying git repo. This assumes a
googlesource repo when deriving URLs from this value.
Events:
None.
Methods:
asString - Returns a text description of the commit.
refresh - Call this if the underlying commit has changed, i.e. the
commit.message has been updated.
Mailbox:
commits - Listens on the sk.Mailbox name of 'commits' to listen for the
data to populate the element. The mailbox data needs to be a serialized
slice of []*types.Commit.
[
{
commit_time: 1439649751,
author: "reed (reed@chromium.org)",
hash: "bb886749feb444edfd8fbf053a9ea815e3605f8a",
},
{
author: "reed (reed@chromium.org)",
commit_time: 1439648914,
hash: "e02716908fe82c7c4ae9b415793277620a22bcd6",
},
]
Note that the hashes need to be in order, but not necessarily
contiguous. Note that they also need to be supplied in time ascending
order, that is, the commit at index 0 needs to be the oldest commit, and
the last commit in the array needs to be the newest commit.
-->
<dom-module id="commit-sk">
<template>
<pre><span>{{_trimPad(commit.author)}}</span> <span>{{_humanize(commit.commit_time)}}</span> <span><a href$="{{_commitHref(commit, repo)}}" target="_blank">{{_trunc(commit.hash)}}</a></span> <span>{{_trimNewline(commit.message)}}</span></pre>
</template>
</dom-module>
<script>
Polymer({
is: "commit-sk",
properties: {
hash: {
type: String,
value: "",
reflectToAttribute: true,
observer: "_hashChange",
},
commit: {
type: Object,
value: null
},
repo: {
type: String,
value: "https://skia.googlesource.com/skia"
}
},
ready: function() {
// All available commits, populated via mailbox.
this._commits = [];
sk.Mailbox.subscribe('commits', function(commits) {
this._commits = commits;
this._hashChange();
}.bind(this));
},
asString: function() {
if (!this.commit) {
return "Commit not found.";
}
return this._trimPad(this.commit.author) + " " +
this._humanize(this.commit.commit_time) + " " +
this._trunc(this.commit.hash) + " " +
this._trimNewline(this.commit.message);
},
// Call this if the underlying commit has changed, i.e. the commit.message has been updated.
refresh: function() {
this.notifyPath('commit.message', this.commit.message);
},
_hashChange: function() {
if (!this._commits) {
return;
}
for (var i = this._commits.length - 1; i >= 0; i--) {
var c = this._commits[i];
if (c.hash == this.hash) {
this.commit = c;
break
}
}
},
_trimPad: function(s) {
if (!this.commit) {
return "Unknown hash."
}
s = s.slice(0, 30);
while (s.length < 30) {
s = s + " ";
}
return s;
},
_trimNewline: function(s) {
if (!s) {
return "";
}
return this._trimPad(s.split("\n")[0]);
},
_humanize: function (s) {
if (!this.commit) {
return "";
}
return sk.human.diffDate(s * 1000);
},
_commitHref: function (c, repo) {
if (!c || !repo) {
return '';
}
if (repo.indexOf('github.com') !== -1) {
return repo + '/commit/' + c.hash;
} else {
return repo + '/+show/' + c.hash;
}
},
_trunc: function (s) {
if (!this.commit) {
return "";
}
return s.slice(0, 7);
},
});
</script>