1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-29 00:58:03 +02:00
onion/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js

222 lines
7.5 KiB
JavaScript
Raw Normal View History

'use strict';
import React from 'react';
import EditionListStore from '../../stores/edition_list_store';
import EditionListActions from '../../actions/edition_list_actions';
import AccordionListItemTable from './accordion_list_item_table';
import AccordionListItemTableToggle from './accordion_list_item_table_toggle';
2015-06-08 14:13:35 +02:00
import AccordionListItemTableSelectAllEditionsCheckbox from './accordion_list_item_table_select_all_editions_checkbox';
import { ColumnModel, TransitionModel } from '../ascribe_table/models/table_models';
import TableItemText from '../ascribe_table/table_item_text';
2015-06-01 14:10:11 +02:00
import TableItemCheckbox from '../ascribe_table/table_item_checkbox';
2015-06-01 13:18:24 +02:00
import TableItemAclFiltered from '../ascribe_table/table_item_acl_filtered';
import AscribeSpinner from '../ascribe_spinner';
import { getLangText } from '../../utils/lang_utils';
2015-07-06 18:29:27 +02:00
import { mergeOptions } from '../../utils/general_utils';
let AccordionListItemTableEditions = React.createClass({
propTypes: {
className: React.PropTypes.string,
parentId: React.PropTypes.number
},
getInitialState() {
2015-07-06 18:29:27 +02:00
return mergeOptions(
EditionListStore.getState(),
{
showMoreLoading: false
}
);
},
componentDidMount() {
EditionListStore.listen(this.onChange);
},
componentWillUnmount() {
EditionListStore.unlisten(this.onChange);
},
onChange(state) {
2015-07-06 18:29:27 +02:00
if(this.state.showMoreLoading) {
this.setState({
showMoreLoading: false
});
}
this.setState(state);
},
2015-06-08 14:13:35 +02:00
selectItem(pieceId, editionId, toValue) {
EditionListActions.selectEdition({pieceId, editionId, toValue});
2015-06-01 14:10:11 +02:00
},
2015-06-08 14:13:35 +02:00
toggleAllItems(checked) {
this.state.editionList[this.props.parentId]
.forEach((edition) => {
2015-06-08 14:13:35 +02:00
this.selectItem(this.props.parentId, edition.id, !checked);
});
},
filterSelectedEditions() {
return this.state
.editionList[this.props.parentId]
.filter((edition) => edition.selected);
},
2015-07-03 16:31:09 +02:00
loadFurtherEditions() {
const { parentId: pieceId } = this.props;
const { page, pageSize, orderBy, orderAsc, filterBy } = this.state.editionList[pieceId];
2015-07-06 18:29:27 +02:00
// trigger loading animation
this.setState({
showMoreLoading: true
});
EditionListActions.fetchEditionList({
pieceId,
pageSize,
orderBy,
orderAsc,
2016-01-18 10:53:01 +01:00
filterBy,
page: page + 1
});
2015-07-03 16:31:09 +02:00
},
2015-06-01 14:10:11 +02:00
render() {
2015-12-18 15:12:29 +01:00
const { className, parentId } = this.props;
const { editionList, isEditionListOpenForPieceId, showMoreLoading } = this.state;
const editionsForPiece = editionList[parentId];
let selectedEditionsCount = 0;
let allEditionsCount = 0;
let orderBy;
let orderAsc;
2015-07-06 18:29:27 +02:00
let show = false;
2015-07-06 15:56:14 +02:00
let showExpandOption = false;
// here we need to check if all editions of a specific
// piece are already defined. Otherwise .length will throw an error and we'll not
// be notified about it.
2015-12-18 15:12:29 +01:00
if (editionsForPiece) {
selectedEditionsCount = this.filterSelectedEditions().length;
2015-07-06 15:56:14 +02:00
allEditionsCount = editionsForPiece.length;
orderBy = editionsForPiece.orderBy;
orderAsc = editionsForPiece.orderAsc;
}
2015-12-18 15:12:29 +01:00
if (parentId in isEditionListOpenForPieceId) {
show = isEditionListOpenForPieceId[parentId].show;
}
2015-07-06 15:56:14 +02:00
// if the number of editions in the array is equal to the maximum number of editions,
// then the "Show me more" dialog should be hidden from the user's view
2015-12-18 15:12:29 +01:00
if (editionsForPiece && editionsForPiece.count > editionsForPiece.length) {
2015-07-06 15:56:14 +02:00
showExpandOption = true;
}
const transition = new TransitionModel({
to: 'editions',
queryKey: 'editionId',
valueKey: 'bitcoin_id',
callback: (e) => e.stopPropagation()
});
2015-12-18 15:12:29 +01:00
const columnList = [
new ColumnModel({
transformFn: (item) => {
2015-06-03 10:45:23 +02:00
return {
'editionId': item.id,
2015-12-18 15:12:29 +01:00
'pieceId': parentId,
2015-06-03 10:45:23 +02:00
'selectItem': this.selectItem,
'selected': item.selected
};
},
displayElement: (
2015-06-08 14:13:35 +02:00
<AccordionListItemTableSelectAllEditionsCheckbox
onChange={this.toggleAllItems}
numOfSelectedEditions={selectedEditionsCount}
numOfAllEditions={allEditionsCount}/>
),
displayType: TableItemCheckbox,
rowWidth: 1
}),
new ColumnModel({
transition,
transformFn: (item) => {
2015-06-03 10:45:23 +02:00
return {
2015-07-03 19:08:56 +02:00
'content': item.edition_number + ' ' + getLangText('of') + ' ' + item.num_editions
};
},
columnName: 'edition_number',
displayElement: getLangText('Edition'),
displayType: TableItemText,
rowWidth: 1
}),
new ColumnModel({
transition,
transformFn: (item) => {
2015-06-03 10:45:23 +02:00
return {
'content': item.bitcoin_id
};
},
columnName: 'bitcoin_id',
displayElement: getLangText('ID'),
displayType: TableItemText,
rowWidth: 5,
className: 'hidden-xs visible-sm visible-md visible-lg'
}),
new ColumnModel({
transition,
transformFn: (item) => {
2015-06-03 10:45:23 +02:00
return {
'content': item.acl,
2015-09-03 17:25:22 +02:00
'notifications': item.notifications
};
},
columnName: 'acl',
displayElement: getLangText('Actions'),
displayType: TableItemAclFiltered,
rowWidth: 4
})
];
2015-12-18 15:12:29 +01:00
if (show && editionsForPiece && editionsForPiece.length) {
2015-07-09 16:09:53 +02:00
return (
2015-12-18 15:12:29 +01:00
<div className={className}>
2015-07-09 16:09:53 +02:00
<AccordionListItemTable
2015-12-18 15:12:29 +01:00
parentId={parentId}
2015-07-09 16:09:53 +02:00
itemList={editionsForPiece}
columnList={columnList}
show={show}
orderBy={orderBy}
orderAsc={orderAsc}
changeOrder={this.changeEditionListOrder}
selectItem={this.selectItem}/>
<AccordionListItemTableToggle
className="ascribe-accordion-list-table-toggle"
onClick={this.loadFurtherEditions}
2015-12-18 15:12:29 +01:00
message={show && showExpandOption ? (
<span>
{showMoreLoading ? <AscribeSpinner size="sm" color="dark-blue" />
: <span className="glyphicon glyphicon-option-horizontal" aria-hidden="true" style={{top: 3}} />}
{getLangText('Show me more')}
</span>
) : null
} />
2015-07-09 16:09:53 +02:00
</div>
);
} else {
return null;
}
}
});
2015-07-03 19:08:56 +02:00
export default AccordionListItemTableEditions;