Skip to content

Commit 5ea663c

Browse files
author
Raymond Augé
committed
LPS-101063 add vocabulary selector opt-in
1 parent e07a8fb commit 5ea663c

File tree

3 files changed

+311
-18
lines changed

3 files changed

+311
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.liferay.segments.web.internal.field.customizer;
2+
3+
import com.liferay.asset.kernel.model.AssetVocabulary;
4+
import com.liferay.asset.kernel.service.AssetCategoryLocalService;
5+
import com.liferay.asset.kernel.service.AssetVocabularyLocalService;
6+
import com.liferay.portal.kernel.log.Log;
7+
import com.liferay.portal.kernel.log.LogFactoryUtil;
8+
import com.liferay.portal.kernel.model.ClassedModel;
9+
import com.liferay.portal.kernel.portlet.LiferayWindowState;
10+
import com.liferay.portal.kernel.portlet.PortletProvider;
11+
import com.liferay.portal.kernel.portlet.PortletProviderUtil;
12+
import com.liferay.portal.kernel.util.GetterUtil;
13+
import com.liferay.portal.kernel.util.Portal;
14+
import com.liferay.segments.context.Context;
15+
import com.liferay.segments.field.Field;
16+
import com.liferay.segments.field.customizer.SegmentsFieldCustomizer;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.Locale;
21+
22+
import javax.portlet.PortletRequest;
23+
import javax.portlet.PortletURL;
24+
25+
import org.osgi.service.component.annotations.Component;
26+
import org.osgi.service.component.annotations.Reference;
27+
28+
@Component(
29+
immediate = true,
30+
property = {
31+
"segments.field.customizer.entity.name=User",
32+
"segments.field.customizer.entity.name=Organization",
33+
"segments.field.customizer.entity.name=Context",
34+
"segments.field.customizer.key=" + FreeformTextSegmentsFieldCustomizer.KEY,
35+
"segments.field.customizer.priority:Integer=50"
36+
},
37+
service = SegmentsFieldCustomizer.class
38+
)
39+
public class FreeformTextSegmentsFieldCustomizer
40+
extends BaseSegmentsFieldCustomizer {
41+
42+
public static final String KEY = "freeform";
43+
44+
@Override
45+
public ClassedModel getClassedModel(String fieldValue) {
46+
return _getAssetVocabulary(fieldValue);
47+
}
48+
49+
@Override
50+
public String getClassName() {
51+
return AssetVocabulary.class.getName();
52+
}
53+
54+
@Override
55+
public List<String> getFieldNames() {
56+
return _fieldNames;
57+
}
58+
59+
@Override
60+
public String getFieldValueName(String fieldValue, Locale locale) {
61+
AssetVocabulary assetVocabulary = _getAssetVocabulary(fieldValue);
62+
63+
if (assetVocabulary == null) {
64+
return fieldValue;
65+
}
66+
67+
return assetVocabulary.getName();
68+
}
69+
70+
@Override
71+
public String getKey() {
72+
return KEY;
73+
}
74+
75+
@Override
76+
public Field.SelectEntity getSelectEntity(PortletRequest portletRequest) {
77+
try {
78+
PortletURL portletURL = PortletProviderUtil.getPortletURL(
79+
portletRequest, AssetVocabulary.class.getName(),
80+
PortletProvider.Action.BROWSE);
81+
82+
portletURL.setParameter("eventName", "selectEntity");
83+
portletURL.setWindowState(LiferayWindowState.POP_UP);
84+
85+
return new Field.SelectEntity(
86+
"selectEntity",
87+
getSelectEntityTitle(
88+
_portal.getLocale(portletRequest),
89+
AssetVocabulary.class.getName()),
90+
portletURL.toString(), true);
91+
}
92+
catch (Exception e) {
93+
if (_log.isWarnEnabled()) {
94+
_log.warn("Unable to get select entity", e);
95+
}
96+
97+
return null;
98+
}
99+
}
100+
101+
private AssetVocabulary _getAssetVocabulary(String fieldValue) {
102+
long _vocabularyId = GetterUtil.getLong(fieldValue);
103+
104+
if (_vocabularyId == 0) {
105+
return null;
106+
}
107+
108+
return _assetVocabularyLocalService.fetchAssetVocabulary(_vocabularyId);
109+
}
110+
111+
private static final Log _log = LogFactoryUtil.getLog(
112+
FreeformTextSegmentsFieldCustomizer.class);
113+
114+
private static final List<String> _fieldNames = Arrays.asList(
115+
"emailAddress", "firstName", "jobTitle", "lastName", "screenName",
116+
"userName", "country", "region", "name", "nameTreePath", "type",
117+
Context.BROWSER, Context.DEVICE_BRAND, Context.DEVICE_MODEL,
118+
Context.HOSTNAME, Context.REFERRER_URL, Context.URL, Context.USER_AGENT,
119+
Context.COOKIES, Context.REQUEST_PARAMETERS);
120+
121+
@Reference
122+
private AssetCategoryLocalService _assetCategoryLocalService;
123+
124+
@Reference
125+
private AssetVocabularyLocalService _assetVocabularyLocalService;
126+
127+
@Reference
128+
private Portal _portal;
129+
130+
}

