2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-02 11:40:55 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2015-08-04 11:39:33 +02:00
|
|
|
import PieceListStore from '../../stores/piece_list_store';
|
|
|
|
import PieceListActions from '../../actions/piece_list_actions';
|
|
|
|
|
2015-06-02 11:40:55 +02:00
|
|
|
import Glyphicon from 'react-bootstrap/lib/Glyphicon';
|
|
|
|
import DropdownButton from 'react-bootstrap/lib/DropdownButton';
|
|
|
|
import MenuItem from 'react-bootstrap/lib/MenuItem';
|
2015-08-04 11:39:33 +02:00
|
|
|
|
2015-08-04 10:06:02 +02:00
|
|
|
import { getLangText } from '../../utils/lang_utils.js';
|
2015-06-02 11:40:55 +02:00
|
|
|
|
|
|
|
let PieceListToolbarFilterWidgetFilter = React.createClass({
|
2015-08-04 11:39:33 +02:00
|
|
|
propTypes: {
|
|
|
|
filterParams: React.PropTypes.arrayOf(React.PropTypes.string).isRequired
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return PieceListStore.getState();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
PieceListStore.listen(this.onChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
PieceListStore.unlisten(this.onChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
|
|
|
generateFilterByStatement(param) {
|
|
|
|
let filterBy = this.state.filterBy;
|
|
|
|
|
|
|
|
if(filterBy) {
|
|
|
|
// we need hasOwnProperty since the values are all booleans
|
|
|
|
if(filterBy.hasOwnProperty(param)) {
|
|
|
|
filterBy[param] = !filterBy[param];
|
|
|
|
|
|
|
|
// if the parameter is false, then we want to remove it again
|
|
|
|
// from the list of queryParameters as this component is only about
|
|
|
|
// which actions *CAN* be done and not what *CANNOT*
|
|
|
|
if(!filterBy[param]) {
|
|
|
|
delete filterBy[param];
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
filterBy[param] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filterBy;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need overloading here to find the correct parameter of the label
|
|
|
|
* the user is clicking on.
|
|
|
|
*/
|
|
|
|
filterBy(param) {
|
|
|
|
return () => {
|
|
|
|
let filterBy = this.generateFilterByStatement(param);
|
|
|
|
PieceListActions.fetchPieceList(this.state.page, this.state.pageSize, this.state.search,
|
|
|
|
this.state.orderBy, this.state.orderAsc, filterBy);
|
|
|
|
};
|
|
|
|
},
|
2015-08-04 10:06:02 +02:00
|
|
|
|
2015-06-02 11:40:55 +02:00
|
|
|
render() {
|
2015-07-01 19:05:47 +02:00
|
|
|
let filterIcon = <Glyphicon glyph='filter' className="filter-glyph"/>;
|
2015-06-02 11:40:55 +02:00
|
|
|
|
|
|
|
return (
|
2015-08-04 10:06:02 +02:00
|
|
|
<DropdownButton
|
|
|
|
title={filterIcon}
|
|
|
|
className="ascribe-piece-list-toolbar-filter-widget">
|
2015-06-02 11:40:55 +02:00
|
|
|
<li style={{'textAlign': 'center'}}>
|
2015-08-04 11:39:33 +02:00
|
|
|
<em>{getLangText('Show works that')}:</em>
|
2015-06-02 11:40:55 +02:00
|
|
|
</li>
|
2015-08-04 11:39:33 +02:00
|
|
|
{this.props.filterParams.map((param, i) => {
|
|
|
|
let name = param.split('_')[1];
|
|
|
|
return (
|
|
|
|
<MenuItem
|
|
|
|
key={i}
|
|
|
|
style={{'textAlign': 'center'}}>
|
|
|
|
<div className="checkbox-line">
|
|
|
|
<span onClick={this.filterBy(param)}>
|
|
|
|
{getLangText('I can') + ' ' + getLangText(name)}
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.filterBy[param]} />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
})}
|
2015-06-02 11:40:55 +02:00
|
|
|
</DropdownButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default PieceListToolbarFilterWidgetFilter;
|