summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarren Smith <garren.smith@gmail.com>2014-01-21 21:57:23 +0200
committerGarren Smith <garren.smith@gmail.com>2014-01-22 12:01:33 +0200
commit4f180704762faff5cc7407ee62013873734ded80 (patch)
tree6cd08a2f954af774cda2a3e393df924d889b9ffd
parent15f4fff02882bdfe879ddbbeba447677d1b18fc1 (diff)
downloadcouchdb-id-ace-warning.tar.gz
Fauxton: block user from deleting _id and _rev from docid-ace-warning
-rw-r--r--src/fauxton/app/addons/documents/views.js33
-rw-r--r--src/fauxton/app/addons/fauxton/components.js4
2 files changed, 37 insertions, 0 deletions
diff --git a/src/fauxton/app/addons/documents/views.js b/src/fauxton/app/addons/documents/views.js
index d5319f39d..702beeeaa 100644
--- a/src/fauxton/app/addons/documents/views.js
+++ b/src/fauxton/app/addons/documents/views.js
@@ -959,6 +959,7 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
this.editor = new Components.Editor({
editorId: "editor-container",
+ forceMissingId: true,
commands: [{
name: 'save',
bindKey: {win: 'Ctrl-S', mac: 'Ctrl-S'},
@@ -970,6 +971,38 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
});
this.editor.render();
this.model.on("sync", this.updateValues, this);
+
+ var editor = this.editor,
+ model = this.model;
+
+ editor.editor.on("change", function (event) {
+ //if (event.data.action !== 'removeText') { return; }
+ //if (!event.data.text.match(/_id/) && !event.data.text.match(/_rev/)) { return; }
+
+ var changedDoc;
+ try {
+ changedDoc = JSON.parse(editor.getValue());
+ } catch(exception) {
+ //not complete doc. Cannot work with it
+ return;
+ }
+
+ var keyChecked = ["_id"];
+ if (model.get("_rev")) { keyChecked.push("_rev");}
+
+ //check the changedDoc has all the required standard keys
+ if (_.isEmpty(_.difference(keyChecked, _.keys(changedDoc)))) { return; }
+
+ editor.setReadOnly(true);
+ setTimeout(function () { editor.setReadOnly(false);}, 400);
+ // use extend so that _id stays at the top of the object with displaying the doc
+ changedDoc = _.extend({_id: model.id, _rev: model.get("_rev")}, changedDoc);
+ editor.setValue(JSON.stringify(changedDoc, null, " "));
+ FauxtonAPI.addNotification({
+ type: "error",
+ msg: "Cannot remove a documents Id or Revision."
+ });
+ });
},
cleanup: function () {
diff --git a/src/fauxton/app/addons/fauxton/components.js b/src/fauxton/app/addons/fauxton/components.js
index cda61c590..6ff0ce372 100644
--- a/src/fauxton/app/addons/fauxton/components.js
+++ b/src/fauxton/app/addons/fauxton/components.js
@@ -313,6 +313,10 @@ function(app, FauxtonAPI, ace) {
this.edited = false;
},
+ setReadOnly: function (value) {
+ return this.editor.setReadOnly(value);
+ },
+
setValue: function (data, lineNumber) {
lineNumber = lineNumber ? lineNumber : -1;
this.editor.setValue(data, lineNumber);