Skip to content

Commit

Permalink
Make text arguments optional in node constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
s3bba committed Feb 26, 2024
1 parent 00eb82d commit 1a1ac6e
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions src/ui/node_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ import {MgNode} from "./node";
export class MgAnchor extends MgNode {
constructor(
url: string,
text: string
text?: string
) {
super(document.createElement('a'));
this.text(text);
this.html_element.setAttribute('href', url);


if (text) {
this.text(text);
}
}

open_in_new_tab(): MgAnchor {
Expand All @@ -44,7 +48,7 @@ export class MgAnchor extends MgNode {
export class MgRouterAnchor extends MgAnchor {
constructor(
url: string,
text: string
text?: string
) {
super(url, text);
this.html_element.onclick = (event: MouseEvent) => {
Expand All @@ -55,9 +59,12 @@ export class MgRouterAnchor extends MgAnchor {
}

export class MgButton extends MgNode {
constructor(text: string) {
constructor(text?: string) {
super(document.createElement('button'));
this.text(text);

if (text) {
this.text(text);
}
}
}

Expand All @@ -68,9 +75,12 @@ export class MgCanvas extends MgNode {
}

export class MgCode extends MgNode {
constructor(text: string) {
constructor(text?: string) {
super(document.createElement('code'));
this.text(text);

if (text) {
this.text(text);
}
}
}

Expand All @@ -81,16 +91,22 @@ export class MgDiv extends MgNode {
}

export class MgHeading extends MgNode {
constructor(level: number, text: string) {
constructor(level: number, text?: string) {
super(document.createElement(`h${level}`));
this.text(text);

if (text) {
this.text(text);
}
}
}

export class MgParagraph extends MgNode {
constructor(text: string) {
constructor(text?: string) {
super(document.createElement('p'));
this.text(text);

if (text) {
this.text(text);
}
}
}

Expand All @@ -101,9 +117,12 @@ export class MgPre extends MgNode {
}

export class MgSpan extends MgNode {
constructor(text: string) {
constructor(text?: string) {
super(document.createElement('span'));
this.text(text);

if (text) {
this.text(text);
}
}
}

Expand All @@ -114,9 +133,12 @@ export class MgTable extends MgNode {
}

export class MgTableCell extends MgNode {
constructor(text: string) {
constructor(text?: string) {
super(document.createElement('td'));
this.text(text);

if (text) {
this.text(text);
}
}
}

Expand All @@ -127,9 +149,12 @@ export class MgTableRow extends MgNode {
}

export class MgTableHeadCell extends MgNode {
constructor(text: string) {
constructor(text?: string) {
super(document.createElement('th'));
this.text(text);

if (text) {
this.text(text);
}
}
}

Expand All @@ -149,57 +174,57 @@ export namespace mg {
return new MgDiv();
}

export function h1(text: string): MgHeading {
export function h1(text?: string): MgHeading {
return new MgHeading(1, text);
}

export function h2(text: string): MgHeading {
export function h2(text?: string): MgHeading {
return new MgHeading(2, text);
}

export function h3(text: string): MgHeading {
export function h3(text?: string): MgHeading {
return new MgHeading(3, text);
}

export function h4(text: string): MgHeading {
export function h4(text?: string): MgHeading {
return new MgHeading(4, text);
}

export function h5(text: string): MgHeading {
export function h5(text?: string): MgHeading {
return new MgHeading(5, text);
}

export function h6(text: string): MgHeading {
export function h6(text?: string): MgHeading {
return new MgHeading(6, text);
}

export function p(text: string = ''): MgParagraph {
export function p(text?: string): MgParagraph {
return new MgParagraph(text);
}

export function span(text: string = ''): MgSpan {
export function span(text?: string): MgSpan {
return new MgSpan(text);
}

export function code(text: string = ''): MgCode {
export function code(text?: string): MgCode {
return new MgCode(text);
}

export function a(
url: string,
text: string
text?: string
): MgAnchor {
return new MgAnchor(url, text);
}

export function router_a(
url: string,
text: string = ''
text?: string
): MgAnchor {
return new MgRouterAnchor(url, text);
}

export function button(text: string): MgButton {
export function button(text?: string): MgButton {
return new MgButton(text);
}

Expand Down

0 comments on commit 1a1ac6e

Please sign in to comment.