diff --git a/src/gui/html/js/events.js b/src/gui/html/js/events.js index 66ea3bf5..d4c6895d 100644 --- a/src/gui/html/js/events.js +++ b/src/gui/html/js/events.js @@ -52,12 +52,7 @@ function updateMasterlist() { result.plugins.forEach((resultPlugin) => { const existingPlugin = loot.game.plugins.find(plugin => plugin.name === resultPlugin.name); if (existingPlugin) { - existingPlugin.isDirty = resultPlugin.isDirty; - existingPlugin.isPriorityGlobal = resultPlugin.isPriorityGlobal; - existingPlugin.masterlist = resultPlugin.masterlist; - existingPlugin.messages = resultPlugin.messages; - existingPlugin.priority = resultPlugin.priority; - existingPlugin.tags = resultPlugin.tags; + existingPlugin.update(resultPlugin); } }); @@ -107,8 +102,7 @@ function onSortPlugins() { item.name === plugin.name )); if (existingPlugin) { - existingPlugin.crc = plugin.crc; - existingPlugin.isEmpty = plugin.isEmpty; + existingPlugin.update(plugin); } }); /* Send discardUnappliedChanges query. Not doing so prevents LOOT's window @@ -123,8 +117,7 @@ function onSortPlugins() { result.plugins.forEach((plugin) => { let existingPlugin = loot.game.plugins.find(item => item.name === plugin.name); if (existingPlugin) { - existingPlugin.crc = plugin.crc; - existingPlugin.isEmpty = plugin.isEmpty; + existingPlugin.update(plugin); } else { existingPlugin = new loot.Plugin(plugin); } @@ -190,11 +183,7 @@ function onClearAllMetadata() { existingPlugin.userlist = undefined; existingPlugin.editor = undefined; - existingPlugin.priority = plugin.priority; - existingPlugin.isPriorityGlobal = plugin.isPriorityGlobal; - existingPlugin.messages = plugin.messages; - existingPlugin.tags = plugin.tags; - existingPlugin.isDirty = plugin.isDirty; + existingPlugin.update(plugin); } }); @@ -384,11 +373,7 @@ function onEditorClose(evt) { const edits = evt.target.readFromEditor(plugin); promise = loot.query('editorClosed', edits).then(JSON.parse).then((result) => { if (result) { - plugin.priority = result.priority; - plugin.isPriorityGlobal = result.isPriorityGlobal; - plugin.messages = result.messages; - plugin.tags = result.tags; - plugin.isDirty = result.isDirty; + plugin.update(result); plugin.userlist = edits.userlist; @@ -466,11 +451,7 @@ function onClearMetadata(evt) { existingPlugin.userlist = undefined; existingPlugin.editor = undefined; - existingPlugin.priority = plugin.priority; - existingPlugin.isPriorityGlobal = plugin.isPriorityGlobal; - existingPlugin.messages = plugin.messages; - existingPlugin.tags = plugin.tags; - existingPlugin.isDirty = plugin.isDirty; + existingPlugin.update(plugin); } loot.Dialog.showNotification(loot.l10n.translate('The user-added metadata for "%s" has been cleared.', evt.target.getName())); /* Now perform search again. If there is no current search, this won't diff --git a/src/gui/html/js/helpers.js b/src/gui/html/js/helpers.js index 3e05f70b..13d3fce2 100644 --- a/src/gui/html/js/helpers.js +++ b/src/gui/html/js/helpers.js @@ -24,12 +24,7 @@ function getConflictingPlugins(pluginName) { } const plugin = loot.game.plugins.find(item => item.name === key); if (plugin) { - plugin.crc = result[key].crc; - plugin.isEmpty = result[key].isEmpty; - - plugin.messages = result[key].messages; - plugin.tags = result[key].tags; - plugin.isDirty = result[key].isDirty; + plugin.update(result[key]); } } } diff --git a/src/gui/html/js/plugin.js b/src/gui/html/js/plugin.js index a27d888f..743b5542 100644 --- a/src/gui/html/js/plugin.js +++ b/src/gui/html/js/plugin.js @@ -189,6 +189,19 @@ this._isSearchResult = false; } + update(plugin) { + if (!plugin) { + return; + } + if (plugin.name !== this.name) { + throw new Error(`Cannot update ${this.name}'s data using data for ${plugin.name}`); + } + + Object.getOwnPropertyNames(plugin).forEach((property) => { + this[property] = plugin[property]; + }); + } + static fromJson(key, value) { if (value !== null && value.__type === 'Plugin') { return new Plugin(value); diff --git a/src/tests/gui/html/js/test_plugin.js b/src/tests/gui/html/js/test_plugin.js index b386a51e..550d2d47 100644 --- a/src/tests/gui/html/js/test_plugin.js +++ b/src/tests/gui/html/js/test_plugin.js @@ -275,6 +275,61 @@ describe('Plugin', () => { }); }); + describe('#update()', () => { + let plugin; + const updatedPlugin = { + name: 'test', + foo: 'bar', + crc: 0xDEADBEEF, + }; + + beforeEach(() => { + plugin = new loot.Plugin({ name: 'test' }); + }); + + it('should do nothing if its argument is undefined', () => { + plugin.update(); + + plugin.should.deepEqual(new loot.Plugin({ name: 'test' })); + }); + + it('should throw if the argument has no name property', () => { + should(() => { plugin.update({}); }).throw(Error); + }); + + it('should throw if the argument\'s name property doesn\'t match the plugin\'s name', () => { + should(() => { plugin.update({ name: 'other test' }); }).throw(Error); + }); + + it('should set property values for all the given argument\'s properties', () => { + plugin.update(updatedPlugin); + + plugin.foo.should.equal(updatedPlugin.foo); + plugin.crc.should.equal(updatedPlugin.crc); + }); + + it('should not change property values for properties not present in the argument', () => { + plugin.isActive = true; + + plugin.update(updatedPlugin); + + plugin.foo.should.equal(updatedPlugin.foo); + plugin.crc.should.equal(updatedPlugin.crc); + plugin.isActive.should.be.true(); + }); + + it('should set explicitly undefined values', () => { + plugin.isActive = true; + + plugin.update({ + name: plugin.name, + isActive: undefined, + }); + + should(plugin.isActive).be.undefined(); + }); + }); + describe('#fromJson()', () => { it('should return the value object if the JSON is not of the Plugin type', () => { const testInputObj = {