ng-sortable.js 3.3 KB
Newer Older
1 2 3 4 5
/**
 * @author RubaXa <trash@rubaxa.org>
 * @licence MIT
 */
angular.module('ng-sortable', [])
R
RubaXa 已提交
6
	.constant('$version', '0.2.1')
7
	.directive('ngSortable', ['$parse', '$rootScope', function ($parse, $rootScope) {
R
RubaXa 已提交
8
		'use strict';
R
RubaXa 已提交
9

10 11 12 13 14 15 16
		var removed;

		function getSource(el) {
			var scope = angular.element(el).scope();
			var ngRepeat = [].filter.call(el.childNodes, function (node) {
				return (
						(node.nodeType === 8) &&
R
RubaXa 已提交
17
						(node.nodeValue.indexOf('ngRepeat:') !== -1)
18 19 20 21
					);
			})[0];
			ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*([^\s]+)\s+in\s+([^\s|]+)/);

R
RubaXa 已提交
22 23
			var itemExpr = $parse(ngRepeat[1]);
			var itemsExpr = $parse(ngRepeat[2]);
24 25 26

			return {
				item: function (el) {
R
RubaXa 已提交
27 28 29 30
					return itemExpr(angular.element(el).scope());
				},
				items: function () {
					return itemsExpr(scope);
31 32
				},
				upd: function () {
R
RubaXa 已提交
33
					itemsExpr.assign(scope, this.items());
34 35 36 37 38
				}
			};
		}


R
RubaXa 已提交
39
		// Export
40 41 42
		return {
			restrict: 'AC',
			link: function (scope, $el, attrs) {
R
RubaXa 已提交
43 44 45 46 47 48 49 50
				var el = $el[0],
					ngSortable = attrs.ngSortable,
					options = scope.$eval(ngSortable) || {},
					source = getSource(el),
					sortable,
					_order = []
				;

51 52 53 54 55

				'Start End Add Update Remove Sort'.split(' ').forEach(function (name) {
					options['on' + name] = options['on' + name] || function () {};
				});

R
RubaXa 已提交
56

57 58 59
				function _sync(evt) {
					sortable.toArray().forEach(function (id, i) {
						if (_order[i] !== id) {
R
RubaXa 已提交
60 61
							var idx = _order.indexOf(id),
								items = source.items();
62 63

							if (idx === -1) {
R
RubaXa 已提交
64 65
								var remoteSource = getSource(evt.from),
									remoteItems = remoteSource.items();
66

R
RubaXa 已提交
67 68
								idx = remoteItems.indexOf(remoteSource.item(evt.item));
								removed = remoteItems.splice(idx, 1)[0];
69 70

								_order.splice(i, 0, id);
R
RubaXa 已提交
71
								items.splice(i, 0, removed);
72 73 74 75 76
								remoteSource.upd();

								evt.from.appendChild(evt.item); // revert element
							} else {
								_order.splice(i, 0, _order.splice(idx, 1)[0]);
R
RubaXa 已提交
77
								items.splice(i, 0, items.splice(idx, 1)[0]);
78 79 80 81 82 83 84 85 86
							}
						}
					});

					source.upd();
					scope.$apply();
				}


R
RubaXa 已提交
87
				sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) {
88 89 90 91 92 93 94
					opts[name] = opts[name] || options[name];
					return opts;
				}, {
					onStart: function () {
						$rootScope.$broadcast('sortable:start', sortable);
						options.onStart();
					},
R
RubaXa 已提交
95
					onEnd: function () {
96 97 98 99 100
						$rootScope.$broadcast('sortable:end', sortable);
						options.onEnd();
					},
					onAdd: function (evt) {
						_sync(evt);
R
RubaXa 已提交
101
						options.onAdd(source.items(), removed);
102 103 104
					},
					onUpdate: function (evt) {
						_sync(evt);
R
RubaXa 已提交
105
						options.onUpdate(source.items(), source.item(evt.item));
106
					},
R
RubaXa 已提交
107
					onRemove: function () {
R
RubaXa 已提交
108
						options.onRemove(source.items(), removed);
109 110
					},
					onSort: function () {
R
RubaXa 已提交
111
						options.onSort(source.items());
112 113 114 115
					}
				}));


R
RubaXa 已提交
116
				if (!/{|}/.test(ngSortable)) { // todo: ugly
R
RubaXa 已提交
117
					angular.forEach(['sort', 'disabled', 'draggable', 'handle', 'animation'], function (name) {
R
RubaXa 已提交
118 119 120 121 122 123 124 125
						scope.$watch(ngSortable + '.' + name, function (value) {
							options[name] = value;
							sortable.option(name, value);
						});
					});
				}


126 127 128 129
				$rootScope.$on('sortable:start', function () {
					_order = sortable.toArray();
				});

R
RubaXa 已提交
130

131 132 133 134 135 136 137 138
				$el.on('$destroy', function () {
					el.sortable = null;
					sortable.destroy();
				});
			}
		};
	}])
;