Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Element and Composite components #459

Merged
merged 11 commits into from
Dec 5, 2023
Merged

feat: Add Element and Composite components #459

merged 11 commits into from
Dec 5, 2023

Conversation

hyyan
Copy link
Member

@hyyan hyyan commented Nov 30, 2023

closes #399 , #427, #436

The PR removes the outdated WebComponent class and introduces the ElementComposite class, designed for creating new components based on a native, custom-element, or a web component. ElementComposite extends the newly introduced Composite class. This class acts as a blueprint for composite components, high-level constructs aimed at encapsulating and managing specific Component instances. It mandates that any subclass extending it must define the type of Component it will manage, thereby ensuring that a subclass of Composite is inherently connected to its corresponding Component.

ElementComposite is linked to the Element class, which represents an HTML Element. It offers methods for setting and retrieving HTML content, manipulating properties, adding event listeners to the element, and executing JavaScript through a fluent async API.

The Engine uses these new APIs in: AppLayout, Dialog , Drawer, Youtube and GoogleChart components

Composite Sample

import org.dwcj.App;
import org.dwcj.component.Composite;
import org.dwcj.component.button.Button;
import org.dwcj.component.field.TextField;
import org.dwcj.component.layout.flexlayout.FlexLayout;
import org.dwcj.component.window.Frame;
import org.dwcj.component.window.Panel;
import org.dwcj.exceptions.DwcjException;

public class CompositeDemo extends App {

  @Override
  public void run() throws DwcjException {
    Frame window = new Frame();
    window.add(new TodoInput());
  }

  public static class TodoInput extends Composite<Panel> {

    @Override
    protected void onDidCreate(Panel container) {
      FlexLayout layout = FlexLayout.create().horizontal().build();
      Button add = new Button("Add Todo");
      TextField text = new TextField("What is Todo?");
      container.add(text, add);

      add.onClick(e -> {
        msgbox(text.getValue());
      });

      container.add(layout);
    }
  }
}

ElementComposite Sample

import java.awt.Color;
import org.dwcj.annotation.Attribute;
import org.dwcj.annotation.JavaScript;
import org.dwcj.component.element.ElementComposite;
import org.dwcj.component.element.PropertyDescriptor;
import org.dwcj.component.element.annotation.NodeName;

/**
 * QRCode Generator using Shoelace QRCode component.
 *
 * @author Hyyan Abo Fakher
 */
@JavaScript(
    value = "https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/dist/shoelace.js",
    attributes = {@Attribute(name = "type", value = "module")})
@NodeName("sl-qr-code")
public final class QRCode extends ElementComposite {

  private final PropertyDescriptor<String> VALUE = PropertyDescriptor.property("value", "");
  private final PropertyDescriptor<Integer> SIZE = PropertyDescriptor.property("size", 200);
  private final PropertyDescriptor<String> COLOR = PropertyDescriptor.property("fill", "#000000");

  /**
   * Create a new QRCode.
   */
  public QRCode() {
    super();
  }

  /**
   * Create a new QRCode with the given value.
   *
   * @param value the value of the QRCode
   */
  public QRCode(String value) {
    super();
    this.setValue(value);
  }

  /**
   * Create a new QRCode with the given value and size.
   *
   * @param value the value of the QRCode
   * @param size the size of the QRCode
   */
  public QRCode(String value, int size) {
    super();
    this.setValue(value);
    this.setSize(size);
  }

  /**
   * Get the value of the QRCode.
   *
   * @return the value of the QRCode
   */
  public String getValue() {
    return get(VALUE);
  }

  /**
   * Set the value of the QRCode.
   *
   * @param value the value of the QRCode
   */
  public QRCode setValue(String value) {
    set(VALUE, value);
    return this;
  }

  /**
   * Get the size of the QRCode.
   *
   * @return the size of the QRCode
   */
  public int getSize() {
    return get(SIZE);
  }

  /**
   * Set the size of the QRCode.
   *
   * @param size the size of the QRCode
   */
  public QRCode setSize(int size) {
    set(SIZE, size);
    return this;
  }

  /**
   * Get the color of the QRCode.
   *
   * @return the color of the QRCode
   */
  public Color getColor() {
    String hex = get(COLOR);
    return Color.decode(hex);
  }

  /**
   * Set the color of the QRCode.
   *
   * @param color the color of the QRCode
   */
  public QRCode setColor(Color color) {
    String hex = String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
    set(COLOR, hex);
    return this;
  }
}

Element Sample

import org.dwcj.App;
import org.dwcj.component.element.Element;
import org.dwcj.component.window.Frame;
import org.dwcj.exceptions.DwcjException;

public class ElementDemo extends App {

  @Override
  public void run() throws DwcjException {
    Frame frame = new Frame();

    Element input = new Element("input", "Enter any text");
    Element button = new Element("button", "Select All");
    button.addEventListener("click", e -> {
      input.callJsFunctionAsync("select");
    });

    frame.add(input, button);
  }
}

@hyyan hyyan requested a review from StephanWald November 30, 2023 14:50
@hyyan hyyan added this to the 23.06 milestone Nov 30, 2023
@hyyan hyyan requested review from mhawkinsbasis and MatthewHawkins and removed request for StephanWald and mhawkinsbasis December 5, 2023 09:37
Copy link

sonarcloud bot commented Dec 5, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 10 Code Smells

No Coverage information No Coverage information
0.4% 0.4% Duplication

@hyyan hyyan requested review from mhawkinsbasis and removed request for mhawkinsbasis December 5, 2023 14:29
@hyyan hyyan merged commit 3d496f0 into main Dec 5, 2023
9 checks passed
@hyyan hyyan deleted the element branch December 5, 2023 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] WebComponent.addRawSlot fails to escape the slot
3 participants