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';
|
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
|
|
|
|
2015-09-17 10:20:35 +02:00
|
|
|
|
2015-11-03 10:39:01 +01:00
|
|
|
let PieceListToolbarFilterWidget = React.createClass({
|
2015-08-04 11:39:33 +02:00
|
|
|
propTypes: {
|
2015-09-16 23:36:21 +02:00
|
|
|
filterParams: React.PropTypes.arrayOf(
|
|
|
|
React.PropTypes.shape({
|
|
|
|
label: React.PropTypes.string,
|
2015-09-17 10:20:35 +02:00
|
|
|
items: React.PropTypes.arrayOf(
|
|
|
|
React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.string,
|
|
|
|
React.PropTypes.shape({
|
|
|
|
key: React.PropTypes.string,
|
|
|
|
label: React.PropTypes.string
|
|
|
|
})
|
|
|
|
])
|
|
|
|
)
|
2015-09-16 23:36:21 +02:00
|
|
|
})
|
|
|
|
).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-11-06 14:10:54 +01:00
|
|
|
const { filterBy } = this.props;
|
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 () => {
|
2015-11-06 14:10:54 +01:00
|
|
|
const 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-11-06 14:10:54 +01:00
|
|
|
const 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-11-06 14:10:54 +01:00
|
|
|
const filterIcon = (
|
2015-08-04 13:28:44 +02:00
|
|
|
<span>
|
2015-10-09 13:14:29 +02:00
|
|
|
<span className="ascribe-icon icon-ascribe-filter" aria-hidden="true"></span>
|
2015-08-04 13:28:44 +02:00
|
|
|
<span style={this.isFilterActive()}>*</span>
|
|
|
|
</span>
|
|
|
|
);
|
2015-06-02 11:40:55 +02:00
|
|
|
|
|
|
|
return (
|
2015-08-04 10:06:02 +02:00
|
|
|
<DropdownButton
|
2015-11-03 10:39:01 +01:00
|
|
|
pullRight={true}
|
2015-08-04 10:06:02 +02:00
|
|
|
title={filterIcon}
|
|
|
|
className="ascribe-piece-list-toolbar-filter-widget">
|
2015-09-17 10:41:23 +02:00
|
|
|
{/* We iterate over filterParams, to receive the label and then for each
|
|
|
|
label also iterate over its items, to get all filterable options */}
|
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) => {
|
2015-09-17 10:41:23 +02:00
|
|
|
|
|
|
|
// As can be seen in the PropTypes, a param can either
|
|
|
|
// be a string or an object of the shape:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// key: <String>,
|
|
|
|
// label: <String>
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This is why we need to distinguish between both here.
|
2015-09-16 23:36:21 +02:00
|
|
|
if(typeof param !== 'string') {
|
|
|
|
label = param.label;
|
|
|
|
param = param.key;
|
|
|
|
} else {
|
|
|
|
param = param;
|
2015-09-17 11:39:45 +02:00
|
|
|
label = param.split('acl_')[1].replace(/_/g, ' ');
|
2015-09-16 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-06 14:10:54 +01:00
|
|
|
export default PieceListToolbarFilterWidget;
|