2 var BOUNDS_FORMAT, MIRROR_ATTACH, defer, extend, getBoundingRect, getBounds, getOuterSize, getSize, updateClasses, _ref,
3 __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
5 _ref = this.Tether.Utils, getOuterSize = _ref.getOuterSize, getBounds = _ref.getBounds, getSize = _ref.getSize, extend = _ref.extend, updateClasses = _ref.updateClasses, defer = _ref.defer;
15 BOUNDS_FORMAT = ['left', 'top', 'right', 'bottom'];
17 getBoundingRect = function(tether, to) {
18 var i, pos, side, size, style, _i, _len;
19 if (to === 'scrollParent') {
20 to = tether.scrollParent;
21 } else if (to === 'window') {
22 to = [pageXOffset, pageYOffset, innerWidth + pageXOffset, innerHeight + pageYOffset];
24 if (to === document) {
25 to = to.documentElement;
27 if (to.nodeType != null) {
28 pos = size = getBounds(to);
29 style = getComputedStyle(to);
30 to = [pos.left, pos.top, size.width + pos.left, size.height + pos.top];
31 for (i = _i = 0, _len = BOUNDS_FORMAT.length; _i < _len; i = ++_i) {
32 side = BOUNDS_FORMAT[i];
33 side = side[0].toUpperCase() + side.substr(1);
34 if (side === 'Top' || side === 'Left') {
35 to[i] += parseFloat(style["border" + side + "Width"]);
37 to[i] -= parseFloat(style["border" + side + "Width"]);
44 this.Tether.modules.push({
45 position: function(_arg) {
46 var addClasses, allClasses, attachment, bounds, changeAttachX, changeAttachY, cls, constraint, eAttachment, height, left, oob, oobClass, p, pin, pinned, pinnedClass, removeClass, side, tAttachment, targetAttachment, targetHeight, targetSize, targetWidth, to, top, width, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8,
48 top = _arg.top, left = _arg.left, targetAttachment = _arg.targetAttachment;
49 if (!this.options.constraints) {
52 removeClass = function(prefix) {
53 var side, _i, _len, _results;
54 _this.removeClass(prefix);
56 for (_i = 0, _len = BOUNDS_FORMAT.length; _i < _len; _i++) {
57 side = BOUNDS_FORMAT[_i];
58 _results.push(_this.removeClass("" + prefix + "-" + side));
62 _ref1 = this.cache('element-bounds', function() {
63 return getBounds(_this.element);
64 }), height = _ref1.height, width = _ref1.width;
65 if (width === 0 && height === 0 && (this.lastSize != null)) {
66 _ref2 = this.lastSize, width = _ref2.width, height = _ref2.height;
68 targetSize = this.cache('target-bounds', function() {
69 return _this.getTargetBounds();
71 targetHeight = targetSize.height;
72 targetWidth = targetSize.width;
75 allClasses = [this.getClass('pinned'), this.getClass('out-of-bounds')];
76 _ref3 = this.options.constraints;
77 for (_i = 0, _len = _ref3.length; _i < _len; _i++) {
78 constraint = _ref3[_i];
79 if (constraint.outOfBoundsClass) {
80 allClasses.push(constraint.outOfBoundsClass);
82 if (constraint.pinnedClass) {
83 allClasses.push(constraint.pinnedClass);
86 for (_j = 0, _len1 = allClasses.length; _j < _len1; _j++) {
88 _ref4 = ['left', 'top', 'right', 'bottom'];
89 for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
91 allClasses.push("" + cls + "-" + side);
95 tAttachment = extend({}, targetAttachment);
96 eAttachment = extend({}, this.attachment);
97 _ref5 = this.options.constraints;
98 for (_l = 0, _len3 = _ref5.length; _l < _len3; _l++) {
99 constraint = _ref5[_l];
100 to = constraint.to, attachment = constraint.attachment, pin = constraint.pin;
101 if (attachment == null) {
104 if (__indexOf.call(attachment, ' ') >= 0) {
105 _ref6 = attachment.split(' '), changeAttachY = _ref6[0], changeAttachX = _ref6[1];
107 changeAttachX = changeAttachY = attachment;
109 bounds = getBoundingRect(this, to);
110 if (changeAttachY === 'target' || changeAttachY === 'both') {
111 if (top < bounds[1] && tAttachment.top === 'top') {
113 tAttachment.top = 'bottom';
115 if (top + height > bounds[3] && tAttachment.top === 'bottom') {
117 tAttachment.top = 'top';
120 if (changeAttachY === 'together') {
121 if (top < bounds[1] && tAttachment.top === 'top') {
122 if (eAttachment.top === 'bottom') {
124 tAttachment.top = 'bottom';
126 eAttachment.top = 'top';
127 } else if (eAttachment.top === 'top') {
129 tAttachment.top = 'bottom';
131 eAttachment.top = 'bottom';
134 if (top + height > bounds[3] && tAttachment.top === 'bottom') {
135 if (eAttachment.top === 'top') {
137 tAttachment.top = 'top';
139 eAttachment.top = 'bottom';
140 } else if (eAttachment.top === 'bottom') {
142 tAttachment.top = 'top';
144 eAttachment.top = 'top';
147 if (tAttachment.top === 'middle') {
148 if (top + height > bounds[3] && eAttachment.top === 'top') {
150 eAttachment.top = 'bottom';
151 } else if (top < bounds[1] && eAttachment.top === 'bottom') {
153 eAttachment.top = 'top';
157 if (changeAttachX === 'target' || changeAttachX === 'both') {
158 if (left < bounds[0] && tAttachment.left === 'left') {
160 tAttachment.left = 'right';
162 if (left + width > bounds[2] && tAttachment.left === 'right') {
164 tAttachment.left = 'left';
167 if (changeAttachX === 'together') {
168 if (left < bounds[0] && tAttachment.left === 'left') {
169 if (eAttachment.left === 'right') {
171 tAttachment.left = 'right';
173 eAttachment.left = 'left';
174 } else if (eAttachment.left === 'left') {
176 tAttachment.left = 'right';
178 eAttachment.left = 'right';
180 } else if (left + width > bounds[2] && tAttachment.left === 'right') {
181 if (eAttachment.left === 'left') {
183 tAttachment.left = 'left';
185 eAttachment.left = 'right';
186 } else if (eAttachment.left === 'right') {
188 tAttachment.left = 'left';
190 eAttachment.left = 'left';
192 } else if (tAttachment.left === 'center') {
193 if (left + width > bounds[2] && eAttachment.left === 'left') {
195 eAttachment.left = 'right';
196 } else if (left < bounds[0] && eAttachment.left === 'right') {
198 eAttachment.left = 'left';
202 if (changeAttachY === 'element' || changeAttachY === 'both') {
203 if (top < bounds[1] && eAttachment.top === 'bottom') {
205 eAttachment.top = 'top';
207 if (top + height > bounds[3] && eAttachment.top === 'top') {
209 eAttachment.top = 'bottom';
212 if (changeAttachX === 'element' || changeAttachX === 'both') {
213 if (left < bounds[0] && eAttachment.left === 'right') {
215 eAttachment.left = 'left';
217 if (left + width > bounds[2] && eAttachment.left === 'left') {
219 eAttachment.left = 'right';
222 if (typeof pin === 'string') {
224 var _len4, _m, _ref7, _results;
225 _ref7 = pin.split(',');
227 for (_m = 0, _len4 = _ref7.length; _m < _len4; _m++) {
229 _results.push(p.trim());
233 } else if (pin === true) {
234 pin = ['top', 'left', 'right', 'bottom'];
239 if (top < bounds[1]) {
240 if (__indexOf.call(pin, 'top') >= 0) {
247 if (top + height > bounds[3]) {
248 if (__indexOf.call(pin, 'bottom') >= 0) {
249 top = bounds[3] - height;
250 pinned.push('bottom');
255 if (left < bounds[0]) {
256 if (__indexOf.call(pin, 'left') >= 0) {
263 if (left + width > bounds[2]) {
264 if (__indexOf.call(pin, 'right') >= 0) {
265 left = bounds[2] - width;
266 pinned.push('right');
272 pinnedClass = (_ref7 = this.options.pinnedClass) != null ? _ref7 : this.getClass('pinned');
273 addClasses.push(pinnedClass);
274 for (_m = 0, _len4 = pinned.length; _m < _len4; _m++) {
276 addClasses.push("" + pinnedClass + "-" + side);
280 oobClass = (_ref8 = this.options.outOfBoundsClass) != null ? _ref8 : this.getClass('out-of-bounds');
281 addClasses.push(oobClass);
282 for (_n = 0, _len5 = oob.length; _n < _len5; _n++) {
284 addClasses.push("" + oobClass + "-" + side);
287 if (__indexOf.call(pinned, 'left') >= 0 || __indexOf.call(pinned, 'right') >= 0) {
288 eAttachment.left = tAttachment.left = false;
290 if (__indexOf.call(pinned, 'top') >= 0 || __indexOf.call(pinned, 'bottom') >= 0) {
291 eAttachment.top = tAttachment.top = false;
293 if (tAttachment.top !== targetAttachment.top || tAttachment.left !== targetAttachment.left || eAttachment.top !== this.attachment.top || eAttachment.left !== this.attachment.left) {
294 this.updateAttachClasses(eAttachment, tAttachment);
298 updateClasses(_this.target, addClasses, allClasses);
299 return updateClasses(_this.element, addClasses, allClasses);