-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup for ESLBasePopupGroupManager
Rename ESLBasePopupGroupManager to ESLPopupGroupDispatcher. ESLPopupGroupDispatcher use hidden dom element to manage state. BREAKING CHANGE: rename ESLBasePopupGroupManager to ESLPopupGroupDispatcher
- Loading branch information
Showing
9 changed files
with
141 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import "./core/esl-popup-dispatcher.less"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './core/esl-base-popup'; | ||
export * from './core/esl-base-popup-group-manager'; | ||
export * from './core/esl-popup-dispatcher'; |
114 changes: 0 additions & 114 deletions
114
src/modules/esl-base-popup/core/esl-base-popup-group-manager.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
esl-popup-dispatcher { | ||
display: none; | ||
} |
125 changes: 125 additions & 0 deletions
125
src/modules/esl-base-popup/core/esl-popup-dispatcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import {ESLBasePopup} from './esl-base-popup'; | ||
import {ESLBaseElement} from '../../esl-base-element/core'; | ||
import {bind} from '../../esl-utils/decorators/bind'; | ||
import {EventUtils} from '../../esl-utils/dom/events'; | ||
import {ExportNs} from '../../esl-utils/environment/export-ns'; | ||
|
||
@ExportNs('PopupGroupDispatcher') | ||
export class ESLPopupDispatcher extends ESLBaseElement { | ||
public static readonly is = 'esl-popup-dispatcher'; | ||
|
||
/** | ||
* Initialize PopupGroupDispatcher | ||
* Uses esl-popup-dispatcher tag and document body root by default | ||
*/ | ||
public static init(root: HTMLElement = document.body, tagName: string = this.is) { | ||
if (!root) throw new Error('Root element should be specified'); | ||
const instances = root.getElementsByTagName(tagName); | ||
if (instances.length) return; | ||
this.register(tagName); | ||
root.insertAdjacentElement('afterbegin', document.createElement(tagName)); | ||
} | ||
|
||
protected _root: HTMLElement | null; | ||
protected _popups: Map<string, ESLBasePopup> = new Map<string, ESLBasePopup>(); | ||
|
||
protected connectedCallback() { | ||
super.connectedCallback(); | ||
this.root = this.parentElement; | ||
} | ||
protected disconnectedCallback() { | ||
super.disconnectedCallback(); | ||
this.root = null; | ||
} | ||
|
||
protected bindEvents() { | ||
if (!this.root) return; | ||
this.root.addEventListener('esl:before:show', this._onBeforeShow); | ||
this.root.addEventListener('esl:show', this._onShow); | ||
this.root.addEventListener('esl:hide', this._onHide); | ||
this.root.addEventListener('esl:change:group', this._onChangeGroup); | ||
} | ||
protected unbindEvents() { | ||
if (!this.root) return; | ||
this.root.removeEventListener('esl:before:show', this._onBeforeShow); | ||
this.root.removeEventListener('esl:show', this._onShow); | ||
this.root.removeEventListener('esl:hide', this._onHide); | ||
this.root.removeEventListener('esl:change:group', this._onChangeGroup); | ||
} | ||
|
||
/** Observed element */ | ||
public get root(): HTMLElement | null { | ||
return this._root; | ||
} | ||
public set root(root: HTMLElement | null) { | ||
this.unbindEvents(); | ||
this._root = root; | ||
this.bindEvents(); | ||
} | ||
|
||
/** Guard-condition for targets */ | ||
protected isAcceptable(target: any): target is ESLBasePopup { | ||
if (!(target instanceof ESLBasePopup)) return false; | ||
return !!target.groupName; | ||
} | ||
|
||
/** Hide active popup in group */ | ||
public hideActive(groupName: string) { | ||
this.getActive(groupName)?.hide(); | ||
} | ||
|
||
/** Set active popup in group */ | ||
public setActive(groupName: string, popup: ESLBasePopup) { | ||
if (!groupName) return; | ||
this.hideActive(groupName); | ||
this._popups.set(groupName, popup); | ||
} | ||
|
||
/** Get active popup in group or undefined if group doesn't exist */ | ||
public getActive(groupName: string): ESLBasePopup | undefined { | ||
return this._popups.get(groupName); | ||
} | ||
|
||
/** Delete popup from the group if passed popup is currently active */ | ||
public deleteActive(groupName: string, popup: ESLBasePopup) { | ||
if (this.getActive(groupName) !== popup) return; | ||
this._popups.delete(groupName); | ||
} | ||
|
||
/** Hide active popup before e.target (popup) will be shown */ | ||
@bind | ||
protected _onBeforeShow(e: CustomEvent) { | ||
const target = EventUtils.source(e); | ||
if (!this.isAcceptable(target)) return; | ||
this.hideActive(target.groupName); | ||
} | ||
|
||
/** Update active popup after a new popup is shown */ | ||
@bind | ||
protected _onShow(e: CustomEvent) { | ||
const target = EventUtils.source(e); | ||
if (!this.isAcceptable(target)) return; | ||
|
||
this.setActive(target.groupName, target); | ||
} | ||
|
||
/** Update group state after active popup is hidden */ | ||
@bind | ||
protected _onHide(e: CustomEvent) { | ||
const target = EventUtils.source(e); | ||
if (!this.isAcceptable(target)) return; | ||
|
||
this.deleteActive(target.groupName, target); | ||
} | ||
|
||
/** Update active popups */ | ||
@bind | ||
protected _onChangeGroup(e: CustomEvent) { | ||
const target = EventUtils.source(e); | ||
if (!this.isAcceptable(target)) return; | ||
|
||
const {oldGroupName, newGroupName} = e.detail; | ||
this.deleteActive(oldGroupName, target); | ||
this.setActive(newGroupName, target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters