Skip to content

Commit

Permalink
v1.1.0 - droppable binding
Browse files Browse the repository at this point in the history
  • Loading branch information
rniemeyer committed Feb 18, 2017
1 parent 30e5844 commit 426300f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 44 deletions.
20 changes: 10 additions & 10 deletions build/knockout-sortable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-sortable 1.0.0 | (c) 2017 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-sortable 1.1.0 | (c) 2017 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand Down Expand Up @@ -440,11 +440,11 @@
helper: "clone"
}
};

// Simple Droppable Implementation
// binding that updates (function or observable)
ko.bindingHandlers.droppable = {
init: function (element, valueAccessor, allBindingsAccessor, data, context) {
init: function(element, valueAccessor, allBindingsAccessor, data, context) {
var value = unwrap(valueAccessor()) || {},
options = value.options || {},
droppableOptions = ko.utils.extend({}, ko.bindingHandlers.droppable.options),
Expand All @@ -454,29 +454,29 @@
ko.utils.extend(droppableOptions, options);

//get reference to drop method
value = "data" in value ? value.data : value;
value = "data" in value ? value.data : valueAccessor();

//set drop method
droppableOptions.drop = function (event, ui) {
var droppedItem = dataGet(ui.draggable[0], DRAGKEY);
droppableOptions.drop = function(event, ui) {
var droppedItem = dataGet(ui.draggable[0], DRAGKEY) || dataGet(ui.draggable[0], ITEMKEY);
value(droppedItem);
};

//initialize droppable
$(element).droppable(droppableOptions);

//handle enabling/disabling sorting
//handle enabling/disabling droppable
if (isEnabled !== undefined) {
ko.computed({
read: function () {
$(element).droppable(unwrap(isEnabled) ? "enable" : "disable");
read: function() {
$(element).droppable(unwrap(isEnabled) ? "enable": "disable");
},
disposeWhenNodeIsRemoved: element
});
}

//handle disposal
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).droppable("destroy");
});
},
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-sortable",
"version": "1.0.0",
"version": "1.1.0",
"author": "Ryan Niemeyer",
"homepage": "https://github.com/rniemeyer/knockout-sortable",
"description": "A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality",
Expand Down
48 changes: 26 additions & 22 deletions spec/knockout-sortable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ describe("knockout-sortable", function(){

it("should call .sortable on the root element", function(done) {
setTimeout(function() {
expect(options.root.data("sortable")).toBeDefined();
expect(options.root.data("uiSortable")).toBeDefined();
expect(options.root.hasClass("ui-sortable")).toBeTruthy();
done();
}, 0);
Expand Down Expand Up @@ -951,7 +951,7 @@ describe("knockout-sortable", function(){
});

it("should call draggable on the element", function() {
expect(options.root.data("draggable")).toBeDefined();
expect(options.root.data("uiDraggable")).toBeDefined();
});

it("should use the default connectClass", function() {
Expand Down Expand Up @@ -1146,49 +1146,53 @@ describe("knockout-sortable", function(){

describe("droppable binding", function(){
var options;
describe("when dropping to an function", function () {

describe("when dropping to a function", function() {
var result;
beforeEach(function () {
beforeEach(function() {
result = null;
options = {
elems: $("<div data-bind='droppable: dropTo'></div>"),
vm: {
dropTo:function(item) {
vm: {
dropTo: function(item) {
result = item;
}
}
};
setup(options);
});
it("it should have droppable instance", function () {

it("it should have droppable instance", function() {
expect(options.root.droppable("instance")).toBeTruthy();
});
});
describe("when dropping to an observable", function () {
beforeEach(function () {
describe("when dropping to an observable", function() {
beforeEach(function() {
options = {
elems: $("<div data-bind='droppable: dropTo'></div>"),
vm: {
dropTo:ko.observable(undefined)
vm: {
dropTo: ko.observable()
}
};
setup(options);
});
it("it should have droppable instance", function () {

it("it should have droppable instance", function() {
expect(options.root.droppable("instance")).toBeTruthy();
});
});
describe("when using options", function () {
beforeEach(function () {
describe("when using options", function() {
beforeEach(function() {
options = {
elems: $("<div data-bind='droppable:{data:dropTo, options:{greedy:true,accept:\".test-accepts\"}}'></div>"),
vm: {
dropTo:ko.observable(undefined)
elems: $("<div data-bind='droppable:{drop:dropTo, options:{greedy:true,accept:\".test-accepts\"}}'></div>"),
vm: {
dropTo :ko.observable()
}
};
setup(options);
});
it("the options should be set", function (done) {

it("the options should be set", function(done) {
setTimeout(function() {
expect(options.root.droppable("option", "greedy")).toBeTruthy();
expect(options.root.droppable("option", "accept")).toEqual(".test-accepts");
Expand All @@ -1197,14 +1201,14 @@ describe("knockout-sortable", function(){
});
});


describe("when setting isEnabled globally", function() {
var options;

beforeEach(function() {
options = {
elems: $("<div data-bind='droppable: dropTo'></div>"),
vm: {
dropTo:ko.observable(undefined),
dropTo: ko.observable(),
isEnabled: ko.observable(false)
}
};
Expand Down Expand Up @@ -1251,7 +1255,7 @@ describe("knockout-sortable", function(){
var options;
beforeEach(function() {
options = {
elems: $("<div data-bind='droppable: { data: dropTo, isEnabled: isEnabled }'></div>"),
elems: $("<div data-bind='droppable: { drop: dropTo, isEnabled: isEnabled }'></div>"),
vm: {
dropTo:ko.observable(undefined),
isEnabled: ko.observable(false)
Expand Down Expand Up @@ -1295,4 +1299,4 @@ describe("knockout-sortable", function(){
});
});
});
});
});
Loading

0 comments on commit 426300f

Please sign in to comment.