forked from matteocargnelutti/this-is-not-a-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppRoot.js
More file actions
138 lines (116 loc) · 3.69 KB
/
AppRoot.js
File metadata and controls
138 lines (116 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* @class AppRoot
* @description App's main entry point and principal handler. Handles navigation and app-wide state management.
* @extends HTMLElement
*/
class AppRoot extends HTMLElement {
constructor() {
super();
//
// App-wide state definition.
//
this.state = new StateManager(this, 'AppRoot', {
navigation: {
defaultScreen: 'screen-index',
currentScreen: 'screen-index',
previousScreen: null
}
});
//
// Settings
//
this.enforceSingleton = true;
// ☝️ If true, will make sure that there is only 1 instance of `<app-root>` in this document
//
// Local binding enforcement
//
this.hashNavigationHandler = this.hashNavigationHandler.bind(this);
}
/**
* Upon injection of <app-root> in the DOM:
* - Render HTML
* - Run navigation handler
*/
connectedCallback() {
// Enforce singleton pattern if needed
if( this.enforceSingleton === true ) {
let appRoots = document.querySelectorAll('app-root');
for( let i = 1; i < appRoots.length; i++ ) {
appRoots[i].remove();
}
}
// Render on DOM injection
this.renderInnerHTML();
// Run navigation handler once
this.hashNavigationHandler();
// Bind hash change to navigation handler
window.addEventListener('hashchange', this.hashNavigationHandler);
}
disconnectedCallback() {
// Enforce removal of bindings to window and document.
window.removeEventListener('hashchange', this.hashNavigationHandler);
}
/**
* Generates and replace inner HTML content
*/
renderInnerHTML() {
this.innerHTML = /*html*/`
<app-header></app-header>
<main></main>
<app-footer></app-footer>
`;
}
/**
* Basic hash URI-based navigation management.
* - Example of valid hash for the `screen-inner` element: `#!/inner`
* - Custom logic needs to be added for parameters handling.
*
* About this URI format: https://www.w3.org/blog/2011/05/hash-uris/
*/
hashNavigationHandler() {
let hash = window.location.hash;
// Extract screen name from URI:
// - #!/inner = inner = screen-inner
let screenName = hash.match(/\#\!\/([A-Za-z0-9_\-]+)/);
// If a screen name is found in the hash, prepend `screen-` to it.
if( screenName && screenName.length > 1 ) {
screenName = `screen-${screenName[1]}`;
}
// If nothing found, redirect to default screen
else {
screenName = this.state.data.navigation.defaultScreen;
}
// Try to load said screen
this.changeScreen(screenName);
}
/**
* Tries to replace the content of `app-root > main` by the content of a screen component.
* - Will update this.state.data.navigation on the fly as needed.
*
* @param {String} newScreenName
*/
changeScreen(newScreenName) {
//
// Check that the element contains `screen-` and exists.
//
if( !newScreenName.includes('screen-') || !customElements.get(newScreenName) ) {
throw new Error(`<${newScreenName}> is not a valid screen name or does not exist.`);
}
//
// If exists: replace content of app-root > main with this screen
//
let newScreen = new (customElements.get(newScreenName));
let main = this.querySelector('main');
main.innerHTML = '';
main.appendChild(newScreen);
//
// Update navigation state
//
this.state.data.navigation.previousScreen = this.state.data.navigation.currentScreen;
this.state.data.navigation.currentScreen = newScreenName;
}
}
// Delay declaration of <app-root> until DOM content is ready to prevent loading race conditions.
window.addEventListener('DOMContentLoaded', () => {
customElements.define('app-root', AppRoot);
});