How can I rename a jsTree node - javascript

I am not talking about $("#demo1").jstree("rename",node) which makes the node editable for the user. I am talking about the name being changed within the code. For example my nodes are all prefixed with a 2 digit number "[01]" so before I call $("#demo1").jstree("rename",node) I want to strip out the prefix, and put it back in once the user has finished editing. I have tried selecting "#nodeid a" but inside the hyperlink there is an ins tag and this gets replaced if i replace the URL contents. The documentation hasn't been helpful and I havent had much luck looking through the libraries code, can any help me? Chris

The recommended method is to use rename_node
$("#demo1").jstree('rename_node', node , text );
Please keep in mind that by default all modifications to the tree are
prevented (create, rename, move, delete). To enable them set
core.check_callback to true
$('#demo1').jstree({
'core': {
'check_callback': true,
/// rest of the options...
}
});
Rename your node (alternative, not recommended)
$("#demo1").jstree('set_text', node , text );
Debugging
If you still encounter trouble, you can use this method to get the last error.
$('#demo1').jstree(true).last_error()
For older versions (v1.*)
$("#demo1").jstree('rename_node', [node , text] );
$("#demo1").jstree('set_text', [node , text] );
See also:
this jsfiddle for the comparison and example of both methods.
Interaction with jsTree (how to call API methods)
API documentation of rename_node
API documentation of set_text

I believe there is an syntax error with respect to the square braces "[" in the above answer. I use jsTree 3.0.4 and this is the correct syntax -
right - $("#demo1").jstree('set_text',node,text);
wrong - $("#demo1").jstree('rename_node', [node , text] );
Example -
$("#tree_3").jstree('set_text',"#idSelectorForNode" ,"NewName");

