Browse Source

Merge pull request #497 from srosengren/dev

Knockout sortable #488
pull/499/head
Lebedev Konstantin 10 years ago
parent
commit
c188d47361
  1. 2
      Sortable.min.js
  2. 27
      knockout-sortable.js

2
Sortable.min.js vendored

File diff suppressed because one or more lines are too long

27
knockout-sortable.js

@ -1,4 +1,17 @@
(function () { (function (factory) {
"use strict";
if (typeof define === "function" && define.amd) {
// AMD anonymous module
define(["knockout"], factory);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS module
var ko = require("knockout");
factory(ko);
} else {
// No module loader (plain <script> tag) - put directly in global namespace
factory(window.ko);
}
})(function (ko) {
"use strict"; "use strict";
var init = function (element, valueAccessor, allBindings, viewModel, bindingContext, sortableOptions) { var init = function (element, valueAccessor, allBindings, viewModel, bindingContext, sortableOptions) {
@ -67,7 +80,13 @@
//Unwrapping this allows us to manipulate the actual array //Unwrapping this allows us to manipulate the actual array
var fromArray = from(), var fromArray = from(),
//It's not certain that the items actual index is the same as the index reported by sortable due to filtering etc. //It's not certain that the items actual index is the same as the index reported by sortable due to filtering etc.
originalIndex = fromArray.indexOf(itemVM); originalIndex = fromArray.indexOf(itemVM),
newIndex = e.newIndex;
if (e.item.previousElementSibling)
newIndex = fromArray.indexOf(ko.dataFor(e.item.previousElementSibling));
if (originalIndex > newIndex)
newIndex = newIndex + 1;
//Remove sortables "unbound" element //Remove sortables "unbound" element
e.item.parentNode.removeChild(e.item); e.item.parentNode.removeChild(e.item);
@ -85,7 +104,7 @@
from.valueHasMutated(); from.valueHasMutated();
} }
//Insert the item on its new position //Insert the item on its new position
to().splice(e.newIndex, 0, itemVM); to().splice(newIndex, 0, itemVM);
//Make sure to tell knockout that we've modified the actual array. //Make sure to tell knockout that we've modified the actual array.
to.valueHasMutated(); to.valueHasMutated();
}; };
@ -158,4 +177,4 @@
} }
}; };
})(); });

Loading…
Cancel
Save