mdb-ui-kit/js/util.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

const Util = (() => {
/**
* ------------------------------------------------------------------------
* Private TransitionEnd Helpers
* ------------------------------------------------------------------------
*/
2017-08-11 17:59:31 +03:00
let transitionEnd = false;
let transitionEndSelector = "";
const TransitionEndEvent = {
2017-08-11 17:59:31 +03:00
WebkitTransition: "webkitTransitionEnd",
MozTransition: "transitionend",
OTransition: "oTransitionEnd otransitionend",
transition: "transitionend"
};
function transitionEndTest() {
if (window.QUnit) {
2017-08-11 17:59:31 +03:00
return false;
}
2017-08-11 17:59:31 +03:00
let el = document.createElement("bmd");
for (let name in TransitionEndEvent) {
if (el.style[name] !== undefined) {
2017-08-11 17:59:31 +03:00
return TransitionEndEvent[name]; // { end: TransitionEndEvent[name] }
}
}
2017-08-11 17:59:31 +03:00
return false;
}
function setTransitionEndSupport() {
2017-08-11 17:59:31 +03:00
transitionEnd = transitionEndTest();
// generate a concatenated transition end event selector
for (let name in TransitionEndEvent) {
2017-08-11 17:59:31 +03:00
transitionEndSelector += ` ${TransitionEndEvent[name]}`;
}
}
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
let Util = {
transitionEndSupported() {
2017-08-11 17:59:31 +03:00
return transitionEnd;
},
2017-08-11 17:59:31 +03:00
transitionEndSelector() {
return transitionEndSelector;
},
isChar(event) {
2017-08-11 17:59:31 +03:00
if (typeof event.which === "undefined") {
return true;
} else if (typeof event.which === "number" && event.which > 0) {
return (
2017-08-11 17:59:31 +03:00
!event.ctrlKey &&
!event.metaKey &&
!event.altKey &&
event.which !== 8 && // backspace
event.which !== 9 && // tab
event.which !== 13 && // enter
event.which !== 16 && // shift
event.which !== 17 && // ctrl
event.which !== 20 && // caps lock
event.which !== 27 // escape
);
}
2017-08-11 17:59:31 +03:00
return false;
},
assert($element, invalidTest, message) {
if (invalidTest) {
if (!$element === undefined) {
2017-08-11 17:59:31 +03:00
$element.css("border", "1px solid red");
}
2017-08-11 17:59:31 +03:00
console.error(message, $element); // eslint-disable-line no-console
throw message;
}
},
describe($element) {
if ($element === undefined) {
2017-08-11 17:59:31 +03:00
return "undefined";
} else if ($element.length === 0) {
2017-08-11 17:59:31 +03:00
return "(no matching elements)";
}
2017-08-11 17:59:31 +03:00
return `${$element[0].outerHTML.split(">")[0]}>`;
}
2017-08-11 17:59:31 +03:00
};
2017-08-11 17:59:31 +03:00
setTransitionEndSupport();
return Util;
})(jQuery);
2017-08-11 17:59:31 +03:00
export default Util;