-
Notifications
You must be signed in to change notification settings - Fork 0
/
guides.js
129 lines (109 loc) · 3.54 KB
/
guides.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var MIN_DISTANCE = 20;
var verticalGuideId = "#guide-v";
var horizontalGuideId = "#guide-h";
var guides = [];
var innerOffsetX, innerOffsetY;
function computeGuidesForElement(elem, pos, w, h) {
if (elem != null) {
var $t = $(elem);
pos = $t.offset();
w = $t.outerWidth() - 1;
h = $t.outerHeight() - 1;
}
return [{
type: "v",
left: pos.left,
top: pos.top
}, {
type: "v",
left: pos.left,
top: pos.top + h
}, {
type: "h",
left: pos.left,
top: pos.top
}, {
type: "h",
left: pos.left + w,
top: pos.top
}, {
type: "v",
left: pos.left,
top: pos.top + h / 2
}, {
type: "h",
left: pos.left + w / 2,
top: pos.top
}];
}
var debounceFun = _.debounce(function(event, ui) {
var self = this;
var chosenGuides = {
top: {
dist: MIN_DISTANCE + 1
},
left: {
dist: MIN_DISTANCE + 1
}
};
var thisNode = $(self);
var pos = thisNode.offset();
if (!(pos && (pos["left"] || (pos["left"] === 0)) && (pos["left"] || (pos["left"] === 0)))) {
console.error(1,"throw no pos");
pos = {
top: event.originalEvent.pageY - event.originalEvent.offsetY,
left: event.originalEvent.pageX - event.originalEvent.offsetX
};
}
var w = thisNode.outerWidth() - 1;
var h = thisNode.outerHeight() - 1;
var elemGuides = computeGuidesForElement(null, pos, w, h);
console.info("position (",pos.top,",",pos.left,")")
$.each(guides, function(i, guide) {
$.each(elemGuides, function(j, elemGuide) {
if (guide.type === elemGuide.type) {
var prop = (guide.type === "v") ? "top" : "left";
var d = Math.abs(elemGuide[prop] - guide[prop]);
if (d < chosenGuides[prop].dist) {
chosenGuides[prop].dist = d;
chosenGuides[prop].offset = elemGuide[prop] - pos[prop];
chosenGuides[prop].guide = guide;
}
}
});
});
console.debug("chosenGuides ", chosenGuides);
console.debug("top,left", ui.position.top, ",", ui.position.left);
if (chosenGuides.top.dist <= MIN_DISTANCE) {
$(horizontalGuideId).css("top", chosenGuides.top.guide.top).show();
console.warn("top move ->", (chosenGuides.top.guide.top - chosenGuides.top.offset));
ui.helper.offset({top: (chosenGuides.top.guide.top - chosenGuides.top.offset) });
}
else {
$(horizontalGuideId).hide();
ui.helper.offset({top: pos.top });
}
if (chosenGuides.left.dist <= MIN_DISTANCE) {
$(verticalGuideId).css("left", chosenGuides.left.guide.left).show();
console.warn("left move->", (chosenGuides.left.guide.left - chosenGuides.left.offset));
ui.helper.offset({left: (chosenGuides.left.guide.left - chosenGuides.left.offset) });
}
else {
$(verticalGuideId).hide();
ui.helper.offset({left: pos.left });
}
}, 100);
$(".draggable").draggable({
start: function(event) {
var siblingsNodes = $(this).siblings(".draggable");
guides = $.map(siblingsNodes, computeGuidesForElement);
innerOffsetX = event.originalEvent.offsetX;
innerOffsetY = event.originalEvent.offsetY;
},
drag: function(event, ui) {
debounceFun.call(this, event, ui);
},
stop: function() {
$(verticalGuideId + "," + horizontalGuideId).hide();
}
});