Browse files

Switch over to modernized options handling.

  • Loading branch information...
1 parent 93ceeed commit 1e8d4efad1593e41b780fe0a696d90f688976e13 @drheld committed Feb 12, 2012
Showing with 108 additions and 90 deletions.
  1. +5 −0 common.js
  2. +5 −7 dominion.js
  3. +1 −2 login.js
  4. +4 −3 manifest.json
  5. +76 −78 options.js
  6. +17 −0 styles.css
View
5 common.js
@@ -0,0 +1,5 @@
+// Returns if an option is set.
+function getOption(name) {
+ return localStorage[name] == "true";
+}
+
View
12 dominion.js
@@ -781,7 +781,7 @@ function initialize(doc) {
disabled = true;
}
- if (!disabled && localStorage["always_display"] != "f") {
+ if (!disabled && getOption("always_display")) {
updateScores();
updateDeck();
}
@@ -854,7 +854,7 @@ function maybeIntroducePlugin() {
writeText("http://goo.gl/iDihS");
writeText("Type !status to see the current score.");
writeText("Type !details to see deck details for each player.");
- if (localStorage["allow_disable"] != "f") {
+ if (getOption("allow_disable")) {
writeText("Type !disable by turn 5 to disable the point counter.");
}
}
@@ -917,9 +917,7 @@ function handleChatText(speaker, text) {
if (text == " !status") delayedRunCommand("maybeShowStatus");
if (text == " !details") delayedRunCommand("maybeShowDetails");
- if (localStorage["allow_disable"] != "f" &&
- text == " !disable" &&
- turn_number <= 5) {
+ if (getOption("allow_disable") && text == " !disable" && turn_number <= 5) {
localStorage.setItem("disabled", "t");
disabled = true;
hideExtension();
@@ -1126,7 +1124,7 @@ function handle(doc) {
doc.childNodes[2].nodeValue);
}
- if (localStorage["always_display"] != "f") {
+ if (getOption("always_display") {
if (!disabled) {
updateScores();
updateDeck();
@@ -1159,7 +1157,7 @@ function buildStatusMessage() {
}
function enterLobby() {
- if (localStorage["status_announce"] == "t" &&
+ if (getOption("status_announce") &&
$('#lobby').length != 0 && $('#lobby').css('display') != "none") {
// Set the original status message.
writeText(buildStatusMessage());
View
3 login.js
@@ -1,6 +1,5 @@
// Set the initial status message to the recorded one if we're handling status.
-if (localStorage["status_announce"] == "t" &&
- localStorage["status_msg"] != undefined) {
+if (getOption("status_announce") && localStorage["status_msg"] != undefined) {
document.getElementsByName("status")[0].value = localStorage["status_msg"];
}
View
7 manifest.json
@@ -1,19 +1,20 @@
{
"content_scripts": [
{
- "js": [ "jquery.js", "card_list.js", "dominion.js" ],
+ "js": [ "jquery.js", "common.js", "card_list.js", "dominion.js" ],
"matches": [ "http://dominion.isotropic.org/play" ]
},
{
"js": [ "jquery.js", "card_list.js", "dominion.js", "debugger.js" ],
"matches": [ "http://*.dominion-point-counter.appspot.com/debug*" ]
},
{
- "js": [ "jquery.js", "options.js" ],
+ "js": [ "jquery.js", "common.js", "options.js" ],
+ "css": [ "styles.css" ],
"matches": [ "http://dominion.isotropic.org/" ]
},
{
- "js": [ "login.js" ],
+ "js": [ "jquery.js", "common.js", "login.js" ],
"matches": [ "http://dominion.isotropic.org/*loggedin*" ]
}
],
View
154 options.js
@@ -1,90 +1,88 @@
-function setupOption(default_value, name) {
- var enable = localStorage[name];
- if (enable == undefined) {
- enable = default_value;
+var optionButtons = {
+ allow_disable: {text: "Allow opponents to disable point counter with !disable."},
+ status_announce: {text: "Change lobby status to announce you use point counter.",
+ extra: "Mandatory if disabling is not allowed."},
+ always_display: {text: "Replace exit/faq with scores and card counts."},
+ show_card_counts: {text: "Show every player's card counts for each card."},
+};
+
+
+function setOption(name, value) {
+ localStorage[name] = value;
+ $('#' + name).attr('checked', value);
+}
+
+function initializeOption(name, default_value) {
+ var value = localStorage[name];
+
+ // If it's not set, set it to the default.
+ if (value == undefined) localStorage[name] = default_value;
+
+ // Move forward deprecated options.
+ if (value == 't') localStorage[name] = 'true';
+ if (value == 'f') localStorage[name] = 'false';
+}
+
+function initializeOptions() {
+ initializeOption('allow_disable', true);
+ initializeOption('status_announce', false);
+ initializeOption('always_display', true);
+ initializeOption('show_card_counts', true);
+
+ // Sanity check the options.
+ if (!getOption('allow_disable') && !getOption('status_announce')) {
+ alert('Allowing disabling.\n' +
+ 'If you do not want to allow disabling, please enable lobby status ' +
+ 'and turn off this setting.');
+ setOption('status_announce', 'true');
}
+}
- var name_to_select = document.getElementById(name + "_" + enable);
- name_to_select.checked = true
+function onButtonChange(evt) {
+ var button = $(evt.target);
+ setOption(button.attr('id'), button.attr('checked'));
}
-function loadOptions() {
- setupOption("t", "allow_disable");
- setupOption("f", "status_announce");
- setupOption("t", "always_display");
-
- // Sanity check the options. There were bugs in enforcing this.
- // If disabling is not allowed, require status announce.
- if (localStorage["allow_disable"] == "f") {
- if (localStorage["status_announce"] != "t") {
- alert("Enabling post in status message.\n" +
- "This setting was lost due to a bug.\n\n" +
- "If you do not want to post in status message, " +
- "please allow disabling and turn off this setting.");
- localStorage["status_announce"] = "t";
- $('#status_announce_t').attr('checked', true);
- }
- $('#status_announce_t').attr('disabled', true);
- $('#status_announce_f').attr('disabled', true);
+function createOptionButton(name, section, option) {
+ var option_desc = option.text;
+ if (option.extra) {
+ option_desc += ' <span class="optionNote">(' + option.extra + ')</span>';
}
+
+ var control = $('<label/>').attr('for', name);
+ var button = $('<input type="checkbox"/>').attr('id', name).attr('name', name);
+
+ button.change(onButtonChange);
+ control.append(button).append(option_desc);
+ button.attr('checked', getOption(name));
+ option.node = button;
+ section.append(control);
}
-function generateOptionButton(name, value, desc) {
- var id = name + "_" + value;
- return "<label for='" + id + "'>" +
- "<input type='radio' name='" + name + "' id='" + id + "'" +
- "onclick='saveOption(\"" + name + "\", \"" + value + "\")'>" +
- desc +
- "</label><br>";
+function onDisableButtonChange() {
+ var allow_disable = getOption('allow_disable');
+ optionButtons['status_announce'].node.attr('disabled', allow_disable == false);
+ if (!allow_disable) setOption('status_announce', true);
}
-function generateOption(option_desc, extra_desc, name, yes_desc, no_desc) {
- if (extra_desc != "") {
- extra_desc += '<div style="line-height:6px;">&nbsp;</div>';
+function buildOptionsSection() {
+ initializeOptions();
+
+ var section = $('<div/>').attr('id', 'optionPanel');
+ section.append('<h3>Dominion Point Counter Options</h3>');
+
+ for (var opt in optionButtons) {
+ createOptionButton(opt, section, optionButtons[opt]);
}
- return "<h3>" + option_desc + "</h3>" + extra_desc +
- generateOptionButton(name, "t", yes_desc) +
- generateOptionButton(name, "f", no_desc);
+
+ // Make sure people are either disableable or show status message.
+ var disable_button = optionButtons['allow_disable'].node;
+ disable_button.change(function() { onDisableButtonChange(); });
+ onDisableButtonChange();
+
+ return section;
}
-var js_element = document.createElement("script");
-js_element.id = "pointCounterOptionsJavascript";
-js_element.type = "text/javascript";
-js_element.innerHTML = "function saveOption(name, value) { localStorage[name] = value; }"
-document.body.appendChild(js_element);
-
-var element = document.createElement("div");
-element.id = "pointCounterOptions";
-
-element.innerHTML =
- "<h1>Dominion Point Counter Options</h1>" +
- generateOption("Allow opponents to disable point counter with !disable?",
- "",
- "allow_disable",
- "Allow disabling.",
- "Do not allow disabling.") +
- generateOption("Change lobby status to announce you use point counter?",
- "Mandatory if disabling is not allowed.",
- "status_announce",
- "Post in status message.",
- "Do not post in status message.") +
- generateOption("Always display counts / points?",
- "",
- "always_display",
- "Replace exit/faq with scores.",
- "Only display in chat box from !status command.");
-
-document.body.appendChild(element);
-loadOptions();
-
-$('#allow_disable_t').click(function() {
- $('#status_announce_t').attr('disabled', false);
- $('#status_announce_f').attr('disabled', false);
-})
-
-$('#allow_disable_f').click(function() {
- localStorage["status_announce"] = "t";
- $('#status_announce_t').attr('checked', true);
- $('#status_announce_t').attr('disabled', true);
- $('#status_announce_f').attr('disabled', true);
-})
+$(document).ready(function() {
+ $(document.body).append(buildOptionsSection());
+});
View
17 styles.css
@@ -0,0 +1,17 @@
+/*
+ * Styles for the options page.
+ */
+#optionPanel {
+ background-color: #dbf8db;
+ padding: 0.5em;
+ width: 100%;
+}
+#optionPanel {
+ display: block;
+}
+#optionPanel .optionNote {
+ font-style: italic;
+}
+#optionPanel > label {
+ display: block;
+}

0 comments on commit 1e8d4ef

Please sign in to comment.