import "./sequence";
import React, { createRef } from "react";
import _ from "underscore";
/**
* Takes sequence accession as props, fetches the sequence from the server, and
* displays it in a modal.
*/
export default class SequenceModal extends React.Component {
constructor(props) {
super(props);
this.state = {
error_msgs: [],
sequences: [],
requestCompleted: false,
};
this.modalRef = createRef();
}
// Lifecycle methods. //
render() {
return (
View sequence
{(this.state.requestCompleted && this.resultsJSX()) ||
this.loadingJSX()}
);
}
/*
* Returns jQuery reference to the main modal container.
*/
modal() {
return $(this.modalRef.current);
}
/**
* Shows sequence viewer.
*/
show(url) {
this.setState({ requestCompleted: false }, () => {
this.modal().modal("show");
this.loadJSON(url);
});
}
/**
* Hide sequence viewer.
*/
hide() {
this.modal().modal("hide");
}
/**
* Loads sequence using AJAX and updates modal state.
*/
loadJSON(url) {
// Fetch sequence and update state.
$.getJSON(url)
.done(
_.bind(function (response) {
this.setState({
sequences: response.sequences,
error_msgs: response.error_msgs,
requestCompleted: true,
});
}, this)
)
.fail((jqXHR, status, error) => {
this.hide();
this.props.showErrorModal(jqXHR.responseJSON);
});
}
resultsJSX() {
return (
{_.map(
this.state.error_msgs,
_.bind(function (error_msg) {
return (
);
}, this)
)}
{_.map(
this.state.sequences,
_.bind(function (sequence) {
return
;
}, this)
)}
);
}
loadingJSX() {
return (
);
}
}
class SequenceViewer extends React.Component {
/**
* The CSS class name that will be assigned to the widget container. ID
* assigned to the widget container is derived from the same.
*/
static widgetClass() {
return "biojs-vis-sequence";
}
render() {
this.widgetID = this.widgetClass + "-" + new Date().getUTCMilliseconds();
return (
{this.props.sequence.id}
{this.props.sequence.title}
);
}
componentDidMount() {
// attach BioJS sequence viewer
var widget = new Sequence({
sequence: this.props.sequence.value,
target: this.widgetID,
format: "PRIDE",
columns: {
size: 40,
spacedEach: 0,
},
formatOptions: {
title: false,
footer: false,
},
});
widget.hideFormatSelector();
}
}