You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.5 KiB

/**
* @author RubaXa <trash@rubaxa.org>
* @licence MIT
*/
10 years ago
(function (factory) {
'use strict';
if (window.angular && window.Sortable) {
factory(angular, Sortable);
}
else if (typeof define === 'function' && define.amd) {
10 years ago
define(['angular', './Sortable'], factory);
10 years ago
}
})(function (angular, Sortable) {
'use strict';
/**
* @typedef {Object} ngSortEvent
* @property {*} model List item
* @property {Object|Array} models List of items
* @property {number} oldIndex before sort
* @property {number} newIndex after sort
*/
10 years ago
angular.module('ng-sortable', [])
.constant('ngSortableVersion', '0.3.7')
.constant('ngSortableConfig', {})
.directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) {
var removed,
nextSibling;
10 years ago
function getSource(el) {
var scope = angular.element(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|]+)/);
10 years ago
var itemExpr = $parse(ngRepeat[1]);
var itemsExpr = $parse(ngRepeat[2]);
return {
item: function (el) {
return itemExpr(angular.element(el).scope());
},
items: function () {
return itemsExpr(scope);
}
};
}
10 years ago
// Export
return {
restrict: 'AC',
scope: { ngSortable: "=?" },
10 years ago
link: function (scope, $el, attrs) {
var el = $el[0],
options = angular.extend(scope.ngSortable || {}, ngSortableConfig),
10 years ago
source = getSource(el),
watchers = [],
10 years ago
sortable
;
function _emitEvent(/**Event*/evt, /*Mixed*/item) {
var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1);
/* jshint expr:true */
options[name] && options[name]({
model: item || source && source.item(evt.item),
models: source && source.items(),
oldIndex: evt.oldIndex,
newIndex: evt.newIndex
});
}
function _sync(/**Event*/evt) {
if (!source) {
// Without ng-repeat
return;
}
10 years ago
var oldIndex = evt.oldIndex,
newIndex = evt.newIndex,
items = source.items();
10 years ago
if (el !== evt.from) {
var prevSource = getSource(evt.from),
prevItems = prevSource.items();
10 years ago
oldIndex = prevItems.indexOf(prevSource.item(evt.item));
removed = prevItems[oldIndex];
if (evt.clone) {
evt.from.removeChild(evt.clone);
removed = angular.copy(removed);
}
else {
prevItems.splice(oldIndex, 1);
}
items.splice(newIndex, 0, removed);
evt.from.insertBefore(evt.item, nextSibling); // revert element
}
else {
10 years ago
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
}
10 years ago
scope.$apply();
}
10 years ago
sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) {
opts[name] = opts[name] || options[name];
return opts;
}, {
10 years ago
onStart: function (/**Event*/evt) {
nextSibling = evt.item.nextSibling;
_emitEvent(evt);
scope.$apply();
10 years ago
},
onEnd: function (/**Event*/evt) {
_emitEvent(evt, removed);
scope.$apply();
10 years ago
},
onAdd: function (/**Event*/evt) {
10 years ago
_sync(evt);
_emitEvent(evt, removed);
scope.$apply();
10 years ago
},
onUpdate: function (/**Event*/evt) {
10 years ago
_sync(evt);
_emitEvent(evt);
10 years ago
},
onRemove: function (/**Event*/evt) {
_emitEvent(evt, removed);
10 years ago
},
onSort: function (/**Event*/evt) {
_emitEvent(evt);
10 years ago
}
}));
$el.on('$destroy', function () {
angular.forEach(watchers, function (/** Function */unwatch) {
unwatch();
});
10 years ago
sortable.destroy();
watchers = null;
10 years ago
sortable = null;
nextSibling = null;
10 years ago
});
angular.forEach([
'sort', 'disabled', 'draggable', 'handle', 'animation',
'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort'
], function (name) {
watchers.push(scope.$watch('ngSortable.' + name, function (value) {
if (value !== void 0) {
options[name] = value;
if (!/^on[A-Z]/.test(name)) {
sortable.option(name, value);
10 years ago
}
}
}));
});
}
10 years ago
};
10 years ago
}]);
10 years ago
});