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 || {};
17 $.ajatus.renderer = $.ajatus.renderer || {};
19 $.ajatus.renderer.form_helpers = {
20 _add_row: function(type, holder, name, data, doc) {
22 && data.hidden == true)
26 $.ajatus.debug("add row "+name);
28 var widget = new $.ajatus.widget(data.widget.name, data.widget.config);
31 if ( typeof data.widget.required != 'undefined'
32 || typeof data.required != 'undefined')
34 widget.required = data.widget.required || data.required;
40 var def_val = data.def_val;
41 if ( typeof doc == 'object'
42 && typeof doc.value[name] != 'undefined')
44 def_val = doc.value[name].val;
47 var wdgt_tpl = widget.get_create_tpl(name, def_val);
51 if (! doc.value[name]) {
57 doc_value = doc.value[name];
60 var edit_data = $.extend({
64 var wdgt_tpl = widget.get_edit_tpl(name, edit_data);
68 if ( typeof data['read_only'] != 'undefined'
69 && data.read_only == true)
71 wdgt_tpl = $.ajatus.widgets.to_readonly(type, name, data.def_val, widget);
75 'li', { id: '_element_'+name, className: 'row' }, [
76 'label', { id: '_element_'+name+'_label' }, $.ajatus.i10n.get(data.label) + (required ? ' *' : ''),
77 'div', { id: '_element_'+name+'_widget', className: widget.name, style: 'width: 80%;' }, wdgt_tpl,
78 'br', { className: 'clear_fix' }, ''
81 widget.init($('#_element_'+name+'_widget', holder), true);
84 $('#_element_'+name+'_label', holder).addClass('required');
87 $('#_element_'+name+'_label', holder).bind('click', function(e){
88 if ($('#_element_'+name+'_widget:visible', holder)[0]) {
89 $('#_element_'+name+'_widget', holder).hide();
91 $('#_element_'+name+'_widget', holder).show();
95 _add_additional_row: function(type, holder, name, data, doc) {
96 $.ajatus.debug("add additional row "+name);
98 var widget = new $.ajatus.widget(data.widget.name, data.widget.config);
100 var required = false;
101 if ( typeof data.widget.required != 'undefined'
102 || typeof data.required != 'undefined')
104 widget.required = data.widget.required || data.required;
110 var wdgt_tpl = widget.get_create_tpl(name, data.def_val);
114 if (! doc.value[name]) {
120 doc_value = doc.value[name];
123 var edit_data = $.extend({
127 var wdgt_tpl = widget.get_edit_tpl(name, edit_data);
131 var additional_actions_tpl = $.ajatus.document.additionals.generate_item_actions_tpl(name, widget, doc);
134 'li', { id: '_element_'+name, className: 'row' }, [
135 'label', { id: '_element_'+name+'_label' }, $.ajatus.i10n.get(data.label) + (required ? ' *' : ''),
136 'input', { type: 'hidden', name: 'widget['+name+':additional]', value: $.ajatus.converter.toJSON(data) }, '',
137 'div', { id: '_element_'+name+'_widget', className: widget.name, style: 'width: 80%;' }, wdgt_tpl,
138 'div', { id: '_element_'+name+'_additional_actions' }, additional_actions_tpl,
139 'br', { className: 'clear_fix' }, ''
142 widget.init($('#_element_'+name+'_widget', holder), true);
145 $('#_element_'+name+'_label', holder).addClass('required');
148 $('#_element_'+name+'_label', holder).bind('click', function(e){
149 if ($('#_element_'+name+'_widget:visible', holder)[0]) {
150 $('#_element_'+name+'_widget', holder).hide();
152 $('#_element_'+name+'_widget', holder).show();
156 _register_for_autosave: function(form, action) {
157 if ($.ajatus.utils.to_boolean($.ajatus.preferences.local.auto_save.enabled) == false) {
161 var save_interval = Number($.ajatus.preferences.local.auto_save.interval);
162 var watcher = new $.ajatus.events.watcher({
166 timed: (save_interval * 1000)
168 $.ajatus.views.on_change_actions.add('$.ajatus.events.watchers.remove('+watcher._id+');');
170 var as_lc_holder = $('.form_structure .auto_save_lc span', form);
171 as_lc_holder.html('0');
172 if (typeof as_lc_holder[0] == 'undefined') {
173 as_holder = $('<div class="auto_save_lc" />').hide().prependTo($('.form_structure', form));
174 as_holder.html($.ajatus.i10n.get("Auto saving in")+" ");
175 as_lc_holder = $('<span/>').appendTo(as_holder);
178 watcher.element.bind('on_tick', function(e, watcher_id, data){
179 // console.log("on_tick");
180 // console.log(data);
181 if (data['left_prc'] > 70) {
183 var ls = data['left_ms'] / 1000;
185 time_left = parseInt(ls).toString();
187 var min = parseInt(ls/60);
188 var sec = parseInt(ls - (min*60));
191 sec = "0" + sec.toString();
194 time_left = parseInt(min).toString() + ":" + sec;
197 if (as_lc_holder.parent().css('display') == 'none') {
198 as_lc_holder.parent().fadeIn('slow', function(){
199 as_lc_holder.html(time_left);
202 as_lc_holder.html(time_left);
206 watcher.element.bind('on_stop', function(e, watcher_id){
209 if ($.ajatus.forms.active) {
210 $.ajatus.renderer.form_helpers._register_for_autosave(form, action);
216 $.ajatus.renderer.form = function(content_type, doc, doc_as_defaults, latest_rev)
219 this.content_type = content_type;
220 this.doc = doc || false;
221 this.doc_as_defaults = doc_as_defaults || false;
224 $.ajatus.toolbar.show();
227 && !this.doc_as_defaults)
229 this.form = this.edit(this.content_type, this.doc, latest_rev);
231 this.form = this.create(this.content_type, this.doc);
236 $.extend($.ajatus.renderer.form.prototype, {
237 create: function(content_type, doc)
239 if (typeof content_type == 'string') {
240 content_type = $.ajatus.preferences.client.content_types[content_type];
243 var schema_fields = content_type.schema;
244 var type_name = content_type.name;
246 var form = $('<form id="create_'+type_name+'" name="create_'+type_name+'" />');
247 var form_actions = $('<div class="form_actions"><input type="submit" name="save" value="' + $.ajatus.i10n.get('Create') + '" /><input type="submit" name="cancel" value="' + $.ajatus.i10n.get('Cancel') + '" /></div>');
248 var form_struct = $('<div class="form_structure" />');
249 var form_errors = $('<div class="form_errors" />').hide();
250 var row_holder = $('<ul class="row_holder" />');
252 var type_title = content_type.title;
253 $('<h2>' + $.ajatus.i10n.get('Create %s', [type_title]) + '</h2>').appendTo(form_struct);
255 if (typeof $.ajatus.forms.process.has_errors != 'undefined') {
256 var error_holder = $('<ul />');
257 $.each($.ajatus.forms.process.error_fields, function(field, data){
258 $('<li class="error" />').html(data.msg).appendTo(error_holder);
260 error_holder.appendTo(form_errors);
261 form_errors.appendTo(form_struct).show();
264 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Create'), {
267 $('#create_'+type_name+' input[@type=submit][name*=save]').trigger("click", []);
271 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Cancel'), 'cancel.png', function(){
272 $('#create_'+type_name+' input[@type=submit][name*=cancel]').trigger("click", []);
279 var hidden_fields = function() {
281 'input', { type: 'hidden', id: '_type', name: '_type', value: this._type }, ''
284 row_holder.tplAppend(hidden_data, hidden_fields);
286 $.each(schema_fields, function(i,n){
287 $.ajatus.renderer.form_helpers._add_row('create', row_holder, i, n, doc);
290 row_holder.appendTo(form_struct);
291 form_struct.appendTo(form);
292 form_actions.appendTo(form);
294 $('li:first', row_holder).addClass('first');
295 $('li:last', row_holder).addClass('last');
297 if ( typeof(content_type['enable_additionals']) != 'undefined'
298 && content_type.enable_additionals)
300 var additionals_add = $('<div class="additionals_add_btn" />').html($.ajatus.i10n.get('Add field'));
301 additionals_add.appendTo(form_struct);
303 additionals_add.bind('click', function(e){
304 $.ajatus.document.additionals.create(content_type, row_holder);
308 // var save_action = "$('#create_"+type_name+" input[@type=submit][name*=save]').trigger('click', []);";
309 // $.ajatus.renderer.form_helpers._register_for_autosave(form, save_action);
313 edit: function(content_type, doc, latest_rev)
315 var schema_fields = content_type.schema;
316 var type_name = content_type.name;
317 var has_additionals = false;
319 if (typeof doc.value._additionals != 'undefined') {
320 has_additionals = true;
321 $.each(doc.value._additionals, function(key,data){
322 schema_fields[key] = data;
326 var form = $('<form id="edit_'+type_name+'" name="edit_'+type_name+'" />');
327 var form_actions = $('<div class="form_actions"><input type="submit" name="save" value="' + $.ajatus.i10n.get('Save') + '" /><input type="submit" name="cancel" value="' + $.ajatus.i10n.get('Cancel') + '" /></div>');
328 var form_struct = $('<div class="form_structure" />');
329 var form_errors = $('<div class="form_errors" />').hide();
330 var row_holder = $('<ul class="row_holder" />');
332 var type_title = content_type.title;
333 $('<h2>' + $.ajatus.i10n.get('Edit %s', [type_title]) + '</h2>').appendTo(form_struct);
335 if (typeof $.ajatus.forms.process.has_errors != 'undefined') {
336 var error_holder = $('<ul />');
337 $.each($.ajatus.forms.process.error_fields, function(field, data){
338 $('<li class="error" />').html(data.msg).appendTo(error_holder);
340 error_holder.appendTo(form_errors);
341 form_errors.appendTo(form_struct).show();
344 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Save'), {
347 $('#edit_'+type_name+' input[@type=submit][name*=save]').trigger("click", []);
351 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Cancel'), 'cancel.png', function(){
352 $('#edit_'+type_name+' input[@type=submit][name*=cancel]').trigger("click", []);
354 $.ajatus.toolbar.add_item($.ajatus.i10n.get('View'), {
357 $.ajatus.views.system.item.render(doc);
364 _rev: (latest_rev || doc._rev),
368 var hidden_fields = function() {
370 'input', { type: 'hidden', id: this._id+'_id', name: '_id', value: this._id }, '',
371 'input', { type: 'hidden', id: this._id+'_revision', name: '_rev', value: this._rev }, '',
372 'input', { type: 'hidden', id: this._id+'_type', name: '_type', value: this._type }, ''
375 $(row_holder).tplAppend(hidden_data, hidden_fields);
377 if (has_additionals) {
378 var additionals_hidden = $('<input type="hidden" name="_additionals" id="' + doc._id + '_additionals" value=\'' + $.ajatus.converter.toJSON(doc.value._additionals) + '\' />');
379 additionals_hidden.appendTo(row_holder);
382 $.each(doc.value.metadata, function(k,m){
383 var data = $.extend({
386 var md_field = function() {
388 'input', { type: 'hidden', id: this._id+'_metadata_'+k, name: 'metadata['+k+']', value: this.val }, '',
391 $(row_holder).tplAppend(data, md_field);
394 $.each(schema_fields, function(i,n){
396 && typeof doc.value._additionals[i] != 'undefined')
398 $.ajatus.renderer.form_helpers._add_additional_row('edit', row_holder, i, n, doc);
400 $.ajatus.renderer.form_helpers._add_row('edit', row_holder, i, n, doc);
404 row_holder.appendTo(form_struct);
405 form_struct.appendTo(form);
406 form_actions.appendTo(form);
408 $('<div class="clear_fix" />').appendTo(form);
410 $('li:first', row_holder).addClass('first');
411 $('li:last', row_holder).addClass('last');
412 $('li:odd', row_holder).addClass('odd');
414 if ( typeof(content_type['enable_additionals']) != 'undefined'
415 && content_type.enable_additionals)
417 var additionals_add = $('<div class="additionals_add_btn" />').html($.ajatus.i10n.get('Add field'));
418 additionals_add.appendTo(form_struct);
420 additionals_add.bind('click', function(e){
421 $.ajatus.document.additionals.create(content_type, row_holder);
425 var save_action = "$('#edit_"+type_name+" input[@type=submit][name*=save]').trigger('click', []);";
426 $.ajatus.renderer.form_helpers._register_for_autosave(form, save_action);