|
|
|
@ -19,7 +19,8 @@
|
|
|
|
|
|
|
|
|
|
var options = buildOptions(valueAccessor, sortableOptions); |
|
|
|
|
|
|
|
|
|
//It's seems that we cannot update the eventhandlers after we've created the sortable, so define them in init instead of update
|
|
|
|
|
// It's seems that we cannot update the eventhandlers after we've created
|
|
|
|
|
// the sortable, so define them in init instead of update
|
|
|
|
|
['onStart', 'onEnd', 'onRemove', 'onAdd', 'onUpdate', 'onSort', 'onFilter'].forEach(function (e) { |
|
|
|
|
if (options[e] || eventHandlers[e]) |
|
|
|
|
options[e] = function (eventType, parentVM, parentBindings, handler, e) { |
|
|
|
@ -56,7 +57,8 @@
|
|
|
|
|
|
|
|
|
|
var moveOperations = [], |
|
|
|
|
tryMoveOperation = function (e, itemVM, parentVM, collection, parentBindings) { |
|
|
|
|
//A move operation is the combination of a add and remove event, this is to make sure that we have both the target and origin collections
|
|
|
|
|
// A move operation is the combination of a add and remove event,
|
|
|
|
|
// this is to make sure that we have both the target and origin collections
|
|
|
|
|
var currentOperation = { event: e, itemVM: itemVM, parentVM: parentVM, collection: collection, parentBindings: parentBindings }, |
|
|
|
|
existingOperation = moveOperations.filter(function (op) { |
|
|
|
|
return op.itemVM === currentOperation.itemVM; |
|
|
|
@ -66,7 +68,8 @@
|
|
|
|
|
moveOperations.push(currentOperation); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
//We're finishing the operation and already have a handle on the operation item meaning that it's safe to remove it
|
|
|
|
|
// We're finishing the operation and already have a handle on
|
|
|
|
|
// the operation item meaning that it's safe to remove it
|
|
|
|
|
moveOperations.splice(moveOperations.indexOf(existingOperation), 1); |
|
|
|
|
|
|
|
|
|
var removeOperation = currentOperation.event.type === 'remove' ? currentOperation : existingOperation, |
|
|
|
@ -75,12 +78,14 @@
|
|
|
|
|
moveItem(itemVM, removeOperation.collection, addOperation.collection, addOperation.event.clone, addOperation.event); |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
//Moves an item from the to (collection to from (collection), these can be references to the same collection which means it's a sort,
|
|
|
|
|
// Moves an item from the "to" collection to the "from" collection, these
|
|
|
|
|
// can be references to the same collection which means it's a sort.
|
|
|
|
|
// clone indicates if we should move or copy the item into the new collection
|
|
|
|
|
moveItem = function (itemVM, from, to, clone, e) { |
|
|
|
|
// Unwrapping this allows us to manipulate the actual array
|
|
|
|
|
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), |
|
|
|
|
newIndex = e.newIndex; |
|
|
|
|
|
|
|
|
@ -96,12 +101,13 @@
|
|
|
|
|
|
|
|
|
|
// This splice is necessary for both clone and move/sort
|
|
|
|
|
// In sort/move since it shouldn't be at this index/in this array anymore
|
|
|
|
|
//In clone since we have to work around knockouts valuHasMutated when manipulating arrays and avoid a "unbound" item added by sortable
|
|
|
|
|
// In clone since we have to work around knockouts valuHasMutated
|
|
|
|
|
// when manipulating arrays and avoid a "unbound" item added by sortable
|
|
|
|
|
fromArray.splice(originalIndex, 1); |
|
|
|
|
// Update the array, this will also remove sortables "unbound" clone
|
|
|
|
|
from.valueHasMutated(); |
|
|
|
|
if (clone && from !== to) { |
|
|
|
|
//Readd the item
|
|
|
|
|
// Read the item
|
|
|
|
|
fromArray.splice(originalIndex, 0, itemVM); |
|
|
|
|
// Force knockout to update
|
|
|
|
|
from.valueHasMutated(); |
|
|
|
@ -115,7 +121,8 @@
|
|
|
|
|
handlers.onRemove = tryMoveOperation; |
|
|
|
|
handlers.onAdd = tryMoveOperation; |
|
|
|
|
handlers.onUpdate = function (e, itemVM, parentVM, collection, parentBindings) { |
|
|
|
|
//This will be performed as a sort since the to/from collections reference the same collection and clone is set to false
|
|
|
|
|
// This will be performed as a sort since the to/from collections
|
|
|
|
|
// reference the same collection and clone is set to false
|
|
|
|
|
moveItem(itemVM, collection, collection, false, e); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -124,7 +131,8 @@
|
|
|
|
|
// bindingOptions are the options set in the "data-bind" attribute in the ui.
|
|
|
|
|
// options are custom options, for instance draggable/sortable specific options
|
|
|
|
|
buildOptions = function (bindingOptions, options) { |
|
|
|
|
//deep clone/copy of properties from the "from" argument onto the "into" argument and returns the modified "into"
|
|
|
|
|
// deep clone/copy of properties from the "from" argument onto
|
|
|
|
|
// the "into" argument and returns the modified "into"
|
|
|
|
|
var merge = function (into, from) { |
|
|
|
|
for (var prop in from) { |
|
|
|
|
if (Object.prototype.toString.call(from[prop]) === '[object Object]') { |
|
|
|
@ -145,8 +153,9 @@
|
|
|
|
|
// Make sure that we don't modify the provided settings object
|
|
|
|
|
options = merge({}, options); |
|
|
|
|
|
|
|
|
|
//group is handled differently since we should both allow to change a draggable to a sortable (and vice versa),
|
|
|
|
|
//but still be able to set a name on a draggable without it becoming a drop target.
|
|
|
|
|
// group is handled differently since we should both allow to change
|
|
|
|
|
// a draggable to a sortable (and vice versa), but still be able to set
|
|
|
|
|
// a name on a draggable without it becoming a drop target.
|
|
|
|
|
if (unwrappedOptions.group && Object.prototype.toString.call(unwrappedOptions.group) !== '[object Object]') { |
|
|
|
|
// group property is a name string declaration, convert to object.
|
|
|
|
|
unwrappedOptions.group = { name: unwrappedOptions.group }; |
|
|
|
|