// submit-turtle-as.js
/*
   Copyright (c) 2011 3 Round Stones Inc, Some Rights Reserved
   Licensed under the Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
*/

(function($){

var calli = window.calli || (window.calli={});

calli.submitTurtleAs = function(event, fileName, create, folder) {
    var button = calli.fixEvent(event).target;
    var form = $(button).closest('form');
    var btn = $(button).filter('button');
    var resource = form.attr("about") || form.attr("resource") || '';
    var local = fileName || localPart(resource);
    btn.button('loading');
    return calli.promptForNewResource(folder, local).then(function(two){
        if (!two) return undefined;
        var url = two[0] + '?create=' + encodeURIComponent(create);
        var iri = two[0].replace(/\/?$/, '/') + two[1].replace(/%20/g, '+');
        form.attr("resource", iri);
        try {
            return calli.copyResourceData(form);
        } finally {
            if (resource) {
                form.attr("resource", resource);
            }
        }
    }).then(function(data){
        if (!data) return data;
        data.results.bindings.push({
            s: {type:'uri', value: data.head.link[0]},
            p: {type:'uri', value: 'http://purl.org/dc/terms/created'},
            o: {
                type:'literal',
                value: new Date().toISOString(),
                datatype: "http://www.w3.org/2001/XMLSchema#dateTime"
            }
        });
        return data;
    }).then(function(data){
        if (!data) return data;
        return calli.createTurtle(url, data);
    }).then(function(redirect){
        if (redirect) {
            window.location.href = redirect;
        } else {
            btn.button('reset');
        }
    }, function(error){
        btn.button('reset');
        return calli.error(error);
    });
};


function localPart(resource) {
    if (resource)
        return resource.replace(/.*\/(.+)/, '$1');
    return null;
}

})(jQuery);


@@ -837,24 +837,76 @@
 20/g, '+');%0A
+        return calli.resolve().then(function()%7B%0A    
         form
@@ -941,14 +941,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1008,16 +1008,20 @@
         
+    
 %7D finall
@@ -1020,24 +1020,28 @@
 %7D finally %7B%0A
+    
             
@@ -1064,32 +1064,36 @@
                 
+  
+  
 form.attr(%22resou
@@ -1113,34 +1113,42 @@
 e);%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A    %7D)
@@ -1137,24 +1137,28 @@
           %7D%0A
+    
     %7D).then(
@@ -1173,32 +1173,36 @@
 (data)%7B%0A        
+ 
+   
 if (!data) retur
@@ -1213,24 +1213,28 @@
 ta;%0A        
+  
+  
 data.results
@@ -1250,16 +1250,20 @@
 .push(%7B%0A
+    
         
@@ -1309,16 +1309,20 @@
 nk%5B0%5D%7D,%0A
+    
         
@@ -1401,13 +1401,21 @@
     
+    
 o: %7B%0A
+    
     
@@ -1458,16 +1458,20 @@
         
+  
+  
 value: n
@@ -1495,16 +1495,20 @@
 ring(),%0A
+    
         
@@ -1569,16 +1569,20 @@
 teTime%22%0A
+    
         
@@ -1595,20 +1595,28 @@
         
-%7D);%0A
+    %7D);%0A    
         
@@ -1628,24 +1628,28 @@
 n data;%0A    
+  
+  
 %7D).then(func
@@ -1660,32 +1660,36 @@
 (data)%7B%0A        
+   
+ 
 if (!data) retur
@@ -1688,32 +1688,36 @@
 a) return data;%0A
+    
         return c
@@ -1721,22 +1721,20 @@
 n calli.
-create
+post
 Turtle(u
@@ -1744,16 +1744,28 @@
  data);%0A
+        %7D);%0A
     %7D).t

Fix calli.submitTurtleAs