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 package info.magnolia.ui.vaadin.ckeditor;
35
36
37 import static info.magnolia.ui.vaadin.gwt.client.ckeditor.VMagnoliaCKEditorTextEvents.*;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Objects;
44 import java.util.Optional;
45
46 import org.apache.commons.lang3.StringUtils;
47 import org.vaadin.alump.ckeditor.CKEditorTextField;
48
49 import com.vaadin.server.PaintException;
50 import com.vaadin.server.PaintTarget;
51
52
53
54
55
56
57
58 public class MagnoliaCKEditorTextField extends CKEditorTextField implements MagnoliaCKEditorTextFieldEvents {
59
60 private String fireEvent;
61 private String fireEventValue;
62 private String[] customEvents;
63 private Map<String, String> serverPlugins;
64 private List<PluginListener> listeners = new ArrayList<>();
65 private MagnoliaCKEditorConfig config;
66
67 public void addListener(PluginListener listener) {
68 listeners.add(listener);
69 }
70
71 public MagnoliaCKEditorTextField(MagnoliaCKEditorConfig config) {
72 super(config);
73 this.config = config;
74 serverPlugins = config.getExternalPlugins();
75 customEvents = config.getListenedEvents();
76 }
77
78 @Override
79 public void changeVariables(Object source, Map<String, Object> variables) {
80 super.changeVariables(source, variables);
81
82 if (isEmpty()) {
83 this.setValue(null, true);
84 }
85
86 Optional.ofNullable(config).ifPresent(c -> {
87
88 Arrays.stream(c.getListenedEvents())
89 .filter(Objects::nonNull)
90 .forEach(eventName -> {
91 final String eventNameResolved = VAR_EVENT_PREFIX + eventName;
92 if (variables.containsKey(eventNameResolved)) {
93 listeners.forEach(l -> l.onPluginEvent(eventName, variables.get(eventNameResolved).toString()));
94 }
95 });
96 });
97 }
98 @Override
99 public void firePluginEvent(String event, String value) {
100 fireEvent = event;
101 fireEventValue = value;
102 markAsDirty();
103 }
104
105 @Override
106 public void paintContent(PaintTarget target) throws PaintException {
107 super.paintContent(target);
108
109 if (serverPlugins != null) {
110 target.addAttribute(VAR_SERVERPLUGINS, serverPlugins);
111 }
112
113
114 if (customEvents != null) {
115 target.addAttribute(VAR_EVENTNAMES, customEvents);
116 }
117
118
119 if (fireEvent != null && fireEventValue != null) {
120 target.addAttribute(VAR_FIRE_PLUGIN_EVENT, fireEvent);
121 target.addAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE, fireEventValue);
122 fireEvent = null;
123 fireEventValue = null;
124 }
125 }
126
127 @Override
128 public void setHeight(float height, Unit unit) {
129 super.setHeight(height, unit);
130 }
131
132 @Override
133 public boolean isEmpty() {
134 return StringUtils.isEmpty(getValue());
135 }
136 }