1
1
/**
2
2
* angular-ui-sortable - This directive allows you to jQueryUI Sortable.
3
- * @version v0.17.0 - 2017-03-08
3
+ * @version v0.14.4 - 2017-04-15
4
4
* @link http://angular-ui.github.com
5
5
* @license MIT
6
6
*/
@@ -23,28 +23,14 @@ angular.module('ui.sortable', [])
23
23
'uiSortableConfig' , '$timeout' , '$log' ,
24
24
function ( uiSortableConfig , $timeout , $log ) {
25
25
return {
26
- require :'?ngModel' ,
26
+ require : '?ngModel' ,
27
27
scope : {
28
- ngModel :'=' ,
29
- uiSortable :'=' ,
30
- ////Expression bindings from html.
31
- create :'&uiSortableCreate' ,
32
- // helper:'&uiSortableHelper',
33
- start :'&uiSortableStart' ,
34
- activate :'&uiSortableActivate' ,
35
- // sort:'&uiSortableSort',
36
- // change:'&uiSortableChange',
37
- // over:'&uiSortableOver',
38
- // out:'&uiSortableOut',
39
- beforeStop :'&uiSortableBeforeStop' ,
40
- update :'&uiSortableUpdate' ,
41
- remove :'&uiSortableRemove' ,
42
- receive :'&uiSortableReceive' ,
43
- deactivate :'&uiSortableDeactivate' ,
44
- stop :'&uiSortableStop'
28
+ ngModel : '=' ,
29
+ uiSortable : '='
45
30
} ,
46
31
link : function ( scope , element , attrs , ngModel ) {
47
32
var savedNodes ;
33
+ var helper ;
48
34
49
35
function combineCallbacks ( first , second ) {
50
36
var firstIsFunc = typeof first === 'function' ;
@@ -121,7 +107,7 @@ angular.module('ui.sortable', [])
121
107
}
122
108
return ;
123
109
}
124
-
110
+
125
111
if ( ! defaultOptions ) {
126
112
defaultOptions = angular . element . ui . sortable ( ) . options ;
127
113
}
@@ -194,13 +180,12 @@ angular.module('ui.sortable', [])
194
180
return helperOption === 'clone' || ( typeof helperOption === 'function' && ui . item . sortable . isCustomHelperUsed ( ) ) ;
195
181
}
196
182
197
- function getSortingHelper ( element , ui , savedNodes ) {
183
+ function getSortingHelper ( element , ui /* , savedNodes*/ ) {
198
184
var result = null ;
199
185
if ( hasSortingHelper ( element , ui ) &&
200
186
element . sortable ( 'option' , 'appendTo' ) === 'parent' ) {
201
187
// The .ui-sortable-helper element (that's the default class name)
202
- // is placed last.
203
- result = savedNodes . last ( ) ;
188
+ result = helper ;
204
189
}
205
190
return result ;
206
191
}
@@ -210,13 +195,16 @@ angular.module('ui.sortable', [])
210
195
return ( / l e f t | r i g h t / ) . test ( item . css ( 'float' ) ) || ( / i n l i n e | t a b l e - c e l l / ) . test ( item . css ( 'display' ) ) ;
211
196
}
212
197
213
- function getElementContext ( elementScopes , element ) {
198
+ function getElementScope ( elementScopes , element ) {
199
+ var result = null ;
214
200
for ( var i = 0 ; i < elementScopes . length ; i ++ ) {
215
- var c = elementScopes [ i ] ;
216
- if ( c . element [ 0 ] === element [ 0 ] ) {
217
- return c ;
201
+ var x = elementScopes [ i ] ;
202
+ if ( x . element [ 0 ] === element [ 0 ] ) {
203
+ result = x . scope ;
204
+ break ;
218
205
}
219
206
}
207
+ return result ;
220
208
}
221
209
222
210
function afterStop ( e , ui ) {
@@ -241,19 +229,11 @@ angular.module('ui.sortable', [])
241
229
} ;
242
230
243
231
var callbacks = {
244
- create : null ,
245
- start : null ,
246
- activate : null ,
247
- // sort: null,
248
- // change: null,
249
- // over: null,
250
- // out: null,
251
- beforeStop : null ,
252
- update : null ,
253
- remove : null ,
254
232
receive : null ,
255
- deactivate : null ,
256
- stop : null
233
+ remove : null ,
234
+ start : null ,
235
+ stop : null ,
236
+ update : null
257
237
} ;
258
238
259
239
var wrappers = {
@@ -295,8 +275,7 @@ angular.module('ui.sortable', [])
295
275
ui . item . sortable = {
296
276
model : ngModel . $modelValue [ index ] ,
297
277
index : index ,
298
- source : element ,
299
- sourceList : ui . item . parent ( ) ,
278
+ source : ui . item . parent ( ) ,
300
279
sourceModel : ngModel . $modelValue ,
301
280
cancel : function ( ) {
302
281
ui . item . sortable . _isCanceled = true ;
@@ -313,33 +292,17 @@ angular.module('ui.sortable', [])
313
292
angular . forEach ( ui . item . sortable , function ( value , key ) {
314
293
ui . item . sortable [ key ] = undefined ;
315
294
} ) ;
316
- } ,
317
- _connectedSortables : [ ] ,
318
- _getElementContext : function ( element ) {
319
- return getElementContext ( this . _connectedSortables , element ) ;
320
295
}
321
296
} ;
322
297
} ;
323
298
324
299
callbacks . activate = function ( e , ui ) {
325
- var isSourceContext = ui . item . sortable . source === element ;
326
- var savedNodesOrigin = isSourceContext ?
327
- ui . item . sortable . sourceList :
328
- element ;
329
- var elementContext = {
330
- element : element ,
331
- scope : scope ,
332
- isSourceContext : isSourceContext ,
333
- savedNodesOrigin : savedNodesOrigin
334
- } ;
335
- // save the directive's scope so that it is accessible from ui.item.sortable
336
- ui . item . sortable . _connectedSortables . push ( elementContext ) ;
337
-
338
300
// We need to make a copy of the current element's contents so
339
301
// we can restore it after sortable has messed it up.
340
302
// This is inside activate (instead of start) in order to save
341
303
// both lists when dragging between connected lists.
342
- savedNodes = savedNodesOrigin . contents ( ) ;
304
+ savedNodes = element . contents ( ) ;
305
+ helper = ui . helper ;
343
306
344
307
// If this list has a placeholder (the connected lists won't),
345
308
// don't inlcude it in saved nodes.
@@ -348,20 +311,29 @@ angular.module('ui.sortable', [])
348
311
var excludes = getPlaceholderExcludesludes ( element , placeholder ) ;
349
312
savedNodes = savedNodes . not ( excludes ) ;
350
313
}
314
+
315
+ // save the directive's scope so that it is accessible from ui.item.sortable
316
+ var connectedSortables = ui . item . sortable . _connectedSortables || [ ] ;
317
+
318
+ connectedSortables . push ( {
319
+ element : element ,
320
+ scope : scope
321
+ } ) ;
322
+
323
+ ui . item . sortable . _connectedSortables = connectedSortables ;
351
324
} ;
352
325
353
326
callbacks . update = function ( e , ui ) {
354
327
// Save current drop position but only if this is not a second
355
328
// update that happens when moving between lists because then
356
329
// the value will be overwritten with the old value
357
- if ( ! ui . item . sortable . received ) {
330
+ if ( ! ui . item . sortable . received ) {
358
331
ui . item . sortable . dropindex = getItemIndex ( ui . item ) ;
359
- var droptarget = ui . item . closest ( '[ui-sortable], [data-ui-sortable], [x-ui-sortable]' ) ;
332
+ var droptarget = ui . item . parent ( ) ;
360
333
ui . item . sortable . droptarget = droptarget ;
361
- ui . item . sortable . droptargetList = ui . item . parent ( ) ;
362
334
363
- var droptargetContext = ui . item . sortable . _getElementContext ( droptarget ) ;
364
- ui . item . sortable . droptargetModel = droptargetContext . scope . ngModel ;
335
+ var droptargetScope = getElementScope ( ui . item . sortable . _connectedSortables , droptarget ) ;
336
+ ui . item . sortable . droptargetModel = droptargetScope . ngModel ;
365
337
366
338
// Cancel the sort (let ng-repeat do the sort for us)
367
339
// Don't cancel if this is the received list because it has
@@ -381,65 +353,61 @@ angular.module('ui.sortable', [])
381
353
// That way it will be garbage collected.
382
354
savedNodes = savedNodes . not ( sortingHelper ) ;
383
355
}
384
- var elementContext = ui . item . sortable . _getElementContext ( element ) ;
385
- savedNodes . appendTo ( elementContext . savedNodesOrigin ) ;
356
+ savedNodes . appendTo ( element ) ;
386
357
387
358
// If this is the target connected list then
388
359
// it's safe to clear the restored nodes since:
389
360
// update is currently running and
390
361
// stop is not called for the target list.
391
- if ( ui . item . sortable . received ) {
362
+ if ( ui . item . sortable . received ) {
392
363
savedNodes = null ;
393
364
}
394
365
395
366
// If received is true (an item was dropped in from another list)
396
367
// then we add the new item to this list otherwise wait until the
397
368
// stop event where we will know if it was a sort or item was
398
369
// moved here from another list
399
- if ( ui . item . sortable . received && ! ui . item . sortable . isCanceled ( ) ) {
370
+ if ( ui . item . sortable . received && ! ui . item . sortable . isCanceled ( ) ) {
400
371
scope . $apply ( function ( ) {
401
372
ngModel . $modelValue . splice ( ui . item . sortable . dropindex , 0 ,
402
373
ui . item . sortable . moved ) ;
403
374
} ) ;
404
- scope . $emit ( 'ui-sortable:moved' , ui ) ;
405
375
}
406
376
} ;
407
377
408
378
callbacks . stop = function ( e , ui ) {
409
379
// If the received flag hasn't be set on the item, this is a
410
380
// normal sort, if dropindex is set, the item was moved, so move
411
381
// the items in the list.
412
- var wasMoved = ( 'dropindex' in ui . item . sortable ) &&
413
- ! ui . item . sortable . isCanceled ( ) ;
414
-
415
- if ( wasMoved && ! ui . item . sortable . received ) {
382
+ if ( ! ui . item . sortable . received &&
383
+ ( 'dropindex' in ui . item . sortable ) &&
384
+ ! ui . item . sortable . isCanceled ( ) ) {
416
385
417
386
scope . $apply ( function ( ) {
418
387
ngModel . $modelValue . splice (
419
388
ui . item . sortable . dropindex , 0 ,
420
389
ngModel . $modelValue . splice ( ui . item . sortable . index , 1 ) [ 0 ] ) ;
421
390
} ) ;
422
- scope . $emit ( 'ui-sortable:moved' , ui ) ;
423
- } else if ( ! wasMoved &&
424
- ! angular . equals ( element . contents ( ) . toArray ( ) , savedNodes . toArray ( ) ) ) {
425
- // if the item was not moved
426
- // and the DOM element order has changed,
427
- // then restore the elements
391
+ } else {
392
+ // if the item was not moved, then restore the elements
428
393
// so that the ngRepeat's comment are correct.
429
-
430
- var sortingHelper = getSortingHelper ( element , ui , savedNodes ) ;
431
- if ( sortingHelper && sortingHelper . length ) {
432
- // Restore all the savedNodes except from the sorting helper element.
433
- // That way it will be garbage collected.
434
- savedNodes = savedNodes . not ( sortingHelper ) ;
394
+ if ( ( ! ( 'dropindex' in ui . item . sortable ) || ui . item . sortable . isCanceled ( ) ) &&
395
+ ! angular . equals ( element . contents ( ) , savedNodes ) ) {
396
+
397
+ var sortingHelper = getSortingHelper ( element , ui , savedNodes ) ;
398
+ if ( sortingHelper && sortingHelper . length ) {
399
+ // Restore all the savedNodes except from the sorting helper element.
400
+ // That way it will be garbage collected.
401
+ savedNodes = savedNodes . not ( sortingHelper ) ;
402
+ }
403
+ savedNodes . appendTo ( element ) ;
435
404
}
436
- var elementContext = ui . item . sortable . _getElementContext ( element ) ;
437
- savedNodes . appendTo ( elementContext . savedNodesOrigin ) ;
438
405
}
439
406
440
- // It's now safe to clear the savedNodes
407
+ // It's now safe to clear the savedNodes and helper
441
408
// since stop is the last callback.
442
409
savedNodes = null ;
410
+ helper = null ;
443
411
} ;
444
412
445
413
callbacks . receive = function ( e , ui ) {
@@ -467,21 +435,6 @@ angular.module('ui.sortable', [])
467
435
}
468
436
} ;
469
437
470
- // setup attribute handlers
471
- angular . forEach ( callbacks , function ( value , key ) {
472
- callbacks [ key ] = combineCallbacks ( callbacks [ key ] ,
473
- function ( ) {
474
- var attrHandler = scope [ key ] ;
475
- var attrHandlerFn ;
476
- if ( typeof attrHandler === 'function' &&
477
- ( 'uiSortable' + key . substring ( 0 , 1 ) . toUpperCase ( ) + key . substring ( 1 ) ) . length &&
478
- typeof ( attrHandlerFn = attrHandler ( ) ) === 'function' ) {
479
- attrHandlerFn . apply ( this , arguments ) ;
480
- }
481
- } ) ;
482
- } ) ;
483
-
484
-
485
438
wrappers . helper = function ( inner ) {
486
439
if ( inner && typeof inner === 'function' ) {
487
440
return function ( e , item ) {
@@ -490,8 +443,7 @@ angular.module('ui.sortable', [])
490
443
item . sortable = {
491
444
model : ngModel . $modelValue [ index ] ,
492
445
index : index ,
493
- source : element ,
494
- sourceList : item . parent ( ) ,
446
+ source : item . parent ( ) ,
495
447
sourceModel : ngModel . $modelValue ,
496
448
_restore : function ( ) {
497
449
angular . forEach ( item . sortable , function ( value , key ) {
@@ -517,7 +469,7 @@ angular.module('ui.sortable', [])
517
469
var sortableWidgetInstance = getSortableWidgetInstance ( element ) ;
518
470
if ( ! ! sortableWidgetInstance ) {
519
471
var optsDiff = patchUISortableOptions ( newVal , oldVal , sortableWidgetInstance ) ;
520
-
472
+
521
473
if ( optsDiff ) {
522
474
element . sortable ( 'option' , optsDiff ) ;
523
475
}
@@ -533,7 +485,7 @@ angular.module('ui.sortable', [])
533
485
} else {
534
486
$log . info ( 'ui.sortable: ngModel not provided!' , element ) ;
535
487
}
536
-
488
+
537
489
// Create sortable
538
490
element . sortable ( opts ) ;
539
491
}
0 commit comments