Commits

Luke Plant committed 0896d2b

Ability to remove names from matrix

Comments (0)

Files changed (2)

static/connections.js

                   top: []};
     var groupSuffices = ["left", "top"];
 
+    function resetGroups () {
+        groups.left = [];
+        groups.top = [];
+    }
+
     function afterChange (changes, source) {
         if (source == "edit" || source == "autofill") {
             var newChanges = [];
     function addNamesToConnections (names) {
         var existingData = connectionsMatrix.getData();
         var newNames = names.slice();
-        for (i in newNames) {
+        for (var i in newNames) {
             if (newNames[i].match(/,/)) {
                 alert("Names must not contain the comma character");
-                return;
+                return false;
+            }
+            if (nameInConnections(newNames[i])) {
+                alert("Name '" + newNames[i] + "' is already in the matrix");
+                return false;
             }
         }
-        for (i in newNames) {
+        for (var i in newNames) {
             var newName = newNames[i];
 
             // Add a new row
             var templateRow = existingData[0].slice();
-            for (j in templateRow) {
+            for (var j in templateRow) {
                 if (j == 0) {
                     templateRow[j] = newName;
                 } else {
             existingData.push(templateRow);
 
             // Add a new column. Have to add item to each row.
-            for (j in existingData) {
-                row = existingData[j];
+            for (var j in existingData) {
+                var row = existingData[j];
                 if (j == 0) {
                     row.push(newName);
                 } else {
             var idx = existingData.length - 1;
             existingData[idx][idx] = "0";
         }
+        resetGroups();
         connectionsMatrix.loadData(existingData);
+        return true;
     };
 
+    function nameInConnections (name) {
+        var existingData = connectionsMatrix.getData();
+        for (var i in existingData) {
+            if (existingData[i][0] == name) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    function removeNamesFromConnections (names) {
+        var existingData = connectionsMatrix.getData();
+        var newNames = names.slice();
+
+        for (var i in newNames) {
+            var newName = newNames[i];
+            if (!nameInConnections(newName)) {
+                alert("Name '" + newName + "' is not in the matrix");
+                return false;
+            }
+        }
+
+        for (var i in newNames) {
+            var newName = newNames[i];
+            // Remove row
+            for (var j in existingData) {
+                var row = existingData[j];
+                if (row[0] == newName) {
+                    existingData.splice(j, 1);
+                    break;
+                }
+            }
+            // remove column
+            var headerRow = existingData[0];
+            var idx = headerRow.indexOf(newName);
+            for (var j in existingData) {
+                var row = existingData[j];
+                row.splice(idx, 1);
+            }
+        }
+        resetGroups();
+        connectionsMatrix.loadData(existingData);
+
+        return true;
+
+    }
+
     $("#add-names").click(function (ev) {
         var names = $("#names").val().trim().split(/\n/);
-        addNamesToConnections(names);
+        if (addNamesToConnections(names)) {
+            $('#names').val('');
+        }
+    });
+
+    $("#remove-names").click(function (ev) {
+        var names = $("#names-to-remove").val().trim().split(/\n/);
+        if (removeNamesFromConnections(names)) {
+            $("#names-to-remove").val('');
+        }
     });
 
     $("#show-raw-connections").click(function () {

templates/index.html

       <li>Enter the list of people who are at the event.</li>
       <li>Enter information about connections between these people.</li>
       <li>Enter other parameters that control the seating plan.</li>
-      <li>Possible seating plans are generated and displayed.</li>
+      <li>Click a button, and a possible seating plan is generated and displayed.</li>
     </ul>
 
-
     <p><strong>Please note:</strong> This app does not save any data. It is up
     to you to copy-paste data that you edit on this page.</p>
 
     <h2>Names</h2>
-    <p>Enter a list of names here:</p>
-    <textarea id="names" rows="20" cols="30"></textarea>
+    <table>
+      <tr>
+        <td>
+          <p>Names to add</p>
+          <textarea id="names" rows="20" cols="30"></textarea>
+          <p><input type="submit" id="add-names" value="Add to connections"></p>
+        </td>
+        <td>
+          <p>Names to remove:</p>
+          <textarea id="names-to-remove" rows="20" cols="30"></textarea>
+          <p><input type="submit" id="remove-names" value="Remove from connections"></p>
+        </td>
+      </tr>
+    </table>
 
-    <p><input type="submit" id="add-names" value="Add to connections"></p>
 
     <h2>Connections</h2>
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.