2015-10-15 11:17:16 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
2015-11-10 11:53:05 +01:00
|
|
|
import { AclInformationText } from '../../constants/acl_information_text';
|
2015-10-20 18:10:48 +02:00
|
|
|
import { replaceSubstringAtIndex, sanitize, intersectLists } from '../../utils/general_utils';
|
2015-10-15 11:17:16 +02:00
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
|
|
|
|
|
|
|
|
|
|
|
let AclInformation = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
verbs: React.PropTypes.arrayOf(React.PropTypes.string),
|
|
|
|
aim: React.PropTypes.string.isRequired,
|
2015-10-23 09:29:17 +02:00
|
|
|
aclObject: React.PropTypes.object,
|
|
|
|
|
|
|
|
// Must be inserted from the outside
|
|
|
|
buttonListSize: React.PropTypes.number.isRequired
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps() {
|
|
|
|
return {
|
|
|
|
buttonListSize: 400
|
|
|
|
};
|
2015-10-15 11:17:16 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
return { isVisible: false };
|
|
|
|
},
|
|
|
|
|
|
|
|
onOff() {
|
|
|
|
if(!this.state.isVisible) {
|
|
|
|
this.setState({ isVisible: true });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setState({ isVisible: false });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getInfoText(title, info, example){
|
2015-11-10 11:53:05 +01:00
|
|
|
const aim = this.props.aim;
|
2015-10-15 11:17:16 +02:00
|
|
|
|
2015-10-20 18:00:59 +02:00
|
|
|
if(aim) {
|
|
|
|
if(aim === 'form') {
|
2015-10-15 11:17:16 +02:00
|
|
|
return (
|
2015-10-20 15:10:18 +02:00
|
|
|
<p>
|
|
|
|
<span className="info">
|
2015-10-23 10:37:23 +02:00
|
|
|
{replaceSubstringAtIndex(info.slice(2), 's ', ' ')}
|
2015-10-20 15:10:18 +02:00
|
|
|
</span>
|
|
|
|
<span className="example">
|
2015-10-23 11:24:26 +02:00
|
|
|
{' ' + example}
|
2015-10-20 15:10:18 +02:00
|
|
|
</span>
|
2015-10-15 11:17:16 +02:00
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
2015-10-20 18:00:59 +02:00
|
|
|
else if(aim === 'button') {
|
2015-10-15 11:17:16 +02:00
|
|
|
return (
|
2015-10-20 15:10:18 +02:00
|
|
|
<p>
|
|
|
|
<span className="title">
|
|
|
|
{title}
|
|
|
|
</span>
|
|
|
|
<span className="info">
|
2015-10-23 10:37:23 +02:00
|
|
|
{info + ' '}
|
2015-10-20 15:10:18 +02:00
|
|
|
</span>
|
|
|
|
<span className="example">
|
|
|
|
{example}
|
|
|
|
</span>
|
2015-10-15 11:17:16 +02:00
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('Aim is required when you want to place information text');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
produceInformationBlock() {
|
2015-11-10 11:53:05 +01:00
|
|
|
const { titles, informationSentences, exampleSentences } = AclInformationText;
|
2015-10-20 15:10:18 +02:00
|
|
|
const { verbs, aim } = this.props;
|
2015-10-15 11:17:16 +02:00
|
|
|
|
2015-10-20 18:10:48 +02:00
|
|
|
const availableInformations = intersectLists(verbs, Object.keys(titles));
|
2015-10-20 18:00:59 +02:00
|
|
|
|
2015-10-15 11:17:16 +02:00
|
|
|
// sorting is not needed, as `this.props.verbs` takes care of sorting already
|
|
|
|
// So we assume a user of `AclInformationButton` puts an ordered version of
|
|
|
|
// `verbs` into `propTypes`
|
|
|
|
let verbsToDisplay = [];
|
2015-10-20 18:00:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
if(aim === 'form' && availableInformations.length > 0) {
|
2015-10-15 11:17:16 +02:00
|
|
|
verbsToDisplay = verbsToDisplay.concat(verbs);
|
|
|
|
} else if(aim === 'button' && this.props.aclObject) {
|
|
|
|
const { aclObject } = this.props;
|
|
|
|
const sanitizedAclObject = sanitize(aclObject, (val) => !val);
|
2015-10-20 18:10:48 +02:00
|
|
|
verbsToDisplay = verbsToDisplay.concat(intersectLists(verbs, Object.keys(sanitizedAclObject)));
|
2015-10-15 11:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return verbsToDisplay.map((verb) => {
|
2015-11-10 11:53:05 +01:00
|
|
|
const title = titles[verb];
|
|
|
|
const informationSentence = informationSentences[verb];
|
|
|
|
const exampleSentence = exampleSentences[verb];
|
|
|
|
|
|
|
|
if (title && informationSentence && exampleSentence) {
|
|
|
|
return this.getInfoText(getLangText(title), getLangText(informationSentence), getLangText(exampleSentence));
|
|
|
|
}
|
2015-10-15 11:17:16 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
getButton() {
|
|
|
|
return this.props.aim === 'button' ?
|
2015-10-20 15:10:18 +02:00
|
|
|
<button
|
|
|
|
style={{ marginTop: 0 }}
|
|
|
|
className="btn btn-transparent glyphicon glyphicon-question-sign" onClick={this.onOff} /> :
|
2015-10-15 11:17:16 +02:00
|
|
|
null;
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
2015-10-23 10:37:23 +02:00
|
|
|
const { aim, buttonListSize, verbs } = this.props;
|
2015-10-20 15:10:18 +02:00
|
|
|
const { isVisible } = this.state;
|
|
|
|
|
2015-10-23 09:29:17 +02:00
|
|
|
/* Lets just fucking get this widget out... */
|
2015-10-23 10:37:23 +02:00
|
|
|
const aclInformationSize = buttonListSize - 30;
|
2015-10-23 09:29:17 +02:00
|
|
|
|
2015-10-15 11:17:16 +02:00
|
|
|
return (
|
2015-10-20 15:10:18 +02:00
|
|
|
<span >
|
2015-10-15 11:17:16 +02:00
|
|
|
{this.getButton()}
|
|
|
|
<div
|
2015-10-23 09:29:17 +02:00
|
|
|
style={{
|
2015-10-23 11:24:26 +02:00
|
|
|
width: verbs.length > 1 && aclInformationSize > 300 ? aclInformationSize : verbs.length === 1 ? null : '100%',
|
2015-10-23 10:37:23 +02:00
|
|
|
marginLeft: verbs.length === 1 ? '.25em' : null
|
2015-10-23 09:29:17 +02:00
|
|
|
}}
|
2015-10-20 15:10:18 +02:00
|
|
|
className={classnames({'acl-information-dropdown-list': true, 'hidden': aim === 'button' && !isVisible})}>
|
2015-10-15 11:17:16 +02:00
|
|
|
<span>{this.produceInformationBlock()}</span>
|
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default AclInformation;
|