View change clears "unsaved" counter
[ajatus.git] / js / ajatus.forms.js
blob8f8ba88cc61120529246905b501a79f973ee3f99
1 (function($){
2     $.ajatus = $.ajatus || {};
4     $.ajatus.forms = {
5         
6     };
7     $.extend($.ajatus.forms, {
8         register: {
9             normal: function(form) {
10                 $.ajatus.events.named_lock_pool.increase('unsaved');
11                 
12                     jqform = jQuery(form);
13                     jqform.bind('submit',function(e){
14                         return false;
15                     });
17                     $('input[@type=submit]', jqform).bind('click', function(){
18                     //$.ajatus.process_form(jqform.formToArray(false));
19                     $.ajatus.forms.process.normal(jqform.formToArray(false));
20                     });
21             },
22             ajax: function(form) {
23                 $.ajatus.events.named_lock_pool.increase('unsaved');
24                 
25                     jqform = jQuery(form);
26                     jqform.bind('submit',function(e){
27                         return false;
28                     });
30                     $('input[@type=submit]', jqform).bind('click', function(){
31                     $.ajatus.forms.process.ajax(jqform.formToArray(false));
32                     });
33             }
34         },        
35         process: {
36             doc: null,
37             common: function(form_data) {
38                     var doc = {
39                         _id: '',
40                         _rev: '',
41                         value: {}
42                     };
43                     var form_values = {};
44                     var form_id = '';
45                     var doc_type = 'note';
47                     $.each(form_data, function(i,row){
48                         //console.log('i: '+i+' row.name: '+row.name+' row.value: '+row.value);
50                         if (   row.name == '_id'
51                             && (   row.value != undefined
52                                 && row.value != ''))
53                         {
54                             doc['_id'] = String(row.value);
55                             form_id = doc['_id'];
56                         }
57                         else if(   row.name == '_rev'
58                                 && (   row.value != undefined
59                                     && row.value != ''))
60                         {
61                             doc['_rev'] = Number(row.value);
62                         } else {
63                             if (row.name != 'submit') {
64                                 if (row.name == '_type') {
65                                 doc_type = row.value;
66                                 form_values[row.name] = row.value;
67                             }
68                             else if (   row.name.substr(0,6) != "widget"
69                                      && row.name.substr(0,8) != "metadata")
70                             {
71                                 var item = {};
72                                 var widget = {};
73                                 var prev_val = false;
74                                 $.each(form_data, function(x,r){
75                                     if (r.name == 'widget['+row.name+':name]') {
76                                         widget['name'] = r.value;
77                                     } else if (r.name == 'widget['+row.name+':config]') {
78                                             widget['config'] = $.ajatus.converter.parseJSON(r.value);
79                                         } else if (r.name == 'widget['+row.name+':prev_val]') {
80                                             prev_val = $.ajatus.converter.parseJSON(r.value);
81                                         }
82                                     });
84                                     var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
85                                 item['val'] = wdgt.value_on_save(row.value, prev_val);
86                                 item['widget'] = widget;
87                                 form_values[row.name] = item;
88                             }
89                             else if (row.name.substr(0,8) == "metadata")
90                             {
91                                 if (!form_values['metadata'])
92                                 {
93                                     form_values['metadata'] = {};
94                                 }
96                                 var re = /\bmetadata\[([a-z]+)\b/;
97                                 var results = re.exec(row.name);
98                                 var key = results[1];
100                                 form_values['metadata'][key] = {
101                                     val: row.value
102                                 };
103                             }
104                             }
105                         }
106                     });
107                     doc['value'] = form_values;
109                     doc = new $.ajatus.document(doc);
111                     var now = $.ajatus.formatter.date.js_to_iso8601(new Date());
113                     var new_metadata = {
114                         revised: now,
115                         revisor: $.ajatus.preferences.local.user
116                     };
117                     if (   doc._id == undefined
118                         || doc._id == '')
119                     {
120                         new_metadata['created'] = now;
121                         new_metadata['creator'] = $.ajatus.preferences.local.user;
122                     }
124                     $.ajatus.forms.process.doc = $.ajatus.document.modify_metadata(doc, new_metadata);
125                     
126                     return $.ajatus.forms.process.doc;
127             },
128             normal: function(form_data) {
129                 var doc = $.ajatus.forms.process.common(form_data);
131                     var jqcouch = $.jqcouch.doc;
132                 var db_path = $.ajatus.preferences.client.application_database + '/content';
133                 jqcouch.on_success = function(data, caller) {
134                     if (data.ok) {
135                         doc['_id'] = data._id;
136                         doc['_rev'] = data._rev;
137                         doc = new $.ajatus.document(doc);
138                         $.ajatus.views.system.item.render(doc);
139                         $.ajatus.events.named_lock_pool.decrease('unsaved');
140                     }
141                 }
142                     jqcouch.save(db_path, $.ajatus.forms.process.doc);                
143                     return false;
144             },
145             ajax: function(form_data) {
146                 var doc = $.ajatus.forms.process.common(form_data);
147                 
148                 $.ajatus.events.named_lock_pool.decrease('unsaved');                
149                     return false;
150             }
151         }        
152     });
153     
154 })(jQuery);