From f6507e21f87f962c7e7731123450914aa12e227d Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Wed, 22 Jul 2009 09:03:40 +0000 Subject: Change document creation in Futon so that it no longer prompts for a document ID, but simply opens the document page for an empty (and unsaved) document, and allows you to modify the ID. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@796635 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/futon.browse.js | 98 ++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 44 deletions(-) (limited to 'share/www/script/futon.browse.js') diff --git a/share/www/script/futon.browse.js b/share/www/script/futon.browse.js index 538a747b..85e9a0ec 100644 --- a/share/www/script/futon.browse.js +++ b/share/www/script/futon.browse.js @@ -122,20 +122,8 @@ ruby: "{|doc|\n emit(nil, doc);\n}" } - this.addDocument = function() { - $.showDialog("dialog/_create_document.html", { - submit: function(data, callback) { - db.saveDoc(data.docid ? {_id: data.docid} : {}, { - error: function(status, error, reason) { - callback({docid: reason}); - }, - success: function(resp) { - location.href = "document.html?" + encodeURIComponent(dbName) + - "/" + $.couch.encodeDocId(resp.id); - } - }); - } - }); + this.newDocument = function() { + location.href = "document.html?" + encodeURIComponent(db.name); } this.compactDatabase = function() { @@ -655,16 +643,23 @@ CouchDocumentPage: function() { var urlParts = location.search.substr(1).split("/"); var dbName = decodeURIComponent(urlParts.shift()); - var idParts = urlParts.join("/").split("@", 2); - var docId = decodeURIComponent(idParts[0]); - var docRev = (idParts.length > 1) ? idParts[1] : null; + if (urlParts.length) { + var idParts = urlParts.join("/").split("@", 2); + var docId = decodeURIComponent(idParts[0]); + var docRev = (idParts.length > 1) ? idParts[1] : null; + this.isNew = false; + } else { + var docId = $.couch.newUUID(); + var docRev = null; + this.isNew = true; + } var db = $.couch.db(dbName); this.dbName = dbName; this.db = db; this.docId = docId; this.doc = null; - this.isDirty = false; + this.isDirty = this.isNew; page = this; this.activateTabularView = function() { @@ -752,27 +747,32 @@ } } - db.openDoc(docId, {revs_info: true, - success: function(doc) { - var revs = doc._revs_info || []; - delete doc._revs_info; - if (docRev != null) { - db.openDoc(docId, {rev: docRev, - error: function(status, error, reason) { - alert("The requested revision was not found. " + - "You will be redirected back to the latest revision."); - location.href = "?" + encodeURIComponent(dbName) + - "/" + $.couch.encodeDocId(docId); - }, - success: function(doc) { - handleResult(doc, revs); - } - }); - } else { - handleResult(doc, revs); + if (!page.isNew) { + db.openDoc(docId, {revs_info: true, + success: function(doc) { + var revs = doc._revs_info || []; + delete doc._revs_info; + if (docRev != null) { + db.openDoc(docId, {rev: docRev, + error: function(status, error, reason) { + alert("The requested revision was not found. You will " + + "be redirected back to the latest revision."); + location.href = "?" + encodeURIComponent(dbName) + + "/" + $.couch.encodeDocId(docId); + }, + success: function(doc) { + handleResult(doc, revs); + } + }); + } else { + handleResult(doc, revs); + } } - } - }); + }); + } else { + handleResult({_id: docId}, []); + $("#fields tbody td").dblclick(); + } } this.deleteDocument = function() { @@ -796,7 +796,7 @@ success: function(resp) { page.isDirty = false; location.href = "?" + encodeURIComponent(dbName) + - "/" + $.couch.encodeDocId(docId); + "/" + $.couch.encodeDocId(page.docId); } }); } @@ -824,7 +824,7 @@ form.find("#progress").css("visibility", "hidden"); page.isDirty = false; location.href = "?" + encodeURIComponent(dbName) + - "/" + $.couch.encodeDocId(docId); + "/" + $.couch.encodeDocId(page.docId); } }); } @@ -899,7 +899,7 @@ } function _initValue(doc, row, fieldName) { - if (fieldName == "_id" || fieldName == "_rev") { + if ((fieldName == "_id" && !page.isNew) || fieldName == "_rev") { return; } @@ -920,8 +920,13 @@ } }, accept: function(newValue) { - doc[row.data("name")] = JSON.parse(newValue); + var fieldName = row.data("name"); + doc[fieldName] = JSON.parse(newValue); page.isDirty = true; + if (fieldName == "_id") { + page.docId = page.doc._id = doc[fieldName]; + $("h1 strong").text(page.docId); + } }, populate: function(value) { return $.futon.formatJSON(doc[row.data("name")]); @@ -929,7 +934,12 @@ validate: function(value) { $("div.error", this).remove(); try { - JSON.parse(value); + var parsed = JSON.parse(value); + if (row.data("name") == "_id" && typeof(parsed) != "string") { + $("
The document ID must be a string.
") + .appendTo(this); + return false; + } return true; } catch (err) { var msg = err.message; @@ -977,7 +987,7 @@ } function _renderAttachmentItem(name, attachment) { - var attachmentHref = db.uri + $.couch.encodeDocId(docId) + var attachmentHref = db.uri + $.couch.encodeDocId(page.docId) + "/" + encodeAttachment(name); var li = $("
  • "); $("").text(name) -- cgit v1.2.3