|
@@ -3,37 +3,29 @@
|
|
Author: CrazyDoctor (Oleg Karataev)
|
|
Author: CrazyDoctor (Oleg Karataev)
|
|
*/
|
|
*/
|
|
|
|
|
|
-function isEmpty(val) {
|
|
|
|
- return val === null || val === undefined ||
|
|
|
|
- val === '' || val.length === 0;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-function isJsonString(str) {
|
|
|
|
- try {
|
|
|
|
- JSON.parse(str);
|
|
|
|
- } catch (e) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
class CDElement {
|
|
class CDElement {
|
|
|
|
|
|
constructor(el) {
|
|
constructor(el) {
|
|
this.element = el;
|
|
this.element = el;
|
|
- el.cdelement = this;
|
|
|
|
- try {
|
|
|
|
- this.events = Object.keys(getEventListeners(el));
|
|
|
|
- } catch(e) {
|
|
|
|
- this.events = [];
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ el.cdelement = this;
|
|
|
|
+ try {
|
|
|
|
+ this.events = Object.keys(getEventListeners(el));
|
|
|
|
+ } catch(e) {
|
|
|
|
+ this.events = [];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- static get(el) {
|
|
|
|
- if(el.cdelement)
|
|
|
|
- return el.cdelement;
|
|
|
|
- return el.cdelement = new CDElement(el);
|
|
|
|
- }
|
|
|
|
|
|
+ static get(el) {
|
|
|
|
+ if(el == null)
|
|
|
|
+ return null;
|
|
|
|
+ if(el instanceof CDElement)
|
|
|
|
+ return el;
|
|
|
|
+ if(el.cdelement)
|
|
|
|
+ return el.cdelement;
|
|
|
|
+ if(el instanceof Element || el instanceof Window)
|
|
|
|
+ return el.cdelement = new CDElement(el);
|
|
|
|
+ throw 'CDElement.get() error';
|
|
|
|
+ }
|
|
|
|
|
|
get() {
|
|
get() {
|
|
return this.element;
|
|
return this.element;
|
|
@@ -44,43 +36,48 @@ class CDElement {
|
|
}
|
|
}
|
|
|
|
|
|
getFirstChild(selector) {
|
|
getFirstChild(selector) {
|
|
- const children = Array.from(this.get().children);
|
|
|
|
- if (children.length == 0)
|
|
|
|
- return null;
|
|
|
|
- if (isEmpty(selector))
|
|
|
|
|
|
+ const children = Array.from(this.get().children);
|
|
|
|
+ if (children.length == 0)
|
|
|
|
+ return null;
|
|
|
|
+ if (CDUtils.isEmpty(selector))
|
|
return CDElement.get(children[0]);
|
|
return CDElement.get(children[0]);
|
|
- const child = this.get().querySelector(selector);
|
|
|
|
- if(child)
|
|
|
|
- return CDElement.get(child);
|
|
|
|
- return null;
|
|
|
|
|
|
+ const child = this.get().querySelector(selector);
|
|
|
|
+ if(child)
|
|
|
|
+ return CDElement.get(child);
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
- hasChildren() {
|
|
|
|
- return Array.from(this.get().children).length > 0;
|
|
|
|
- }
|
|
|
|
|
|
+ hasChildren() {
|
|
|
|
+ return Array.from(this.get().children).length > 0;
|
|
|
|
+ }
|
|
|
|
|
|
- getChildren(selector) {
|
|
|
|
- if (isEmpty(selector))
|
|
|
|
|
|
+ getChildren(selector) {
|
|
|
|
+ if (CDUtils.isEmpty(selector))
|
|
return Array.from(this.get().children).map((element) => CDElement.get(element));
|
|
return Array.from(this.get().children).map((element) => CDElement.get(element));
|
|
- return Array.from(this.get().querySelectorAll(selector)).map((element) => CDElement.get(element));
|
|
|
|
- }
|
|
|
|
|
|
+ return Array.from(this.get().querySelectorAll(selector)).map((element) => CDElement.get(element));
|
|
|
|
+ }
|
|
|
|
|
|
- getChildrenRecursive() {
|
|
|
|
- let children = this.getChildren();
|
|
|
|
- for(const child of children) {
|
|
|
|
- if(this.hasChildren())
|
|
|
|
- children = children.concat(child.getChildrenRecursive());
|
|
|
|
- }
|
|
|
|
- return children;
|
|
|
|
- }
|
|
|
|
|
|
+ getChildrenRecursive() {
|
|
|
|
+ let children = this.getChildren();
|
|
|
|
+ for(const child of children) {
|
|
|
|
+ if(this.hasChildren())
|
|
|
|
+ children = children.concat(child.getChildrenRecursive());
|
|
|
|
+ }
|
|
|
|
+ return children;
|
|
|
|
+ }
|
|
|
|
|
|
- getParent() {
|
|
|
|
- return CDElement.get(this.get().parentElement);
|
|
|
|
- }
|
|
|
|
|
|
+ getParent() {
|
|
|
|
+ return CDElement.get(this.get().parentElement);
|
|
|
|
+ }
|
|
|
|
|
|
- getValue() {
|
|
|
|
- return this.get().value || this.getInnerHTML();
|
|
|
|
- }
|
|
|
|
|
|
+ getValue() {
|
|
|
|
+ return this.get().value || this.getInnerHTML();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setValue(value) {
|
|
|
|
+ this.get().value = value;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
|
|
append(element) {
|
|
append(element) {
|
|
this.get().append(element.get());
|
|
this.get().append(element.get());
|
|
@@ -102,7 +99,7 @@ class CDElement {
|
|
|
|
|
|
enable(display) {
|
|
enable(display) {
|
|
this.removeClass('disabled');
|
|
this.removeClass('disabled');
|
|
- this.get().style.display = isEmpty(display) ? 'block' : display;
|
|
|
|
|
|
+ this.get().style.display = CDUtils.isEmpty(display) ? 'block' : display;
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -117,50 +114,54 @@ class CDElement {
|
|
}
|
|
}
|
|
|
|
|
|
expandHeight(size) {
|
|
expandHeight(size) {
|
|
- if (isEmpty(size))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(size))
|
|
return this;
|
|
return this;
|
|
this.get().style.height = size + 'px';
|
|
this.get().style.height = size + 'px';
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
expandWidth(size){
|
|
expandWidth(size){
|
|
- if (isEmpty(size))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(size))
|
|
return this;
|
|
return this;
|
|
this.get().style.width = size + 'px';
|
|
this.get().style.width = size + 'px';
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
isMinimized(el) {
|
|
isMinimized(el) {
|
|
- return isEmpty(this.get().style.height);
|
|
|
|
|
|
+ return CDUtils.isEmpty(this.get().style.height);
|
|
}
|
|
}
|
|
|
|
|
|
isDisabled(el) {
|
|
isDisabled(el) {
|
|
- return isEmpty(this.get().style.display);
|
|
|
|
|
|
+ return CDUtils.isEmpty(this.get().style.display);
|
|
}
|
|
}
|
|
|
|
|
|
setId(id) {
|
|
setId(id) {
|
|
- if (isEmpty(id))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(id))
|
|
return this;
|
|
return this;
|
|
this.get().id = id;
|
|
this.get().id = id;
|
|
return id;
|
|
return id;
|
|
}
|
|
}
|
|
|
|
|
|
- nextSibling() {
|
|
|
|
- return CDElement.get(this.get().nextElementSibling);
|
|
|
|
|
|
+ previousSibling() {
|
|
|
|
+ return CDElement.get(this.get().previousElementSibling);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ nextSibling() {
|
|
|
|
+ return CDElement.get(this.get().nextElementSibling);
|
|
|
|
+ }
|
|
|
|
+
|
|
addClass(cls) {
|
|
addClass(cls) {
|
|
- if (isEmpty(cls))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(cls))
|
|
return this;
|
|
return this;
|
|
cls.split(' ').forEach((c) => {
|
|
cls.split(' ').forEach((c) => {
|
|
- if(c.length > 0)
|
|
|
|
- this.get().classList.add(c);
|
|
|
|
|
|
+ if(c.length > 0 && !this.hasClass(c))
|
|
|
|
+ this.get().classList.add(c);
|
|
});
|
|
});
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
getClass() {
|
|
getClass() {
|
|
- if (isEmpty(this.get().classList))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(this.get().classList))
|
|
return '';
|
|
return '';
|
|
let classList = '';
|
|
let classList = '';
|
|
this.get().classList.forEach((cls) => {
|
|
this.get().classList.forEach((cls) => {
|
|
@@ -170,14 +171,14 @@ class CDElement {
|
|
}
|
|
}
|
|
|
|
|
|
removeClass(cls) {
|
|
removeClass(cls) {
|
|
- if (isEmpty(cls))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(cls))
|
|
return this;
|
|
return this;
|
|
this.get().classList.remove(cls);
|
|
this.get().classList.remove(cls);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
hasClass(cls) {
|
|
hasClass(cls) {
|
|
- if (isEmpty(this.get().classList))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(this.get().classList))
|
|
return false;
|
|
return false;
|
|
let has = false;
|
|
let has = false;
|
|
this.get().classList.forEach((c) => {
|
|
this.get().classList.forEach((c) => {
|
|
@@ -187,36 +188,36 @@ class CDElement {
|
|
}
|
|
}
|
|
|
|
|
|
removeClass(cls) {
|
|
removeClass(cls) {
|
|
- if (isEmpty(cls))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(cls))
|
|
return this;
|
|
return this;
|
|
this.get().classList.remove(cls);
|
|
this.get().classList.remove(cls);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
switchClass(cls, condition) {
|
|
switchClass(cls, condition) {
|
|
- if(condition != null)
|
|
|
|
- return condition ? this.addClass(cls) : this.removeClass(cls);
|
|
|
|
|
|
+ if(condition != null)
|
|
|
|
+ return condition ? this.addClass(cls) : this.removeClass(cls);
|
|
return this.hasClass(cls) ? this.removeClass(cls) : this.addClass(cls);
|
|
return this.hasClass(cls) ? this.removeClass(cls) : this.addClass(cls);
|
|
}
|
|
}
|
|
|
|
|
|
removeCssProperty(prop) {
|
|
removeCssProperty(prop) {
|
|
- if (isEmpty(prop))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(prop))
|
|
return this;
|
|
return this;
|
|
this.get().style.setProperty(prop, '');
|
|
this.get().style.setProperty(prop, '');
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
setAttribute(attr, value) {
|
|
setAttribute(attr, value) {
|
|
- this.get().setAttribute(attr, isEmpty(value) ? '' : value);
|
|
|
|
|
|
+ this.get().setAttribute(attr, CDUtils.isEmpty(value) ? '' : value);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- getAttribute(attr) {
|
|
|
|
- return this.get().getAttribute(attr);
|
|
|
|
- }
|
|
|
|
|
|
+ getAttribute(attr) {
|
|
|
|
+ return this.get().getAttribute(attr);
|
|
|
|
+ }
|
|
|
|
|
|
setInnerHTML(value) {
|
|
setInnerHTML(value) {
|
|
- this.get().innerHTML = isEmpty(value) ? '' : value;
|
|
|
|
|
|
+ this.get().innerHTML = CDUtils.isEmpty(value) ? '' : value;
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -232,30 +233,45 @@ class CDElement {
|
|
return this.get().innerHeight;
|
|
return this.get().innerHeight;
|
|
}
|
|
}
|
|
|
|
|
|
- getOffsetTop() {
|
|
|
|
- return this.get().offsetTop;
|
|
|
|
- }
|
|
|
|
|
|
+ getOffsetTop() {
|
|
|
|
+ return this.get().offsetTop;
|
|
|
|
+ }
|
|
|
|
|
|
- scrollTo(selector) {
|
|
|
|
- const child = this.getFirstChild(selector);
|
|
|
|
- if(child)
|
|
|
|
- this.get().scrollTop = child.getOffsetTop() - this.getOffsetTop();
|
|
|
|
- }
|
|
|
|
|
|
+ scrollTo(selector) {
|
|
|
|
+ const child = this.getFirstChild(selector);
|
|
|
|
+ if(child)
|
|
|
|
+ this.get().scrollTop = child.getOffsetTop() - this.getOffsetTop();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ scrollIntoView() {
|
|
|
|
+ this.get().scrollIntoView();
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ style(property, value, priority) {
|
|
|
|
+ this.get().style.setProperty(property, value, priority);
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ focus() {
|
|
|
|
+ this.get().focus();
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
|
|
- style(property, value, priority) {
|
|
|
|
- this.get().style.setProperty(property, value, priority);
|
|
|
|
|
|
+ click() {
|
|
|
|
+ this.get().click();
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
on(event, callback) {
|
|
on(event, callback) {
|
|
- if (isEmpty(event) || isEmpty(callback))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(event) || CDUtils.isEmpty(callback))
|
|
return this;
|
|
return this;
|
|
this.get().addEventListener(event, callback);
|
|
this.get().addEventListener(event, callback);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
un(event, callback) {
|
|
un(event, callback) {
|
|
- if (isEmpty(event))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(event))
|
|
return this;
|
|
return this;
|
|
this.get().removeEventListener(event, callback);
|
|
this.get().removeEventListener(event, callback);
|
|
return this;
|
|
return this;
|
|
@@ -263,34 +279,51 @@ class CDElement {
|
|
}
|
|
}
|
|
|
|
|
|
class Url {
|
|
class Url {
|
|
- constructor(url) {
|
|
|
|
- this.url = new URL(url || location);
|
|
|
|
- this.urlSearch = new URLSearchParams(this.url.search);
|
|
|
|
- }
|
|
|
|
|
|
+ constructor(url) {
|
|
|
|
+ this.url = new URL(url || location);
|
|
|
|
+ this.urlSearch = new URLSearchParams(this.url.search);
|
|
|
|
+ }
|
|
|
|
|
|
- static getHash() {
|
|
|
|
- return new Url().getHash();
|
|
|
|
- }
|
|
|
|
|
|
+ static getHash() {
|
|
|
|
+ return new Url().getHash();
|
|
|
|
+ }
|
|
|
|
|
|
- static setHash(hash) {
|
|
|
|
- return new Url().setHash(hash);
|
|
|
|
- }
|
|
|
|
|
|
+ static setHash(hash) {
|
|
|
|
+ return new Url().setHash(hash);
|
|
|
|
+ }
|
|
|
|
|
|
- static goTo(url) {
|
|
|
|
- window.location = url;
|
|
|
|
|
|
+ static getOrigin() {
|
|
|
|
+ return new Url().getOrigin();
|
|
}
|
|
}
|
|
|
|
|
|
- getHash() {
|
|
|
|
- const hash = this.url.hash.substring(1);
|
|
|
|
- return hash.length > 0 ? hash : null;
|
|
|
|
- }
|
|
|
|
|
|
+ static goTo(url, blank) {
|
|
|
|
+ window.open(url, blank ? '_blank' : '_self');
|
|
|
|
+ }
|
|
|
|
|
|
- setHash(hash) {
|
|
|
|
- this.url.hash = !hash || hash.length == 0 ? '' : `#${hash}`;
|
|
|
|
- return this;
|
|
|
|
|
|
+ static reload() {
|
|
|
|
+ location.reload();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static getFullPath() {
|
|
|
|
+ const url = new Url().url;
|
|
|
|
+ return `${url.origin}${url.pathname}`;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getHash() {
|
|
|
|
+ const hash = this.url.hash.substring(1);
|
|
|
|
+ return hash.length > 0 ? hash : null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setHash(hash) {
|
|
|
|
+ this.url.hash = !hash || hash.length == 0 ? '' : `#${hash}`;
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getOrigin() {
|
|
|
|
+ return this.url.origin;
|
|
}
|
|
}
|
|
|
|
|
|
- toString() {
|
|
|
|
|
|
+ toString() {
|
|
this.url.search = this.urlSearch.toString();
|
|
this.url.search = this.urlSearch.toString();
|
|
return this.url.toString();
|
|
return this.url.toString();
|
|
}
|
|
}
|
|
@@ -301,86 +334,115 @@ class Url {
|
|
}
|
|
}
|
|
|
|
|
|
updateLocation() {
|
|
updateLocation() {
|
|
|
|
+ const hashChanged = Url.getHash() !== this.getHash();
|
|
|
|
+
|
|
history.replaceState(null, null, this.toLocalString());
|
|
history.replaceState(null, null, this.toLocalString());
|
|
- }
|
|
|
|
|
|
+ hashChanged && window.dispatchEvent(new HashChangeEvent('hashchange'));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
class Style {
|
|
class Style {
|
|
- static apply(element, styleStr) {
|
|
|
|
- element = element instanceof CDElement ? element : CDElement.get(element);
|
|
|
|
- const propertiesMap = this.getCssPropertiesMap(styleStr);
|
|
|
|
- for(const prop of Object.keys(propertiesMap))
|
|
|
|
- element.style(prop, propertiesMap[prop].value, propertiesMap[prop].priority);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- static getCssPropertiesMap(styleStr) {
|
|
|
|
- const parts = styleStr.split(';');
|
|
|
|
- const map = {};
|
|
|
|
- for(let part of parts) {
|
|
|
|
- part = part.trim();
|
|
|
|
- if(part.length == 0)
|
|
|
|
- continue;
|
|
|
|
- const propVal = part.split(':');
|
|
|
|
- const property = propVal[0].trim();
|
|
|
|
- const value = propVal[1].trim().split('!');
|
|
|
|
- map[property] = { value: value[0], priority: value.length > 1 ? value[1] : '' };
|
|
|
|
- }
|
|
|
|
- return map;
|
|
|
|
- }
|
|
|
|
|
|
+ static apply(element, styleStr) {
|
|
|
|
+ element = element instanceof CDElement ? element : CDElement.get(element);
|
|
|
|
+ const propertiesMap = this.getCssPropertiesMap(styleStr);
|
|
|
|
+ for(const prop of Object.keys(propertiesMap))
|
|
|
|
+ element.style(prop, propertiesMap[prop].value, propertiesMap[prop].priority);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static getCssPropertiesMap(styleStr) {
|
|
|
|
+ const parts = styleStr.split(';');
|
|
|
|
+ const map = {};
|
|
|
|
+ for(let part of parts) {
|
|
|
|
+ part = part.trim();
|
|
|
|
+ if(part.length == 0)
|
|
|
|
+ continue;
|
|
|
|
+ const propVal = part.split(':');
|
|
|
|
+ const property = propVal[0].trim();
|
|
|
|
+ const value = propVal[1].trim().split('!');
|
|
|
|
+ map[property] = { value: value[0], priority: value.length > 1 ? value[1] : '' };
|
|
|
|
+ }
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
class DOM {
|
|
class DOM {
|
|
|
|
|
|
- static Events = {
|
|
|
|
- Click: 'click',
|
|
|
|
- Load: 'load',
|
|
|
|
- KeyDown: 'keydown',
|
|
|
|
- KeyUp: 'keyup'
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- static Tags = {
|
|
|
|
- Div: 'div',
|
|
|
|
- Span: 'span',
|
|
|
|
- H1: 'h1',
|
|
|
|
- H2: 'h2',
|
|
|
|
- H3: 'h3',
|
|
|
|
- P: 'p'
|
|
|
|
- };
|
|
|
|
|
|
+ static Events = {
|
|
|
|
+ Click: 'click',
|
|
|
|
+ Load: 'load',
|
|
|
|
+ KeyDown: 'keydown',
|
|
|
|
+ KeyUp: 'keyup',
|
|
|
|
+ KeyPress: 'keypress',
|
|
|
|
+ Change: 'change',
|
|
|
|
+ Cut: 'cut',
|
|
|
|
+ Drop: 'drop',
|
|
|
|
+ Paste: 'paste',
|
|
|
|
+ Input: 'input',
|
|
|
|
+ HashChange: 'hashchange',
|
|
|
|
+ MouseDown: 'mousedown',
|
|
|
|
+ ContextMenu: 'contextmenu',
|
|
|
|
+ Blur: 'blur'
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ static Tags = {
|
|
|
|
+ Div: 'div',
|
|
|
|
+ Span: 'span',
|
|
|
|
+ H1: 'h1',
|
|
|
|
+ H2: 'h2',
|
|
|
|
+ H3: 'h3',
|
|
|
|
+ P: 'p',
|
|
|
|
+ Textarea: 'textarea',
|
|
|
|
+ Input: 'input'
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ static Keys = {
|
|
|
|
+ Enter: 'Enter',
|
|
|
|
+ Escape: 'Escape',
|
|
|
|
+ Control: 'Control',
|
|
|
|
+ Shift: 'Shift',
|
|
|
|
+ Backspace: 'Backspace'
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ static MouseButtons = {
|
|
|
|
+ Left: 1,
|
|
|
|
+ Right: 2,
|
|
|
|
+ Middle: 4
|
|
|
|
+ };
|
|
|
|
|
|
static get(selector) {
|
|
static get(selector) {
|
|
- if (isEmpty(selector))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(selector))
|
|
throw "DOM.get() invalid selector.";
|
|
throw "DOM.get() invalid selector.";
|
|
const element = document.querySelector(selector);
|
|
const element = document.querySelector(selector);
|
|
- if (isEmpty(element))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(element))
|
|
return null;
|
|
return null;
|
|
return CDElement.get(element);
|
|
return CDElement.get(element);
|
|
}
|
|
}
|
|
|
|
|
|
- static getAll(selector) {
|
|
|
|
- if (isEmpty(selector))
|
|
|
|
- throw "DOM.getAll() invalid selector.";
|
|
|
|
- const elements = document.querySelectorAll(selector);
|
|
|
|
- if(isEmpty(elements))
|
|
|
|
- return [];
|
|
|
|
- return Array.from(elements).map((element) => CDElement.get(element));
|
|
|
|
- }
|
|
|
|
|
|
+ static getAll(selector) {
|
|
|
|
+ if (CDUtils.isEmpty(selector))
|
|
|
|
+ throw "DOM.getAll() invalid selector.";
|
|
|
|
+ const elements = document.querySelectorAll(selector);
|
|
|
|
+ if(CDUtils.isEmpty(elements))
|
|
|
|
+ return [];
|
|
|
|
+ return Array.from(elements).map((element) => CDElement.get(element));
|
|
|
|
+ }
|
|
|
|
|
|
static create(config, container, prepend) {
|
|
static create(config, container, prepend) {
|
|
- if (isEmpty(config) || isEmpty(config.tag))
|
|
|
|
|
|
+ if (CDUtils.isEmpty(config) || CDUtils.isEmpty(config.tag))
|
|
return;
|
|
return;
|
|
const element = CDElement.get(document.createElement(config.tag));
|
|
const element = CDElement.get(document.createElement(config.tag));
|
|
|
|
|
|
- if (!isEmpty(config.attr)) {
|
|
|
|
|
|
+ if (!CDUtils.isEmpty(config.attr)) {
|
|
Object.keys(config.attr).forEach((name) => {
|
|
Object.keys(config.attr).forEach((name) => {
|
|
element.setAttribute(name, config.attr[name]);
|
|
element.setAttribute(name, config.attr[name]);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- if (!isEmpty(config.cls)) element.addClass(config.cls);
|
|
|
|
- if (!isEmpty(config.id)) element.setId(config.id);
|
|
|
|
- if (!isEmpty(config.style)) Style.apply(element, config.style);
|
|
|
|
|
|
+ if (!CDUtils.isEmpty(config.cls)) element.addClass(config.cls);
|
|
|
|
+ if (!CDUtils.isEmpty(config.id)) element.setId(config.id);
|
|
|
|
+ if (!CDUtils.isEmpty(config.style)) Style.apply(element, config.style);
|
|
|
|
|
|
- if (!isEmpty(config.cn)) {
|
|
|
|
|
|
+ if (!CDUtils.isEmpty(config.cn)) {
|
|
config.cn.forEach((el) => {
|
|
config.cn.forEach((el) => {
|
|
if (el instanceof Element) {
|
|
if (el instanceof Element) {
|
|
element.append(CDElement.get(el));
|
|
element.append(CDElement.get(el));
|
|
@@ -390,16 +452,16 @@ class DOM {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- // innerHTML appends after cn
|
|
|
|
- if (!isEmpty(config.innerHTML)) element.setInnerHTML(element.getInnerHTML() + config.innerHTML);
|
|
|
|
|
|
+ // innerHTML appends after cn
|
|
|
|
+ if (!CDUtils.isEmpty(config.innerHTML)) element.setInnerHTML(element.getInnerHTML() + config.innerHTML);
|
|
|
|
|
|
- if (!isEmpty(container))
|
|
|
|
|
|
+ if (!CDUtils.isEmpty(container))
|
|
(prepend === true ? container.prepend(element) : container.append(element));
|
|
(prepend === true ? container.prepend(element) : container.append(element));
|
|
return element;
|
|
return element;
|
|
}
|
|
}
|
|
|
|
|
|
static append(container, element) {
|
|
static append(container, element) {
|
|
- if (isEmpty(element) || isEmpty(container) ||
|
|
|
|
|
|
+ if (CDUtils.isEmpty(element) || CDUtils.isEmpty(container) ||
|
|
(!(element instanceof Element) && !(element instanceof CDElement)) ||
|
|
(!(element instanceof Element) && !(element instanceof CDElement)) ||
|
|
(!(container instanceof Element) && !(container instanceof CDElement)))
|
|
(!(container instanceof Element) && !(container instanceof CDElement)))
|
|
return;
|
|
return;
|
|
@@ -421,38 +483,89 @@ class DOM {
|
|
cookies[key.trim()] = value;
|
|
cookies[key.trim()] = value;
|
|
});
|
|
});
|
|
|
|
|
|
- const cookieValue = cookies[name];
|
|
|
|
- if(isEmpty(cookieValue))
|
|
|
|
- return null;
|
|
|
|
- if(isJsonString(cookieValue))
|
|
|
|
- return JSON.parse(cookieValue);
|
|
|
|
- else
|
|
|
|
- return cookieValue;
|
|
|
|
|
|
+ const cookieValue = cookies[name];
|
|
|
|
+ if(CDUtils.isEmpty(cookieValue))
|
|
|
|
+ return null;
|
|
|
|
+ if(CDUtils.isJsonString(cookieValue))
|
|
|
|
+ return JSON.parse(cookieValue);
|
|
|
|
+ else
|
|
|
|
+ return cookieValue;
|
|
}
|
|
}
|
|
|
|
|
|
- static getCookieProperty(name, property) {
|
|
|
|
- const cookie = DOM.getCookie(name);
|
|
|
|
- if(cookie && !(cookie instanceof Object))
|
|
|
|
- throw 'DOM.getCookieProperty(): cookie value is not a JSON';
|
|
|
|
- return cookie ? cookie[property] : null;
|
|
|
|
- }
|
|
|
|
|
|
+ static getCookieProperty(name, property) {
|
|
|
|
+ const cookie = DOM.getCookie(name);
|
|
|
|
+ if(cookie && !(cookie instanceof Object))
|
|
|
|
+ throw 'DOM.getCookieProperty(): cookie value is not a JSON';
|
|
|
|
+ return cookie ? cookie[property] : null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static setCookieProperty(name, property, value, hours) {
|
|
|
|
+ const cookie = DOM.getCookie(name);
|
|
|
|
+
|
|
|
|
+ if(cookie) {
|
|
|
|
+ if(!(cookie instanceof Object))
|
|
|
|
+ throw 'DOM.setCookieProperty(): initial cookie value is not a JSON';
|
|
|
|
+
|
|
|
|
+ cookie[property] = value;
|
|
|
|
+ DOM.setCookie(name, cookie, hours || 24);
|
|
|
|
+ } else {
|
|
|
|
+ DOM.setCookie(name, { [property]: value }, hours || 24);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- static setCookieProperty(name, property, value, hours) {
|
|
|
|
- const cookie = DOM.getCookie(name);
|
|
|
|
|
|
+ static documentOn(event, callback) {
|
|
|
|
+ if (CDUtils.isEmpty(event) || CDUtils.isEmpty(callback))
|
|
|
|
+ return;
|
|
|
|
+ document.addEventListener(event, callback);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static copyToClipboard(str) {
|
|
|
|
+ const body = DOM.get('body');
|
|
|
|
+ const input = DOM.create({ tag: DOM.Tags.Input, style: 'display: none;' }, body);
|
|
|
|
|
|
- if(cookie) {
|
|
|
|
- if(!(cookie instanceof Object))
|
|
|
|
- throw 'DOM.setCookieProperty(): initial cookie value is not a JSON';
|
|
|
|
|
|
+ input.setValue(str);
|
|
|
|
+ input.get().select();
|
|
|
|
+ input.get().setSelectionRange(0, 99999);
|
|
|
|
|
|
- cookie[property] = value;
|
|
|
|
- DOM.setCookie(name, cookie, hours || 24);
|
|
|
|
- } else {
|
|
|
|
- DOM.setCookie(name, { [property]: value }, hours || 24);
|
|
|
|
- }
|
|
|
|
|
|
+ navigator.clipboard.writeText(input.get().value).then(() => {
|
|
|
|
+ input.remove();
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
class CDUtils {
|
|
class CDUtils {
|
|
|
|
+
|
|
|
|
+ static isEmpty(val) {
|
|
|
|
+ return val === null || val === undefined ||
|
|
|
|
+ val === '' || val.length === 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static isJsonString(str) {
|
|
|
|
+ try {
|
|
|
|
+ JSON.parse(str);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static nl2br(str) {
|
|
|
|
+ return str.replaceAll(/(?:\r\n|\r|\n)/g, '<br/>');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static br2nl(str) {
|
|
|
|
+ return str.replaceAll(/<br\/?>/g, '\n');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static async SHA256(input) {
|
|
|
|
+ return crypto.subtle.digest('SHA-256', new TextEncoder('utf8').encode(input)).then(h => {
|
|
|
|
+ const hexes = [], view = new DataView(h);
|
|
|
|
+ for (let i = 0; i < view.byteLength; i += 4)
|
|
|
|
+ hexes.push(('00000000' + view.getUint32(i).toString(16)).slice(-8));
|
|
|
|
+ return hexes.join('');
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Returns string representation of <b>x</b> with leading zeros.<br/>
|
|
* Returns string representation of <b>x</b> with leading zeros.<br/>
|
|
* Length of the resulting string will be equal to <b>length</b> or <b>2</b> if <b>length</b> was not specified.<br/>
|
|
* Length of the resulting string will be equal to <b>length</b> or <b>2</b> if <b>length</b> was not specified.<br/>
|
|
@@ -536,10 +649,6 @@ class CDUtils {
|
|
.sort((a, b) => a.sort - b.sort)
|
|
.sort((a, b) => a.sort - b.sort)
|
|
.map(({ value }) => value);
|
|
.map(({ value }) => value);
|
|
}
|
|
}
|
|
-
|
|
|
|
- static toHtml(str) {
|
|
|
|
- return str.replace(/(?:\r\n|\r|\n)/g, '<br>');
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
const window_ = CDElement.get(window);
|
|
const window_ = CDElement.get(window);
|