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

Commit 90d2672

Browse files
authored
Merge pull request #496 from angular-ui/v0.16.0-dev
Merge branch 'v0.16.x-dev'
2 parents 57c5021 + 7c3254c commit 90d2672

8 files changed

+357
-35
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: node_js
22
node_js:
33
- '0.10'
4+
addons:
5+
firefox: "latest"
46
before_install:
57
- export DISPLAY=:99.0
68
- sh -e /etc/init.d/xvfb start

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ update: function(event, ui) {
160160

161161
**Single sortable** [demo](http://codepen.io/thgreasi/pen/QKmWGj)
162162
```
163+
create
164+
165+
/* dragging starts */
166+
helper
163167
start
164168
activate
165169
@@ -174,6 +178,11 @@ stop
174178
**Connected sortables** [demo](http://codepen.io/thgreasi/pen/YGazpJ)
175179

176180
```
181+
list A: create
182+
list B: create
183+
184+
/* dragging starts from sortable A to B */
185+
list A: helper
177186
list A: start
178187
list B: activate
179188
list A: activate
@@ -199,6 +208,30 @@ list A: stop
199208

200209
For more details about the events check the [jQueryUI API documentation](http://api.jqueryui.com/sortable/).
201210

211+
## Integrating with directives doing transclusion
212+
Wrap the transclusion directive element with the ui-sortable directive and set the `items` to target your `ng-repeat`ed elements. Following best practices, it is also highly recommended that you add a `track by` expression to your `ng-repeat`. [Angular Meterial example](http://codepen.io/thgreasi/pen/NbyLVK).
213+
214+
```js
215+
myAppModule.controller('MyController', function($scope) {
216+
$scope.items = ["One", "Two", "Three"];
217+
218+
$scope.sortableOptions = {
219+
items: '.sortable-item'
220+
// It is suggested to use the most specific cssselector you can,
221+
// after analyzing the DOM elements generated by the transclusion directive
222+
// eg: items: '> .transclusionLvl1 > .transclusionLvl2 > .sortable-item'
223+
};
224+
});
225+
```
226+
227+
```html
228+
<div ui-sortable="sortableOptions" ng-model="items">
229+
<a-transclusion-directive>
230+
<div ng-repeat="item in items" class="sortable-item">{{ item }}</div>
231+
</a-transclusion-directive>
232+
</div>
233+
```
234+
202235
## Examples
203236

204237
- [Simple Demo](http://codepen.io/thgreasi/pen/wzmvgw)
@@ -224,6 +257,7 @@ For more details about the events check the [jQueryUI API documentation](http://
224257
## Integrations
225258
- [firebase](http://codepen.io/thgreasi/pen/repEZg?editors=0010)
226259
- [ui.bootstrap.accordion](http://plnkr.co/edit/TGIeeEbbvJwpJ3WRqo2z?p=preview)
260+
- [Angular Meterial](http://codepen.io/thgreasi/pen/NbyLVK) (thanks yenoh2)
227261
- [Asynchronous loading jQuery+jQueryUI with crisbeto/angular-ui-sortable-loader](https://github.com/crisbeto/angular-ui-sortable-loader)
228262

229263
## Reporting Issues

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-sortable",
3-
"version": "0.15.1",
3+
"version": "0.16.0",
44
"description": "This directive allows you to jQueryUI Sortable.",
55
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
66
"license": "MIT",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-ui-sortable",
3-
"version": "0.15.1",
3+
"version": "0.16.0",
44
"description": "This directive allows you to jQueryUI Sortable.",
55
"author": "https://github.com/angular-ui/ui-sortable/graphs/contributors",
66
"license": "MIT",

src/sortable.js

+37-28
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ angular.module('ui.sortable', [])
9797
}
9898
return;
9999
}
100-
100+
101101
if (!defaultOptions) {
102102
defaultOptions = angular.element.ui.sortable().options;
103103
}
@@ -186,16 +186,13 @@ angular.module('ui.sortable', [])
186186
return (/left|right/).test(item.css('float')) || (/inline|table-cell/).test(item.css('display'));
187187
}
188188

189-
function getElementScope(elementScopes, element) {
190-
var result = null;
189+
function getElementContext(elementScopes, element) {
191190
for (var i = 0; i < elementScopes.length; i++) {
192-
var x = elementScopes[i];
193-
if (x.element[0] === element[0]) {
194-
result = x.scope;
195-
break;
191+
var c = elementScopes[i];
192+
if (c.element[0] === element[0]) {
193+
return c;
196194
}
197195
}
198-
return result;
199196
}
200197

201198
function afterStop(e, ui) {
@@ -266,7 +263,8 @@ angular.module('ui.sortable', [])
266263
ui.item.sortable = {
267264
model: ngModel.$modelValue[index],
268265
index: index,
269-
source: ui.item.parent(),
266+
source: element,
267+
sourceList: ui.item.parent(),
270268
sourceModel: ngModel.$modelValue,
271269
cancel: function () {
272270
ui.item.sortable._isCanceled = true;
@@ -283,16 +281,33 @@ angular.module('ui.sortable', [])
283281
angular.forEach(ui.item.sortable, function(value, key) {
284282
ui.item.sortable[key] = undefined;
285283
});
284+
},
285+
_connectedSortables: [],
286+
_getElementContext: function (element) {
287+
return getElementContext(this._connectedSortables, element);
286288
}
287289
};
288290
};
289291

290292
callbacks.activate = function(e, ui) {
293+
var isSourceContext = ui.item.sortable.source === element;
294+
var savedNodesOrigin = isSourceContext ?
295+
ui.item.sortable.sourceList :
296+
element;
297+
var elementContext = {
298+
element: element,
299+
scope: scope,
300+
isSourceContext: isSourceContext,
301+
savedNodesOrigin: savedNodesOrigin
302+
};
303+
// save the directive's scope so that it is accessible from ui.item.sortable
304+
ui.item.sortable._connectedSortables.push(elementContext);
305+
291306
// We need to make a copy of the current element's contents so
292307
// we can restore it after sortable has messed it up.
293308
// This is inside activate (instead of start) in order to save
294309
// both lists when dragging between connected lists.
295-
savedNodes = element.contents();
310+
savedNodes = savedNodesOrigin.contents();
296311

297312
// If this list has a placeholder (the connected lists won't),
298313
// don't inlcude it in saved nodes.
@@ -301,16 +316,6 @@ angular.module('ui.sortable', [])
301316
var excludes = getPlaceholderExcludesludes(element, placeholder);
302317
savedNodes = savedNodes.not(excludes);
303318
}
304-
305-
// save the directive's scope so that it is accessible from ui.item.sortable
306-
var connectedSortables = ui.item.sortable._connectedSortables || [];
307-
308-
connectedSortables.push({
309-
element: element,
310-
scope: scope
311-
});
312-
313-
ui.item.sortable._connectedSortables = connectedSortables;
314319
};
315320

316321
callbacks.update = function(e, ui) {
@@ -319,11 +324,12 @@ angular.module('ui.sortable', [])
319324
// the value will be overwritten with the old value
320325
if(!ui.item.sortable.received) {
321326
ui.item.sortable.dropindex = getItemIndex(ui.item);
322-
var droptarget = ui.item.parent();
327+
var droptarget = ui.item.closest('[ui-sortable]');
323328
ui.item.sortable.droptarget = droptarget;
329+
ui.item.sortable.droptargetList = ui.item.parent();
324330

325-
var droptargetScope = getElementScope(ui.item.sortable._connectedSortables, droptarget);
326-
ui.item.sortable.droptargetModel = droptargetScope.ngModel;
331+
var droptargetContext = ui.item.sortable._getElementContext(droptarget);
332+
ui.item.sortable.droptargetModel = droptargetContext.scope.ngModel;
327333

328334
// Cancel the sort (let ng-repeat do the sort for us)
329335
// Don't cancel if this is the received list because it has
@@ -343,7 +349,8 @@ angular.module('ui.sortable', [])
343349
// That way it will be garbage collected.
344350
savedNodes = savedNodes.not(sortingHelper);
345351
}
346-
savedNodes.appendTo(element);
352+
var elementContext = ui.item.sortable._getElementContext(element);
353+
savedNodes.appendTo(elementContext.savedNodesOrigin);
347354

348355
// If this is the target connected list then
349356
// it's safe to clear the restored nodes since:
@@ -392,7 +399,8 @@ angular.module('ui.sortable', [])
392399
// That way it will be garbage collected.
393400
savedNodes = savedNodes.not(sortingHelper);
394401
}
395-
savedNodes.appendTo(element);
402+
var elementContext = ui.item.sortable._getElementContext(element);
403+
savedNodes.appendTo(elementContext.savedNodesOrigin);
396404
}
397405

398406
// It's now safe to clear the savedNodes
@@ -433,7 +441,8 @@ angular.module('ui.sortable', [])
433441
item.sortable = {
434442
model: ngModel.$modelValue[index],
435443
index: index,
436-
source: item.parent(),
444+
source: element,
445+
sourceList: item.parent(),
437446
sourceModel: ngModel.$modelValue,
438447
_restore: function () {
439448
angular.forEach(item.sortable, function(value, key) {
@@ -459,7 +468,7 @@ angular.module('ui.sortable', [])
459468
var sortableWidgetInstance = getSortableWidgetInstance(element);
460469
if (!!sortableWidgetInstance) {
461470
var optsDiff = patchUISortableOptions(newVal, oldVal, sortableWidgetInstance);
462-
471+
463472
if (optsDiff) {
464473
element.sortable('option', optsDiff);
465474
}
@@ -475,7 +484,7 @@ angular.module('ui.sortable', [])
475484
} else {
476485
$log.info('ui.sortable: ngModel not provided!', element);
477486
}
478-
487+
479488
// Create sortable
480489
element.sortable(opts);
481490
}

0 commit comments

Comments
 (0)