Add scripts to create myops-getqueryview:
[myops.git] / web / query / vendor / couchapp / lib / docform.js
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
3 // of the License at
4 //
5 //   http://www.apache.org/licenses/LICENSE-2.0
6 //
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
11 // the License.
12
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
17
18 function docForm(formSelector, opts) {
19   var localFormDoc = {};
20   opts = opts || {};
21   opts.fields = opts.fields || [];
22
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) {
27     form = $(form);
28     fields.forEach(function(field) {
29       var element = form.find("[name="+field+"]"),
30           parts = field.split('-'),
31           frontObj = doc, frontName = parts.shift();
32
33       if (element.attr('type') === 'checkbox') {
34           var val = element.attr('checked');
35       } else {
36           var val = element.val();
37           if (!val) {
38             if (frontObj[field]) {
39                 delete frontObj[field];
40             }
41             return;
42           }
43       }
44       
45       while (parts.length > 0) {
46         frontObj[frontName] = frontObj[frontName] || {};
47         frontObj = frontObj[frontName];
48         frontName = parts.shift();
49       }
50       frontObj[frontName] = val;
51     });
52   }
53   
54   // Apply the behavior
55   $(formSelector).submit(function(e) {
56     e.preventDefault();
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);}
64       }
65     });
66     
67     return false;
68   });
69
70   // populate form from an existing doc
71   function docToForm(doc) {
72     var form = $(formSelector);
73     // fills in forms
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();
80       }
81       if (frontObj && frontObj[frontName]) {
82         var element = form.find("[name="+field+"]");
83         if (element.attr('type') === 'checkbox') {
84           element.attr('checked', frontObj[frontName]);
85         } else {
86           element.val(frontObj[frontName]);
87         }
88       }
89     });
90   }
91   
92   if (opts.id) {
93     db.openDoc(opts.id, {
94       attachPrevRev : opts.attachPrevRev,
95       error: function() {
96         if (opts.error) {opts.error.apply(opts, arguments);}
97       },
98       success: function(doc) {
99         if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(doc);}
100         localFormDoc = doc;
101         docToForm(doc);
102     }});
103   } else if (opts.template) {
104     if (opts.load || opts.onLoad) {(opts.load || opts.onLoad)(opts.template);}
105     localFormDoc = opts.template;
106     docToForm(localFormDoc);
107   }
108   var instance = {
109     deleteDoc : function(opts) {
110       opts = opts || {};
111       if (confirm("Really delete this document?")) {                
112         db.removeDoc(localFormDoc, opts);
113       }
114     },
115     localDoc : function() {
116       formToDeepJSON(formSelector, opts.fields, localFormDoc);
117       return localFormDoc;
118     }
119   };
120   return instance;
121 }