Some refactoring

This commit is contained in:
jos 2018-08-06 14:24:24 +02:00
parent ab8e6322d2
commit 3a439ba795
2 changed files with 11 additions and 29 deletions

View File

@ -538,24 +538,21 @@ Node.prototype.getNodePath = function () {
*/ */
Node.prototype.clone = function() { Node.prototype.clone = function() {
var clone = new Node(this.editor); var clone = new Node(this.editor);
clone.type = this.type;
clone.field = this.field; for (var prop in this) {
clone.fieldInnerText = this.fieldInnerText; // we don't clone the DOM elements, that would give issues with what's currently in the DOM
clone.fieldEditable = this.fieldEditable; if (this.hasOwnProperty(prop) && prop !== 'dom') {
clone.value = this.value; clone[prop] = this[prop];
clone.valueInnerText = this.valueInnerText; }
clone.expanded = this.expanded; }
clone.visibleChilds = this.visibleChilds;
if (this.childs) { if (this.childs) {
// an object or array // an object or array
var cloneChilds = []; clone.childs = this.childs.map(function (child) {
this.childs.forEach(function (child) {
var childClone = child.clone(); var childClone = child.clone();
childClone.setParent(clone); childClone.setParent(clone);
cloneChilds.push(childClone); return childClone;
}); });
clone.childs = cloneChilds;
} }
else { else {
// a value // a value
@ -3262,16 +3259,12 @@ Node.prototype.sort = function (path, direction) {
* @param {*} newValue * @param {*} newValue
*/ */
Node.prototype.update = function (newValue) { Node.prototype.update = function (newValue) {
// copy the childs array (the old one will be kept for an undo action
var oldType = this.type;
var oldValue = this.getValue(); var oldValue = this.getValue();
this.setValue(newValue); this.setValue(newValue);
this.editor._onAction('transform', { this.editor._onAction('transform', {
node: this, node: this,
oldType: oldType,
newType: this.type,
oldValue: oldValue, oldValue: oldValue,
newValue: newValue newValue: newValue
}); });
@ -3289,11 +3282,6 @@ Node.prototype.transform = function (query) {
this.hideChilds(); // sorting is faster when the childs are not attached to the dom this.hideChilds(); // sorting is faster when the childs are not attached to the dom
// copy the childs array (the old one will be kept for an undo action
var oldType = this.type;
var oldChilds = this.childs;
this.childs = this.childs.concat();
try { try {
// apply the JMESPath query // apply the JMESPath query
var oldValue = this.getValue(); var oldValue = this.getValue();
@ -3303,13 +3291,8 @@ Node.prototype.transform = function (query) {
this.editor._onAction('transform', { this.editor._onAction('transform', {
node: this, node: this,
oldType: oldType,
newType: this.type,
oldValue: oldValue, oldValue: oldValue,
newValue: newValue, newValue: newValue
oldChilds: oldChilds,
newChilds: this.childs
// TODO: use oldChilds/newChilds in history or clean it up
}); });
this.showChilds(); this.showChilds();

View File

@ -348,8 +348,7 @@ treemode.focus = function () {
*/ */
treemode.clear = function () { treemode.clear = function () {
if (this.node) { if (this.node) {
this.node.collapse(); this.node.hide();
this.tbody.removeChild(this.node.getDom());
delete this.node; delete this.node;
} }