2 * @author RubaXa <trash@rubaxa.org>
8 if (typeof define === 'function' && define.amd) {
9 define(['angular', './Sortable'], factory);
11 else if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
13 factory(angular, require('./Sortable'));
14 module.exports = 'ng-sortable';
16 else if (window.angular && window.Sortable) {
17 factory(angular, Sortable);
19 })(function (angular, Sortable) {
24 * @typedef {Object} ngSortEvent
25 * @property {*} model List item
26 * @property {Object|Array} models List of items
27 * @property {number} oldIndex before sort
28 * @property {number} newIndex after sort
31 var expando = 'Sortable:ng-sortable';
33 angular.module('ng-sortable', [])
34 .constant('ngSortableVersion', '0.4.0')
35 .constant('ngSortableConfig', {})
36 .directive('ngSortable', ['$parse', 'ngSortableConfig', function ($parse, ngSortableConfig) {
39 getSourceFactory = function getSourceFactory(el, scope) {
40 var ngRepeat = [].filter.call(el.childNodes, function (node) {
42 (node.nodeType === 8) &&
43 (node.nodeValue.indexOf('ngRepeat:') !== -1)
54 // tests: http://jsbin.com/kosubutilo/1/edit?js,output
55 ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*(?:\(.*?,\s*)?([^\s)]+)[\s)]+in\s+([^\s|]+)/);
57 var itemsExpr = $parse(ngRepeat[2]);
60 return itemsExpr(scope.$parent) || [];
68 scope: { ngSortable: "=?" },
69 link: function (scope, $el) {
71 options = angular.extend(scope.ngSortable || {}, ngSortableConfig),
73 getSource = getSourceFactory(el, scope),
77 el[expando] = getSource;
79 function _emitEvent(/**Event*/evt, /*Mixed*/item) {
80 var name = 'on' + evt.type.charAt(0).toUpperCase() + evt.type.substr(1);
81 var source = getSource();
83 /* jshint expr:true */
84 options[name] && options[name]({
85 model: item || source[evt.newIndex],
87 oldIndex: evt.oldIndex,
88 newIndex: evt.newIndex
93 function _sync(/**Event*/evt) {
94 var items = getSource();
101 var oldIndex = evt.oldIndex,
102 newIndex = evt.newIndex;
104 if (el !== evt.from) {
105 var prevItems = evt.from[expando]();
107 removed = prevItems[oldIndex];
110 removed = angular.copy(removed);
111 prevItems.splice(Sortable.utils.index(evt.clone), 0, prevItems.splice(oldIndex, 1)[0]);
112 evt.from.removeChild(evt.clone);
115 prevItems.splice(oldIndex, 1);
118 items.splice(newIndex, 0, removed);
120 evt.from.insertBefore(evt.item, nextSibling); // revert element
123 items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
130 sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) {
131 opts[name] = opts[name] || options[name];
134 onStart: function (/**Event*/evt) {
135 nextSibling = evt.item.nextSibling;
139 onEnd: function (/**Event*/evt) {
140 _emitEvent(evt, removed);
143 onAdd: function (/**Event*/evt) {
145 _emitEvent(evt, removed);
148 onUpdate: function (/**Event*/evt) {
152 onRemove: function (/**Event*/evt) {
153 _emitEvent(evt, removed);
155 onSort: function (/**Event*/evt) {
160 $el.on('$destroy', function () {
161 angular.forEach(watchers, function (/** Function */unwatch) {
175 'sort', 'disabled', 'draggable', 'handle', 'animation', 'group', 'ghostClass', 'filter',
176 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort'
178 watchers.push(scope.$watch('ngSortable.' + name, function (value) {
179 if (value !== void 0) {
180 options[name] = value;
182 if (!/^on[A-Z]/.test(name)) {
183 sortable.option(name, value);