diff --git a/app/appListeners.js b/app/appListeners.js index 81ba06d..176979e 100644 --- a/app/appListeners.js +++ b/app/appListeners.js @@ -1,9 +1,9 @@ export const appListeners = (app, state, emitter) => { - emitter.on('DOMContentLoaded', () => { - emitter.emit('DOMTitleChange', app.siteConfig.siteName); + emitter.on(state.events.DOMCONTENTLOADED, () => { + emitter.emit(state.events.DOMTITLECHANGE, app.siteConfig.siteName); // Emitter listeners - emitter.on('render', callback => { + emitter.on(state.events.RENDER, callback => { // This is a dirty hack to get the callback to call *after* re-rendering. if (callback && typeof callback === "function") { setTimeout(() => { @@ -12,17 +12,17 @@ export const appListeners = (app, state, emitter) => { } }); - emitter.on('set-language', newLanguage => { + emitter.on(state.events.SET_LANGUAGE, newLanguage => { app.setSettingsItem('lang', newLanguage); state.language = newLanguage; state.i18n.fetchLocaleUI().then(() => { - emitter.emit('render'); + emitter.emit(state.events.RENDER); }); }); state.i18n.fetchLocaleUI().then(() => { app.checkIfLoggedIn(state).then(isLoggedIn => { - emitter.emit('render'); // This should hopefully only run once after the DOM is loaded. It prevents routing issues where 'render' hasn't been defined yet + emitter.emit(state.events.RENDER); // This should hopefully only run once after the DOM is loaded. It prevents routing issues where 'render' hasn't been defined yet }); }) }); diff --git a/app/appState.js b/app/appState.js index 49f1b46..7d6eacd 100644 --- a/app/appState.js +++ b/app/appState.js @@ -1,6 +1,8 @@ import { I18n } from "./i18n"; export const appState = (app, state, emitter) => { + state.events.SET_LANGUAGE = 'set-language'; + state.language = app.getSettingsItem('lang') ? app.getSettingsItem('lang') : (navigator.language || navigator.userLanguage).split('-')[0]; state.viewStates = {}; state.isLoggedIn = false; diff --git a/app/views/about.js b/app/views/about.js index 6e09cb4..30df952 100644 --- a/app/views/about.js +++ b/app/views/about.js @@ -13,7 +13,7 @@ export const aboutView = (state, emit, i18n) => { community.innerHTML = i18n.pages.community; } if (promises.length > 0) { - Promise.all(promises).then(fulfilled => emit('render')); + Promise.all(promises).then(fulfilled => emit(state.events.RENDER)); } return [ content, diff --git a/app/views/login/controller.js b/app/views/login/controller.js index e64e5aa..b8bbe82 100644 --- a/app/views/login/controller.js +++ b/app/views/login/controller.js @@ -34,7 +34,7 @@ export class LoginController extends ViewController { this.state.fieldValues.loginEmail = ''; this.state.fieldValues.loginPassword = ''; - this.emit('render'); + this.emit(this.appState.events.RENDER); } clearCreateAccountForm () { @@ -44,7 +44,7 @@ export class LoginController extends ViewController { this.state.fieldValues.createPassword = ''; this.state.fieldValues.createConfirm = ''; - this.emit('render'); + this.emit(this.appState.events.RENDER); } validateLogin () { @@ -52,7 +52,7 @@ export class LoginController extends ViewController { this.state.loginError = ''; this.state.isChecking = true; - this.emit('render', () => { + this.emit(this.appState.events.RENDER, () => { const { loginEmail, loginPassword @@ -64,7 +64,7 @@ export class LoginController extends ViewController { ].includes('')) { this.state.loginError = __('login.login_required_field_blank'); this.state.isChecking = false; - this.emit('render'); + this.emit(this.appState.events.RENDER); return; } @@ -77,7 +77,7 @@ export class LoginController extends ViewController { this.state.createError = ''; this.state.isChecking = true; - this.emit('render', () => { + this.emit(this.appState.events.RENDER, () => { const { createEmail, createUsername, @@ -93,14 +93,14 @@ export class LoginController extends ViewController { ].includes('')) { this.state.createError = __('login.create_required_field_blank'); this.state.isChecking = false; - this.emit('render'); + this.emit(this.appState.events.RENDER); return; } if (createPassword !== createConfirm) { this.state.createError = __('login.create_password_confirm_mismatch'); this.state.isChecking = false; - this.emit('render'); + this.emit(this.appState.events.RENDER); return; } @@ -130,7 +130,7 @@ export class LoginController extends ViewController { console.error(response); this.state.loginError = __(response.message); this.state.isChecking = false; - this.emit('render'); + this.emit(this.appState.events.RENDER); return; } @@ -169,7 +169,7 @@ export class LoginController extends ViewController { console.error(response); this.state.createError = __(response.message); this.state.isChecking = false; - this.emit('render'); + this.emit(this.appState.events.RENDER); return; } diff --git a/app/views/search/controller.js b/app/views/search/controller.js index e5ca2b3..a4e24d2 100644 --- a/app/views/search/controller.js +++ b/app/views/search/controller.js @@ -66,7 +66,7 @@ export class SearchController extends ViewController { search() { if (this.hasQuery) { this.state.done = false; - this.emit('render', () => { + this.emit(this.appState.events.RENDER, () => { this.state.lastSearch = this.appState.query.for; this.state.lastSource = this.state.searchSource; this.state.lastBy = this.state.searchBy; @@ -79,7 +79,7 @@ export class SearchController extends ViewController { this.state.results = responseJSON; this.state.done = true; }) - .then(() => this.emit('render')); + .then(() => this.emit(this.appState.events.RENDER)); }); } } @@ -143,7 +143,7 @@ export class SearchController extends ViewController { shelvesPromise.then(() => { console.log(shelfController.state.myShelves); this.showShelves = true; - this.emit('render'); + this.emit(this.appState.events.RENDER); }); } @@ -152,7 +152,7 @@ export class SearchController extends ViewController { shelfController.addItemToShelf(bookData, shelfId).then(result => { console.log(result); this.showShelves = false; - this.emit('render'); + this.emit(this.appState.events.RENDER); }); } } \ No newline at end of file diff --git a/app/views/shelves/shelf.js b/app/views/shelves/shelf.js index c2f7e16..e24b5bf 100644 --- a/app/views/shelves/shelf.js +++ b/app/views/shelves/shelf.js @@ -24,7 +24,7 @@ export const shelfView = (shelvesController, emit) => { if (typeof shelvesController.state.loadedShelves[shelvesController.targetShelf] === 'undefined') { shelvesController.getTargetShelf().then(() => { - emit('render'); + emit(shelvesController.appState.events.RENDER); }); } @@ -77,7 +77,7 @@ export const shelfView = (shelvesController, emit) => {
diff --git a/app/views/shelves/userShelves/index.js b/app/views/shelves/userShelves/index.js index e32c3a5..818be52 100644 --- a/app/views/shelves/userShelves/index.js +++ b/app/views/shelves/userShelves/index.js @@ -22,7 +22,7 @@ export const userShelvesView = (shelvesController, emit) => { if (shelvesController.state.myShelves.length <= 0) { shelvesController.getUserShelves().then(() => { - emit('render'); + emit(shelvesController.appState.events.RENDER); }); }