How to get event.target and event.currentTarget in a webComponent (within a shadow root) - event-handling

I tried to use the target object and the current target object of an event which occurs in one of my webComponents.
It seems that these methods aren't reachable within a shadow root.
Does anyone have a solution for this issue?

event.composedPath() returns an Array of all elements/Nodes the Event passed
so the target you are after is in: event.composedPath()[0]
unless there are mode:closed shadowRoots in between
https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath
The composedPath() method of the Event interface returns the event’s
path which is an array of the objects on which listeners will be
invoked. This does not include nodes in shadow trees if the shadow
root was created with its ShadowRoot.mode closed.`
On Chrome/new Edge you can also use event.path

Related

Setting callback for SEditableText widget to save text

I have implemented an SEditableText widget in my editor plugin but I can't figure out a reasonable way of accessing the value in the widget. I know there is an SEditableText.OnTextChanged() function but I don't see anyway to override it or set a callback of my own. Is there a standard way to save the content of an SEditableText to a variable?
I am working in the context of FModeToolKit, not sure if that makes a difference.
When you instantiate the slate widget, you should already be making a call to SNew. As part of the property initialiser chain list, initialise SEditableText::OnTextChanged and bind to your container UObject, using BIND_UOBJECT_DELEGATE. The delegate signature should be FOnTextChanged. Your handler will receive an FText which may be stored in a variable as required.
Example:
SEditableText EditableText = SNew(SEditableText)
.OnTextChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, OnTextChanged);
then:
void UMyUObject::HandleOnTextChanged(const FText& InText)
{
// Do something with text
}
Take a look at it in action in UEditableText::RebuildWidget (EditableText.cpp:50, v4.22.1).
NB. I know you specifically asked about OnTextChanged, but also consider OnTextComitted; this is only fired when the slate widget loses focus.
Ah; BIND_UOBJECT_DELEGATE is in UMG. The macro is however just a helper to create a UObject to which you may bind:
#define BIND_UOBJECT_DELEGATE(Type, Function) \
Type::CreateUObject( this, &ThisClass::Function )
So there's two options, either use <my type>::CreateUObject, or you could bind via a shared pointer. Off the top of my head, SP method should look something like:
Edit (based on comments):
If you're not linked against UMG, you could try manually creating the UObject based on the macro defined in SlateWrapperTypes in UMG:
#define BIND_UOBJECT_DELEGATE(Type, Function) \
Type::CreateUObject( this, &ThisClass::Function )
To instead use a shared pointer, the syntax should be along the following lines:
auto OnTextChangedSP = FOnTextChanged::CreateSP(this, &MyClass::MyOnTextChangedHandler);
SEditableText EditableText = SNew(SEditableText)
.OnTextChanged(OnTextChangedSP);

How to update information in an existing node instead of creating a new one using Dgraph?

I am writing a Golang application using Dgraph for persisting objects. From the documentation, I can infer that a new UID and hence a new node is created everytime I mutate an object/run the code.
Is there a way to update the same node data instead for creating a new node?
I tried changing the UID to use "_:name" for the UID field but even this creates a new node everytime the application is run. I wish to be able to update the existing node if it is already present in the DB instead of creating a new node for it.
Unfortunately the docs aren't very beginner friendly yet :/
To modify / mutate existing data you have to run a set operation and supply a rdf-triple like <uid> <predicate> "value" / <objectYouWantToModify> <attributeYouWantToModify> "quotedStringValue". If it is not an attribute but an edge, the value has to be another <uid>.
The full mutation would be for example
{
set {
<0x2> <name> "modified-name" .
}
}
The . terminates the sequence and there is an optional fourth parameter you can use to also assign a label.
Check https://www.w3.org/TR/n-quads/ for further details.

How to copy properties from source node to destination node in cq

I want to get Some Property from Source Node Using getProperties() and setProperty in another Destination Node. How can i check that property is protected or not.As if i copy all the property in destination it gives me ConstraintViolationException
You'd need to get the definition of the property:
PropertyDefinition propDefinition = node.getProperty("/yourprop").getDefinition();
on the definition you can call isProtected():
Boolean isPropertyProtected = propDefinition.isProtected();
or just inline it:
node.getProperty("/yourprop").getDefinition().isProtected();
for further reading I suggest:
http://www.day.com/specs/jcr/2.0/16_Access_Control_Management.html;
Chapter 16.3.12 Interaction with Protected Properties
And the JCR documentation on node types:
http://jackrabbit.apache.org/jcr/node-types.html
This is probably because you are trying to copy all properties which includes cq:primaryType as well. If you see in crx, these basic properties are not editable.
For copy you can take a specific property and set a specific property, instead of copying and pasting all the properties.

GXT 3 dynamically update an element in a treeStore

i'm currently using GXT 3 to display elements in a Tree.
These elements are retrieved from database and identified in the Tree by their id (by that, I mean that the id is the ModelKeyProvider of my store).
I also made it possible for users to create objects locally in the tree with the following code:
private Tree<EntityDAO, String> tree;
private TreeStore<EntityDAO> store;
int count = 1;
// instanciation and irrelevant stuff
...
EntityDAO sel = tree.getSelectionModel().getSelectedItem();
EntityDAO child = new EntityDAO();
child.setId((long) count);
store.add(store.getParent(sel), child);
count++;
tree.setExpanded(sel, true);
tree.getSelectionModel().select(child, false);
As you can see, i set a temporary id (count) to my local object.
The issue occurs when I save my object in database. A permanent id is then set to my EntityDAO but when i try to set this id to my local object to sync it with the database, it doesn't work.
I've tried to modify the child id directly
child.setId(result);
tree.update(child);
I've tried to add a copy of my object with the proper id, and then to remove my object from the tree
EntityDAO newPR = child;
newPR.setId(result);
store.add(store.getParent(child), newPR);
store.remove(child);
But the display is never updated. Any clue?
Let's discuss about the first way you tried, the update method:
child.setId(result);
tree.update(child);
From the update method API state this :
Replaces the item that matches the key of the given item, and fires a
StoreUpdateEvent to indicate that this change has occurred. Any
changes to the previous model via it's record instance will be lost
and the record will be removed. This will not cause the sort or filter
to be re-applied to the object. Overrides: update(...) in Store
Parameters: item the new item to take its place in the Store.
So basically, the update method will replace the item inside the store that have the same key with your parameter. Your data have a new key that doesn't exist inside the store, that's why it doesn't effected anything to your tree display.
Second, let's discuss the create a copy of your object and set it with the proper id:
EntityDAO newPR = child;
newPR.setId(result);
store.add(store.getParent(child), newPR);
store.remove(child);
This way actually will work, but you only have one small problem. The first line of your code actually just give you a variable that have a reference to your old object (the child object), so whenever you remove the child, the newPR also removed. You should really create a new object using the constructor, here how I think you should do it:
EntityDAO newPR = new EntityDAO();
newPR.setId(result);
newPR.setOtherProperty(child.getOtherProperty());
// just copy all property of child to newPR
store.add(store.getParent(child), newPR);
store.remove(child);
Hope this can help you.

Get field values in record.js

I override the record view, by creating in custom/modules/myModule/clients/base/views/record/record.js this file record.js. I want to get the value of a field for the current object and I use this.model.get('duration'), but I get nothing.The only field available is "id". How can I retrieve the values for others fileds?
When the record.js script is initially called, the model won't have fully loaded, so the only available field with be the id.
Your best bet is probably to override the _renderHtml function; by the the time the view is being rendered all the model details will have fully loaded:
_renderHtml: function() {
// custom code involving this.model.get('duration')
// call parent
app.view.View.prototype._renderHtml.call(this);
}
Note that you may find _renderHtml is called multiple times, sometines before the model is fully loaded. This is just a quirk of Sugar so it may be best to add a check in your code:
if (this.model.get('duration')) {
// custom code involving this.model.get('duration')
}
Dont forget that app.model.get('myfield') only delivers the right content (from this field) when your field is already displayed in detailview - else you will get "undefined"!
So you
Have to call the rest api (rest/v10/yourmodel/yourid) - than you
have all the values available
Display your fields (even you dont want to) to be able to use it in app.model.get('yourfield'), an alternative you could append your record.js (after rendering) with $('div [data-name="yourfield"]').hide();
I know this question is quite old already (but if someone else run into this he could find this useful).