2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-27 13:57:11 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2015-06-04 15:38:15 +02:00
|
|
|
import { mergeOptions } from '../../utils/general_utils';
|
2015-06-04 15:30:21 +02:00
|
|
|
|
2015-05-27 13:57:11 +02:00
|
|
|
import EditionListStore from '../../stores/edition_list_store';
|
2015-06-01 15:12:31 +02:00
|
|
|
import EditionListActions from '../../actions/edition_list_actions';
|
2015-05-27 13:57:11 +02:00
|
|
|
|
2015-06-02 17:32:38 +02:00
|
|
|
import UserStore from '../../stores/user_store';
|
2015-06-05 11:40:49 +02:00
|
|
|
import UserActions from '../../actions/user_actions';
|
2015-06-02 17:32:38 +02:00
|
|
|
|
2015-06-02 09:10:26 +02:00
|
|
|
import PieceListBulkModalSelectedEditionsWidget from './piece_list_bulk_modal_selected_editions_widget';
|
2015-06-03 10:27:11 +02:00
|
|
|
import AclButtonList from '../ascribe_buttons/acl_button_list';
|
|
|
|
|
2015-05-27 14:35:33 +02:00
|
|
|
|
2015-06-02 09:10:26 +02:00
|
|
|
let PieceListBulkModal = React.createClass({
|
2015-06-01 15:12:31 +02:00
|
|
|
propTypes: {
|
|
|
|
className: React.PropTypes.string
|
|
|
|
},
|
|
|
|
|
2015-05-27 13:57:11 +02:00
|
|
|
getInitialState() {
|
2015-06-04 15:38:15 +02:00
|
|
|
return mergeOptions(EditionListStore.getState(), UserStore.getState());
|
2015-05-27 13:57:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
2015-06-02 17:32:38 +02:00
|
|
|
EditionListStore.listen(this.onChange);
|
|
|
|
UserStore.listen(this.onChange);
|
2015-06-04 15:30:21 +02:00
|
|
|
UserActions.fetchCurrentUser();
|
2015-05-27 13:57:11 +02:00
|
|
|
},
|
|
|
|
|
2015-06-04 13:17:59 +02:00
|
|
|
componentWillUnmount() {
|
2015-06-02 17:32:38 +02:00
|
|
|
EditionListStore.unlisten(this.onChange);
|
2015-06-08 14:51:13 +02:00
|
|
|
UserStore.unlisten(this.onChange);
|
2015-05-27 13:57:11 +02:00
|
|
|
},
|
|
|
|
|
2015-06-04 15:30:21 +02:00
|
|
|
fetchSelectedPieceEditionList() {
|
|
|
|
let filteredPieceIdList = Object.keys(this.state.editionList)
|
|
|
|
.filter((pieceId) => {
|
2015-06-08 14:23:19 +02:00
|
|
|
return this.state.editionList[pieceId]
|
2015-06-04 15:30:21 +02:00
|
|
|
.filter((edition) => edition.selected).length > 0;
|
|
|
|
});
|
|
|
|
return filteredPieceIdList;
|
2015-05-27 13:57:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchSelectedEditionList() {
|
|
|
|
let selectedEditionList = [];
|
|
|
|
|
|
|
|
Object
|
|
|
|
.keys(this.state.editionList)
|
2015-06-04 15:30:21 +02:00
|
|
|
.forEach((pieceId) => {
|
|
|
|
let filteredEditionsForPiece = this.state.editionList[pieceId].filter((edition) => edition.selected);
|
2015-05-27 13:57:11 +02:00
|
|
|
selectedEditionList = selectedEditionList.concat(filteredEditionsForPiece);
|
|
|
|
});
|
|
|
|
|
|
|
|
return selectedEditionList;
|
|
|
|
},
|
|
|
|
|
|
|
|
intersectAcls(a, b) {
|
|
|
|
return a.filter((val) => b.indexOf(val) > -1);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAvailableAcls() {
|
|
|
|
let availableAcls = [];
|
|
|
|
let selectedEditionList = this.fetchSelectedEditionList();
|
|
|
|
|
|
|
|
// If no edition has been selected, availableActions is empty
|
|
|
|
// If only one edition has been selected, their actions are available
|
2015-05-27 14:35:33 +02:00
|
|
|
// If more than one editions have been selected, their acl properties are intersected
|
2015-05-27 13:57:11 +02:00
|
|
|
if(selectedEditionList.length >= 1) {
|
|
|
|
availableAcls = selectedEditionList[0].acl;
|
|
|
|
}
|
|
|
|
if(selectedEditionList.length >= 2) {
|
|
|
|
for(let i = 1; i < selectedEditionList.length; i++) {
|
|
|
|
availableAcls = this.intersectAcls(availableAcls, selectedEditionList[i].acl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableAcls;
|
|
|
|
},
|
|
|
|
|
2015-06-01 15:12:31 +02:00
|
|
|
clearAllSelections() {
|
|
|
|
EditionListActions.clearAllEditionSelections();
|
|
|
|
},
|
|
|
|
|
2015-06-04 13:48:07 +02:00
|
|
|
handleSuccess() {
|
2015-06-04 15:30:21 +02:00
|
|
|
this.fetchSelectedPieceEditionList()
|
|
|
|
.forEach((pieceId) => {
|
|
|
|
EditionListActions.fetchEditionList(pieceId, this.state.orderBy, this.state.orderAsc);
|
|
|
|
});
|
|
|
|
EditionListActions.clearAllEditionSelections();
|
2015-06-02 17:32:38 +02:00
|
|
|
},
|
|
|
|
|
2015-05-27 13:57:11 +02:00
|
|
|
render() {
|
2015-05-27 14:35:33 +02:00
|
|
|
let availableAcls = this.getAvailableAcls();
|
2015-06-02 17:32:38 +02:00
|
|
|
let selectedEditions = this.fetchSelectedEditionList();
|
2015-05-27 13:57:11 +02:00
|
|
|
|
2015-06-01 15:12:31 +02:00
|
|
|
if(availableAcls.length > 0) {
|
|
|
|
return (
|
|
|
|
<div className={this.props.className}>
|
|
|
|
<div className="row no-margin">
|
2015-06-02 09:10:26 +02:00
|
|
|
<div className="col-xs-12 col-sm-12 col-md-12 col-lg-12 piece-list-bulk-modal">
|
2015-06-01 15:12:31 +02:00
|
|
|
<p></p>
|
|
|
|
<div className="row">
|
|
|
|
<div className="text-center">
|
2015-06-02 09:10:26 +02:00
|
|
|
<PieceListBulkModalSelectedEditionsWidget
|
2015-06-01 15:12:31 +02:00
|
|
|
numberOfSelectedEditions={this.fetchSelectedEditionList().length} />
|
|
|
|
|
2015-06-05 11:06:36 +02:00
|
|
|
<span
|
2015-06-02 09:10:26 +02:00
|
|
|
className="piece-list-bulk-modal-clear-all"
|
2015-06-01 15:12:31 +02:00
|
|
|
onClick={this.clearAllSelections}>clear all</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<p></p>
|
|
|
|
<div className="row">
|
2015-06-03 10:27:11 +02:00
|
|
|
<AclButtonList
|
|
|
|
availableAcls={availableAcls}
|
|
|
|
editions={selectedEditions}
|
2015-06-08 14:01:47 +02:00
|
|
|
handleSuccess={this.handleSuccess}
|
|
|
|
className="text-center"/>
|
2015-06-01 15:12:31 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2015-05-27 13:57:11 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2015-06-01 15:12:31 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-06-01 15:12:31 +02:00
|
|
|
|
2015-05-27 13:57:11 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-02 09:10:26 +02:00
|
|
|
export default PieceListBulkModal;
|