4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
7 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8 * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9 * Website: http://ajatus.info
10 * Licensed under the GPL license
11 * http://www.gnu.org/licenses/gpl.html
16 $.ajatus
= $.ajatus
|| {};
21 $.extend($.ajatus
.forms
, {
23 normal: function(form
) {
24 $.ajatus
.events
.named_lock_pool
.increase('unsaved');
25 $.ajatus
.forms
.active
= form
;
28 jqform
.bind('submit',function(e
){
32 $('input[@type=submit][name*=save]', jqform
).bind('click', function(){
33 var status
= $.ajatus
.forms
.process
.normal(jqform
.formToArray(false));
36 $.ajatus
.forms
.active
= false;
41 $('input[@type=submit][name*=cancel]', jqform
).bind('click', function(){
42 if ($.ajatus
.history
.enabled
) {
43 $.ajatus
.history
.navigate(-1);
46 $.ajatus
.forms
.active
= false;
50 ajax: function(form
, callback
) {
51 if (typeof callback
== 'undefined') {
52 $.ajatus
.forms
.register
.normal(form
);
55 $.ajatus
.events
.named_lock_pool
.increase('unsaved');
56 $.ajatus
.forms
.active
= form
;
59 jqform
.bind('submit',function(e
){
63 $('input[@type=submit][name*=save]', jqform
).bind('click', function(){
64 $.ajatus
.forms
.process
.ajax(jqform
.formToArray(false), callback
);
66 $.ajatus
.forms
.active
= false;
69 $('input[@type=submit][name*=cancel]', jqform
).bind('click', function(){
70 if ($.ajatus
.history
.enabled
) {
71 $.ajatus
.history
.navigate(-1);
74 $.ajatus
.forms
.active
= false;
78 custom: function(form
, save_func
) {
79 if (typeof save_func
== 'undefined') {
82 $.ajatus
.events
.named_lock_pool
.increase('unsaved');
83 $.ajatus
.forms
.active
= form
;
86 jqform
.bind('submit',function(e
){
90 $('input[@type=submit][name*=save]', jqform
).bind('click', function(){
92 if (typeof save_func
== 'string') {
95 fn
.apply(fn
, [jqform
.formToArray(false), form
]);
97 $.ajatus
.forms
.active
= false;
100 $('input[@type=submit][name*=cancel]', jqform
).bind('click', function(){
101 if ($.ajatus
.history
.enabled
) {
102 $.ajatus
.history
.navigate(-1);
105 $.ajatus
.forms
.active
= false;
115 common: function(form_data
) {
116 $.ajatus
.forms
.process
.has_errors
= false;
117 $.ajatus
.forms
.process
.error_fields
= {};
124 var form_values
= {};
126 var doc_type
= 'note';
127 var additionals
= false;
129 $.each(form_data
, function(i
,row
){
130 // console.log('i: '+i+' row.name: '+row.name+' row.value: '+row.value);
131 if (row
.name
.toString().match(/__(.*?)/)) {
134 if ( row
.name
== '_id'
135 && ( typeof row
.value
!= 'undefined'
138 doc
['_id'] = String(row
.value
);
139 form_id
= doc
['_id'];
141 else if( row
.name
== '_rev'
142 && ( typeof row
.value
!= 'undefined'
145 doc
['_rev'] = String(row
.value
);
147 else if( row
.name
== '_additionals'
148 && ( typeof row
.value
!= 'undefined'
151 additionals
= $.ajatus
.converter
.parseJSON(row
.value
);
153 if (row
.name
!= 'submit') {
154 if (row
.name
== '_type') {
155 doc_type
= row
.value
;
156 form_values
[row
.name
] = row
.value
;
158 else if ( row
.name
.substr(0,6) != "widget"
159 && row
.name
.substr(0,8) != "metadata")
163 var prev_val
= false;
164 var additional
= false;
166 $.each(form_data
, function(x
,r
){
167 if (r
.name
== 'widget['+row
.name
+':name]') {
168 widget
['name'] = r
.value
;
169 } else if (r
.name
== 'widget['+row
.name
+':config]') {
170 widget
['config'] = $.ajatus
.converter
.parseJSON(r
.value
);
171 } else if (r
.name
== 'widget['+row
.name
+':prev_val]') {
172 prev_val
= $.ajatus
.converter
.parseJSON(r
.value
);
173 } else if (r
.name
== 'widget['+row
.name
+':additional]') {
174 additional
= $.ajatus
.converter
.parseJSON(r
.value
);
175 } else if (r
.name
== 'widget['+row
.name
+':required]') {
176 widget
['required'] = $.ajatus
.utils
.to_boolean(r
.value
);
181 var wdgt
= new $.ajatus
.widget(widget
['name'], widget
['config']);
183 if (typeof widget
['required'] != 'undefined') {
184 wdgt
.required
= widget
['required'];
187 if (typeof wdgt
.validate
== 'function') {
188 var status
= wdgt
.validate(row
.name
, row
.value
);
189 if (typeof status
== 'object') {
190 $.ajatus
.forms
.process
.has_errors
= true;
191 $.ajatus
.forms
.process
.error_fields
[row
.name
] = status
;
195 item
['val'] = wdgt
.value_on_save(row
.value
, prev_val
);
196 item
['widget'] = widget
;
199 item
['additional'] = true;
200 if (additionals
== false) {
203 additionals
[row
.name
] = additional
;
206 form_values
[row
.name
] = item
;
208 else if (row
.name
.substr(0,8) == "metadata")
210 if (typeof form_values
['metadata'] == 'undefined') {
211 form_values
['metadata'] = {};
214 var re
= /\bmetadata\[([a-z]+)\b/;
215 var results
= re
.exec(row
.name
);
216 var key
= results
[1];
218 form_values
['metadata'][key
] = {
227 form_values
['_additionals'] = additionals
;
230 doc
['value'] = form_values
;
232 doc
= new $.ajatus
.document(doc
);
234 var now
= $.ajatus
.formatter
.date
.js_to_iso8601(new Date());
238 revisor
: $.ajatus
.preferences
.local
.user
.email
240 if ( typeof doc
._id
== 'undefined'
243 new_metadata
['created'] = now
;
244 new_metadata
['creator'] = $.ajatus
.preferences
.local
.user
.email
;
247 $.ajatus
.forms
.process
.doc
= $.ajatus
.document
.modify_metadata(doc
, new_metadata
);
249 return $.ajatus
.forms
.process
.doc
;
251 normal: function(form_data
) {
252 $.ajatus
.events
.named_lock_pool
.decrease('unsaved');
254 var doc
= $.ajatus
.forms
.process
.common(form_data
);
256 if ($.ajatus
.forms
.process
.has_errors
) {
257 if ( typeof doc
._id
== 'undefined'
260 $.ajatus
.views
.system
.create
.render(doc
.value
._type
, doc
);
262 $.ajatus
.views
.system
.edit
.render(doc
.value
._type
, doc
, true);
267 $.jqCouch
.connection('doc').save($.ajatus
.preferences
.client
.content_database
, doc
);
268 doc
= new $.ajatus
.document(doc
);
270 $.ajatus
.views
.system
.edit
.render(doc
._type
, doc
);
272 var content_type
= $.ajatus
.preferences
.client
.content_types
[doc
.value
._type
];
273 var msg
= $.ajatus
.elements
.messages
.create(
274 $.ajatus
.i10n
.get('Object saved'),
275 $.ajatus
.i10n
.get("%s saved successfully", [content_type
.title
])
281 ajax: function(form_data
, callback
) {
282 var doc
= $.ajatus
.forms
.process
.common(form_data
);
284 $.jqCouch
.connection('doc').save($.ajatus
.preferences
.client
.content_database
, doc
);
285 doc
= new $.ajatus
.document(doc
);
287 $.ajatus
.events
.named_lock_pool
.decrease('unsaved');
289 if (typeof callback
== 'function') {
290 callback
.apply(callback
, [doc
]);