/
eb
/
js-excel
Обзор
Документация
Войти
/
eb
/
js-excel
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/core/dom.js
142 строки
2 KB
Evgeniy Budaev
Routing implemented
17 июн 2020, 10:58
17 июн 2020, 10:58
6622668
Код
Авторство
О чём код?
class Dom { constructor(selector) { // #app this.$el = typeof selector === 'string' ? document.querySelector(selector) : selector } html(html) { if (typeof html === 'string') { this.$el.innerHTML = html return this } return this.$el.outerHTML.trim() } clear() { this.html('') return this } text(text) { if (typeof text !== 'undefined') { this.$el.textContent = text return this } if (this.$el.tagName.toLowerCase() === 'input') { return this.$el.value.trim() } return this.$el.textContent.trim() } on(eventType, callback) { this.$el.addEventListener(eventType, callback) } off(eventType, callback) { this.$el.removeEventListener(eventType, callback) } find(selector) { return $(this.$el.querySelector(selector)) } // Element append(node) { if (node instanceof Dom) { node = node.$el } if (Element.prototype.append) { this.$el.append(node) } else { this.$el.appendChild(node) } return this } // dataset() { // return this.$el.dataset // } get data() { return this.$el.dataset } closest(selector) { return $(this.$el.closest(selector)) } getCords() { return this.$el.getBoundingClientRect() } findAll(selector) { return this.$el.querySelectorAll(selector) } css(styles = {}) { Object .keys(styles) .forEach(key => { this.$el.style[key] = styles[key] // console.log(key) // console.log(styles[key]) }) } id(parse) { if (parse) { const parsed = this.id().split(':') return { row: +parsed [0], col: +parsed[1] } } return this.data.id } getStyles(styles = []) { return styles.reduce((res, s) => { res[s] = this.$el.style[s] return res }, {}) } focus() { this.$el.focus() return this } attr(name, value) { if (value) { this.$el.setAttribute(name, value) return this } return this.$el.getAttribute(name) } addClass(className) { this.$el.classList.add(className) } removeClass(className) { this.$el.classList.remove(className) } } // $('div').html('<h1>Test</h1>') // event.target export function $(selector) { return new Dom(selector) } $.create = (tagName, classes = '') => { const el = document.createElement(tagName) if (classes) { el.classList.add(classes) } return $(el) }