Browse Source

#195: + amd

requirejs
RubaXa 10 years ago
parent
commit
bb09bb423a
  1. 212
      ng-sortable.js

212
ng-sortable.js

@ -2,119 +2,129 @@
* @author RubaXa <trash@rubaxa.org> * @author RubaXa <trash@rubaxa.org>
* @licence MIT * @licence MIT
*/ */
angular.module('ng-sortable', []) (function (factory) {
.constant('$version', '0.3.2') 'use strict';
.directive('ngSortable', ['$parse', function ($parse) {
'use strict'; if (window.angular && window.Sortable) {
factory(angular, Sortable);
var removed; }
else if (typeof define === 'function' && define.amd) {
function getSource(el) { define(['angular', 'sortable'], factory);
var scope = angular.element(el).scope(); }
var ngRepeat = [].filter.call(el.childNodes, function (node) { })(function (angular, Sortable) {
return ( 'use strict';
(node.nodeType === 8) &&
(node.nodeValue.indexOf('ngRepeat:') !== -1) angular.module('ng-sortable', [])
); .constant('$version', '0.3.2')
})[0]; .directive('ngSortable', ['$parse', function ($parse) {
ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*([^\s]+)\s+in\s+([^\s|]+)/); var removed;
var itemExpr = $parse(ngRepeat[1]); function getSource(el) {
var itemsExpr = $parse(ngRepeat[2]); var scope = angular.element(el).scope();
var ngRepeat = [].filter.call(el.childNodes, function (node) {
return { return (
item: function (el) { (node.nodeType === 8) &&
return itemExpr(angular.element(el).scope()); (node.nodeValue.indexOf('ngRepeat:') !== -1)
}, );
items: function () { })[0];
return itemsExpr(scope); ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*([^\s]+)\s+in\s+([^\s|]+)/);
}
}; 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);
}
};
}
// Export
return {
restrict: 'AC',
link: function (scope, $el, attrs) {
var el = $el[0],
ngSortable = attrs.ngSortable,
options = scope.$eval(ngSortable) || {},
source = getSource(el),
sortable
;
// Export
return {
restrict: 'AC',
link: function (scope, $el, attrs) {
var el = $el[0],
ngSortable = attrs.ngSortable,
options = scope.$eval(ngSortable) || {},
source = getSource(el),
sortable
;
'Start End Add Update Remove Sort'.split(' ').forEach(function (name) {
options['on' + name] = options['on' + name] || function () {};
});
'Start End Add Update Remove Sort'.split(' ').forEach(function (name) {
options['on' + name] = options['on' + name] || function () {};
});
function _sync(evt) {
var oldIndex = evt.oldIndex,
newIndex = evt.newIndex,
items = source.items();
function _sync(evt) { if (el !== evt.from) {
var oldIndex = evt.oldIndex, var prevSource = getSource(evt.from),
newIndex = evt.newIndex, prevItems = prevSource.items();
items = source.items();
if (el !== evt.from) { oldIndex = prevItems.indexOf(prevSource.item(evt.item));
var prevSource = getSource(evt.from), removed = prevItems.splice(oldIndex, 1)[0];
prevItems = prevSource.items();
oldIndex = prevItems.indexOf(prevSource.item(evt.item)); items.splice(newIndex, 0, removed);
removed = prevItems.splice(oldIndex, 1)[0];
items.splice(newIndex, 0, removed); evt.from.appendChild(evt.item); // revert element
} else {
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
}
evt.from.appendChild(evt.item); // revert element scope.$apply();
} else {
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
} }
scope.$apply();
}
sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) {
opts[name] = opts[name] || options[name];
return opts;
}, {
onStart: function () {
options.onStart(source.items());
},
onEnd: function () {
options.onEnd(source.items());
},
onAdd: function (evt) {
_sync(evt);
options.onAdd(source.items(), removed);
},
onUpdate: function (evt) {
_sync(evt);
options.onUpdate(source.items(), source.item(evt.item));
},
onRemove: function () {
options.onRemove(source.items(), removed);
},
onSort: function () {
options.onSort(source.items());
}
}));
$el.on('$destroy', function () {
sortable.destroy();
sortable = null;
});
sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) { if (!/{|}/.test(ngSortable)) { // todo: ugly
opts[name] = opts[name] || options[name]; angular.forEach(['sort', 'disabled', 'draggable', 'handle', 'animation'], function (name) {
return opts; scope.$watch(ngSortable + '.' + name, function (value) {
}, { if (value !== void 0) {
onStart: function () { options[name] = value;
options.onStart(source.items()); sortable.option(name, value);
}, }
onEnd: function () { });
options.onEnd(source.items());
},
onAdd: function (evt) {
_sync(evt);
options.onAdd(source.items(), removed);
},
onUpdate: function (evt) {
_sync(evt);
options.onUpdate(source.items(), source.item(evt.item));
},
onRemove: function () {
options.onRemove(source.items(), removed);
},
onSort: function () {
options.onSort(source.items());
}
}));
$el.on('$destroy', function () {
sortable.destroy();
sortable = null;
});
if (!/{|}/.test(ngSortable)) { // todo: ugly
angular.forEach(['sort', 'disabled', 'draggable', 'handle', 'animation'], function (name) {
scope.$watch(ngSortable + '.' + name, function (value) {
if (value !== void 0) {
options[name] = value;
sortable.option(name, value);
}
}); });
}); }
} }
} };
}; }]);
}]) });
;

Loading…
Cancel
Save