forked from maxazan/jquery-ui-droppable-iframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-ui-droppable-iframe.js
134 lines (117 loc) · 5.29 KB
/
jquery-ui-droppable-iframe.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
130
131
132
133
134
/*
* jQuery ui dropppable Plugin
* https://github.com/maxazan/jquery-ui-droppable-iframe
*
* Copyright 2015, Pomazan Maksym
* Licensed under the MIT licenses.
*/
//Save native prepareOffsets method from ddmanager
var nativePrepareOffsets = $.ui.ddmanager.prepareOffsets;
//Overrided prepareOffsets method
$.ui.ddmanager.prepareOffsets = function(t, event) {
//Call parent method
nativePrepareOffsets.apply(this, arguments);
var m = $.ui.ddmanager.droppables[t.options.scope] || [];
for (i = 0; i < m.length; i++) {
if(m[i].offset == undefined){
continue;
}
//Iframe fixes
if ((doc = m[i].document[0]) !== document) {
var iframe = $((doc.defaultView || doc.parentWindow).frameElement);
var iframeOffset = iframe.offset();
var el = m[i].element;
//Check our droppable element is in the viewport of out iframe
var viewport = {
top: iframe.contents().scrollTop(),
left: iframe.contents().scrollLeft()
};
viewport.right = viewport.left + iframe.width();
viewport.bottom = viewport.top + iframe.height();
var bounds = el.offset();
bounds.right = bounds.left + el.outerWidth();
bounds.bottom = bounds.top + el.outerHeight();
if (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom)) {
//In view port
var ytop = bounds.top - iframe.contents().scrollTop();
ytop = ytop < 0 ? 0 : ytop;
var xtop = bounds.left - iframe.contents().scrollLeft();
xtop = xtop < 0 ? 0 : xtop;
var ybottom = bounds.top + el.height() - iframe.contents().scrollTop();
ybottom = ybottom > iframe.height() ? iframe.height() : ybottom;
var xbottom = bounds.left + el.width() - iframe.contents().scrollLeft();
xbottom = xbottom > iframe.width() ? iframe.width() : xbottom;
m[i].offset.top = iframeOffset.top + ytop;
m[i].offset.left = iframeOffset.left + xtop;
m[i].proportions({
width: xbottom - xtop,
height: ybottom - ytop,
});
} else {
//Out of view port - skip
m[i].proportions().height = 0;
continue;
}
}
}
};
$.ui.plugin.add("draggable", "iframeScroll", {
drag: function(event, ui, i) {
var o = i.options;
var selector = o.iframeFix === true ? "iframe" : o.iframeFix;
//check if mouse in scroll zone
i.document.find(selector).each(function() {
var scrolled = false;
var iframeDocument;
var iframe = $(this);
var offset = iframe.offset();
offset.width = iframe.width();
offset.height = iframe.height();
//Check scroll top
if (offset.left < event.pageX && event.pageX < offset.left + offset.width) {
if (offset.top < event.pageY && event.pageY < offset.top + o.scrollSensitivity) {
iframeDocument = iframe.contents();
scrolled = iframeDocument.scrollTop(iframeDocument.scrollTop() - o.scrollSpeed);
}
}
//Check scroll down
if (offset.left < event.pageX && event.pageX < offset.left + offset.width) {
if ((offset.top + offset.height - o.scrollSensitivity) < event.pageY && event.pageY < offset.top + offset.height) {
iframeDocument = iframe.contents();
scrolled = iframeDocument.scrollTop(iframeDocument.scrollTop() + o.scrollSpeed);
}
}
//Check scroll left
if (offset.left < event.pageX && event.pageX < offset.left + o.scrollSensitivity) {
if (offset.top < event.pageY && event.pageY < offset.top + offset.height) {
iframeDocument = iframe.contents();
scrolled = iframeDocument.scrollLeft(iframeDocument.scrollLeft() - o.scrollSpeed);
}
}
//Check scroll right
if ((offset.left + offset.width - o.scrollSensitivity) < event.pageX && event.pageX < offset.left + offset.width) {
if (offset.top < event.pageY && event.pageY < offset.top + offset.height) {
iframeDocument = iframe.contents();
scrolled = iframeDocument.scrollLeft(iframeDocument.scrollLeft() + o.scrollSpeed);
}
}
if (scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) {
$.ui.ddmanager.prepareOffsets(i, event);
}
clearTimeout(i.scrollTimer);
if (i._mouseStarted) {
i.scrollTimer = setTimeout(function() {
//call drag trigger
i._trigger("drag", event);
//update offsets
if ($.ui.ddmanager) {
$.ui.ddmanager.drag(i, event);
}
}, 10);
}
});
},
stop: function(event, ui, i) {
clearInterval(i.scrollTimer);
}
});