Don't override existing signal listeners
[ajatus.git] / js / views / system.js
blob3eeaf2665e507f5a395148127a8a848c3cda9796
1 /*
2  * This file is part of
3  *
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
12  * 
13  */
15 (function($){
16     $.ajatus = $.ajatus || {};
17     $.ajatus.views = $.ajatus.views || {};
18     
19     $.ajatus.views.system = {
20         available: [
21             'frontpage', 'edit', 'create', 'item', 'tags', 'trash'
22         ]
23     };
24     $.ajatus.views.system.frontpage = {
25         title: 'Frontpage',
26         options: {
27             group_item_count: 3,
28             max_items_before_pool: 3
29         },
30         in_tabs: true,
31         history_support: true,
32         
33         pool_settings: {
34             enabled: false,
35             settings: {
36                 tick_limit: 4
37             }
38         },
39                 
40         tab: {
41             on_click: function(e)
42             {
43                 $.ajatus.tabs.on_click(e);
44                 $.ajatus.views.system.frontpage.render();
45             }
46         },
47     
48         render: function()
49         {
50             $.ajatus.application_content_area.html('');
51             $.ajatus.layout.body.set_class('frontpage');
52             $.ajatus.views.on_change("#view.frontpage");
53             
54             $.each($.ajatus.preferences.client.content_types, function(key,type){
55                 if (   !type.views['list']
56                     || type.on_frontpage == false) {
57                     return false;
58                 }
59             
60                 var on_success = function(data) {
61                     if (data.total_rows == 0) {
62                         return;
63                     }
64                     
65                     var pool_settings = $.ajatus.views.system.frontpage.pool_settings;
66                     if (data.total_rows >= $.ajatus.views.system.frontpage.options.max_items_before_pool) {
67                         pool_settings.enabled = true;
68                     }                    
69                     
70                     var renderer = new $.ajatus.renderer.list(
71                         $.ajatus.application_content_area,
72                         type,
73                         {
74                             id: type.name + '_list_holder',
75                             append: true,
76                             pool: pool_settings
77                         }
78                     );
80                     $.each(data.rows, function(i,doc){
81                         var doc = new $.ajatus.document(doc);
82                         renderer.render_item(doc, i);
83                     });
84                     
85                     return data;
86                 };
88                 if (   $.ajatus.tags.active != ''
89                     && typeof type.views['list_at'] != 'undefined')
90                 {
91                     $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, type.views['list_at'], {
92                         count: $.ajatus.views.system.frontpage.options.group_item_count,
93                         descending: true
94                     });
95                 } else {
96                     $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, type.name+'/list', {
97                         count: $.ajatus.views.system.frontpage.options.group_item_count,
98                         descending: true
99                     });                        
100                 }
101             });
102         }
103     };
104     $.ajatus.views.system.tags = {
105         title: 'Tags',
106         options: {            
107         },
108         in_tabs: true,
109         application_tab: true,
110         history_support: true,
111         icon: 'tag.png',
112                 
113         tab: {
114             on_click: function(e) {
115                 $.ajatus.tabs.on_click(e);
116                 $.ajatus.views.system.tags.render();
117             }
118         },
119         
120         render: function() {
121             $.ajatus.application_content_area.html('');
122             $.ajatus.layout.body.set_class('tags');
123             $.ajatus.views.on_change("#view.tags");
124             
125             $.ajatus.toolbar.init();
126             
127             $.ajatus.views.system.tags.get_items();
128         },
130         render_results: function(tags, items) {
131             var type = $.ajatus.preferences.client.content_types['tag'];
132             
133             var renderer = new $.ajatus.renderer.list(
134                 $.ajatus.application_content_area,
135                 type,
136                 {
137                     id: 'tags_list_holder',
138                     headers: [
139                         'title', 'context', 'value', 'created', 'creator', 'used'
140                     ],
141                     pool: {
142                         type: 'scroll'
143                     }
144                 }
145             );
146             
147             var x = 0;
148             $.each(tags, function(i,doc){
149                 var doc = new $.ajatus.document(doc);
150                 doc.value['used'] = {
151                     val: (typeof items[doc._id] != 'undefined' ? items[doc._id].length : 0)
152                 };
153                 renderer.render_item(doc, x);
154                 x+=1;
155             });
156             
157             renderer.enable_sorting();
158             
159             $.ajatus.views.system.tags.get_duplicates(items);
160         },
161         
162         render_duplicates: function(rows, items) {
163             var type = $.ajatus.preferences.client.content_types['tag'];
164             
165             var duplicates = [];
166             var duplicate_ids = [];
167             
168             var tags = {};
169             
170             var hashes = [];
171             var real_hashes = [];
172             var false_hashes = {};
173             var broken_hashes = {};
174             var ids = [];
175             
176             var real_hashes_cnt = 0;
177             var false_hashes_cnt = 0;
178             
179             $.each (rows, function(i,doc){
180                 var doc = new $.ajatus.document(doc);
181                 // console.log(doc._id);
182                 // console.log(doc.key);
183                 // console.log(doc.value.context.val);
184                 // console.log(doc.value.value.val);
185                 
186                 var tag_hash = $.ajatus.utils.md5.encode(doc.value.context.val + ':' + doc.key + '=' + doc.value.value.val);
187                 
188                 hashes.push(tag_hash);
189                 ids.push(doc._id);
191                 if (tag_hash == doc._id) {
192                     real_hashes.push(tag_hash);
193                     real_hashes_cnt += 1;
194                 } else {
195                     if (typeof false_hashes[tag_hash] == 'undefined') {
196                         false_hashes[tag_hash] = [doc._id];
197                         false_hashes_cnt += 1;
198                     } else {
199                         false_hashes[tag_hash].push(doc._id);
200                         false_hashes_cnt += 1;
201                     }
202                 }
204                 tags[doc._id] = doc;
205             });
206             
207             // console.log("rows length: "+rows.length);
208             // 
209             // console.log("real_hashes ("+real_hashes_cnt+"): ");
210             // console.log(real_hashes);
211             // console.log("false_hashes ("+false_hashes_cnt+"): ");
212             // console.log(false_hashes);
213             
214             var duplicates_with = {};
215             
216             var broken_hashes_cnt = 0;
217             var dublicate_cnt = 0;
218             
219             $.each(false_hashes, function(hash,hids){
220                 if (typeof duplicates_with[hash] == 'undefined') {
221                     duplicates_with[hash] = [];
222                 }
223                 
224                 if (hids.length > 1) {
225                     if ($.inArray(hash, real_hashes) != -1) {
226                         duplicates_with[hash] = hids;
227                         dublicate_cnt += 1;
228                     } else {
229                         broken_hashes[hash] = hids;
230                         broken_hashes_cnt += 1;
231                     }
232                 } else {
233                     if ($.inArray(hash, real_hashes) != -1) {
234                         duplicates_with[hash].push(hids[0]);
235                         dublicate_cnt += 1;
236                     } else {
237                         broken_hashes[hash] = hids;
238                         broken_hashes_cnt += 1;
239                     }
240                 }
241             });
243             // console.log("broken_hashes ("+broken_hashes_cnt+"): ");
244             // console.log(broken_hashes);
245             
246             if (dublicate_cnt > 0) {
247                 var drenderer = new $.ajatus.renderer.list(
248                     $.ajatus.application_content_area,
249                     type,
250                     {
251                         id: 'tag_dups_list_holder',
252                         title: $.ajatus.i10n.get("Duplicate"),
253                         headers: [
254                             'title', 'context', 'value', 'creator', 'used'
255                         ],
256                         append: true,
257                         item_id_suffix: '_duplicate',
258                         show_actions: false,
259                         className: 'list_holder duplicates_list'
260                     }
261                 );
263                 var dub_tags = [];
264                 var x = 0;            
265                 $.each(duplicates_with, function(hash, dids){
266                     for (var i=0; i<dids.length; i++) {
267                         if (typeof tags[dids[i]] != 'undefined') {
268                             var d = tags[dids[i]];
269                             d.value['used'] = {
270                                 val: (typeof items[d._id] != 'undefined' ? items[d._id].length : 0)
271                             };
272                             d.value['real_hash'] = {
273                                 val: hash
274                             };
275                             drenderer.render_item(d, x);
276                             dub_tags.push(d);
277                             x+=1;
278                         } else {
279                             $.ajatus.debug("Couldn't find tag with id: "+dids[i]);
280                         }
281                     }
282                 });
283                 drenderer.enable_sorting();
285                 var merger = new $.ajatus.tags.merger(dub_tags, duplicates_with, tags);                
286             }
287             
288             if (broken_hashes_cnt > 0) {
289                 var brenderer = new $.ajatus.renderer.list(
290                     $.ajatus.application_content_area,
291                     type,
292                     {
293                         id: 'tag_broken_list_holder',
294                         title: $.ajatus.i10n.get("Broken"),
295                         plural_title: false,
296                         headers: [
297                             'title', 'context', 'value', 'curr_hash', 'real_hash', 'used'
298                         ],
299                         schema: {
300                             'curr_hash': {
301                                 label: 'Current hash'
302                             },
303                             'real_hash': {
304                                 label: 'Real hash'
305                             }
306                         },
307                         append: true,
308                         item_id_suffix: '_broken',
309                         show_actions: false,
310                         className: 'list_holder broken_list'
311                     }
312                 );
313                 
314                 var broken_tags = {};
315                 var x = 0;
316                 $.each(broken_hashes, function(hash, dids){
317                     for (var i=0; i<dids.length; i++) {
318                         if (typeof tags[dids[i]] != 'undefined') {
319                             var d = tags[dids[i]];
320                             d.value['used'] = {
321                                 val: (typeof items[d._id] != 'undefined' ? items[d._id].length : 0)
322                             };
323                             d.value['curr_hash'] = {
324                                 val: d._id
325                             };
326                             d.value['real_hash'] = {
327                                 val: hash
328                             };
329                             brenderer.render_item(d, x);
330                             broken_tags[d._id] = d;
331                             x+=1;
332                         } else {
333                             $.ajatus.debug("Couldn't find tag with id: "+dids[i]);
334                         }
335                     }
336                 });
337                 brenderer.enable_sorting();
339                 var fixer = new $.ajatus.tags.fixer(broken_tags, broken_hashes, tags);                
340             }
341         },
342         
343         get_items: function(on_success) {
344             var conn_on_success = function(data) {                
345                 if (data.total_rows == 0) {
346                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get('tag'));
347                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
348                     return;
349                 }
350                 
351                 var tags = {};
352                 var items = {};
353                 
354                 $.each(data.rows, function(i,n){
355                     if (n.key[1] == 0) {
356                         tags[n.key[0]] = n;
357                     } else {
358                         if (typeof items[n.key[0]] == 'undefined') {
359                             items[n.key[0]] = [];
360                         }
361                         items[n.key[0]].push(n);
362                     }
363                 });
365                 if (typeof on_success == 'undefined') {
366                     on_success = $.ajatus.views.system.tags.render_results;
367                 }
368                 
369                 on_success(tags, items);
370                 
371                 return data;
372             };
374             $.jqCouch.connection('view', conn_on_success).get($.ajatus.preferences.client.content_database, 'tag/list_with_docs', {
375                 // startkey: ["ED862081B2FBF46FB0CDBBE848ED6856", 0],
376                 // endkey: ["ED862081B2FBF46FB0CDBBE848ED6856", 1]
377             });
378         },
379         
380         get_duplicates: function(items) {
381             var conn_on_success = function(data) {                
382                 if (data.total_rows == 0) {
383                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get('dublicate'));
384                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
385                     return;
386                 }
388                 if (typeof on_success == 'undefined') {
389                     on_success = $.ajatus.views.system.tags.render_duplicates;
390                 }
391                 
392                 on_success(data.rows, items);
393                 
394                 return data;
395             };
397             $.jqCouch.connection('view', conn_on_success).get($.ajatus.preferences.client.content_database, 'tag/list_by_title', {
398             });
399         }
401     };
402     $.ajatus.views.system.trash = {
403         title: 'Trash',
404         options: {
405             group_item_count: 10
406         },
407         in_tabs: true,
408         application_tab: true,
409         history_support: true,
410         icon: 'trash.png',
411         
412         tab: {
413             on_click: function(e)
414             {
415                 $.ajatus.tabs.on_click(e);
416                 $.ajatus.views.system.trash.render();
417             }
418         },
419         
420         get_items: function(on_success) {            
421             var trash_view = 'if (doc.value.metadata.deleted.val == true) {';
422             trash_view += 'map( doc.value.metadata.revised, {"_type": doc.value._type,';
423             trash_view += '"title": doc.value.title,';
424             
425             $.each($.ajatus.preferences.client.content_types, function(key,type){
426                 if (type['title_item']) {
427                     $.each(type['title_item'], function(i,part){
428                         if (type.schema[part]) {
429                             trash_view += '"'+part+'": doc.value.'+part+',';
430                         }
431                     });
432                 }
433             });
435             trash_view += '"revised": doc.value.metadata.revised,"by": doc.value.metadata.revisor,"tags": doc.value.tags})}';
436             
437             var conn_on_success = function(data) {
438                 if (data.total_rows == 0) {
439                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get('deleted item'));
440                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
441                     return;
442                 }
443                 
444                 if (typeof on_success == 'undefined') {
445                     on_success = $.ajatus.views.system.trash.render_results;
446                 }
447                 
448                 on_success(data.rows);
449                 return data;
450             };
451             
452             $.jqCouch.connection('view', conn_on_success).temp($.ajatus.preferences.client.content_database, "$.ajatus.views.generate('"+trash_view+"')", {
453                 count: $.ajatus.views.system.trash.options.group_item_count,
454                 descending: true
455             });
456         },
457         
458         render: function()
459         {
460             $.ajatus.application_content_area.html('');
461             $.ajatus.layout.body.set_class('trash');
462             $.ajatus.views.on_change("#view.trash");
464             $.ajatus.toolbar.init();
465             $.ajatus.toolbar.show();
466             
467             $.ajatus.views.system.trash.get_items();
468         },
469         
470         render_results: function(rows)
471         {
472             var result_count = {};
473             var show_empty_trash = false;
474             
475             $.each($.ajatus.preferences.client.content_types, function(key,type){
476                 if (!type.views['list']) {
477                     return false;
478                 }
479                 
480                 result_count[type.name] = 0;
481                 
482                 $.each(rows, function(i,doc){
483                     if (doc.value._type == type.name) {
484                         result_count[type.name] += 1;
485                     }
486                 });
487                 
488                 if (result_count[type.name] == 0) {
489                     return;
490                 }
491                 
492                 show_empty_trash = true;
493                 
494                 var renderer = new $.ajatus.renderer.list(
495                     $.ajatus.application_content_area,
496                     false,
497                     {
498                         id: type.name + '_list_holder',
499                         append: true,
500                         title: type.title,
501                         headers: [
502                             'title', 'revised', 'by'
503                         ],
504                         schema: $.extend({
505                             by: {
506                                 label: 'By'
507                             }
508                         }, type.schema)
509                     }
510                 );
512                 $.each(rows, function(i,doc){
513                     if (doc.value._type == type.name) {
514                         var doc = new $.ajatus.document(doc);
515                         
516                         renderer.set_actions(
517                             [
518                                 {
519                                     name: 'view',
520                                     title: $.ajatus.i10n.get('View'),
521                                     icon: 'view.png',
522                                     click_action: '$.ajatus.views.system.item.render(doc);'
523                                 },
524                                 {
525                                     name: 'undelete',
526                                     title: $.ajatus.i10n.get('Undelete'),
527                                     icon: 'undo.png',
528                                     click_action: '$.ajatus.document.actions.execute("undelete", doc);'
529                                 },
530                                 {
531                                     name: 'delete_final',
532                                     title: $.ajatus.i10n.get('Delete'),
533                                     icon: 'trash.png',
534                                     click_action: '$.ajatus.document.actions.execute("delete_final", doc);'
535                                 }
536                             ]
537                         );
538                         renderer.render_item(doc, i);
539                     }
540                 });
541             
542                 renderer.enable_sorting();
543             });
544             
545             if (show_empty_trash) {
546                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Empty trash'), 'trash.png', function(){
547                     $.ajatus.views.system.trash.empty();
548                 });
549             }
550         },
551         empty: function() {
552             var answer = confirm($.ajatus.i10n.get("Deleting all items in trash. Are you sure?"));
553             if (answer) {
554                 $.ajatus.views.system.trash.get_items($.ajatus.views.system.trash.execute_empty);                
555             }
556         },
557         execute_empty: function(rows) {            
558             $.each(rows, function(i,doc){
559                 doc = new $.ajatus.document(doc);
560                 $.ajatus.document.actions.execute("delete_final", doc);
561             });
562         }
563     };
564     $.ajatus.views.system.edit = {
565         title: 'Edit',
566         options: {
567         },
568         in_tabs: false,
569         dynamic_history: true
570     };
571     $.extend($.ajatus.views.system.edit, {
572         render: function(type, doc, use_given_doc)
573         {
574             if (typeof use_given_doc == 'undefined') {
575                 use_given_doc = false;
576             }
577             
578             var content_type = type; 
579             
580             if (! use_given_doc) {
581                 var doc = new $.ajatus.document.loader(doc._id, doc._rev);                
582             }
583             
584             if (! type) {
585                 content_type = $.ajatus.preferences.client.content_types[doc.value._type];
586             }
587                         
588             if (typeof(type) == 'string') {
589                 content_type = $.ajatus.preferences.client.content_types[type];
590             }
591             
592             $.ajatus.layout.body.set_class('edit '+content_type.name);
593             
594             var view_hash = '#edit.'+doc.value._type+'.'+doc._id+'.'+doc._rev;
595             $.ajatus.history.update(view_hash);
596             $.ajatus.views.on_change(view_hash);
597             
598             var latest_rev = $.ajatus.document.revisions.get(doc).active.rev;
599             
600             if (   content_type.custom_renderer
601                 && typeof(content_type.custom_renderer['edit']) == 'function')
602             {
603                 var renderer = content_type.custom_renderer.edit(doc, latest_rev);
604             } else {            
605                 var renderer = new $.ajatus.renderer.form(content_type, doc, false, latest_rev);
606                 $.ajatus.application_content_area.html(renderer.form);
607                 $.ajatus.forms.register.normal(renderer.form);
608             }
609         },
610         history_register: function() {
611             $.ajatus.history.add_dynamic_map(['edit'], 3, '$.ajatus.views.system.edit.history_render');
612         },
613         history_render: function() {
614             var type = arguments[0];
615             var id = arguments[1];
616             var rev = false;
618             if (   typeof arguments[2] != 'undefined'
619                 && arguments[2] != '')
620             {
621                 rev = arguments[2];
622             }
623             
624             var doc = new $.ajatus.document.loader(id, rev);
625             $.ajatus.views.system.edit.render(type, doc);
626         }
627     });
628     
629     $.ajatus.views.system.create = {
630         title: 'Create',
631         options: {
632         },
633         in_tabs: false,
634         dynamic_history: true
635     };
636     $.extend($.ajatus.views.system.create, {
637         render: function(type, doc)
638         {
639             var content_type = type; 
640             
641             if (typeof(type) == 'string') {
642                 content_type = $.ajatus.preferences.client.content_types[type];
643             }
644             
645             $.ajatus.layout.body.set_class('create '+content_type.name);
646             
647             var view_hash = '#create.'+content_type.name;
648             
649             $.ajatus.history.update(view_hash);
650             
651             $.ajatus.views.on_change(view_hash);
652             
653             if (   content_type.custom_renderer
654                 && typeof(content_type.custom_renderer['create']) == 'function')
655             {
656                 var renderer = content_type.custom_renderer.create();
657             } else {
658                 var renderer = new $.ajatus.renderer.form(content_type, doc, true);
659                 $.ajatus.application_content_area.html(renderer.form);
660                 $.ajatus.forms.register.normal(renderer.form);
661             }
662         },
663         history_register: function() {
664             $.ajatus.history.add_dynamic_map(['create'], 2, '$.ajatus.views.system.create.history_render');
665         },
666         history_render: function() {            
667             var type = arguments[0];
668                         
669             $.ajatus.views.system.create.render(type);
670         }
671     });
673     $.ajatus.views.system.item = {
674         title: 'View item',
675         options: {
676         },
677         in_tabs: false,
678         dynamic_history: true
679     };
680     $.extend($.ajatus.views.system.item, {
681         render: function(doc)
682         {            
683             if (typeof doc._rev != 'undefined') {
684                 var doc = new $.ajatus.document.loader(doc._id, doc._rev);
685             } else {
686                 var doc = new $.ajatus.document.loader(doc._id);
687             }
688             
689             var content_type = $.ajatus.preferences.client.content_types[doc.value._type];
690             
691             $.ajatus.layout.body.set_class('view '+doc.value._type);
692             
693             var view_hash = '#view.'+doc.value._type+'.'+doc._id+'.'+doc._rev;
694             $.ajatus.history.update(view_hash);
695             $.ajatus.views.on_change(view_hash);
696             
697             $.ajatus.toolbar.add_item($.ajatus.i10n.get('Edit'), {
698                 icon: 'edit.png',
699                 action: function(){
700                     $.ajatus.views.system.edit.render(content_type, doc);
701                 },
702                 access_key: 'Ctrl+e'
703             });
705             if (doc.value.metadata.archived.val) {
706                 var unarch_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Restore'), 'undo.png', function(item, doc){
707                     doc._rev = null;
708                     $.ajatus.document.actions.execute("unarchive", doc);
709                     
710                     $.ajatus.toolbar.hide_item(item.id);
711                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive'), 'archive.png', function(item, doc, unarch_item_id){
712                         doc._rev = null;
713                         $.ajatus.document.actions.execute("archive", doc);
714                         
715                         $.ajatus.toolbar.hide_item(item.id);
716                         $.ajatus.toolbar.show_item(unarch_item_id);
717                     }, [doc, unarch_id], unarch_id);
718                 }, [doc]);
719             } else {
720                 var arch_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive'), 'archive.png', function(item, doc){
721                     doc._rev = null;
722                     $.ajatus.document.actions.execute("archive", doc);
723                     
724                     $.ajatus.toolbar.hide_item(item.id);
725                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Restore'), 'undo.png', function(item, doc, arch_item_id){
726                         doc._rev = null;
727                         $.ajatus.document.actions.execute("unarchive", doc);
728                         
729                         $.ajatus.toolbar.hide_item(item.id);
730                         $.ajatus.toolbar.show_item(arch_item_id);
731                     }, [doc, arch_id], arch_id);
732                 }, [doc]);
733             }
734             
735             if (doc.value.metadata.deleted.val) {
736                 var undel_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Undelete'), 'undo.png', function(item, doc){
737                     doc._rev = null;
738                     $.ajatus.document.actions.execute("undelete", doc);
739                     
740                     var d = doc;
741                     $.ajatus.toolbar.hide_item(item.id);
742                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Delete'), 'trash.png', function(item, doc, undel_item_id){
743                         doc._rev = null;
744                         $.ajatus.document.actions.execute("delete", doc);
745                         
746                         $.ajatus.toolbar.hide_item(item.id);
747                         $.ajatus.toolbar.show_item(undel_item_id);
748                     }, [d, undel_id], undel_id);
749                 }, [doc]);                
750             } else {                
751                 var del_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Delete'), 'trash.png', function(item, doc){
752                     doc._rev = null;
753                     $.ajatus.document.actions.execute("delete", doc);
755                     var d = doc;                    
756                     $.ajatus.toolbar.hide_item(item.id);
757                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Undelete'), 'undo.png', function(item, doc, del_item_id){
758                         doc._rev = null;
759                         $.ajatus.document.actions.execute("undelete", doc);
760                         
761                         $.ajatus.toolbar.hide_item(item.id);
762                         $.ajatus.toolbar.show_item(del_item_id);
763                     }, [d, del_id], del_id);
764                 }, [doc]);
765             }
767             $.ajatus.toolbar.show();
768             
769             var metadata_sb = new $.ajatus.renderer.blocks.sub('metadata_for_'+doc._id);
770             metadata_sb.set_title($.ajatus.i10n.get('Metadata'));
771             $.ajatus.views.on_change_actions.add('$.ajatus.renderer.blocks.clear_sub("'+metadata_sb.id+'");');
772             
773             var md_renderer = new $.ajatus.renderer.metadata(doc, metadata_sb.element);            
775             if (typeof doc.value.tags != 'undefined') {
776                 var rel_docs = $.ajatus.tags.related(doc.value.tags.val, [doc._id]);
778                 var related_sb = new $.ajatus.renderer.blocks.sub('related_objects');
779                 related_sb.set_title($.ajatus.i10n.get('Related Objects'));
780                 $.ajatus.views.on_change_actions.add('$.ajatus.renderer.blocks.clear_sub("'+related_sb.id+'");');
782                 var tinylist = new $.ajatus.renderer.tinylist(related_sb.element);
783                 tinylist.render_items(rel_docs);
784             }
785             
786             if (   content_type.custom_renderer
787                 && typeof(content_type.custom_renderer['item']) == 'function')
788             {
789                 var renderer = content_type.custom_renderer.item();
790             } else {
791                 var renderer = new $.ajatus.renderer.item(doc);
792             }
793         },
794         history_register: function() {
795             $.ajatus.history.add_dynamic_map(['view'], 3, '$.ajatus.views.system.item.history_render');
796         },
797         history_render: function() {
798             var type = arguments[0];
799             var id = arguments[1];
800             var rev = false;
802             if (   typeof arguments[2] != 'undefined'
803                 && arguments[2] != '')
804             {
805                 rev = arguments[2];
806             }
808             if (rev) {
809                 var data = {
810                     _id: id,
811                     _rev: rev,
812                     value: {
813                         _type: type
814                     }
815                 };                
816             } else {
817                 var data = {
818                     _id: id,
819                     value: {
820                         _type: type
821                     }
822                 };
823             }
825             if (typeof data['toSource'] != 'undefined') {
826                 data = data.toSource();
827             }
829             setTimeout("$.ajatus.views.system.item.render("+data+");", 200);
830         }
831     });
833 })(jQuery);