Last active
January 28, 2019 14:37
-
-
Save krisimmig/c27bb955bf951f8751f5d65a194c24ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remote modules | |
import Emitter from 'tiny-emitter'; | |
import Accordion from './_patterns/components/accordion/accordion'; | |
import Header from './_patterns/template-components/header/header'; | |
import MainNavigation from './_patterns/atoms/main-navigation/main-navigation'; | |
import References from './_patterns/components/components-references/components-references'; | |
import SearchBox from './_patterns/atoms/search-box/search-box'; | |
const Factory = { | |
'.js-Header': Header, | |
'.js-References': References, | |
'.js-SearchBox': SearchBox, | |
'.js-MainNavigation': MainNavigation, | |
'.js-Accordion': Accordion, | |
}; | |
class App { | |
constructor() { | |
if ( | |
!('querySelector' in document) || | |
!('addEventListener' in window) || | |
!document.documentElement.classList | |
) { | |
return; | |
} | |
this.bind(document); | |
} | |
bind(context) { | |
let el; | |
if (typeof context === 'string') { | |
el = document.querySelector(context); | |
} else if (typeof context[0] !== 'undefined') { | |
el = context[0]; | |
} else { | |
el = context; | |
} | |
Object.keys(Factory).forEach((key) => { | |
const mangledKey = key.toLowerCase().replace('.js-', ''); | |
if (!this[mangledKey]) { | |
this[mangledKey] = []; | |
} | |
Array.prototype.push.apply( | |
this[mangledKey], | |
[].slice | |
.call(el.querySelectorAll(key)) | |
.map(element => new Factory[key](element))); | |
}); | |
} | |
} | |
document.addEventListener('DOMContentLoaded', () => { | |
new App(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment