001
014
015 package com.liferay.taglib.ui;
016
017 import com.liferay.portal.kernel.editor.Editor;
018 import com.liferay.portal.kernel.editor.configuration.EditorConfiguration;
019 import com.liferay.portal.kernel.editor.configuration.EditorConfigurationFactoryUtil;
020 import com.liferay.portal.kernel.language.LanguageUtil;
021 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
022 import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
023 import com.liferay.portal.kernel.util.JavaConstants;
024 import com.liferay.portal.kernel.util.MapUtil;
025 import com.liferay.portal.kernel.util.PropsKeys;
026 import com.liferay.portal.kernel.util.PropsUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.util.WebKeys;
029 import com.liferay.portal.model.Portlet;
030 import com.liferay.portal.theme.ThemeDisplay;
031 import com.liferay.portlet.RequestBackedPortletURLFactory;
032 import com.liferay.portlet.RequestBackedPortletURLFactoryUtil;
033 import com.liferay.registry.Registry;
034 import com.liferay.registry.RegistryUtil;
035 import com.liferay.registry.ServiceReference;
036 import com.liferay.registry.collections.ServiceReferenceMapper;
037 import com.liferay.registry.collections.ServiceTrackerCollections;
038 import com.liferay.registry.collections.ServiceTrackerMap;
039 import com.liferay.taglib.aui.AUIUtil;
040 import com.liferay.taglib.util.IncludeTag;
041 import com.liferay.taglib.util.TagResourceBundleUtil;
042
043 import java.io.IOException;
044
045 import java.util.Enumeration;
046 import java.util.HashMap;
047 import java.util.Map;
048 import java.util.ResourceBundle;
049
050 import javax.portlet.PortletRequest;
051 import javax.portlet.PortletResponse;
052
053 import javax.servlet.ServletException;
054 import javax.servlet.http.HttpServletRequest;
055 import javax.servlet.http.HttpServletResponse;
056
057
060 public class InputEditorTag extends IncludeTag {
061
062 public static Editor getEditor(
063 HttpServletRequest request, String editorName) {
064
065 if (!BrowserSnifferUtil.isRtf(request)) {
066 return _serviceTrackerMap.getService("simple");
067 }
068
069 if (Validator.isNull(editorName)) {
070 return _serviceTrackerMap.getService(_EDITOR_WYSIWYG_DEFAULT);
071 }
072
073 if (!_serviceTrackerMap.containsKey(editorName)) {
074 return _serviceTrackerMap.getService(_EDITOR_WYSIWYG_DEFAULT);
075 }
076
077 return _serviceTrackerMap.getService(editorName);
078 }
079
080 public void setAllowBrowseDocuments(boolean allowBrowseDocuments) {
081 _allowBrowseDocuments = allowBrowseDocuments;
082 }
083
084 public void setAutoCreate(boolean autoCreate) {
085 _autoCreate = autoCreate;
086 }
087
088 public void setConfigKey(String configKey) {
089 _configKey = configKey;
090 }
091
092 public void setConfigParams(Map<String, String> configParams) {
093 _configParams = configParams;
094 }
095
096 public void setContents(String contents) {
097 _contents = contents;
098 }
099
100 public void setContentsLanguageId(String contentsLanguageId) {
101 _contentsLanguageId = contentsLanguageId;
102 }
103
104 public void setCssClass(String cssClass) {
105 _cssClass = cssClass;
106 }
107
108 public void setData(Map<String, Object> data) {
109 _data = data;
110 }
111
112
115 @Deprecated
116 public void setEditorImpl(String editorImpl) {
117 _editorName = PropsUtil.get(editorImpl);
118 }
119
120 public void setEditorName(String editorName) {
121 _editorName = editorName;
122 }
123
124 public void setFileBrowserParams(Map<String, String> fileBrowserParams) {
125 _fileBrowserParams = fileBrowserParams;
126 }
127
128 public void setHeight(String height) {
129 _height = height;
130 }
131
132
135 @Deprecated
136 public void setInitMethod(String initMethod) {
137 _initMethod = initMethod;
138 }
139
140 public void setInlineEdit(boolean inlineEdit) {
141 _inlineEdit = inlineEdit;
142 }
143
144 public void setInlineEditSaveURL(String inlineEditSaveURL) {
145 _inlineEditSaveURL = inlineEditSaveURL;
146 }
147
148 public void setName(String name) {
149 _name = name;
150 }
151
152 public void setOnBlurMethod(String onBlurMethod) {
153 _onBlurMethod = onBlurMethod;
154 }
155
156 public void setOnChangeMethod(String onChangeMethod) {
157 _onChangeMethod = onChangeMethod;
158 }
159
160 public void setOnFocusMethod(String onFocusMethod) {
161 _onFocusMethod = onFocusMethod;
162 }
163
164 public void setOnInitMethod(String onInitMethod) {
165 _onInitMethod = onInitMethod;
166 }
167
168 public void setPlaceholder(String placeholder) {
169 _placeholder = placeholder;
170 }
171
172 public void setResizable(boolean resizable) {
173 _resizable = resizable;
174 }
175
176 public void setShowSource(boolean showSource) {
177 _showSource = showSource;
178 }
179
180 public void setSkipEditorLoading(boolean skipEditorLoading) {
181 _skipEditorLoading = skipEditorLoading;
182 }
183
184 public void setToolbarSet(String toolbarSet) {
185 _toolbarSet = toolbarSet;
186 }
187
188 public void setWidth(String width) {
189 _width = width;
190 }
191
192 @Override
193 protected void cleanUp() {
194 _allowBrowseDocuments = true;
195 _autoCreate = true;
196 _configKey = null;
197 _configParams = null;
198 _contents = null;
199 _contentsLanguageId = null;
200 _cssClass = null;
201 _data = null;
202 _editorName = null;
203 _fileBrowserParams = null;
204 _height = null;
205 _initMethod = "initEditor";
206 _inlineEdit = false;
207 _inlineEditSaveURL = null;
208 _name = "editor";
209 _onChangeMethod = null;
210 _onBlurMethod = null;
211 _onFocusMethod = null;
212 _onInitMethod = null;
213 _placeholder = null;
214 _resizable = true;
215 _showSource = true;
216 _skipEditorLoading = false;
217 _toolbarSet = _TOOLBAR_SET_DEFAULT;
218 _width = null;
219 }
220
221 protected String getConfigKey() {
222 String configKey = _configKey;
223
224 if (Validator.isNull(configKey)) {
225 configKey = _name;
226 }
227
228 return configKey;
229 }
230
231 protected String getContentsLanguageId() {
232 if (_contentsLanguageId == null) {
233 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
234 WebKeys.THEME_DISPLAY);
235
236 _contentsLanguageId = themeDisplay.getLanguageId();
237 }
238
239 return _contentsLanguageId;
240 }
241
242 protected String getCssClasses() {
243 Portlet portlet = (Portlet)request.getAttribute(WebKeys.RENDER_PORTLET);
244
245 String cssClasses = "portlet ";
246
247 if (portlet != null) {
248 cssClasses += portlet.getCssClassWrapper();
249 }
250
251 return cssClasses;
252 }
253
254 protected Map<String, Object> getData() {
255 String portletId = (String)request.getAttribute(WebKeys.PORTLET_ID);
256
257 if (portletId == null) {
258 return _data;
259 }
260
261 Map<String, Object> attributes = new HashMap<>();
262
263 Enumeration<String> enumeration = request.getAttributeNames();
264
265 while (enumeration.hasMoreElements()) {
266 String attributeName = enumeration.nextElement();
267
268 if (attributeName.startsWith("liferay-ui:input-editor")) {
269 attributes.put(
270 attributeName, request.getAttribute(attributeName));
271 }
272 }
273
274 attributes.put("liferay-ui:input-editor:namespace", getNamespace());
275
276 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
277 WebKeys.THEME_DISPLAY);
278
279 EditorConfiguration editorConfiguration =
280 EditorConfigurationFactoryUtil.getEditorConfiguration(
281 portletId, getConfigKey(), getEditorName(request), attributes,
282 themeDisplay, getRequestBackedPortletURLFactory());
283
284 Map<String, Object> data = editorConfiguration.getData();
285
286 if (MapUtil.isNotEmpty(_data)) {
287 MapUtil.merge(_data, data);
288 }
289
290 return data;
291 }
292
293 protected Editor getEditor(HttpServletRequest request) {
294 return getEditor(request, _editorName);
295 }
296
297 protected String getEditorName(HttpServletRequest request) {
298 Editor editor = getEditor(request);
299
300 return editor.getName();
301 }
302
303 protected String getEditorResourceType() {
304 Editor editor = getEditor(request);
305
306 return editor.getResourceType();
307 }
308
309 protected String getNamespace() {
310 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
311 JavaConstants.JAVAX_PORTLET_REQUEST);
312 PortletResponse portletResponse = (PortletResponse)request.getAttribute(
313 JavaConstants.JAVAX_PORTLET_RESPONSE);
314
315 if ((portletRequest == null) || (portletResponse == null)) {
316 return AUIUtil.getNamespace(request);
317 }
318
319 return AUIUtil.getNamespace(portletRequest, portletResponse);
320 }
321
322 @Override
323 protected String getPage() {
324 Editor editor = getEditor(request);
325
326 return editor.getJspPath();
327 }
328
329 protected RequestBackedPortletURLFactory
330 getRequestBackedPortletURLFactory() {
331
332 PortletRequest portletRequest = (PortletRequest)request.getAttribute(
333 JavaConstants.JAVAX_PORTLET_REQUEST);
334
335 if (portletRequest == null) {
336 return RequestBackedPortletURLFactoryUtil.create(request);
337 }
338
339 return RequestBackedPortletURLFactoryUtil.create(portletRequest);
340 }
341
342 protected String getToolbarSet() {
343 if (Validator.isNotNull(_toolbarSet)) {
344 return _toolbarSet;
345 }
346
347 return _TOOLBAR_SET_DEFAULT;
348 }
349
350 @Override
351 protected void includePage(String page, HttpServletResponse response)
352 throws IOException, ServletException {
353
354 servletContext = PortalWebResourcesUtil.getServletContext(
355 getEditorResourceType());
356
357 super.includePage(page, response);
358 }
359
360 @Override
361 protected void setAttributes(HttpServletRequest request) {
362 request.setAttribute(
363 "liferay-ui:input-editor:allowBrowseDocuments",
364 String.valueOf(_allowBrowseDocuments));
365 request.setAttribute(
366 "liferay-ui:input-editor:autoCreate", String.valueOf(_autoCreate));
367 request.setAttribute(
368 "liferay-ui:input-editor:configParams", _configParams);
369 request.setAttribute("liferay-ui:input-editor:contents", _contents);
370 request.setAttribute(
371 "liferay-ui:input-editor:contentsLanguageId",
372 getContentsLanguageId());
373 request.setAttribute("liferay-ui:input-editor:cssClass", _cssClass);
374 request.setAttribute(
375 "liferay-ui:input-editor:cssClasses", getCssClasses());
376 request.setAttribute(
377 "liferay-ui:input-editor:editorName", getEditorName(request));
378 request.setAttribute(
379 "liferay-ui:input-editor:fileBrowserParams", _fileBrowserParams);
380 request.setAttribute("liferay-ui:input-editor:height", _height);
381 request.setAttribute("liferay-ui:input-editor:initMethod", _initMethod);
382 request.setAttribute(
383 "liferay-ui:input-editor:inlineEdit", String.valueOf(_inlineEdit));
384 request.setAttribute(
385 "liferay-ui:input-editor:inlineEditSaveURL", _inlineEditSaveURL);
386 request.setAttribute("liferay-ui:input-editor:name", _name);
387 request.setAttribute(
388 "liferay-ui:input-editor:onBlurMethod", _onBlurMethod);
389 request.setAttribute(
390 "liferay-ui:input-editor:onChangeMethod", _onChangeMethod);
391 request.setAttribute(
392 "liferay-ui:input-editor:onFocusMethod", _onFocusMethod);
393 request.setAttribute(
394 "liferay-ui:input-editor:onInitMethod", _onInitMethod);
395
396 ResourceBundle resourceBundle = TagResourceBundleUtil.getResourceBundle(
397 pageContext);
398
399 if (Validator.isNull(_placeholder)) {
400 _placeholder = LanguageUtil.get(
401 resourceBundle, "write-your-content-here");
402 }
403
404 request.setAttribute(
405 "liferay-ui:input-editor:placeholder", _placeholder);
406
407 request.setAttribute(
408 "liferay-ui:input-editor:resizable", String.valueOf(_resizable));
409 request.setAttribute(
410 "liferay-ui:input-editor:showSource", String.valueOf(_showSource));
411 request.setAttribute(
412 "liferay-ui:input-editor:skipEditorLoading",
413 String.valueOf(_skipEditorLoading));
414 request.setAttribute(
415 "liferay-ui:input-editor:toolbarSet", getToolbarSet());
416 request.setAttribute("liferay-ui:input-editor:width", _width);
417
418 request.setAttribute("liferay-ui:input-editor:data", getData());
419 }
420
421 private static final String _EDITOR_WYSIWYG_DEFAULT = PropsUtil.get(
422 PropsKeys.EDITOR_WYSIWYG_DEFAULT);
423
424 private static final String _TOOLBAR_SET_DEFAULT = "liferay";
425
426 private static final ServiceTrackerMap<String, Editor> _serviceTrackerMap =
427 ServiceTrackerCollections.singleValueMap(
428 Editor.class, null,
429 new ServiceReferenceMapper<String, Editor>() {
430
431 @Override
432 public void map(
433 ServiceReference<Editor> serviceReference,
434 Emitter<String> emitter) {
435
436 Registry registry = RegistryUtil.getRegistry();
437
438 Editor editor = registry.getService(serviceReference);
439
440 emitter.emit(editor.getName());
441 }
442
443 });
444
445 static {
446 _serviceTrackerMap.open();
447 }
448
449 private boolean _allowBrowseDocuments = true;
450 private boolean _autoCreate = true;
451 private String _configKey;
452 private Map<String, String> _configParams;
453 private String _contents;
454 private String _contentsLanguageId;
455 private String _cssClass;
456 private Map<String, Object> _data = null;
457 private String _editorName;
458 private Map<String, String> _fileBrowserParams;
459 private String _height;
460 private String _initMethod = "initEditor";
461 private boolean _inlineEdit;
462 private String _inlineEditSaveURL;
463 private String _name = "editor";
464 private String _onBlurMethod;
465 private String _onChangeMethod;
466 private String _onFocusMethod;
467 private String _onInitMethod;
468 private String _placeholder;
469 private boolean _resizable = true;
470 private boolean _showSource = true;
471 private boolean _skipEditorLoading;
472 private String _toolbarSet = _TOOLBAR_SET_DEFAULT;
473 private String _width;
474
475 }