Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for jQuery sortable's "over" event #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ Note: The sortable binding assumes that the child "templates" have a single cont

This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.afterMove`. This callback also receives the `event` and `ui` objects as the second and third arguments.

* **hovering** - specify a function to execute while an item is being dragged over a potential location. This function receives an object for its first argument that contains the following information:
* `arg.item` - the actual item being moved
* `arg.sourceIndex` - the position of the item in the original observableArray
* `arg.sourceParent` - the original observableArray
* `arg.sourceParentNode` - the container node of the original list. Useful if moving items between lists, but within a single array. The value of `this` in the callback will be the target container node.
* `arg.targetIndex` - the targeted position of the item in the destination observableArray
* `arg.targetParent` - the destination observableArray

This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.afterMove`. This callback also receives the `event` and `ui` objects as the second and third arguments. Warning: this callback might be called multiple times for the same target location.

* **dragged** - specify a function to execute after a draggable item has been dropped into a sortable. This callback receives the drag item as the first argument, the `event` as the second argument, and the `ui` object as the third argument. If the function returns a value, then it will be used as item that is dropped into the sortable. This can be used as an alternative to the original item including a `clone` function.

* **isEnabled** - specify whether the sortable widget should be enabled. If this is an observable, then it will enable/disable the widget when the observable's value changes. This option can be passed in the binding or configured globally by setting `ko.bindingHandlers.sortable.isEnabled`.
Expand Down
26 changes: 26 additions & 0 deletions build/knockout-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@
startActual.apply(this, arguments);
}
},
over: function(event, ui) {
if(!sortable.hovering)
return;
var sourceParent, targetParent, sourceIndex, targetIndex, arg,
el = ui.item[0],
placeHolderEl = ui.placeholder[0],
item = dataGet(el, ITEMKEY) || dragItem;

sourceParent = dataGet(el, PARENTKEY);
sourceIndex = dataGet(el, INDEXKEY);
targetParent = dataGet(placeHolderEl.parentNode, LISTKEY);
targetIndex = ko.utils.arrayIndexOf(ui.placeholder.parent().children(), placeHolderEl);
if (sourceParent === targetParent && targetIndex > sourceIndex)
targetIndex--;

arg = {
item: item,
sourceParent: sourceParent,
sourceParentNode: sourceParent && ui.sender || el.parentNode,
sourceIndex: sourceIndex,
targetParent: targetParent,
targetIndex: targetIndex
};
sortable.hovering.call(this, arg, event, ui);
},
receive: function(event, ui) {
dragItem = dataGet(ui.item[0], DRAGKEY);
if (dragItem) {
Expand Down Expand Up @@ -274,6 +299,7 @@
allowDrop: true,
afterMove: null,
beforeMove: null,
hovering: null,
options: {}
};

Expand Down
5 changes: 3 additions & 2 deletions build/knockout-sortable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/knockout-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@
startActual.apply(this, arguments);
}
},
over: function(event, ui) {
if(!sortable.hovering)
return;
var sourceParent, targetParent, sourceIndex, targetIndex, arg,
el = ui.item[0],
placeHolderEl = ui.placeholder[0],
item = dataGet(el, ITEMKEY) || dragItem;

sourceParent = dataGet(el, PARENTKEY);
sourceIndex = dataGet(el, INDEXKEY);
targetParent = dataGet(placeHolderEl.parentNode, LISTKEY);
targetIndex = ko.utils.arrayIndexOf(ui.placeholder.parent().children(), placeHolderEl);
if (sourceParent === targetParent && targetIndex > sourceIndex)
targetIndex--;

arg = {
item: item,
sourceParent: sourceParent,
sourceParentNode: sourceParent && ui.sender || el.parentNode,
sourceIndex: sourceIndex,
targetParent: targetParent,
targetIndex: targetIndex
};
sortable.hovering.call(this, arg, event, ui);
},
receive: function(event, ui) {
dragItem = dataGet(ui.item[0], DRAGKEY);
if (dragItem) {
Expand Down Expand Up @@ -273,6 +298,7 @@
allowDrop: true,
afterMove: null,
beforeMove: null,
hovering: null,
options: {}
};

Expand Down