changeset 4:cf3ff3dc306f

Client-side upload widget creation
author Adam Chlipala <adam@chlipala.net>
date Thu, 29 Dec 2011 16:07:28 -0500
parents 6ad01456dc2e
children 9dca7b936311
files examples/client.ur examples/client.urp examples/client.urs src/js/ajaxUpload.js src/ur/lib.urp
diffstat 5 files changed, 82 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/client.ur	Thu Dec 29 16:07:28 2011 -0500
@@ -0,0 +1,52 @@
+(* Note that this will only work if you copy src/js/ajaxUpload.js to /tmp! *)
+
+sequence ids
+
+table images : { Id : int, MimeType : string, Content : blob }
+  PRIMARY KEY Id
+
+fun choice b =
+    widget <- source <xml/>;
+    status <- source <xml/>;
+    
+    return <xml><body>
+      <button value="Create new widget"
+              onclick={au <- AjaxUpload.render {SubmitLabel = if b then None else Some "Upload it!",
+                                                OnBegin = set status <xml>Uploading</xml>,
+                                                OnSuccess = fn h =>
+                                                               let
+                                                                   fun addImage () =
+                                                                       r <- AjaxUpload.claim h;
+                                                                       case r of
+                                                                           AjaxUpload.NotFound => return None
+                                                                         | AjaxUpload.Found r =>
+                                                                           id <- nextval ids;
+                                                                           dml (INSERT INTO images (Id, MimeType, Content)
+                                                                                VALUES ({[id]}, {[r.MimeType]}, {[r.Content]}));
+                                                                           return (Some id)
+                                                               in
+                                                                   ido <- rpc (addImage ());
+                                                                   case ido of
+                                                                       None => alert "Newly uploaded image not found!"
+                                                                     | Some id =>
+                                                                       let
+                                                                           fun image () =
+                                                                               r <- oneRow1 (SELECT images.MimeType, images.Content
+                                                                                             FROM images
+                                                                                             WHERE images.Id = {[id]});
+                                                                               returnBlob r.Content (blessMime r.MimeType)
+                                                                       in
+                                                                           set status <xml><img src={url (image ())}/></xml>
+                                                                       end
+                                                               end};
+                       set widget au}/>
+      <hr/>
+      <dyn signal={signal widget}/>
+      <hr/>
+      <dyn signal={signal status}/>
+    </body></xml>
+
+fun main () = return <xml><body>
+  <a link={choice False}>Normal</a><br/>
+  <a link={choice True}>Auto-submit</a>
+</body></xml>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/client.urp	Thu Dec 29 16:07:28 2011 -0500
@@ -0,0 +1,8 @@
+library ../src/ur
+rewrite url Client/*
+database dbname=test
+sql client.sql
+allow mime image/jpeg
+script http://localhost/ajaxUpload.js
+
+client
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/client.urs	Thu Dec 29 16:07:28 2011 -0500
@@ -0,0 +1,1 @@
+val main : {} -> transaction page
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/js/ajaxUpload.js	Thu Dec 29 16:07:28 2011 -0500
@@ -0,0 +1,19 @@
+function tweakForm(autoSubmit, iframeId, submitId) {
+    return "<iframe id=\""
+        + iframeId
+        + "\" name=\""
+        + iframeId
+        + "\" src=\"#\" style=\"width:0;height:0;border:0px solid #fff;\"></iframe>\n<script type=\"text/javascript\">var subm = document.getElementById(\""
+        + submitId
+        + "\"); subm.parentNode.target = \""
+        + iframeId
+        + "\"; if (subm.begin == undefined) { subm.begin = subm.onmousedown; subm.onmousedown = undefined; } subm.parentNode.onsubmit = function() { subm.begin(); return true; }; if (subm.withHandle == undefined) { subm.withHandle = subm.onkeydown; subm.onkeydown = undefined; } "
+        + (autoSubmit
+           ? "subm.style.visibility = \"hidden\"; for (var node = subm.previousSibling; node.tagName != \"INPUT\"; node = node.previousSibling); node.onchange = function() { subm.parentNode.submit(); }; "
+           : "")
+        + "</script>";
+}
+
+function idToString(x) {
+    return x;
+}
--- a/src/ur/lib.urp	Thu Dec 29 15:46:15 2011 -0500
+++ b/src/ur/lib.urp	Thu Dec 29 16:07:28 2011 -0500
@@ -1,7 +1,8 @@
 library config
 ffi ajaxUploadFfi
 link -lurweb_ajaxUpload
-jsFunc AjaxUploadFfi.getHandle=getHandle
+jsFunc AjaxUploadFfi.tweakForm=tweakForm
+jsFunc AjaxUploadFfi.idToString=idToString
 
 $/option
 ajaxUpload