2015-07-09 14:04:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
2015-07-13 10:07:22 +02:00
|
|
|
import classNames from 'classnames';
|
2015-07-09 14:04:48 +02:00
|
|
|
|
|
|
|
import EditionListActions from '../../actions/edition_list_actions';
|
|
|
|
import EditionListStore from '../../stores/edition_list_store';
|
|
|
|
|
2015-07-16 12:11:05 +02:00
|
|
|
import Button from 'react-bootstrap/lib/Button';
|
|
|
|
|
2015-07-13 18:52:55 +02:00
|
|
|
import CreateEditionsButton from '../ascribe_buttons/create_editions_button';
|
|
|
|
|
2015-07-09 14:04:48 +02:00
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
|
|
|
|
|
|
|
let AccordionListItemEditionWidget = React.createClass({
|
|
|
|
propTypes: {
|
2015-07-13 10:07:22 +02:00
|
|
|
className: React.PropTypes.string,
|
2015-07-10 13:54:25 +02:00
|
|
|
piece: React.PropTypes.object.isRequired,
|
2015-07-13 18:52:55 +02:00
|
|
|
toggleCreateEditionsDialog: React.PropTypes.func.isRequired,
|
|
|
|
onPollingSuccess: React.PropTypes.func
|
2015-07-09 14:04:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return EditionListStore.getState();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
EditionListStore.listen(this.onChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
EditionListStore.unlisten(this.onChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
2015-07-09 15:44:20 +02:00
|
|
|
/**
|
|
|
|
* Calls the store to either show or hide the editionListTable
|
|
|
|
*/
|
2015-07-09 14:04:48 +02:00
|
|
|
toggleTable() {
|
|
|
|
let pieceId = this.props.piece.id;
|
|
|
|
let isEditionListOpen = this.state.isEditionListOpenForPieceId[pieceId] ? this.state.isEditionListOpenForPieceId[pieceId].show : false;
|
|
|
|
|
|
|
|
if(isEditionListOpen) {
|
|
|
|
EditionListActions.toggleEditionList(pieceId);
|
|
|
|
} else {
|
|
|
|
EditionListActions.toggleEditionList(pieceId);
|
|
|
|
EditionListActions.fetchEditionList(pieceId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-07-09 15:44:20 +02:00
|
|
|
/**
|
|
|
|
* Depending on the state of isEditionListOpenForPieceId we either want to display
|
|
|
|
* a glyphicon arrow pointing upwards or downwards
|
|
|
|
*/
|
2015-07-09 14:04:48 +02:00
|
|
|
getGlyphicon() {
|
|
|
|
let pieceId = this.props.piece.id;
|
|
|
|
let isEditionListOpen = this.state.isEditionListOpenForPieceId[pieceId] ? this.state.isEditionListOpenForPieceId[pieceId].show : false;
|
|
|
|
|
|
|
|
if(isEditionListOpen) {
|
2015-07-15 17:32:51 +02:00
|
|
|
// this is the loading feedback for the editions
|
|
|
|
// button.
|
2015-07-16 11:39:05 +02:00
|
|
|
//
|
2015-07-15 17:32:51 +02:00
|
|
|
// PLEASE FUTURE TIM, DO NOT FUCKING REMOVE IT AGAIN!
|
|
|
|
if(typeof this.state.editionList[pieceId] === 'undefined') {
|
|
|
|
return (
|
|
|
|
<span className="glyph-ascribe-spool-chunked ascribe-color spin"/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<span className="glyphicon glyphicon-menu-up" aria-hidden="true" style={{top: 2}}></span>
|
|
|
|
);
|
|
|
|
}
|
2015-07-09 14:04:48 +02:00
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<span className="glyphicon glyphicon-menu-down" aria-hidden="true" style={{top: 2}}></span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
2015-07-09 15:44:20 +02:00
|
|
|
let piece = this.props.piece;
|
|
|
|
let numEditions = piece.num_editions;
|
|
|
|
|
2015-07-13 18:52:55 +02:00
|
|
|
if(numEditions <= 0) {
|
2015-07-13 23:57:16 +02:00
|
|
|
if (piece.acl.acl_editions){
|
|
|
|
return (
|
|
|
|
<CreateEditionsButton
|
|
|
|
label={getLangText('Create editions')}
|
|
|
|
className="btn-xs pull-right"
|
|
|
|
piece={piece}
|
|
|
|
toggleCreateEditionsDialog={this.props.toggleCreateEditionsDialog}
|
|
|
|
onPollingSuccess={this.props.onPollingSuccess}/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-07-16 12:11:05 +02:00
|
|
|
if(piece.first_edition === null) {
|
|
|
|
// user has deleted all his editions and only the piece is showing
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
disabled
|
|
|
|
title={getLangText('All editions for this have been deleted already.')}
|
|
|
|
className={classNames('btn', 'btn-default', 'btn-xs', 'ascribe-accordion-list-item-edition-widget', this.props.className)}>
|
|
|
|
{'0 ' + getLangText('Editions')}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let editionMapping = piece && piece.first_edition ? piece.first_edition.num_editions_available + '/' + piece.num_editions : '';
|
2015-07-09 15:44:20 +02:00
|
|
|
|
2015-07-16 12:11:05 +02:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
onClick={this.toggleTable}
|
|
|
|
className={classNames('btn', 'btn-default', 'btn-xs', 'ascribe-accordion-list-item-edition-widget', this.props.className)}>
|
|
|
|
{editionMapping + ' ' + getLangText('Editions')} {this.getGlyphicon()}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
2015-07-09 15:44:20 +02:00
|
|
|
}
|
2015-07-09 14:04:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default AccordionListItemEditionWidget;
|