From ca9c8889833dd0e5b14936dde935f0618f86fd3a Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 28 May 2016 08:02:48 +0100 Subject: [PATCH] Refactor misc. event handling code --- src/gui/html/js/events.js | 54 ++++------------ src/gui/html/js/game.js | 37 +++++++++++ src/tests/gui/html/js/test_game.js | 98 ++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 43 deletions(-) diff --git a/src/gui/html/js/events.js b/src/gui/html/js/events.js index 6baeacb7..24cf6662 100644 --- a/src/gui/html/js/events.js +++ b/src/gui/html/js/events.js @@ -91,9 +91,7 @@ function onSortPlugins() { if (loot.settings.updateMasterlist) { promise = promise.then(updateMasterlist); } - promise.then(() => { - return loot.query('sortPlugins').then(JSON.parse); - }).then((result) => { + promise.then(() => loot.query('sortPlugins')).then(JSON.parse).then((result) => { if (!result) { return; } @@ -153,9 +151,7 @@ function onSortPlugins() { }).catch(handlePromiseError); } function onApplySort() { - const loadOrder = loot.game.plugins.map((plugin) => { - return plugin.name; - }); + const loadOrder = loot.game.getPluginNames(); return loot.query('applySort', loadOrder).then(() => { /* Remove old load order storage. */ delete loot.game.loadOrder; @@ -219,50 +215,24 @@ function onClearAllMetadata() { }); } function onCopyContent() { - let messages = []; - let plugins = []; + let content = { + messages: [], + plugins: [], + }; if (loot.game) { - if (loot.game.globalMessages) { - messages = loot.game.globalMessages.map((message) => { - return { - type: message.type, - content: message.content[0].str, - }; - }); - } - if (loot.game.plugins) { - plugins = loot.game.plugins.map((plugin) => { - return { - name: plugin.name, - crc: plugin.crc, - version: plugin.version, - isActive: plugin.isActive, - isEmpty: plugin.isEmpty, - loadsArchive: plugin.loadsArchive, - - priority: plugin.priority, - isPriorityGlobal: plugin.isPriorityGlobal, - messages: plugin.messages, - tags: plugin.tags, - isDirty: plugin.isDirty, - }; - }); - } + content = loot.game.getContent(); } else { const message = document.getElementById('summary').getElementsByTagName('ul')[0].firstElementChild; if (message) { - messages.push({ - type: 'error', + content.messages.push({ + type: message.className, content: message.textContent, }); } } - loot.query('copyContent', { - messages, - plugins, - }).then(() => { + loot.query('copyContent', content).then(() => { loot.Dialog.showNotification(loot.l10n.translate("LOOT's content has been copied to the clipboard.")); }).catch(handlePromiseError); } @@ -270,9 +240,7 @@ function onCopyLoadOrder() { let plugins = []; if (loot.game && loot.game.plugins) { - plugins = loot.game.plugins.map((plugin) => { - return plugin.name; - }); + plugins = loot.game.getPluginNames(); } loot.query('copyLoadOrder', plugins).then(() => { diff --git a/src/gui/html/js/game.js b/src/gui/html/js/game.js index a95ea157..3f1d13a9 100644 --- a/src/gui/html/js/game.js +++ b/src/gui/html/js/game.js @@ -254,6 +254,43 @@ 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, + })); + } + 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, + + priority: plugin.priority, + isPriorityGlobal: plugin.isPriorityGlobal, + messages: plugin.messages, + tags: plugin.tags, + isDirty: plugin.isDirty, + })); + } + + return { + messages, + plugins, + }; + } + + getPluginNames() { + return this.plugins.map(plugin => plugin.name); + } + static onPluginsChange(evt) { if (!evt.detail.valuesAreTotals) { evt.detail.totalMessageNo += parseInt(document.getElementById('totalMessageNo').textContent, 10); diff --git a/src/tests/gui/html/js/test_game.js b/src/tests/gui/html/js/test_game.js index 18de1394..7e094d8b 100644 --- a/src/tests/gui/html/js/test_game.js +++ b/src/tests/gui/html/js/test_game.js @@ -358,4 +358,102 @@ describe('Game', () => { game.removePluginAtIndex(0); }); }); + + describe('#getContent()', () => { + let game; + + beforeEach(() => { + game = new loot.Game({}, l10n); + }); + + it('should return an object of two empty arrays if there is no game data', () => { + game.getContent().should.deepEqual({ + messages: [], + plugins: [], + }); + }); + + it('should return a structure containing converted plugin and message structures', () => { + game._globalMessages = [{ + type: 'say', + condition: 'file("foo.esp")', + content: [{ + lang: 'fr', + str: 'Bonjour le monde', + }], + }]; + game._plugins = [{ + name: 'foo', + crc: 0xDEADBEEF, + version: '1.0', + isActive: true, + isEmpty: true, + loadsArchive: true, + + masterlist: {}, + userlist: {}, + + priority: 500, + isPriorityGlobal: true, + messages: [{ + type: 'warn', + condition: 'file("bar.esp")', + content: [{ + lang: 'en', + str: 'Hello world', + }], + }], + tags: ['invalidStructure'], + isDirty: true, + + id: '', + _isEditorOpen: true, + isConflictFilterChecked: true, + _isSearchResult: true, + }]; + + game.getContent().should.deepEqual({ + messages: [{ + type: game._globalMessages[0].type, + content: game._globalMessages[0].content[0].str, + }], + plugins: [{ + name: game._plugins[0].name, + crc: game._plugins[0].crc, + version: game._plugins[0].version, + isActive: game._plugins[0].isActive, + isEmpty: game._plugins[0].isEmpty, + loadsArchive: game._plugins[0].loadsArchive, + + priority: game._plugins[0].priority, + isPriorityGlobal: game._plugins[0].isPriorityGlobal, + messages: game._plugins[0].messages, + tags: game._plugins[0].tags, + isDirty: game._plugins[0].isDirty, + }], + }); + }); + }); + + describe('#getPluginNames()', () => { + let game; + + beforeEach(() => { + game = new loot.Game({}, l10n); + }); + + it('should return an empty array if there are no plugins', () => { + game.getPluginNames().should.be.empty(); + }); + + it('should return an array of plugin filenames if there are plugins', () => { + game._plugins = [{ + name: 'foo', + isActive: true, + messages: [{ type: 'warn' }], + }]; + + game.getPluginNames().should.deepEqual(['foo']); + }); + }); });