Source

pager/AdmPager.js

  1. "use strict"
  2. /**
  3. * ----------------------------------------------------------------------------------------------
  4. * Imports
  5. * ----------------------------------------------------------------------------------------------
  6. */
  7. import AdmEventBus from '../event/AdmEventBus';
  8. /**
  9. * ----------------------------------------------------------------------------------------------
  10. * Class Page Handling
  11. * ----------------------------------------------------------------------------------------------
  12. */
  13. class AdmPager extends AdmEventBus{
  14. /**
  15. * Create a basic adamantium app.
  16. * @param {Object} config - Configuration object of adamantium app
  17. */
  18. constructor(config = {}){
  19. super();
  20. this.appendTo = config.appendTo;
  21. this.loader = config.loader;
  22. this.defaultLayout = config.defaultLayout;
  23. this.initComplete = config.initComplete || null;
  24. this.currentLayout = null;
  25. this.currentPage = null;
  26. this._currentPageModule = null;
  27. this._currentLayoutModule = null;
  28. }
  29. /**
  30. * Load page with external loader.
  31. * @access private
  32. * @param {Object} data - page data
  33. */
  34. async loadPage(data){
  35. const Module = await this.loader(data);
  36. if(Module){
  37. this.setPage(Module.Page, data);
  38. }
  39. }
  40. /**
  41. * Set loaded page.
  42. * @access public
  43. * @param {Class} Page - Class of page
  44. * @param {Object} pageData - page route data
  45. */
  46. async setPage(Page, pageData){
  47. if(this._currentPageModule === Page){
  48. if(this.currentPage.load){
  49. await this.currentPage.load(pageData);
  50. }
  51. if(this.currentPage.show){
  52. await this.currentPage.show();
  53. }
  54. }
  55. else{
  56. if(this.currentPage){
  57. await this.removePage();
  58. }
  59. if( pageData?.config?.layout ){
  60. if( pageData.config.layout != this._currentLayoutModule ){
  61. await this.setLayout(pageData.config.layout);
  62. }
  63. }
  64. else if( this.defaultLayout && this.defaultLayout != this._currentLayoutModule ){
  65. await this.setLayout(this.defaultLayout);
  66. }
  67. await this.addPage(Page, pageData);
  68. if(this.currentPage.load){
  69. await this.currentPage.load(pageData);
  70. }
  71. if(this.currentPage.show){
  72. await this.currentPage.show();
  73. }
  74. }
  75. this.dispatchEvent("pageReady");
  76. }
  77. async addPage(Page, pageData){
  78. return new Promise(async (resolve) => {
  79. this._currentPageModule = Page;
  80. this.currentPage = new Page(pageData);
  81. if(this.currentPage.preInit){
  82. await this.currentPage.preInit();
  83. }
  84. document.querySelector(this.appendTo).appendChild(this.currentPage);
  85. if(this.currentPage.init){
  86. this.currentPage.init(resolve);
  87. }else{
  88. this.currentPage.init = resolve;
  89. }
  90. })
  91. }
  92. async removePage(){
  93. if(this.currentPage.unload){
  94. await this.currentPage.unload();
  95. }
  96. if(this.currentPage.hide){
  97. await this.currentPage.hide();
  98. }
  99. this.currentPage.remove();
  100. }
  101. /**
  102. * Set a page layout.
  103. * @access public
  104. * @param {Class} Layout - Class of a layout
  105. * @return {Promise} A resolved promise
  106. */
  107. setLayout(Layout){
  108. document.body.innerHTML = "";
  109. return new Promise((resolve) => {
  110. this._currentLayoutModule = Layout;
  111. this.currentLayout = new Layout();
  112. document.body.appendChild(this.currentLayout);
  113. if(this.currentLayout.init){
  114. this.currentLayout.init(resolve);
  115. }else{
  116. this.currentLayout.init = resolve;
  117. }
  118. })
  119. }
  120. }
  121. export { AdmPager }