changeset 7:6ff1ee9122b2

Work more on generalization of the process.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 10 Jan 2011 06:43:45 +0200
parents 2fb901161013
children 5e1cdf22d647
files index.php
diffstat 1 files changed, 69 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Mon Jan 10 05:43:16 2011 +0200
+++ b/index.php	Mon Jan 10 06:43:45 2011 +0200
@@ -58,13 +58,18 @@
  <style type=\"text/css\">
   <!--
   .buttons { width: 100%; }
-  div.options { display: none; border: 1px solid white; padding: 1em; }
+  select { width: 100%; }
+  
+  div.options, #commands, #status {
+    background: #268;
+    border: 1px solid white;
+  }
+  
+  div.options { display: none; position: fixed; top: 2em; left: 2em; padding: 1em; }
 
-  select { width: 100%; }
-    
-  #commands { display: none; position: fixed; top: 2em; left: 2em; background: black; padding: 1em; z-index: 15; }
+  #commands { display: none; position: fixed; top: 2em; left: 2em; padding: 1em; z-index: 15; }
   
-  #status { position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 1pt; margin: 2pt; border: 1px solid white; }
+  #status { position: absolute; bottom: 1em; right: 1em; left: 1em; padding: 2pt; z-index: -1; }
   -->
  </style>
 ");
@@ -287,16 +292,18 @@
   <? 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_separate" value="true" />
-  <label for="cmd_separate">Create one command for each item, instead of a list.</label><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 />
   <br />
   <input type="button" value=" Create " onclick="createCommands('batmud');" />
+  <input type="button" value=" Cancel " onclick="createInit('clear');" />
 </div>
 
 <div id="options_tf" class="options">
   <input type="button" value=" Create " onclick="createCommands('tf');" />
+  <input type="button" value=" Cancel " onclick="createInit('clear');" />
 </div>
 
 
@@ -326,8 +333,12 @@
   }
 
   this.addItem = function(item) {
-    this.changed = true;
-    return this.items.push(item);
+    if (this.items.length < slots) {
+      this.changed = true;
+      this.items.push(item);
+      return true;
+    } else
+      return false;
   }
 
   this.deleteItemById = function(id) {
@@ -348,10 +359,13 @@
   this.moveItemById = function(container, id) {
     var item = this.getItemByIndex(id);
     if (item != null) {
+      if (!container.addItem(item))
+        return false;
       this.deleteItemById(id);
-      container.addItem(item);
       this.changed = true;
-    }
+      return true;
+    } else
+      return false;
   }
 
   this.getItemId = function(item) {
@@ -529,8 +543,12 @@
   if (curr_container == ret.container)
     curr_container = null;
 
-  for (var i = 0; i < len; i++)
-    ret.container.moveItemById(list_items, i);
+  for (var i = 0; i < len; i++) {
+    if (!ret.container.moveItemById(list_items, i)) {
+      statusMsg("Internal error moving item "+ i);
+      return;
+    }
+  }
 
   list_containers.splice(ret.id, 1);
       
@@ -609,8 +627,12 @@
       return;
     }
   
-    for (var i = 0; i < selected.length; i++)
-      list_items.moveItemById(curr_container, selected[i]);
+    for (var i = 0; i < selected.length; i++) {
+      if (!list_items.moveItemById(curr_container, selected[i])) {	
+        statusMsg("Internal error moving item #"+i+": '"+selected[i]+"'.");
+        return;
+      }
+    }
 
     list_items.flush();
     updatePage();
@@ -656,6 +678,13 @@
 }
 
 
+function expandCmd(cmd, item, name)
+{
+  var str = cmd.replace(/\$1/g, item);
+  return str.replace(/\$2/g, name);
+}
+
+
 function createCommands(mode)
 {
   if (list_containers.length == 0) {
@@ -667,7 +696,6 @@
   var str = "";
   for (var i = 0; i < list_containers.length; i++) {
     var container = list_containers[i];
-    var s = "";
     var counts = [];
     var curr = [];
 
@@ -677,28 +705,39 @@
       curr[container.items[n]] = 1;
     }
 
+    var cmd_autoclose = document.getElementById("cmd_autoclose").checked;
+    var cmd_delim = document.getElementById("cmd_delim").value;
+    var cmd_prefix = document.getElementById("cmd_prefix").value;
+    if (cmd_delim == "") cmd_delim = ";";
+    var cmd_cmd = document.getElementById("cmd_cmd").value;
+    var cmd_seq = document.getElementById("cmd_sequence").checked;
+    var s = "";
+
     for (var n = 0; n < container.items.length; n++) {
       var item = container.items[n];
+      var tmp = ""+item;
+
+      if (counts[item] > 1) {
+        tmp += " " + curr[item];
+        curr[item]++;
+      }
+
       if (mode == "batmud") {
-        if (s != "") s += ",";
-        s += item;
-        if (counts[item] > 1) {
-          s += " " + curr[item];
-          curr[item]++;
+        if (cmd_seq) {
+          s += expandCmd(cmd_cmd, tmp, container.name) + cmd_delim;
+        } else {
+          if (s != "") s += ",";
+          s += tmp;
         }
       }
     }
     
     if (mode == "batmud") {
-      var opt = document.getElementById("cmd_autoclose").checked;
-      var delim = document.getElementById("cmd_delim").value;
-      var prefix = document.getElementById("cmd_prefix").value;
-      if (delim == "") delim = ";";
-
-      str += "command "+prefix+container.name+" ";
-      if (opt) str += "open "+container.name+delim;
-      str += "put "+s+" in "+container.name;
-      if (opt) str += delim+"close "+container.name;
+      if (!cmd_seq)      str += "command "+cmd_prefix+container.name+" ";
+      if (cmd_autoclose) str += "open "+container.name+delim;
+      if (!cmd_seq)      str += expandCmd(cmd_cmd, s, container.name);
+        else 		 str += s;
+      if (cmd_autoclose) str += delim+"close "+container.name;
       str += "\n";
     }
   }