@@ -112,18 +112,27 @@ export class ProcPageManager {
112112
113113 // Check for forward-back history navigation
114114 window . addEventListener ( "popstate" , ( e ) => {
115- let newPage = this . getPageURL ( ) ;
116- if ( newPage != this . curPageName ) {
115+ let urlData = this . getPageURL ( ) ;
116+ let newPage = urlData . page ;
117+
118+ if ( ! this . pageListing . hasOwnProperty ( newPage ) ) {
119+ console . warn ( "Page not found in pageListing: " + newPage ) ;
120+ newPage = this . defaultPage ;
121+ }
117122
123+ if ( newPage != this . curPageName ) {
118124 // Swap page content
119125 this . changePage ( newPage ) ;
120126
121127 // Run page theming and callback functions
122- if ( this . pageListing . hasOwnProperty ( newPage ) ) {
123- this . curPageName = newPage ;
124- this . curPage = this . pageListing [ newPage ] [ "obj" ] ;
125- this . postLoad ( ) ;
126- }
128+ this . curPageName = newPage ;
129+ this . curPage = this . pageListing [ newPage ] [ "obj" ] ;
130+ this . postLoad ( ) ;
131+ }
132+
133+ if ( urlData . section != null ) {
134+ let pageData = this . pageListing [ newPage ] [ "pageData" ] ;
135+ pageData . activateSection ( urlData . section ) ;
127136 }
128137 } ) ;
129138
@@ -177,7 +186,10 @@ export class ProcPageManager {
177186 } ) ;
178187
179188 // Rectify the URL state and set the current page
180- let pageURL = this . getPageURL ( ) ;
189+ let pageURLData = this . getPageURL ( ) ;
190+ let pageURL = pageURLData . page ;
191+ let pageSection = pageURLData . section ;
192+
181193
182194 // Find the dom user clickable actions
183195 this . findDomUserEvents ( ) ;
@@ -213,6 +225,7 @@ export class ProcPageManager {
213225 this . updateDocumentMetaData ( pageURL ) ;
214226 }
215227
228+
216229 this . curPageName = pageURL ;
217230
218231
@@ -302,6 +315,14 @@ export class ProcPageManager {
302315 //this.runHidePages();
303316 this . activateNavButton ( this . curPageName ) ;
304317
318+ // Check the current page section
319+ if ( pageSection != null && pageSection != "" ) {
320+ let pageData = this . pageListing [ pageURL ] [ "pageData" ] ;
321+ if ( pageData ) {
322+ pageData . activateSection ( pageSection ) ;
323+ }
324+ }
325+
305326 }
306327
307328 // -- -- --
@@ -371,7 +392,7 @@ export class ProcPageManager {
371392 // -- -- --
372393
373394/**
374- * @method getPageURL
395+ * @method triggerDomEvent
375396 * @returns {string } - The current page URL
376397 * @description Retrieves the current page URL from the browser's address bar
377398 */
@@ -495,7 +516,10 @@ export class ProcPageManager {
495516
496517 let pageObj = pageData [ pageKey ] . buildPage ( ) ;
497518 this . pageListing [ pageKey ] [ "obj" ] = pageObj ;
498- console . log ( pageObj )
519+
520+ // Connect when sections change, to update the page manager
521+ // This is used for history state pushes to a `folder/page` URL
522+ pageData [ pageKey ] . subscribe ( this . sectionChangeCallback . bind ( this ) ) ;
499523 } ) ;
500524
501525 }
@@ -640,7 +664,16 @@ export class ProcPageManager {
640664 }
641665
642666 // Update URL page display & history state
643- this . shiftHistoryState ( pageName ) ;
667+ let pageData = this . pageListing [ pageName ] [ 'pageData' ] ;
668+ let prevSection = pageData [ 'prevSection' ] ;
669+ let sectionData = pageData [ 'sectionData' ] [ prevSection ] ;
670+ if ( sectionData && sectionData . hasOwnProperty ( "htmlName" ) ) {
671+ let htmlName = sectionData [ "htmlName" ] ;
672+ let formattedURL = this . formatURL ( pageName , htmlName ) ;
673+ this . shiftHistoryState ( formattedURL ) ;
674+ } else {
675+ this . shiftHistoryState ( pageName ) ;
676+ }
644677
645678 // Update Meta Data
646679 this . updateDocumentMetaData ( pageName ) ;
@@ -710,8 +743,9 @@ export class ProcPageManager {
710743 shiftHistoryState ( pageName ) {
711744 let urlDisplay = pageName ;
712745
713- if ( urlDisplay . includes ( ".htm" ) ) {
714- urlDisplay = urlDisplay . split ( "." ) [ 0 ] ;
746+ let urlCheck = urlDisplay . split ( "/" ) [ 0 ] ;
747+ if ( urlCheck . includes ( ".htm" ) ) {
748+ urlCheck = urlCheck . split ( "." ) [ 0 ] ;
715749 }
716750
717751 // Check for specific capitalization of file url names
@@ -728,6 +762,7 @@ export class ProcPageManager {
728762 urlDisplay += ".htm" ;
729763 }
730764
765+ /*
731766 let urlFolderPath = window.location.pathname;
732767 if( urlFolderPath.includes(".htm") ){
733768 urlFolderPath = urlFolderPath.split("/");
@@ -737,12 +772,51 @@ export class ProcPageManager {
737772 urlFolderPath = urlFolderPath+"/";
738773 }
739774 let url = window.location.origin + urlFolderPath + urlDisplay;
775+ */
776+ if ( ! urlDisplay . startsWith ( "/" ) ) {
777+ urlDisplay = "/" + urlDisplay ;
778+ }
779+ let url = window . location . origin + urlDisplay ;
740780 window . history . pushState ( { path :url } , '' , url ) ;
741781
742782 }
743783
784+ /**
785+ * Recieve page section change callback from the page itself
786+ *
787+ * This is used to push state to the browser history when the page has a section change
788+ * @method sectionChangeCallback
789+ * @param {Object } sectionData - Data object containing section information
790+ */
744791 sectionChangeCallback ( sectionData ) {
745- console . log ( sectionData ) ;
792+ let pageName = null ;
793+ let sectionName = null ;
794+ if ( ! sectionData || ! sectionData . hasOwnProperty ( "page" ) ) {
795+ pageName = this . curPageName ;
796+ } else {
797+ pageName = sectionData [ 'dir' ] ;
798+ if ( sectionData . hasOwnProperty ( "page" ) ) {
799+ sectionName = sectionData [ 'page' ] ;
800+ }
801+ }
802+
803+ let formattedURL = this . formatURL ( pageName , sectionName ) ;
804+ this . shiftHistoryState ( formattedURL ) ;
805+ }
806+
807+ /**
808+ * Format URL from page + section callback
809+ */
810+ formatURL ( pageName , sectionName ) {
811+ let urlDisplay = pageName ;
812+
813+ if ( sectionName != null ) {
814+ urlDisplay += "/" + sectionName ;
815+ }
816+ if ( ! urlDisplay . includes ( ".htm" ) ) {
817+ urlDisplay += ".htm" ;
818+ }
819+ return urlDisplay ;
746820 }
747821
748822
@@ -772,8 +846,15 @@ export class ProcPageManager {
772846 this . checkForRedirect ( ) ;
773847
774848 // Pull base name from the url
775- let url = window . location . href ;
849+ let url = window . location . pathname ;
850+
851+ if ( url == "/index.htm" || url == "/" ) {
852+ this . shiftHistoryState ( this . defaultPage ) ;
853+ return { 'page' :this . defaultPage , 'section' :null } ;
854+ }
855+
776856 let urlSplit = url . split ( "/" ) ;
857+
777858 let urlLast = urlSplit [ urlSplit . length - 1 ] ;
778859 if ( urlLast != "" ) {
779860 let urlSplitDot = urlLast . split ( "." ) ;
@@ -782,11 +863,32 @@ export class ProcPageManager {
782863 ret = urlPage ;
783864 }
784865 }
785- if ( ret . toLowerCase ( ) == "index" ) {
786- ret = this . defaultPage ;
787- this . shiftHistoryState ( ret ) ;
866+
867+ urlSplit = urlSplit . filter ( ( path ) => { return path != "" ; } ) ;
868+
869+
870+ let urlFolderPath = null ;
871+ let urlPagePath = urlSplit . pop ( ) ;
872+ if ( urlSplit . length > 0 ) {
873+ urlFolderPath = urlSplit . join ( "/" ) ;
788874 }
789- return ret ;
875+
876+ let pageCheck = ( urlFolderPath || urlPagePath ) . toLowerCase ( ) ;
877+ if ( pageCheck . includes ( ".htm" ) ) {
878+ pageCheck = pageCheck . split ( "." ) [ 0 ] ;
879+ }
880+
881+ if ( ( urlFolderPath == null || urlFolderPath == "" ) && urlPagePath != "" ) {
882+ urlFolderPath = urlPagePath ;
883+ urlPagePath = "" ;
884+ }
885+ urlFolderPath = urlFolderPath . replace ( / \. h t m / g, "" ) ;
886+ if ( urlFolderPath == "" ) {
887+ urlFolderPath = this . defaultPage ;
888+ urlPagePath = "" ;
889+ }
890+
891+ return { 'page' :urlFolderPath , 'section' :urlPagePath } ;
790892 }
791893
792894
0 commit comments