ng-sortable.js 3.4 KB
Newer Older
1 2 3 4
/**
 * @author RubaXa <trash@rubaxa.org>
 * @licence MIT
 */
R
RubaXa 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17
(function (factory) {
	'use strict';

	if (window.angular && window.Sortable) {
		factory(angular, Sortable);
	}
	else if (typeof define === 'function' && define.amd) {
		define(['angular', 'sortable'], factory);
	}
})(function (angular, Sortable) {
	'use strict';

	angular.module('ng-sortable', [])
R
RubaXa 已提交
18
		.constant('$version', '0.3.3')
R
RubaXa 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
		.directive('ngSortable', ['$parse', function ($parse) {
			var removed;

			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];
				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);
					}
				};
			}
44

R
RubaXa 已提交
45

R
RubaXa 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
			// 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 () {};
					});
61 62


R
RubaXa 已提交
63 64 65 66
					function _sync(evt) {
						var oldIndex = evt.oldIndex,
							newIndex = evt.newIndex,
							items = source.items();
R
RubaXa 已提交
67

R
RubaXa 已提交
68 69 70
						if (el !== evt.from) {
							var prevSource = getSource(evt.from),
								prevItems = prevSource.items();
R
RubaXa 已提交
71

R
RubaXa 已提交
72 73
							oldIndex = prevItems.indexOf(prevSource.item(evt.item));
							removed = prevItems.splice(oldIndex, 1)[0];
R
RubaXa 已提交
74

R
RubaXa 已提交
75
							items.splice(newIndex, 0, removed);
R
RubaXa 已提交
76

R
RubaXa 已提交
77 78
							if (evt.clone) {
								newIndex = Sortable.utils.index(evt.clone);
R
RubaXa 已提交
79
								prevItems.splice(oldIndex, 0, removed);
R
RubaXa 已提交
80 81 82 83

								evt.from.removeChild(evt.clone);
							}

R
RubaXa 已提交
84 85 86 87
							evt.from.appendChild(evt.item); // revert element
						} else {
							items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
						}
R
RubaXa 已提交
88

R
RubaXa 已提交
89
						scope.$apply();
R
RubaXa 已提交
90
					}
91 92


R
RubaXa 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
					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;
					});
123

R
RubaXa 已提交
124
					if (ngSortable && !/{|}/.test(ngSortable)) { // todo: ugly
R
RubaXa 已提交
125 126 127 128 129 130 131
						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);
								}
							});
R
RubaXa 已提交
132
						});
R
RubaXa 已提交
133
					}
R
RubaXa 已提交
134
				}
R
RubaXa 已提交
135
			};
R
RubaXa 已提交
136
		}]);
R
RubaXa 已提交
137
});