AUIController.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { PointF2D } from "../../Common/DataObjects";
  2. import * as Handlebars from "handlebars";
  3. export abstract class AUIController<T> {
  4. protected parentElement: HTMLElement;
  5. protected insertLocation: InsertPosition;
  6. protected isTouchDevice: boolean;
  7. //TODO: This is a better pattern than annotations. Need to use this when merging
  8. protected eventListeners: T[] = [];
  9. protected abstract initialize(): void;
  10. public abstract show(location?: PointF2D): void;
  11. public abstract hideAndClear(): void;
  12. constructor(parentElement: HTMLElement = document.body, isTouchDevice: boolean = false, where: InsertPosition = "beforeend") {
  13. this.parentElement = parentElement;
  14. this.insertLocation = where;
  15. this.isTouchDevice = isTouchDevice;
  16. this.initialize();
  17. }
  18. public addListener(listener: T): void {
  19. this.eventListeners.push(listener);
  20. }
  21. protected generateHtmlTemplate(rawHtml: string, data: Object = {}): string {
  22. const template: HandlebarsTemplateDelegate<any> = Handlebars.compile(rawHtml);
  23. const html: string = template(data);
  24. return html;
  25. }
  26. //Utility for subclasses if html template is pretty standard
  27. protected generateAndInsertHtmlTemplate(rawHtml: string, data: Object = {}, where: InsertPosition = this.insertLocation,
  28. parent: HTMLElement = this.parentElement): void {
  29. const processedHtml: string = this.generateHtmlTemplate(rawHtml, data);
  30. parent.insertAdjacentHTML(where, processedHtml);
  31. }
  32. protected get downEventName(): string {
  33. return this.isTouchDevice ? "ontouchstart" : "onmousedown";
  34. }
  35. protected get upEventName(): string {
  36. return this.isTouchDevice ? "ontouchend" : "onmouseup";
  37. }
  38. protected get moveEventName(): string {
  39. return this.isTouchDevice ? "ontouchmove" : "onmousemove";
  40. }
  41. }