controllers/dashboard/frame_loader_controller.js

  1. import { Controller } from "@hotwired/stimulus"
  2. import { subscription } from "~/javascripts/store/mixins/subscription"
  3. /**
  4. * @class Dashboard.FrameLoaderController
  5. * @classdesc Requests the correct Turbo frame to load in the required data visualisation
  6. * @extends Controller
  7. **/
  8. export default class extends Controller {
  9. static targets = [ "frame", "loading" ]
  10. static values = {
  11. defaultFrame: String,
  12. path: String,
  13. storeId: String
  14. }
  15. /**
  16. * Subscribe to the store.
  17. *
  18. * @instance
  19. * @memberof Dashboard.FrameLoaderController
  20. **/
  21. connect() {
  22. this.clonedLoader = this.loadingTarget.cloneNode(true)
  23. subscription(this)
  24. this.subscribe()
  25. this.reconnect()
  26. }
  27. /**
  28. * Handles a repeated Turbo visit to the dashboard page.
  29. *
  30. * @instance
  31. * @memberof Dashboard.FrameLoaderController
  32. **/
  33. reconnect() {
  34. if (this.store("frameSelected")) {
  35. this.editStore("frameSelected", this.defaultFrameValue)
  36. }
  37. }
  38. /**
  39. * Generate a Turbo frame to load the correct data visualisation and append the loader inside
  40. *
  41. * @instance
  42. * @memberof Dashboard.FrameLoaderController
  43. **/
  44. storeUpdated(prop, storeId) {
  45. if (prop === "frameSelected") {
  46. let turboFrame = document.createElement("turbo-frame")
  47. turboFrame.id = this.store("frameSelected")
  48. turboFrame.src = this.pathValue
  49. turboFrame.appendChild(this.clonedLoader)
  50. this.frameTarget.innerHTML = ""
  51. this.frameTarget.appendChild(turboFrame)
  52. }
  53. }
  54. /**
  55. * Unsubscribe from the store
  56. *
  57. * @instance
  58. * @memberof Dashboard.FrameLoaderController
  59. **/
  60. disconnect() {
  61. this.unsubscribe()
  62. }
  63. }