366 lines
12 KiB
TypeScript
366 lines
12 KiB
TypeScript
import React, { Component, ChangeEvent } from 'react';
|
|
import { RouteComponentProps, StaticContext } from 'react-router';
|
|
import { Link } from 'react-router-dom';
|
|
import qs from 'query-string';
|
|
|
|
import styles from './DatabaseList.module.css';
|
|
|
|
import treeJson from '../assets/tree.json';
|
|
import iconFolder from '../assets/images/database/icon_folder.gif';
|
|
|
|
interface ITreeJson {
|
|
id: number;
|
|
title: string;
|
|
num_of_items: number;
|
|
children: ITreeJson[];
|
|
}
|
|
|
|
interface IIndexBase {
|
|
id: number;
|
|
title: string;
|
|
}
|
|
|
|
interface IIndex extends IIndexBase {
|
|
num_of_indexes: number;
|
|
num_of_items: number;
|
|
}
|
|
|
|
interface IIndexDetail extends IIndex {
|
|
path: IIndexBase[];
|
|
children: IIndex[];
|
|
}
|
|
|
|
interface IProps extends RouteComponentProps<{ id: string }, StaticContext, any> { }
|
|
|
|
interface IState {
|
|
index: IIndexDetail;
|
|
orderby: string;
|
|
order_dir: number;
|
|
page: number;
|
|
itemcount: number;
|
|
itemElement: JSX.Element[];
|
|
}
|
|
|
|
const PUBCIC_INDEX_ID = 3;
|
|
|
|
const getIndexUrl = (id: number): string => {
|
|
return '/database/list/' + String(id);
|
|
}
|
|
|
|
const IndexNode = (index: IIndex): JSX.Element => {
|
|
const url = getIndexUrl(index.id);
|
|
return (
|
|
<table>
|
|
<tbody>
|
|
<tr>
|
|
<td className={styles.listIcon}>
|
|
<Link to={url}><img alt={index.title} src={iconFolder} /></Link>
|
|
</td>
|
|
<td>
|
|
<Link to={url}>{index.title}</Link>
|
|
<br />
|
|
{index.num_of_indexes} indexes / {index.num_of_items} items
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
);
|
|
};
|
|
|
|
class DatabaseList extends Component<IProps, IState> {
|
|
|
|
public state: IState = {
|
|
index: {
|
|
id: 0,
|
|
title: '',
|
|
num_of_indexes: 0,
|
|
num_of_items: 0,
|
|
path: [],
|
|
children: []
|
|
},
|
|
orderby: 'title',
|
|
order_dir: 0,
|
|
itemcount: 20,
|
|
page: 1,
|
|
itemElement: []
|
|
};
|
|
|
|
constructor(props: IProps) {
|
|
super(props);
|
|
const { params } = this.props.match;
|
|
const query = qs.parse(this.props.location.search);
|
|
const indexId = params.id ? parseInt(params.id, 10) : PUBCIC_INDEX_ID;
|
|
const indexInfo = this.getIndexInfo(indexId);
|
|
this.state.index = indexInfo;
|
|
if (typeof query.orderby === 'string') {
|
|
this.state.orderby = query.orderby;
|
|
}
|
|
if (typeof query.order_dir === 'string') {
|
|
this.state.order_dir = parseInt(query.order_dir, 10);
|
|
}
|
|
if (typeof query.itemcount === 'string') {
|
|
this.state.itemcount = parseInt(query.itemcount, 10);
|
|
}
|
|
if (typeof query.page === 'string') {
|
|
this.state.page = parseInt(query.page, 10);
|
|
}
|
|
|
|
this.handleSelectOrderby = this.handleSelectOrderby.bind(this);
|
|
this.handleSelectItemcount = this.handleSelectItemcount.bind(this);
|
|
}
|
|
|
|
getIndexId(): number {
|
|
return this.state.index.id;
|
|
}
|
|
|
|
componentWillReceiveProps(nextProps: IProps) {
|
|
const { params } = nextProps.match;
|
|
const query = qs.parse(nextProps.location.search);
|
|
const indexId = params.id ? parseInt(params.id, 10) : PUBCIC_INDEX_ID;
|
|
const index = this.getIndexInfo(indexId);
|
|
if (typeof query.orderby === 'string') {
|
|
this.setState({ orderby: query.orderby });
|
|
}
|
|
if (typeof query.order_dir === 'string') {
|
|
this.setState({ order_dir: parseInt(query.order_dir, 10) });
|
|
}
|
|
if (typeof query.itemcount === 'string') {
|
|
this.setState({ itemcount: parseInt(query.itemcount, 10) });
|
|
}
|
|
this.setState({ index });
|
|
}
|
|
|
|
getLinkUrl(query: { orderby?: string, order_dir?: number, itemcount?: number, page?: number }): string {
|
|
const newOrderby = query.orderby ? query.orderby : this.state.orderby;
|
|
const newOrderDir = query.order_dir ? query.order_dir : this.state.order_dir;
|
|
const newItemcount = query.itemcount ? query.itemcount : this.state.itemcount;
|
|
const newPage = query.page ? query.page : this.state.page;
|
|
const url = getIndexUrl(this.getIndexId());
|
|
let params: string[] = [];
|
|
if ('title' !== newOrderby) {
|
|
params.push('orderby=' + newOrderby);
|
|
}
|
|
if (0 !== newOrderDir) {
|
|
params.push('order_dir=' + String(newOrderDir));
|
|
}
|
|
if (20 !== newItemcount) {
|
|
params.push('itemcount=' + String(newItemcount));
|
|
}
|
|
if (1 !== newPage) {
|
|
params.push('page=' + String(newPage));
|
|
}
|
|
return (params.length > 0 ? url + '?' + params.join('&') : url);
|
|
}
|
|
|
|
handleSelectOrderby(event: ChangeEvent<HTMLSelectElement>) {
|
|
const target = event.target;
|
|
const value = target.value;
|
|
const url = this.getLinkUrl({ orderby: value });
|
|
this.props.history.push(url);
|
|
}
|
|
|
|
handleSelectItemcount(event: ChangeEvent<HTMLSelectElement>) {
|
|
const target = event.target;
|
|
const value = parseInt(target.value, 10);
|
|
const url = this.getLinkUrl({ itemcount: value });
|
|
this.props.history.push(url);
|
|
}
|
|
|
|
getIndexInfo(indexId: number): IIndexDetail {
|
|
let index: IIndexDetail = {
|
|
id: indexId,
|
|
title: '',
|
|
num_of_indexes: 0,
|
|
num_of_items: 0,
|
|
path: [],
|
|
children: []
|
|
};
|
|
let found: boolean = false;
|
|
const loop = (data: ITreeJson[]) => {
|
|
data.forEach((item: ITreeJson) => {
|
|
if (found) {
|
|
return;
|
|
}
|
|
const path: IIndexBase = {
|
|
id: item.id,
|
|
title: item.title,
|
|
}
|
|
index.path.push(path);
|
|
if (indexId === item.id) {
|
|
index.id = item.id;
|
|
index.title = item.title;
|
|
index.num_of_indexes = item.children.length;
|
|
index.num_of_items = item.num_of_items;
|
|
found = true;
|
|
item.children.forEach((item: ITreeJson) => {
|
|
index.children.push({
|
|
id: item.id,
|
|
title: item.title,
|
|
num_of_indexes: item.children.length,
|
|
num_of_items: item.num_of_items
|
|
});
|
|
});
|
|
} else {
|
|
loop(item.children);
|
|
}
|
|
if (found === false) {
|
|
index.path.pop();
|
|
}
|
|
});
|
|
};
|
|
loop(treeJson);
|
|
return index;
|
|
}
|
|
|
|
renderIndexPath(): JSX.Element {
|
|
let elements: JSX.Element[] = [];
|
|
this.state.index.path.forEach((path: IIndexBase) => {
|
|
const url: string = getIndexUrl(path.id);
|
|
elements.push(<span key={path.id}>/ <Link to={url}>{path.title}</Link> </span>);
|
|
});
|
|
return <div>{elements}</div>;
|
|
}
|
|
|
|
renderIndexNodeList(): JSX.Element {
|
|
if (this.state.index.children.length === 0) {
|
|
return (<></>);
|
|
}
|
|
let evenodd: string = 'even';
|
|
const treeList = this.state.index.children.map((cindex: IIndex) => {
|
|
evenodd = evenodd === 'even' ? 'odd' : 'even';
|
|
return (
|
|
<tr key={cindex.id}>
|
|
<td className={evenodd}>{IndexNode(cindex)}</td>
|
|
</tr >
|
|
);
|
|
});
|
|
return (
|
|
<table className={styles.listTable}>
|
|
<tbody>
|
|
{treeList}
|
|
</tbody>
|
|
</table>
|
|
);
|
|
}
|
|
|
|
renderSortCriteria(): JSX.Element {
|
|
if (this.state.index.num_of_items === 0) {
|
|
return (<></>);
|
|
}
|
|
let orderDir = <></>;
|
|
if (this.state.order_dir === 1) {
|
|
const url = this.getLinkUrl({ order_dir: 0 });
|
|
orderDir = (<span>
|
|
▼ <Link to={url}>▲</Link>
|
|
</span>);
|
|
} else {
|
|
const url = this.getLinkUrl({ order_dir: 1 });
|
|
orderDir = (<span>
|
|
<Link to={url}>▼</Link> ▲
|
|
</span>);
|
|
}
|
|
return (
|
|
<div className={styles.sortCriteria}>
|
|
<table className={styles.listTable}>
|
|
<tbody>
|
|
<tr>
|
|
<td className="odd">
|
|
Order by
|
|
<select value={this.state.orderby} onChange={this.handleSelectOrderby}>
|
|
<option value="title">Title</option>
|
|
<option value="doi">ID</option>
|
|
<option value="last_update_date">Last Modified Date</option>
|
|
<option value="creation_date">Created Date</option>
|
|
<option value="publication_date">Date</option>
|
|
</select>
|
|
{orderDir}
|
|
</td>
|
|
<td className="odd">
|
|
No.Item per page
|
|
<select value={this.state.itemcount} onChange={this.handleSelectItemcount}>
|
|
<option value="20">20</option>
|
|
<option value="50">50</option>
|
|
<option value="100">100</option>
|
|
</select>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
reunderNumOfItems(): JSX.Element {
|
|
if (this.state.index.num_of_items === 0) {
|
|
return (<p>no items found</p>);
|
|
}
|
|
let startNum: number = 1 + this.state.itemcount * (this.state.page - 1);
|
|
let endNum: number = this.state.itemcount * this.state.page;
|
|
if (startNum > this.state.index.num_of_items) {
|
|
return (<p>out of range</p>);
|
|
}
|
|
if (endNum > this.state.index.num_of_items) {
|
|
endNum = this.state.index.num_of_items;
|
|
}
|
|
return (
|
|
<p>{startNum} - {endNum} of {this.state.index.num_of_items}</p>
|
|
);
|
|
}
|
|
|
|
reunderPageNavi(): JSX.Element {
|
|
if (this.state.index.num_of_items === 0) {
|
|
return (<></>);
|
|
}
|
|
const maxPage = (this.state.index.num_of_items / this.state.itemcount) + 1;
|
|
const link = (title: string, page: number) => {
|
|
if (page === 0 || page === this.state.page) {
|
|
return (<span className={styles.pageNaviItem}>{title}</span>);
|
|
}
|
|
const url: string = this.getLinkUrl({ page: page });
|
|
return (<Link className={styles.pageNaviItem} to={url}>{title}</Link>);
|
|
}
|
|
const startPage = (this.state.page - 5) > 1 ? this.state.page - 5 : 1;
|
|
const endPage = (startPage + 5) > maxPage ? maxPage : startPage + 5;
|
|
let pageLinks: JSX.Element[] = [];
|
|
pageLinks.push(link('PREV', this.state.page - 1));
|
|
for (let i = startPage; i <= endPage; i++) {
|
|
pageLinks.push(link(String(i), i));
|
|
}
|
|
pageLinks.push(link('NEXT', this.state.page === maxPage ? 0 : this.state.page + 1));
|
|
return (
|
|
<div className={styles.pageNavi}>
|
|
{pageLinks}
|
|
</div>
|
|
);
|
|
}
|
|
render() {
|
|
const pageNavi = this.reunderPageNavi();
|
|
return (
|
|
<div className={styles.list}>
|
|
<h3>Listing item</h3>
|
|
{this.renderIndexPath()}
|
|
{this.renderIndexNodeList()}
|
|
{this.renderSortCriteria()}
|
|
{this.reunderNumOfItems()}
|
|
{pageNavi}
|
|
<div>BODY</div>
|
|
{pageNavi}
|
|
</div>
|
|
);
|
|
/*
|
|
|
|
<table width="100%" cellspacing="5" border="0">
|
|
<tbody><tr valign="top">
|
|
<td class="even">
|
|
|
|
</td>
|
|
</tr>
|
|
</tbody></table>
|
|
|
|
|
|
*/
|
|
}
|
|
}
|
|
|
|
export default DatabaseList;
|