You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2010/06/25 19:15:17 UTC
svn commit: r958039 - in /couchdb/branches/0.11.x/share/www: document.html
script/base64.js script/jquery.couch.js
Author: jan
Date: Fri Jun 25 17:15:16 2010
New Revision: 958039
URL: http://svn.apache.org/viewvc?rev=958039&view=rev
Log:
Backport r948262 from trunk:
add attachment versioning support to jquery.couch.js
Added:
couchdb/branches/0.11.x/share/www/script/base64.js
Modified:
couchdb/branches/0.11.x/share/www/document.html
couchdb/branches/0.11.x/share/www/script/jquery.couch.js
Modified: couchdb/branches/0.11.x/share/www/document.html
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/share/www/document.html?rev=958039&r1=958038&r2=958039&view=diff
==============================================================================
--- couchdb/branches/0.11.x/share/www/document.html [utf-8] (original)
+++ couchdb/branches/0.11.x/share/www/document.html [utf-8] Fri Jun 25 17:15:16 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/branches/0.11.x/share/www/script/base64.js
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/share/www/script/base64.js?rev=958039&view=auto
==============================================================================
--- couchdb/branches/0.11.x/share/www/script/base64.js (added)
+++ couchdb/branches/0.11.x/share/www/script/base64.js Fri Jun 25 17:15:16 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/branches/0.11.x/share/www/script/jquery.couch.js
URL: http://svn.apache.org/viewvc/couchdb/branches/0.11.x/share/www/script/jquery.couch.js?rev=958039&r1=958038&r2=958039&view=diff
==============================================================================
--- couchdb/branches/0.11.x/share/www/script/jquery.couch.js [utf-8] (original)
+++ couchdb/branches/0.11.x/share/www/script/jquery.couch.js [utf-8] Fri Jun 25 17:15:16 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) + "/",
@@ -310,6 +328,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",
@@ -318,6 +358,8 @@
},
saveDoc: function(doc, options) {
options = options || {};
+ var db = this;
+ var beforeSend = fullCommit(options);
if (doc._id === undefined) {
var method = "POST";
var uri = this.uri;
@@ -325,6 +367,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",
@@ -334,7 +377,17 @@
if (req.status == 200 || req.status == 201 || req.status == 202) {
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 {
@@ -554,6 +607,7 @@
options.ajaxStart(resp);
}
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 && resp.error || errorMessage, resp && resp.reason || "no response");