Skip to content
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

Cannot discern if user cancels edit for nodes #1160

Closed
Slogo opened this issue Jun 24, 2015 · 3 comments
Closed

Cannot discern if user cancels edit for nodes #1160

Slogo opened this issue Jun 24, 2015 · 3 comments

Comments

@Slogo
Copy link

Slogo commented Jun 24, 2015

The edit function for a node to rename a node has support for a user to cancel their edit. When this happens the old value of the node is restored and the input is then blurred.

This works great when a user is editing an existing node, the rename_node event fires and the old and new values of the name are the same so that information can be used to indicate a canceled action. Typically in this case there is no further action that needs to be taken.

However, there are cases where you want the default text to be a valid input that triggers an action (like persisting the node to the server). In those cases it's impossible to tell if the user canceled the edit with escape or committed the default value using enter.

All the necessary information is tracked, but neither the edit callback nor the rename_node event are privy to this information. As far as I can tell there's no way to discern that information cleanly outside of jsTree.

@vakata
Copy link
Owner

vakata commented Jun 25, 2015

Hi,
Thanks for the detailed report.

The edit callback will always contain information about the status of the rename - if it was canceled or completed.

I guess you are trying to persist newly created nodes when the rename_node event is fired (and you are probably creating nodes with the contextmenu plugin, as it is the only place where you can't pass a callback to edit - well, you can, but if you are using the defaults the callback is not specified)?

jsTree depends on a completely different concept (like a file system):

  • a node is created (persisted) and assigned a default name
  • put into edit mode
  • if the user confirms the edit, the node will be renamed
  • if the user cancels the edit, the node will still be there with its default name

So for a list of possible solutions (I do not know if my guesses about what you are trying to achieve are correct):

  1. Do not use the default create in the contextmenu, then you can always rely on the second parameter of the edit callback

  2. Since not all new nodes will have an edit box or be renamed, you'd be better off persisting the node when create_node is fired.

  3. There is also the more low-level set_text event - if you are building something more complicated.

  4. And if you want full control and are building something very complex out of the scope of jstree you can always use event delegation and catch the blur event (which jQuery will make bubble up) or any other events like keypresses, etc (the selector you need is .jstree-rename-input)

If the above solutions are not useful to you, please tell me what you are trying to achieve and share any ideas you may have about a solution that I can add to jsTree. I will implement it if it is useful and does not duplicate existing functionality.

I am closing this until further information.

Best regards,
Ivan

@vakata vakata closed this as completed Jun 25, 2015
@Slogo
Copy link
Author

Slogo commented Jun 25, 2015

Hi thanks for the prompt reply. Let me try to clarify. The workflow I am trying to capture is this:

  • User clicks "Create Node"
  • A new node in edit mode is presented to the user with generic default text "New Node"
  • User edits the name (or not)
    • If user hits enter the node is created and persisted to the server
    • If user hits escape the operation is canceled and no new node is created

I don't wish to persist the node until the user confirms their action via the enter key. Even if I wanted to in this particular case I can't because the node name must be unique so the operation may not even succeed.

JsTree provides nearly all the functionality required to do this, but just not quite. The callback from the edit function does not include an indication of whether or not the edit was canceled.

I don't see where the edit callback contains information about whether or not the action is canceled. Looking at the code I see the edit callback being passed the relevant node and a boolean that indicates whether or not the rename succeeded. But even if the user cancels the action the rename has 'succeeded'. The only time the rename fails is if the operation is not allowed on the tree.

In the edit function the second parameter is the value of nv as set here:

nv = !!this.rename_node(obj, f ? $('<div></div>').text(v).text() : $('<div></div>').append($.parseHTML(v)).html());

but rename_node only returns false in this case:

            if(!this.check("rename_node", obj, this.get_parent(obj), val)) {
                this.settings.core.error.call(this, this._data.core.last_error);
                return false;
            }

which is not related to the user's actions.

The rename event includes both the old text and new text. In many cases you can simply do:

if data.old === data.text
  //Persist Change
else
 //Ignore Change

But that does not allow for the nuance of being allowed to persist a node with the defaultText or cancel the creation of the node if the user has canceled their action.

To help illustrate the problem here is a workaround I've developed that I would really prefer to not have to use:

  • Create the new node with blank text
  • Call edit without any default text
  • Use jquery to set the input's value to "New Node" (i.e $('.jstree-rename-input').val("New Node"))
  • In the rename event I can now check if val.text === val.old.

If the user canceled the action both values in the callback will be empty strings, if they confirmed without typing the new value will be "New Node". Using this information I can now persist the created node only when the user has not hit escape to cancel the action.

I'd really prefer to not use my workaround if possible. Ideally I would think this sort of information would be communicated by the arguments passed to the edit callback.

@vakata
Copy link
Owner

vakata commented Jun 25, 2015

I understand what you need now - thank you, and sorry had to make you clarify. I will add this to the edit callback. I will close this issue and let you know as soon as I am done.

Best regards,
Ivan

@vakata vakata reopened this Jun 25, 2015
@vakata vakata closed this as completed in 40ab93b Jun 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants