9
9
10
10
import Map from 'core-js/es6/map'
11
11
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'
13
14
import 'fancytree/skin-win8/ui.fancytree.css'
14
15
import '../../css/fontawesome-style.css'
15
16
@@ -50,6 +51,21 @@ export class FancytreeAdapter {
50
51
this . rootNode = options . root_node || '/' ;
51
52
this . useCache = undefined === options . use_cache ? true : options . use_cache ;
52
53
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
+
53
69
// available actions (array)
54
70
this . actions = new Array ( ) ;
55
71
// the Fancytree instance (FancytreeTree)
@@ -71,6 +87,19 @@ export class FancytreeAdapter {
71
87
72
88
this . $tree = $elem ;
73
89
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
+ } ;
74
103
var requestNodeToFancytreeNode = ( requestNode ) => {
75
104
if ( requestNode . length === 0 ) {
76
105
return ;
@@ -86,7 +115,8 @@ export class FancytreeAdapter {
86
115
key : key ,
87
116
children : [ ] ,
88
117
actions : { } ,
89
- refPath : requestNode . path . replace ( '\/' , '/' ) . replace ( '//' , '/' )
118
+ refPath : requestNode . path . replace ( '\/' , '/' ) . replace ( '//' , '/' ) ,
119
+ type : requestNode . payload_type
90
120
} ;
91
121
92
122
this . pathKeyMap [ fancytreeNode . refPath ] = key ;
@@ -97,10 +127,7 @@ export class FancytreeAdapter {
97
127
98
128
for ( let actionName in actions ) {
99
129
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 ) ;
104
131
105
132
if ( url === undefined ) {
106
133
continue ;
@@ -135,7 +162,7 @@ export class FancytreeAdapter {
135
162
136
163
var requestData = this . requestData ;
137
164
var useCache = this . useCache ;
138
- this . $tree . fancytree ( {
165
+ var fancytreeOptions = {
139
166
// the start data (root node + children)
140
167
source : ( useCache && cache . has ( this . rootNode ) ) ? cache . get ( this . rootNode ) : requestData . load ( this . rootNode ) ,
141
168
@@ -191,7 +218,59 @@ export class FancytreeAdapter {
191
218
192
219
// always show the active node
193
220
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 ) ;
195
274
196
275
if ( this . actions ) {
197
276
this . $tree . cmfContextMenu ( {
0 commit comments