diff --git a/src/gui/html/js/dom.js b/src/gui/html/js/dom.js index 2da58605..610be306 100644 --- a/src/gui/html/js/dom.js +++ b/src/gui/html/js/dom.js @@ -4,6 +4,13 @@ function showElement(element) { element.classList.toggle('hidden', false); } } +function getElementInTableRowTemplate(rowTemplateId, elementClass) { + const select = document.querySelector('link[rel="import"][href$="editable-table.html"]'); + if (select) { + return select.import.querySelector('#' + rowTemplateId).content.querySelector('.' + elementClass); + } + return document.querySelector('#' + rowTemplateId).content.querySelector('.' + elementClass); +} function hideElement(element) { if (element !== null) { element.classList.toggle('hidden', true); diff --git a/src/gui/html/js/events.js b/src/gui/html/js/events.js index ed4a564e..9b76f7a7 100644 --- a/src/gui/html/js/events.js +++ b/src/gui/html/js/events.js @@ -432,8 +432,9 @@ function onCloseSettingsDialog(evt) { }; /* Send the settings back to the C++ side. */ - loot.query('closeSettings', settings).then(JSON.parse).then((result) => { - setInstalledGames(result); + loot.query('closeSettings', settings).then(JSON.parse).then((installedGames) => { + loot.installedGames = installedGames; + updateEnabledGames(installedGames); }).catch(handlePromiseError).then(() => { loot.settings = settings; updateSettingsDialog(loot.settings, loot.installedGames, loot.game.folder); diff --git a/src/gui/html/js/helpers.js b/src/gui/html/js/helpers.js index bc0610e4..3c214d77 100644 --- a/src/gui/html/js/helpers.js +++ b/src/gui/html/js/helpers.js @@ -71,7 +71,3 @@ function filterPluginData(plugins, filters) { document.getElementById('hiddenMessageNo').textContent = hiddenMessageNo; }); } -function setInstalledGames(installedGames) { - loot.installedGames = installedGames; - updateEnabledGames(installedGames); -} diff --git a/src/gui/html/js/init.js b/src/gui/html/js/init.js index 7fe85bf6..2308cba2 100644 --- a/src/gui/html/js/init.js +++ b/src/gui/html/js/init.js @@ -22,10 +22,22 @@ . */ 'use strict'; -function applyEnabledFilters() { - if (!loot.filters) { - return; +(function exportModule(root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define([], factory); + } else { + // Browser globals + root.loot = root.loot || {}; + root.loot.initialise = factory(root.loot.Dialog, + root.loot.Filters, + root.loot.Game, + root.loot.translateStaticText, + root.loot.Plugin, + root.loot.query, + root.loot.Translator); } +}(this, (Dialog, Filters, Game, translateStaticText, Plugin, query, Translator) => { function setupEventHandlers() { /* Set up handlers for filters. */ document.getElementById('hideVersionNumbers').addEventListener('change', onToggleDisplayCSS); @@ -41,10 +53,6 @@ function applyEnabledFilters() { document.getElementById('hideMessagelessPlugins').addEventListener('change', onSidebarFilterToggle); document.body.addEventListener('loot-filter-conflicts', onConflictsFilter); - if (loot.settings.filters) { - for (const filter in loot.settings.filters) { - loot.filters[filter] = loot.settings.filters[filter]; - document.getElementById(filter).checked = loot.filters[filter]; /* Set up event handlers for content filter. */ document.getElementById('contentFilter').addEventListener('change', onSidebarFilterToggle); @@ -96,181 +104,181 @@ function applyEnabledFilters() { document.addEventListener('loot-game-plugins-change', onGamePluginsChange); } + function applyEnabledFilters(filters, settings, plugins) { + if (!filters) { + return; } - } - if (loot.filters.hideMessagelessPlugins - || loot.filters.hideInactivePlugins - || loot.filters.hideNotes - || loot.filters.hideDoNotCleanMessages - || loot.filters.hideAllPluginMessages) { + if (settings.filters) { + for (const filter in settings.filters) { + filters[filter] = settings.filters[filter]; + document.getElementById(filter).checked = filters[filter]; + } + } + + if (filters.hideMessagelessPlugins + || filters.hideInactivePlugins + || filters.hideNotes + || filters.hideDoNotCleanMessages + || filters.hideAllPluginMessages) { filterPluginData(plugins, filters); - } - - if (loot.filters.hideVersionNumbers) { - document.getElementById('hideVersionNumbers').dispatchEvent(new Event('change')); - } - - if (loot.filters.hideCRCs) { - document.getElementById('hideCRCs').dispatchEvent(new Event('change')); - } - - if (loot.filters.hideBashTags) { - document.getElementById('hideBashTags').dispatchEvent(new Event('change')); - } -} - -function getVersion() { - return loot.query('getVersion').then(JSON.parse).then((result) => { - /* The fourth part of the version string is the build number. Trim it. */ - const pos = result.lastIndexOf('.'); - loot.version = result.substring(0, pos); - document.getElementById('LOOTVersion').textContent = loot.version; - document.getElementById('firstTimeLootVersion').textContent = loot.version; - document.getElementById('LOOTBuild').textContent = result.substring(pos + 1); - }); -} - -function getLanguages() { - return loot.query('getLanguages').then(JSON.parse).then((result) => { - /* Now fill in language options. */ - const settingsLangSelect = document.getElementById('languageSelect'); - let messageLangSelect = document.querySelector('link[rel="import"][href$="editable-table.html"]'); - if (messageLangSelect) { - messageLangSelect = messageLangSelect.import.querySelector('#messageRow').content.querySelector('.language'); - } else { - messageLangSelect = document.querySelector('#messageRow').content.querySelector('.language'); } - for (let i = 0; i < result.length; ++i) { - const settingsItem = document.createElement('paper-item'); - settingsItem.setAttribute('value', result[i].locale); - settingsItem.setAttribute('noink', ''); - settingsItem.textContent = result[i].name; - settingsLangSelect.appendChild(settingsItem); - messageLangSelect.appendChild(settingsItem.cloneNode(true)); + if (filters.hideVersionNumbers) { + document.getElementById('hideVersionNumbers').dispatchEvent(new Event('change')); } - messageLangSelect.setAttribute('value', messageLangSelect.firstElementChild.getAttribute('value')); - }); -} - -function getInitErrors() { - return loot.query('getInitErrors').then(JSON.parse).then((result) => { - if (!result) { - return result; + if (filters.hideCRCs) { + document.getElementById('hideCRCs').dispatchEvent(new Event('change')); } - const generalMessagesList = document.getElementById('summary').getElementsByTagName('ul')[0]; - result.forEach((message) => { - const li = document.createElement('li'); - li.className = 'error'; - /* Use the Marked library for Markdown formatting support. */ - li.innerHTML = window.marked(message); - generalMessagesList.appendChild(li); + if (filters.hideBashTags) { + document.getElementById('hideBashTags').dispatchEvent(new Event('change')); + } + } + + function setVersion(appData) { + return query('getVersion').then(JSON.parse).then((result) => { + /* The fourth part of the version string is the build number. Trim it. */ + const pos = result.lastIndexOf('.'); + appData.version = result.substring(0, pos); + document.getElementById('LOOTVersion').textContent = appData.version; + document.getElementById('firstTimeLootVersion').textContent = appData.version; + document.getElementById('LOOTBuild').textContent = result.substring(pos + 1); }); + } - document.getElementById('filterTotalMessageNo').textContent = result.length; - document.getElementById('totalMessageNo').textContent = result.length; - document.getElementById('totalErrorNo').textContent = result.length; + function setLanguages() { + return query('getLanguages').then(JSON.parse).then((result) => { + /* Now fill in language options. */ + const settingsLangSelect = document.getElementById('languageSelect'); + const messageLangSelect = getElementInTableRowTemplate('messageRow', 'language'); - return result; - }); -} + result.forEach((language) => { + const settingsItem = document.createElement('paper-item'); + settingsItem.setAttribute('value', language.locale); + settingsItem.setAttribute('noink', ''); + settingsItem.textContent = language.name; + settingsLangSelect.appendChild(settingsItem); + messageLangSelect.appendChild(settingsItem.cloneNode(true)); + }); -function getGameTypes() { - return loot.query('getGameTypes').then(JSON.parse).then((result) => { - /* Fill in game row template's game type options. */ - let select = document.querySelector('link[rel="import"][href$="editable-table.html"]'); - if (select) { - select = select.import.querySelector('#gameRow').content.querySelector('.type'); - } else { - select = document.querySelector('#gameRow').content.querySelector('.type'); - } - for (let j = 0; j < result.length; ++j) { - const item = document.createElement('paper-item'); - item.setAttribute('value', result[j]); - item.setAttribute('noink', ''); - item.textContent = result[j]; - select.appendChild(item); - } - select.setAttribute('value', select.firstElementChild.getAttribute('value')); - }); -} + messageLangSelect.setAttribute('value', messageLangSelect.firstElementChild.getAttribute('value')); + }); + } -function getInstalledGames() { - return loot.query('getInstalledGames').then(JSON.parse).then(setInstalledGames); -} + function displayInitErrors() { + return query('getInitErrors').then(JSON.parse).then((result) => { + if (!result) { + return result; + } + const generalMessagesList = document.getElementById('summary').getElementsByTagName('ul')[0]; -function getSettings() { - return loot.query('getSettings').then(JSON.parse).then((result) => { - loot.settings = result; + result.forEach((message) => { + const li = document.createElement('li'); + li.className = 'error'; + /* Use the Marked library for Markdown formatting support. */ + li.innerHTML = window.marked(message); + generalMessagesList.appendChild(li); + }); + + document.getElementById('filterTotalMessageNo').textContent = result.length; + document.getElementById('totalMessageNo').textContent = result.length; + document.getElementById('totalErrorNo').textContent = result.length; + + return result; + }); + } + + function setGameTypes() { + return query('getGameTypes').then(JSON.parse).then((result) => { + /* Fill in game row template's game type options. */ + const select = getElementInTableRowTemplate('gameRow', 'type'); + result.forEach((gameType) => { + const item = document.createElement('paper-item'); + item.setAttribute('value', gameType); + item.setAttribute('noink', ''); + item.textContent = gameType; + select.appendChild(item); + }); + select.setAttribute('value', select.firstElementChild.getAttribute('value')); + }); + } + + function setInstalledGames(appData) { + return query('getInstalledGames').then(JSON.parse).then((installedGames) => { + appData.installedGames = installedGames; + updateEnabledGames(installedGames); + }); + } + + function setSettings(appData) { + return query('getSettings').then(JSON.parse).then((result) => { + appData.settings = result; updateSettingsDialog(appData.settings, appData.installedGames, appData.game.folder); - }); -} + }); + } -function getGameData() { - return loot.query('getGameData').then((result) => { - const game = JSON.parse(result, loot.Plugin.fromJson); - loot.game = new loot.Game(game, loot.l10n); - document.getElementById('cardsNav').data = loot.game.plugins; - document.getElementById('main').lastElementChild.data = loot.game.plugins; - applyEnabledFilters(); + function setGameData(appData) { + return query('getGameData').then((result) => { + const game = JSON.parse(result, Plugin.fromJson); + appData.game = new Game(game, appData.l10n); + document.getElementById('cardsNav').data = appData.game.plugins; + document.getElementById('main').lastElementChild.data = appData.game.plugins; + applyEnabledFilters(appData.filters, appData.settings, appData.game.plugins); + Dialog.closeProgress(); + }); + } - setTimeout(() => { - document.getElementById('cardsNav').updateSize(); - loot.Dialog.closeProgress(); - }, 100); - }); -} + return () => { + Dialog.showProgress('Initialising user interface...'); + /* Set the plugin list's scroll target to its parent. */ + document.getElementById('pluginCardList').scrollTarget = document.getElementById('main'); -function initialise() { - loot.Dialog.showProgress('Initialising user interface...'); - /* Set the plugin list's scroll target to its parent. */ - document.getElementById('pluginCardList').scrollTarget = document.getElementById('main'); + /* Make sure settings are what I want. */ + window.marked.setOptions({ + gfm: true, + tables: true, + sanitize: true, + }); + setupEventHandlers(); - /* Make sure settings are what I want. */ - window.marked.setOptions({ - gfm: true, - tables: true, - sanitize: true, - }); - setupEventHandlers(); + loot.version = ''; + loot.settings = {}; + loot.l10n = new Translator(); + loot.game = new Game({}, loot.l10n); + loot.filters = new Filters(loot.l10n); - loot.l10n = new loot.Translator(); - loot.l10n.load().then(() => { - loot.filters = new loot.Filters(loot.l10n); - loot.game = new loot.Game({}, loot.l10n); - }).then(() => { - return Promise.all([ - getVersion(), - getLanguages(), - getGameTypes(), - getInstalledGames(), - getSettings(), - ]); - }).then(() => { - /* Translate static text. */ - loot.l10n = new loot.Translator(loot.settings.language); - return loot.l10n.load(); - }).then(() => { - loot.translateStaticText(loot.l10n); - /* Also need to update the settings UI. */ - updateSettingsUI(); - }).then(() => { - return getInitErrors(); - }).then((result) => { - if (result) { - loot.Dialog.closeProgress(); - document.getElementById('settingsButton').click(); - return Promise.resolve(''); - } - return getGameData(); - }).then(() => { - if (!loot.settings.lastVersion || loot.settings.lastVersion !== loot.version) { - document.getElementById('firstRun').showModal(); - } - }).catch(handlePromiseError); -} + Promise.all([ + setLanguages(), + setGameTypes(), + setInstalledGames(loot), + setVersion(loot), + setSettings(loot), + ]).then(() => { + /* Translate static text. */ + loot.l10n = new Translator(loot.settings.language); + return loot.l10n.load(); + }).then(() => { + loot.filters = new Filters(loot.l10n); + translateStaticText(loot.l10n); + /* Also need to update the settings UI. */ + updateSettingsDialog(loot.settings, loot.installedGames, loot.game.folder); + }).then(() => { + return displayInitErrors(); + }).then((result) => { + if (result) { + Dialog.closeProgress(); + document.getElementById('settingsButton').click(); + return Promise.resolve(); + } + return setGameData(loot); + }).then(() => { + if (!loot.settings.lastVersion || loot.settings.lastVersion !== loot.version) { + document.getElementById('firstRun').showModal(); + } + }).catch(handlePromiseError); + }; +})); -window.addEventListener('polymer-ready', initialise); +window.addEventListener('polymer-ready', loot.initialise);