2
0

SmartButton.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @class SmartButton
  3. * @description SmartButton is a class that enables the creation of a movable button.
  4. */
  5. export class SmartButton {
  6. constructor(buttonId) {
  7. this.button = document.getElementById(buttonId);
  8. if (!this.button) throw new Error("SmartButton not found");
  9. this.isVisible = false;
  10. }
  11. /**
  12. * Move the smartButton to the target position
  13. * @param {number} x - X Position
  14. * @param {number} y - Y position
  15. */
  16. moveTo(x, y) {
  17. Object.assign(this.button.style, {
  18. left: `${x}px`,
  19. top: `${y}px`,
  20. position: "absolute",
  21. });
  22. this.show();
  23. }
  24. show() {
  25. this.button.style.display = "block";
  26. this.isVisible = true;
  27. }
  28. hide() {
  29. this.button.style.display = "none";
  30. this.isVisible = false;
  31. }
  32. setPosition(x, y) {
  33. this.moveTo(x, y);
  34. }
  35. /**
  36. *
  37. * @returns {HTMLElement}
  38. */
  39. getElement() {
  40. return this.button;
  41. }
  42. onClick(callback) {
  43. this.button.addEventListener("click", () => {
  44. callback();
  45. });
  46. }
  47. }