applet.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. static hide_opacity_generator(time) {
  25. return (state, target) => {
  26. if (state) {
  27. target.style.display = "";
  28. setTimeout(() => {
  29. target.style.opacity = "1";
  30. }, time);
  31. } else {
  32. target.style.opacity = "0";
  33. setTimeout(() => {
  34. target.style.display = "none";
  35. }, time);
  36. }
  37. };
  38. }
  39. static hide_opacity(state, target) {
  40. if (state) {
  41. target.style.display = "";
  42. setTimeout(() => { target.style.opacity = "1"; }, 1);
  43. } else {
  44. target.style.opacity = "0";
  45. setTimeout(() => { target.style.display = "none"; }, 500);
  46. }
  47. }
  48. }
  49. class applet_builder {
  50. #minimalise;
  51. #maximalise;
  52. #target;
  53. #animation;
  54. constructor() {
  55. this.#minimalise = undefined;
  56. this.#maximalise = undefined;
  57. this.#target = undefined;
  58. this.#animation = applet_animations.display;
  59. }
  60. set minimalise(target) {
  61. if (!dom_manager.is_element(target)) {
  62. throw "Minimalise button must be HTML element.";
  63. }
  64. this.#minimalise = target;
  65. }
  66. set maximalise(target) {
  67. if (!dom_manager.is_element(target)) {
  68. throw "Maximalise button must be HTML element.";
  69. }
  70. this.#maximalise = target;
  71. }
  72. set target(target) {
  73. if (!dom_manager.is_element(target)) {
  74. throw "Target must be HTML element.";
  75. }
  76. this.#target = target;
  77. }
  78. set animation(target) {
  79. if (!type_manager.is_function(target)) {
  80. throw "Animation must be an function.";
  81. }
  82. this.#animation = target;
  83. }
  84. get is_valid() {
  85. if (this.#minimalise === undefined) {
  86. return false;
  87. }
  88. if (this.#maximalise === undefined) {
  89. return false;
  90. }
  91. if (this.#target === undefined) {
  92. return false;
  93. }
  94. if (this.#animation === undefined) {
  95. return false;
  96. }
  97. return true;
  98. }
  99. get #is_swapable() {
  100. return this.#minimalise === this.#maximalise;
  101. }
  102. #build_swapable() {
  103. return new swapable_applet(
  104. this.#target,
  105. this.#minimalise,
  106. this.#animation
  107. );
  108. }
  109. #build_dualbutton() {
  110. return new dualbutton_applet(
  111. this.#target,
  112. this.#maximalise,
  113. this.#minimalise,
  114. this.#animation
  115. );
  116. }
  117. build() {
  118. if (!this.is_valid) {
  119. throw "Builder is not valid yes.";
  120. }
  121. if (this.#is_swapable) {
  122. return this.#build_swapable();
  123. }
  124. return this.#build_dualbutton();
  125. }
  126. }
  127. class applet {
  128. _target;
  129. _animation;
  130. _state;
  131. get _has_animation() {
  132. return this._animation !== undefined;
  133. }
  134. _run_animation() {
  135. this._animation(this._state, this._target);
  136. }
  137. show() {
  138. this._state = true;
  139. this._run_animation();
  140. }
  141. hide() {
  142. this._state = false;
  143. this._run_animation();
  144. }
  145. }
  146. class swapable_applet extends applet {
  147. #swaper;
  148. constructor(target, swaper, animation) {
  149. super();
  150. if (!dom_manager.is_element(target)) {
  151. throw "Target must be HTML element.";
  152. }
  153. if (!dom_manager.is_element(swaper)) {
  154. throw "Swaper must be HTML element.";
  155. }
  156. if (animation !== undefined && !type_manager.is_function(animation)) {
  157. throw "Animation must be undefined (disabled), or function.";
  158. }
  159. this._target = target;
  160. this.#swaper = swaper;
  161. this._animation = animation;
  162. this._state = false;
  163. this.hide();
  164. this.#listen();
  165. }
  166. #listen() {
  167. this.#swaper.addEventListener("click", () => { this.swap(); });
  168. }
  169. swap() {
  170. if (this._state) {
  171. this.hide();
  172. } else {
  173. this.show();
  174. }
  175. }
  176. }
  177. class dualbutton_applet extends applet {
  178. #open;
  179. #close;
  180. constructor(target, open, close, animation) {
  181. super();
  182. if (!dom_manager.is_element(target)) {
  183. throw "Target must be an HTML element.";
  184. }
  185. if (!dom_manager.is_element(close)) {
  186. throw "Closer must be an HTML element.";
  187. }
  188. if (!dom_manager.is_element(open)) {
  189. throw "Opener must be an HTML element.";
  190. }
  191. if (!type_manager.is_function(animation)) {
  192. throw "Animation must be an function.";
  193. }
  194. this.#open = open;
  195. this.#close = close;
  196. this._animation = animation;
  197. this._target = target;
  198. this._state = false;
  199. this.hide();
  200. this.#listen();
  201. }
  202. #listen() {
  203. this.#open.addEventListener("click", () => {
  204. this.show();
  205. });
  206. this.#close.addEventListener("click", () => {
  207. this.hide();
  208. });
  209. }
  210. }
  211. export { applet_builder, applet_animations };