Commits

Luke Plant committed a41316c

Added uploading of connections

Comments (0)

Files changed (2)

static/connections.js

         $('#download-container').append('<iframe height="1", width="1" frameborder="0" src="' + $SCRIPT_ROOT + '/download-form/"></iframe>');
     }
 
+    function uploadConnections () {
+        var xhr = new XMLHttpRequest();
+        var file = $("#upload-selector").get(0).files[0];
+        xhr.file = file;
+        xhr.onreadystatechange = function(e) {
+            if (this.readyState == 4) {
+                console.log(['xhr upload complete', e]);
+                $("#connections-raw").val(xhr.response);
+                updateMatrixFromRaw();
+            }
+        };
+        xhr.open('post', $SCRIPT_ROOT + "/upload-connections/", true);
+        xhr.send(file);
+    }
 
 
     // --- Wiring ---
         updateMatrixFromRaw();
     });
 
-    $("#download-connections").click(function () {
+    $("#download-connections").click(function (ev) {
         downloadConnections();
     });
 
+    $("#upload-connections").click(function (ev) {
+        ev.preventDefault();
+        uploadConnections();
+    });
+
     function makeGroupUnique (group) {
         // Has to edit the array in place, not return a new one.
         var seen = [];
     return jsonify({'solution': normalise_plan(planning_data.plan_to_people(plan))})
 
 
+
+# Download and upload are just echoing back data that is there client side,
+# but this mechanism allows us to make use of:
+#
+# * file selection
+# * automatic saving of downloads in most browsers (using Content-Disposition)
+
+
 @app.route('/download-form/')
 def download_form():
     return render_template('download_iframe.html')
     response.set_data(request.form.get('data'))
     return response
 
+
+@app.route('/upload-connections/', methods=['POST'])
+def upload_connections():
+    response = Response(mimetype="text/plain")
+    response.set_data(request.data)
+    return response
+
+
 if __name__ == '__main__':
     app.run(debug=True)