applet.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { dom_manager, type_manager } from "./functions.js";
  2. class applet_animations {
  3. static display(state, target) {
  4. if (state) {
  5. target.style.display = "block";
  6. } else {
  7. target.style.display = "none";
  8. }
  9. }
  10. static hide_left(state, target) {
  11. if (state) {
  12. target.style.left = "0";
  13. } else {
  14. target.style.left = -target.offsetWidth + "px";
  15. }
  16. }
  17. static hide_right(state, target) {
  18. if (state) {
  19. target.style.right = "0";
  20. } else {
  21. target.style.right = -target.offsetWidth + "px";
  22. }
  23. }
  24. }
  25. class applet_builder {
  26. #minimalise;
  27. #maximalise;
  28. #target;
  29. #animation;
  30. constructor() {
  31. this.#minimalise = undefined;
  32. this.#maximalise = undefined;
  33. this.#target = undefined;
  34. this.#animation = applet_animations.display;
  35. }
  36. set minimalise(target) {
  37. if (!dom_manager.is_element(target)) {
  38. throw "Minimalise button must be HTML element.";
  39. }
  40. this.#minimalise = target;
  41. }
  42. set maximalise(target) {
  43. if (!dom_manager.is_element(target)) {
  44. throw "Maximalise button must be HTML element.";
  45. }
  46. this.#maximalise = target;
  47. }
  48. set target(target) {
  49. if (!dom_manager.is_element(target)) {
  50. throw "Target must be HTML element.";
  51. }
  52. this.#target = target;
  53. }
  54. set animation(target) {
  55. if (!type_manager.is_function(target)) {
  56. throw "Animation must be an function.";
  57. }
  58. this.#animation = target;
  59. }
  60. get is_valid() {
  61. if (this.#minimalise === undefined) {
  62. return false;
  63. }
  64. if (this.#maximalise === undefined) {
  65. return false;
  66. }
  67. if (this.#target === undefined) {
  68. return false;
  69. }
  70. if (this.#animation === undefined) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. get #is_swapable() {
  76. return this.#minimalise === this.#maximalise;
  77. }
  78. #build_swapable() {
  79. return new swapable_applet(
  80. this.#target,
  81. this.#minimalise,
  82. this.#animation
  83. );
  84. }
  85. build() {
  86. if (!this.is_valid) {
  87. throw "Builder is not valid yes.";
  88. }
  89. if (this.#is_swapable) {
  90. return this.#build_swapable();
  91. }
  92. }
  93. }
  94. class applet {
  95. _target;
  96. _animation;
  97. _state;
  98. get _has_animation() {
  99. return this._animation !== undefined;
  100. }
  101. _run_animation() {
  102. this._animation(this._state, this._target);
  103. }
  104. show() {
  105. this._state = true;
  106. this._run_animation();
  107. }
  108. hide() {
  109. this._state = false;
  110. this._run_animation();
  111. }
  112. }
  113. class swapable_applet extends applet {
  114. #swaper;
  115. constructor(target, swaper, animation) {
  116. super();
  117. if (!dom_manager.is_element(target)) {
  118. throw "Target must be HTML element.";
  119. }
  120. if (!dom_manager.is_element(swaper)) {
  121. throw "Swaper must be HTML element.";
  122. }
  123. if (animation !== undefined && !type_manager.is_function(animation)) {
  124. throw "Animation must be undefined (disabled), or function.";
  125. }
  126. this._target = target;
  127. this.#swaper = swaper;
  128. this._animation = animation;
  129. this._state = false;
  130. this.hide();
  131. this.#listen();
  132. }
  133. #listen() {
  134. this.#swaper.addEventListener("click", () => { this.swap(); });
  135. }
  136. swap() {
  137. if (this._state) {
  138. this.hide();
  139. } else {
  140. this.show();
  141. }
  142. }
  143. }
  144. export { applet_builder, applet_animations };