You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by jc...@apache.org on 2010/05/26 02:08:42 UTC

svn commit: r948262 - in /couchdb/trunk: NOTICE share/www/document.html share/www/script/base64.js share/www/script/jquery.couch.js

Author: jchris
Date: Wed May 26 00:08:42 2010
New Revision: 948262

URL: http://svn.apache.org/viewvc?rev=948262&view=rev
Log:
add attachment versioning support to jquery.couch.js

Added:
    couchdb/trunk/share/www/script/base64.js
Modified:
    couchdb/trunk/NOTICE
    couchdb/trunk/share/www/document.html
    couchdb/trunk/share/www/script/jquery.couch.js

Modified: couchdb/trunk/NOTICE
URL: http://svn.apache.org/viewvc/couchdb/trunk/NOTICE?rev=948262&r1=948261&r2=948262&view=diff
==============================================================================
--- couchdb/trunk/NOTICE (original)
+++ couchdb/trunk/NOTICE Wed May 26 00:08:42 2010
@@ -41,3 +41,7 @@ This product also includes the following
  * mimeparse.js (http://code.google.com/p/mimeparse/)
 
    Copyright 2009, Chris Anderson <jc...@apache.org>
+
+ * base64.js
+
+   Copyright 1999, Masanao Izumo <iz...@onicos.co.jp>
\ No newline at end of file

Modified: couchdb/trunk/share/www/document.html
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/document.html?rev=948262&r1=948261&r2=948262&view=diff
==============================================================================
--- couchdb/trunk/share/www/document.html [utf-8] (original)
+++ couchdb/trunk/share/www/document.html [utf-8] Wed May 26 00:08:42 2010
@@ -20,6 +20,7 @@ specific language governing permissions 
     <link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
     <script src="script/json2.js"></script>
     <script src="script/sha1.js"></script>
+    <script src="script/base64.js"></script>
     <script src="script/jquery.js?1.4.2"></script>
     <script src="script/jquery.couch.js?0.11.0"></script>
     <script src="script/jquery.dialog.js?0.11.0"></script>

Added: couchdb/trunk/share/www/script/base64.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/base64.js?rev=948262&view=auto
==============================================================================
--- couchdb/trunk/share/www/script/base64.js (added)
+++ couchdb/trunk/share/www/script/base64.js Wed May 26 00:08:42 2010
@@ -0,0 +1,124 @@
+/* Copyright (C) 1999 Masanao Izumo <iz...@onicos.co.jp>
+ * Version: 1.0
+ * LastModified: Dec 25 1999
+ * This library is free.  You can redistribute it and/or modify it.
+ */
+ /* Modified by Chris Anderson to not use CommonJS */
+ /* Modified by Dan Webb not to require Narwhal's binary library */
+
+var Base64 = {};
+(function(exports) {
+  
+  var encodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+  var decodeChars = [
+      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
+      52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
+      -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
+      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
+      -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
+  ];
+
+  exports.encode = function (str) {
+      var out, i, length;
+      var c1, c2, c3;
+
+      length = len(str);
+      i = 0;
+      out = [];
+      while(i < length) {
+          c1 = str.charCodeAt(i++) & 0xff;
+          if(i == length)
+          {
+              out.push(encodeChars.charCodeAt(c1 >> 2));
+              out.push(encodeChars.charCodeAt((c1 & 0x3) << 4));
+              out.push("=".charCodeAt(0));
+              out.push("=".charCodeAt(0));
+              break;
+          }
+          c2 = str.charCodeAt(i++);
+          if(i == length)
+          {
+              out.push(encodeChars.charCodeAt(c1 >> 2));
+              out.push(encodeChars.charCodeAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)));
+              out.push(encodeChars.charCodeAt((c2 & 0xF) << 2));
+              out.push("=".charCodeAt(0));
+              break;
+          }
+          c3 = str.charCodeAt(i++);
+          out.push(encodeChars.charCodeAt(c1 >> 2));
+          out.push(encodeChars.charCodeAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)));
+          out.push(encodeChars.charCodeAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)));
+          out.push(encodeChars.charCodeAt(c3 & 0x3F));
+      }
+
+      var str = ""; 
+      out.forEach(function(chr) { str += String.fromCharCode(chr) });
+      return str;
+  };
+
+  exports.decode = function (str) {
+      var c1, c2, c3, c4;
+      var i, length, out;
+
+      length = len(str);
+      i = 0;
+      out = [];
+      while(i < length) {
+          /* c1 */
+          do {
+              c1 = decodeChars[str.charCodeAt(i++) & 0xff];
+          } while(i < length && c1 == -1);
+          if(c1 == -1)
+              break;
+
+          /* c2 */
+          do {
+              c2 = decodeChars[str.charCodeAt(i++) & 0xff];
+          } while(i < length && c2 == -1);
+          if(c2 == -1)
+              break;
+
+          out.push(String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)));
+
+          /* c3 */
+          do {
+              c3 = str.charCodeAt(i++) & 0xff;
+              if(c3 == 61)
+                  return out.join('');
+              c3 = decodeChars[c3];
+          } while(i < length && c3 == -1);
+          if(c3 == -1)
+              break;
+
+          out.push(String.fromCharCode(((c2 & 0xF) << 4) | ((c3 & 0x3C) >> 2)));
+
+          /* c4 */
+          do {
+              c4 = str.charCodeAt(i++) & 0xff;
+              if(c4 == 61)
+                  return out.join('');
+              c4 = decodeChars[c4];
+          } while(i < length && c4 == -1);
+
+          if(c4 == -1)
+              break;
+
+          out.push(String.fromCharCode(((c3 & 0x03) << 6) | c4));
+      }
+
+      return out.join('');
+  };
+
+  var len = function (object) {
+      if (object.length !== undefined) {
+          return object.length;
+      } else if (object.getLength !== undefined) {
+          return object.getLength();
+      } else {
+          return undefined;
+      }
+  };
+})(Base64);