You should turn on the switch to allow the rename operation, such as:
$('#container').jstree({
'core' : {
'check_callback' : function (operation, node, node_parent, node_position, more) {
// operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
// in case of 'rename_node' node_position is filled with the new node name
return operation === 'rename_node' ? true : false;
}
});

You can use this for updating node text with jstree refresh:
$("#demo1").jstree(true).rename_node(node , "Renamed_Text");

Related

JointJs - Discarding the command in CommandManager.cmdBeforeAdd

In my JointJs application, I want to discard particular commands so that they are not added in the Undo/Redo stack.
I followed the exact same code snippet from the JointJs documentation like below:
var commandManager = new joint.dia.CommandManager({
graph: graph,
cmdBeforeAdd: function(cmdName, cell, graph, options) {
options = options || {};
return !options.ignoreCommandManager;
}
});
// ...
// Note that the last argument to set() is an options object that gets passed to the cmdBeforeAdd() function.
element.set({ 'z': 1 }, { ignoreCommandManager: true });
But when I look into the options object in the debug mode, it doesn't contain any property with the name ignoreCommandManager.
I have also tried the below call to set the z value but it didn't work either.
element.set('z', 1 , { ignoreCommandManager: true });
Any idea why the options object is missing this property to ignore the command, please?
Initially, It was failing in Firefox when I posted the question here. The cache was also disabled.
I tried in another browser (Chrome) today without introducing any new changes and it was working without any issues.

CKEditor5 Get Plain Text

I know how to get data using CKEditor5 API as it's mentioned in documentation and on another SO post.
However, how could I get the Plain Text? I tried following but it returns nothing.
alert($(editorObj.element).val());
Interesting Note: Even following code returns nothing if TextArea is bind with CKEditor
alert( $("#editor").val());
But if I don't bind TextArea with CKEditor then it works fine.
Any solution or feedback would be highly appreciated.
CKEditor 5 does not expose such a method but you can use one of the utils of the #ckeditor/ckeditor5-clipboard package – viewToPlainText().
It's used by the clipboard feature to set the text/plain flavour in the clipboard when the user copies some content from the editor.
To use it you'll need to use CKEditor 5 from source (because this function is not exposed publicly). You can read about that in the CKEditor 5 Framework's Quick start guide.
You can use this method to stringify the entire editor's view:
import viewToPlainText from '#ckeditor/ckeditor5-clipboard/src/utils/viewtoplaintext';
import ClassicEditorBuild from '#ckeditor/ckeditor5-build-classic/src/ckeditor';
ClassicEditorBuild
.create( element )
.then( editor => {
// Will get the stringified content.
console.log( viewToPlainText( editor.editing.view.getRoot() ) );
} )
.catch( error => {
console.error( error.stack );
} )
Ok, I found a workaround:
var plainText = $(editorObj.getData()).text();
Until we get a proper solution or a method exposed by library, I hope this workaround will work.

Delete empty tags when testing with Jmeter

I am setting up some tests for Jmeter but i've encountered a problem which i've difficulty solving. In my JMS Point-to-Point i am using the following in the content section in JMeter content
i have a csv file with testdata for testing. All variables are written in this CSV with their value. for instance CoverageNr has a value of 1 then during testing Jmeter will use "< CoverageNr>1< /CovereageNr>" if it doesn't have a value it is empty: "< CoverageNr>< /CovereageNr>". And here we have the issue which i have. CoverageNr cannot be empty when used. What i want to achieve is when CoverageNr has no value, the tag "< CoverageNr>< /CovereageNr>" is not included in the test. I guess i have to make the entire tag configurable or use an if controller but so far to no avail. Can somebody give me some insights how to solve this problem?
I heard Groovy is a new Black so you can do this as follows:
Add JSR223 PreProcessor as a child of the JMS Point to Point sampler
Put the following code into the "Script" area:
import groovy.xml.XmlUtil
def content = sampler.getContent()
Node xml = new XmlParser().parseText(content)
cleanNode(xml)
def newContent = XmlUtil.serialize(xml)
sampler.setContent(newContent)
boolean cleanNode(Node node) {
node.attributes().with { a ->
a.findAll { !it.value }.each { a.remove(it.key) }
}
node.children().with { kids ->
kids.findAll { it instanceof Node ? !cleanNode(it) : false }
.each { kids.remove(it) }
}
node.attributes() || node.children() || node.text()
}
Demo:
Source: Remove null attributes and empty children from Node
Going forward I would recommend migrating to JSR223 Test Elements from Beanshell so in terms of performance it would be much better. Moreover Groovy has some nice sexy features, i.e. aforementioned XML processing so it will make your life easier
So i've solved the issue thanks to the other questions mentioned in the comment above and thanks to Dmitri T's answers for those questions. using a Beanshell Pre Processor did the trick with script being:
String data = sampler.getContent();
data = data.replaceAll("<CoverageN></CoverageN>","");
sampler.setContent(data);
Maybe it can be usefull for others.

Changing JS Indenting in Atom Keymap.cson

I'm trying to update my keymap.cson file so that JavaScript source is indented slightly differently. I do not want it to de-indent case and default statements in a switch.
By default, Atom will format this way:
switch(x) {
case 1:
//stuff
case 2:
//stuff
default:
//stuff
}
I like my case statements to be indented once (and //stuff to be indented once more).
So I'm trying to edit my keymap to make it format things this way:
switch(x) {
case 1:
//stuff
case 2:
//stuff
default:
//stuff
}
Unfortunately, whatever I try, I can't get it to stop decreasing the indentation as soon as I hit the spacebar after "case".
From the Keymaps Documentation it looks like putting the following coffeescript in my keymap.cson file should disable the default behavior and add my new behavior (which omits |case|default from the second line of the regular expression) should do the trick but I'm not sure why it's not working:
'.source.js':
'editor':
'decreaseIndentPattern': 'unset!'
'.source.js':
'editor':
'decreaseIndentPattern': '(?x)
^(.*\\*/)?\\s*(\\}|\\))
| ^\\s* else \\s*$
'
I think the original keymap is in the language-javascript package here.
I determined that this probably isn't technically a Keymap so it can't be overridden in keymap.cson. What I ended up doing was forking the language-javascript repo, making my change, uninstalling the original language-javascript package, and installing my custom fork instead.
Would still like to know if there is a way to update this at runtime instead; with this solution I will have to manually keep my fork up to date with any upstream changes.
Edit: Just an update for this particular change, it was actually accepted into the official language-javascript package via PR #36.

CodeMirror: make atomic range of token

I'm implementing CodeMirror to use as an editor for special files that require some syntax highlighting. I wrote my own parser for it, but now I face the following problem: there is a specific kind of token that I would always like to mark as an atomic range (with doc.markText).
I would have thought that there would exist some event handler for when tokens have been parsed, containing {line, ch} objects for its start and end positions. Reading through the docs, this does not seem to exist, so I would write my own, but the problem is that there seems to be no way to get any kind of position data whatsoever related to the parser.
What would be the best way to go about this? There are really crude ways like registering a change handler or iterating over the whole contents every few seconds, but of course this should be avoided.
I've forked the CodeMirror github repo and made an event that fires when a token gets parsed.
The syntax is this:
"tokenParsed" (instance: CodeMirror, start: {ch, line}, end: {ch, line}, style: String, text: String)
And then I handle it as follows:
myCodeMirror.on("tokenParsed", function(instance, start, end, style, text) {
if(!instance.findMarksAt(end).length) { //check if the mark doesn't exist yet
if(style && style.indexOf("param") > -1) {
instance.markText(start, end, {atomic: true});
}
}
});
If anyone wants this, see my repository.

Categories

Resources