Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 3fcb117

Browse files
Merge pull request #147 from symfony-cmf/fix_dnd
enable way to trigger reorder after move
2 parents eca77ed + 712a6b0 commit 3fcb117

File tree

2 files changed

+61
-36
lines changed

2 files changed

+61
-36
lines changed

src/Resources/assets/js/adapter/fancytree.js

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class FancytreeAdapter {
5151
this.rootNode = options.root_node || '/';
5252
this.useCache = undefined === options.use_cache ? true : options.use_cache;
5353
this.boundToInput = false;
54+
this.sortableBy = undefined == options.sortableBy ? false : options.sortableBy;
5455

5556
if (options.dnd && undefined == options.dnd.enabled) {
5657
options.dnd.enabled = true;
@@ -128,6 +129,10 @@ export class FancytreeAdapter {
128129
fancytreeNode.icon = requestNode.descriptors.icon;
129130
}
130131

132+
if(requestNode.descriptors.hasOwnProperty('position')) {
133+
fancytreeNode.position = requestNode.descriptors.position;
134+
}
135+
131136
for (let actionName in actions) {
132137
var action = actions[actionName];
133138
var url = parseUrl(action.url, requestNode);
@@ -223,6 +228,20 @@ export class FancytreeAdapter {
223228
activeVisible: true
224229
};
225230

231+
if (this.sortableBy) {
232+
fancytreeOptions.sortChildren = (a, b) => {
233+
var current = a.data[this.sortableBy];
234+
var next = b.data[this.sortableBy];
235+
if (current == next) {
236+
return 0;
237+
} else if (current < next) {
238+
return -1;
239+
} else {
240+
return 1;
241+
}
242+
};
243+
}
244+
226245
if (this.dndOptions.enabled) {
227246
fancytreeOptions.extensions = ['dnd'];
228247
fancytreeOptions.dnd = {
@@ -236,39 +255,45 @@ export class FancytreeAdapter {
236255
return true;
237256
},
238257
dragDrop: (node, data) => {
239-
var targetParentKeyPath = node.data.refPath;
240-
if ('over' != data.hitMode && 'child' != data.hitMode) {
241-
// a node at a specific place can still be a drop in a new parent
242-
targetParentKeyPath = node.parent.data.refPath;
243-
}
244-
var dropNodePath = data.otherNode.data.refPath;
245-
var targetPath = targetParentKeyPath + '/' + dropNodePath.substr(1 + dropNodePath.lastIndexOf('/'));
246-
247-
var oldIcon = data.otherNode.icon;
248-
data.otherNode.icon = 'fa fa-spinner fa-spin';
249-
data.otherNode.renderTitle();
250-
this.requestData.move(dropNodePath, targetPath)
251-
.done(function (responseData) {
252-
data.otherNode.remove();
253-
254-
if ('over' != data.hitMode) {
255-
node = node.parent;
256-
}
257-
258-
node.addChildren(requestNodeToFancytreeNode(responseData));
259-
})
260-
.fail(function (jqxhr, textStatus, errorThrown) {
261-
console.error(errorThrown);
262-
263-
node._error = { message: 'Failed to move the node.', details: errorThrown };
264-
node.renderStatus();
265-
266-
setTimeout(function () {
267-
node._error = null;
268-
node.renderStatus();
269-
}, 1000);
270-
})
271-
;
258+
let dropedNode = data.otherNode;
259+
let dropedAtNode = data.node;
260+
261+
let dropNodePath = dropedNode.data.refPath;
262+
let dropedAtPath = dropedAtNode.data.refPath;
263+
let positionBefore = 'over' != data.hitMode && 'child' != data.hitMode;
264+
let parentNode = positionBefore ? dropedAtNode.parent : dropedAtNode;
265+
let parenPath = parentNode.data.refPath;
266+
let targetPath = parenPath + '/' + dropNodePath.substr(1 + dropNodePath.lastIndexOf('/'));
267+
268+
dropedNode.icon = 'fa fa-spinner fa-spin';
269+
dropedNode.renderTitle();
270+
271+
let moveNodeInTree = (responseData) => {
272+
dropedNode.remove();
273+
parentNode.addChildren(requestNodeToFancytreeNode(responseData));
274+
};
275+
let onError = (jqxhr, textStatus, errorThrown) => {
276+
node._error = { message: 'Failed to move the node.', details: errorThrown };
277+
node.renderStatus();
278+
console.error(errorThrown);
279+
280+
setTimeout(function () {
281+
node._error = null;
282+
node.renderStatus();
283+
}, 1000);
284+
};
285+
this.requestData.move(dropNodePath, targetPath).done((responseData) => {
286+
if (this.dndOptions.reorder) {
287+
this.requestData.reorder(parenPath, dropedAtPath, targetPath, data.hitMode).done((responseData) => {
288+
moveNodeInTree(responseData);
289+
if (fancytreeOptions.hasOwnProperty('sortChildren')) {
290+
parentNode.sortChildren(fancytreeOptions.sortChildren, true);
291+
}
292+
}).fail(onError);
293+
} else {
294+
moveNodeInTree(responseData);
295+
}
296+
}).fail(onError);
272297
}
273298
};
274299
}

0 commit comments

Comments
 (0)