Some more JS refactoring.

Moved a couple more event-related functions into events.js, moved all
helper functions into helpers.js, and renamed script.js to init.js.
This commit is contained in:
Oliver Hamlet
2015-01-15 11:07:34 +00:00
parent 84731e1f72
commit 62ec80a771
4 changed files with 161 additions and 162 deletions
+78
View File
@@ -301,6 +301,17 @@ function switchSidebarTab(evt) {
function showAboutDialog(evt) {
document.getElementById('about').showModal();
}
function areSettingsValid() {
/* Validate inputs individually. */
var inputs = document.getElementById('settingsDialog').getElementsByTagName('input');
for (var i = 0; i < inputs.length; ++i) {
if (!inputs[i].checkValidity()) {
return false;
console.log(inputs[i]);
}
}
return true;
}
function closeSettingsDialog(evt) {
if (evt.target.classList.contains('accept')) {
if (!areSettingsValid()) {
@@ -541,6 +552,73 @@ function jumpToGeneralInfo(evt) {
window.location.hash = '';
document.getElementById('main').scrollTop = 0;
}
function onContentRefresh(evt) {
/* Send a query for updated load order and plugin header info. */
showProgress(l10n.jed.translate('Refreshing data...').fetch());
loot.query('getGameData').then(function(result){
/* Parse the data sent from C++. */
try {
/* We don't want the plugin info creating cards, so don't convert
to plugin objects. */
var gameInfo = JSON.parse(result);
} catch (e) {
console.log(e);
console.log('getGameData response: ' + result);
}
/* Now overwrite plugin data with the newly sent data. Also update
card and li vars as they were unset when the game was switched
from before. */
var pluginNames = [];
gameInfo.plugins.forEach(function(plugin){
var foundPlugin = false;
for (var i = 0; i < loot.game.plugins.length; ++i) {
if (loot.game.plugins[i].name == plugin.name) {
loot.game.plugins[i].isActive = plugin.isActive;
loot.game.plugins[i].isEmpty = plugin.isEmpty;
loot.game.plugins[i].loadsBSA = plugin.loadsBSA;
loot.game.plugins[i].crc = plugin.crc;
loot.game.plugins[i].version = plugin.version;
loot.game.plugins[i].modPriority = plugin.modPriority;
loot.game.plugins[i].isGlobalPriority = plugin.isGlobalPriority;
loot.game.plugins[i].messages = plugin.messages;
loot.game.plugins[i].tags = plugin.tags;
loot.game.plugins[i].isDirty = plugin.isDirty;
foundPlugin = true;
break;
}
}
if (!foundPlugin) {
/* A new plugin. */
loot.game.plugins.push(new Plugin(plugin));
}
pluginNames.push(plugin.name);
});
for (var i = 0; i < loot.game.plugins.length;) {
var foundPlugin = false;
for (var j = 0; j < pluginNames.length; ++j) {
if (loot.game.plugins[i].name == pluginNames[j]) {
foundPlugin = true;
break;
}
}
if (!foundPlugin) {
/* Remove plugin. */
loot.game.plugins.splice(i, 1);
} else {
++i;
}
}
/* Reapply filters. */
setFilteredUIData();
closeProgressDialog();
}).catch(processCefError);
}
function setupEventHandlers() {
/*Set up handlers for filters.*/
document.getElementById('hideVersionNumbers').addEventListener('change', toggleDisplayCSS, false);
+81
View File
@@ -0,0 +1,81 @@
function processCefError(err) {
/* Error.stack seems to be Chromium-specific. It gives a lot more useful
info than just the error message. Also, this can be used to catch any
promise errors, not just CEF errors. */
console.log(err.stack);
closeProgressDialog();
showMessageBox(l10n.jed.translate('Error').fetch(), err.message);
}
function showElement(element) {
if (element != null) {
element.classList.toggle('hidden', false);
}
}
function hideElement(element) {
if (element != null) {
element.classList.toggle('hidden', true);
}
}
function toggleDisplayCSS(evt) {
var attr = 'data-hide-' + evt.target.getAttribute('data-class');
if (evt.target.checked) {
document.getElementById('main').setAttribute(attr, true);
} else {
document.getElementById('main').removeAttribute(attr);
}
}
function toast(text) {
var toast = document.getElementById('toast');
toast.text = text;
toast.show();
}
function showMessageDialog(title, text, positiveText, closeCallback) {
var dialog = document.createElement('loot-message-dialog');
dialog.setButtonText(positiveText, l10n.jed.translate('Cancel').fetch());
dialog.showModal(title, text, closeCallback);
document.body.appendChild(dialog);
}
function showMessageBox(title, text) {
var dialog = document.createElement('loot-message-dialog');
dialog.setButtonText(l10n.jed.translate('OK').fetch());
dialog.showModal(title, text);
document.body.appendChild(dialog);
}
function showProgress(message) {
var progressDialog = document.getElementById('progressDialog');
if (message) {
progressDialog.getElementsByTagName('p')[0].textContent = message;
}
if (!progressDialog.opened) {
progressDialog.showModal();
}
}
function closeProgressDialog() {
var progressDialog = document.getElementById('progressDialog');
if (progressDialog.opened) {
progressDialog.close();
}
}
function handleUnappliedChangesClose(change) {
showMessageDialog('', l10n.jed.translate('You have not yet applied or cancelled your %s. Are you sure you want to quit?').fetch(change), l10n.jed.translate('Quit').fetch(), function(result){
if (result) {
/* Cancel any sorting and close any editors. Cheat by sending a
cancelSort query for as many times as necessary. */
var queries = [];
var numQueries = 0;
if (!document.getElementById('applySortButton').classList.contains('hidden')) {
numQueries += 1;
}
numQueries += document.body.getAttribute('data-editors');
for (var i = 0; i < numQueries; ++i) {
queries.push(loot.query('cancelSort'));
}
Promise.all(queries).then(function(){
window.close();
}).catch(processCefError);
}
});
}
@@ -25,99 +25,6 @@
var marked;
var l10n;
function processCefError(err) {
/* Error.stack seems to be Chromium-specific. It gives a lot more useful
info than just the error message. Also, this can be used to catch any
promise errors, not just CEF errors. */
console.log(err.stack);
closeProgressDialog();
showMessageBox(l10n.jed.translate('Error').fetch(), err.message);
}
function showElement(element) {
if (element != null) {
element.classList.toggle('hidden', false);
}
}
function hideElement(element) {
if (element != null) {
element.classList.toggle('hidden', true);
}
}
function toggleDisplayCSS(evt) {
var attr = 'data-hide-' + evt.target.getAttribute('data-class');
if (evt.target.checked) {
document.getElementById('main').setAttribute(attr, true);
} else {
document.getElementById('main').removeAttribute(attr);
}
}
function toast(text) {
var toast = document.getElementById('toast');
toast.text = text;
toast.show();
}
function showMessageDialog(title, text, positiveText, closeCallback) {
var dialog = document.createElement('loot-message-dialog');
dialog.setButtonText(positiveText, l10n.jed.translate('Cancel').fetch());
dialog.showModal(title, text, closeCallback);
document.body.appendChild(dialog);
}
function showMessageBox(title, text) {
var dialog = document.createElement('loot-message-dialog');
dialog.setButtonText(l10n.jed.translate('OK').fetch());
dialog.showModal(title, text);
document.body.appendChild(dialog);
}
function showProgress(message) {
var progressDialog = document.getElementById('progressDialog');
if (message) {
progressDialog.getElementsByTagName('p')[0].textContent = message;
}
if (!progressDialog.opened) {
progressDialog.showModal();
}
}
function closeProgressDialog() {
var progressDialog = document.getElementById('progressDialog');
if (progressDialog.opened) {
progressDialog.close();
}
}
function areSettingsValid() {
/* Validate inputs individually. */
var inputs = document.getElementById('settingsDialog').getElementsByTagName('input');
for (var i = 0; i < inputs.length; ++i) {
if (!inputs[i].checkValidity()) {
return false;
console.log(inputs[i]);
}
}
return true;
}
function handleUnappliedChangesClose(change) {
showMessageDialog('', l10n.jed.translate('You have not yet applied or cancelled your %s. Are you sure you want to quit?').fetch(change), l10n.jed.translate('Quit').fetch(), function(result){
if (result) {
/* Cancel any sorting and close any editors. Cheat by sending a
cancelSort query for as many times as necessary. */
var queries = [];
var numQueries = 0;
if (!document.getElementById('applySortButton').classList.contains('hidden')) {
numQueries += 1;
}
numQueries += document.body.getAttribute('data-editors');
for (var i = 0; i < numQueries; ++i) {
queries.push(loot.query('cancelSort'));
}
Promise.all(queries).then(function(){
window.close();
}).catch(processCefError);
}
});
}
function initVars() {
loot.query('getVersion').then(function(result){
try {
@@ -267,79 +174,12 @@ function initVars() {
}).catch(processCefError);
}).catch(processCefError);
}
function onContentRefresh(evt) {
/* Send a query for updated load order and plugin header info. */
showProgress(l10n.jed.translate('Refreshing data...').fetch());
loot.query('getGameData').then(function(result){
/* Parse the data sent from C++. */
try {
/* We don't want the plugin info creating cards, so don't convert
to plugin objects. */
var gameInfo = JSON.parse(result);
} catch (e) {
console.log(e);
console.log('getGameData response: ' + result);
}
/* Now overwrite plugin data with the newly sent data. Also update
card and li vars as they were unset when the game was switched
from before. */
var pluginNames = [];
gameInfo.plugins.forEach(function(plugin){
var foundPlugin = false;
for (var i = 0; i < loot.game.plugins.length; ++i) {
if (loot.game.plugins[i].name == plugin.name) {
loot.game.plugins[i].isActive = plugin.isActive;
loot.game.plugins[i].isEmpty = plugin.isEmpty;
loot.game.plugins[i].loadsBSA = plugin.loadsBSA;
loot.game.plugins[i].crc = plugin.crc;
loot.game.plugins[i].version = plugin.version;
loot.game.plugins[i].modPriority = plugin.modPriority;
loot.game.plugins[i].isGlobalPriority = plugin.isGlobalPriority;
loot.game.plugins[i].messages = plugin.messages;
loot.game.plugins[i].tags = plugin.tags;
loot.game.plugins[i].isDirty = plugin.isDirty;
foundPlugin = true;
break;
}
}
if (!foundPlugin) {
/* A new plugin. */
loot.game.plugins.push(new Plugin(plugin));
}
pluginNames.push(plugin.name);
});
for (var i = 0; i < loot.game.plugins.length;) {
var foundPlugin = false;
for (var j = 0; j < pluginNames.length; ++j) {
if (loot.game.plugins[i].name == pluginNames[j]) {
foundPlugin = true;
break;
}
}
if (!foundPlugin) {
/* Remove plugin. */
loot.game.plugins.splice(i, 1);
} else {
++i;
}
}
/* Reapply filters. */
setFilteredUIData();
closeProgressDialog();
}).catch(processCefError);
}
window.addEventListener('polymer-ready', function(e) {
/* Set the plugin list's scroll target to its parent. */
document.getElementById('main').lastElementChild.scrollTarget = document.getElementById('main');
require(['bower_components/marked/lib/marked', 'js/l10n', 'js/plugin', 'js/loot', 'js/filters', 'js/events.js'], function(markedResponse, l10nResponse) {
require(['bower_components/marked/lib/marked', 'js/l10n', 'js/plugin', 'js/helpers', 'js/loot', 'js/filters', 'js/events'], function(markedResponse, l10nResponse) {
/* Register object observers. */
Object.observe(loot, loot.observer);
+1 -1
View File
@@ -298,5 +298,5 @@
<p>LOOT is free, but if you want to show your appreciation with some money, donations may be made to WrinklyNinja (LOOT's creator and main developer) using <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HDR3YBGGYCLBG">PayPal</a>, <a href="https://wrinklyninja.github.io/bitcoin/">Bitcoin</a> or <a href="https://flattr.com/submit/auto?user_id=OliverHamlet&url=https://wrinklyninja.github.io&title=LOOT">Flattr</a>.</p>
<paper-button affirmative autofocus>OK</paper-button>
</loot-dialog>
<script src="js/script.js"></script>
<script src="js/init.js"></script>
</body>