jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/dom.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-07-18 11:50:00 -07:00
define([], function () {
function parentWithAttribute(elem, name, value) {
while ((value ? elem.getAttribute(name) != value : !elem.getAttribute(name))) {
elem = elem.parentNode;
if (!elem || !elem.getAttribute) {
return null;
}
}
return elem;
}
2016-07-23 13:00:56 -07:00
function parentWithTag(elem, tagNames) {
2016-07-18 11:50:00 -07:00
2016-07-23 13:00:56 -07:00
// accept both string and array passed in
if (!Array.isArray(tagNames)) {
tagNames = [tagNames];
}
while (tagNames.indexOf(elem.tagName || '') == -1) {
2016-07-18 11:50:00 -07:00
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
2016-08-07 12:43:52 -07:00
var supportsCaptureOption = false;
try {
var opts = Object.defineProperty({}, 'capture', {
get: function () {
supportsCaptureOption = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) { }
function addEventListenerWithOptions(target, type, handler, options) {
var optionsOrCapture = options;
if (!supportsCaptureOption) {
optionsOrCapture = options.capture;
}
target.addEventListener(type, handler, optionsOrCapture);
}
function removeEventListenerWithOptions(target, type, handler, options) {
var optionsOrCapture = options;
if (!supportsCaptureOption) {
optionsOrCapture = options.capture;
}
target.removeEventListener(type, handler, optionsOrCapture);
}
2016-08-11 20:23:12 -07:00
var windowSize;
2016-08-25 09:55:57 -07:00
var windowSizeEventsBound;
function clearWindowSize() {
windowSize = null;
2016-08-11 20:23:12 -07:00
}
function getWindowSize() {
if (!windowSize) {
2016-08-25 09:55:57 -07:00
windowSize = {
innerHeight: window.innerHeight,
innerWidth: window.innerWidth
};
if (!windowSizeEventsBound) {
windowSizeEventsBound = true;
addEventListenerWithOptions(window, "orientationchange", clearWindowSize, { passive: true });
addEventListenerWithOptions(window, 'resize', clearWindowSize, { passive: true });
}
2016-08-11 20:23:12 -07:00
}
return windowSize;
}
2016-07-18 11:50:00 -07:00
return {
parentWithAttribute: parentWithAttribute,
parentWithClass: parentWithClass,
2016-08-07 12:43:52 -07:00
parentWithTag: parentWithTag,
addEventListener: addEventListenerWithOptions,
2016-08-11 20:23:12 -07:00
removeEventListener: removeEventListenerWithOptions,
getWindowSize: getWindowSize
2016-07-18 11:50:00 -07:00
};
});