19
19
import java .io .Serializable ;
20
20
import java .util .Objects ;
21
21
22
+ import com .fasterxml .jackson .databind .JsonNode ;
23
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
24
+ import com .fasterxml .jackson .databind .node .ValueNode ;
25
+
22
26
import com .vaadin .flow .component .Component ;
23
27
import com .vaadin .flow .dom .Element ;
28
+ import com .vaadin .flow .internal .JacksonUtils ;
24
29
import com .vaadin .flow .server .webcomponent .PropertyConfigurationImpl ;
25
30
import com .vaadin .flow .server .webcomponent .WebComponentBinding ;
26
31
27
- import elemental .json .Json ;
28
- import elemental .json .JsonObject ;
29
32
import elemental .json .JsonValue ;
30
33
31
34
/**
@@ -81,14 +84,55 @@ public WebComponent(WebComponentBinding binding, Element componentHost) {
81
84
82
85
/**
83
86
* Fires a custom event on the client-side originating from the web
84
- * component. This event does not bubble in the DOM hierarchy.
87
+ * component with custom event data. This event does not bubble in the DOM
88
+ * hierarchy.
85
89
*
86
90
* @param eventName
87
91
* name of the event, not null
92
+ * @param objectData
93
+ * data the event should carry. This data is placed as the {@code
94
+ * detail} property of the event, nullable
88
95
* @see #fireEvent(String, JsonValue, EventOptions) for full set of options
89
96
*/
97
+ @ Deprecated
98
+ public void fireEvent (String eventName , JsonValue objectData ) {
99
+ fireEvent (eventName , objectData , BASIC_OPTIONS );
100
+ }
101
+
102
+ /**
103
+ * Fires a custom event on the client-side originating from the web
104
+ * component with custom event data. Allows modifying the default event
105
+ * behavior with {@link EventOptions}.
106
+ *
107
+ * @param eventName
108
+ * name of the event, not null
109
+ * @param objectData
110
+ * data the event should carry. This data is placed as the {@code
111
+ * detail} property of the event, nullable
112
+ * @param options
113
+ * event options for {@code bubbles}, {@code cancelable}, and
114
+ * {@code
115
+ * composed} flags, not null
116
+ * @throws NullPointerException
117
+ * if either {@code eventName} or {@code options} is
118
+ * {@code null}
119
+ */
120
+ @ Deprecated
121
+ public void fireEvent (String eventName , JsonValue objectData ,
122
+ EventOptions options ) {
123
+ fireEvent (eventName , JacksonUtils .mapElemental (objectData ), options );
124
+ }
125
+
126
+ /**
127
+ * Fires a custom event on the client-side originating from the web
128
+ * component. This event does not bubble in the DOM hierarchy.
129
+ *
130
+ * @param eventName
131
+ * name of the event, not null
132
+ * @see #fireEvent(String, JsonNode, EventOptions) for full set of options
133
+ */
90
134
public void fireEvent (String eventName ) {
91
- fireEvent (eventName , Json . createNull (), BASIC_OPTIONS );
135
+ fireEvent (eventName , JacksonUtils . nullNode (), BASIC_OPTIONS );
92
136
}
93
137
94
138
/**
@@ -101,9 +145,9 @@ public void fireEvent(String eventName) {
101
145
* @param objectData
102
146
* data the event should carry. This data is placed as the {@code
103
147
* detail} property of the event, nullable
104
- * @see #fireEvent(String, JsonValue , EventOptions) for full set of options
148
+ * @see #fireEvent(String, JsonNode , EventOptions) for full set of options
105
149
*/
106
- public void fireEvent (String eventName , JsonValue objectData ) {
150
+ public void fireEvent (String eventName , JsonNode objectData ) {
107
151
fireEvent (eventName , objectData , BASIC_OPTIONS );
108
152
}
109
153
@@ -125,21 +169,20 @@ public void fireEvent(String eventName, JsonValue objectData) {
125
169
* if either {@code eventName} or {@code options} is
126
170
* {@code null}
127
171
*/
128
- public void fireEvent (String eventName , JsonValue objectData ,
172
+ public void fireEvent (String eventName , JsonNode objectData ,
129
173
EventOptions options ) {
130
174
Objects .requireNonNull (eventName ,
131
175
"Parameter 'eventName' must not be " + "null!" );
132
176
Objects .requireNonNull (options , "Parameter 'options' must not be null" );
133
177
134
- JsonObject object = Json . createObject ();
178
+ ObjectNode object = JacksonUtils . createObjectNode ();
135
179
object .put ("bubbles" , options .isBubbles ());
136
180
object .put ("cancelable" , options .isCancelable ());
137
181
object .put ("composed" , options .isComposed ());
138
- object .put ("detail" ,
139
- objectData == null ? Json . createNull () : objectData );
182
+ object .set ("detail" ,
183
+ objectData == null ? JacksonUtils . nullNode () : objectData );
140
184
141
- componentHost .executeJs (String .format (CUSTOM_EVENT , object .toJson ()),
142
- eventName );
185
+ componentHost .executeJs (String .format (CUSTOM_EVENT , object ), eventName );
143
186
}
144
187
145
188
/**
@@ -222,9 +265,15 @@ private void setProperty(String propertyName, Object value) {
222
265
} else if (value instanceof Boolean ) {
223
266
componentHost .executeJs (UPDATE_PROPERTY , propertyName ,
224
267
(Boolean ) value );
268
+ } else if (value instanceof ValueNode ) {
269
+ // this gets around executeJavaScript limitation.
270
+ // Since properties can take JSON values, this was needed to allow
271
+ // that expected behavior.
272
+ componentHost .executeJs (String .format (UPDATE_PROPERTY_FORMAT ,
273
+ ((ValueNode ) value ).toString ()), propertyName );
225
274
} else if (value instanceof JsonValue ) {
226
275
// this gets around executeJavaScript limitation.
227
- // Since properties can take JsonValues , this was needed to allow
276
+ // Since properties can take JSON values , this was needed to allow
228
277
// that expected behavior.
229
278
componentHost .executeJs (String .format (UPDATE_PROPERTY_FORMAT ,
230
279
((JsonValue ) value ).toJson ()), propertyName );
0 commit comments