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

Commit c2a4ace

Browse files
authored
Merge pull request #99 from wouterj/dragndrop
Reintroduce Drag 'n Drop
2 parents cf400aa + 7f28fc9 commit c2a4ace

File tree

3 files changed

+91
-12
lines changed

3 files changed

+91
-12
lines changed

Resources/assets/js/adapter/fancytree.js

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import Map from 'core-js/es6/map'
1111
import '../jquery.cmf_context_menu'
12-
import 'fancytree/jquery.fancytree.js'
12+
import 'fancytree/src/jquery.fancytree.js'
13+
import 'fancytree/src/jquery.fancytree.dnd.js'
1314
import 'fancytree/skin-win8/ui.fancytree.css'
1415
import '../../css/fontawesome-style.css'
1516

@@ -50,6 +51,21 @@ export class FancytreeAdapter {
5051
this.rootNode = options.root_node || '/';
5152
this.useCache = undefined === options.use_cache ? true : options.use_cache;
5253

54+
if (options.dnd && undefined == options.dnd.enabled) {
55+
options.dnd.enabled = true;
56+
}
57+
var alwaysTrueFunction = () => { return true };
58+
this.dndOptions = jQuery.extend({
59+
enabled: false,
60+
isNodeDraggable: alwaysTrueFunction,
61+
nodeAcceptsDraggable: alwaysTrueFunction
62+
}, options.dnd);
63+
64+
if (this.dndOptions.enabled && !options.request.move) {
65+
throw 'The move request needs to be configured when drag \'n drop is enabled, pass it using the `request.move` option.';
66+
}
67+
this.dndOptions.request = options.request.move;
68+
5369
// available actions (array)
5470
this.actions = new Array();
5571
// the Fancytree instance (FancytreeTree)
@@ -71,6 +87,19 @@ export class FancytreeAdapter {
7187

7288
this.$tree = $elem;
7389
var actions = this.actions;
90+
var parseUrl = function (url, node) {
91+
if (typeof url == 'object' && url.hasOwnProperty('data')) {
92+
return getPropertyFromString(url.data, node.descriptors);
93+
}
94+
95+
if (typeof url == 'function') {
96+
return url(requestNode);
97+
}
98+
99+
if (typeof url == 'string') {
100+
return url;
101+
}
102+
};
74103
var requestNodeToFancytreeNode = (requestNode) => {
75104
if (requestNode.length === 0) {
76105
return;
@@ -86,7 +115,8 @@ export class FancytreeAdapter {
86115
key: key,
87116
children: [],
88117
actions: {},
89-
refPath: requestNode.path.replace('\/', '/').replace('//', '/')
118+
refPath: requestNode.path.replace('\/', '/').replace('//', '/'),
119+
type: requestNode.payload_type
90120
};
91121

92122
this.pathKeyMap[fancytreeNode.refPath] = key;
@@ -97,10 +127,7 @@ export class FancytreeAdapter {
97127

98128
for (let actionName in actions) {
99129
var action = actions[actionName];
100-
var url = action.url;
101-
if (typeof action.url == 'object' && action.url.hasOwnProperty('data')) {
102-
url = getPropertyFromString(action.url.data, requestNode.descriptors);
103-
}
130+
var url = parseUrl(action.url, requestNode);
104131

105132
if (url === undefined) {
106133
continue;
@@ -135,7 +162,7 @@ export class FancytreeAdapter {
135162

136163
var requestData = this.requestData;
137164
var useCache = this.useCache;
138-
this.$tree.fancytree({
165+
var fancytreeOptions = {
139166
// the start data (root node + children)
140167
source: (useCache && cache.has(this.rootNode)) ? cache.get(this.rootNode) : requestData.load(this.rootNode),
141168

@@ -191,7 +218,59 @@ export class FancytreeAdapter {
191218

192219
// always show the active node
193220
activeVisible: true
194-
});
221+
};
222+
223+
if (this.dndOptions.enabled) {
224+
fancytreeOptions.extensions = ['dnd'];
225+
fancytreeOptions.dnd = {
226+
dragStart: (node, data) => {
227+
return this.dndOptions.isNodeDraggable(node, data);
228+
},
229+
dragEnter: (node, data) => {
230+
return this.dndOptions.nodeAcceptsDraggable(node, data);
231+
},
232+
dragExpand: (node, data) => {
233+
return true;
234+
},
235+
dragDrop: (node, data) => {
236+
var targetParentKeyPath = node.data.refPath;
237+
if ('over' != data.hitMode && 'child' != data.hitMode) {
238+
// a node at a specific place can still be a drop in a new parent
239+
targetParentKeyPath = node.parent.data.refPath;
240+
}
241+
var dropNodePath = data.otherNode.data.refPath;
242+
var targetPath = targetParentKeyPath + '/' + dropNodePath.substr(1 + dropNodePath.lastIndexOf('/'));
243+
244+
var oldIcon = data.otherNode.icon;
245+
data.otherNode.icon = 'fa fa-spinner fa-spin';
246+
data.otherNode.renderTitle();
247+
this.requestData.move(dropNodePath, targetPath)
248+
.done(function (responseData) {
249+
data.otherNode.remove();
250+
251+
if ('over' != data.hitMode) {
252+
node = node.parent;
253+
}
254+
255+
node.addChildren(requestNodeToFancytreeNode(responseData));
256+
})
257+
.fail(function (jqxhr, textStatus, errorThrown) {
258+
console.error(errorThrown);
259+
260+
node._error = { message: 'Failed to move the node.', details: errorThrown };
261+
node.renderStatus();
262+
263+
setTimeout(function () {
264+
node._error = null;
265+
node.renderStatus();
266+
}, 1000);
267+
})
268+
;
269+
}
270+
};
271+
}
272+
273+
this.$tree.fancytree(fancytreeOptions);
195274

196275
if (this.actions) {
197276
this.$tree.cmfContextMenu({

0 commit comments

Comments
 (0)