Skip to content

Commit

Permalink
Merge pull request #1642 from exadel-inc/feat/esl-base-interface
Browse files Browse the repository at this point in the history
feat: `ESLBaseComponent` interface shared between `ESLMixinElement` and `ESLBaseElement`
  • Loading branch information
ala-n authored May 8, 2023
2 parents 58bf417 + 71699f9 commit e19871f
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/modules/draft/esl-carousel/core/esl-carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class ESLCarousel extends ESLBaseElement {
ESLCarouselViewRegistry.instance.removeListener(this._onRegistryChange);
}

private attributeChangedCallback(attrName: string, oldVal: string, newVal: string) {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string) {
// TODO: change observed attributes
if (attrName === 'config') {
this.configRules = ESLMediaRuleList.parse<CarouselConfig>(this.config, ESLMediaRuleList.OBJECT_PARSER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ESLCarouselLinkPlugin extends ESLCarouselPlugin {
$target.goTo($source.firstIndex, e.detail.direction);
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string) {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string) {
if (this.carousel && oldVal !== newVal) {
this.unbind();
if (attrName === 'to') {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-animate/core/esl-animate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ESLAnimate extends ESLBaseElement {
return ESLTraversingQuery.all(this.target, this) as HTMLElement[];
}

protected attributeChangedCallback(): void {
protected override attributeChangedCallback(): void {
if (!this.connected) return;
this.reanimate();
}
Expand Down
9 changes: 8 additions & 1 deletion src/modules/esl-base-element/core/esl-base-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import type {
ESLListenerCriteria,
ESLListenerDescriptor
} from '../../esl-utils/dom/events';
import type {ESLBaseComponent} from '../../esl-utils/abstract/component';

/**
* Base class for ESL custom elements
* Allows defining custom element with the optional custom tag name
*/
export abstract class ESLBaseElement extends HTMLElement {
export abstract class ESLBaseElement extends HTMLElement implements ESLBaseComponent {
/** Custom element tag name */
public static is = '';

Expand All @@ -40,6 +41,12 @@ export abstract class ESLBaseElement extends HTMLElement {
ESLEventUtils.unsubscribe(this);
}

/**
* Callback to handle changing of element attributes.
* Happens when attribute accessed for writing independently of the actual value change
*/
protected attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void {}

/** Check that the element is connected and `connectedCallback` has been executed */
public get connected(): boolean {
return this._connected;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-footnotes/core/esl-note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class ESLNote extends ESLBaseElement {
this.restore();
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || oldVal === newVal) return;
if (attrName === 'tooltip-shown' && newVal === null) {
this._$footnotes?.turnOffHighlight(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ESLSelectItem extends ESLBaseElement {
this.setAttribute('aria-selected', String(this.selected));
}

protected attributeChangedCallback(attrName: string): void {
protected override attributeChangedCallback(attrName: string): void {
if (attrName === 'selected') {
this.setAttribute('aria-selected', String(this.selected));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ESLSelectList extends ESLSelectWrapper {
this.$selectAll.classList.add('esl-select-all-item');
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || newVal === oldVal) return;
if (attrName === 'select-all-label') {
this.$selectAll.textContent = newVal;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-forms/esl-select/core/esl-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class ESLSelect extends ESLSelectWrapper {
this.$dropdown = document.createElement(ESLSelectDropdown.is);
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (attrName === 'disabled') this._updateDisabled();
if (attrName === 'dropdown-class') {
this.$dropdown.$$cls(oldVal, false);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-image/core/esl-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ESLImage extends ESLBaseElement {
}
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || oldVal === newVal) return;
switch (attrName) {
case 'aria-label':
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-media/core/esl-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class ESLMedia extends ESLBaseElement {
this._provider && this._provider.unbind();
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || oldVal === newVal) return;
switch (attrName) {
case 'disabled':
Expand Down
3 changes: 2 additions & 1 deletion src/modules/esl-mixin-element/ui/esl-mixin-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import type {
ESLListenerDescriptor,
ESLListenerHandler
} from '../../esl-utils/dom/events';
import type {ESLBaseComponent} from '../../esl-utils/abstract/component';
import type {ESLDomElementRelated} from '../../esl-utils/abstract/dom-target';

/**
* Base class for mixin elements.
* Mixin elements attaches to the DOM element via attribute.
* Allows multiple mixin elements per one DOM element
*/
export class ESLMixinElement implements ESLDomElementRelated {
export class ESLMixinElement implements ESLBaseComponent, ESLDomElementRelated {
/** Root attribute to identify mixin targets. Should contain dash in the name. */
static is: string;
/** Additional observed attributes */
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-panel-group/core/esl-panel-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class ESLPanelGroup extends ESLBaseElement {
});
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || oldVal === newVal) return;
if (attrName === 'mode' || attrName === 'min-open-items' || attrName === 'max-open-items') {
this.$$off(this._onConfigChange);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-scrollbar/core/esl-scrollbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ESLScrollbar extends ESLBaseElement {
this._scrollTimer && window.clearTimeout(this._scrollTimer);
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || oldVal === newVal) return;
if (attrName === 'target') this.findTarget();
if (attrName === 'horizontal') this.refresh();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-tab/core/esl-tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class ESLTabs extends ESLBaseElement {
this.updateScrollableType();
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || oldVal === newVal) return;
if (attrName === 'scrollable') {
memoize.clear(this, 'scrollableTypeRules');
Expand Down
4 changes: 2 additions & 2 deletions src/modules/esl-toggleable/core/esl-toggleable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ESLToggleableRequestDetails extends ESLToggleableActionParams {
match?: string | ((target: Element) => boolean);
}

/** @deprecated alias for ESLToggleableActionParams */
/** @deprecated alias for ESLToggleableActionParams, will be removed in 5.0.0*/
export type ToggleableActionParams = ESLToggleableActionParams;

const activators: WeakMap<ESLToggleable, HTMLElement | undefined> = new WeakMap();
Expand Down Expand Up @@ -132,7 +132,7 @@ export class ESLToggleable extends ESLBaseElement {
activators.delete(this);
}

protected attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
protected override attributeChangedCallback(attrName: string, oldVal: string, newVal: string): void {
if (!this.connected || newVal === oldVal) return;
switch (attrName) {
case 'open':
Expand Down
2 changes: 1 addition & 1 deletion src/modules/esl-trigger/core/esl-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ESLTrigger extends ESLBaseElement {

protected _$target: ESLToggleable | null;

protected attributeChangedCallback(attrName: string): void {
protected override attributeChangedCallback(attrName: string): void {
if (!this.connected) return;
if (attrName === 'target') return this.updateTargetFromSelector();
}
Expand Down
43 changes: 43 additions & 0 deletions src/modules/esl-utils/abstract/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {
ESLEventListener,
ESLListenerHandler,
ESLListenerCriteria,
ESLListenerDescriptor
} from '../../esl-event-listener/core';

export interface ESLBaseComponent {
/** Subscribes `handler` method marked with `@listen` decorator */
$$on(handler: ESLListenerHandler): ESLEventListener[];
/** Subscribes `handler` function by the passed DOM event descriptor {@link ESLListenerDescriptor} or event name */
$$on<EType extends keyof ESLListenerEventMap>(
event: EType | ESLListenerDescriptor<EType>,
handler: ESLListenerHandler<ESLListenerEventMap[EType]>
): ESLEventListener[];

/** Unsubscribes event listeners */
$$off(...condition: ESLListenerCriteria[]): ESLEventListener[];

/**
* Dispatches component custom event on current DOM related element.
* @param eventName - event name
* @param eventInit - custom event init. See {@link CustomEventInit}
*/
$$fire(eventName: string, eventInit?: CustomEventInit): boolean;

/**
* Gets or sets CSS classes for current DOM related element.
* @param cls - CSS classes query {@link CSSClassUtils}
* @param value - boolean to set CSS class(es) state or undefined to skip mutation
* @returns current classes state or passed state
*/
$$cls(cls: string, value?: boolean): boolean;

/**
* Gets or sets an attribute for the current DOM related element.
* If the `value` param is undefined then skips mutation.
* @param name - attribute name
* @param value - string attribute value, boolean attribute state or `null` to delete attribute
* @returns the current attribute value or previous value for mutation
*/
$$attr(name: string, value?: null | boolean | string): string | null;
}

0 comments on commit e19871f

Please sign in to comment.