Skip to content

Commit

Permalink
WIP: preventing duplicated initialization logic is added
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethanCeyer committed Mar 30, 2018
1 parent 91ec37c commit 6ec057d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
37 changes: 24 additions & 13 deletions dist/formulize.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/formulize.umd.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions dist/types/ui/ui.dom.d.ts
@@ -1,14 +1,15 @@
import { FormulizeOptions } from '../formulize.interface';
export declare abstract class UIDom {
protected elem: HTMLElement;
protected options: FormulizeOptions;
protected container: JQuery;
protected statusBox: JQuery;
protected textBox: JQuery;
protected cursor: JQuery;
protected elem: HTMLElement;
protected options: FormulizeOptions;
protected readonly cursorIndex: number;
protected readonly dragElem: JQuery;
constructor(elem: HTMLElement, options?: FormulizeOptions);
protected initializeDOM(): void;
protected isAlreadyInitialized(): boolean;
protected attachEvents(): void;
protected getPrevUnit(elem: HTMLElement): HTMLElement;
protected getNextUnit(elem: HTMLElement): HTMLElement;
Expand Down
12 changes: 10 additions & 2 deletions src/ui/ui.base.ts
@@ -1,9 +1,17 @@
import { FormulizeOptions } from '../../dist/types/formulize/option.interface';
import { UIHook } from './ui.hook';
import { defaultOptions } from '../option.value';

export abstract class UIBase extends UIHook {
public constructor(elem: HTMLElement, options?: FormulizeOptions) {
super(elem, options);
public constructor(elem: HTMLElement, options: FormulizeOptions = { ...defaultOptions }) {
super();
this.elem = elem;
this.options = options;

if (this.isAlreadyInitialized())
return;

this.initializeDOM();
this.attachEvents();
}
}
11 changes: 10 additions & 1 deletion src/ui/ui.dom.ts
Expand Up @@ -8,6 +8,8 @@ export abstract class UIDom {
protected statusBox: JQuery;
protected textBox: JQuery;
protected cursor: JQuery;
protected elem: HTMLElement;
protected options: FormulizeOptions;

protected get cursorIndex(): number {
return this.cursor
Expand All @@ -20,7 +22,7 @@ export abstract class UIDom {
.find(`.${this.options.id}-drag`);
}

constructor(protected elem: HTMLElement, protected options: FormulizeOptions = { ...defaultOptions }) {
protected initializeDOM() {
this.container = $(this.elem);
this.container.addClass(`${this.options.id}-container`);
this.container.wrap(`<div class="${this.options.id}-wrapper"></div>`);
Expand All @@ -33,6 +35,13 @@ export abstract class UIDom {
this.textBox.trigger('focus');
}

protected isAlreadyInitialized(): boolean {
const selfAndContainer = $(this.elem)
.closest(`.${this.options.id}-container`)
.add(this.elem);
return !!selfAndContainer.filter(`.${this.options.id}-container`).length;
}

protected attachEvents() {
throw new Error('method not implemented');
}
Expand Down

0 comments on commit 6ec057d

Please sign in to comment.