Added archive and unarchive actions
[ajatus.git] / js / ajatus.document.js
blob760e6694837c5eae07f19c3045ee92a6dcabc4db
1 /*
2 * This file is part of
4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
6 *
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
15 (function($){
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 var self = this;
26 this.ready_doc = {};
28 $.each(doc, function(i,n){
29 if ( (i == '_id' && n != '')
30 || i == 'id' && n != '') {
31 self.ready_doc['_id'] = n;
32 } else if (i == '_rev' && n != '') {
33 self.ready_doc[i] = n;
34 } else if (i == 'key' && n != '') {
35 self.ready_doc[i] = n;
36 } else if (i == 'value' && n != '') {
37 self.ready_doc[i] = n;
39 });
41 if (typeof this.ready_doc['value'] == 'undefined') {
42 $.ajatus.debug("Error: Document didn't have value field defined!", "ajatus.document");
43 return this.ready_doc;
46 if (check_metadata) {
47 if (! this.ready_doc.value.metadata) {
48 this.add_metadata();
49 } else {
50 this.check_metadata();
54 if (! $.ajatus.preferences.client.content_types[this.ready_doc.value._type]) {
55 this.ready_doc.value._type = 'note';
58 if (check_schema) {
59 this.check_schema();
62 this.prepare_doc_title();
64 return this.ready_doc;
66 $.extend($.ajatus.document.prototype, {
67 check_schema: function() {
68 var self = this;
69 var type = $.ajatus.preferences.client.content_types[this.ready_doc.value._type];
70 var changed = false;
71 var new_schema_parts = {};
72 $.each(type.original_schema, function(k,si){
73 // console.log("self.ready_doc.value.metadata["+k+"]:"+self.ready_doc.value.metadata[k])
74 if (typeof(self.ready_doc.value[k]) == 'undefined') {
75 def_val = '';
76 if (typeof si['def_val'] != 'undefined') {
77 def_val = si['def_val'];
79 new_schema_parts[k] = {
80 val: def_val,
81 widget: si.widget
83 changed = true;
84 } else {
85 def_val = self.ready_doc.value[k].val || '';
86 if (typeof si['def_val'] != 'undefined') {
87 def_val = si['def_val'];
89 self.ready_doc.value[k] = $.extend({
90 val: def_val,
91 widget: si.widget
92 }, self.ready_doc.value[k]);
94 });
96 if (changed) {
97 // console.log("Found schema parts that need to be updated:");
98 // console.log(new_schema_parts);
99 self._fix_doc_schema(new_schema_parts);
102 _fix_doc_schema: function(new_parts) {
103 var _self = this;
105 doc = new $.ajatus.document.loader(_self.ready_doc._id, _self.ready_doc._rev, false, false);
106 doc.value = $.extend(doc.value, new_parts);
108 var db_path = $.ajatus.preferences.client.content_database;
109 if (doc.value._type == 'tag') {
110 db_path = $.ajatus.preferences.client.tags_database;
113 $.jqCouch.connection('doc').save(db_path, doc);
114 _self.ready_doc = new $.ajatus.document(doc);
116 add_metadata: function() {
117 var self = this;
118 this.ready_doc.value.metadata = {};
119 $.each($.ajatus.document.metadata, function(k,m){
120 self.ready_doc.value.metadata[k] = {
121 val: '',
122 widget: m.widget
126 check_metadata: function() {
127 // console.log("check metadata");
128 var self = this;
129 var changed = false;
130 var new_md_parts = {};
131 $.each($.ajatus.document.metadata, function(k,m){
132 // console.log("self.ready_doc.value.metadata["+k+"]:"+self.ready_doc.value.metadata[k])
133 if (typeof(self.ready_doc.value.metadata[k]) == 'undefined') {
134 def_val = '';
135 if (typeof m['def_val'] != 'undefined') {
136 def_val = m['def_val'];
138 new_md_parts[k] = {
139 val: def_val,
140 widget: m.widget
142 changed = true;
143 } else {
144 def_val = self.ready_doc.value.metadata[k].val || '';
145 if (typeof m['def_val'] != 'undefined') {
146 def_val = m['def_val'];
148 self.ready_doc.value.metadata[k] = $.extend({
149 val: def_val,
150 widget: m.widget
151 }, self.ready_doc.value.metadata[k]);
155 if (changed) {
156 self._fix_metadata(new_md_parts);
159 _fix_metadata: function(new_metadata) {
160 var _self = this;
162 // console.log("_fix_metadata for: "+_self.ready_doc._id);
163 // console.log(new_metadata);
165 doc = new $.ajatus.document.loader(_self.ready_doc._id, _self.ready_doc._rev, false);
167 // console.log("Before:");
168 // console.log(doc.value.metadata);
170 doc.value.metadata = $.extend({}, doc.value.metadata, new_metadata);
172 // console.log("After:");
173 // console.log(doc.value.metadata);
175 var db_path = $.ajatus.preferences.client.content_database;
176 if (doc.value._type == 'tag') {
177 db_path = $.ajatus.preferences.client.tags_database;
180 $.jqCouch.connection('doc').save(db_path, doc);
181 _self.ready_doc = new $.ajatus.document(doc);
183 prepare_doc_title: function() {
184 var self = this;
186 if ( $.ajatus.preferences.client.content_types[this.ready_doc.value._type]['title_item']
187 && ( typeof(this.ready_doc.value['title']) == 'undefined'
188 || this.ready_doc.value['title'].val != '') ) {
189 $.ajatus.preferences.client.content_types[this.ready_doc.value._type].schema['title'] = {
190 label: 'Title',
191 widget: {
192 name: 'text',
193 config: {}
195 hidden: true
198 var title_val = '';
199 $.each($.ajatus.preferences.client.content_types[this.ready_doc.value._type]['title_item'], function(i,part){
200 if (self.ready_doc.value[part]) {
201 var widget = new $.ajatus.widget(self.ready_doc.value[part].widget.name, self.ready_doc.value[part].widget.config);
202 var val = widget.value_on_view(self.ready_doc.value[part].val, 'plain');
203 if (typeof val != 'string') {
204 val = self.ready_doc.value[part].val;
206 title_val += val;
207 } else {
208 title_val += $.ajatus.i10n.get(part);
212 this.ready_doc.value['title'] = {
213 val: title_val,
214 widget: $.ajatus.preferences.client.content_types[this.ready_doc.value._type].schema['title'].widget
219 $.ajatus.document.loader = function(id, rev, check_metadata) {
220 if (typeof check_metadata == 'undefined') {
221 var check_metadata = true;
223 var original = {};
225 var args = {};
226 if (typeof rev != 'undefined') {
227 args['_rev'] = rev;
230 original = new $.ajatus.document(
231 $.jqCouch.connection('doc').get($.ajatus.preferences.client.content_database + '/' + id, args),
232 check_metadata,
233 true
236 return original;
238 $.ajatus.document.metadata = {
239 creator: {
240 label: 'Creator',
241 widget: {
242 name: 'text',
243 config: {}
246 created: {
247 label: 'Created',
248 widget: {
249 name: 'date',
250 config: {}
253 revisor: {
254 label: 'Revisor',
255 widget: {
256 name: 'text',
257 config: {}
260 revised: {
261 label: 'Revised',
262 widget: {
263 name: 'date',
264 config: {}
267 archiver: {
268 label: 'Archiver',
269 widget: {
270 name: 'text',
271 config: {}
274 archived: {
275 label: 'Archived',
276 widget: {
277 name: 'date',
278 config: {}
281 deleted: {
282 label: 'Deleted',
283 widget: {
284 name: 'text',
285 config: {}
287 def_val: false
290 $.ajatus.document.modify_metadata = function(doc, metadata) {
291 $.each(metadata, function(k,v){
292 if ($.ajatus.document.metadata[k])
294 var wdgt = new $.ajatus.widget($.ajatus.document.metadata[k].widget.name, $.ajatus.document.metadata[k].widget.config);
295 doc.value.metadata[k] = {
296 val: wdgt.value_on_save(wdgt.value_on_save(v)),
297 widget: $.ajatus.document.metadata[k].widget
302 return doc;
304 $.ajatus.document.actions = {
305 execute: function(action, tmpdoc) {
306 var doc = new $.ajatus.document.loader(tmpdoc._id, tmpdoc._rev);
308 var act_info = $.ajatus.document.actions._get_action_info(action);
309 if (! act_info) {
310 var msg = $.ajatus.elements.messages.create(
311 $.ajatus.i10n.get('Unknown document action'),
312 $.ajatus.i10n.get('Unknown action %s requested for object %s!', [action, doc.value.title.val])
314 return false;
317 if (act_info.pool_action)
319 var pa = act_info.pool_action;
320 var pai = $.ajatus.document.actions._get_action_info(fa);
321 if (pai) {
322 var pool_action = {
323 action: fa,
324 doc: doc
326 var pool_id = $.ajatus.document.history.pool.add(pool_action);
330 if (act_info.undoable) {
331 $.ajatus.document.actions._execute_temp(act_info.action, doc);
332 } else {
333 $.ajatus.document.actions._execute_final(act_info.action, doc);
336 _execute_temp: function(action, doc) {
337 // console.log("_execute_temp: "+action);
338 switch(action) {
339 case 'delete':
340 var on_success = function(data) {
341 var tmpdoc = {
342 _id: doc._id,
343 _rev: doc._rev
345 var msg = $.ajatus.elements.messages.create(
346 $.ajatus.i10n.get('Object deleted'),
347 $.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>'
349 msg.bind_in_content('click', 'a.undo', function(e){
350 $.ajatus.document.actions.execute("undelete", tmpdoc);
351 msg.fade('out', true);
354 return data;
357 doc = $.ajatus.document.modify_metadata(doc, {
358 deleted: true,
359 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
360 revisor: $.ajatus.preferences.local.user.email
363 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
365 $('#object_'+doc._id, $.ajatus.application_content_area).hide().addClass('deleted');
366 break;
367 case 'archive':
368 var on_success = function(data) {
369 var tmpdoc = {
370 _id: doc._id,
371 _rev: doc._rev
373 var msg = $.ajatus.elements.messages.create(
374 $.ajatus.i10n.get('Object archived'),
375 $.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>'
377 msg.bind_in_content('click', 'a.undo', function(e){
378 $.ajatus.document.actions.execute("unarchive", tmpdoc);
379 msg.fade('out', true);
382 return data;
385 doc = $.ajatus.document.modify_metadata(doc, {
386 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
387 revisor: $.ajatus.preferences.local.user.email,
388 archived: $.ajatus.formatter.date.js_to_iso8601(new Date()),
389 archiver: $.ajatus.preferences.local.user.email
392 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
394 $('#object_'+doc._id, $.ajatus.application_content_area).hide().addClass('archived');
395 break;
398 _execute_final: function(action, doc) {
399 switch(action) {
400 case 'delete':
401 var on_success = function(data) {
402 var msg = $.ajatus.elements.messages.create(
403 $.ajatus.i10n.get('Object deleted'),
404 $.ajatus.i10n.get('Object %s removed from Ajatus.', [doc.value.title.val])
407 return data;
409 $.jqCouch.connection('doc', on_success).del($.ajatus.preferences.client.content_database, doc);
410 $('#object_'+doc._id, $.ajatus.application_content_area).remove();
411 break;
412 case 'undelete':
413 var on_success = function(data) {
414 var msg = $.ajatus.elements.messages.create(
415 $.ajatus.i10n.get('Object restored'),
416 $.ajatus.i10n.get('Object %s restored succesfully.', [doc.value.title.val])
419 return data;
421 doc = $.ajatus.document.modify_metadata(doc, {
422 deleted: false,
423 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
424 revisor: $.ajatus.preferences.local.user.email
426 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
428 $('#object_'+doc._id, $.ajatus.application_content_area).not('.deleted').remove();
429 $('#object_'+doc._id, $.ajatus.application_content_area).filter('.deleted').show().removeClass('deleted');
430 break;
431 case 'unarchive':
432 var on_success = function(data) {
433 var msg = $.ajatus.elements.messages.create(
434 $.ajatus.i10n.get('Object restored'),
435 $.ajatus.i10n.get('Object %s restored succesfully.', [doc.value.title.val])
438 return data;
440 doc = $.ajatus.document.modify_metadata(doc, {
441 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
442 revisor: $.ajatus.preferences.local.user.email,
443 archived: '',
444 archiver: ''
446 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.content_database, doc);
448 $('#object_'+doc._id, $.ajatus.application_content_area).filter('.archived').show().removeClass('archived');
449 break;
452 empty_pool: function () {
453 var items = $.ajatus.document.history.pool.get_all();
454 if (items.length > 0) {
455 var msg = $.ajatus.elements.messages.create(
456 $.ajatus.i10n.get('Emptying action pool'),
457 $.ajatus.i10n.get('Running %d pooled actions.', [items.length])
460 $.each(items, function(i,item){
461 $.ajatus.document.history.pool.remove(i);
462 $.ajatus.document.actions.execute(item.action, item.doc);
465 _get_action_info: function(action) {
466 switch(action) {
467 case 'delete':
468 return {
469 action: 'delete',
470 label: $.ajatus.i10n.get('Delete'),
471 undoable: true,
472 undo_label: $.ajatus.i10n.get('Undelete'),
473 pool_action: false
475 break;
476 case 'delete_final':
477 return {
478 action: 'delete',
479 label: $.ajatus.i10n.get('Delete'),
480 undoable: false,
481 pool_action: false
483 break;
484 case 'undelete':
485 return {
486 action: 'undelete',
487 label: $.ajatus.i10n.get('Undelete'),
488 undoable: false,
489 pool_action: false
491 break;
492 case 'archive':
493 return {
494 action: 'archive',
495 label: $.ajatus.i10n.get('Archive'),
496 undoable: true,
497 undo_label: $.ajatus.i10n.get('Restore'),
498 pool_action: false
500 break;
501 case 'unarchive':
502 return {
503 action: 'unarchive',
504 label: $.ajatus.i10n.get('Restore'),
505 undoable: false,
506 pool_action: false
508 break;
509 default:
510 return false;
515 $.ajatus.document.history = {};
516 $.ajatus.document.history.pool = {
517 items: [],
519 add: function(action) {
520 var new_length = $.ajatus.document.history.pool.items.push(action);
521 var pool_id = new_length-1;
522 return pool_id;
524 remove: function(item_id) {
525 $.ajatus.document.history.pool.items = $.grep($.ajatus.document.history.pool.items, function(n,i){
526 return i != item_id;
529 get_item: function(item_id) {
530 if (items[item_id]) {
531 return items[item_id];
532 } else {
533 return false;
536 get_all: function() {
537 return $.ajatus.document.history.pool.items;
541 $.ajatus.document.additionals = {
542 defaults: {
543 auto_show: true
545 open: [],
546 active: null,
547 top_start: 22,
548 left_start: 20
550 $.extend($.ajatus.document.additionals, {
551 create: function(content_type, row_holder, opts) {
552 $.ajatus.document.additionals._reset();
554 var id = $.ajatus.document.additionals._generate_id();
556 var options = $.ajatus.document.additionals.defaults;
557 options = $.extend(
558 options,
559 opts || {}
562 var af_win = $.ajatus.document.additionals._create_structure(id, options);
563 $('.title', af_win).html($.ajatus.i10n.get('Add field'));
565 var jqform = $('.form', af_win);
566 var contents = $('.contents', af_win);
568 var wdgts = $.ajatus.document.additionals._generate_widget_list();
570 $('<label for="widget"/>').html($.ajatus.i10n.get('Widget')).appendTo(contents);
571 var wdgt_sel_holder = $('<div id="widget_selection_holder"/>');
572 wdgt_sel_holder.html(wdgts).appendTo(contents);
574 var wdgt_details = $('<div id="widget_details_holder"/>');
575 wdgt_details.appendTo(contents);
577 var wdgts_on_change = function(e){
578 var sel_w = e.currentTarget.value;
579 if (sel_w != '') {
580 wdgt_sel_holder.html($.ajatus.i10n.get(sel_w));
581 wdgt_sel_holder.addClass('selected');
582 wdgt_details.html($.ajatus.document.additionals._generate_widget_details(sel_w));
584 var field = $('<input type="hidden" />').attr({
585 name: '_widget',
586 value: sel_w
588 field.appendTo(jqform);
592 wdgts.bind('change', wdgts_on_change);
594 wdgt_sel_holder.bind('dblclick', function(e){
595 wdgt_sel_holder.removeClass('selected');
596 wdgt_sel_holder.html(wdgts);
597 wdgts.bind('change', wdgts_on_change);
600 $('.actions #submit_save', af_win).bind('click', function(e){
601 var result = $.ajatus.document.additionals.save_widget(jqform.formToArray(false), jqform);
603 if (result) {
604 $.ajatus.document.additionals.close(id);
607 return false;
610 if (options.auto_show) {
611 $.ajatus.document.additionals.show(id);
614 edit: function(content_type, row_holder, data, opts) {
615 $.ajatus.document.additionals._reset();
617 var id = $.ajatus.document.additionals._generate_id();
618 console.log("Edit win id: "+id);
620 var options = $.ajatus.document.additionals.defaults;
621 options = $.extend(
622 options,
623 opts || {}
626 var af_win = $.ajatus.document.additionals._create_structure(id, options);
627 $('.title', af_win).html($.ajatus.i10n.get('Edit field %s', [data.name]));
629 $('.actions #submit_save', af_win).bind('click', function(e){
630 console.log("save additional field (on edit)");
631 $.ajatus.document.additionals.close(id);
634 if (options.auto_show) {
635 $.ajatus.document.additionals.show(id);
638 config: function(widget, prev_vals, on_save) {
639 if (typeof prev_vals == 'undefined') {
640 var prev_vals = {};
642 if (typeof on_save == 'undefined') {
643 var on_save = '$.ajatus.document.additionals.save_widget_settings';
646 var id = $.ajatus.document.additionals._generate_id();
648 var af_win = $.ajatus.document.additionals._create_structure(id);
649 $('.title', af_win).html($.ajatus.i10n.get('Configure widget %s', [$.ajatus.i10n.get(widget.name)]));
651 var jqform = $('.form', af_win);
652 var contents = $('.contents', af_win);
654 var wdgt_settings = $('<div id="widget_settings_holder" />');
655 wdgt_settings.appendTo(contents);
656 wdgt_settings.html($.ajatus.document.additionals._generate_widget_settings(widget, prev_vals));
658 $('.actions #submit_save', af_win).bind('click', function(e){
659 var parent_win = $.ajatus.document.additionals._get_parent();
661 var fn = on_save;
662 if (typeof on_save == 'string') {
663 fn = eval(on_save);
665 var result = fn.apply(fn, [jqform.formToArray(false), parent_win, jqform]);
667 if (result) {
668 $.ajatus.document.additionals.close(id);
672 $.ajatus.document.additionals.show(id);
674 show: function(id) {
675 $.ajatus.document.additionals.active = id;
676 $.ajatus.document.additionals.open.push(id);
677 $.ajatus.document.additionals._position(id);
679 $('#'+id).show();
681 close: function(id) {
682 if (typeof id == 'undefined') {
683 var id = $.ajatus.document.additionals.active;
686 $('#'+id).hide();
688 var active_idx = null;
689 var still_open = $.grep($.ajatus.document.additionals.open, function(n,i){
690 if (n != id) {
691 return true;
692 } else {
693 active_idx = i;
694 return false;
698 if ( active_idx > 0
699 && $.ajatus.document.additionals.open.length-1 >= (active_idx-1))
701 $.ajatus.document.additionals.active = still_open[active_idx-1];
702 } else {
703 $.ajatus.document.additionals.active = null;
706 $.ajatus.document.additionals.open = still_open;
708 save_widget_settings: function(form_data, parent_win_id, form) {
709 var form_values = {};
711 $.each(form_data, function(i,row){
712 if (row.name.toString().match(/__(.*?)/)) {
713 return;
715 if (row.name.substr(0,6) != "widget") {
716 var item = {};
717 var widget = {};
718 var prev_val = false;
719 var name_parts = [];
720 var name_parts_count = 0;
721 if (row.name.toString().match(/;/g)) {
722 name_parts = row.name.toString().split(";");
723 name_parts_count = name_parts.length;
726 $.each(form_data, function(x,r){
727 if (r.name == 'widget['+row.name+':name]') {
728 widget['name'] = r.value;
729 } else if (r.name == 'widget['+row.name+':config]') {
730 widget['config'] = $.ajatus.converter.parseJSON(r.value);
731 } else if (r.name == 'widget['+row.name+':prev_val]') {
732 prev_val = $.ajatus.converter.parseJSON(r.value);
736 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
738 item['val'] = wdgt.value_on_save(row.value, prev_val);
739 item['widget'] = widget;
741 if (name_parts_count > 0) {
742 var prevs = [];
743 for (var i=0; i < name_parts_count; i++) {
744 var key = "['"+name_parts[i]+"']";
746 if (prevs.length > 0) {
747 var key_prefix = '';
748 $.each(prevs, function(pi, pk){
749 key_prefix = "['" + pk + "']" + key_prefix;
751 key = key_prefix + key;
754 if (typeof eval("form_values"+key) == 'undefined') {
755 eval("form_values"+key+"={};");
758 prevs.push(name_parts[i]);
759 if (i == name_parts_count-1) {
760 if ($.browser.mozilla) {
761 if (typeof item == 'object') {
762 eval("form_values"+key+"="+item.toSource()+";");
763 } else {
764 eval("form_values"+key+"="+item+";");
766 } else {
767 eval("form_values"+key+"="+item+";");
771 } else {
772 form_values[row.name] = item;
777 var config_holder = $('#'+parent_win_id+' .saved_settings');
778 config_holder.html('');
780 $.each(form_values, function(k,item){
781 var field = $('<input type="hidden" />').attr({
782 name: 'config;'+k,
783 value: $.ajatus.converter.toJSON(item.val)
785 field.appendTo(config_holder);
788 return true;
790 save_widget: function(form_data, form) {
791 var form_values = {};
792 var sel_widget = 'text';
794 // console.log(form_values);
796 $.each(form_data, function(i,row){
797 if (row.name.toString().match(/__(.*?)/)) {
798 return;
800 if (row.name == '_widget') {
801 sel_widget = String(row.value);
803 else if (row.name.substr(0,6) != "widget") {
804 var item = {};
805 var widget = {};
806 var prev_val = false;
807 var name_parts = [];
808 var name_parts_count = 0;
810 if (row.name.toString().match(/;/g)) {
811 name_parts = row.name.toString().split(";");
812 name_parts_count = name_parts.length;
815 $.each(form_data, function(x,r){
816 if (r.name == 'widget['+row.name+':name]') {
817 widget['name'] = r.value;
818 } else if (r.name == 'widget['+row.name+':config]') {
819 widget['config'] = $.ajatus.converter.parseJSON(r.value);
820 } else if (r.name == 'widget['+row.name+':prev_val]') {
821 prev_val = $.ajatus.converter.parseJSON(r.value);
825 if (typeof widget['name'] == 'undefined') {
826 return;
829 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
831 item['val'] = wdgt.value_on_save(row.value, prev_val);
832 item['widget'] = widget;
834 if (name_parts_count > 0) {
835 console.log("name_parts_count > 0");
836 var prevs = [];
837 for (var i=0; i < name_parts_count; i++) {
838 var key = "['"+name_parts[i]+"']";
840 if (prevs.length > 0) {
841 var key_prefix = '';
842 $.each(prevs, function(pi, pk){
843 key_prefix = "['" + pk + "']" + key_prefix;
845 key = key_prefix + key;
848 if (typeof eval("form_values"+key) == 'undefined') {
849 eval("form_values"+key+"={};");
852 prevs.push(name_parts[i]);
853 if (i == name_parts_count-1) {
854 if ($.browser.mozilla) {
855 if (typeof item == 'object') {
856 eval("form_values"+key+"="+item.toSource()+";");
857 } else {
858 eval("form_values"+key+"="+item+";");
860 } else {
861 eval("form_values"+key+"="+item+";");
865 } else {
866 form_values[row.name] = item;
871 // var additional_items = {};
873 var config = {};
874 if (typeof form_values['config'] != 'undefined') {
875 $.each(form_values['config'], function(i,n){
876 if (typeof n == 'object') {
877 config[i] = $.ajatus.converter.parseJSON(n.val);
878 } else {
879 config[i] = n;
881 });
884 var item_name = form_values.name.val.toString().toLowerCase();
886 if (item_name == '') {
887 return false;
890 var additional_item = {
891 label: form_values.label.val,
892 widget: {
893 name: sel_widget,
894 config: config
896 def_val: form_values.def_val.val,
897 required: form_values.required.val
899 // additional_items[item_name] = additional_item;
900 // console.log(additional_items);
901 console.log(additional_item);
903 var row_holder = $('.form_structure ul:eq(0)', $.ajatus.forms.active);
905 $.ajatus.renderer.form_helpers._add_additional_row('create', row_holder, item_name, additional_item);
907 return true;
909 generate_item_actions_tpl: function(name, widget, doc) {
910 return [
911 'img', { className: 'additional_edit_btn', src: 'images/icons/edit.png', title: $.ajatus.i10n.get('Edit'), alt: $.ajatus.i10n.get('Edit') }, '',
912 'img', { className: 'additional_delete_btn', src: 'images/icons/trash.png', title: $.ajatus.i10n.get('Delete'), alt: $.ajatus.i10n.get('Delete') }, '',
915 _get_parent: function() {
916 var prev_id = $.ajatus.document.additionals.open[$.ajatus.document.additionals.open.length-2];
918 return prev_id;
920 _position: function(id) {
921 var win = $('#' + id);
922 var top = $.ajatus.document.additionals.top_start;
923 var left = $.ajatus.document.additionals.left_start;
925 if ($.ajatus.document.additionals.open.length > 1) {
926 var incr = $.ajatus.document.additionals.open.length * 2;
927 top += incr;
928 left += incr;
931 win.css({
932 top: top+'%',
933 left: left+'%'
936 _create_structure: function(id, options) {
937 var af_win = $('<div class="additionals_window" />').attr({
938 id: id
939 }).hide();
940 var title = $('<h2 class="title"/>');
941 var form = $('<form id="additionals_window_form" class="form" />');
942 var content_area = $('<div class="contents"/>');
943 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') + '" />');
945 af_win.appendTo($.ajatus.application_dynamic_elements);
946 title.appendTo(af_win);
947 form.appendTo(af_win);
948 content_area.appendTo(form);
949 actions.appendTo(af_win);
951 $('#submit_cancel', actions).bind('click', function(e){
952 $.ajatus.document.additionals.close(id);
953 return false;
956 return af_win;
958 _generate_widget_list: function() {
959 var select = $('<select id="widget" name="widget" />');
961 $.each($.ajatus.widgets.loaded_widgets, function(i,w){
962 var opt = $('<option />').attr({
963 value: w
964 }).html($.ajatus.i10n.get(w));
965 opt.appendTo(select);
968 var opt = $('<option />').attr({
969 value: '',
970 selected: 'selected'
971 }).html($.ajatus.i10n.get('Select one') + '&nbsp;');
972 opt.prependTo(select);
974 return select;
976 _generate_widget_details: function(widget_name, data) {
977 var widget = $.ajatus.widget(widget_name);
978 var details = $('<p>' + $.ajatus.i10n.get("Widget %s doesn't support dynamic creation.", [$.ajatus.i10n.get(widget_name)]) + '</p>');
979 if (typeof(widget['create_widget_details']) == 'function') {
980 details = widget.create_widget_details(data || {});
983 return details;
985 _generate_widget_settings: function(widget_name, data) {
986 var widget = $.ajatus.widget(widget_name);
987 var details = $('<p>' + $.ajatus.i10n.get("Widget %s doesn't support dynamic settings.", [$.ajatus.i10n.get(widget_name)]) + '</p>');
988 if (typeof(widget['create_widget_settings']) == 'function') {
989 details = widget.create_widget_settings(data || {});
992 return details;
994 _reset: function() {
995 $.ajatus.document.additionals.open = [];
996 $.ajatus.document.additionals.active = null;
997 $.ajatus.application_dynamic_elements.html('');
999 _generate_id: function()
1001 return "additionals_window_" + $.ajatus.utils.generate_id();
1005 })(jQuery);