modules/apps/segments/segments-web/src/main/resources/META-INF/resources/js/components/inputs/CollectionInput.es.js

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class CollectionInput extends React.Component {
2626
static propTypes = {
2727
disabled: propTypes.bool,
2828
onChange: propTypes.func.isRequired,
29+
selectEntity: propTypes.shape({
30+
id: propTypes.string,
31+
multiple: propTypes.bool,
32+
title: propTypes.string,
33+
uri: propTypes.string
34+
}),
2935
value: propTypes.string
3036
};
3137

@@ -75,22 +81,97 @@ class CollectionInput extends React.Component {
7581
};
7682
};
7783

84+
/**
85+
* Opens a modal for selecting entities. Uses different methods for
86+
* selecting multiple entities versus single because of the way the event
87+
* and data is submitted.
88+
*/
89+
_handleSelectEntity = () => {
90+
const {
91+
onChange,
92+
selectEntity: {id, multiple, title, uri}
93+
} = this.props;
94+
95+
if (multiple) {
96+
AUI().use('liferay-item-selector-dialog', A => {
97+
const itemSelectorDialog = new A.LiferayItemSelectorDialog({
98+
eventName: id,
99+
on: {
100+
selectedItemChange: event => {
101+
const newVal = event.newVal;
102+
103+
if (newVal) {
104+
const selectedValues = event.newVal.map(
105+
item => ({
106+
displayValue: item.name,
107+
value: item.id
108+
})
109+
);
110+
111+
onChange(selectedValues);
112+
}
113+
}
114+
},
115+
strings: {
116+
add: Liferay.Language.get('select'),
117+
cancel: Liferay.Language.get('cancel')
118+
},
119+
title,
120+
url: uri
121+
});
122+
123+
itemSelectorDialog.open();
124+
});
125+
} else {
126+
Liferay.Util.selectEntity(
127+
{
128+
dialog: {
129+
constrain: true,
130+
destroyOnHide: true,
131+
modal: true
132+
},
133+
id,
134+
title,
135+
uri
136+
},
137+
event => {
138+
onChange({
139+
displayValue: event.entityname,
140+
value: event.entityid
141+
});
142+
}
143+
);
144+
}
145+
};
146+
78147
render() {
79148
const {disabled} = this.props;
80149
const {key, value} = this._stringToKeyValueObject(this.props.value);
81150

82151
return (
83152
<>
84-
<input
85-
className="criterion-input form-control"
86-
data-testid="collection-key-input"
87-
disabled={disabled}
88-
onChange={this._handleKeyChange}
89-
onKeyDown={this._handleKeyDown}
90-
placeholder={Liferay.Language.get('key')}
91-
type="text"
92-
value={key}
93-
/>
153+
<div className="criterion-input input-group">
154+
<input
155+
className="form-control"
156+
data-testid="collection-key-input"
157+
disabled={disabled}
158+
onChange={this._handleKeyChange}
159+
onKeyDown={this._handleKeyDown}
160+
placeholder={Liferay.Language.get('key')}
161+
type="text"
162+
value={key}
163+
/>
164+
<div className="input-group-append">
165+
<button
166+
className="btn btn-secondary"
167+
id="button-addon1"
168+
onClick={this._handleSelectEntity}
169+
type="button"
170+
>
171+
{Liferay.Language.get('select')}
172+
</button>
173+
</div>
174+
</div>
94175

95176
<input
96177
className="criterion-input form-control"

modules/apps/segments/segments-web/src/main/resources/META-INF/resources/js/components/inputs/StringInput.es.js

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class StringInput extends React.Component {
2121
disabled: propTypes.bool,
2222
onChange: propTypes.func.isRequired,
2323
options: propTypes.array,
24+
selectEntity: propTypes.shape({
25+
id: propTypes.string,
26+
multiple: propTypes.bool,
27+
title: propTypes.string,
28+
uri: propTypes.string
29+
}),
2430
value: propTypes.oneOfType([propTypes.string, propTypes.number])
2531
};
2632

@@ -32,18 +38,94 @@ class StringInput extends React.Component {
3238
this.props.onChange({value: event.target.value});
3339
};
3440

41+
/**
42+
* Opens a modal for selecting entities. Uses different methods for
43+
* selecting multiple entities versus single because of the way the event
44+
* and data is submitted.
45+
*/
46+
_handleSelectEntity = () => {
47+
const {
48+
onChange,
49+
selectEntity: {id, multiple, title, uri}
50+
} = this.props;
51+
52+
if (multiple) {
53+
AUI().use('liferay-item-selector-dialog', A => {
54+
const itemSelectorDialog = new A.LiferayItemSelectorDialog({
55+
eventName: id,
56+
on: {
57+
selectedItemChange: event => {
58+
const newVal = event.newVal;
59+
60+
if (newVal) {
61+
const selectedValues = event.newVal.map(
62+
item => ({
63+
displayValue: item.name,
64+
value: item.id
65+
})
66+
);
67+
68+
onChange(selectedValues);
69+
}
70+
}
71+
},
72+
strings: {
73+
add: Liferay.Language.get('select'),
74+
cancel: Liferay.Language.get('cancel')
75+
},
76+
title,
77+
url: uri
78+
});
79+
80+
itemSelectorDialog.open();
81+
});
82+
} else {
83+
Liferay.Util.selectEntity(
84+
{
85+
dialog: {
86+
constrain: true,
87+
destroyOnHide: true,
88+
modal: true
89+
},
90+
id,
91+
title,
92+
uri
93+
},
94+
event => {
95+
onChange({
96+
displayValue: event.entityname,
97+
value: event.entityid
98+
});
99+
}
100+
);
101+
}
102+
};
103+
35104
render() {
36105
const {disabled, options, value} = this.props;
37106

38107
return options.length === 0 ? (
39-
<input
40-
className="criterion-input form-control"
41-
data-testid="simple-string"
42-
disabled={disabled}
43-
onChange={this._handleChange}
44-
type="text"
45-
value={value}
46-
/>
108+
<div className="criterion-input input-group">
109+
<input
110+
className="form-control"
111+
data-testid="simple-string"
112+
disabled={disabled}
113+
onChange={this._handleChange}
114+
placeholder={Liferay.Language.get('value')}
115+
type="text"
116+
value={value}
117+
/>
118+
<div className="input-group-append">
119+
<button
120+
className="btn btn-secondary"
121+
id="button-addon1"
122+
onClick={this._handleSelectEntity}
123+
type="button"
124+
>
125+
{Liferay.Language.get('select')}
126+
</button>
127+
</div>
128+
</div>
47129
) : (
48130
<ClaySelectWithOption
49131
className="criterion-input form-control"

0 commit comments

Comments
 (0)