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,
4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
6 if (this.Tether == null) {
7 throw new Error("You must include the utils.js file before tether.js");
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;
14 within = function(a, b, diff) {
18 return (a + diff >= b && b >= a - diff);
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++) {
27 if (el.style[key] !== void 0) {
35 position = function() {
37 for (_i = 0, _len = tethers.length; _i < _len; _i++) {
39 tether.position(false);
46 return (_ref1 = typeof performance !== "undefined" && performance !== null ? typeof performance.now === "function" ? performance.now() : void 0 : void 0) != null ? _ref1 : +(new Date);
50 var event, lastCall, lastDuration, pendingTimeout, tick, _i, _len, _ref1, _results;
53 pendingTimeout = null;
55 if ((lastDuration != null) && lastDuration > 16) {
56 lastDuration = Math.min(lastDuration - 16, 250);
57 pendingTimeout = setTimeout(tick, 250);
60 if ((lastCall != null) && (now() - lastCall) < 10) {
63 if (pendingTimeout != null) {
64 clearTimeout(pendingTimeout);
65 pendingTimeout = null;
69 return lastDuration = now() - lastCall;
71 _ref1 = ['resize', 'scroll', 'touchmove'];
73 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
75 _results.push(window.addEventListener(event, tick));
101 autoToFixedAttachment = function(attachment, relativeToAttachment) {
103 left = attachment.left, top = attachment.top;
104 if (left === 'auto') {
105 left = MIRROR_LR[relativeToAttachment.left];
107 if (top === 'auto') {
108 top = MIRROR_TB[relativeToAttachment.top];
116 attachmentToOffset = function(attachment) {
119 left: (_ref1 = OFFSET_MAP[attachment.left]) != null ? _ref1 : attachment.left,
120 top: (_ref2 = OFFSET_MAP[attachment.top]) != null ? _ref2 : attachment.top
124 addOffset = function() {
125 var left, offsets, out, top, _i, _len, _ref1;
126 offsets = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
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);
136 if (typeof left === 'string') {
137 left = parseFloat(left, 10);
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;
149 if (typeof offset.top === 'string' && offset.top.indexOf('%') !== -1) {
150 offset.top = parseFloat(offset.top, 10) / 100 * size.height;
155 parseAttachment = parseOffset = function(value) {
156 var left, top, _ref1;
157 _ref1 = value.split(' '), top = _ref1[0], left = _ref1[1];
164 _Tether = (function() {
165 _Tether.modules = [];
167 function _Tether(options) {
168 this.position = __bind(this.position, this);
169 var module, _i, _len, _ref1, _ref2;
172 this.setOptions(options, false);
173 _ref1 = Tether.modules;
174 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
176 if ((_ref2 = module.initialize) != null) {
183 _Tether.prototype.getClass = function(key) {
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;
198 _Tether.prototype.setOptions = function(options, position) {
199 var defaults, key, _i, _len, _ref1, _ref2;
200 this.options = options;
201 if (position == null) {
207 targetAttachment: 'auto auto',
208 classPrefix: 'tether'
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';
219 _ref2 = ['element', 'target'];
220 for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
222 if (this[key] == null) {
223 throw new Error("Tether Error: Both element and target must be defined");
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]);
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");
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) {
243 if (this.targetModifier === 'scroll-handle') {
244 this.scrollParent = this.target;
246 this.scrollParent = getScrollParent(this.target);
248 if (this.options.enabled !== false) {
249 return this.enable(position);
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) {
258 if (this.target === document.body) {
266 bounds = getBounds(this.target);
268 height: bounds.height,
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);
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);
281 if (out.top < pageYOffset) {
282 out.top = pageYOffset;
284 if (out.left < pageXOffset) {
285 out.left = pageXOffset;
290 case 'scroll-handle':
291 target = this.target;
292 if (target === document.body) {
293 target = document.documentElement;
301 bounds = getBounds(target);
303 style = getComputedStyle(target);
304 hasBottomScroll = target.scrollWidth > target.clientWidth || 'scroll' === [style.overflow, style.overflowX] || this.target !== document.body;
306 if (hasBottomScroll) {
309 height = bounds.height - parseFloat(style.borderTopWidth) - parseFloat(style.borderBottomWidth) - scrollBottom;
312 height: height * 0.975 * (height / target.scrollHeight),
313 left: bounds.left + bounds.width - parseFloat(style.borderLeftWidth) - 15
316 if (height < 408 && this.target === document.body) {
317 fitAdj = -0.00011 * Math.pow(height, 2) - 0.00727 * height + 22.58;
319 if (this.target !== document.body) {
320 out.height = Math.max(out.height, 24);
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);
330 return getBounds(this.target);
334 _Tether.prototype.clearCache = function() {
335 return this._cache = {};
338 _Tether.prototype.cache = function(k, getter) {
339 if (this._cache == null) {
342 if (this._cache[k] == null) {
343 this._cache[k] = getter.call(this);
345 return this._cache[k];
348 _Tether.prototype.enable = function(position) {
349 if (position == null) {
352 addClass(this.target, this.getClass('enabled'));
353 addClass(this.element, this.getClass('enabled'));
355 if (this.scrollParent !== document) {
356 this.scrollParent.addEventListener('scroll', this.position);
359 return this.position();
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);
372 _Tether.prototype.destroy = function() {
373 var i, tether, _i, _len, _results;
376 for (i = _i = 0, _len = tethers.length; _i < _len; i = ++_i) {
378 if (tether === this) {
379 tethers.splice(i, 1);
382 _results.push(void 0);
388 _Tether.prototype.updateAttachClasses = function(elementAttach, targetAttach) {
389 var add, all, side, sides, _i, _j, _len, _len1, _ref1,
391 if (elementAttach == null) {
392 elementAttach = this.attachment;
394 if (targetAttach == null) {
395 targetAttach = this.targetAttachment;
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);
401 add = this._addAttachClasses != null ? this._addAttachClasses : this._addAttachClasses = [];
402 if (elementAttach.top) {
403 add.push("" + (this.getClass('element-attached')) + "-" + elementAttach.top);
405 if (elementAttach.left) {
406 add.push("" + (this.getClass('element-attached')) + "-" + elementAttach.left);
408 if (targetAttach.top) {
409 add.push("" + (this.getClass('target-attached')) + "-" + targetAttach.top);
411 if (targetAttach.left) {
412 add.push("" + (this.getClass('target-attached')) + "-" + targetAttach.left);
415 for (_i = 0, _len = sides.length; _i < _len; _i++) {
417 all.push("" + (this.getClass('element-attached')) + "-" + side);
419 for (_j = 0, _len1 = sides.length; _j < _len1; _j++) {
421 all.push("" + (this.getClass('target-attached')) + "-" + side);
423 return defer(function() {
424 if (_this._addAttachClasses == null) {
427 updateClasses(_this.element, _this._addAttachClasses, all);
428 updateClasses(_this.target, _this._addAttachClasses, all);
429 return _this._addAttachClasses = void 0;
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,
436 if (flushChanges == null) {
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);
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;
457 targetSize = targetPos = this.cache('target-bounds', function() {
458 return _this.getTargetBounds();
460 offset = offsetToPx(attachmentToOffset(this.attachment), {
464 targetOffset = offsetToPx(attachmentToOffset(targetAttachment), targetSize);
465 manualOffset = offsetToPx(this.offset, {
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++) {
477 ret = module.position.call(this, {
480 targetAttachment: targetAttachment,
481 targetPos: targetPos,
482 attachment: this.attachment,
483 elementPos: elementPos,
485 targetOffset: targetOffset,
486 manualOffset: manualOffset,
487 manualTargetOffset: manualTargetOffset,
488 scrollbarSize: scrollbarSize
490 if ((ret == null) || typeof ret !== 'object') {
492 } else if (ret === false) {
495 top = ret.top, left = ret.left;
504 top: top - pageYOffset,
505 bottom: pageYOffset - top - height + innerHeight,
506 left: left - pageXOffset,
507 right: pageXOffset - left - width + innerWidth
510 if (document.body.scrollWidth > window.innerWidth) {
511 scrollbarSize = this.cache('scrollbar-size', getScrollBarSize);
512 next.viewport.bottom -= scrollbarSize.height;
514 if (document.body.scrollHeight > window.innerHeight) {
515 scrollbarSize = this.cache('scrollbar-size', getScrollBarSize);
516 next.viewport.right -= scrollbarSize.width;
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;
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);
526 offsetPosition = this.cache('target-offsetparent-bounds', function() {
527 return getBounds(offsetParent);
529 offsetParentStyle = getComputedStyle(offsetParent);
530 elementStyle = getComputedStyle(this.element);
531 offsetParentSize = offsetPosition;
533 _ref6 = ['Top', 'Left', 'Bottom', 'Right'];
534 for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) {
536 offsetBorder[side.toLowerCase()] = parseFloat(offsetParentStyle["border" + side + "Width"]);
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;
545 top: next.page.top - offsetPosition.top + scrollTop - offsetBorder.top,
546 left: next.page.left - offsetPosition.left + scrollLeft - offsetBorder.left
552 this.history.unshift(next);
553 if (this.history.length > 3) {
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,
565 if (this.element.parentNode == null) {
569 for (type in position) {
571 for (key in position[type]) {
573 _ref1 = this.history;
574 for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
576 if (!within((_ref2 = point[type]) != null ? _ref2[key] : void 0, position[type][key])) {
582 same[type][key] = true;
592 transcribe = function(same, pos) {
593 var xPos, yPos, _ref3;
594 if (((_ref3 = _this.options.optimizations) != null ? _ref3.gpu : void 0) !== false) {
609 css[transformKey] = "translateX(" + (Math.round(xPos)) + "px) translateY(" + (Math.round(yPos)) + "px)";
610 if (transformKey !== 'msTransform') {
611 return css[transformKey] += " translateZ(0)";
615 css.top = "" + pos.top + "px";
617 css.bottom = "" + pos.bottom + "px";
620 return css.left = "" + pos.left + "px";
622 return css.right = "" + pos.right + "px";
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);
638 if (getOffsetParent(this.element) !== offsetParent) {
640 _this.element.parentNode.removeChild(_this.element);
641 return offsetParent.appendChild(_this.element);
644 transcribe(same.offset, position.offset);
647 css.position = 'absolute';
653 if (!moved && this.element.parentNode.tagName !== 'BODY') {
654 this.element.parentNode.removeChild(this.element);
655 document.body.appendChild(this.element);
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);
668 writeCSS[key] = css[key];
672 return defer(function() {
673 return extend(_this.element.style, writeCSS);
682 Tether.position = position;
684 this.Tether = extend(_Tether, Tether);