-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmodal.js
216 lines (167 loc) · 6.44 KB
/
unmodal.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// unmodal jQuery Plugin
// Copyright (c) 2012 Foliotek Inc.
// MIT License
// https://github.com/Foliotek/ajaxq
// Contains inverse-intersection under MIT License
// https://github.com/bgrins/inverse-intersection
(function($) {
var blockCSS = { position: "absolute" };
var blockMarkup = "<div class='unmodal unmodal-hidden' />";
var visible = false;
var inverseIntersection = (function() {
function intersection(r1, r2) {
var x0 = Math.max(r1.left, r2.left);
var x1 = Math.min(r1.left + r1.width, r2.left + r2.width);
if (x0 <= x1) {
var y0 = Math.max(r1.top, r2.top);
var y1 = Math.min(r1.top + r1.height, r2.top + r2.height);
if (y0 <= y1) {
return {
left: x0,
top: y0,
width: x1 - x0,
height: y1 - y0
};
}
}
return false;
}
function filterWithArea(rectangles) {
var output = [];
for (var i = 0; i < rectangles.length; i++) {
if (rectangles[i].width > 0 && rectangles[i].height > 0) {
output.push(rectangles[i]);
}
}
return output;
}
function splitRectangleOnIntersection(r, mask) {
var inter = intersection(mask, r);
if (!inter) {
return [r];
}
// If there is an intersection, return 4 rectangles surrounding this box
var rectangles = filterWithArea([
{
left: r.left,
top: r.top,
width: r.width,
height: inter.top - r.top
},
{
left: r.left,
top: inter.top + inter.height,
width: r.width,
height: (r.top + r.height) - (inter.top + inter.height)
},
{
left: r.left,
top: inter.top,
width: inter.left - r.left,
height: inter.height
},
{
left: inter.left + inter.width,
top: inter.top,
width: (r.left + r.width) - (inter.left + inter.width), height: inter.height
}
]);
return rectangles;
}
return function (parentBounds, allBounds) {
if (!allBounds.length) {
return [];
}
var outputBounds = [parentBounds];
for (var i = 0; i < allBounds.length; i++) {
var newOutputs = [];
for (var j = 0; j < outputBounds.length; j++) {
var inters = splitRectangleOnIntersection(outputBounds[j], allBounds[i]);
newOutputs = newOutputs.concat(inters);
}
outputBounds = newOutputs;
}
return outputBounds;
};
})();
function removeBoxes(doc) {
$(".unmodal", doc || document).remove();
}
function appendBoxes(doc, nodes) {
var bounds = nodes.map(function () {
var node = $(this);
if (node.is(":visible")) {
var rect = node.offset();
rect.width = node.outerWidth();
rect.height = node.outerHeight();
return rect;
}
}).toArray();
var parentRect = { left:0, top:0, width: $(doc).width(), height: $(doc).height() };
var boxes = inverseIntersection(parentRect, bounds);
var blocks = [];
for (var i = 0; i < boxes.length; i++) {
var box = boxes[i];
var block = $(blockMarkup).css(blockCSS);
block.width(box.width).height(box.height).css({
left: box.left,
top: box.top
});
blocks.push(block[0]);
}
removeBoxes(doc);
function removeHiddenClass() {
$(blocks).removeClass("unmodal-hidden");
}
if (visible) {
$(blocks).appendTo(doc.body);
removeHiddenClass();
}
else {
$(blocks).appendTo(doc.body);
setTimeout(removeHiddenClass, 1);
}
visible = true;
}
$.fn.unmodal = function(opts) {
if (this.length) {
var o = $.extend({}, $.fn.unmodal.defaults, opts);
var doc = this[0].ownerDocument || document;
var body = doc.body;
var nodes = this.not(".unmodal");
$(doc).data("hide.unmodal", o.hide);
$(doc).unbind("click.unmodal").delegate(".unmodal", "click.unmodal", function() {
$.fn.unmodal.clear(doc);
return false;
});
$(doc).unbind("selectstart.unmodal").bind("selectstart.unmodal", function() {
return false;
});
$(doc).unbind("keydown.unmodal").bind("keydown.unmodal", function(e) {
if (e.keyCode == 27) {
$.fn.unmodal.clear(doc);
}
});
$.fn.unmodal.refresh = function(e) {
appendBoxes(doc, nodes, true);
};
$(window).unbind("resize.unmodal input.unmodal").bind("resize.unmodal input.unmodal", $.fn.unmodal.refresh);
appendBoxes(doc, nodes);
}
return this;
};
$.fn.unmodal.defaults = {
hide: $.noop
};
$.fn.unmodal.clear = function(doc) {
removeBoxes(doc);
visible = false;
$(window).unbind("resize.unmodal input.unmodal");
$(doc || document).unbind("click.unmodal keydown.unmodal selectstart.unmodal");
if ($(doc || document).data("hide.unmodal")) {
$(doc || document).data("hide.unmodal").apply();
$(doc || document).removeData("hide.unmodal");
}
};
$.fn.unmodal.refresh = $.noop;
})(jQuery);