-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEntityListItemMainTaxonomies.js
More file actions
152 lines (139 loc) · 5.15 KB
/
EntityListItemMainTaxonomies.js
File metadata and controls
152 lines (139 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React from 'react';
import PropTypes from 'prop-types';
import { Map } from 'immutable';
import { injectIntl } from 'react-intl';
import styled from 'styled-components';
import { palette } from 'styled-theme';
import appMessages from 'containers/App/messages';
import ButtonTagCategory from 'components/buttons/ButtonTagCategory';
import ButtonTagCategoryInverse from 'components/buttons/ButtonTagCategoryInverse';
import { truncateText } from 'utils/string';
import { TEXT_TRUNCATE } from 'themes/config';
const SmartGroup = styled.div`
display: inline-block;
margin-right: 0.5em;
padding-right: 0.5em;
border-right: ${(props) => props.border ? '1px solid' : 'none'};
border-right-color: ${palette('light', 3)};
`;
const Styled = styled.div`
width: 100%;
display: block;
padding-top: 3px;
margin-top: 8px;
@media print {
padding-top: 2px;
margin-top: 3px;
}
`;
// border-top-color:;
class EntityListItemMainTaxonomies extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
getEntityTags = (categories, taxonomies, onClick) => {
const tags = [];
if (categories) {
taxonomies
.filter((tax) => !tax.getIn(['attributes', 'is_smart']))
.sortBy((tax) => tax.getIn(['attributes', 'priority']))
.forEach((tax) => {
tax
.get('categories')
.sortBy((category) => category.getIn(['attributes', 'draft']))
.forEach((category, catId) => {
if (categories.includes(parseInt(catId, 10))) {
const label = (category.getIn(['attributes', 'short_title']) && category.getIn(['attributes', 'short_title']).trim().length > 0
? category.getIn(['attributes', 'short_title'])
: category.getIn(['attributes', 'title']));
tags.push({
taxId: tax.get('id'),
title: category.getIn(['attributes', 'title']),
inverse: category.getIn(['attributes', 'draft']),
label: truncateText(label, TEXT_TRUNCATE.ENTITY_TAG, categories.size < 5),
onClick: () => onClick(catId, 'category'),
});
}
});
});
}
return tags;
};
getSmartTitle = (title, isSmart) => {
const { intl } = this.props;
return intl
? `${title}: ${intl.formatMessage(isSmart ? appMessages.labels.smart.met : appMessages.labels.smart.notMet)}`
: title;
};
getEntitySmartTags = (categories, smartTaxonomy, onClick) => {
const tags = [];
smartTaxonomy
.get('categories')
.forEach((category, catId) => {
const label = (category.getIn(['attributes', 'short_title']) && category.getIn(['attributes', 'short_title']).trim().length > 0
? category.getIn(['attributes', 'short_title'])
: category.getIn(['attributes', 'title']));
const isSmart = categories && categories.includes(parseInt(catId, 10));
tags.push({
taxId: smartTaxonomy.get('id'),
title: this.getSmartTitle(category.getIn(['attributes', 'title']), isSmart),
isSmart,
label: truncateText(label, TEXT_TRUNCATE.ENTITY_TAG, false),
onClick: () => onClick(catId, 'category'),
});
});
return tags;
};
render() {
const { categories, taxonomies, onEntityClick } = this.props;
const smartTaxonomy = taxonomies && taxonomies.find((tax) => tax.getIn(['attributes', 'is_smart']));
const entityTags = categories && this.getEntityTags(categories, taxonomies, onEntityClick);
return (
<Styled>
{smartTaxonomy && (
<SmartGroup border={entityTags && entityTags.length > 0}>
{ this.getEntitySmartTags(categories, smartTaxonomy, onEntityClick).map((tag, i) => (
<ButtonTagCategory
key={i}
onClick={tag.onClick}
taxId={parseInt(tag.taxId, 10)}
title={tag.title}
isSmartTag
isSmart={tag.isSmart}
>
{tag.label}
</ButtonTagCategory>
))}
</SmartGroup>
)}
{entityTags && entityTags.map((tag, i) => tag.inverse
? (
<ButtonTagCategoryInverse
key={i}
onClick={tag.onClick}
taxId={parseInt(tag.taxId, 10)}
disabled={!tag.onClick}
title={tag.title}
>
{tag.label}
</ButtonTagCategoryInverse>
)
: (
<ButtonTagCategory
key={i}
onClick={tag.onClick}
taxId={parseInt(tag.taxId, 10)}
disabled={!tag.onClick}
title={tag.title}
>
{tag.label}
</ButtonTagCategory>
))}
</Styled>
);
}
}
EntityListItemMainTaxonomies.propTypes = {
categories: PropTypes.instanceOf(Map), // eslint-disable-line react/no-unused-prop-types
taxonomies: PropTypes.instanceOf(Map), // eslint-disable-line react/no-unused-prop-types
onEntityClick: PropTypes.func,
intl: PropTypes.object.isRequired,
};
export default injectIntl(EntityListItemMainTaxonomies);