Modified: couchdb/trunk/share/www/script/jquery.couch.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/jquery.couch.js?rev=948262&r1=948261&r2=948262&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/jquery.couch.js [utf-8] (original)
+++ couchdb/trunk/share/www/script/jquery.couch.js [utf-8] Wed May 26 00:08:42 2010
@@ -153,7 +153,25 @@
       });
     },
 
-    db: function(name) {
+    db: function(name, db_opts) {
+      db_opts = db_opts || {};
+      var rawDocs = {};
+      function maybeApplyVersion(doc) {
+        if (doc._id && doc._rev && rawDocs[doc._id] && rawDocs[doc._id].rev == doc._rev) {
+          // todo: can we use commonjs require here?
+          if (typeof Base64 == "undefined") {
+            alert("please include /_utils/script/base64.js in the page for base64 support");
+            return false;
+          } else {
+            doc._attachments = doc._attachments || {};
+            doc._attachments["rev-"+doc._rev] = {
+              content_type :"application/json",
+              data : Base64.encode(rawDocs[doc._id].raw)
+            }
+            return true;
+          }
+        }
+      };
       return {
         name: name,
         uri: this.urlPrefix + "/" + encodeURIComponent(name) + "/",
@@ -251,6 +269,28 @@
           }
         },
         openDoc: function(docId, options, ajaxOptions) {
+          options = options || {};
+          if (db_opts.attachPrevRev || options.attachPrevRev) {
+            $.extend(options, {
+              beforeSuccess : function(req, doc) {
+                rawDocs[doc._id] = {
+                  rev : doc._rev,
+                  raw : req.responseText
+                };
+              }
+            });
+          } else {
+            $.extend(options, {
+              beforeSuccess : function(req, doc) {
+                if (doc["jquery.couch.attachPrevRev"]) {
+                  rawDocs[doc._id] = {
+                    rev : doc._rev,
+                    raw : req.responseText
+                  };
+                }
+              }
+            });
+          }
           ajax({url: this.uri + encodeDocId(docId) + encodeOptions(options)},
             options,
             "The document could not be retrieved",
@@ -258,8 +298,9 @@
           );
         },
         saveDoc: function(doc, options) {
-          var beforeSend = fullCommit(options);
           options = options || {};
+          var db = this;
+          var beforeSend = fullCommit(options);
           if (doc._id === undefined) {
             var method = "POST";
             var uri = this.uri;
@@ -267,6 +308,7 @@
             var method = "PUT";
             var uri = this.uri + encodeDocId(doc._id);
           }
+          var versioned = maybeApplyVersion(doc);
           $.ajax({
             type: method, url: uri + encodeOptions(options),
             contentType: "application/json",
@@ -277,7 +319,17 @@
               if (req.status == 201) {
                 doc._id = resp.id;
                 doc._rev = resp.rev;
-                if (options.success) options.success(resp);
+                if (versioned) {
+                  db.openDoc(doc._id, {
+                    attachPrevRev : true,
+                    success : function(d) {
+                      doc._attachments = d._attachments;
+                      if (options.success) options.success(resp);
+                    }
+                  });
+                } else {
+                  if (options.success) options.success(resp);
+                }
               } else if (options.error) {
                 options.error(req.status, resp.error, resp.reason);
               } else {
@@ -465,6 +517,7 @@
       complete: function(req) {
         var resp = $.httpData(req, "json");
         if (req.status == options.successStatus) {
+          if (options.beforeSuccess) options.beforeSuccess(req, resp);
           if (options.success) options.success(resp);
         } else if (options.error) {
           options.error(req.status, resp.error, resp.reason);