1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
2 // use this file except in compliance with the License. You may obtain a copy
5 // http://www.apache.org/licenses/LICENSE-2.0
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 // License for the specific language governing permissions and limitations under
13 // turn the form into deep json
14 // field names like 'author-email' get turned into json like
15 // {"author":{"email":"quentin@example.com"}}
16 // acts on doc by reference, so you can safely pass non-form fields through
18 function docForm(formSelector, opts) {
19 var localFormDoc = {};
21 opts.fields = opts.fields || [];
23 // turn the form into deep json
24 // field names like 'author-email' get turned into json like
25 // {"author":{"email":"quentin@example.com"}}
26 function formToDeepJSON(form, fields, doc) {
28 fields.forEach(function(field) {
29 var element = form.find("[name="+field+"]"),
30 parts = field.split('-'),
31 frontObj = doc, frontName = parts.shift();
33 if (element.attr('type') === 'checkbox') {
34 var val = element.attr('checked');
36 var val = element.val();
38 if (frontObj[field]) {
39 delete frontObj[field];
45 while (parts.length > 0) {
46 frontObj[frontName] = frontObj[frontName] || {};
47 frontObj = frontObj[frontName];
48 frontName = parts.shift();
50 frontObj[frontName] = val;
55 $(formSelector).submit(function(e) {
57 if (opts.validate && opts.validate() == false) { return false;}
58 // formToDeepJSON acts on localFormDoc by reference
59 formToDeepJSON(this, opts.fields, localFormDoc);
60 if (opts.beforeSave) {opts.beforeSave(localFormDoc);}
61 db.saveDoc(localFormDoc, {
62 success : function(resp) {
63 if (opts.success) {opts.success(resp, localFormDoc);}
70 // populate form from an existing doc
71 function docToForm(doc) {
72 var form = $(formSelector);
74 opts.fields.forEach(function(field) {
75 var parts = field.split('-');
76 var run = true, frontObj = doc, frontName = parts.shift();
77 while (frontObj && parts.length > 0) {
78 frontObj = frontObj[frontName];
79 frontName = parts.shift();
81 if (frontObj && frontObj[frontName]) {
82 var element = form.find("[name="+field+"]");
83 if (element.attr('type') === 'checkbox') {
84 element.attr('checked', frontObj[frontName]);
86 element.val(frontObj[frontName]);
94 attachPrevRev : opts.attachPrevRev,
96 if (opts.error) {opts.error.apply(opts, arguments);}
98 success: function(doc) {
99 if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(doc);}
103 } else if (opts.template) {
104 if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(opts.template);}
105 localFormDoc = opts.template;
106 docToForm(localFormDoc);
109 deleteDoc : function(opts) {
111 if (confirm("Really delete this document?")) {
112 db.removeDoc(localFormDoc, opts);
115 localDoc : function() {
116 formToDeepJSON(formSelector, opts.fields, localFormDoc);