@@ -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 ;
@@ -223,6 +224,20 @@ export class FancytreeAdapter {
223
224
activeVisible : true
224
225
} ;
225
226
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
+
226
241
if ( this . dndOptions . enabled ) {
227
242
fancytreeOptions . extensions = [ 'dnd' ] ;
228
243
fancytreeOptions . dnd = {
@@ -236,32 +251,39 @@ export class FancytreeAdapter {
236
251
return true ;
237
252
} ,
238
253
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 = '' ;
240
260
if ( 'over' != data . hitMode && 'child' != data . hitMode ) {
241
261
// 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 ;
243
265
}
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 ( '/' ) ) ;
246
267
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 ( ) ;
251
272
if ( 'over' != data . hitMode ) {
252
- node = node . parent ;
273
+ dropedAtNode . addChildren ( requestNodeToFancytreeNode ( responseData ) ) ;
274
+ } else {
275
+ dropedAtNode . parent . addChildren ( requestNodeToFancytreeNode ( responseData ) ) ;
253
276
}
254
- node . addChildren ( requestNodeToFancytreeNode ( responseData ) ) ;
255
277
} ;
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 ) ;
260
282
} ) ;
261
283
} else {
262
- moveNodeInTree ( data , node , responseData ) ;
284
+ moveNodeInTree ( responseData ) ;
263
285
}
264
- } ) . fail ( function ( jqxhr , textStatus , errorThrown ) {
286
+ } ) . fail ( ( jqxhr , textStatus , errorThrown ) => {
265
287
console . error ( errorThrown ) ;
266
288
267
289
node . _error = { message : 'Failed to move the node.' , details : errorThrown } ;
0 commit comments