The modal dialogs were implemented using BootstrapDialog which offered simple a simple template to create dynamic modal dialog boxes. I used a simple String operation to hide the add-as-child option.
var isLeaf = this.isLeafElement ? ' style="display:none;" ' : ' '; BootstrapDialog.show({ title: "Add new node", message: 'Title: <input id="title" type="text"><br>Type:<select id="type">' + this.getTypeOptionList("Object") + '</select><div ' + isLeaf + ' ><br>Add as child: <input type="checkbox" id="isChild" ></div> ', draggable: true, buttons: [{ label: 'Add', cssClass: "btn-primary", action: function (dialogRef) { self.model.addNode(dialogRef.getModalBody().find('#title').val()); dialogRef.close(); } }, { label: 'Cancel', action: function (dialogRef) { dialogRef.close(); } } ] });
The rules I assumed as optimum included:
- The options for the type of the root element are limited to Object and Array
- Only nodes of type Object or Array are allowed to add child nodes
- New nodes to the root element are added only as children nodes
- Sibling added to a node will be placed right after the triggered node
- Child added to a node will be placed as the last child of the triggered node
The initial goal was to update the schema, and to reload the container which will draw the tree-structure according to the updated schema. However, this will remove any current connectors attached to the container. Therefore dynamic node drawing was needed - which was kept for later consideration.
The schema-update was achieved using a recursive method, which was quite similar to the method I used to get the JSON path from a schema.
Initially, it would determine the relevant parent node of the expected new node and iterate the schema to find it. Adding as a child was not a big deal since it was to be added as the last child of the parent and simply updating the object as data[newAttribute] = newValue; would give the desired result.
Adding the sibling was the tough job since, it had to be added as the immediate sibling - to enhance user experience. The main reason was that JavaScript objects are unordered lists of properties. Therefore, in order to achieve the desired result of adding as the immediate sibling, I had to follow a String operation on parent data of the node.
The steps taken were:
Initially, it would determine the relevant parent node of the expected new node and iterate the schema to find it. Adding as a child was not a big deal since it was to be added as the last child of the parent and simply updating the object as data[newAttribute] = newValue; would give the desired result.
Adding the sibling was the tough job since, it had to be added as the immediate sibling - to enhance user experience. The main reason was that JavaScript objects are unordered lists of properties. Therefore, in order to achieve the desired result of adding as the immediate sibling, I had to follow a String operation on parent data of the node.
The steps taken were:
- Select the parent data set - the target
- Detect the triggered node in the target set as a String
- Concatenate the triggered node string with the new node string
- Replace with the previously detected string
- Parse the new string as a JSON
0 comments:
Post a Comment