Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / tether / js / constraint.js
1 (function() {
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; };
4
5   _ref = this.Tether.Utils, getOuterSize = _ref.getOuterSize, getBounds = _ref.getBounds, getSize = _ref.getSize, extend = _ref.extend, updateClasses = _ref.updateClasses, defer = _ref.defer;
6
7   MIRROR_ATTACH = {
8     left: 'right',
9     right: 'left',
10     top: 'bottom',
11     bottom: 'top',
12     middle: 'middle'
13   };
14
15   BOUNDS_FORMAT = ['left', 'top', 'right', 'bottom'];
16
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];
23     }
24     if (to === document) {
25       to = to.documentElement;
26     }
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"]);
36         } else {
37           to[i] -= parseFloat(style["border" + side + "Width"]);
38         }
39       }
40     }
41     return to;
42   };
43
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,
47         _this = this;
48       top = _arg.top, left = _arg.left, targetAttachment = _arg.targetAttachment;
49       if (!this.options.constraints) {
50         return true;
51       }
52       removeClass = function(prefix) {
53         var side, _i, _len, _results;
54         _this.removeClass(prefix);
55         _results = [];
56         for (_i = 0, _len = BOUNDS_FORMAT.length; _i < _len; _i++) {
57           side = BOUNDS_FORMAT[_i];
58           _results.push(_this.removeClass("" + prefix + "-" + side));
59         }
60         return _results;
61       };
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;
67       }
68       targetSize = this.cache('target-bounds', function() {
69         return _this.getTargetBounds();
70       });
71       targetHeight = targetSize.height;
72       targetWidth = targetSize.width;
73       tAttachment = {};
74       eAttachment = {};
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);
81         }
82         if (constraint.pinnedClass) {
83           allClasses.push(constraint.pinnedClass);
84         }
85       }
86       for (_j = 0, _len1 = allClasses.length; _j < _len1; _j++) {
87         cls = allClasses[_j];
88         _ref4 = ['left', 'top', 'right', 'bottom'];
89         for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
90           side = _ref4[_k];
91           allClasses.push("" + cls + "-" + side);
92         }
93       }
94       addClasses = [];
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) {
102           attachment = '';
103         }
104         if (__indexOf.call(attachment, ' ') >= 0) {
105           _ref6 = attachment.split(' '), changeAttachY = _ref6[0], changeAttachX = _ref6[1];
106         } else {
107           changeAttachX = changeAttachY = attachment;
108         }
109         bounds = getBoundingRect(this, to);
110         if (changeAttachY === 'target' || changeAttachY === 'both') {
111           if (top < bounds[1] && tAttachment.top === 'top') {
112             top += targetHeight;
113             tAttachment.top = 'bottom';
114           }
115           if (top + height > bounds[3] && tAttachment.top === 'bottom') {
116             top -= targetHeight;
117             tAttachment.top = 'top';
118           }
119         }
120         if (changeAttachY === 'together') {
121           if (top < bounds[1] && tAttachment.top === 'top') {
122             if (eAttachment.top === 'bottom') {
123               top += targetHeight;
124               tAttachment.top = 'bottom';
125               top += height;
126               eAttachment.top = 'top';
127             } else if (eAttachment.top === 'top') {
128               top += targetHeight;
129               tAttachment.top = 'bottom';
130               top -= height;
131               eAttachment.top = 'bottom';
132             }
133           }
134           if (top + height > bounds[3] && tAttachment.top === 'bottom') {
135             if (eAttachment.top === 'top') {
136               top -= targetHeight;
137               tAttachment.top = 'top';
138               top -= height;
139               eAttachment.top = 'bottom';
140             } else if (eAttachment.top === 'bottom') {
141               top -= targetHeight;
142               tAttachment.top = 'top';
143               top += height;
144               eAttachment.top = 'top';
145             }
146           }
147           if (tAttachment.top === 'middle') {
148             if (top + height > bounds[3] && eAttachment.top === 'top') {
149               top -= height;
150               eAttachment.top = 'bottom';
151             } else if (top < bounds[1] && eAttachment.top === 'bottom') {
152               top += height;
153               eAttachment.top = 'top';
154             }
155           }
156         }
157         if (changeAttachX === 'target' || changeAttachX === 'both') {
158           if (left < bounds[0] && tAttachment.left === 'left') {
159             left += targetWidth;
160             tAttachment.left = 'right';
161           }
162           if (left + width > bounds[2] && tAttachment.left === 'right') {
163             left -= targetWidth;
164             tAttachment.left = 'left';
165           }
166         }
167         if (changeAttachX === 'together') {
168           if (left < bounds[0] && tAttachment.left === 'left') {
169             if (eAttachment.left === 'right') {
170               left += targetWidth;
171               tAttachment.left = 'right';
172               left += width;
173               eAttachment.left = 'left';
174             } else if (eAttachment.left === 'left') {
175               left += targetWidth;
176               tAttachment.left = 'right';
177               left -= width;
178               eAttachment.left = 'right';
179             }
180           } else if (left + width > bounds[2] && tAttachment.left === 'right') {
181             if (eAttachment.left === 'left') {
182               left -= targetWidth;
183               tAttachment.left = 'left';
184               left -= width;
185               eAttachment.left = 'right';
186             } else if (eAttachment.left === 'right') {
187               left -= targetWidth;
188               tAttachment.left = 'left';
189               left += width;
190               eAttachment.left = 'left';
191             }
192           } else if (tAttachment.left === 'center') {
193             if (left + width > bounds[2] && eAttachment.left === 'left') {
194               left -= width;
195               eAttachment.left = 'right';
196             } else if (left < bounds[0] && eAttachment.left === 'right') {
197               left += width;
198               eAttachment.left = 'left';
199             }
200           }
201         }
202         if (changeAttachY === 'element' || changeAttachY === 'both') {
203           if (top < bounds[1] && eAttachment.top === 'bottom') {
204             top += height;
205             eAttachment.top = 'top';
206           }
207           if (top + height > bounds[3] && eAttachment.top === 'top') {
208             top -= height;
209             eAttachment.top = 'bottom';
210           }
211         }
212         if (changeAttachX === 'element' || changeAttachX === 'both') {
213           if (left < bounds[0] && eAttachment.left === 'right') {
214             left += width;
215             eAttachment.left = 'left';
216           }
217           if (left + width > bounds[2] && eAttachment.left === 'left') {
218             left -= width;
219             eAttachment.left = 'right';
220           }
221         }
222         if (typeof pin === 'string') {
223           pin = (function() {
224             var _len4, _m, _ref7, _results;
225             _ref7 = pin.split(',');
226             _results = [];
227             for (_m = 0, _len4 = _ref7.length; _m < _len4; _m++) {
228               p = _ref7[_m];
229               _results.push(p.trim());
230             }
231             return _results;
232           })();
233         } else if (pin === true) {
234           pin = ['top', 'left', 'right', 'bottom'];
235         }
236         pin || (pin = []);
237         pinned = [];
238         oob = [];
239         if (top < bounds[1]) {
240           if (__indexOf.call(pin, 'top') >= 0) {
241             top = bounds[1];
242             pinned.push('top');
243           } else {
244             oob.push('top');
245           }
246         }
247         if (top + height > bounds[3]) {
248           if (__indexOf.call(pin, 'bottom') >= 0) {
249             top = bounds[3] - height;
250             pinned.push('bottom');
251           } else {
252             oob.push('bottom');
253           }
254         }
255         if (left < bounds[0]) {
256           if (__indexOf.call(pin, 'left') >= 0) {
257             left = bounds[0];
258             pinned.push('left');
259           } else {
260             oob.push('left');
261           }
262         }
263         if (left + width > bounds[2]) {
264           if (__indexOf.call(pin, 'right') >= 0) {
265             left = bounds[2] - width;
266             pinned.push('right');
267           } else {
268             oob.push('right');
269           }
270         }
271         if (pinned.length) {
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++) {
275             side = pinned[_m];
276             addClasses.push("" + pinnedClass + "-" + side);
277           }
278         }
279         if (oob.length) {
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++) {
283             side = oob[_n];
284             addClasses.push("" + oobClass + "-" + side);
285           }
286         }
287         if (__indexOf.call(pinned, 'left') >= 0 || __indexOf.call(pinned, 'right') >= 0) {
288           eAttachment.left = tAttachment.left = false;
289         }
290         if (__indexOf.call(pinned, 'top') >= 0 || __indexOf.call(pinned, 'bottom') >= 0) {
291           eAttachment.top = tAttachment.top = false;
292         }
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);
295         }
296       }
297       defer(function() {
298         updateClasses(_this.target, addClasses, allClasses);
299         return updateClasses(_this.element, addClasses, allClasses);
300       });
301       return {
302         top: top,
303         left: left
304       };
305     }
306   });
307
308 }).call(this);