2 var Evented, addClass, defer, deferred, extend, flush, getBounds, getOffsetParent, getOrigin, getScrollBarSize, getScrollParent, hasClass, node, removeClass, uniqueId, updateClasses, zeroPosCache,
3 __hasProp = {}.hasOwnProperty,
4 __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; },
7 if (this.Tether == null) {
13 getScrollParent = function(el) {
14 var parent, position, scrollParent, style, _ref;
15 position = getComputedStyle(el).position;
16 if (position === 'fixed') {
19 scrollParent = void 0;
21 while (parent = parent.parentNode) {
23 style = getComputedStyle(parent);
28 if (/(auto|scroll)/.test(style['overflow'] + style['overflow-y'] + style['overflow-x'])) {
29 if (position !== 'absolute' || ((_ref = style['position']) === 'relative' || _ref === 'absolute' || _ref === 'fixed')) {
37 uniqueId = (function() {
47 getOrigin = function(doc) {
48 var id, k, node, v, _ref;
49 node = doc._tetherZeroElement;
51 node = doc.createElement('div');
52 node.setAttribute('data-tether-id', uniqueId());
58 doc.body.appendChild(node);
59 doc._tetherZeroElement = node;
61 id = node.getAttribute('data-tether-id');
62 if (zeroPosCache[id] == null) {
63 zeroPosCache[id] = {};
64 _ref = node.getBoundingClientRect();
67 zeroPosCache[id][k] = v;
70 return zeroPosCache[id] = void 0;
73 return zeroPosCache[id];
78 getBounds = function(el) {
79 var box, doc, docEl, k, origin, v, _ref;
80 if (el === document) {
82 el = document.documentElement;
84 doc = el.ownerDocument;
86 docEl = doc.documentElement;
88 _ref = el.getBoundingClientRect();
93 origin = getOrigin(doc);
94 box.top -= origin.top;
95 box.left -= origin.left;
96 if (box.width == null) {
97 box.width = document.body.scrollWidth - box.left - box.right;
99 if (box.height == null) {
100 box.height = document.body.scrollHeight - box.top - box.bottom;
102 box.top = box.top - docEl.clientTop;
103 box.left = box.left - docEl.clientLeft;
104 box.right = doc.body.clientWidth - box.width - box.left;
105 box.bottom = doc.body.clientHeight - box.height - box.top;
109 getOffsetParent = function(el) {
110 return el.offsetParent || document.documentElement;
113 getScrollBarSize = function() {
114 var inner, outer, width, widthContained, widthScroll;
115 inner = document.createElement('div');
116 inner.style.width = '100%';
117 inner.style.height = '200px';
118 outer = document.createElement('div');
119 extend(outer.style, {
120 position: 'absolute',
123 pointerEvents: 'none',
124 visibility: 'hidden',
129 outer.appendChild(inner);
130 document.body.appendChild(outer);
131 widthContained = inner.offsetWidth;
132 outer.style.overflow = 'scroll';
133 widthScroll = inner.offsetWidth;
134 if (widthContained === widthScroll) {
135 widthScroll = outer.clientWidth;
137 document.body.removeChild(outer);
138 width = widthContained - widthScroll;
145 extend = function(out) {
146 var args, key, obj, val, _i, _len, _ref;
151 Array.prototype.push.apply(args, arguments);
152 _ref = args.slice(1);
153 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
157 if (!__hasProp.call(obj, key)) continue;
166 removeClass = function(el, name) {
167 var cls, _i, _len, _ref, _results;
168 if (el.classList != null) {
169 _ref = name.split(' ');
171 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
174 _results.push(el.classList.remove(cls));
179 return el.className = el.className.replace(new RegExp("(^| )" + (name.split(' ').join('|')) + "( |$)", 'gi'), ' ');
183 addClass = function(el, name) {
184 var cls, _i, _len, _ref, _results;
185 if (el.classList != null) {
186 _ref = name.split(' ');
188 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
191 _results.push(el.classList.add(cls));
196 removeClass(el, name);
197 return el.className += " " + name;
201 hasClass = function(el, name) {
202 if (el.classList != null) {
203 return el.classList.contains(name);
205 return new RegExp("(^| )" + name + "( |$)", 'gi').test(el.className);
209 updateClasses = function(el, add, all) {
210 var cls, _i, _j, _len, _len1, _results;
211 for (_i = 0, _len = all.length; _i < _len; _i++) {
213 if (__indexOf.call(add, cls) < 0) {
214 if (hasClass(el, cls)) {
215 removeClass(el, cls);
220 for (_j = 0, _len1 = add.length; _j < _len1; _j++) {
222 if (!hasClass(el, cls)) {
223 _results.push(addClass(el, cls));
225 _results.push(void 0);
233 defer = function(fn) {
234 return deferred.push(fn);
240 while (fn = deferred.pop()) {
246 Evented = (function() {
247 function Evented() {}
249 Evented.prototype.on = function(event, handler, ctx, once) {
254 if (this.bindings == null) {
257 if ((_base = this.bindings)[event] == null) {
260 return this.bindings[event].push({
267 Evented.prototype.once = function(event, handler, ctx) {
268 return this.on(event, handler, ctx, true);
271 Evented.prototype.off = function(event, handler) {
272 var i, _ref, _results;
273 if (((_ref = this.bindings) != null ? _ref[event] : void 0) == null) {
276 if (handler == null) {
277 return delete this.bindings[event];
281 while (i < this.bindings[event].length) {
282 if (this.bindings[event][i].handler === handler) {
283 _results.push(this.bindings[event].splice(i, 1));
292 Evented.prototype.trigger = function() {
293 var args, ctx, event, handler, i, once, _ref, _ref1, _results;
294 event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
295 if ((_ref = this.bindings) != null ? _ref[event] : void 0) {
298 while (i < this.bindings[event].length) {
299 _ref1 = this.bindings[event][i], handler = _ref1.handler, ctx = _ref1.ctx, once = _ref1.once;
300 handler.apply(ctx != null ? ctx : this, args);
302 _results.push(this.bindings[event].splice(i, 1));
315 this.Tether.Utils = {
316 getScrollParent: getScrollParent,
317 getBounds: getBounds,
318 getOffsetParent: getOffsetParent,
321 removeClass: removeClass,
323 updateClasses: updateClasses,
328 getScrollBarSize: getScrollBarSize