Refactor misc. event handling code

This commit is contained in:
Oliver Hamlet
2016-05-28 08:32:55 +01:00
parent 4368d8a737
commit ca9c888983
3 changed files with 146 additions and 43 deletions
+11 -43
View File
@@ -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(() => {
+37
View File
@@ -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);
+98
View File
@@ -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']);
});
});
});