@@ -51,6 +51,7 @@ export class FancytreeAdapter {
51
51
this . rootNode = options . root_node || '/' ;
52
52
this . useCache = undefined === options . use_cache ? true : options . use_cache ;
53
53
this . boundToInput = false ;
54
+ this . sortableBy = undefined == options . sortableBy ? false : options . sortableBy ;
54
55
55
56
if ( options . dnd && undefined == options . dnd . enabled ) {
56
57
options . dnd . enabled = true ;
@@ -128,6 +129,10 @@ export class FancytreeAdapter {
128
129
fancytreeNode . icon = requestNode . descriptors . icon ;
129
130
}
130
131
132
+ if ( requestNode . descriptors . hasOwnProperty ( 'position' ) ) {
133
+ fancytreeNode . position = requestNode . descriptors . position ;
134
+ }
135
+
131
136
for ( let actionName in actions ) {
132
137
var action = actions [ actionName ] ;
133
138
var url = parseUrl ( action . url , requestNode ) ;
@@ -223,6 +228,20 @@ export class FancytreeAdapter {
223
228
activeVisible : true
224
229
} ;
225
230
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
+
226
245
if ( this . dndOptions . enabled ) {
227
246
fancytreeOptions . extensions = [ 'dnd' ] ;
228
247
fancytreeOptions . dnd = {
@@ -236,39 +255,45 @@ export class FancytreeAdapter {
236
255
return true ;
237
256
} ,
238
257
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 ) ;
272
297
}
273
298
} ;
274
299
}
0 commit comments