improve js

This commit is contained in:
Daniel 2021-06-23 23:19:35 +05:30
parent 7334c5e73f
commit ba2a2cebfe
9 changed files with 117 additions and 69 deletions

View file

@ -11,7 +11,7 @@ export {
SidebarOverlay, SidebarOverlay,
Treeview, Treeview,
DirectChat, DirectChat,
CardWidget CardWidget,
} }
// //

View file

@ -8,7 +8,7 @@
import { import {
domReady, domReady,
slideUp, slideUp,
slideDown slideDown,
} from './util/index' } from './util/index'
/** /**
@ -39,7 +39,7 @@ const Default = {
collapseIcon: 'fa-minus', collapseIcon: 'fa-minus',
expandIcon: 'fa-plus', expandIcon: 'fa-plus',
maximizeIcon: 'fa-expand', maximizeIcon: 'fa-expand',
minimizeIcon: 'fa-compress' minimizeIcon: 'fa-compress',
} }
interface Config { interface Config {
@ -69,51 +69,63 @@ class CardWidget {
} }
collapse() { collapse() {
this._parent?.classList.add(CLASS_NAME_COLLAPSING) if (this._parent) {
this._parent.classList.add(CLASS_NAME_COLLAPSING)
const elm = this._parent?.querySelectorAll(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`) const elm = this._parent?.querySelectorAll(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`)
if (elm !== undefined) { if (elm !== undefined) {
for (const el of elm) { for (const el of elm) {
if (el instanceof HTMLElement) { if (el instanceof HTMLElement) {
slideUp(el, this._config.animationSpeed) slideUp(el, this._config.animationSpeed)
}
} }
} }
}
setTimeout(() => { setTimeout(() => {
this._parent?.classList.add(CLASS_NAME_COLLAPSED) if (this._parent) {
this._parent?.classList.remove(CLASS_NAME_COLLAPSING) this._parent.classList.add(CLASS_NAME_COLLAPSED)
}, this._config.animationSpeed) this._parent.classList.remove(CLASS_NAME_COLLAPSING)
}
}, this._config.animationSpeed)
}
const icon = this._parent?.querySelector(`${SELECTOR_CARD_HEADER} ${this._config.collapseTrigger} .${this._config.collapseIcon}`) const icon = this._parent?.querySelector(`${SELECTOR_CARD_HEADER} ${this._config.collapseTrigger} .${this._config.collapseIcon}`)
icon?.classList.add(this._config.expandIcon) if (icon) {
icon?.classList.remove(this._config.collapseIcon) icon.classList.remove(this._config.collapseIcon)
icon.classList.add(this._config.expandIcon)
}
} }
expand() { expand() {
this._parent?.classList.add(CLASS_NAME_EXPANDING) if (this._parent) {
this._parent.classList.add(CLASS_NAME_EXPANDING)
const elm = this._parent?.querySelectorAll(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`) const elm = this._parent?.querySelectorAll(`${SELECTOR_CARD_BODY}, ${SELECTOR_CARD_FOOTER}`)
if (elm !== undefined) { if (elm !== undefined) {
for (const el of elm) { for (const el of elm) {
if (el instanceof HTMLElement) { if (el instanceof HTMLElement) {
slideDown(el, this._config.animationSpeed) slideDown(el, this._config.animationSpeed)
}
} }
} }
}
setTimeout(() => { setTimeout(() => {
this._parent?.classList.remove(CLASS_NAME_COLLAPSED) if (this._parent) {
this._parent?.classList.remove(CLASS_NAME_EXPANDING) this._parent.classList.remove(CLASS_NAME_COLLAPSED)
}, this._config.animationSpeed) this._parent.classList.remove(CLASS_NAME_EXPANDING)
}
}, this._config.animationSpeed)
}
const icon = this._parent?.querySelector(`${SELECTOR_CARD_HEADER} ${this._config.collapseTrigger} .${this._config.expandIcon}`) const icon = this._parent?.querySelector(`${SELECTOR_CARD_HEADER} ${this._config.collapseTrigger} .${this._config.expandIcon}`)
icon?.classList.add(this._config.collapseIcon) if (icon) {
icon?.classList.remove(this._config.expandIcon) icon.classList.add(this._config.collapseIcon)
icon.classList.remove(this._config.expandIcon)
}
} }
remove() { remove() {
@ -134,18 +146,29 @@ class CardWidget {
maximize() { maximize() {
if (this._parent) { if (this._parent) {
const maxElm = this._parent.querySelector(`${this._config.maximizeTrigger} .${this._config.maximizeIcon}`) const maxElm = this._parent.querySelector(`${this._config.maximizeTrigger} .${this._config.maximizeIcon}`)
maxElm?.classList.add(this._config.minimizeIcon)
maxElm?.classList.remove(this._config.maximizeIcon) if (maxElm) {
maxElm.classList.add(this._config.minimizeIcon)
maxElm.classList.remove(this._config.maximizeIcon)
}
this._parent.style.height = `${this._parent.scrollHeight}px` this._parent.style.height = `${this._parent.scrollHeight}px`
this._parent.style.width = `${this._parent.scrollWidth}px` this._parent.style.width = `${this._parent.scrollWidth}px`
this._parent.style.transition = 'all .15s' this._parent.style.transition = 'all .15s'
setTimeout(() => { setTimeout(() => {
document.querySelector('html')?.classList.add(CLASS_NAME_MAXIMIZED) const htmlTag = document.querySelector('html')
this._parent?.classList.add(CLASS_NAME_MAXIMIZED)
if (this._parent?.classList.contains(CLASS_NAME_COLLAPSED)) { if (htmlTag) {
this._parent.classList.add(CLASS_NAME_WAS_COLLAPSED) htmlTag.classList.add(CLASS_NAME_MAXIMIZED)
}
if (this._parent) {
this._parent.classList.add(CLASS_NAME_MAXIMIZED)
if (this._parent.classList.contains(CLASS_NAME_COLLAPSED)) {
this._parent.classList.add(CLASS_NAME_WAS_COLLAPSED)
}
} }
}, 150) }, 150)
} }
@ -155,13 +178,20 @@ class CardWidget {
if (this._parent) { if (this._parent) {
const minElm = this._parent.querySelector(`${this._config.maximizeTrigger} .${this._config.minimizeIcon}`) const minElm = this._parent.querySelector(`${this._config.maximizeTrigger} .${this._config.minimizeIcon}`)
minElm?.classList.add(this._config.maximizeIcon) if (minElm) {
minElm?.classList.remove(this._config.minimizeIcon) minElm.classList.add(this._config.maximizeIcon)
minElm.classList.remove(this._config.minimizeIcon)
}
this._parent.style.cssText = `height: ${this._parent.style.height} !important; width: ${this._parent.style.width} !important; transition: all .15s;` this._parent.style.cssText = `height: ${this._parent.style.height} !important; width: ${this._parent.style.width} !important; transition: all .15s;`
setTimeout(() => { setTimeout(() => {
document.querySelector('html')?.classList.remove(CLASS_NAME_MAXIMIZED) const htmlTag = document.querySelector('html')
if (htmlTag) {
htmlTag.classList.remove(CLASS_NAME_MAXIMIZED)
}
if (this._parent) { if (this._parent) {
this._parent.classList.remove(CLASS_NAME_MAXIMIZED) this._parent.classList.remove(CLASS_NAME_MAXIMIZED)

View file

@ -6,7 +6,7 @@
*/ */
import { import {
domReady domReady,
} from './util/index' } from './util/index'
/** /**

View file

@ -6,7 +6,7 @@
*/ */
import { import {
domReady domReady,
} from './util/index' } from './util/index'
/** /**
@ -21,7 +21,7 @@ const SELECTOR_SIDEBAR = '.sidebar'
const Default = { const Default = {
scrollbarTheme: 'os-theme-light', scrollbarTheme: 'os-theme-light',
scrollbarAutoHide: 'leave' scrollbarAutoHide: 'leave',
} }
interface Config { interface Config {
@ -67,8 +67,8 @@ domReady(() => {
sizeAutoCapable: true, sizeAutoCapable: true,
scrollbars: { scrollbars: {
autoHide: Default.scrollbarAutoHide, autoHide: Default.scrollbarAutoHide,
clickScrolling: true clickScrolling: true,
} },
}) })
} }
}) })

View file

@ -6,7 +6,7 @@
*/ */
import { import {
domReady domReady,
} from './util/index' } from './util/index'
/** /**
@ -109,13 +109,16 @@ class PushMenu {
sidebarHover(): void { sidebarHover(): void {
const selSidebar = document.querySelector(SELECTOR_SIDEBAR) const selSidebar = document.querySelector(SELECTOR_SIDEBAR)
selSidebar?.addEventListener('mouseover', () => {
this._bodyClass.add(CLASS_NAME_SIDEBAR_IS_HOVER)
})
selSidebar?.addEventListener('mouseout', () => { if (selSidebar) {
this._bodyClass.remove(CLASS_NAME_SIDEBAR_IS_HOVER) selSidebar.addEventListener('mouseover', () => {
}) this._bodyClass.add(CLASS_NAME_SIDEBAR_IS_HOVER)
})
selSidebar.addEventListener('mouseout', () => {
this._bodyClass.remove(CLASS_NAME_SIDEBAR_IS_HOVER)
})
}
} }
toggleFull(): void { toggleFull(): void {

View file

@ -6,7 +6,7 @@
*/ */
import { import {
domReady domReady,
} from './util/index' } from './util/index'
/** /**
@ -25,7 +25,7 @@ const SELECTOR_SIDEBAR_SM = `.${CLASS_NAME_LAYOUT_MOBILE}`
const SELECTOR_CONTENT_WRAPPER = '.content-wrapper' const SELECTOR_CONTENT_WRAPPER = '.content-wrapper'
const Defaults = { const Defaults = {
onLayouMobile: 992 onLayouMobile: 992,
} }
class SidebarOverlay { class SidebarOverlay {
@ -60,8 +60,10 @@ class SidebarOverlay {
const selSidebarSm = document.querySelector(SELECTOR_SIDEBAR_SM) const selSidebarSm = document.querySelector(SELECTOR_SIDEBAR_SM)
const selContentWrapper = selSidebarSm?.querySelector(SELECTOR_CONTENT_WRAPPER) const selContentWrapper = selSidebarSm?.querySelector(SELECTOR_CONTENT_WRAPPER)
selContentWrapper?.addEventListener('touchstart', this.removeOverlaySidebar) if (selContentWrapper) {
selContentWrapper?.addEventListener('click', this.removeOverlaySidebar) selContentWrapper.addEventListener('touchstart', this.removeOverlaySidebar)
selContentWrapper.addEventListener('click', this.removeOverlaySidebar)
}
} }
} }

View file

@ -8,7 +8,7 @@
import { import {
domReady, domReady,
slideDown, slideDown,
slideUp slideUp,
} from './util/index' } from './util/index'
/** /**
@ -17,6 +17,14 @@ import {
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
// const NAME = 'Treeview'
const DATA_KEY = 'lte.treeview'
const EVENT_KEY = `.${DATA_KEY}`
const EVENT_EXPANDED = `expanded${EVENT_KEY}`
const EVENT_COLLAPSED = `collapsed${EVENT_KEY}`
// const EVENT_LOAD_DATA_API = `load${EVENT_KEY}`
const CLASS_NAME_MENU_OPEN = 'menu-open' const CLASS_NAME_MENU_OPEN = 'menu-open'
const CLASS_NAME_MENU_IS_OPEN = 'menu-is-open' const CLASS_NAME_MENU_IS_OPEN = 'menu-is-open'
const SELECTOR_NAV_ITEM = '.nav-item' const SELECTOR_NAV_ITEM = '.nav-item'
@ -24,7 +32,7 @@ const SELECTOR_TREEVIEW_MENU = '.nav-treeview'
const SELECTOR_DATA_TOGGLE = '[data-lte-toggle="treeview"]' const SELECTOR_DATA_TOGGLE = '[data-lte-toggle="treeview"]'
const Default = { const Default = {
animationSpeed: 300 animationSpeed: 300,
} }
interface Config { interface Config {
@ -45,27 +53,36 @@ class Treeview {
constructor(element: HTMLElement, config: Config) { constructor(element: HTMLElement, config: Config) {
this._element = element this._element = element
this._navItem = this._element.closest(SELECTOR_NAV_ITEM) this._navItem = this._element?.closest(SELECTOR_NAV_ITEM)
this._childNavItem = this._navItem?.querySelector(SELECTOR_TREEVIEW_MENU) this._childNavItem = this._navItem?.querySelector(SELECTOR_TREEVIEW_MENU)
this._config = Object.assign({}, Default, config) this._config = {...Default, ...config}
} }
open(): void { open(): void {
this._navItem?.classList.add(CLASS_NAME_MENU_OPEN) const event = new CustomEvent(EVENT_EXPANDED)
this._navItem?.classList.add(CLASS_NAME_MENU_IS_OPEN)
if (this._navItem) {
this._navItem.classList.add(CLASS_NAME_MENU_OPEN)
this._navItem.classList.add(CLASS_NAME_MENU_IS_OPEN)
}
if (this._childNavItem) { if (this._childNavItem) {
slideDown(this._childNavItem, this._config.animationSpeed) slideDown(this._childNavItem, this._config.animationSpeed)
this._element.dispatchEvent(event)
} }
} }
close(): void { close(): void {
this._navItem?.classList.remove(CLASS_NAME_MENU_IS_OPEN) const event = new CustomEvent(EVENT_COLLAPSED)
this._navItem?.classList.remove(CLASS_NAME_MENU_OPEN) if (this._navItem) {
this._navItem.classList.remove(CLASS_NAME_MENU_IS_OPEN)
this._navItem.classList.remove(CLASS_NAME_MENU_OPEN)
}
if (this._childNavItem) { if (this._childNavItem) {
slideUp(this._childNavItem, this._config.animationSpeed) slideUp(this._childNavItem, this._config.animationSpeed)
this._element.dispatchEvent(event)
} }
} }
@ -86,6 +103,7 @@ class Treeview {
domReady(() => { domReady(() => {
const button = document.querySelectorAll(SELECTOR_DATA_TOGGLE) const button = document.querySelectorAll(SELECTOR_DATA_TOGGLE)
for (const btn of button) { for (const btn of button) {
btn.addEventListener('click', event => { btn.addEventListener('click', event => {
// event.preventDefault() // event.preventDefault()
@ -99,5 +117,3 @@ domReady(() => {
}) })
export default Treeview export default Treeview
//

View file

@ -94,5 +94,5 @@ export {
windowReady, windowReady,
slideUp, slideUp,
slideDown, slideDown,
slideToggle slideToggle,
} }

View file

@ -1,13 +1,10 @@
{ {
"compilerOptions": { "compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"noImplicitAny": true, "noImplicitAny": true,
"target": "esnext", "target": "es2018",
"module": "esnext", "module": "esnext",
"strict": true, "strict": true,
"strictNullChecks": true, "strictNullChecks": true,