perkeep/server/perkeepd/ui/blob_detail.js

214 lines
4.8 KiB
JavaScript

/*
Copyright 2014 The Perkeep Authors
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.
*/
goog.provide('cam.BlobDetail');
goog.require('cam.blobref');
goog.require('cam.ServerConnection');
goog.require('goog.Promise');
cam.BlobDetail = React.createClass({
displayName: 'BlobDetail',
BLOBREF_PATTERN_: new RegExp(cam.blobref.PATTERN, 'g'),
propTypes: {
getDetailURL: React.PropTypes.func.isRequired,
meta: React.PropTypes.object.isRequired,
serverConnection: React.PropTypes.instanceOf(cam.ServerConnection).isRequired,
},
getInitialState: function() {
return {
content: null,
metadata: null,
claims: null,
refs: null,
};
},
componentWillReceiveProps: function(nextProps) {
// this.props == nextProps is for the very first load.
if (this.props == nextProps || this.props.meta.blobRef != nextProps.meta.blobRef) {
var sc = this.props.serverConnection;
// TODO(mpl): see if we can get any of the information below
// from the search session, in particular from the resolved meta.
sc.getBlobContents(nextProps.meta.blobRef, this.handleBlobContents_);
sc.permanodeClaims(nextProps.meta.blobRef, this.handleClaims_);
goog.Promise.all([
new goog.Promise(sc.pathsOfSignerTarget.bind(sc, nextProps.meta.blobRef)),
new goog.Promise(sc.search.bind(sc, {
permanode: {
attr: 'camliMember',
value: nextProps.meta.blobRef,
},
}, null))
]).then(this.handleRefs_);
}
},
componentWillMount: function() {
this.componentWillReceiveProps(this.props, true);
},
render: function() {
return React.DOM.div(
{
style: {
fontFamily: 'Open Sans',
margin: '1.5em 2em',
}
},
this.getSection_("Blob content", this.state.content),
this.getSection_("Indexer metadata", this.props.meta),
this.getSection_("Mutation claims", this.state.claims),
this.getReferencesSection_(this.state.refs)
);
},
getReferencesSection_: function(refs) {
if (!refs) {
return this.getReferencesBlock_("Loading...");
}
if (refs.length <= 0) {
return this.getReferencesBlock_("No references");
}
return this.getReferencesBlock_(
React.DOM.ul(
null,
refs.map(function(blobref) {
return React.DOM.li(
{key: blobref},
React.DOM.a(
{
href: this.props.getDetailURL(blobref),
},
blobref
)
);
}, this)
)
);
},
getReferencesBlock_: function(content) {
return React.DOM.div(
{
key: 'References',
},
this.getHeader_("Referenced by"),
content
);
},
getSection_: function(title, content) {
return React.DOM.div(
{
key: title
},
this.getHeader_(title),
this.getCodeBlock_(content)
);
},
getHeader_: function(title) {
return React.DOM.h1(
{
key: 'header',
style: {
fontSize: '1.5em',
}
},
title
);
},
getCodeBlock_: function(stuff) {
return React.DOM.pre(
{
key: 'code-block',
style: {
overflowX: 'auto',
},
},
stuff ? this.linkify_(JSON.stringify(stuff, null, 2)) : "No data"
);
},
linkify_: function(code) {
var result = [];
var match;
var index = 0;
while ((match = this.BLOBREF_PATTERN_.exec(code)) !== null) {
result.push(code.substring(index, match.index));
result.push(React.DOM.a({key: match.index, href: this.props.getDetailURL(match[0]).toString()}, match[0]));
index = match.index + match[0].length;
}
result.push(code.substring(index));
return result;
},
handleBlobContents_: function(data) {
this.setState({content: JSON.parse(data)});
},
handleClaims_: function(data) {
this.setState({claims: data});
},
handleRefs_: function(results) {
var refs = [];
if (results[0].paths) {
refs = refs.concat(results[0].paths.map(function(path) {
return path.baseRef;
}));
}
if (results[1].blobs) {
refs = refs.concat(results[1].blobs.map(function(blob) {
return blob.blob;
}));
}
this.setState({refs: refs});
},
});
cam.BlobDetail.getAspect = function(getDetailURL, serverConnection, blobref, targetSearchSession) {
if(!targetSearchSession) {
return;
}
var m = targetSearchSession.getMeta(blobref);
if (!m) {
return null;
}
return {
fragment: 'blob',
title: 'Blob',
createContent: function(size) {
return React.createElement(cam.BlobDetail, {
getDetailURL: getDetailURL,
meta: m,
serverConnection: serverConnection,
});
},
};
};