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

Implement optional "strategyMove" behaviour that moves rather than reinserts DOM nodes #160

Merged
merged 2 commits into from
Apr 23, 2016
Merged
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
8 changes: 7 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"name": "knockout-sortable",
"version": "0.12.0",
"description": "A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality",
"homepage": "https://github.com/rniemeyer/knockout-sortable",
"authors": [
{ "name": "Ryan Niemeyer" }
],
"license": "MIT",
"version": "0.13.0",
"main": "./build/knockout-sortable.min.js",
"ignore": [
"examples",
Expand Down
87 changes: 68 additions & 19 deletions build/knockout-sortable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-sortable 0.12.0 | (c) 2016 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-sortable 0.13.0 | (c) 2016 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand Down Expand Up @@ -247,24 +247,73 @@
if (arg && arg.cancelDrop) {
return;
}

//do the actual move
if (targetIndex >= 0) {
if (sourceParent) {
sourceParent.splice(sourceIndex, 1);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
}
}

targetParent.splice(targetIndex, 0, item);
}

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);


//if the strategy option is unset or false, employ the order strategy involving removal and insertion of items
if(!sortable.hasOwnProperty("strategyMove") || sortable.strategyMove === false)
{
//do the actual move
if (targetIndex >= 0) {
if (sourceParent) {
sourceParent.splice(sourceIndex, 1);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
}
}

targetParent.splice(targetIndex, 0, item);
}

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);
}
else { //employ the strategy of moving items
if (targetIndex >= 0) {
if (sourceParent) {
if (sourceParent !== targetParent) {
// moving from one list to another

sourceParent.splice(sourceIndex, 1);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
}

targetParent.splice(targetIndex, 0, item);

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);
ui.item.remove();
}
else {
// moving within same list
var underlyingList = unwrap(sourceParent);

// notify 'beforeChange' subscribers
sourceParent.valueWillMutate();

// move from source index ...
underlyingList.splice(sourceIndex, 1);
// ... to target index
underlyingList.splice(targetIndex, 0, item);

// notify subscribers
sourceParent.valueHasMutated();
}
}
else {
// drop new element from outside
targetParent.splice(targetIndex, 0, item);

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);
ui.item.remove();
}
}
}

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
Expand Down
4 changes: 2 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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "knockout-sortable",
"version": "0.12.0",
"version": "0.13.0",
"author": "Ryan Niemeyer",
"homepage": "https://github.com/rniemeyer/knockout-sortable",
"description": "A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/rniemeyer/knockout-sortable.git"
Expand Down
85 changes: 67 additions & 18 deletions src/knockout-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,73 @@
if (arg && arg.cancelDrop) {
return;
}

//do the actual move
if (targetIndex >= 0) {
if (sourceParent) {
sourceParent.splice(sourceIndex, 1);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
}
}

targetParent.splice(targetIndex, 0, item);
}

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);


//if the strategy option is unset or false, employ the order strategy involving removal and insertion of items
if(!sortable.hasOwnProperty("strategyMove") || sortable.strategyMove === false)
{
//do the actual move
if (targetIndex >= 0) {
if (sourceParent) {
sourceParent.splice(sourceIndex, 1);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
}
}

targetParent.splice(targetIndex, 0, item);
}

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);
}
else { //employ the strategy of moving items
if (targetIndex >= 0) {
if (sourceParent) {
if (sourceParent !== targetParent) {
// moving from one list to another

sourceParent.splice(sourceIndex, 1);

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
}

targetParent.splice(targetIndex, 0, item);

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);
ui.item.remove();
}
else {
// moving within same list
var underlyingList = unwrap(sourceParent);

// notify 'beforeChange' subscribers
sourceParent.valueWillMutate();

// move from source index ...
underlyingList.splice(sourceIndex, 1);
// ... to target index
underlyingList.splice(targetIndex, 0, item);

// notify subscribers
sourceParent.valueHasMutated();
}
}
else {
// drop new element from outside
targetParent.splice(targetIndex, 0, item);

//rendering is handled by manipulating the observableArray; ignore dropped element
dataSet(el, ITEMKEY, null);
ui.item.remove();
}
}
}

//if using deferred updates plugin, force updates
if (ko.processAllDeferredBindingUpdates) {
ko.processAllDeferredBindingUpdates();
Expand Down