changeset 9:da404b8ff282

Container editing, persistent settings.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 10 Jan 2011 07:21:04 +0200
parents 5e1cdf22d647
children e2f975b0c518
files index.php
diffstat 1 files changed, 77 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Mon Jan 10 06:58:14 2011 +0200
+++ b/index.php	Mon Jan 10 07:21:04 2011 +0200
@@ -36,11 +36,22 @@
 
 function printInputField($id, $label, $len, $value = "")
 {
+  global $jsData;
+  $jsData .= "document.getElementById(\"".$id."\").value = \"".$value."\";\n";
+
   echo "<label for=\"".$id."\">".htmlentities($label).": </label>".
   "<input type=\"text\" name=\"".$id."\" maxlength=\"".$len.
   "\" size=\"".$len."\" id=\"".$id."\" value=\"".htmlentities($value)."\" />\n";
 }
 
+function printCheckBox($id, $label, $value = FALSE)
+{
+  global $jsData;
+  $jsData .= "document.getElementById(\"".$id."\").checked = ".($value ? "true" : "false").";\n";
+  
+  echo "<input type=\"checkbox\" id=\"".$id."\" value=\"".$value."\" ".($value ? "checked=\"checked\"" : "")." />".
+  "<label for=\"".$id."\">".htmlentities($label)."</label>";
+}
 
 function printOptionSelect($id, $size, $multi = FALSE)
 {
@@ -264,7 +275,8 @@
 printInputField("container_name", "Identifier/name", 15);
 printInputField("container_slots", "Number of slots", 5);
  ?>
-    <input type="button" value=" Create " onclick="containerCreate(this.form);" id="create_button" />
+    <input type="button" value=" Create new " onclick="containerCreate(this.form);" id="create_button" />
+    <input type="button" value=" Change " onclick="containerChange(this.form);" id="change_button" />
    </td>
 
    <td id="container_list_editor">
@@ -274,7 +286,6 @@
      <tr>
       <td><input type="button" value=" Switch to " onclick="containerEdit(this.form);" /></td>
       <td><input type="button" value=" Delete " onclick="containerDelete(this.form);" /></td>
-      <td><input type="button" value=" Edit " onclick="containerEditProps(this.form);" disabled="disabled" /></td>
      </tr>
     </table> 
    </td>
@@ -293,10 +304,8 @@
   <? printInputField("cmd_delim", "Command delimiter", 15, ";"); ?><br />
   <? printInputField("cmd_prefix", "Command name prefix", 15, "mcp"); ?><br />
   <? printInputField("cmd_cmd", "Command", 32, "put \$1 in \$2"); ?> (<b>$1</b> = item, <b>$2</b> = container)<br />
-  <input type="checkbox" id="cmd_sequence" value="true" />
-  <label for="cmd_sequence">Create sequence for each item, instead of command alias list.</label><br />
-  <input type="checkbox" id="cmd_autoclose" value="true" />
-  <label for="cmd_autoclose">Add commands for automatically opening and closing each container.</label><br />
+  <? printCheckBox("cmd_sequence", "Create sequence for each item, instead of command alias list.", FALSE); ?><br />
+  <? printCheckBox("cmd_autoclose", "Add commands for automatically opening and closing each container.", FALSE); ?><br />
   <br />
   <input type="button" value=" Create " onclick="createCommands('batmud');" />
   <input type="button" value=" Cancel " onclick="createInit('clear');" />
@@ -401,6 +410,7 @@
 {
   list_containers = [".$str."];
   list_items = ".(isset($data["items"]) ? getContainerObject("items", 10000, $data["items"]) : "null").";
+".$jsData."
   updatePage();
 }
 ";
@@ -492,39 +502,69 @@
 }
 
 
-// Create a new container, set current container to it
-function containerCreate(f)
+function validateContainerData(f)
 {
   var name = f.elements['container_name'].value;
   var slots = parseInt(f.elements['container_slots'].value, 10);
 
   if (isNaN(slots) || slots < 1) {
     statusMsg("Number of slots not set or is invalid.");
-    return;
+    return null;
   }
 
   if (name == "") {
     statusMsg("Empty container name "+name+".");
-    return;
+    return null;
   }
 
   if (name.match(/[^a-z0-9_]/)) {
     statusMsg("Invalid container name, only lower case alphanumerics and underscore are allowed.");
-    return;
+    return null;
   }
   
+  return {name: name, slots: slots};
+}
+
+// Create a new container, set current container to it
+function containerCreate(f)
+{
+  var c = validateContainerData(f);
+  if (c == null)
+    return;
+    
   for (var i = 0; i < list_containers.length; i++) {
-    if (list_containers[i].name == name) {
-      statusMsg("Container with identifier '<b>"+name+"</b>' already exists!");
+    if (list_containers[i].name == c.name) {
+      statusMsg("Container with identifier '<b>"+c.name+"</b>' already exists!");
       return;
     }
   }
 
-  curr_container = new Container(name, slots, []);
+  curr_container = new Container(c.name, c.slots, []);
   list_containers.push(curr_container);
 
   clearForm(f);
-  statusMsg("OK, created new container '<b>"+name+"</b>' with <b>"+slots+"</b> slots.");
+  statusMsg("Created new container '<b>"+c.name+"</b>' with <b>"+c.slots+"</b> slots.");
+  updatePage();
+}
+
+
+// Change current container
+function containerChange(f)
+{
+  var c = validateContainerData(f);
+  if (c == null)
+    return;
+
+  if (c.slots < curr_container.items.length) {
+    statusMsg("New number of slots can't be smaller than number of items in it! ("+ c.slots +" < "+ curr_container.items.length +")");
+    return;
+  }
+
+  curr_container.name = c.name;
+  curr_container.slots = c.slots;
+
+  clearForm(f);
+  statusMsg("Updated container.");
   updatePage();
 }
 
@@ -591,6 +631,28 @@
 }
 
 
+function containerRemoveItems(f)
+{
+  if (curr_container == null) {
+    statusMsg("Internal error.");
+    return;
+  }
+  
+  var selected = getSelectedItems(f.elements['curr_container']);
+  
+  if (selected.length == 0) {
+    statusMsg("No items selected for deletion.");
+    return;
+  } else {
+    for (var i = 0; i < selected.length; i++)
+      curr_container.moveItemById(list_items, selected[i]);
+
+    curr_container.flush();
+    updatePage();
+    statusMsg("Removed " + selected.length + " from current container.");
+  }
+}
+
 function getSelectedItems(items)
 {
   var selected = [];
@@ -658,28 +720,6 @@
 }
 
 
-function containerRemoveItems(f)
-{
-  if (curr_container == null) {
-    statusMsg("Internal error.");
-    return;
-  }
-  
-  var selected = getSelectedItems(f.elements['curr_container']);
-  
-  if (selected.length == 0) {
-    statusMsg("No items selected for deletion.");
-    return;
-  } else {
-    for (var i = 0; i < selected.length; i++)
-      curr_container.moveItemById(list_items, selected[i]);
-
-    curr_container.flush();
-    updatePage();
-    statusMsg("Removed " + selected.length + " from current container.");
-  }
-}
-
 var edit_modes = ['batmud', 'tf', 'save'];
 
 function createInit(mode)