pebble/third_party/hubspot/utils.js
2025-02-24 18:58:29 -08:00

331 lines
8.5 KiB
JavaScript
Executable file

(function() {
var Evented, addClass, defer, deferred, extend, flush, getBounds, getOffsetParent, getOrigin, getScrollBarSize, getScrollParent, hasClass, node, removeClass, uniqueId, updateClasses, zeroPosCache,
__hasProp = {}.hasOwnProperty,
__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; },
__slice = [].slice;
if (this.Tether == null) {
this.Tether = {
modules: []
};
}
getScrollParent = function(el) {
var parent, position, scrollParent, style, _ref;
position = getComputedStyle(el).position;
if (position === 'fixed') {
return el;
}
scrollParent = void 0;
parent = el;
while (parent = parent.parentNode) {
try {
style = getComputedStyle(parent);
} catch (_error) {}
if (style == null) {
return parent;
}
if (/(auto|scroll)/.test(style['overflow'] + style['overflow-y'] + style['overflow-x'])) {
if (position !== 'absolute' || ((_ref = style['position']) === 'relative' || _ref === 'absolute' || _ref === 'fixed')) {
return parent;
}
}
}
return document.body;
};
uniqueId = (function() {
var id;
id = 0;
return function() {
return id++;
};
})();
zeroPosCache = {};
getOrigin = function(doc) {
var id, k, node, v, _ref;
node = doc._tetherZeroElement;
if (node == null) {
node = doc.createElement('div');
node.setAttribute('data-tether-id', uniqueId());
extend(node.style, {
top: 0,
left: 0,
position: 'absolute'
});
doc.body.appendChild(node);
doc._tetherZeroElement = node;
}
id = node.getAttribute('data-tether-id');
if (zeroPosCache[id] == null) {
zeroPosCache[id] = {};
_ref = node.getBoundingClientRect();
for (k in _ref) {
v = _ref[k];
zeroPosCache[id][k] = v;
}
defer(function() {
return zeroPosCache[id] = void 0;
});
}
return zeroPosCache[id];
};
node = null;
getBounds = function(el) {
var box, doc, docEl, k, origin, v, _ref;
if (el === document) {
doc = document;
el = document.documentElement;
} else {
doc = el.ownerDocument;
}
docEl = doc.documentElement;
box = {};
_ref = el.getBoundingClientRect();
for (k in _ref) {
v = _ref[k];
box[k] = v;
}
origin = getOrigin(doc);
box.top -= origin.top;
box.left -= origin.left;
if (box.width == null) {
box.width = document.body.scrollWidth - box.left - box.right;
}
if (box.height == null) {
box.height = document.body.scrollHeight - box.top - box.bottom;
}
box.top = box.top - docEl.clientTop;
box.left = box.left - docEl.clientLeft;
box.right = doc.body.clientWidth - box.width - box.left;
box.bottom = doc.body.clientHeight - box.height - box.top;
return box;
};
getOffsetParent = function(el) {
return el.offsetParent || document.documentElement;
};
getScrollBarSize = function() {
var inner, outer, width, widthContained, widthScroll;
inner = document.createElement('div');
inner.style.width = '100%';
inner.style.height = '200px';
outer = document.createElement('div');
extend(outer.style, {
position: 'absolute',
top: 0,
left: 0,
pointerEvents: 'none',
visibility: 'hidden',
width: '200px',
height: '150px',
overflow: 'hidden'
});
outer.appendChild(inner);
document.body.appendChild(outer);
widthContained = inner.offsetWidth;
outer.style.overflow = 'scroll';
widthScroll = inner.offsetWidth;
if (widthContained === widthScroll) {
widthScroll = outer.clientWidth;
}
document.body.removeChild(outer);
width = widthContained - widthScroll;
return {
width: width,
height: width
};
};
extend = function(out) {
var args, key, obj, val, _i, _len, _ref;
if (out == null) {
out = {};
}
args = [];
Array.prototype.push.apply(args, arguments);
_ref = args.slice(1);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
obj = _ref[_i];
if (obj) {
for (key in obj) {
if (!__hasProp.call(obj, key)) continue;
val = obj[key];
out[key] = val;
}
}
}
return out;
};
removeClass = function(el, name) {
var cls, _i, _len, _ref, _results;
if (el.classList != null) {
_ref = name.split(' ');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cls = _ref[_i];
if (cls.trim()) {
_results.push(el.classList.remove(cls));
}
}
return _results;
} else {
return el.className = el.className.replace(new RegExp("(^| )" + (name.split(' ').join('|')) + "( |$)", 'gi'), ' ');
}
};
addClass = function(el, name) {
var cls, _i, _len, _ref, _results;
if (el.classList != null) {
_ref = name.split(' ');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cls = _ref[_i];
if (cls.trim()) {
_results.push(el.classList.add(cls));
}
}
return _results;
} else {
removeClass(el, name);
return el.className += " " + name;
}
};
hasClass = function(el, name) {
if (el.classList != null) {
return el.classList.contains(name);
} else {
return new RegExp("(^| )" + name + "( |$)", 'gi').test(el.className);
}
};
updateClasses = function(el, add, all) {
var cls, _i, _j, _len, _len1, _results;
for (_i = 0, _len = all.length; _i < _len; _i++) {
cls = all[_i];
if (__indexOf.call(add, cls) < 0) {
if (hasClass(el, cls)) {
removeClass(el, cls);
}
}
}
_results = [];
for (_j = 0, _len1 = add.length; _j < _len1; _j++) {
cls = add[_j];
if (!hasClass(el, cls)) {
_results.push(addClass(el, cls));
} else {
_results.push(void 0);
}
}
return _results;
};
deferred = [];
defer = function(fn) {
return deferred.push(fn);
};
flush = function() {
var fn, _results;
_results = [];
while (fn = deferred.pop()) {
_results.push(fn());
}
return _results;
};
Evented = (function() {
function Evented() {}
Evented.prototype.on = function(event, handler, ctx, once) {
var _base;
if (once == null) {
once = false;
}
if (this.bindings == null) {
this.bindings = {};
}
if ((_base = this.bindings)[event] == null) {
_base[event] = [];
}
return this.bindings[event].push({
handler: handler,
ctx: ctx,
once: once
});
};
Evented.prototype.once = function(event, handler, ctx) {
return this.on(event, handler, ctx, true);
};
Evented.prototype.off = function(event, handler) {
var i, _ref, _results;
if (((_ref = this.bindings) != null ? _ref[event] : void 0) == null) {
return;
}
if (handler == null) {
return delete this.bindings[event];
} else {
i = 0;
_results = [];
while (i < this.bindings[event].length) {
if (this.bindings[event][i].handler === handler) {
_results.push(this.bindings[event].splice(i, 1));
} else {
_results.push(i++);
}
}
return _results;
}
};
Evented.prototype.trigger = function() {
var args, ctx, event, handler, i, once, _ref, _ref1, _results;
event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if ((_ref = this.bindings) != null ? _ref[event] : void 0) {
i = 0;
_results = [];
while (i < this.bindings[event].length) {
_ref1 = this.bindings[event][i], handler = _ref1.handler, ctx = _ref1.ctx, once = _ref1.once;
handler.apply(ctx != null ? ctx : this, args);
if (once) {
_results.push(this.bindings[event].splice(i, 1));
} else {
_results.push(i++);
}
}
return _results;
}
};
return Evented;
})();
this.Tether.Utils = {
getScrollParent: getScrollParent,
getBounds: getBounds,
getOffsetParent: getOffsetParent,
extend: extend,
addClass: addClass,
removeClass: removeClass,
hasClass: hasClass,
updateClasses: updateClasses,
defer: defer,
flush: flush,
uniqueId: uniqueId,
Evented: Evented,
getScrollBarSize: getScrollBarSize
};
}).call(this);