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
17 $.ajatus.document = function(doc, check_metadata, check_schema) {
18 if (typeof check_metadata == 'undefined') {
19 var check_metadata = true;
21 if (typeof check_schema == 'undefined') {
22 var check_schema = false;
25 // console.log("Check schema: "+check_schema);
26 // console.log("Check metadata: "+check_metadata);
31 $.each(doc, function(i,n){
32 if ( (i == '_id' && n != '')
33 || i == 'id' && n != '')
35 self.ready_doc['_id'] = n;
36 } else if (i == '_rev' && n != '') {
37 self.ready_doc[i] = n;
38 } else if (i == 'key' && n != '') {
39 self.ready_doc[i] = n;
40 } else if (i == 'value' && n != '') {
41 self.ready_doc[i] = n;
45 if (typeof this.ready_doc['value'] == 'undefined') {
46 $.ajatus.debug("Error: Document didn't have value field defined!", "ajatus.document");
47 return this.ready_doc;
51 if (! this.ready_doc.value.metadata) {
54 this.check_metadata();
58 if (! $.ajatus.preferences.client.content_types[this.ready_doc.value._type]) {
59 this.ready_doc.value._type = 'note';
66 this.prepare_doc_title();
68 return this.ready_doc;
70 $.extend($.ajatus.document.prototype, {
71 check_schema: function() {
73 var type = $.ajatus.preferences.client.content_types[this.ready_doc.value._type];
75 var new_schema_parts = {};
77 $.each(type.original_schema, function(k,si){
78 // console.log("self.ready_doc.value.metadata["+k+"]:"+self.ready_doc.value.metadata[k])
79 if (typeof(self.ready_doc.value[k]) == 'undefined') {
81 if (typeof si['def_val'] != 'undefined') {
82 def_val = si['def_val'];
84 new_schema_parts[k] = {
90 def_val = self.ready_doc.value[k].val || '';
91 if (typeof si['def_val'] != 'undefined') {
92 def_val = si['def_val'];
94 self.ready_doc.value[k] = $.extend({
97 }, self.ready_doc.value[k]);
102 // console.log("Found schema parts that need to be updated:");
103 // console.log(new_schema_parts);
104 self._fix_doc_schema(new_schema_parts);
107 _fix_doc_schema: function(new_parts) {
108 // console.log("FIX SCHEMA:");
109 // console.log(new_parts);
112 doc = _self.ready_doc;//new $.ajatus.document.loader(_self.ready_doc._id, _self.ready_doc._rev, false, false);
113 doc.value = $.extend(doc.value, new_parts);
115 var db_path = $.ajatus.preferences.client.content_database;
117 $.jqCouch.connection('doc').save(db_path, doc);
118 _self.ready_doc = doc;//new $.ajatus.document(doc);
120 add_metadata: function() {
122 this.ready_doc.value.metadata = {};
123 $.each($.ajatus.document.metadata, function(k,m){
124 self.ready_doc.value.metadata[k] = {
130 check_metadata: function() {
131 // console.log("check metadata");
134 var new_md_parts = {};
135 $.each($.ajatus.document.metadata, function(k,m){
136 // console.log("self.ready_doc.value.metadata["+k+"]:"+self.ready_doc.value.metadata[k])
137 if (typeof(self.ready_doc.value.metadata[k]) == 'undefined') {
139 if (typeof m['def_val'] != 'undefined') {
140 def_val = m['def_val'];
148 def_val = self.ready_doc.value.metadata[k].val || '';
149 if (typeof m['def_val'] != 'undefined') {
150 def_val = m['def_val'];
152 self.ready_doc.value.metadata[k] = $.extend({
155 }, self.ready_doc.value.metadata[k]);
160 self._fix_metadata(new_md_parts);
163 _fix_metadata: function(new_metadata) {
166 // console.log("_fix_metadata for: "+_self.ready_doc._id);
167 // console.log(new_metadata);
169 doc = new $.ajatus.document.loader(_self.ready_doc._id, _self.ready_doc._rev, false);
171 // console.log("Before:");
172 // console.log(doc.value.metadata);
174 doc.value.metadata = $.extend({}, doc.value.metadata, new_metadata);
176 // console.log("After:");
177 // console.log(doc.value.metadata);
179 var db_path = $.ajatus.preferences.client.content_database;
181 $.jqCouch.connection('doc').save(db_path, doc);
182 _self.ready_doc = new $.ajatus.document(doc);
184 prepare_doc_title: function() {
187 if ($.ajatus.preferences.client.content_types[this.ready_doc.value._type]['title_item']) {
188 $.ajatus.preferences.client.content_types[this.ready_doc.value._type].schema['title'] = {
198 $.each($.ajatus.preferences.client.content_types[this.ready_doc.value._type]['title_item'], function(i,part){
199 if (self.ready_doc.value[part]) {
200 var widget = new $.ajatus.widget(self.ready_doc.value[part].widget.name, self.ready_doc.value[part].widget.config);
201 var val = widget.value_on_view(self.ready_doc.value[part].val, 'plain');
202 if (typeof val != 'string') {
203 val = self.ready_doc.value[part].val;
207 title_val += $.ajatus.i10n.get(part);
211 this.ready_doc.value['title'] = {
213 widget: $.ajatus.preferences.client.content_types[this.ready_doc.value._type].schema['title'].widget
218 $.ajatus.document.loader = function(id, rev, check_metadata) {
219 if (typeof check_metadata == 'undefined') {
220 var check_metadata = true;
224 if ( typeof rev != 'undefined'
230 return new $.ajatus.document(
231 $.jqCouch.connection('doc').get($.ajatus.preferences.client.content_database + '/' + id, args),
236 $.ajatus.document.revisions = {
238 // console.log("get_revisions for "+doc._id);
239 if (typeof doc._revs_info == 'undefined') {
240 doc = $.jqCouch.connection('doc').get($.ajatus.preferences.client.content_database + '/' + doc._id, {
246 var available_revs = [];
247 var active_rev_status = false;
248 $.each(doc._revs_info, function(i,n){
249 if (n.rev == doc._rev) {
250 active_rev_status = n.status;
252 if (n.status == 'disk') {
253 available_revs.push(n);
259 revs_data['active'] = {
261 status: active_rev_status
263 revs_data['available'] = available_revs;
264 revs_data['all'] = doc._revs_info;
266 // console.log("Revs data:");
267 // console.log(revs_data);
271 navigate_to: function(pos, current, rev_data) {
272 // console.log("Navigate_to ("+pos+"), current rev: "+current);
276 var revisions = rev_data.all;
278 var put_behind = true;
280 $.each(revisions, function(i,n){
282 if (n.rev == current) {
293 var curr_key = rev_idxs[current];
295 // console.log("Behind:");
296 // console.log(behind);
297 // console.log("Ahead:");
298 // console.log(ahead);
300 var return_rev = null;
304 if (typeof ahead[(ahead.length-1)] != 'undefined') {
305 return_rev = ahead[(ahead.length-1)];
306 curr_key = rev_idxs[return_rev];
310 if (typeof behind[0] != 'undefined') {
311 return_rev = behind[0];
312 curr_key = rev_idxs[return_rev];
314 if (rev_data.active.rev == current) {
315 curr_key = rev_idxs[current];
320 if (typeof behind[(behind.length-1)] != 'undefined') {
321 return_rev = behind[(behind.length-1)];
322 curr_key = rev_idxs[return_rev];
323 } else if (typeof ahead[0] != 'undefined') {
324 return_rev = ahead[0];
325 curr_key = rev_idxs[return_rev];
328 if (rev_data.active.rev == current) {
329 curr_key = rev_idxs[current];
334 if (typeof ahead[0] != 'undefined') {
335 return_rev = ahead[0];
336 curr_key = rev_idxs[return_rev];
341 // console.log("Total revs: "+revisions.length);
342 // console.log("curr_key: "+curr_key);
343 // console.log("Current: "+(revisions.length - (curr_key)));
345 // console.log("Return rev: "+return_rev);
348 total: revisions.length,
349 current: (revisions.length - curr_key),
354 $.ajatus.document.metadata = {
404 $.ajatus.document.modify_metadata = function(doc, metadata) {
405 $.each(metadata, function(k,v){
406 if ($.ajatus.document.metadata[k])
408 var wdgt = new $.ajatus.widget($.ajatus.document.metadata[k].widget.name, $.ajatus.document.metadata[k].widget.config);
409 doc.value.metadata[k] = {
410 val: wdgt.value_on_save(wdgt.value_on_save(v)),
411 widget: $.ajatus.document.metadata[k].widget
418 $.ajatus.document.actions = {
419 execute: function(action, tmpdoc, force_load_rev) {
420 if (typeof force_load_rev == 'undefined') {
421 force_load_rev = false;
424 if ( typeof tmpdoc._rev != 'undefined'
425 && tmpdoc._rev != null
428 var doc = new $.ajatus.document.loader(tmpdoc._id, tmpdoc._rev);
430 var doc = new $.ajatus.document.loader(tmpdoc._id);
433 var act_info = $.ajatus.document.actions._get_action_info(action);
435 var msg = $.ajatus.elements.messages.create(
436 $.ajatus.i10n.get('Unknown document action'),
437 $.ajatus.i10n.get('Unknown action %s requested for object %s!', [action, doc.value.title.val])
442 if (act_info.pool_action)
444 var pa = act_info.pool_action;
445 var pai = $.ajatus.document.actions._get_action_info(fa);
451 var pool_id = $.ajatus.document.history.pool.add(pool_action);
455 if (act_info.undoable) {
456 $.ajatus.document.actions._execute_temp(act_info.action, doc);
458 $.ajatus.document.actions._execute_final(act_info.action, doc);
461 _execute_temp: function(action, doc) {
462 // console.log("_execute_temp: "+action);
465 var on_success = function(data) {
470 var msg = $.ajatus.elements.messages.create(
471 $.ajatus.i10n.get('Object deleted'),
472 $.ajatus.i10n.get('Object %s moved to trash.', [doc.value.title.val]) + ' <a href="#undelete.'+doc.value._type+'.'+doc._id+'" class="undo">' + $.ajatus.i10n.get('Undo') + '</a>'
474 msg.bind_in_content('click', 'a.undo', function(e){
475 $.ajatus.document.actions.execute("undelete", tmpdoc, true);
476 msg.fade('out', true);
482 doc = $.ajatus.document.modify_metadata(doc, {
484 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
485 revisor: $.ajatus.preferences.local.user.email
488 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
490 $('#object_'+doc._id, $.ajatus.application_content_area).hide().addClass('deleted');
493 var on_success = function(data) {
498 var msg = $.ajatus.elements.messages.create(
499 $.ajatus.i10n.get('Object archived'),
500 $.ajatus.i10n.get('Object %s moved to archive.', [doc.value.title.val]) + ' <a href="#unarchive.'+doc.value._type+'.'+doc._id+'" class="undo">' + $.ajatus.i10n.get('Undo') + '</a>'
502 msg.bind_in_content('click', 'a.undo', function(e){
503 $.ajatus.document.actions.execute("unarchive", tmpdoc, true);
504 msg.fade('out', true);
510 doc = $.ajatus.document.modify_metadata(doc, {
511 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
512 revisor: $.ajatus.preferences.local.user.email,
516 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
518 $('#object_'+doc._id, $.ajatus.application_content_area).hide().addClass('archived');
522 _execute_final: function(action, doc) {
525 var on_success = function(data) {
526 var msg = $.ajatus.elements.messages.create(
527 $.ajatus.i10n.get('Object deleted'),
528 $.ajatus.i10n.get('Object %s removed from Ajatus.', [doc.value.title.val])
533 $.jqCouch.connection('doc', on_success).del($.ajatus.preferences.client.content_database, doc);
534 $('#object_'+doc._id, $.ajatus.application_content_area).remove();
537 var on_success = function(data) {
538 var msg = $.ajatus.elements.messages.create(
539 $.ajatus.i10n.get('Object restored'),
540 $.ajatus.i10n.get('Object %s restored succesfully.', [doc.value.title.val])
545 doc = $.ajatus.document.modify_metadata(doc, {
547 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
548 revisor: $.ajatus.preferences.local.user.email
550 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
552 $('#object_'+doc._id, $.ajatus.application_content_area).not('.deleted').remove();
553 $('#object_'+doc._id, $.ajatus.application_content_area).filter('.deleted').show().removeClass('deleted');
556 var on_success = function(data) {
557 var msg = $.ajatus.elements.messages.create(
558 $.ajatus.i10n.get('Object restored'),
559 $.ajatus.i10n.get('Object %s restored succesfully.', [doc.value.title.val])
564 doc = $.ajatus.document.modify_metadata(doc, {
565 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
566 revisor: $.ajatus.preferences.local.user.email,
569 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
571 $('#object_'+doc._id, $.ajatus.application_content_area).filter('.archived').show().removeClass('archived');
575 empty_pool: function () {
576 var items = $.ajatus.document.history.pool.get_all();
577 if (items.length > 0) {
578 var msg = $.ajatus.elements.messages.create(
579 $.ajatus.i10n.get('Emptying action pool'),
580 $.ajatus.i10n.get('Running %d pooled actions.', [items.length])
583 $.each(items, function(i,item){
584 $.ajatus.document.history.pool.remove(i);
585 $.ajatus.document.actions.execute(item.action, item.doc);
588 _get_action_info: function(action) {
593 label: $.ajatus.i10n.get('Delete'),
595 undo_label: $.ajatus.i10n.get('Undelete'),
602 label: $.ajatus.i10n.get('Delete'),
610 label: $.ajatus.i10n.get('Undelete'),
618 label: $.ajatus.i10n.get('Archive'),
620 undo_label: $.ajatus.i10n.get('Restore'),
627 label: $.ajatus.i10n.get('Restore'),
638 $.ajatus.document.history = {};
639 $.ajatus.document.history.pool = {
642 add: function(action) {
643 var new_length = $.ajatus.document.history.pool.items.push(action);
644 var pool_id = new_length-1;
647 remove: function(item_id) {
648 $.ajatus.document.history.pool.items = $.grep($.ajatus.document.history.pool.items, function(n,i){
652 get_item: function(item_id) {
653 if (items[item_id]) {
654 return items[item_id];
659 get_all: function() {
660 return $.ajatus.document.history.pool.items;
664 $.ajatus.document.scheme = {
667 $.ajatus.document.scheme.items = {
668 configure: function(name, widget) {
669 var dialog = new $.ajatus.elements.dialog($.ajatus.i10n.get('Edit field %s', [name]), '', {
676 var prev_vals = $.ajatus.document.scheme.items._read_settings(name, $.ajatus.forms.active, widget);
677 var on_save = '$.ajatus.document.scheme.items.save_config';
679 var content_holder = $.ajatus.document.scheme.items._create_structure( dialog );
681 var jqform = $('.form', content_holder);
682 var contents = $('.contents', content_holder);
684 var wdgt_settings = $('<div id="widget_settings_holder" />');
685 wdgt_settings.appendTo(contents);
686 wdgt_settings.html($.ajatus.document.scheme.items._generate_widget_settings(widget, prev_vals));
688 $('.actions #submit_save', content_holder).bind('click', function(e){
690 if (typeof on_save == 'string') {
693 var result = fn.apply(fn, [name, jqform.formToArray(false)]);
702 generate_actions_tpl: function(name, widget, data, row_holder) {
703 var item_data = data;
705 'img', { className: 'edit_settings_btn', src: $.ajatus.preferences.client.theme_icons_url + 'preferences.png', title: $.ajatus.i10n.get('Settings'), alt: $.ajatus.i10n.get('Settings'), onclick: function(){$.ajatus.document.scheme.items.configure(name, widget);} }, ''
708 save_config: function(name, form_data) {
709 var form_values = {};
711 $.ajatus.document.scheme.items._process_widget_settings(form_data, form_values);
714 $.each(form_values, function(i,n){
718 var row = $('#_element_'+name, $.ajatus.forms.active);
720 var add_upd_elem = $('input[name="widget['+name+':additional_updated]"]', row);
721 if (typeof add_upd_elem[0] == 'undefined') {
723 'input', { type: 'hidden', name: 'widget['+name+':field_updated]', value: $.ajatus.converter.toJSON(true) }, ''
726 add_upd_elem.val($.ajatus.converter.toJSON(true));
729 var widget_config_input = $('#_element_'+name+'_widget input[name="widget['+name+':config]"]', $.ajatus.forms.active)
730 .val($.ajatus.converter.toJSON(config));
734 _create_structure: function(dialog) {
735 var cholder = dialog.get_content_holder();
737 var title = $('<h2 class="title"/>');
738 var form = $('<form id="additionals_window_form" class="form" />');
739 var form_errors = $('<div class="form_errors" />').hide();
740 var content_area = $('<div class="contents"/>');
741 var actions = $('<div class="actions" />').html('<input type="submit" id="submit_save" name="save" value="' + $.ajatus.i10n.get('Save') + '" /><input type="submit" id="submit_cancel" name="cancel" value="' + $.ajatus.i10n.get('Cancel') + '" />');
743 title.appendTo(cholder);
744 form.appendTo(cholder);
745 form_errors.appendTo(form);
746 content_area.appendTo(form);
747 actions.appendTo(cholder);
749 $('#submit_cancel', actions).bind('click', function(e){
756 _read_settings: function(name, form, widget) {
759 if (typeof form == 'undefined') {
760 var form = $.ajatus.forms.active;
762 if (typeof widget != 'undefined') {
763 prev_vals = widget.config;
766 var widget_config_input = $('#_element_'+name+'_widget input[name="widget['+name+':config]"]', form);
767 if (typeof widget_config_input[0] != 'undefined') {
768 prev_vals = $.ajatus.converter.parseJSON(widget_config_input.val());
773 _generate_widget_settings: function(widget_name, data) {
774 var widget = $.ajatus.widget(widget_name);
775 var details = $('<p>' + $.ajatus.i10n.get("Widget %s doesn't support dynamic settings.", [$.ajatus.i10n.get(widget_name)]) + '</p>');
776 if (typeof(widget['create_widget_settings']) == 'function') {
777 details = widget.create_widget_settings(data || {});
782 _process_widget_settings: function(form_data, form_values) {
783 $.each(form_data, function(i,row){
784 if (row.name.toString().match(/__(.*?)/)) {
787 if (row.name.substr(0,6) != "widget") {
790 var prev_val = false;
792 var name_parts_count = 0;
793 if (row.name.toString().match(/;/g)) {
794 name_parts = row.name.toString().split(";");
795 name_parts_count = name_parts.length;
798 $.each(form_data, function(x,r){
799 if (r.name == 'widget['+row.name+':name]') {
800 widget['name'] = r.value;
801 } else if (r.name == 'widget['+row.name+':config]') {
802 widget['config'] = $.ajatus.converter.parseJSON(r.value);
803 } else if (r.name == 'widget['+row.name+':prev_val]') {
804 prev_val = $.ajatus.converter.parseJSON(r.value);
808 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
810 item['val'] = wdgt.value_on_save(row.value, prev_val);
811 item['widget'] = widget;
813 if (name_parts_count > 0) {
815 for (var i=0; i < name_parts_count; i++) {
816 var key = "['"+name_parts[i]+"']";
818 if (prevs.length > 0) {
820 $.each(prevs, function(pi, pk){
821 key_prefix = "['" + pk + "']" + key_prefix;
823 key = key_prefix + key;
826 if (typeof eval("form_values"+key) == 'undefined') {
827 eval("form_values"+key+"={};");
830 prevs.push(name_parts[i]);
831 if (i == name_parts_count-1) {
832 if ($.browser.mozilla) {
833 if (typeof item == 'object') {
834 eval("form_values"+key+"="+item.toSource()+";");
836 eval("form_values"+key+"="+item+";");
839 eval("form_values"+key+"="+item+";");
844 form_values[row.name] = item;
851 $.ajatus.document.additionals = {
860 $.extend($.ajatus.document.additionals, {
861 create: function(content_type, row_holder, opts) {
862 $.ajatus.document.additionals._reset();
864 var dialog = new $.ajatus.elements.dialog($.ajatus.i10n.get('Add field'), '', {
871 var content_holder = $.ajatus.document.scheme.items._create_structure( dialog );
873 var jqform = $('.form', content_holder);
874 var contents = $('.contents', content_holder);
875 var form_errors = $('.form_errors', content_holder);
876 var wdgts = $.ajatus.document.additionals._generate_widget_list();
878 $('<label for="widget"/>').html($.ajatus.i10n.get('Widget')).appendTo(contents);
879 var wdgt_sel_holder = $('<div id="widget_selection_holder"/>');
880 wdgt_sel_holder.html(wdgts).appendTo(contents);
882 var wdgt_details = $('<div id="widget_details_holder"/>');
883 wdgt_details.appendTo(contents);
885 var wdgts_on_change = function(e){
886 var sel_w = e.currentTarget.value;
888 wdgt_sel_holder.html($.ajatus.i10n.get(sel_w));
889 wdgt_sel_holder.addClass('selected');
890 wdgt_details.html($.ajatus.document.additionals._generate_widget_details(sel_w));
892 var field = $('<input type="hidden" />').attr({
896 field.appendTo(jqform);
900 wdgts.bind('change', wdgts_on_change);
902 wdgt_sel_holder.bind('dblclick', function(e){
903 wdgt_sel_holder.removeClass('selected');
904 wdgt_sel_holder.html(wdgts);
905 wdgts.bind('change', wdgts_on_change);
908 $('.actions #submit_save', content_holder).bind('click', function(e){
909 var result = $.ajatus.document.additionals.save_widget(jqform.formToArray(false), jqform);
911 if (result == true) {
912 $.ajatus.document.additionals.close(dialog);
914 if (typeof result == 'object') {
915 form_errors.html('');
916 var error_holder = $('<ul />');
917 $.each(result, function(field, data){
918 $('<li class="error" />').html(data.msg).appendTo(error_holder);
920 error_holder.appendTo(form_errors);
928 $.ajatus.document.additionals.show(dialog);
930 edit: function(widget, row_holder, name, data, opts) {
931 $.ajatus.document.additionals._reset();
933 var dialog = new $.ajatus.elements.dialog($.ajatus.i10n.get('Edit field %s', [name]), '', {
940 var content_holder = $.ajatus.document.scheme.items._create_structure( dialog );
942 var jqform = $('.form', content_holder);
943 var contents = $('.contents', content_holder);
944 var form_errors = $('.form_errors', content_holder);
946 if (typeof data['widget'] != 'undefined') {
947 data.widget.config = $.ajatus.document.additionals._read_settings(name, $.ajatus.forms.active, widget);
950 var prev_value = null;
951 var prev_val_input = $('#_element_'+name+'_widget input[name="widget['+name+':prev_val]"]', $.ajatus.forms.active);
952 if (typeof prev_val_input[0] != 'undefined') {
953 prev_value = $.ajatus.converter.parseJSON(prev_val_input.val());
956 var wdgt_details = $('<div id="widget_details_holder"/>');
957 wdgt_details.appendTo(contents);
959 wdgt_details.html($.ajatus.document.additionals._generate_widget_details(widget.name, name, data));
961 $('.actions #submit_save', content_holder).bind('click', function(e){
966 if (prev_value != null) {
967 prev_data.value = prev_value;
969 var result = $.ajatus.document.additionals.save_widget(jqform.formToArray(false), jqform, prev_data);
971 if (result == true) {
972 $.ajatus.document.additionals.close(dialog);
974 if (typeof result == 'object') {
975 form_errors.html('');
976 var error_holder = $('<ul />');
977 $.each(result, function(field, data){
978 $('<li class="error" />').html(data.msg).appendTo(error_holder);
980 error_holder.appendTo(form_errors);
986 $.ajatus.document.additionals.show(dialog);
988 config: function(widget, prev_vals, on_save, parent, name) {
989 var dialog = new $.ajatus.elements.dialog($.ajatus.i10n.get('Configure widget %s', [$.ajatus.i10n.get(widget.name)]), '', {
997 var content_holder = $.ajatus.document.scheme.items._create_structure( dialog );
999 if (typeof prev_vals == 'undefined') {
1002 if (typeof on_save == 'undefined') {
1003 var on_save = '$.ajatus.document.additionals.save_widget_settings';
1006 var jqform = $('.form', content_holder);
1007 var contents = $('.contents', content_holder);
1009 var wdgt_settings = $('<div id="widget_settings_holder" />');
1010 wdgt_settings.appendTo(contents);
1011 wdgt_settings.html($.ajatus.document.additionals._generate_widget_settings(widget, prev_vals));
1013 $('.actions #submit_save', content_holder).bind('click', function(e){
1014 var parent_dialog = parent;
1015 if (typeof parent == 'undefined') {
1016 parent_dialog = $.ajatus.document.additionals._get_parent(dialog.id);
1020 if (typeof on_save == 'string') {
1023 var result = fn.apply(fn, [jqform.formToArray(false), parent_dialog, name, jqform]);
1026 $.ajatus.document.additionals.close(dialog);
1030 $.ajatus.document.additionals.show(dialog);
1032 del: function(widget, row_holder, name, data) {
1034 if ( typeof data == 'object'
1035 && typeof data.label != 'undefined')
1040 var confirmation = confirm($.ajatus.i10n.get("Do you really want to delete item %s", [label]));
1042 if (confirmation == true) {
1043 $('#_element_'+name, row_holder).remove();
1046 show: function(dialog) {
1047 var dlg = dialog.get_dialog();
1048 dlg.addClass('additionals_window');
1051 $.ajatus.document.additionals.open.push(dialog.id);
1052 $.ajatus.document.additionals._position(dlg);
1054 close: function(dialog) {
1057 $.ajatus.document.additionals.open = $.grep($.ajatus.document.additionals.open, function(n,i){
1058 if (n != dialog.id) {
1065 _position: function(dialog) {
1066 var top = $.ajatus.document.additionals.top_start;
1067 var left = $.ajatus.document.additionals.left_start;
1069 if ($.ajatus.document.additionals.open.length > 1) {
1070 var incr = $.ajatus.document.additionals.open.length * 2;
1080 _process_widget_settings: function(form_data, form_values) {
1081 $.ajatus.document.scheme.items._process_widget_settings(form_data, form_values);
1083 _settings_to_saved: function(values, holder) {
1084 $.each(values, function(k,item){
1085 var field = $('<input type="hidden" />').attr({
1086 name: 'widget[config;'+k+':name]',
1087 value: item.widget.name
1089 field.appendTo(holder);
1091 var field = $('<input type="hidden" />').attr({
1092 name: 'widget[config;'+k+':config]',
1093 value: $.ajatus.converter.toJSON(item.widget.config)
1095 field.appendTo(holder);
1097 var field = $('<input type="hidden" />').attr({
1099 value: item.val != '' ? $.ajatus.converter.toJSON(item.val) : ''
1101 field.appendTo(holder);
1104 save_widget_settings: function(form_data, parent_dialog, name, form) {
1105 var form_values = {};
1107 $.ajatus.document.additionals._process_widget_settings(form_data, form_values);
1109 var config_holder = $('.saved_settings', parent_dialog);
1110 config_holder.html('');
1112 $.ajatus.document.additionals._settings_to_saved(form_values, config_holder);
1116 _read_settings: function(name, form, widget) {
1117 if (typeof form == 'undefined') {
1118 var form = $.ajatus.forms.active;
1123 if (typeof widget != 'undefined') {
1124 prev_vals = widget.config;
1127 var widget_config_input = $('#_element_'+name+'_widget input[name="widget['+name+':config]"]', form);
1128 if (typeof widget_config_input[0] != 'undefined') {
1129 prev_vals = $.ajatus.converter.parseJSON(widget_config_input.val());
1134 update_widget_settings: function(form_data, parent_form, name, form) {
1135 var form_values = {};
1137 $.ajatus.document.additionals._process_widget_settings(form_data, form_values);
1140 $.each(form_values, function(i,n){
1144 var row = $('#_element_'+name, parent_form);
1146 var add_upd_elem = $('input[name="widget['+name+':additional_updated]"]', row);
1147 if (typeof add_upd_elem[0] == 'undefined') {
1149 'input', { type: 'hidden', name: 'widget['+name+':additional_updated]', value: $.ajatus.converter.toJSON(true) }, ''
1152 add_upd_elem.val($.ajatus.converter.toJSON(true));
1155 var additional_input = $('#_element_'+name+' input[name="widget['+name+':additional]"]', parent_form);
1156 var widget_config_input = $('#_element_'+name+'_widget input[name="widget['+name+':config]"]', parent_form);
1158 var add_data = $.ajatus.converter.parseJSON(additional_input.val());
1159 if ( typeof add_data.widget != 'undefined'
1160 && typeof add_data.widget.config != 'undefined')
1162 add_data.widget.config = config;
1165 additional_input.val($.ajatus.converter.toJSON(add_data));
1166 widget_config_input.val($.ajatus.converter.toJSON(config));
1170 save_widget: function(form_data, form, prev_data) {
1171 var form_values = {};
1172 var sel_widget = 'text';
1174 var has_errors = false;
1175 var error_fields = {};
1177 $.each(form_data, function(i,row){
1178 if (row.name.toString().match(/__(.*?)/)) {
1181 if (row.name == '_widget') {
1182 sel_widget = String(row.value);
1184 else if (row.name.substr(0,6) != "widget") {
1187 var prev_val = false;
1188 var name_parts = [];
1189 var name_parts_count = 0;
1190 var row_key = row.name;
1192 if (row.name.toString().match(/;/g)) {
1193 name_parts = row.name.toString().split(";");
1194 name_parts_count = name_parts.length;
1196 row_key = name_parts[0];
1199 $.each(form_data, function(x,r){
1200 if (r.name == 'widget['+row.name+':name]') {
1201 widget['name'] = r.value;
1202 } else if (r.name == 'widget['+row.name+':config]') {
1203 widget['config'] = $.ajatus.converter.parseJSON(r.value);
1204 } else if (r.name == 'widget['+row.name+':prev_val]') {
1205 if (r.value == '') {
1208 prev_val = $.ajatus.converter.parseJSON(r.value);
1210 } else if (r.name == 'widget['+row.name+':required]') {
1211 widget['required'] = $.ajatus.utils.to_boolean(r.value);
1215 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
1217 if (typeof widget['required'] != 'undefined') {
1218 wdgt.required = widget['required'];
1221 if (typeof wdgt.validate == 'function') {
1222 var status = wdgt.validate(row_key, row.value);
1223 if (typeof status == 'object') {
1225 error_fields[row_key] = status;
1229 item['val'] = wdgt.value_on_save(row.value, prev_val);
1231 item['widget'] = widget;
1233 if (name_parts_count > 0) {
1235 for (var i=0; i < name_parts_count; i++) {
1236 var arr_keys = false;
1237 var key_prefix = '';
1239 if (name_parts[i].match(/\[/g)) {
1240 var arr_keys = name_parts[i].split('[');
1242 name_parts[i] = name_parts[i].replace(/\[/g, '"][');
1243 var key = '["'+name_parts[i];
1245 var key = "['"+name_parts[i]+"']";
1248 if (prevs.length > 0) {
1249 $.each(prevs, function(pi, pk){
1250 key_prefix = "['" + pk + "']" + key_prefix;
1252 key = key_prefix + key;
1256 var tmp_key = key_prefix + "['" + arr_keys[0] + "']";
1257 if (typeof eval("form_values"+tmp_key) == 'undefined') {
1258 eval("form_values"+tmp_key+"=[];");
1262 var multiple = false;
1263 if (typeof eval("form_values"+key) == 'undefined') {
1264 if (key_prefix != '') {
1265 eval("form_values"+key+"=new Array();");
1267 eval("form_values"+key+"={};");
1273 prevs.push(name_parts[i]);
1275 if (i == name_parts_count-1) {
1276 if (typeof item['val'] == 'undefined') {
1280 if (typeof item == 'object') {
1281 if (item.widget.name == 'boolean') {
1282 eval("form_values"+key+"="+item.val+";");
1284 eval("form_values"+key+".push('"+item.val+"');");
1287 eval("form_values"+key+".push('"+item+"');");
1290 if (typeof item == 'object') {
1291 eval("form_values"+key+"='"+item.val+"';");
1293 eval("form_values"+key+"='"+item.val+"';");
1299 form_values[row.name] = item;
1305 if (typeof form_values['config'] != 'undefined') {
1306 $.each(form_values['config'], function(i,n){
1307 if (typeof n == 'object') {
1308 config[i] = n.val != '' ? $.ajatus.converter.parseJSON(n.val) : '';
1310 config[i] = n != '' ? $.ajatus.converter.parseJSON(n) : '';
1315 var item_name = form_values.name.val.toString().toLowerCase();
1318 return error_fields;
1322 var additional_item = {
1323 label: form_values.label.val,
1328 def_val: form_values.def_val.val,
1329 required: form_values.required.val
1332 var row_holder = $('.form_structure ul.row_holder:eq(0)', $.ajatus.forms.active);
1334 if (typeof prev_data != 'undefined') {
1335 if (typeof prev_data.value != 'undefined') {
1336 additional_item.value = prev_data.value;
1339 $.ajatus.renderer.form_helpers._add_additional_row('edit', row_holder, item_name, additional_item, {}, prev_data.name);
1344 $.ajatus.renderer.form_helpers._add_additional_row('create', row_holder, item_name, additional_item);
1348 edit_existing_config: function(name, widget) {
1349 var prev_vals = $.ajatus.document.additionals._read_settings(name, $.ajatus.forms.active, widget);
1351 var on_save = '$.ajatus.document.additionals.update_widget_settings';
1353 $.ajatus.document.additionals.config(widget, prev_vals, on_save, $.ajatus.forms.active, name);
1355 generate_item_actions_tpl: function(name, widget, data, doc, row_holder) {
1356 var item_data = data;
1358 'img', { className: 'additional_edit_btn', src: $.ajatus.preferences.client.theme_icons_url + 'edit.png', title: $.ajatus.i10n.get('Edit'), alt: $.ajatus.i10n.get('Edit'), onclick: function(){$.ajatus.document.additionals.edit(widget, row_holder, name, item_data, {});} }, '',
1359 'img', { className: 'additional_delete_btn', src: $.ajatus.preferences.client.theme_icons_url + 'trash.png', title: $.ajatus.i10n.get('Delete'), alt: $.ajatus.i10n.get('Delete'), onclick: function() {$.ajatus.document.additionals.del(widget, row_holder, name, item_data);} }, ''
1362 _get_parent: function() {
1363 var parent_id = $.ajatus.document.additionals.open[$.ajatus.document.additionals.open.length-2];
1364 return $('#'+parent_id);
1366 _create_structure: function(id, options) {
1367 var af_win = $('<div class="additionals_window" />').attr({
1370 var title = $('<h2 class="title"/>');
1371 var form = $('<form id="additionals_window_form" class="form" />');
1372 var form_errors = $('<div class="form_errors" />').hide();
1373 var content_area = $('<div class="contents"/>');
1374 var actions = $('<div class="actions" />').html('<input type="submit" id="submit_save" name="save" value="' + $.ajatus.i10n.get('Save') + '" /><input type="submit" id="submit_cancel" name="cancel" value="' + $.ajatus.i10n.get('Cancel') + '" />');
1376 af_win.appendTo($.ajatus.application_dynamic_elements);
1377 title.appendTo(af_win);
1378 form.appendTo(af_win);
1379 form_errors.appendTo(form);
1380 content_area.appendTo(form);
1381 actions.appendTo(af_win);
1383 $('#submit_cancel', actions).bind('click', function(e){
1384 $.ajatus.document.additionals.close(id);
1390 _generate_widget_list: function() {
1391 var select = $('<select id="widget" name="widget" />');
1393 $.each($.ajatus.widgets.loaded_widgets, function(i,w){
1394 var opt = $('<option />').attr({
1396 }).html($.ajatus.i10n.get(w));
1397 opt.appendTo(select);
1400 var opt = $('<option />').attr({
1402 selected: 'selected'
1403 }).html($.ajatus.i10n.get('Select one') + ' ');
1404 opt.prependTo(select);
1408 _generate_widget_details: function(widget_name, name, data) {
1409 var widget = $.ajatus.widget(widget_name);
1410 var details = $('<p>' + $.ajatus.i10n.get("Widget %s doesn't support dynamic creation.", [$.ajatus.i10n.get(widget_name)]) + '</p>');
1411 if (typeof(widget['create_widget_details']) == 'function') {
1412 details = widget.create_widget_details(data || {}, name);
1417 _generate_widget_settings: function(widget_name, data) {
1418 return $.ajatus.document.scheme.items._generate_widget_settings(widget_name, data);
1420 _reset: function() {
1421 $.ajatus.document.additionals.open = [];
1422 $.ajatus.application_dynamic_elements.html('');