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

Commit fde040f

Browse files
committed
use sortable
1 parent 02afc2c commit fde040f

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

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

Lines changed: 38 additions & 16 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;
@@ -223,6 +224,20 @@ export class FancytreeAdapter {
223224
activeVisible: true
224225
};
225226

227+
if (this.sortableBy) {
228+
fancytreeOptions.sortChildren = (a, b) => {
229+
var firstItem = a.descriptors[this.sortableBy];
230+
var secondtItem = b.descriptors[this.sortableBy];
231+
if (firstItem == secondtItem) {
232+
return 0;
233+
} else if (firstItem < secondtItem) {
234+
return 1;
235+
} else {
236+
return -1;
237+
}
238+
};
239+
}
240+
226241
if (this.dndOptions.enabled) {
227242
fancytreeOptions.extensions = ['dnd'];
228243
fancytreeOptions.dnd = {
@@ -236,32 +251,39 @@ export class FancytreeAdapter {
236251
return true;
237252
},
238253
dragDrop: (node, data) => {
239-
var targetParentKeyPath = node.data.refPath;
254+
var dropedNode = data.otherNode;
255+
var dropedAtNode = data.node;
256+
257+
var dropNodePath = dropedNode.data.refPath;
258+
var dropedAtPath = dropedAtNode.data.refPath;
259+
var parenPath = '';
240260
if ('over' != data.hitMode && 'child' != data.hitMode) {
241261
// a node at a specific place can still be a drop in a new parent
242-
targetParentKeyPath = node.parent.data.refPath;
262+
parenPath = dropedAtNode.parent.data.refPath;
263+
} else {
264+
parenPath = dropedAtPath;
243265
}
244-
var dropNodePath = data.otherNode.data.refPath;
245-
var targetPath = targetParentKeyPath + '/' + dropNodePath.substr(1 + dropNodePath.lastIndexOf('/'));
266+
var targetPath = parenPath + '/' + dropNodePath.substr(1 + dropNodePath.lastIndexOf('/'));
246267

247-
data.otherNode.icon = 'fa fa-spinner fa-spin';
248-
data.otherNode.renderTitle();
249-
var moveNodeInTree = function (data, node, responseData) {
250-
data.otherNode.remove();
268+
dropedNode.icon = 'fa fa-spinner fa-spin';
269+
dropedNode.renderTitle();
270+
var moveNodeInTree = (responseData) => {
271+
dropedNode.remove();
251272
if ('over' != data.hitMode) {
252-
node = node.parent;
273+
dropedAtNode.addChildren(requestNodeToFancytreeNode(responseData));
274+
} else {
275+
dropedAtNode.parent.addChildren(requestNodeToFancytreeNode(responseData));
253276
}
254-
node.addChildren(requestNodeToFancytreeNode(responseData));
255277
};
256-
_this.requestData.move(dropNodePath, targetPath).done(function (responseData) {
257-
if (_this.dndOptions.reorder) {
258-
_this.requestData.reorder(targetParentKeyPath, targetPath, dropNodePath, data.hitMode).done(function (responseData) {
259-
moveNodeInTree(data, node, responseData);
278+
this.requestData.move(dropNodePath, targetPath).done((responseData) => {
279+
if (this.dndOptions.reorder) {
280+
this.requestData.reorder(parenPath, dropedAtPath, targetPath, data.hitMode).done((responseData) => {
281+
moveNodeInTree(responseData);
260282
});
261283
} else {
262-
moveNodeInTree(data, node, responseData);
284+
moveNodeInTree(responseData);
263285
}
264-
}).fail(function (jqxhr, textStatus, errorThrown) {
286+
}).fail((jqxhr, textStatus, errorThrown) => {
265287
console.error(errorThrown);
266288

267289
node._error = { message: 'Failed to move the node.', details: errorThrown };

0 commit comments

Comments
 (0)