edo-htqw
Version:
A easier HTML element's dom operation libary.
198 lines (189 loc) • 5.62 kB
JavaScript
function basic (edo) {
edo.proto("text", function (val) {
if (val === undefined) {
return this.el.innerText;
}
return this.el.innerText = val;
});
edo.proto("html", function (val) {
if (val === undefined) {
return this.el.innerHTML;
}
return this.el.innerHTML = val;
});
edo.proto("href", function (val) {
if (val === undefined) {
return this.el.href;
}
return this.el.href = val;
});
}
class edoTypeError extends Error {
constructor(msg) {
super(msg);
this.name = "edoTypeError";
}
}
function listen (edo) {
function prevent(event) {
event.preventDefault();
}
function ob(val) {
const temp = { prevent };
for (let s in val) {
temp[s] = val[s];
}
return temp;
}
edo.proto('click', function (call) {
try {
if (call === null) {
this.el.onclick = null;
}
else {
this.el.onclick = (event) => {
const temp = ob({ 0: event });
call.call(this, temp);
};
}
return true;
}
catch (_a) {
return false;
}
});
edo.proto('onEventL', function (n, l) {
if (!(l instanceof Function)) {
throw new edoTypeError(`This type should is Function, fault ${Object.prototype.toString.call(l).split(' ')[1].replace(']', '')}`);
}
this.el.addEventListener(n, l);
return true;
});
edo.proto('offEventL', function (n, l) {
if (!(l instanceof Function)) {
throw new edoTypeError(`This type should is Function, fault ${Object.prototype.toString.call(l).split(' ')[1].replace(']', '')}`);
}
this.el.removeEventListener(n, l);
return true;
});
}
function style (edo) {
edo.css = function (options, a2) {
const head = document.querySelector('head');
let str = '';
if (options instanceof Object) {
for (let s in options) {
str += `${s}{`;
let temp = options[s];
for (let s2 in temp) {
str += `${s2}:${temp[s2]};`;
}
str += '}';
}
}
else if (typeof options === 'string') {
str += `${options}{`;
if (typeof a2 === 'string') {
str += a2;
}
else if (a2 instanceof Object) {
for (let s in a2) {
str += `${s}:${a2[s]};`;
}
}
else {
throw new edoTypeError(`This type should is Object or string, fault ${Object.prototype.toString.call(a2).split(' ')[1].replace(']', '')}`);
}
str += `}`;
}
else {
throw new edoTypeError(`This type should is Object or string, fault ${Object.prototype.toString.call(options).split(' ')[1].replace(']', '')}`);
}
head.innerHTML += `<style>${str}</style>`;
};
edo.proto('css', function (a1, a2) {
if (a2 === undefined) {
if (typeof a1 === 'string') {
this.el.style = a1;
}
else {
for (let s in a1) {
this.el.style[s] = a1[s];
}
}
}
else {
this.el.style[a1] = a2;
}
return true;
});
edo.proto('getCss', function (name) {
return this.el.style[name];
});
}
class edoHTMLElementIsNotFoundError extends Error {
constructor(message) {
super(message);
this.name = "edoHTMLElementIsNotFoundError";
}
}
class edoCssSelectorError extends Error {
constructor(msg) {
super(msg);
this.name = "edoCssSelectorError";
}
}
var errors = {
edoHTMLElementIsNotFoundError,
edoTypeError,
edoCssSelectorError
};
var _a;
class edoElement {
constructor(val) {
this[_a] = 'edoElement';
this.el = document.createElement('div');
if (typeof val === 'string') {
try {
if (document.querySelector(val) === null) {
throw new errors.edoHTMLElementIsNotFoundError(`The element "${val}" is not found!!!`);
}
else {
this.el = document.querySelector(val);
}
}
catch (e) {
if (!e.name.startsWith('edo')) {
throw new errors.edoCssSelectorError(`This Css Selector "${val}" is error`);
}
else {
throw e;
}
}
}
else if (val instanceof HTMLElement) {
this.el = val;
}
else {
throw new errors.edoTypeError(`This type should is HTMLElement or string, fault ${Object.prototype.toString.call(val).split(' ')[1].replace(']', '')}`);
}
this.el;
}
}
_a = Symbol.toStringTag;
const edo = function (val) {
return new edoElement(val);
};
edo.proto = function (name, value) {
try {
edoElement.prototype[name] = value;
return true;
}
catch (_b) {
return false;
}
};
basic(edo);
listen(edo);
style(edo);
export default edo;