From 01ceba1b45a7f2d0db727179d5da1f8dc23734c2 Mon Sep 17 00:00:00 2001 From: RubaXa Date: Sat, 6 Dec 2014 12:00:06 +0300 Subject: [PATCH] #142: + options watcher --- ng-sortable.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/ng-sortable.js b/ng-sortable.js index e064967..4b5d0b6 100644 --- a/ng-sortable.js +++ b/ng-sortable.js @@ -3,9 +3,9 @@ * @licence MIT */ angular.module('ng-sortable', []) - .constant('$version', '0.1.0') + .constant('$version', '0.2.0') .directive('ngSortable', ['$parse', '$rootScope', function ($parse, $rootScope) { - "use strict"; + 'use strict'; var removed; @@ -14,7 +14,7 @@ angular.module('ng-sortable', []) var ngRepeat = [].filter.call(el.childNodes, function (node) { return ( (node.nodeType === 8) && - (node.nodeValue.indexOf("ngRepeat:") !== -1) + (node.nodeValue.indexOf('ngRepeat:') !== -1) ); })[0]; ngRepeat = ngRepeat.nodeValue.match(/ngRepeat:\s*([^\s]+)\s+in\s+([^\s|]+)/); @@ -34,18 +34,24 @@ angular.module('ng-sortable', []) } + // Export return { restrict: 'AC', link: function (scope, $el, attrs) { - var el = $el[0]; - var options = scope.$eval(attrs.ngSortable) || {}; - var _order = []; - var source = getSource(el); + var el = $el[0], + ngSortable = attrs.ngSortable, + options = scope.$eval(ngSortable) || {}, + source = getSource(el), + sortable, + _order = [] + ; + 'Start End Add Update Remove Sort'.split(' ').forEach(function (name) { options['on' + name] = options['on' + name] || function () {}; }); + function _sync(evt) { sortable.toArray().forEach(function (id, i) { if (_order[i] !== id) { @@ -74,7 +80,7 @@ angular.module('ng-sortable', []) } - var sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) { + sortable = Sortable.create(el, Object.keys(options).reduce(function (opts, name) { opts[name] = opts[name] || options[name]; return opts; }, { @@ -94,7 +100,7 @@ angular.module('ng-sortable', []) _sync(evt); options.onUpdate(source.items, source.item(evt.item)); }, - onRemove: function (evt) { + onRemove: function () { options.onRemove(source.items, removed); }, onSort: function () { @@ -103,10 +109,21 @@ angular.module('ng-sortable', []) })); + if (!/{|}/.test(ngSortable)) { // todo: ugly + angular.forEach(sortable.options, function (v, name) { + scope.$watch(ngSortable + '.' + name, function (value) { + options[name] = value; + sortable.option(name, value); + }); + }); + } + + $rootScope.$on('sortable:start', function () { _order = sortable.toArray(); }); + $el.on('$destroy', function () { el.sortable = null; sortable.destroy();