-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Element.executeJavaScript #4539
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
import com.vaadin.flow.dom.impl.BasicTextElementStateProvider; | ||
import com.vaadin.flow.dom.impl.CustomAttribute; | ||
import com.vaadin.flow.dom.impl.ThemeListImpl; | ||
import com.vaadin.flow.function.SerializableConsumer; | ||
import com.vaadin.flow.internal.JavaScriptSemantics; | ||
import com.vaadin.flow.internal.JsonCodec; | ||
import com.vaadin.flow.internal.StateNode; | ||
|
@@ -466,12 +467,14 @@ public Element removeAttribute(String attribute) { | |
throw new IllegalArgumentException(ATTRIBUTE_NAME_CANNOT_BE_NULL); | ||
} | ||
String lowerCaseAttribute = attribute.toLowerCase(Locale.ENGLISH); | ||
if(hasAttribute(lowerCaseAttribute)) { | ||
Optional<CustomAttribute> customAttribute = CustomAttribute.get(lowerCaseAttribute); | ||
if (hasAttribute(lowerCaseAttribute)) { | ||
Optional<CustomAttribute> customAttribute = CustomAttribute | ||
.get(lowerCaseAttribute); | ||
if (customAttribute.isPresent()) { | ||
customAttribute.get().removeAttribute(this); | ||
} else { | ||
getStateProvider().removeAttribute(getNode(), lowerCaseAttribute); | ||
getStateProvider().removeAttribute(getNode(), | ||
lowerCaseAttribute); | ||
} | ||
} | ||
return this; | ||
|
@@ -1338,12 +1341,13 @@ public Registration addAttachListener( | |
// This explicit class instantiation is the workaround | ||
// which fixes a JVM optimization+serialization bug. | ||
// Do not convert to lambda | ||
// Detected under Win7_64 /JDK 1.8.0_152, 1.8.0_172 | ||
// Detected under Win7_64 /JDK 1.8.0_152, 1.8.0_172 | ||
// see ElementAttributeMap#deferRegistration | ||
new Command() { | ||
@Override | ||
public void execute() { | ||
attachListener.onAttach(new ElementAttachEvent(Element.this)); | ||
attachListener | ||
.onAttach(new ElementAttachEvent(Element.this)); | ||
} | ||
}); | ||
} | ||
|
@@ -1370,12 +1374,13 @@ public Registration addDetachListener( | |
// This explicit class instantiation is the workaround | ||
// which fixes a JVM optimization+serialization bug. | ||
// Do not convert to lambda | ||
// Detected under Win7_64 /JDK 1.8.0_152, 1.8.0_172 | ||
// Detected under Win7_64 /JDK 1.8.0_152, 1.8.0_172 | ||
// see ElementAttributeMap#deferRegistration | ||
new Command() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@Override | ||
public void execute() { | ||
detachListener.onDetach(new ElementDetachEvent(Element.this)); | ||
detachListener | ||
.onDetach(new ElementDetachEvent(Element.this)); | ||
} | ||
}); | ||
} | ||
|
@@ -1451,27 +1456,75 @@ public void callFunction(String functionName, Serializable... arguments) { | |
assert !functionName | ||
.startsWith(".") : "Function name should not start with a dot"; | ||
|
||
getNode().runWhenAttached( | ||
ui -> doCallFunction(ui, functionName, arguments)); | ||
|
||
runBeforeAttachedResponse(ui -> { | ||
// $0.method($1,$2,$3) | ||
String paramPlaceholderString = IntStream | ||
.range(1, arguments.length + 1).mapToObj(i -> "$" + i) | ||
.collect(Collectors.joining(",")); | ||
Serializable[] jsParameters = Stream | ||
.concat(Stream.of(this), Stream.of(arguments)) | ||
.toArray(Serializable[]::new); | ||
|
||
ui.getPage().executeJavaScript( | ||
"$0." + functionName + "(" + paramPlaceholderString + ")", | ||
jsParameters); | ||
}); | ||
} | ||
|
||
private void doCallFunction(UI ui, String functionName, | ||
Serializable... arguments) { | ||
ui.getInternals().getStateTree().beforeClientResponse(getNode(), | ||
context -> { | ||
// $0.method($1,$2,$3) | ||
String paramPlaceholderString = IntStream | ||
.range(1, arguments.length + 1) | ||
.mapToObj(i -> "$" + i) | ||
.collect(Collectors.joining(",")); | ||
Serializable[] jsParameters = Stream | ||
.concat(Stream.of(this), Stream.of(arguments)) | ||
.toArray(Serializable[]::new); | ||
// When updating JavaDocs here, keep in sync with Page.executeJavaScript | ||
/** | ||
* Asynchronously runs the given JavaScript expression in the browser in the | ||
* context of this element. This element will be available to the expression | ||
* as <code>this</code>. The given parameters will be available as variables | ||
* named <code>$0</code>, <code>$1</code>, and so on. Supported parameter | ||
* types are: | ||
* <ul> | ||
* <li>{@link String} | ||
* <li>{@link Integer} | ||
* <li>{@link Double} | ||
* <li>{@link Boolean} | ||
* <li>{@link Element} (will be sent as <code>null</code> if the server-side | ||
* element instance is not attached when the invocation is sent to the | ||
* client) | ||
* </ul> | ||
* Note that the parameter variables can only be used in contexts where a | ||
* JavaScript variable can be used. You should for instance do | ||
* <code>'prefix' + $0</code> instead of <code>'prefix$0'</code> and | ||
* <code>value[$0]</code> instead of <code>value.$0</code> since JavaScript | ||
* variables aren't evaluated inside strings or property names. | ||
* | ||
* @param expression | ||
* the JavaScript expression to invoke | ||
* @param parameters | ||
* parameters to pass to the expression | ||
*/ | ||
public void executeJavaScript(String expression, | ||
Serializable... parameters) { | ||
|
||
ui.getPage().executeJavaScript("$0." + functionName + "(" | ||
+ paramPlaceholderString + ")", jsParameters); | ||
}); | ||
// Add "this" as the last parameter | ||
Serializable[] wrappedParameters = Stream | ||
.concat(Stream.of(parameters), Stream.of(this)) | ||
.toArray(Serializable[]::new); | ||
|
||
// Wrap in a function that is applied with last parameter as "this" | ||
String wrappedExpression = "(function() { " + expression + "}).apply($" | ||
+ parameters.length + ")"; | ||
|
||
runBeforeAttachedResponse(ui -> ui.getPage() | ||
.executeJavaScript(wrappedExpression, wrappedParameters)); | ||
} | ||
|
||
/** | ||
* Runs the given action right before the next response during which this | ||
* element is attached. | ||
* | ||
* @param action | ||
* the action to run | ||
*/ | ||
private void runBeforeAttachedResponse(SerializableConsumer<UI> action) { | ||
getNode().runWhenAttached( | ||
ui -> ui.getInternals().getStateTree().beforeClientResponse( | ||
getNode(), context -> action.accept(context.getUI()))); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this anonymous inner class a lambda