+
+ chrome.storage.local.set({
+ 'computers': computers,
+ });
+}
+
+/*
+ * If they try deleting all entries, make sure we re-add the default.
+ */
+function check_empty_computers(select) {
+ if (select.length == 0) {
+ var option = document.createElement('option');
+ option.text = 'Default';
+ select.add(option, 0);
+ load_computer(0);
+ }
+}
+
+/*
+ * Fill out the computer drop down with existing config options.
+ */
+function populate_computers() {
+ var select = $$('select[name=computer]');
+ select.length = 0;
+
+ for (var i = 0; i < computers.length; i++) {
+ var option = document.createElement('option');
+ var computer = computers[i] || {};
+ option.text = computer['name'] || default_name;
+ select.add(option, i);
+ }
+
+ check_empty_computers(select);
+}
+
+/*
+ * When a computer config is selected, load the corresponding settings.
+ */
+function select_computer() {
+ load_computer($$('select[name=computer]').selectedIndex);
+}
+
+/*
+ * Toggle between the computer drop down & new name input field.
+ */
+function toggle_add_fields(hide_obj, show_obj) {
+ hide_obj.disabled = true;
+ hide_obj.style.display = 'none';
+ show_obj.disabled = false;
+ show_obj.style.display = 'inline';
+ show_obj.focus();
+}
+
+/*
+ * Del curent slected computer
+ */
+function del_computer() {
+ var select = $$('select[name=computer]');
+
+ var idx = select.selectedIndex;
+ // Delete the currently selected index.
+ computers.splice(idx, 1);
+ select.remove(idx);
+
+ // Make sure the list isn't entirely empty now.
+ check_empty_computers(select);
+
+ // Load/select the next entry in the list.
+ if (idx == select.length && idx > 0)
+ --idx;
+ load_computer(idx);
+
+ store_settings();
+}
+
+
+/*
+ * Shows an input box to enter new computer name.
+ */
+function add_computer_start() {
+ var select = $$('select[name=computer]');
+ var text = $$('input[name=computer_name]');
+
+ // If the box isn't visible, show it. Otherwise, they want to
+ // actually add the current settings so create a new entry.
+ if (select.style.display == 'none')
+ add_computer();
+ else
+ toggle_add_fields(select, text);
+}
+
+/*
+ * Wait for the enter key in the add text field.
+ */
+function add_computer_check(e) {
+ if (e.key == 'Enter') {
+ add_computer();
+ e.preventDefault();
+ }
+}
+
+/*
+ * Try to actually create a new computer entry.
+ */
+function add_computer() {
+ var select = $$('select[name=computer]');
+ var form = $$('form[name=settings]');
+ var text = $$('input[name=computer_name]');
+
+ var name = text.value.trim();
+ // Make sure they've added a valid name first.
+ // Options fields don't allow leading/trailing whitespace.
+ if (name == '') {
+ text.value = '';
+ toggle_add_fields(text, select);
+ return;
+ }
+
+ // Make sure they don't try to add a duplicate name.
+ for (var i = 0; i < select.length; ++i) {
+ if (select.options[i].value == name) {
+ popup_msg(text, 'ERROR: computer name already exists!');
+ return;
+ }
+ }
+ text.value = '';
+
+ var option = document.createElement('option');
+ option.text = name;
+ select.add(option, -1);
+
+ // Let the load_computer logic fill out the default values for us.
+ var idx = select.length - 1;
+ computers[idx] = {
+ 'name': name,
+ };
+ load_computer(select.length - 1);
+
+ toggle_add_fields(text, select);
+
+ store_settings();