2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-02 11:40:55 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
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: {
|
2015-08-04 15:39:20 +02:00
|
|
|
// An array of either strings (which represent acl enums) or objects of the form
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// key: <acl enum>,
|
|
|
|
// label: <a human readable string>
|
|
|
|
// }
|
|
|
|
//
|
2015-09-16 23:36:21 +02:00
|
|
|
filterParams: React.PropTypes.arrayOf(
|
|
|
|
React.PropTypes.shape({
|
|
|
|
label: React.PropTypes.string,
|
|
|
|
items: React.PropTypes.array
|
|
|
|
})
|
|
|
|
).isRequired,
|
2015-08-06 13:15:52 +02:00
|
|
|
filterBy: React.PropTypes.object,
|
|
|
|
applyFilterBy: React.PropTypes.func
|
2015-08-04 11:39:33 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
generateFilterByStatement(param) {
|
2015-08-06 13:15:52 +02:00
|
|
|
let filterBy = this.props.filterBy;
|
2015-08-04 11:39:33 +02:00
|
|
|
|
|
|
|
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);
|
2015-08-06 13:15:52 +02:00
|
|
|
this.props.applyFilterBy(filterBy);
|
2015-08-04 11:39:33 +02:00
|
|
|
};
|
|
|
|
},
|
2015-08-04 10:06:02 +02:00
|
|
|
|
2015-08-04 13:28:44 +02:00
|
|
|
isFilterActive() {
|
2015-08-06 13:15:52 +02:00
|
|
|
let trueValuesOnly = Object.keys(this.props.filterBy).filter((acl) => acl);
|
2015-08-04 13:28:44 +02:00
|
|
|
|
|
|
|
// We're hiding the star in that complicated matter so that,
|
|
|
|
// the surrounding button is not resized up on appearance
|
|
|
|
if(trueValuesOnly.length > 0) {
|
|
|
|
return { visibility: 'visible'};
|
|
|
|
} else {
|
|
|
|
return { visibility: 'hidden' };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-02 11:40:55 +02:00
|
|
|
render() {
|
2015-08-04 13:28:44 +02:00
|
|
|
let filterIcon = (
|
|
|
|
<span>
|
|
|
|
<span className="glyphicon glyphicon-filter" aria-hidden="true"></span>
|
|
|
|
<span style={this.isFilterActive()}>*</span>
|
|
|
|
</span>
|
|
|
|
);
|
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-09-16 23:36:21 +02:00
|
|
|
{this.props.filterParams.map(({ label, items }, i) => {
|
2015-08-04 11:39:33 +02:00
|
|
|
return (
|
2015-09-16 23:36:21 +02:00
|
|
|
<div>
|
|
|
|
<li
|
|
|
|
style={{'textAlign': 'center'}}
|
|
|
|
key={i}>
|
|
|
|
<em>{label}:</em>
|
|
|
|
</li>
|
|
|
|
{items.map((param, j) => {
|
|
|
|
let label;
|
|
|
|
|
|
|
|
if(typeof param !== 'string') {
|
|
|
|
label = param.label;
|
|
|
|
param = param.key;
|
|
|
|
} else {
|
|
|
|
param = param;
|
|
|
|
label = param.split('_')[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<li
|
|
|
|
key={j}
|
|
|
|
onClick={this.filterBy(param)}
|
|
|
|
className="filter-widget-item">
|
|
|
|
<div className="checkbox-line">
|
|
|
|
<span>
|
|
|
|
{getLangText(label)}
|
|
|
|
</span>
|
|
|
|
<input
|
|
|
|
readOnly
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.props.filterBy[param]} />
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2015-08-04 11:39:33 +02:00
|
|
|
);
|
|
|
|
})}
|
2015-06-02 11:40:55 +02:00
|
|
|
</DropdownButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default PieceListToolbarFilterWidgetFilter;
|