|
|
@ -23,61 +23,60 @@ |
|
|
|
* @property {number} newIndex after sort |
|
|
|
* @property {number} newIndex after sort |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var expando = 'Sortable:ng-sortable'; |
|
|
|
|
|
|
|
|
|
|
|
angular.module('ng-sortable', []) |
|
|
|
angular.module('ng-sortable', []) |
|
|
|
.constant('ngSortableVersion', '0.3.7') |
|
|
|
.constant('ngSortableVersion', '0.4.0') |
|
|
|
.constant('ngSortableConfig', {}) |
|
|
|
.constant('ngSortableConfig', {}) |
|
|
|
.directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) { |
|
|
|
.directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) { |
|
|
|
var removed, |
|
|
|
var removed, |
|
|
|
nextSibling; |
|
|
|
nextSibling, |
|
|
|
|
|
|
|
getSourceFactory = function getSourceFactory(el, scope) { |
|
|
|
|
|
|
|
var ngRepeat = [].filter.call(el.childNodes, function (node) { |
|
|
|
|
|
|
|
return ( |
|
|
|
|
|
|
|
(node.nodeType === 8) && |
|
|
|
|
|
|
|
(node.nodeValue.indexOf('ngRepeat:') !== -1) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
})[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!ngRepeat) { |
|
|
|
|
|
|
|
// Without ng-repeat
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tests: http://jsbin.com/kosubutilo/1/edit?js,output
|
|
|
|
|
|
|
|
ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var itemsExpr = $parse(ngRepeat[2]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return function () { |
|
|
|
|
|
|
|
return itemsExpr(scope.$parent) || []; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Export
|
|
|
|
// Export
|
|
|
|
return { |
|
|
|
return { |
|
|
|
restrict: 'AC', |
|
|
|
restrict: 'AC', |
|
|
|
scope: { ngSortable: "=?" }, |
|
|
|
scope: { ngSortable: "=?" }, |
|
|
|
link: function (scope, $el, attrs) { |
|
|
|
link: function (scope, $el) { |
|
|
|
|
|
|
|
|
|
|
|
function getSource(el) { |
|
|
|
|
|
|
|
var ngRepeat = [].filter.call(el.childNodes, function (node) { |
|
|
|
|
|
|
|
return ( |
|
|
|
|
|
|
|
(node.nodeType === 8) && |
|
|
|
|
|
|
|
(node.nodeValue.indexOf('ngRepeat:') !== -1) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
})[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!ngRepeat) { |
|
|
|
|
|
|
|
// Without ng-repeat
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// tests: http://jsbin.com/kosubutilo/1/edit?js,output
|
|
|
|
|
|
|
|
ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var itemExpr = $parse(ngRepeat[1]); |
|
|
|
|
|
|
|
var itemsExpr = $parse(ngRepeat[2]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
items: function () { |
|
|
|
|
|
|
|
return itemsExpr(scope); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var el = $el[0], |
|
|
|
var el = $el[0], |
|
|
|
options = angular.extend(scope.ngSortable || {}, ngSortableConfig), |
|
|
|
options = angular.extend(scope.ngSortable || {}, ngSortableConfig), |
|
|
|
source = getSource(el), |
|
|
|
|
|
|
|
watchers = [], |
|
|
|
watchers = [], |
|
|
|
|
|
|
|
getSource = getSourceFactory(el, scope), |
|
|
|
sortable |
|
|
|
sortable |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
el[expando] = getSource; |
|
|
|
|
|
|
|
|
|
|
|
function _emitEvent(/**Event*/evt, /*Mixed*/item) { |
|
|
|
function _emitEvent(/**Event*/evt, /*Mixed*/item) { |
|
|
|
var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1); |
|
|
|
var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1); |
|
|
|
|
|
|
|
var source = getSource(); |
|
|
|
|
|
|
|
|
|
|
|
/* jshint expr:true */ |
|
|
|
/* jshint expr:true */ |
|
|
|
options[name] && options[name]({ |
|
|
|
options[name] && options[name]({ |
|
|
|
model: item || source && source.items()[evt.newIndex], |
|
|
|
model: item || source[evt.newIndex], |
|
|
|
models: source && source.items(), |
|
|
|
models: source, |
|
|
|
oldIndex: evt.oldIndex, |
|
|
|
oldIndex: evt.oldIndex, |
|
|
|
newIndex: evt.newIndex |
|
|
|
newIndex: evt.newIndex |
|
|
|
}); |
|
|
|
}); |
|
|
@ -85,25 +84,25 @@ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _sync(/**Event*/evt) { |
|
|
|
function _sync(/**Event*/evt) { |
|
|
|
if (!source) { |
|
|
|
var items = getSource(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!items) { |
|
|
|
// Without ng-repeat
|
|
|
|
// Without ng-repeat
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var oldIndex = evt.oldIndex, |
|
|
|
var oldIndex = evt.oldIndex, |
|
|
|
newIndex = evt.newIndex, |
|
|
|
newIndex = evt.newIndex; |
|
|
|
items = source.items(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (el !== evt.from) { |
|
|
|
if (el !== evt.from) { |
|
|
|
var prevSource = getSource(evt.from), |
|
|
|
var prevItems = evt.from[expando](); |
|
|
|
prevItems = prevSource.items(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oldIndex = prevItems.indexOf(prevSource.item(evt.item)); |
|
|
|
|
|
|
|
removed = prevItems[oldIndex]; |
|
|
|
removed = prevItems[oldIndex]; |
|
|
|
|
|
|
|
|
|
|
|
if (evt.clone) { |
|
|
|
if (evt.clone) { |
|
|
|
evt.from.removeChild(evt.clone); |
|
|
|
|
|
|
|
removed = angular.copy(removed); |
|
|
|
removed = angular.copy(removed); |
|
|
|
|
|
|
|
prevItems.splice(Sortable.utils.index(evt.clone), 0, prevItems.splice(oldIndex, 1)[0]); |
|
|
|
|
|
|
|
evt.from.removeChild(evt.clone); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
prevItems.splice(oldIndex, 1); |
|
|
|
prevItems.splice(oldIndex, 1); |
|
|
@ -155,7 +154,11 @@ |
|
|
|
angular.forEach(watchers, function (/** Function */unwatch) { |
|
|
|
angular.forEach(watchers, function (/** Function */unwatch) { |
|
|
|
unwatch(); |
|
|
|
unwatch(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
sortable.destroy(); |
|
|
|
sortable.destroy(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
el[expando] = null; |
|
|
|
|
|
|
|
el = null; |
|
|
|
watchers = null; |
|
|
|
watchers = null; |
|
|
|
sortable = null; |
|
|
|
sortable = null; |
|
|
|
nextSibling = null; |
|
|
|
nextSibling = null; |
|
|
|