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