Skip to content

Instantly share code, notes, and snippets.

@krisimmig
Last active January 28, 2019 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisimmig/c27bb955bf951f8751f5d65a194c24ed to your computer and use it in GitHub Desktop.
Save krisimmig/c27bb955bf951f8751f5d65a194c24ed to your computer and use it in GitHub Desktop.
// 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