Skip to content

Commit 73c13bf

Browse files
committed
Added snapping plugin to resizable.
1 parent c86319e commit 73c13bf

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

ui/jquery.ui.resizable.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,125 @@ $.ui.plugin.add("resizable", "alsoResize", {
876876
}
877877
});
878878

879+
$.ui.plugin.add("resizable", "snap", {
880+
start: function(event, ui) {
881+
var self = $(this).data("ui-resizable"), o = self.options;
882+
self.snapElements = [];
883+
var useOffset = self._helper != null;
884+
885+
$(o.snap.constructor != String ? ( o.snap.items || ':data(ui-resizable)' ) : o.snap).each(function() {
886+
if(this != self.element[0]){
887+
888+
var inst = $(this).data("ui-resizable");
889+
var el = null;
890+
if(inst != null) {
891+
el = inst.element;
892+
}
893+
else {
894+
el = $(this);
895+
}
896+
897+
var curleft = useOffset ? el.offset().left : parseFloat(el.css('left'), 10) || 0;
898+
var curtop = useOffset ? el.offset().top : parseFloat(el.css('top'), 10) || 0;
899+
900+
var curwidth = el.width();
901+
var curheight = el.height();
902+
903+
self.snapElements.push({
904+
item: this,
905+
width: curwidth,
906+
height: curheight,
907+
top: curtop,
908+
left: curleft
909+
});
910+
}
911+
});
912+
913+
},
914+
resize: function(event, ui) {
915+
var self = $(this).data("ui-resizable"), o = self.options;
916+
917+
var d = (o.snapTolerance === undefined ? 20 : o.snapTolerance);
918+
var mode = (o.snapMode === undefined ? 'both' : o.snapMode);
919+
920+
var x1 = ui.position.left, x2 = ui.position.left + ui.size.width,
921+
y1 = ui.position.top, y2 = ui.position.top + ui.size.height;
922+
923+
var axis = {};
924+
axis.n = self.axis.indexOf("n") != -1;
925+
axis.s = self.axis.indexOf("s") != -1;
926+
axis.w = self.axis.indexOf("w") != -1;
927+
axis.e = self.axis.indexOf("e") != -1;
928+
929+
// Container to hold returned position and size.
930+
var snapped = {};
931+
932+
for (var index = self.snapElements.length - 1; index >= 0; index--){
933+
934+
var l = self.snapElements[index].left, r = l + self.snapElements[index].width,
935+
t = self.snapElements[index].top, b = t + self.snapElements[index].height;
936+
937+
var o = {};
938+
var i = {};
939+
940+
// Check for snapping on the outside of the element.
941+
if(mode != 'inner') {
942+
o.ts = Math.abs(t - y2) <= d;
943+
o.bs = Math.abs(b - y1) <= d;
944+
o.ls = Math.abs(l - x2) <= d;
945+
o.rs = Math.abs(r - x1) <= d;
946+
}
947+
948+
// Check for snapping on the inside of the element.
949+
if(mode != 'outer') {
950+
i.ts = Math.abs(t - y1) <= d;
951+
i.bs = Math.abs(b - y2) <= d;
952+
i.ls = Math.abs(l - x1) <= d;
953+
i.rs = Math.abs(r - x2) <= d;
954+
}
955+
956+
// If the resizeable would touch the snapElement if they were snapped together.
957+
var v = (y2 >= t && y2 <= b) || (y1 >= t && y1 <= b) || (y1 <= t && y2 >= b) || o.ts || o.bs || i.ts || i.bs;
958+
var h = (x2 >= l && x2 <= r) || (x1 >= l && x1 <= r) || (x1 <= l && x2 >= r) || o.ls || o.rs || i.ls || i.rs;
959+
960+
if(o.ts && axis.s && h) {
961+
snapped.height = t - ui.position.top;
962+
}
963+
if(o.bs && axis.n && h) {
964+
snapped.height = ui.size.height - (b - ui.position.top);
965+
snapped.top = b;
966+
}
967+
if(o.ls && axis.e && v) {
968+
snapped.width = l - ui.position.left;
969+
}
970+
if(o.rs && axis.w && v) {
971+
snapped.width = ui.size.width - (r - ui.position.left);
972+
snapped.left = r;
973+
}
974+
975+
if(i.ts && axis.n && h) {
976+
snapped.height = ui.size.height - (t - ui.position.top);
977+
snapped.top = t;
978+
}
979+
if(i.bs && axis.s && h) {
980+
snapped.height = b - ui.position.top;
981+
}
982+
if(i.ls && axis.w && v) {
983+
snapped.width = ui.size.width - (l - ui.position.left);
984+
snapped.left = l;
985+
}
986+
if(i.rs && axis.e && v) {
987+
snapped.width = r - ui.position.left;
988+
}
989+
}
990+
991+
if(snapped.left != undefined) ui.position.left = snapped.left;
992+
if(snapped.top != undefined) ui.position.top = snapped.top;
993+
if(snapped.width != undefined) ui.size.width = snapped.width;
994+
if(snapped.height != undefined) ui.size.height = snapped.height;
995+
},
996+
});
997+
879998
$.ui.plugin.add("resizable", "ghost", {
880999

8811000
start: function() {

0 commit comments

Comments
 (0)