From 95e29daa93c9fae85ee1839236b1535e154768c7 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Mon, 30 May 2016 17:12:39 +0100 Subject: [PATCH] Misc. JS style tweaks Mostly arrow function shorthand changes. --- .eslintrc.yml | 2 - src/gui/html/js/dialog.js | 68 ++-- src/gui/html/js/dom.js | 157 +++++---- src/gui/html/js/events.js | 47 ++- src/gui/html/js/filters.js | 104 +++--- src/gui/html/js/game.js | 602 +++++++++++++++++----------------- src/gui/html/js/helpers.js | 4 +- src/gui/html/js/initialise.js | 8 +- src/gui/html/js/plugin.js | 10 +- src/gui/html/js/query.js | 46 ++- src/gui/html/js/state.js | 207 ++++++------ src/gui/html/js/translator.js | 116 ++++--- 12 files changed, 670 insertions(+), 701 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index b0fb30ac..569f5aaf 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -20,11 +20,9 @@ rules: strict: - 2 - global - guard-for-in: 1 no-param-reassign: - 2 - props: false - arrow-body-style: 1 max-len: 1 plugins: diff --git a/src/gui/html/js/dialog.js b/src/gui/html/js/dialog.js index 93df9f69..afa92d3e 100644 --- a/src/gui/html/js/dialog.js +++ b/src/gui/html/js/dialog.js @@ -8,46 +8,44 @@ root.loot = root.loot || {}; root.loot.Dialog = factory(); } -}(this, () => { - return class { - static showProgress(text) { - const progressDialog = document.getElementById('progressDialog'); - progressDialog.getElementsByTagName('p')[0].textContent = text; - if (!progressDialog.opened) { - progressDialog.open(); - } else { - progressDialog.refit(); - } +}(this, () => class { + static showProgress(text) { + const progressDialog = document.getElementById('progressDialog'); + progressDialog.getElementsByTagName('p')[0].textContent = text; + if (!progressDialog.opened) { + progressDialog.open(); + } else { + progressDialog.refit(); } + } - static closeProgress() { - const progressDialog = document.getElementById('progressDialog'); - if (progressDialog.opened) { - progressDialog.refit(); - progressDialog.close(); - } + static closeProgress() { + const progressDialog = document.getElementById('progressDialog'); + if (progressDialog.opened) { + progressDialog.refit(); + progressDialog.close(); } + } - static showMessage(title, text) { - const dialog = document.createElement('loot-message-dialog'); - document.body.appendChild(dialog); - dialog.setDismissable(false); - dialog.showModal(title, text); - } + static showMessage(title, text) { + const dialog = document.createElement('loot-message-dialog'); + document.body.appendChild(dialog); + dialog.setDismissable(false); + dialog.showModal(title, text); + } - static askQuestion(title, text, confirmText, closeCallback) { - const dialog = document.createElement('loot-message-dialog'); - document.body.appendChild(dialog); - dialog.setConfirmText(confirmText); - dialog.showModal(title, text, closeCallback); - } + static askQuestion(title, text, confirmText, closeCallback) { + const dialog = document.createElement('loot-message-dialog'); + document.body.appendChild(dialog); + dialog.setConfirmText(confirmText); + dialog.showModal(title, text, closeCallback); + } - static showNotification(text) { - const toast = document.getElementById('toast'); - if (toast.opened) { - toast.hide(); - } - toast.show(text); + static showNotification(text) { + const toast = document.getElementById('toast'); + if (toast.opened) { + toast.hide(); } - }; + toast.show(text); + } })); diff --git a/src/gui/html/js/dom.js b/src/gui/html/js/dom.js index b08e048c..7d74c43a 100644 --- a/src/gui/html/js/dom.js +++ b/src/gui/html/js/dom.js @@ -8,97 +8,96 @@ root.loot = root.loot || {}; root.loot.DOM = factory(); } -}(this, () => { - return class { - static getElementInTableRowTemplate(rowTemplateId, elementClass) { - const select = document.querySelector('link[rel="import"][href$="editable-table.html"]'); - if (select) { - return select.import.querySelector(`#${rowTemplateId}`).content.querySelector(`.${elementClass}`); +}(this, () => class { + static 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}`); + } + + static show(elementId, showElement = true) { + document.getElementById(elementId).hidden = !showElement; + } + + static enable(elementId, enableElement = true) { + if (enableElement) { + document.getElementById(elementId).removeAttribute('disabled'); + } else { + document.getElementById(elementId).setAttribute('disabled', ''); + } + } + + static updateSelectedGame(gameFolder) { + document.getElementById('gameMenu').value = gameFolder; + + /* Also disable deletion of the game's row in the settings dialog. */ + const table = document.getElementById('gameTable'); + for (let i = 0; i < table.tBodies[0].rows.length; ++i) { + const folderElements = table.tBodies[0].rows[i].getElementsByClassName('folder'); + if (folderElements.length === 1) { + table.setReadOnly(table.tBodies[0].rows[i], + ['delete'], + folderElements[0].value === gameFolder); } - return document.querySelector(`#${rowTemplateId}`).content.querySelector(`.${elementClass}`); } + } - static show(elementId, showElement = true) { - document.getElementById(elementId).hidden = !showElement; - } - - static enable(elementId, enableElement = true) { - if (enableElement) { - document.getElementById(elementId).removeAttribute('disabled'); + static updateEnabledGames(installedGames) { + const gameMenuItems = document.getElementById('gameMenu').children; + for (let i = 0; i < gameMenuItems.length; ++i) { + if (installedGames.indexOf(gameMenuItems[i].getAttribute('value')) === -1) { + gameMenuItems[i].setAttribute('disabled', true); } else { - document.getElementById(elementId).setAttribute('disabled', ''); + gameMenuItems[i].removeAttribute('disabled'); } } + } - static updateSelectedGame(gameFolder) { - document.getElementById('gameMenu').value = gameFolder; + static createGameItem(game) { + const menuItem = document.createElement('paper-item'); + menuItem.setAttribute('value', game.folder); + menuItem.textContent = game.name; - /* Also disable deletion of the game's row in the settings dialog. */ - const table = document.getElementById('gameTable'); - for (let i = 0; i < table.tBodies[0].rows.length; ++i) { - const folderElements = table.tBodies[0].rows[i].getElementsByClassName('folder'); - if (folderElements.length === 1) { - table.setReadOnly(table.tBodies[0].rows[i], - ['delete'], - folderElements[0].value === gameFolder); - } - } + return menuItem; + } + + static setGameMenuItems(games) { + const gameMenu = document.getElementById('gameMenu'); + + /* First make sure game listing elements don't have any existing entries. */ + while (gameMenu.firstElementChild) { + gameMenu.removeChild(gameMenu.firstElementChild); } - static updateEnabledGames(installedGames) { - const gameMenuItems = document.getElementById('gameMenu').children; - for (let i = 0; i < gameMenuItems.length; ++i) { - if (installedGames.indexOf(gameMenuItems[i].getAttribute('value')) === -1) { - gameMenuItems[i].setAttribute('disabled', true); - } else { - gameMenuItems[i].removeAttribute('disabled'); - } - } + games.forEach((game) => { + gameMenu.appendChild(this.createGameItem(game)); + }); + } + + static updateSettingsDialog(settings) { + const gameSelect = document.getElementById('defaultGameSelect'); + const gameTable = document.getElementById('gameTable'); + + /* First make sure game listing elements don't have any existing entries. */ + while (gameSelect.children.length > 1) { + gameSelect.removeChild(gameSelect.lastElementChild); } + gameTable.clear(); - static createGameItem(game) { - const menuItem = document.createElement('paper-item'); - menuItem.setAttribute('value', game.folder); - menuItem.textContent = game.name; + /* Now fill with new values. */ + settings.games.forEach((game) => { + gameSelect.appendChild(this.createGameItem(game)); - return menuItem; - } + const row = gameTable.addRow(game); + gameTable.setReadOnly(row, ['name', 'folder', 'type']); + }); - static setGameMenuItems(games) { - const gameMenu = document.getElementById('gameMenu'); - - /* First make sure game listing elements don't have any existing entries. */ - while (gameMenu.firstElementChild) { - gameMenu.removeChild(gameMenu.firstElementChild); - } - - games.forEach((game) => { - gameMenu.appendChild(this.createGameItem(game)); - }); - } - - static updateSettingsDialog(settings) { - const gameSelect = document.getElementById('defaultGameSelect'); - const gameTable = document.getElementById('gameTable'); - - /* First make sure game listing elements don't have any existing entries. */ - while (gameSelect.children.length > 1) { - gameSelect.removeChild(gameSelect.lastElementChild); - } - gameTable.clear(); - - /* Now fill with new values. */ - settings.games.forEach((game) => { - gameSelect.appendChild(this.createGameItem(game)); - - const row = gameTable.addRow(game); - gameTable.setReadOnly(row, ['name', 'folder', 'type']); - }); - - gameSelect.value = settings.game; - document.getElementById('languageSelect').value = settings.language; - document.getElementById('enableDebugLogging').checked = settings.enableDebugLogging; - document.getElementById('updateMasterlist').checked = settings.updateMasterlist; - } - }; + gameSelect.value = settings.game; + document.getElementById('languageSelect').value = settings.language; + document.getElementById('enableDebugLogging').checked = settings.enableDebugLogging; + document.getElementById('updateMasterlist').checked = settings.updateMasterlist; + } })); diff --git a/src/gui/html/js/events.js b/src/gui/html/js/events.js index 0f184b03..3912aa6b 100644 --- a/src/gui/html/js/events.js +++ b/src/gui/html/js/events.js @@ -57,9 +57,7 @@ function updateMasterlist() { loot.game.globalMessages = result.globalMessages; result.plugins.forEach((resultPlugin) => { - const existingPlugin = loot.game.plugins.find((plugin) => { - return plugin.name === resultPlugin.name; - }); + const existingPlugin = loot.game.plugins.find(plugin => plugin.name === resultPlugin.name); if (existingPlugin) { existingPlugin.isDirty = resultPlugin.isDirty; existingPlugin.isPriorityGlobal = resultPlugin.isPriorityGlobal; @@ -70,7 +68,8 @@ function updateMasterlist() { } }); - loot.Dialog.showNotification(loot.l10n.translate('Masterlist updated to revision %s.', loot.game.masterlist.revision)); + loot.Dialog.showNotification(loot.l10n.translate('Masterlist updated to revision %s.', + loot.game.masterlist.revision)); } else { loot.Dialog.showNotification(loot.l10n.translate('No masterlist update was necessary.')); } @@ -99,21 +98,21 @@ function onSortPlugins() { loot.game.globalMessages = result.globalMessages; if (!result.plugins) { - const message = result.globalMessages.find((item) => { - return item.content[0].str.startsWith('Cyclic interaction detected'); - }); - throw new Error(loot.l10n.translate('Failed to sort plugins. Details: ' + message.content[0].str)); + const message = result.globalMessages.find(item => ( + item.content[0].str.startsWith('Cyclic interaction detected' + ))).content[0].str; + throw new Error(loot.l10n.translate(`Failed to sort plugins. Details: ${message}`)); } /* Check if sorted load order differs from current load order. */ - const loadOrderIsUnchanged = result.plugins.every((plugin, index) => { - return plugin.name === loot.game.plugins[index].name; - }); + const loadOrderIsUnchanged = result.plugins.every((plugin, index) => ( + plugin.name === loot.game.plugins[index].name + )); if (loadOrderIsUnchanged) { result.plugins.forEach((plugin) => { - const existingPlugin = loot.game.plugins.find((item) => { - return item.name === plugin.name; - }); + const existingPlugin = loot.game.plugins.find((item) => ( + item.name === plugin.name + )); if (existingPlugin) { existingPlugin.crc = plugin.crc; existingPlugin.isEmpty = plugin.isEmpty; @@ -129,9 +128,7 @@ function onSortPlugins() { loot.game.oldLoadOrder = loot.game.plugins; loot.game.loadOrder = []; result.plugins.forEach((plugin) => { - let existingPlugin = loot.game.plugins.find((item) => { - return item.name === plugin.name; - }); + let existingPlugin = loot.game.plugins.find(item => item.name === plugin.name); if (existingPlugin) { existingPlugin.crc = plugin.crc; existingPlugin.isEmpty = plugin.isEmpty; @@ -195,9 +192,7 @@ function onClearAllMetadata() { } /* Need to empty the UI-side user metadata. */ plugins.forEach((plugin) => { - const existingPlugin = loot.game.plugins.find((item) => { - return item.name === plugin.name; - }); + const existingPlugin = loot.game.plugins.find(item => item.name === plugin.name); if (existingPlugin) { existingPlugin.userlist = undefined; existingPlugin.editor = undefined; @@ -381,9 +376,9 @@ function onEditorOpen(evt) { return loot.query('editorOpened').catch(handlePromiseError); } function onEditorClose(evt) { - const plugin = loot.game.plugins.find((item) => { - return item.name === evt.target.querySelector('h1').textContent; - }); + const plugin = loot.game.plugins.find((item) => ( + item.name === evt.target.querySelector('h1').textContent + )); /* Update the plugin's editor state tracker */ plugin.isEditorOpen = false; @@ -430,7 +425,7 @@ function onEditorClose(evt) { }).catch(handlePromiseError); } function undoConflictsFilter() { - let wasConflictsFilterEnabled = (loot.filters.conflictTargetPluginName); + const wasConflictsFilterEnabled = (loot.filters.conflictTargetPluginName); loot.filters.conflictTargetPluginName = undefined; /* Deactivate any existing plugin conflict filter. */ @@ -473,9 +468,7 @@ function onClearMetadata(evt) { return; } /* Need to empty the UI-side user metadata. */ - const existingPlugin = loot.game.plugins.find((item) => { - return item.id === evt.target.id; - }); + const existingPlugin = loot.game.plugins.find(item => item.id === evt.target.id); if (existingPlugin) { existingPlugin.userlist = undefined; existingPlugin.editor = undefined; diff --git a/src/gui/html/js/filters.js b/src/gui/html/js/filters.js index 36c97852..f1a66932 100644 --- a/src/gui/html/js/filters.js +++ b/src/gui/html/js/filters.js @@ -8,64 +8,62 @@ root.loot = root.loot || {}; root.loot.Filters = factory(); } -}(this, () => { - return class { - constructor(l10n) { - /* Plugin filters */ - this.hideMessagelessPlugins = false; - this.hideInactivePlugins = false; - this.conflictingPluginNames = []; - this.contentSearchString = ''; +}(this, () => class { + constructor(l10n) { + /* Plugin filters */ + this.hideMessagelessPlugins = false; + this.hideInactivePlugins = false; + this.conflictingPluginNames = []; + this.contentSearchString = ''; - /* Plugin content filters */ - this.hideVersionNumbers = false; - this.hideCRCs = false; - this.hideBashTags = false; - this.hideAllPluginMessages = false; - this.hideNotes = false; - this.hideDoNotCleanMessages = false; + /* Plugin content filters */ + this.hideVersionNumbers = false; + this.hideCRCs = false; + this.hideBashTags = false; + this.hideAllPluginMessages = false; + this.hideNotes = false; + this.hideDoNotCleanMessages = false; - this._doNotCleanString = l10n.translate('Do not clean').toLowerCase(); + this._doNotCleanString = l10n.translate('Do not clean').toLowerCase(); + } + + pluginFilter(plugin) { + if (this.hideInactivePlugins && !plugin.isActive) { + return false; } - pluginFilter(plugin) { - if (this.hideInactivePlugins && !plugin.isActive) { - return false; - } - - if (this.conflictingPluginNames.length !== 0 - && this.conflictingPluginNames.indexOf(plugin.name) === -1) { - return false; - } - - if (this.hideMessagelessPlugins - && plugin.getCardContent(this).messages.length === 0) { - return false; - } - - if (this.contentSearchString.length !== 0 - && !plugin.getCardContent(this).containsText(this.contentSearchString)) { - return false; - } - - return true; + if (this.conflictingPluginNames.length !== 0 + && this.conflictingPluginNames.indexOf(plugin.name) === -1) { + return false; } - messageFilter(message) { - if (this.hideAllPluginMessages) { - return false; - } - - if (this.hideNotes && message.type === 'say') { - return false; - } - - if (this.hideDoNotCleanMessages - && message.content.toLowerCase().indexOf(this._doNotCleanString) !== -1) { - return false; - } - - return true; + if (this.hideMessagelessPlugins + && plugin.getCardContent(this).messages.length === 0) { + return false; } - }; + + if (this.contentSearchString.length !== 0 + && !plugin.getCardContent(this).containsText(this.contentSearchString)) { + return false; + } + + return true; + } + + messageFilter(message) { + if (this.hideAllPluginMessages) { + return false; + } + + if (this.hideNotes && message.type === 'say') { + return false; + } + + if (this.hideDoNotCleanMessages + && message.content.toLowerCase().indexOf(this._doNotCleanString) !== -1) { + return false; + } + + return true; + } })); diff --git a/src/gui/html/js/game.js b/src/gui/html/js/game.js index 92490c8c..8fa24988 100644 --- a/src/gui/html/js/game.js +++ b/src/gui/html/js/game.js @@ -9,131 +9,148 @@ root.loot = root.loot || {}; root.loot.Game = factory(root.marked); } -}(this, (marked) => { - return class { - constructor(obj, l10n) { - this.folder = obj.folder || ''; - this.globalMessages = obj.globalMessages || []; - this.masterlist = obj.masterlist || {}; - this.plugins = obj.plugins || []; +}(this, (marked) => class { + constructor(obj, l10n) { + this.folder = obj.folder || ''; + this.globalMessages = obj.globalMessages || []; + this.masterlist = obj.masterlist || {}; + this.plugins = obj.plugins || []; - this.loadOrder = undefined; - this.oldLoadOrder = undefined; + this.loadOrder = undefined; + this.oldLoadOrder = undefined; - this._notApplicableString = l10n.translate('N/A'); + this._notApplicableString = l10n.translate('N/A'); + } + + get folder() { + return this._folder; + } + + set folder(folder) { + if (folder !== this._folder) { + document.dispatchEvent(new CustomEvent('loot-game-folder-change', { + detail: { folder }, + })); } - get folder() { - return this._folder; - } + this._folder = folder; + } - set folder(folder) { - if (folder !== this._folder) { - document.dispatchEvent(new CustomEvent('loot-game-folder-change', { - detail: { folder }, - })); - } + get globalMessages() { + return this._globalMessages; + } - this._folder = folder; - } + set globalMessages(globalMessages) { + /* Update the message counts. */ + let oldTotal = 0; + let newTotal = 0; + let oldWarns = 0; + let newWarns = 0; + let oldErrs = 0; + let newErrs = 0; - get globalMessages() { - return this._globalMessages; - } - - set globalMessages(globalMessages) { - /* Update the message counts. */ - let oldTotal = 0; - let newTotal = 0; - let oldWarns = 0; - let newWarns = 0; - let oldErrs = 0; - let newErrs = 0; - - if (this._globalMessages) { - oldTotal = this._globalMessages.length; - this._globalMessages.forEach((message) => { - if (message.type === 'warn') { - ++oldWarns; - } else if (message.type === 'error') { - ++oldErrs; - } - }); - } - - if (globalMessages) { - newTotal = globalMessages.length; - - globalMessages.forEach((message) => { - if (message.type === 'warn') { - ++newWarns; - } else if (message.type === 'error') { - ++newErrs; - } - }); - } - - if (newTotal !== oldTotal || newWarns !== oldWarns || newErrs !== oldErrs) { - document.dispatchEvent(new CustomEvent('loot-game-global-messages-change', { - detail: { - totalDiff: newTotal - oldTotal, - warningDiff: newWarns - oldWarns, - errorDiff: newErrs - oldErrs, - messages: globalMessages, - }, - })); - } - - this._globalMessages = globalMessages; - } - - get masterlist() { - return this._masterlist; - } - - set masterlist(masterlist) { - if (masterlist !== this._masterlist - && (masterlist === undefined || this._masterlist === undefined - || masterlist.revision !== this._masterlist.revision - || masterlist.date !== this._masterlist.date)) { - let revision = this._notApplicableString; - let date = this._notApplicableString; - if (masterlist && masterlist.revision) { - revision = masterlist.revision; - } - if (masterlist && masterlist.date) { - date = masterlist.date; + if (this._globalMessages) { + oldTotal = this._globalMessages.length; + this._globalMessages.forEach((message) => { + if (message.type === 'warn') { + ++oldWarns; + } else if (message.type === 'error') { + ++oldErrs; } + }); + } - document.dispatchEvent(new CustomEvent('loot-game-masterlist-change', { - detail: { - revision, - date, - }, - })); + if (globalMessages) { + newTotal = globalMessages.length; + + globalMessages.forEach((message) => { + if (message.type === 'warn') { + ++newWarns; + } else if (message.type === 'error') { + ++newErrs; + } + }); + } + + if (newTotal !== oldTotal || newWarns !== oldWarns || newErrs !== oldErrs) { + document.dispatchEvent(new CustomEvent('loot-game-global-messages-change', { + detail: { + totalDiff: newTotal - oldTotal, + warningDiff: newWarns - oldWarns, + errorDiff: newErrs - oldErrs, + messages: globalMessages, + }, + })); + } + + this._globalMessages = globalMessages; + } + + get masterlist() { + return this._masterlist; + } + + set masterlist(masterlist) { + if (masterlist !== this._masterlist + && (masterlist === undefined || this._masterlist === undefined + || masterlist.revision !== this._masterlist.revision + || masterlist.date !== this._masterlist.date)) { + let revision = this._notApplicableString; + let date = this._notApplicableString; + if (masterlist && masterlist.revision) { + revision = masterlist.revision; + } + if (masterlist && masterlist.date) { + date = masterlist.date; } - this._masterlist = masterlist; + document.dispatchEvent(new CustomEvent('loot-game-masterlist-change', { + detail: { + revision, + date, + }, + })); } - get plugins() { - return this._plugins; + this._masterlist = masterlist; + } + + get plugins() { + return this._plugins; + } + + set plugins(plugins) { + /* Update plugin and message counts. Unlike for global messages + it's not worth calculating the count differences, just count + from zero. */ + let totalMessageNo = 0; + let warnMessageNo = 0; + let errorMessageNo = 0; + let activePluginNo = 0; + let dirtyPluginNo = 0; + + /* Include global messages in the count. */ + if (this.globalMessages) { + totalMessageNo = this.globalMessages.length; + this.globalMessages.forEach((message) => { + if (message.type === 'warn') { + ++warnMessageNo; + } else if (message.type === 'error') { + ++errorMessageNo; + } + }); } - set plugins(plugins) { - /* Update plugin and message counts. Unlike for global messages - it's not worth calculating the count differences, just count - from zero. */ - let totalMessageNo = 0; - let warnMessageNo = 0; - let errorMessageNo = 0; - let activePluginNo = 0; - let dirtyPluginNo = 0; - - /* Include global messages in the count. */ - if (this.globalMessages) { - totalMessageNo = this.globalMessages.length; - this.globalMessages.forEach((message) => { + plugins.forEach((plugin) => { + if (plugin.isActive) { + ++activePluginNo; + } + if (plugin.isDirty) { + ++dirtyPluginNo; + } + if (plugin.messages) { + totalMessageNo += plugin.messages.length; + plugin.messages.forEach((message) => { if (message.type === 'warn') { ++warnMessageNo; } else if (message.type === 'error') { @@ -141,218 +158,197 @@ } }); } + }); - plugins.forEach((plugin) => { - if (plugin.isActive) { - ++activePluginNo; - } - if (plugin.isDirty) { - ++dirtyPluginNo; - } - if (plugin.messages) { - totalMessageNo += plugin.messages.length; - plugin.messages.forEach((message) => { - if (message.type === 'warn') { - ++warnMessageNo; - } else if (message.type === 'error') { - ++errorMessageNo; - } - }); + document.dispatchEvent(new CustomEvent('loot-game-plugins-change', { + detail: { + valuesAreTotals: true, + totalMessageNo, + warnMessageNo, + errorMessageNo, + totalPluginNo: plugins.length, + activePluginNo, + dirtyPluginNo, + }, + })); + + this._plugins = plugins; + } + + appendPlugin(plugin) { + let totalChange = 0; + let warnChange = 0; + let errorChange = 0; + let activeChange = 0; + let dirtyChange = 0; + + if (plugin.isActive) { + ++activeChange; + } + if (plugin.isDirty) { + ++dirtyChange; + } + if (plugin.messages) { + totalChange += plugin.messages.length; + plugin.messages.forEach((message) => { + if (message.type === 'warn') { + ++warnChange; + } else if (message.type === 'error') { + ++errorChange; } }); + } - document.dispatchEvent(new CustomEvent('loot-game-plugins-change', { - detail: { - valuesAreTotals: true, - totalMessageNo, - warnMessageNo, - errorMessageNo, - totalPluginNo: plugins.length, - activePluginNo, - dirtyPluginNo, - }, + document.dispatchEvent(new CustomEvent('loot-game-plugins-change', { + detail: { + valuesAreTotals: false, + totalMessageNo: totalChange, + warnMessageNo: warnChange, + errorMessageNo: errorChange, + totalPluginNo: 1, + activePluginNo: activeChange, + dirtyPluginNo: dirtyChange, + }, + })); + + this._plugins.push(plugin); + } + + removePluginAtIndex(index) { + let totalChange = 0; + let warnChange = 0; + let errorChange = 0; + let activeChange = 0; + let dirtyChange = 0; + + if (this._plugins[index].isActive) { + --activeChange; + } + if (this._plugins[index].isDirty) { + --dirtyChange; + } + if (this._plugins[index].messages) { + totalChange -= this._plugins[index].messages.length; + this._plugins[index].messages.forEach((message) => { + if (message.type === 'warn') { + --warnChange; + } else if (message.type === 'error') { + --errorChange; + } + }); + } + + document.dispatchEvent(new CustomEvent('loot-game-plugins-change', { + detail: { + valuesAreTotals: false, + totalMessageNo: totalChange, + warnMessageNo: warnChange, + errorMessageNo: errorChange, + totalPluginNo: -1, + activePluginNo: activeChange, + dirtyPluginNo: dirtyChange, + }, + })); + + this._plugins.splice(index, 1); + } + + getContent() { + let messages = []; + let plugins = []; + + if (this.globalMessages) { + messages = this.globalMessages.map(message => ({ + type: message.type, + content: message.content[0].str, })); - - this._plugins = plugins; } + if (this.plugins) { + plugins = this.plugins.map(plugin => ({ + name: plugin.name, + crc: plugin.crc, + version: plugin.version, + isActive: plugin.isActive, + isEmpty: plugin.isEmpty, + loadsArchive: plugin.loadsArchive, - appendPlugin(plugin) { - let totalChange = 0; - let warnChange = 0; - let errorChange = 0; - let activeChange = 0; - let dirtyChange = 0; - - if (plugin.isActive) { - ++activeChange; - } - if (plugin.isDirty) { - ++dirtyChange; - } - if (plugin.messages) { - totalChange += plugin.messages.length; - plugin.messages.forEach((message) => { - if (message.type === 'warn') { - ++warnChange; - } else if (message.type === 'error') { - ++errorChange; - } - }); - } - - document.dispatchEvent(new CustomEvent('loot-game-plugins-change', { - detail: { - valuesAreTotals: false, - totalMessageNo: totalChange, - warnMessageNo: warnChange, - errorMessageNo: errorChange, - totalPluginNo: 1, - activePluginNo: activeChange, - dirtyPluginNo: dirtyChange, - }, + priority: plugin.priority, + isPriorityGlobal: plugin.isPriorityGlobal, + messages: plugin.messages, + tags: plugin.tags, + isDirty: plugin.isDirty, })); - - this._plugins.push(plugin); } - removePluginAtIndex(index) { - let totalChange = 0; - let warnChange = 0; - let errorChange = 0; - let activeChange = 0; - let dirtyChange = 0; + return { + messages, + plugins, + }; + } - if (this._plugins[index].isActive) { - --activeChange; - } - if (this._plugins[index].isDirty) { - --dirtyChange; - } - if (this._plugins[index].messages) { - totalChange -= this._plugins[index].messages.length; - this._plugins[index].messages.forEach((message) => { - if (message.type === 'warn') { - --warnChange; - } else if (message.type === 'error') { - --errorChange; - } - }); - } + getPluginNames() { + return this.plugins.map(plugin => plugin.name); + } - document.dispatchEvent(new CustomEvent('loot-game-plugins-change', { - detail: { - valuesAreTotals: false, - totalMessageNo: totalChange, - warnMessageNo: warnChange, - errorMessageNo: errorChange, - totalPluginNo: -1, - activePluginNo: activeChange, - dirtyPluginNo: dirtyChange, - }, - })); - - this._plugins.splice(index, 1); + static onPluginsChange(evt) { + if (!evt.detail.valuesAreTotals) { + evt.detail.totalMessageNo += parseInt(document.getElementById('totalMessageNo').textContent, 10); + evt.detail.warnMessageNo += parseInt(document.getElementById('totalWarningNo').textContent, 10); + evt.detail.errorMessageNo += parseInt(document.getElementById('totalErrorNo').textContent, 10); + evt.detail.totalPluginNo += parseInt(document.getElementById('totalPluginNo').textContent, 10); + evt.detail.activePluginNo += parseInt(document.getElementById('activePluginNo').textContent, 10); + evt.detail.dirtyPluginNo += parseInt(document.getElementById('dirtyPluginNo').textContent, 10); } - getContent() { - let messages = []; - let plugins = []; + document.getElementById('filterTotalMessageNo').textContent = evt.detail.totalMessageNo; + document.getElementById('totalMessageNo').textContent = evt.detail.totalMessageNo; + document.getElementById('totalWarningNo').textContent = evt.detail.warnMessageNo; + document.getElementById('totalErrorNo').textContent = evt.detail.errorMessageNo; - if (this.globalMessages) { - messages = this.globalMessages.map(message => ({ - type: message.type, - content: message.content[0].str, - })); - } - if (this.plugins) { - plugins = this.plugins.map(plugin => ({ - name: plugin.name, - crc: plugin.crc, - version: plugin.version, - isActive: plugin.isActive, - isEmpty: plugin.isEmpty, - loadsArchive: plugin.loadsArchive, + document.getElementById('filterTotalPluginNo').textContent = evt.detail.totalPluginNo; + document.getElementById('totalPluginNo').textContent = evt.detail.totalPluginNo; + document.getElementById('activePluginNo').textContent = evt.detail.activePluginNo; + document.getElementById('dirtyPluginNo').textContent = evt.detail.dirtyPluginNo; + } + static onGlobalMessagesChange(evt) { + document.getElementById('filterTotalMessageNo').textContent = parseInt(document.getElementById('filterTotalMessageNo').textContent, 10) + evt.detail.totalDiff; + document.getElementById('totalMessageNo').textContent = parseInt(document.getElementById('totalMessageNo').textContent, 10) + evt.detail.totalDiff; + document.getElementById('totalWarningNo').textContent = parseInt(document.getElementById('totalWarningNo').textContent, 10) + evt.detail.warningDiff; + document.getElementById('totalErrorNo').textContent = parseInt(document.getElementById('totalErrorNo').textContent, 10) + evt.detail.errorDiff; - priority: plugin.priority, - isPriorityGlobal: plugin.isPriorityGlobal, - messages: plugin.messages, - tags: plugin.tags, - isDirty: plugin.isDirty, - })); - } - - return { - messages, - plugins, - }; + /* Remove old messages from UI. */ + const generalMessagesList = document.getElementById('summary').getElementsByTagName('ul')[0]; + while (generalMessagesList.firstElementChild) { + generalMessagesList.removeChild(generalMessagesList.firstElementChild); } - getPluginNames() { - return this.plugins.map(plugin => plugin.name); + /* Add new messages. */ + if (evt.detail.messages) { + evt.detail.messages.forEach((message) => { + const li = document.createElement('li'); + li.className = message.type; + /* Use the Marked library for Markdown formatting support. */ + li.innerHTML = marked(message.content[0].str); + generalMessagesList.appendChild(li); + }); } - - static onPluginsChange(evt) { - if (!evt.detail.valuesAreTotals) { - evt.detail.totalMessageNo += parseInt(document.getElementById('totalMessageNo').textContent, 10); - evt.detail.warnMessageNo += parseInt(document.getElementById('totalWarningNo').textContent, 10); - evt.detail.errorMessageNo += parseInt(document.getElementById('totalErrorNo').textContent, 10); - evt.detail.totalPluginNo += parseInt(document.getElementById('totalPluginNo').textContent, 10); - evt.detail.activePluginNo += parseInt(document.getElementById('activePluginNo').textContent, 10); - evt.detail.dirtyPluginNo += parseInt(document.getElementById('dirtyPluginNo').textContent, 10); - } - - document.getElementById('filterTotalMessageNo').textContent = evt.detail.totalMessageNo; - document.getElementById('totalMessageNo').textContent = evt.detail.totalMessageNo; - document.getElementById('totalWarningNo').textContent = evt.detail.warnMessageNo; - document.getElementById('totalErrorNo').textContent = evt.detail.errorMessageNo; - - document.getElementById('filterTotalPluginNo').textContent = evt.detail.totalPluginNo; - document.getElementById('totalPluginNo').textContent = evt.detail.totalPluginNo; - document.getElementById('activePluginNo').textContent = evt.detail.activePluginNo; - document.getElementById('dirtyPluginNo').textContent = evt.detail.dirtyPluginNo; + } + static onMasterlistChange(evt) { + document.getElementById('masterlistRevision').textContent = evt.detail.revision; + document.getElementById('masterlistDate').textContent = evt.detail.date; + } + static onFolderChange(evt) { + loot.DOM.updateSelectedGame(evt.detail.folder); + /* Enable/disable the redate plugins option. */ + let gameSettings = undefined; + if (loot.settings && loot.settings.games) { + gameSettings = loot.settings.games.find(game => game.folder === evt.detail.folder); } - static onGlobalMessagesChange(evt) { - document.getElementById('filterTotalMessageNo').textContent = parseInt(document.getElementById('filterTotalMessageNo').textContent, 10) + evt.detail.totalDiff; - document.getElementById('totalMessageNo').textContent = parseInt(document.getElementById('totalMessageNo').textContent, 10) + evt.detail.totalDiff; - document.getElementById('totalWarningNo').textContent = parseInt(document.getElementById('totalWarningNo').textContent, 10) + evt.detail.warningDiff; - document.getElementById('totalErrorNo').textContent = parseInt(document.getElementById('totalErrorNo').textContent, 10) + evt.detail.errorDiff; - - /* Remove old messages from UI. */ - const generalMessagesList = document.getElementById('summary').getElementsByTagName('ul')[0]; - while (generalMessagesList.firstElementChild) { - generalMessagesList.removeChild(generalMessagesList.firstElementChild); - } - - /* Add new messages. */ - if (evt.detail.messages) { - evt.detail.messages.forEach((message) => { - const li = document.createElement('li'); - li.className = message.type; - /* Use the Marked library for Markdown formatting support. */ - li.innerHTML = marked(message.content[0].str); - generalMessagesList.appendChild(li); - }); - } + const redateButton = document.getElementById('redatePluginsButton'); + if (gameSettings && gameSettings.type === 'Skyrim') { + redateButton.removeAttribute('disabled'); + } else { + redateButton.setAttribute('disabled', true); } - static onMasterlistChange(evt) { - document.getElementById('masterlistRevision').textContent = evt.detail.revision; - document.getElementById('masterlistDate').textContent = evt.detail.date; - } - static onFolderChange(evt) { - loot.DOM.updateSelectedGame(evt.detail.folder); - /* Enable/disable the redate plugins option. */ - let gameSettings = undefined; - if (loot.settings && loot.settings.games) { - gameSettings = loot.settings.games.find((game) => { - return game.folder === evt.detail.folder; - }); - } - const redateButton = document.getElementById('redatePluginsButton'); - if (gameSettings && gameSettings.type === 'Skyrim') { - redateButton.removeAttribute('disabled'); - } else { - redateButton.setAttribute('disabled', true); - } - } - }; + } })); diff --git a/src/gui/html/js/helpers.js b/src/gui/html/js/helpers.js index b5bf1e07..3e05f70b 100644 --- a/src/gui/html/js/helpers.js +++ b/src/gui/html/js/helpers.js @@ -22,9 +22,7 @@ function getConflictingPlugins(pluginName) { if (result[key].conflicts) { conflicts.push(key); } - const plugin = loot.game.plugins.find((item) => { - return item.name === key; - }); + const plugin = loot.game.plugins.find(item => item.name === key); if (plugin) { plugin.crc = result[key].crc; plugin.isEmpty = result[key].isEmpty; diff --git a/src/gui/html/js/initialise.js b/src/gui/html/js/initialise.js index 0fff85d6..89df635e 100644 --- a/src/gui/html/js/initialise.js +++ b/src/gui/html/js/initialise.js @@ -115,10 +115,10 @@ } if (settings.filters) { - for (const filter in settings.filters) { + Object.getOwnPropertyNames(settings.filters).forEach((filter) => { filters[filter] = settings.filters[filter]; document.getElementById(filter).checked = filters[filter]; - } + }); } if (filters.hideMessagelessPlugins @@ -271,9 +271,7 @@ loot.DOM.setGameMenuItems(loot.settings.games); loot.DOM.updateEnabledGames(loot.installedGames); loot.DOM.updateSelectedGame(loot.game.folder); - }).then(() => { - return displayInitErrors(); - }).then((result) => { + }).then(displayInitErrors).then((result) => { if (result) { Dialog.closeProgress(); document.getElementById('settingsButton').click(); diff --git a/src/gui/html/js/plugin.js b/src/gui/html/js/plugin.js index 432134cc..a27d888f 100644 --- a/src/gui/html/js/plugin.js +++ b/src/gui/html/js/plugin.js @@ -61,12 +61,10 @@ this._tags = []; } - this._messages = plugin.messages.map((message) => { - return { - type: message.type, - content: message.content[0].str, - }; - }).filter(filters.messageFilter, filters); + this._messages = plugin.messages.map(message => ({ + type: message.type, + content: message.content[0].str, + })).filter(filters.messageFilter, filters); } get name() { diff --git a/src/gui/html/js/query.js b/src/gui/html/js/query.js index a5be1158..8a07730c 100644 --- a/src/gui/html/js/query.js +++ b/src/gui/html/js/query.js @@ -8,29 +8,27 @@ root.loot = root.loot || {}; root.loot.query = factory(); } -}(this, () => { - return (requestName, ...args) => { - if (!requestName) { - throw new Error('No request name passed'); - } - let request; - if (args.length === 0) { - request = requestName; - } else { - request = JSON.stringify({ - name: requestName, - args, - }); - } - return new Promise((resolve, reject) => { - window.cefQuery({ - request, - persistent: false, - onSuccess: resolve, - onFailure: (errorCode, errorMessage) => { - reject(new Error(`Error code: ${errorCode}; ${errorMessage}`)); - }, - }); +}(this, () => (requestName, ...args) => { + if (!requestName) { + throw new Error('No request name passed'); + } + let request; + if (args.length === 0) { + request = requestName; + } else { + request = JSON.stringify({ + name: requestName, + args, }); - }; + } + return new Promise((resolve, reject) => { + window.cefQuery({ + request, + persistent: false, + onSuccess: resolve, + onFailure: (errorCode, errorMessage) => { + reject(new Error(`Error code: ${errorCode}; ${errorMessage}`)); + }, + }); + }); })); diff --git a/src/gui/html/js/state.js b/src/gui/html/js/state.js index 2cab1d34..017b8b65 100644 --- a/src/gui/html/js/state.js +++ b/src/gui/html/js/state.js @@ -9,119 +9,116 @@ root.loot.State = factory(root.loot.DOM); root.loot.state = new root.loot.State(); } -}(this, (dom) => { - return class State { +}(this, dom => class State { + static get DEFAULT_STATE() { + return 0; + } - static get DEFAULT_STATE() { - return 0; + static get SORTING_STATE() { + return 1; + } + + static get EDITING_STATE() { + return 2; + } + + constructor() { + this.currentState = State.DEFAULT_STATE; + } + + isInDefaultState() { + return this.currentState === State.DEFAULT_STATE; + } + + isInEditingState() { + return this.currentState === State.EDITING_STATE; + } + + isInSortingState() { + return this.currentState === State.SORTING_STATE; + } + + enterSortingState() { + if (this.isInSortingState()) { + return; + } + if (this.isInEditingState()) { + throw new Error('Cannot enter sorting state from editing state'); } - static get SORTING_STATE() { - return 1; + /* Hide the masterlist update buttons, and display the accept and + cancel sort buttons. */ + dom.show('updateMasterlistButton', false); + dom.show('sortButton', false); + dom.show('applySortButton'); + dom.show('cancelSortButton'); + + /* Disable changing game. */ + dom.enable('gameMenu', false); + dom.enable('refreshContentButton', false); + + this.currentState = State.SORTING_STATE; + } + + exitSortingState() { + if (this.isInDefaultState()) { + return; + } + if (this.isInEditingState()) { + throw new Error('Cannot exit sorting state from editing state'); } - static get EDITING_STATE() { - return 2; + /* Show the masterlist update buttons, and hide the accept and + cancel sort buttons. */ + dom.show('updateMasterlistButton'); + dom.show('sortButton'); + dom.show('applySortButton', false); + dom.show('cancelSortButton', false); + + /* Enable changing game. */ + dom.enable('gameMenu'); + dom.enable('refreshContentButton'); + + this.currentState = State.DEFAULT_STATE; + } + + enterEditingState() { + if (this.isInEditingState()) { + return; + } + if (this.isInSortingState()) { + throw new Error('Cannot enter editing state from sorting state'); } - constructor() { - this.currentState = State.DEFAULT_STATE; + /* Disable the toolbar elements. */ + dom.enable('wipeUserlistButton', false); + dom.enable('copyContentButton', false); + dom.enable('refreshContentButton', false); + dom.enable('settingsButton', false); + dom.enable('gameMenu', false); + dom.enable('updateMasterlistButton', false); + dom.enable('sortButton', false); + + this.currentState = State.EDITING_STATE; + } + + exitEditingState() { + if (this.isInDefaultState()) { + return; + } + if (this.isInSortingState()) { + throw new Error('Cannot exit editing state from sorting state'); } - isInDefaultState() { - return this.currentState === State.DEFAULT_STATE; - } + /* Re-enable toolbar elements. */ + dom.enable('wipeUserlistButton'); + dom.enable('copyContentButton'); + dom.enable('refreshContentButton'); + dom.enable('settingsButton'); + dom.enable('gameMenu'); + dom.enable('updateMasterlistButton'); + dom.enable('sortButton'); - isInEditingState() { - return this.currentState === State.EDITING_STATE; - } - - isInSortingState() { - return this.currentState === State.SORTING_STATE; - } - - enterSortingState() { - if (this.isInSortingState()) { - return; - } - if (this.isInEditingState()) { - throw new Error('Cannot enter sorting state from editing state'); - } - - /* Hide the masterlist update buttons, and display the accept and - cancel sort buttons. */ - loot.DOM.show('updateMasterlistButton', false); - loot.DOM.show('sortButton', false); - loot.DOM.show('applySortButton'); - loot.DOM.show('cancelSortButton'); - - /* Disable changing game. */ - loot.DOM.enable('gameMenu', false); - loot.DOM.enable('refreshContentButton', false); - - this.currentState = State.SORTING_STATE; - } - - exitSortingState() { - if (this.isInDefaultState()) { - return; - } - if (this.isInEditingState()) { - throw new Error('Cannot exit sorting state from editing state'); - } - - /* Show the masterlist update buttons, and hide the accept and - cancel sort buttons. */ - loot.DOM.show('updateMasterlistButton'); - loot.DOM.show('sortButton'); - loot.DOM.show('applySortButton', false); - loot.DOM.show('cancelSortButton', false); - - /* Enable changing game. */ - loot.DOM.enable('gameMenu'); - loot.DOM.enable('refreshContentButton'); - - this.currentState = State.DEFAULT_STATE; - } - - enterEditingState() { - if (this.isInEditingState()) { - return; - } - if (this.isInSortingState()) { - throw new Error('Cannot enter editing state from sorting state'); - } - - /* Disable the toolbar elements. */ - loot.DOM.enable('wipeUserlistButton', false); - loot.DOM.enable('copyContentButton', false); - loot.DOM.enable('refreshContentButton', false); - loot.DOM.enable('settingsButton', false); - loot.DOM.enable('gameMenu', false); - loot.DOM.enable('updateMasterlistButton', false); - loot.DOM.enable('sortButton', false); - - this.currentState = State.EDITING_STATE; - } - - exitEditingState() { - if (this.isInDefaultState()) { - return; - } - if (this.isInSortingState()) { - throw new Error('Cannot exit editing state from sorting state'); - } - - /* Re-enable toolbar elements. */ - loot.DOM.enable('wipeUserlistButton'); - loot.DOM.enable('copyContentButton'); - loot.DOM.enable('refreshContentButton'); - loot.DOM.enable('settingsButton'); - loot.DOM.enable('gameMenu'); - loot.DOM.enable('updateMasterlistButton'); - loot.DOM.enable('sortButton'); - - this.currentState = State.DEFAULT_STATE; - } - }; + this.currentState = State.DEFAULT_STATE; + } })); diff --git a/src/gui/html/js/translator.js b/src/gui/html/js/translator.js index 80d389ac..63f210cb 100644 --- a/src/gui/html/js/translator.js +++ b/src/gui/html/js/translator.js @@ -11,69 +11,67 @@ root.loot = root.loot || {}; root.loot.Translator = factory(root.Jed, root.jedGettextParser); } -}(this, (Jed, jedGettextParser) => { - return class { - /* Returns a Promise */ - constructor(locale) { - this.locale = locale || 'en'; - this.jed = undefined; - } +}(this, (Jed, jedGettextParser) => class { + /* Returns a Promise */ + constructor(locale) { + this.locale = locale || 'en'; + this.jed = undefined; + } - load() { - const defaultTranslationData = { - messages: { - '': { - domain: 'messages', - lang: 'en', - plural_forms: 'nplurals=2; plural=(n != 1);', - }, - }, - }; - - let translationDataPromise; - if (this.locale === 'en') { - /* Just resolve to an empty data set. */ - translationDataPromise = Promise.resolve(defaultTranslationData); - } else { - translationDataPromise = new Promise((resolve, reject) => { - const url = `loot://l10n/${this.locale}/LC_MESSAGES/loot.mo`; - const xhr = new XMLHttpRequest(); - xhr.open('GET', url); - xhr.responseType = 'arraybuffer'; - xhr.addEventListener('readystatechange', (evt) => { - if (evt.target.readyState === 4) { - /* Status is 0 for local file URL loading. */ - if (evt.target.status >= 200 && evt.target.status < 400) { - resolve(jedGettextParser.mo.parse(evt.target.response)); - } else { - reject(new Error(evt.target.statusText)); - } - } - }, false); - xhr.send(); - }); - } - - return translationDataPromise.catch((error) => { - console.log(`Error loading translation data: ${error.message}`); // eslint-disable-line no-console - return defaultTranslationData; - }).then((result) => { - this.jed = new Jed({ - locale_data: result, + load() { + const defaultTranslationData = { + messages: { + '': { domain: 'messages', - }); + lang: 'en', + plural_forms: 'nplurals=2; plural=(n != 1);', + }, + }, + }; + + let translationDataPromise; + if (this.locale === 'en') { + /* Just resolve to an empty data set. */ + translationDataPromise = Promise.resolve(defaultTranslationData); + } else { + translationDataPromise = new Promise((resolve, reject) => { + const url = `loot://l10n/${this.locale}/LC_MESSAGES/loot.mo`; + const xhr = new XMLHttpRequest(); + xhr.open('GET', url); + xhr.responseType = 'arraybuffer'; + xhr.addEventListener('readystatechange', (evt) => { + if (evt.target.readyState === 4) { + /* Status is 0 for local file URL loading. */ + if (evt.target.status >= 200 && evt.target.status < 400) { + resolve(jedGettextParser.mo.parse(evt.target.response)); + } else { + reject(new Error(evt.target.statusText)); + } + } + }, false); + xhr.send(); }); } - translate(text, ...substitutions) { - if (text === undefined) { - return ''; - } - if (this.jed === undefined) { - return text; - } - const func = this.jed.translate(text); - return func.fetch.apply(func, substitutions); + return translationDataPromise.catch((error) => { + console.log(`Error loading translation data: ${error.message}`); // eslint-disable-line no-console + return defaultTranslationData; + }).then((result) => { + this.jed = new Jed({ + locale_data: result, + domain: 'messages', + }); + }); + } + + translate(text, ...substitutions) { + if (text === undefined) { + return ''; } - }; + if (this.jed === undefined) { + return text; + } + const func = this.jed.translate(text); + return func.fetch.apply(func, substitutions); + } }));