jquery-ui/tests/unit/draggable/draggable_options.js

428 lines
10 KiB
JavaScript
Raw Normal View History

/*
* draggable_options.js
*/
(function($) {
function testScroll(position) {
$("#main").css('position', position);
drag(el, 50, 50);
moved(50, 50, position+' parent');
}
function setScroll(what) {
if(what) {
$(document).scrollTop(100); $(document).scrollLeft(100);
} else {
$("#main")[0].scrollTop = 100; $("#main")[0].scrollLeft = 100;
}
}
var border = function(el, side) { return parseInt(el.css('border-' + side + '-width')); }
var margin = function(el, side) { return parseInt(el.css('margin-' + side)); }
module("draggable: options");
test("{ addClasses: true }, default", function() {
equals(draggable_defaults.addClasses, true);
el = $("<div></div>").draggable({ addClasses: true });
ok(el.is(".ui-draggable"), "'ui-draggable' class added");
2009-02-02 13:23:51 +00:00
el.draggable("destroy");
});
test("{ addClasses: false }", function() {
el = $("<div></div>").draggable({ addClasses: false });
ok(!el.is(".ui-draggable"), "'ui-draggable' class not added");
2009-02-02 13:23:51 +00:00
el.draggable("destroy");
});
test("{ appendTo: 'parent' }, default", function() {
equals(draggable_defaults.appendTo, "parent");
el = $("#draggable2").draggable({ appendTo: 'parent' });
drag(el, 50, 50);
moved(50, 50);
el = $("#draggable1").draggable({ appendTo: 'parent' });
drag(el, 50, 50);
moved(50, 50);
});
test("{ appendTo: Element }", function() {
el = $("#draggable2").draggable({ appendTo: $("#draggable2").parent()[0] });
drag(el, 50, 50);
moved(50, 50);
el = $("#draggable1").draggable({ appendTo: $("#draggable2").parent()[0] });
drag(el, 50, 50);
moved(50, 50);
});
test("{ appendTo: Selector }", function() {
el = $("#draggable2").draggable({ appendTo: "#main" });
drag(el, 50, 50);
moved(50, 50);
el = $("#draggable1").draggable({ appendTo: "#main" });
drag(el, 50, 50);
moved(50, 50);
});
test("{ axis: false }, default", function() {
equals(draggable_defaults.axis, false);
el = $("#draggable2").draggable({ axis: false });
drag(el, 50, 50);
moved(50, 50);
});
test("{ axis: 'x' }", function() {
el = $("#draggable2").draggable({ axis: "x" });
drag(el, 50, 50);
moved(50, 0);
});
test("{ axis: 'y' }", function() {
el = $("#draggable2").draggable({ axis: "y" });
drag(el, 50, 50);
moved(0, 50);
});
test("{ axis: ? }, unexpected", function() {
var unexpected = {
"true": true,
"{}": {},
"[]": [],
"null": null,
"undefined": undefined,
"function() {}": function() {}
};
$.each(unexpected, function(key, val) {
el = $("#draggable2").draggable({ axis: val });
drag(el, 50, 50);
moved(50, 50, "axis: " + key);
el.draggable("destroy");
})
});
test("{ cancel: 'span' }", function() {
el = $("#draggable2").draggable();
drag("#draggable2 span", 50, 50);
moved(50, 50);
el.draggable("destroy");
el = $("#draggable2").draggable({ cancel: 'span' });
drag("#draggable2 span", 50, 50);
moved(0, 0);
});
test("{ cancel: ? }, unexpected", function() {
var unexpected = {
"true": true,
"false": false,
"{}": {},
"[]": [],
"null": null,
"undefined": undefined,
"function() {return '';}": function() {return '';},
"function() {return true;}": function() {return true;},
"function() {return false;}": function() {return false;}
};
$.each(unexpected, function(key, val) {
el = $("#draggable2").draggable({ cancel: val });
drag(el, 50, 50);
var expected = [50, 50];
moved(expected[0], expected[1], "cancel: " + key);
el.draggable("destroy");
})
});
test("{ containment: 'parent' }, relative", function() {
el = $("#draggable1").draggable({ containment: 'parent' });
var p = el.parent(), po = p.offset();
drag(el, -100, -100);
var expected = {
left: po.left + border(p, 'left') + margin(el, 'left'),
top: po.top + border(p, 'top') + margin(el, 'top')
}
same(offsetAfter, expected, 'compare offset to parent');
});
test("{ containment: 'parent' }, absolute", function() {
el = $("#draggable2").draggable({ containment: 'parent' });
var p = el.parent(), po = p.offset();
drag(el, -100, -100);
var expected = {
left: po.left + border(p, 'left') + margin(el, 'left'),
top: po.top + border(p, 'top') + margin(el, 'top')
}
same(offsetAfter, expected, 'compare offset to parent');
});
test("{ cursor: 'auto' }, default", function() {
equals(draggable_defaults.cursor, 'auto');
function getCursor() { return $("#draggable2").css("cursor"); }
expect(3);
var expected = "auto", actual, before, after;
el = $("#draggable2").draggable({
cursor: expected,
start: function(event, ui) {
actual = getCursor();
}
});
before = getCursor();
drag("#draggable2", -1, -1);
after = getCursor();
equals(actual, expected, "start callback: cursor '" + expected + "'");
equals(after, before, "after drag: cursor restored");
});
test("{ cursor: 'move' }", function() {
function getCursor() { return $("body").css("cursor"); }
expect(2);
var expected = "move", actual, before, after;
el = $("#draggable2").draggable({
cursor: expected,
start: function(event, ui) {
actual = getCursor();
}
});
before = getCursor();
drag("#draggable2", -1, -1);
after = getCursor();
equals(actual, expected, "start callback: cursor '" + expected + "'");
equals(after, before, "after drag: cursor restored");
});
test("{ cursorAt: { left: -5, top: -5 } }", function() {
expect(4);
var deltaX = -3, deltaY = -3,
offsetX = 5, offsetY = 5,
cursorAtX = -5, cursorAtY = -5;
$.each(['relative', 'absolute'], function(i, position) {
var el = $('#draggable' + (i + 1)).draggable({
cursorAt: { left: cursorAtX, top: cursorAtY },
drag: function(event, ui) {
equals(ui.offset.left, expected.left, position + ' left');
equals(ui.offset.top, expected.top, position + ' top');
}
}),
before = el.offset(),
pos = {
clientX: before.left + offsetX,
clientY: before.top + offsetY
},
expected = {
left: before.left + offsetX - cursorAtX + deltaX,
top: before.top + offsetY - cursorAtY + deltaY
};
el.simulate("mousedown", pos);
pos.clientX += deltaX;
pos.clientY += deltaY;
$(document).simulate("mousemove", pos);
el.simulate("mouseup", pos);
});
});
test("{ cursorAt: { right: 10, bottom: 20 } }", function() {
expect(4);
var deltaX = -3, deltaY = -3,
offsetX = 5, offsetY = 5,
cursorAtX = 10, cursorAtY = 20;
$.each(['relative', 'absolute'], function(i, position) {
var el = $('#draggable' + (i + 1)).draggable({
cursorAt: { right: cursorAtX, bottom: cursorAtY },
drag: function(event, ui) {
equals(ui.offset.left, expected.left, position + ' left');
equals(ui.offset.top, expected.top, position + ' top');
}
}),
before = el.offset(),
pos = {
clientX: before.left + offsetX,
clientY: before.top + offsetY
},
expected = {
left: before.left + offsetX - el.width() + cursorAtX + deltaX,
top: before.top + offsetY - el.height() + cursorAtY + deltaY
};
el.simulate("mousedown", pos);
pos.clientX += deltaX;
pos.clientY += deltaY;
$(document).simulate("mousemove", pos);
el.simulate("mouseup", pos);
});
});
test("{ cursorAt: [10, 20] }", function() {
expect(4);
var deltaX = -3, deltaY = -3,
offsetX = 5, offsetY = 5,
cursorAtX = 10, cursorAtY = 20;
$.each(['relative', 'absolute'], function(i, position) {
var el = $('#draggable' + (i + 1)).draggable({
cursorAt: { left: cursorAtX, top: cursorAtY },
drag: function(event, ui) {
equals(ui.offset.left, expected.left, position + ' left');
equals(ui.offset.top, expected.top, position + ' top');
}
}),
before = el.offset(),
pos = {
clientX: before.left + offsetX,
clientY: before.top + offsetY
},
expected = {
left: before.left + offsetX - cursorAtX + deltaX,
top: before.top + offsetY - cursorAtY + deltaY
};
el.simulate("mousedown", pos);
pos.clientX += deltaX;
pos.clientY += deltaY;
$(document).simulate("mousemove", pos);
el.simulate("mouseup", pos);
});
});
test("{ cursorAt: '20, 40' }", function() {
expect(4);
var deltaX = -3, deltaY = -3,
offsetX = 5, offsetY = 5,
cursorAtX = 20, cursorAtY = 40;
$.each(['relative', 'absolute'], function(i, position) {
var el = $('#draggable' + (i + 1)).draggable({
cursorAt: { left: cursorAtX, top: cursorAtY },
drag: function(event, ui) {
equals(ui.offset.left, expected.left, position + ' left');
equals(ui.offset.top, expected.top, position + ' top');
}
}),
before = el.offset(),
pos = {
clientX: before.left + offsetX,
clientY: before.top + offsetY
},
expected = {
left: before.left + offsetX - cursorAtX + deltaX,
top: before.top + offsetY - cursorAtY + deltaY
};
el.simulate("mousedown", pos);
pos.clientX += deltaX;
pos.clientY += deltaY;
$(document).simulate("mousemove", pos);
el.simulate("mouseup", pos);
});
});
test("{ distance: 10 }", function() {
el = $("#draggable2").draggable({ distance: 10 });
drag(el, -9, -9);
moved(0, 0, 'distance not met');
drag(el, -10, -10);
moved(-10, -10, 'distance met');
drag(el, 9, 9);
moved(0, 0, 'distance not met');
});
test("{ grid: [50, 50] }, relative", function() {
el = $("#draggable1").draggable({ grid: [50, 50] });
drag(el, 24, 24);
moved(0, 0);
drag(el, 26, 25);
moved(50, 50);
});
test("{ grid: [50, 50] }, absolute", function() {
el = $("#draggable2").draggable({ grid: [50, 50] });
drag(el, 24, 24);
moved(0, 0);
drag(el, 26, 25);
moved(50, 50);
});
test("{ handle: 'span' }", function() {
el = $("#draggable2").draggable({ handle: 'span' });
drag("#draggable2 span", 50, 50);
moved(50, 50, "drag span");
drag("#draggable2", 50, 50);
moved(0, 0, "drag element");
});
test("{ opacity: 0.5 }", function() {
expect(1);
var opacity = null;
el = $("#draggable2").draggable({
opacity: 0.5,
start: function(event, ui) {
opacity = $(this).css("opacity");
}
});
drag("#draggable2", -1, -1);
equals(opacity, 0.5, "start callback: opacity is");
});
test("{ zIndex: 10 }", function() {
expect(1);
var expected = 10, actual;
var zIndex = null;
el = $("#draggable2").draggable({
zIndex: expected,
start: function(event, ui) {
actual = $(this).css("zIndex");
}
});
drag("#draggable2", -1, -1);
equals(actual, expected, "start callback: zIndex is");
});
})(jQuery);