Refactor conflicts filter code

This commit is contained in:
Oliver Hamlet
2016-06-04 17:01:43 +01:00
parent 6a3ec1717a
commit f1f1563fca
4 changed files with 72 additions and 69 deletions
+7 -10
View File
@@ -407,8 +407,12 @@ namespace loot {
YAML::Node node;
auto plugin = _lootState.CurrentGame().GetPlugin(pluginName);
for (const auto& otherPlugin : _lootState.CurrentGame().GetPlugins()) {
YAML::Node pluginNode;
// Plugin loading may have produced an error message, so rederive
// displayed data.
YAML::Node pluginNode = GenerateDerivedMetadata(otherPlugin.Name());
pluginNode["name"] = otherPlugin.Name();
pluginNode["crc"] = otherPlugin.Crc();
pluginNode["isEmpty"] = otherPlugin.IsEmpty();
if (plugin.DoFormIDsOverlap(otherPlugin)) {
@@ -419,20 +423,13 @@ namespace loot {
pluginNode["conflicts"] = false;
}
// Plugin loading may have produced an error message, so rederive displayed data.
YAML::Node derivedNode = GenerateDerivedMetadata(otherPlugin.Name());
for (const auto &pair : derivedNode) {
const string key = pair.first.as<string>();
pluginNode[key] = pair.second;
}
node[otherPlugin.Name()] = pluginNode;
node.push_back(pluginNode);
}
if (node.size() > 0)
callback->Success(JSON::stringify(node));
else
callback->Success("null");
callback->Success("[]");
}
void Handler::CopyMetadata(const std::string& pluginName) {
+14 -6
View File
@@ -21,7 +21,7 @@ function onChangeGame(evt) {
filter. Don't need to deactivate the others beforehand. Strictly not
deactivating the conflicts filter either, just resetting it's value.
*/
loot.filters.conflictTargetPluginName = undefined;
loot.filters.deactivateConflictsFilter();
/* Clear the UI of all existing game-specific data. Also
clear the card and li variables for each plugin object. */
@@ -403,9 +403,8 @@ function onEditorClose(evt) {
}).catch(loot.handlePromiseError);
}
function undoConflictsFilter() {
const wasConflictsFilterEnabled = (loot.filters.conflictTargetPluginName);
const wasConflictsFilterEnabled = loot.filters.deactivateConflictsFilter();
loot.filters.conflictTargetPluginName = undefined;
/* Deactivate any existing plugin conflict filter. */
loot.game.plugins.forEach((plugin) => {
plugin.isConflictFilterChecked = false;
@@ -424,10 +423,19 @@ function onConflictsFilter(evt) {
/* evt.detail is true if the filter has been activated. */
if (evt.detail) {
evt.target.data.isConflictFilterChecked = true;
loot.filters.conflictTargetPluginName = evt.target.getName();
evt.target.classList.toggle('highlight', true);
} else {
loot.filters.conflictTargetPluginName = undefined;
/* Now get conflicts for the plugin. */
loot.Dialog.showProgress(loot.l10n.translate('Identifying conflicting plugins...'));
loot.filters.activateConflictsFilter(evt.target.getName()).then((plugins) => {
plugins.forEach((plugin) => {
const gamePlugin = loot.game.plugins.find(item => item.name === plugin.name);
if (gamePlugin) {
gamePlugin.update(plugin);
}
});
loot.Dialog.closeProgress();
}).catch(loot.handlePromiseError);
}
filterPluginData(loot.game.plugins, loot.filters);
}
+30 -2
View File
@@ -6,9 +6,10 @@
} else {
// Browser globals
root.loot = root.loot || {};
root.loot.Filters = factory();
root.loot.Filters = factory(root.loot.query,
root.loot.handlePromiseError);
}
}(this, () => class {
}(this, (query, handlePromiseError) => class {
constructor(l10n) {
/* Plugin filters */
this.hideMessagelessPlugins = false;
@@ -66,4 +67,31 @@
return true;
}
deactivateConflictsFilter() {
const wasEnabled = (this.conflictingPluginNames);
this.conflictingPluginNames = [];
return wasEnabled;
}
activateConflictsFilter(targetPluginName) {
if (!targetPluginName) {
return Promise.resolve([]);
}
/* Filter everything but the plugin itself if there are no
conflicts. */
this.conflictingPluginNames = [targetPluginName];
return query('getConflictingPlugins', targetPluginName).then(JSON.parse).then((plugins) => {
plugins.forEach((plugin) => {
if (plugin.conflicts) {
this.conflictingPluginNames.push(plugin.name);
}
});
return plugins;
}).catch(handlePromiseError);
}
}));
+21 -51
View File
@@ -1,58 +1,28 @@
'use strict';
function getConflictingPlugins(pluginName) {
if (!pluginName) {
return Promise.resolve([]);
}
/* Now get conflicts for the plugin. */
loot.Dialog.showProgress(loot.l10n.translate('Identifying conflicting plugins...'));
return loot.query('getConflictingPlugins', pluginName).then(JSON.parse).then((result) => {
const conflicts = [pluginName];
if (result) {
/* Filter everything but the plugin itself if there are no
conflicts. */
for (const key in result) {
if (result[key].conflicts) {
conflicts.push(key);
}
const plugin = loot.game.plugins.find(item => item.name === key);
if (plugin) {
plugin.update(result[key]);
}
}
}
loot.Dialog.closeProgress();
return conflicts;
}).catch(loot.handlePromiseError);
}
function filterPluginData(plugins, filters) {
getConflictingPlugins(filters.conflictTargetPluginName).then((conflictingPluginNames) => {
filters.conflictingPluginNames = conflictingPluginNames;
return plugins.filter(filters.pluginFilter, filters);
}).then((filteredPlugins) => {
document.getElementById('cardsNav').items = filteredPlugins;
document.getElementById('pluginCardList').items = filteredPlugins;
const filteredPlugins = plugins.filter(filters.pluginFilter, filters);
const pluginCards = document.getElementById('pluginCardList').children;
for (let i = 0; i < pluginCards.length; ++i) {
if (pluginCards[i].data) {
pluginCards[i].updateContent(true);
}
document.getElementById('cardsNav').items = filteredPlugins;
document.getElementById('pluginCardList').items = filteredPlugins;
const pluginCards = document.getElementById('pluginCardList').children;
for (let i = 0; i < pluginCards.length; ++i) {
if (pluginCards[i].data) {
pluginCards[i].updateContent(true);
}
document.getElementById('cardsNav').notifyResize();
document.getElementById('pluginCardList').notifyResize();
}
document.getElementById('cardsNav').notifyResize();
document.getElementById('pluginCardList').notifyResize();
/* Now perform search again. If there is no current search, this won't
do anything. */
document.getElementById('searchBar').search();
/* Now perform search again. If there is no current search, this won't
do anything. */
document.getElementById('searchBar').search();
/* Re-count all hidden plugins and messages. */
document.getElementById('hiddenPluginNo').textContent = plugins.length - filteredPlugins.length;
let hiddenMessageNo = 0;
plugins.forEach((plugin) => {
hiddenMessageNo += plugin.messages.length - plugin.getCardContent(filters).messages.length;
});
document.getElementById('hiddenMessageNo').textContent = hiddenMessageNo;
}).catch(loot.handlePromiseError);
/* Re-count all hidden plugins and messages. */
document.getElementById('hiddenPluginNo').textContent = plugins.length - filteredPlugins.length;
let hiddenMessageNo = 0;
plugins.forEach((plugin) => {
hiddenMessageNo += plugin.messages.length - plugin.getCardContent(filters).messages.length;
});
document.getElementById('hiddenMessageNo').textContent = hiddenMessageNo;
}