Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / tether / js / tether.js
1 (function() {
2   var MIRROR_LR, MIRROR_TB, OFFSET_MAP, Tether, addClass, addOffset, attachmentToOffset, autoToFixedAttachment, defer, extend, flush, getBounds, getOffsetParent, getOuterSize, getScrollBarSize, getScrollParent, getSize, now, offsetToPx, parseAttachment, parseOffset, position, removeClass, tethers, transformKey, updateClasses, within, _Tether, _ref,
3     __slice = [].slice,
4     __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
5
6   if (this.Tether == null) {
7     throw new Error("You must include the utils.js file before tether.js");
8   }
9
10   Tether = this.Tether;
11
12   _ref = Tether.Utils, getScrollParent = _ref.getScrollParent, getSize = _ref.getSize, getOuterSize = _ref.getOuterSize, getBounds = _ref.getBounds, getOffsetParent = _ref.getOffsetParent, extend = _ref.extend, addClass = _ref.addClass, removeClass = _ref.removeClass, updateClasses = _ref.updateClasses, defer = _ref.defer, flush = _ref.flush, getScrollBarSize = _ref.getScrollBarSize;
13
14   within = function(a, b, diff) {
15     if (diff == null) {
16       diff = 1;
17     }
18     return (a + diff >= b && b >= a - diff);
19   };
20
21   transformKey = (function() {
22     var el, key, _i, _len, _ref1;
23     el = document.createElement('div');
24     _ref1 = ['transform', 'webkitTransform', 'OTransform', 'MozTransform', 'msTransform'];
25     for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
26       key = _ref1[_i];
27       if (el.style[key] !== void 0) {
28         return key;
29       }
30     }
31   })();
32
33   tethers = [];
34
35   position = function() {
36     var tether, _i, _len;
37     for (_i = 0, _len = tethers.length; _i < _len; _i++) {
38       tether = tethers[_i];
39       tether.position(false);
40     }
41     return flush();
42   };
43
44   now = function() {
45     var _ref1;
46     return (_ref1 = typeof performance !== "undefined" && performance !== null ? typeof performance.now === "function" ? performance.now() : void 0 : void 0) != null ? _ref1 : +(new Date);
47   };
48
49   (function() {
50     var event, lastCall, lastDuration, pendingTimeout, tick, _i, _len, _ref1, _results;
51     lastCall = null;
52     lastDuration = null;
53     pendingTimeout = null;
54     tick = function() {
55       if ((lastDuration != null) && lastDuration > 16) {
56         lastDuration = Math.min(lastDuration - 16, 250);
57         pendingTimeout = setTimeout(tick, 250);
58         return;
59       }
60       if ((lastCall != null) && (now() - lastCall) < 10) {
61         return;
62       }
63       if (pendingTimeout != null) {
64         clearTimeout(pendingTimeout);
65         pendingTimeout = null;
66       }
67       lastCall = now();
68       position();
69       return lastDuration = now() - lastCall;
70     };
71     _ref1 = ['resize', 'scroll', 'touchmove'];
72     _results = [];
73     for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
74       event = _ref1[_i];
75       _results.push(window.addEventListener(event, tick));
76     }
77     return _results;
78   })();
79
80   MIRROR_LR = {
81     center: 'center',
82     left: 'right',
83     right: 'left'
84   };
85
86   MIRROR_TB = {
87     middle: 'middle',
88     top: 'bottom',
89     bottom: 'top'
90   };
91
92   OFFSET_MAP = {
93     top: 0,
94     left: 0,
95     middle: '50%',
96     center: '50%',
97     bottom: '100%',
98     right: '100%'
99   };
100
101   autoToFixedAttachment = function(attachment, relativeToAttachment) {
102     var left, top;
103     left = attachment.left, top = attachment.top;
104     if (left === 'auto') {
105       left = MIRROR_LR[relativeToAttachment.left];
106     }
107     if (top === 'auto') {
108       top = MIRROR_TB[relativeToAttachment.top];
109     }
110     return {
111       left: left,
112       top: top
113     };
114   };
115
116   attachmentToOffset = function(attachment) {
117     var _ref1, _ref2;
118     return {
119       left: (_ref1 = OFFSET_MAP[attachment.left]) != null ? _ref1 : attachment.left,
120       top: (_ref2 = OFFSET_MAP[attachment.top]) != null ? _ref2 : attachment.top
121     };
122   };
123
124   addOffset = function() {
125     var left, offsets, out, top, _i, _len, _ref1;
126     offsets = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
127     out = {
128       top: 0,
129       left: 0
130     };
131     for (_i = 0, _len = offsets.length; _i < _len; _i++) {
132       _ref1 = offsets[_i], top = _ref1.top, left = _ref1.left;
133       if (typeof top === 'string') {
134         top = parseFloat(top, 10);
135       }
136       if (typeof left === 'string') {
137         left = parseFloat(left, 10);
138       }
139       out.top += top;
140       out.left += left;
141     }
142     return out;
143   };
144
145   offsetToPx = function(offset, size) {
146     if (typeof offset.left === 'string' && offset.left.indexOf('%') !== -1) {
147       offset.left = parseFloat(offset.left, 10) / 100 * size.width;
148     }
149     if (typeof offset.top === 'string' && offset.top.indexOf('%') !== -1) {
150       offset.top = parseFloat(offset.top, 10) / 100 * size.height;
151     }
152     return offset;
153   };
154
155   parseAttachment = parseOffset = function(value) {
156     var left, top, _ref1;
157     _ref1 = value.split(' '), top = _ref1[0], left = _ref1[1];
158     return {
159       top: top,
160       left: left
161     };
162   };
163
164   _Tether = (function() {
165     _Tether.modules = [];
166
167     function _Tether(options) {
168       this.position = __bind(this.position, this);
169       var module, _i, _len, _ref1, _ref2;
170       tethers.push(this);
171       this.history = [];
172       this.setOptions(options, false);
173       _ref1 = Tether.modules;
174       for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
175         module = _ref1[_i];
176         if ((_ref2 = module.initialize) != null) {
177           _ref2.call(this);
178         }
179       }
180       this.position();
181     }
182
183     _Tether.prototype.getClass = function(key) {
184       var _ref1, _ref2;
185       if ((_ref1 = this.options.classes) != null ? _ref1[key] : void 0) {
186         return this.options.classes[key];
187       } else if (((_ref2 = this.options.classes) != null ? _ref2[key] : void 0) !== false) {
188         if (this.options.classPrefix) {
189           return "" + this.options.classPrefix + "-" + key;
190         } else {
191           return key;
192         }
193       } else {
194         return '';
195       }
196     };
197
198     _Tether.prototype.setOptions = function(options, position) {
199       var defaults, key, _i, _len, _ref1, _ref2;
200       this.options = options;
201       if (position == null) {
202         position = true;
203       }
204       defaults = {
205         offset: '0 0',
206         targetOffset: '0 0',
207         targetAttachment: 'auto auto',
208         classPrefix: 'tether'
209       };
210       this.options = extend(defaults, this.options);
211       _ref1 = this.options, this.element = _ref1.element, this.target = _ref1.target, this.targetModifier = _ref1.targetModifier;
212       if (this.target === 'viewport') {
213         this.target = document.body;
214         this.targetModifier = 'visible';
215       } else if (this.target === 'scroll-handle') {
216         this.target = document.body;
217         this.targetModifier = 'scroll-handle';
218       }
219       _ref2 = ['element', 'target'];
220       for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
221         key = _ref2[_i];
222         if (this[key] == null) {
223           throw new Error("Tether Error: Both element and target must be defined");
224         }
225         if (this[key].jquery != null) {
226           this[key] = this[key][0];
227         } else if (typeof this[key] === 'string') {
228           this[key] = document.querySelector(this[key]);
229         }
230       }
231       addClass(this.element, this.getClass('element'));
232       addClass(this.target, this.getClass('target'));
233       if (!this.options.attachment) {
234         throw new Error("Tether Error: You must provide an attachment");
235       }
236       this.targetAttachment = parseAttachment(this.options.targetAttachment);
237       this.attachment = parseAttachment(this.options.attachment);
238       this.offset = parseOffset(this.options.offset);
239       this.targetOffset = parseOffset(this.options.targetOffset);
240       if (this.scrollParent != null) {
241         this.disable();
242       }
243       if (this.targetModifier === 'scroll-handle') {
244         this.scrollParent = this.target;
245       } else {
246         this.scrollParent = getScrollParent(this.target);
247       }
248       if (this.options.enabled !== false) {
249         return this.enable(position);
250       }
251     };
252
253     _Tether.prototype.getTargetBounds = function() {
254       var bounds, fitAdj, hasBottomScroll, height, out, scrollBottom, scrollPercentage, style, target;
255       if (this.targetModifier != null) {
256         switch (this.targetModifier) {
257           case 'visible':
258             if (this.target === document.body) {
259               return {
260                 top: pageYOffset,
261                 left: pageXOffset,
262                 height: innerHeight,
263                 width: innerWidth
264               };
265             } else {
266               bounds = getBounds(this.target);
267               out = {
268                 height: bounds.height,
269                 width: bounds.width,
270                 top: bounds.top,
271                 left: bounds.left
272               };
273               out.height = Math.min(out.height, bounds.height - (pageYOffset - bounds.top));
274               out.height = Math.min(out.height, bounds.height - ((bounds.top + bounds.height) - (pageYOffset + innerHeight)));
275               out.height = Math.min(innerHeight, out.height);
276               out.height -= 2;
277               out.width = Math.min(out.width, bounds.width - (pageXOffset - bounds.left));
278               out.width = Math.min(out.width, bounds.width - ((bounds.left + bounds.width) - (pageXOffset + innerWidth)));
279               out.width = Math.min(innerWidth, out.width);
280               out.width -= 2;
281               if (out.top < pageYOffset) {
282                 out.top = pageYOffset;
283               }
284               if (out.left < pageXOffset) {
285                 out.left = pageXOffset;
286               }
287               return out;
288             }
289             break;
290           case 'scroll-handle':
291             target = this.target;
292             if (target === document.body) {
293               target = document.documentElement;
294               bounds = {
295                 left: pageXOffset,
296                 top: pageYOffset,
297                 height: innerHeight,
298                 width: innerWidth
299               };
300             } else {
301               bounds = getBounds(target);
302             }
303             style = getComputedStyle(target);
304             hasBottomScroll = target.scrollWidth > target.clientWidth || 'scroll' === [style.overflow, style.overflowX] || this.target !== document.body;
305             scrollBottom = 0;
306             if (hasBottomScroll) {
307               scrollBottom = 15;
308             }
309             height = bounds.height - parseFloat(style.borderTopWidth) - parseFloat(style.borderBottomWidth) - scrollBottom;
310             out = {
311               width: 15,
312               height: height * 0.975 * (height / target.scrollHeight),
313               left: bounds.left + bounds.width - parseFloat(style.borderLeftWidth) - 15
314             };
315             fitAdj = 0;
316             if (height < 408 && this.target === document.body) {
317               fitAdj = -0.00011 * Math.pow(height, 2) - 0.00727 * height + 22.58;
318             }
319             if (this.target !== document.body) {
320               out.height = Math.max(out.height, 24);
321             }
322             scrollPercentage = this.target.scrollTop / (target.scrollHeight - height);
323             out.top = scrollPercentage * (height - out.height - fitAdj) + bounds.top + parseFloat(style.borderTopWidth);
324             if (this.target === document.body) {
325               out.height = Math.max(out.height, 24);
326             }
327             return out;
328         }
329       } else {
330         return getBounds(this.target);
331       }
332     };
333
334     _Tether.prototype.clearCache = function() {
335       return this._cache = {};
336     };
337
338     _Tether.prototype.cache = function(k, getter) {
339       if (this._cache == null) {
340         this._cache = {};
341       }
342       if (this._cache[k] == null) {
343         this._cache[k] = getter.call(this);
344       }
345       return this._cache[k];
346     };
347
348     _Tether.prototype.enable = function(position) {
349       if (position == null) {
350         position = true;
351       }
352       addClass(this.target, this.getClass('enabled'));
353       addClass(this.element, this.getClass('enabled'));
354       this.enabled = true;
355       if (this.scrollParent !== document) {
356         this.scrollParent.addEventListener('scroll', this.position);
357       }
358       if (position) {
359         return this.position();
360       }
361     };
362
363     _Tether.prototype.disable = function() {
364       removeClass(this.target, this.getClass('enabled'));
365       removeClass(this.element, this.getClass('enabled'));
366       this.enabled = false;
367       if (this.scrollParent != null) {
368         return this.scrollParent.removeEventListener('scroll', this.position);
369       }
370     };
371
372     _Tether.prototype.destroy = function() {
373       var i, tether, _i, _len, _results;
374       this.disable();
375       _results = [];
376       for (i = _i = 0, _len = tethers.length; _i < _len; i = ++_i) {
377         tether = tethers[i];
378         if (tether === this) {
379           tethers.splice(i, 1);
380           break;
381         } else {
382           _results.push(void 0);
383         }
384       }
385       return _results;
386     };
387
388     _Tether.prototype.updateAttachClasses = function(elementAttach, targetAttach) {
389       var add, all, side, sides, _i, _j, _len, _len1, _ref1,
390         _this = this;
391       if (elementAttach == null) {
392         elementAttach = this.attachment;
393       }
394       if (targetAttach == null) {
395         targetAttach = this.targetAttachment;
396       }
397       sides = ['left', 'top', 'bottom', 'right', 'middle', 'center'];
398       if ((_ref1 = this._addAttachClasses) != null ? _ref1.length : void 0) {
399         this._addAttachClasses.splice(0, this._addAttachClasses.length);
400       }
401       add = this._addAttachClasses != null ? this._addAttachClasses : this._addAttachClasses = [];
402       if (elementAttach.top) {
403         add.push("" + (this.getClass('element-attached')) + "-" + elementAttach.top);
404       }
405       if (elementAttach.left) {
406         add.push("" + (this.getClass('element-attached')) + "-" + elementAttach.left);
407       }
408       if (targetAttach.top) {
409         add.push("" + (this.getClass('target-attached')) + "-" + targetAttach.top);
410       }
411       if (targetAttach.left) {
412         add.push("" + (this.getClass('target-attached')) + "-" + targetAttach.left);
413       }
414       all = [];
415       for (_i = 0, _len = sides.length; _i < _len; _i++) {
416         side = sides[_i];
417         all.push("" + (this.getClass('element-attached')) + "-" + side);
418       }
419       for (_j = 0, _len1 = sides.length; _j < _len1; _j++) {
420         side = sides[_j];
421         all.push("" + (this.getClass('target-attached')) + "-" + side);
422       }
423       return defer(function() {
424         if (_this._addAttachClasses == null) {
425           return;
426         }
427         updateClasses(_this.element, _this._addAttachClasses, all);
428         updateClasses(_this.target, _this._addAttachClasses, all);
429         return _this._addAttachClasses = void 0;
430       });
431     };
432
433     _Tether.prototype.position = function(flushChanges) {
434       var elementPos, elementStyle, height, left, manualOffset, manualTargetOffset, module, next, offset, offsetBorder, offsetParent, offsetParentSize, offsetParentStyle, offsetPosition, ret, scrollLeft, scrollTop, scrollbarSize, side, targetAttachment, targetOffset, targetPos, targetSize, top, width, _i, _j, _len, _len1, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6,
435         _this = this;
436       if (flushChanges == null) {
437         flushChanges = true;
438       }
439       if (!this.enabled) {
440         return;
441       }
442       this.clearCache();
443       targetAttachment = autoToFixedAttachment(this.targetAttachment, this.attachment);
444       this.updateAttachClasses(this.attachment, targetAttachment);
445       elementPos = this.cache('element-bounds', function() {
446         return getBounds(_this.element);
447       });
448       width = elementPos.width, height = elementPos.height;
449       if (width === 0 && height === 0 && (this.lastSize != null)) {
450         _ref1 = this.lastSize, width = _ref1.width, height = _ref1.height;
451       } else {
452         this.lastSize = {
453           width: width,
454           height: height
455         };
456       }
457       targetSize = targetPos = this.cache('target-bounds', function() {
458         return _this.getTargetBounds();
459       });
460       offset = offsetToPx(attachmentToOffset(this.attachment), {
461         width: width,
462         height: height
463       });
464       targetOffset = offsetToPx(attachmentToOffset(targetAttachment), targetSize);
465       manualOffset = offsetToPx(this.offset, {
466         width: width,
467         height: height
468       });
469       manualTargetOffset = offsetToPx(this.targetOffset, targetSize);
470       offset = addOffset(offset, manualOffset);
471       targetOffset = addOffset(targetOffset, manualTargetOffset);
472       left = targetPos.left + targetOffset.left - offset.left;
473       top = targetPos.top + targetOffset.top - offset.top;
474       _ref2 = Tether.modules;
475       for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
476         module = _ref2[_i];
477         ret = module.position.call(this, {
478           left: left,
479           top: top,
480           targetAttachment: targetAttachment,
481           targetPos: targetPos,
482           attachment: this.attachment,
483           elementPos: elementPos,
484           offset: offset,
485           targetOffset: targetOffset,
486           manualOffset: manualOffset,
487           manualTargetOffset: manualTargetOffset,
488           scrollbarSize: scrollbarSize
489         });
490         if ((ret == null) || typeof ret !== 'object') {
491           continue;
492         } else if (ret === false) {
493           return false;
494         } else {
495           top = ret.top, left = ret.left;
496         }
497       }
498       next = {
499         page: {
500           top: top,
501           left: left
502         },
503         viewport: {
504           top: top - pageYOffset,
505           bottom: pageYOffset - top - height + innerHeight,
506           left: left - pageXOffset,
507           right: pageXOffset - left - width + innerWidth
508         }
509       };
510       if (document.body.scrollWidth > window.innerWidth) {
511         scrollbarSize = this.cache('scrollbar-size', getScrollBarSize);
512         next.viewport.bottom -= scrollbarSize.height;
513       }
514       if (document.body.scrollHeight > window.innerHeight) {
515         scrollbarSize = this.cache('scrollbar-size', getScrollBarSize);
516         next.viewport.right -= scrollbarSize.width;
517       }
518       if (((_ref3 = document.body.style.position) !== '' && _ref3 !== 'static') || ((_ref4 = document.body.parentElement.style.position) !== '' && _ref4 !== 'static')) {
519         next.page.bottom = document.body.scrollHeight - top - height;
520         next.page.right = document.body.scrollWidth - left - width;
521       }
522       if (((_ref5 = this.options.optimizations) != null ? _ref5.moveElement : void 0) !== false && (this.targetModifier == null)) {
523         offsetParent = this.cache('target-offsetparent', function() {
524           return getOffsetParent(_this.target);
525         });
526         offsetPosition = this.cache('target-offsetparent-bounds', function() {
527           return getBounds(offsetParent);
528         });
529         offsetParentStyle = getComputedStyle(offsetParent);
530         elementStyle = getComputedStyle(this.element);
531         offsetParentSize = offsetPosition;
532         offsetBorder = {};
533         _ref6 = ['Top', 'Left', 'Bottom', 'Right'];
534         for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) {
535           side = _ref6[_j];
536           offsetBorder[side.toLowerCase()] = parseFloat(offsetParentStyle["border" + side + "Width"]);
537         }
538         offsetPosition.right = document.body.scrollWidth - offsetPosition.left - offsetParentSize.width + offsetBorder.right;
539         offsetPosition.bottom = document.body.scrollHeight - offsetPosition.top - offsetParentSize.height + offsetBorder.bottom;
540         if (next.page.top >= (offsetPosition.top + offsetBorder.top) && next.page.bottom >= offsetPosition.bottom) {
541           if (next.page.left >= (offsetPosition.left + offsetBorder.left) && next.page.right >= offsetPosition.right) {
542             scrollTop = offsetParent.scrollTop;
543             scrollLeft = offsetParent.scrollLeft;
544             next.offset = {
545               top: next.page.top - offsetPosition.top + scrollTop - offsetBorder.top,
546               left: next.page.left - offsetPosition.left + scrollLeft - offsetBorder.left
547             };
548           }
549         }
550       }
551       this.move(next);
552       this.history.unshift(next);
553       if (this.history.length > 3) {
554         this.history.pop();
555       }
556       if (flushChanges) {
557         flush();
558       }
559       return true;
560     };
561
562     _Tether.prototype.move = function(position) {
563       var css, elVal, found, key, moved, offsetParent, point, same, transcribe, type, val, write, writeCSS, _i, _len, _ref1, _ref2,
564         _this = this;
565       if (this.element.parentNode == null) {
566         return;
567       }
568       same = {};
569       for (type in position) {
570         same[type] = {};
571         for (key in position[type]) {
572           found = false;
573           _ref1 = this.history;
574           for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
575             point = _ref1[_i];
576             if (!within((_ref2 = point[type]) != null ? _ref2[key] : void 0, position[type][key])) {
577               found = true;
578               break;
579             }
580           }
581           if (!found) {
582             same[type][key] = true;
583           }
584         }
585       }
586       css = {
587         top: '',
588         left: '',
589         right: '',
590         bottom: ''
591       };
592       transcribe = function(same, pos) {
593         var xPos, yPos, _ref3;
594         if (((_ref3 = _this.options.optimizations) != null ? _ref3.gpu : void 0) !== false) {
595           if (same.top) {
596             css.top = 0;
597             yPos = pos.top;
598           } else {
599             css.bottom = 0;
600             yPos = -pos.bottom;
601           }
602           if (same.left) {
603             css.left = 0;
604             xPos = pos.left;
605           } else {
606             css.right = 0;
607             xPos = -pos.right;
608           }
609           css[transformKey] = "translateX(" + (Math.round(xPos)) + "px) translateY(" + (Math.round(yPos)) + "px)";
610           if (transformKey !== 'msTransform') {
611             return css[transformKey] += " translateZ(0)";
612           }
613         } else {
614           if (same.top) {
615             css.top = "" + pos.top + "px";
616           } else {
617             css.bottom = "" + pos.bottom + "px";
618           }
619           if (same.left) {
620             return css.left = "" + pos.left + "px";
621           } else {
622             return css.right = "" + pos.right + "px";
623           }
624         }
625       };
626       moved = false;
627       if ((same.page.top || same.page.bottom) && (same.page.left || same.page.right)) {
628         css.position = 'absolute';
629         transcribe(same.page, position.page);
630       } else if ((same.viewport.top || same.viewport.bottom) && (same.viewport.left || same.viewport.right)) {
631         css.position = 'fixed';
632         transcribe(same.viewport, position.viewport);
633       } else if ((same.offset != null) && same.offset.top && same.offset.left) {
634         css.position = 'absolute';
635         offsetParent = this.cache('target-offsetparent', function() {
636           return getOffsetParent(_this.target);
637         });
638         if (getOffsetParent(this.element) !== offsetParent) {
639           defer(function() {
640             _this.element.parentNode.removeChild(_this.element);
641             return offsetParent.appendChild(_this.element);
642           });
643         }
644         transcribe(same.offset, position.offset);
645         moved = true;
646       } else {
647         css.position = 'absolute';
648         transcribe({
649           top: true,
650           left: true
651         }, position.page);
652       }
653       if (!moved && this.element.parentNode.tagName !== 'BODY') {
654         this.element.parentNode.removeChild(this.element);
655         document.body.appendChild(this.element);
656       }
657       writeCSS = {};
658       write = false;
659       for (key in css) {
660         val = css[key];
661         elVal = this.element.style[key];
662         if (elVal !== '' && val !== '' && (key === 'top' || key === 'left' || key === 'bottom' || key === 'right')) {
663           elVal = parseFloat(elVal);
664           val = parseFloat(val);
665         }
666         if (elVal !== val) {
667           write = true;
668           writeCSS[key] = css[key];
669         }
670       }
671       if (write) {
672         return defer(function() {
673           return extend(_this.element.style, writeCSS);
674         });
675       }
676     };
677
678     return _Tether;
679
680   })();
681
682   Tether.position = position;
683
684   this.Tether = extend(_Tether, Tether);
685
686 }).call(this);