UNPKG

ts-query

Version:

its simple library for dom control.

172 lines 6.28 kB
var Browser; (function (Browser) { function throwError(message) { console.error(message); throw new Error(message); } Browser.throwError = throwError; function isPointer() { return !!window.navigator.msPointerEnabled; } Browser.isPointer = isPointer; function isTouch() { var event = "ontouchmove"; var target = document.createElement("div"); target.setAttribute(event, ""); var isSupported = typeof target[event] === "function"; if (typeof target[event] !== "undefined") target[event] = null; target.removeAttribute(event); return isSupported; } Browser.isTouch = isTouch; function isMouse() { return !Browser.isPointer() && !Browser.isTouch(); } Browser.isMouse = isMouse; function getType(type) { var events = { mouse: { start: "mousedown", move: "mousemove", end: "mouseup" }, mobile: { start: "touchstart", move: "touchmove", end: "touchend" }, win: { start: "MSPointerDown", move: "MSPointerMove", end: "MSPointerUp" } }; if (Browser.isPointer()) return events.win[type]; if (Browser.isTouch()) return events.mobile[type]; if (Browser.isMouse()) return events.mouse[type]; } Browser.getType = getType; function createElement(tagName, className, parent) { var $element = document.createElement(tagName || "div"); if (className) $element.className = className; if (parent) parent.appendChild($element); $element.events = {}; $element.addEventListener = function (eventName, handler, delegate) { if (!eventName) Browser.throwError("Не передано имя события!"); if (!handler) Browser.throwError("Не передан обработчик события!"); if (!this.events[eventName]) this.events[eventName] = []; this.events[eventName].push(handler); }; $element.removeEventListener = function (eventName, handler, delegate) { if (!eventName) Browser.throwError("Не передано имя события!"); if (!handler) Browser.throwError("Не передан обработчик события!"); if (this.events[eventName]) { this.events[eventName] = this.events[eventName].filter(function ($handler) { return handler != $handler; }); } }; $element.trigger = function (eventName, Event) { var _this = this; if (!eventName) Browser.throwError("Не передано имя события!"); if (eventName in this.events) { this.events[eventName].forEach(function (handler) { handler.call(_this, Event); }); } }; return $element; } Browser.createElement = createElement; function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } Browser.randomInt = randomInt; var $Event = (function () { function $Event(options) { this.pointerId = null; this.timeStamp = null; this.pageX = null; this.pageY = null; this.target = null; this.touches = null; this.changedTouches = null; this.init(options); } $Event.prototype.preventDefault = function () { }; $Event.prototype.stopPropagation = function () { }; $Event.prototype.init = function (options) { this.type = Browser.getType(options.type || "start"); if (Browser.isPointer()) { this.initPointer(options); } else if (Browser.isTouch()) { this.initTouch(options); } else if (Browser.isMouse()) { this.initMouse(options); } }; $Event.prototype.initPointer = function (options) { this.pointerId = options.id || Browser.randomInt(0, 1000); $Event.setData(this, options); }; $Event.prototype.initTouch = function (options) { var touch = $Event.setData({}, options); touch.identifier = options.id || Browser.randomInt(0, 1000); if (this.type == "touchstart" || this.type == "touchmove") { this.touches = options.touches || { length: 1, 0: touch }; this.changedTouches = { length: 1, 0: touch }; } else if (this.type == "touchend") { this.touches = options.touches || { length: 0 }; this.changedTouches = options.changedTouches || { length: 1, 0: touch }; } else { Browser.throwError("Неизвестный тип события!"); } }; $Event.prototype.initMouse = function (options) { $Event.setData(this, options); }; $Event.setData = function (object, data) { object.pageX = data.pageX || Browser.randomInt(0, innerWidth); object.pageY = data.pageY || Browser.randomInt(0, innerHeight); object.target = data.target || document.body; return object; }; return $Event; })(); Browser.$Event = $Event; })(Browser || (Browser = {})); var $document = Browser.createElement("document", "document", document.body); $document.createElement = function (tagName) { return document.createElement(tagName); }; $document.querySelectorAll = function (selector) { return document.querySelectorAll(selector); }; //# sourceMappingURL=Browser.js.map