Moved metadata above related objects
[ajatus.git] / js / views / system.js
blobec1eb228fddef201c1f8817601c674281da7eb33
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', 'trash', 'tags'
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                     return false;
57                 }
58             
59                 var on_success = function(data) {
60                     if (data.total_rows == 0) {
61                         return;
62                     }
63                     
64                     var pool_settings = $.ajatus.views.system.frontpage.pool_settings;
65                     if (data.total_rows >= $.ajatus.views.system.frontpage.options.max_items_before_pool) {
66                         pool_settings.enabled = true;
67                     }                    
68                     
69                     var renderer = new $.ajatus.renderer.list(
70                         $.ajatus.application_content_area,
71                         type,
72                         {
73                             id: type.name + '_list_holder',
74                             append: true,
75                             pool: pool_settings
76                         }
77                     );
79                     $.each(data.rows, function(i,doc){
80                         var doc = new $.ajatus.document(doc);
81                         if ($.ajatus.tags.active != '') {
82                             if ($.ajatus.utils.array.has_match($.ajatus.tags.active, doc.value.tags.val)) {
83                                 renderer.render_item(doc, i);                            
84                             }
85                         } else {
86                             renderer.render_item(doc, i);
87                         }
88                     });
89                     
90                     return data;
91                 };
93                 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, type.views.list, {
94                     count: $.ajatus.views.system.frontpage.options.group_item_count,
95                     descending: true
96                 });
97             });
98         }
99     };
100     $.ajatus.views.system.tags = {
101         title: 'Tags',
102         options: {            
103         },
104         in_tabs: true,
105         application_tab: true,
106         history_support: true,
107                 
108         tab: {
109             on_click: function(e) {
110                 $.ajatus.tabs.on_click(e);
111                 $.ajatus.views.system.tags.render();
112             }
113         },
114         
115         render: function() {
116             $.ajatus.application_content_area.html('');
117             $.ajatus.layout.body.set_class('tags');
118             $.ajatus.views.on_change("#view.tags");
120             $.ajatus.toolbar.init();
121             $.ajatus.toolbar.show();
122             
123             $.ajatus.views.system.tags.get_items();
124         },
126         render_results: function(tags, items) {
127             var type = $.ajatus.preferences.client.content_types['tag'];
128             
129             var renderer = new $.ajatus.renderer.list(
130                 $.ajatus.application_content_area,
131                 type,
132                 {
133                     id: 'tags_list_holder',
134                     headers: [
135                         'title', 'created', 'creator', 'used'
136                     ],
137                     schema: $.extend({
138                         used: {
139                             label: 'Used'
140                         }
141                     }, type.original_schema)
142                 }
143             );
144             
145             $.each(tags, function(i,doc){
146                 var doc = new $.ajatus.document(doc);
147                 doc.value['used'] = {
148                     val: (typeof items[doc._id] != 'undefined' ? items[doc._id].length : 0)
149                 };
150                 renderer.render_item(doc, i);
151             });
152             
153             renderer.enable_sorting();
154         },
155         
156         get_items: function(on_success) {
157             var conn_on_success = function(data) {                
158                 if (data.total_rows == 0) {
159                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get('tag'));
160                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
161                     return;
162                 }
163                 
164                 var tags = {};
165                 var items = {};
166                 
167                 $.each(data.rows, function(i,n){
168                     if (n.key[1] == 0) {
169                         tags[n.key[0]] = n;
170                     } else {
171                         if (typeof items[n.key[0]] == 'undefined') {
172                             items[n.key[0]] = [];
173                         }
174                         items[n.key[0]].push(n);
175                     }
176                 });
178                 if (typeof on_success == 'undefined') {
179                     on_success = $.ajatus.views.system.tags.render_results;
180                 }
181                 
182                 on_success(tags, items);
183                 
184                 return data;
185             };
187             $.jqCouch.connection('view', conn_on_success).get($.ajatus.preferences.client.content_database, 'tag/list_with_docs', {
188                 // startkey: ["ED862081B2FBF46FB0CDBBE848ED6856", 0],
189                 // endkey: ["ED862081B2FBF46FB0CDBBE848ED6856", 1]
190             });
191         }
193     };
194     $.ajatus.views.system.trash = {
195         title: 'Trash',
196         options: {
197             group_item_count: 10
198         },
199         in_tabs: true,
200         application_tab: true,
201         history_support: true,
202                 
203         tab: {
204             on_click: function(e)
205             {
206                 $.ajatus.tabs.on_click(e);
207                 $.ajatus.views.system.trash.render();
208             }
209         },
210         
211         get_items: function(on_success) {            
212             var trash_view = 'if (doc.value.metadata.deleted.val == true) {';
213             trash_view += 'map( doc.value.metadata.revised, {"_type": doc.value._type,';
214             trash_view += '"title": doc.value.title,';
215             
216             $.each($.ajatus.preferences.client.content_types, function(key,type){
217                 if (type['title_item']) {
218                     $.each(type['title_item'], function(i,part){
219                         if (type.schema[part]) {
220                             trash_view += '"'+part+'": doc.value.'+part+',';
221                         }
222                     });
223                 }
224             });
226             trash_view += '"revised": doc.value.metadata.revised,"by": doc.value.metadata.revisor,"tags": doc.value.tags})}';
227             
228             var conn_on_success = function(data) {
229                 if (data.total_rows == 0) {
230                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get('deleted item'));
231                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
232                     return;
233                 }
234                 
235                 if (typeof on_success == 'undefined') {
236                     on_success = $.ajatus.views.system.trash.render_results;
237                 }
238                 
239                 on_success(data.rows);
240                 return data;
241             };
242             
243             $.jqCouch.connection('view', conn_on_success).temp($.ajatus.preferences.client.content_database, "$.ajatus.views.generate('"+trash_view+"')", {
244                 count: $.ajatus.views.system.trash.options.group_item_count,
245                 descending: true
246             });
247         },
248         
249         render: function()
250         {
251             $.ajatus.application_content_area.html('');
252             $.ajatus.layout.body.set_class('trash');
253             $.ajatus.views.on_change("#view.trash");
255             $.ajatus.toolbar.init();
256             $.ajatus.toolbar.show();
257             
258             $.ajatus.views.system.trash.get_items();
259         },
260         
261         render_results: function(rows)
262         {
263             var result_count = {};
264             var show_empty_trash = false;
265             
266             $.each($.ajatus.preferences.client.content_types, function(key,type){
267                 if (!type.views['list']) {
268                     return false;
269                 }
270                 
271                 result_count[type.name] = 0;
272                 
273                 $.each(rows, function(i,doc){
274                     if (doc.value._type == type.name) {
275                         result_count[type.name] += 1;
276                     }
277                 });
278                 
279                 if (result_count[type.name] == 0) {
280                     return;
281                 }
282                 
283                 show_empty_trash = true;
284                 
285                 var renderer = new $.ajatus.renderer.list(
286                     $.ajatus.application_content_area,
287                     false,
288                     {
289                         id: type.name + '_list_holder',
290                         append: true,
291                         title: type.title,
292                         headers: [
293                             'title', 'revised', 'by'
294                         ],
295                         schema: $.extend({
296                             by: {
297                                 label: 'By'
298                             }
299                         }, type.schema)
300                     }
301                 );
303                 $.each(rows, function(i,doc){
304                     if (doc.value._type == type.name) {
305                         var doc = new $.ajatus.document(doc);
306                         
307                         renderer.set_actions(
308                             [
309                                 {
310                                     name: 'view',
311                                     title: $.ajatus.i10n.get('View'),
312                                     icon: 'view.png',
313                                     click_action: '$.ajatus.views.system.item.render(doc);'
314                                 },
315                                 {
316                                     name: 'undelete',
317                                     title: $.ajatus.i10n.get('Undelete'),
318                                     icon: 'undo.png',
319                                     click_action: '$.ajatus.document.actions.execute("undelete", doc);'
320                                 },
321                                 {
322                                     name: 'delete_final',
323                                     title: $.ajatus.i10n.get('Delete'),
324                                     icon: 'trash.png',
325                                     click_action: '$.ajatus.document.actions.execute("delete_final", doc);'
326                                 }
327                             ]
328                         );
329                         if ($.ajatus.tags.active != '') {
330                             if ($.ajatus.utils.array.has_match($.ajatus.tags.active, doc.value.tags.val)) {
331                                 renderer.render_item(doc, i);                            
332                             }
333                         } else {
334                             renderer.render_item(doc, i);
335                         }
336                     }
337                 });
338             
339                 renderer.enable_sorting();
340             });
341             
342             if (show_empty_trash) {
343                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Empty trash'), 'trash.png', function(){
344                     $.ajatus.views.system.trash.empty();
345                 });
346             }
347         },
348         empty: function() {
349             $.ajatus.views.system.trash.get_items($.ajatus.views.system.trash.execute_empty);
350         },
351         execute_empty: function(rows) {            
352             $.each(rows, function(i,doc){
353                 doc = new $.ajatus.document(doc);
354                 $.ajatus.document.actions.execute("delete_final", doc);
355             });
356         }
357     };
358     $.ajatus.views.system.edit = {
359         title: 'Edit',
360         options: {
361         },
362         in_tabs: false,
363         dynamic_history: true
364     };
365     $.extend($.ajatus.views.system.edit, {
366         render: function(type, doc, use_given_doc)
367         {
368             if (typeof use_given_doc == 'undefined') {
369                 use_given_doc = false;
370             }
371             
372             var content_type = type; 
373             
374             if (! use_given_doc) {
375                 var doc = new $.ajatus.document.loader(doc._id, doc._rev);                
376             }
377             
378             if (! type) {
379                 content_type = $.ajatus.preferences.client.content_types[doc.value._type];
380             }
381                         
382             if (typeof(type) == 'string') {
383                 content_type = $.ajatus.preferences.client.content_types[type];
384             }
385             
386             $.ajatus.layout.body.set_class('edit '+content_type.name);
387             
388             var view_hash = '#edit.'+doc.value._type+'.'+doc._id+'.'+doc._rev;
389             $.ajatus.history.update(view_hash);
390             $.ajatus.views.on_change(view_hash);
391             
392             var latest_rev = $.ajatus.document.revisions.get(doc).active.rev;
393             
394             var renderer = new $.ajatus.renderer.form(content_type, doc, false, latest_rev);
395             $.ajatus.application_content_area.html(renderer.form);
396             $.ajatus.forms.register.normal(renderer.form);
397         },
398         history_register: function() {
399             $.ajatus.history.add_dynamic_map(['edit'], 3, '$.ajatus.views.system.edit.history_render');
400         },
401         history_render: function() {
402             var type = arguments[0];
403             var id = arguments[1];
404             var rev = false;
406             if (   typeof arguments[2] != 'undefined'
407                 && arguments[2] != '')
408             {
409                 rev = arguments[2];
410             }
411             
412             var doc = new $.ajatus.document.loader(id, rev);
413             $.ajatus.views.system.edit.render(type, doc);
414         }
415     });
416     
417     $.ajatus.views.system.create = {
418         title: 'Create',
419         options: {
420         },
421         in_tabs: false,
422         dynamic_history: true
423     };
424     $.extend($.ajatus.views.system.create, {
425         render: function(type, doc)
426         {
427             var content_type = type; 
428             
429             if (typeof(type) == 'string') {
430                 content_type = $.ajatus.preferences.client.content_types[type];
431             }
432             
433             $.ajatus.layout.body.set_class('create '+content_type.name);
434             
435             var view_hash = '#create.'+content_type.name;
436             
437             $.ajatus.history.update(view_hash);
438             
439             $.ajatus.views.on_change(view_hash);
440             
441             if (   content_type.custom_renderer
442                 && typeof(content_type.custom_renderer['create']) == 'function')
443             {
444                 var renderer = content_type.custom_renderer.create();
445             } else {
446                 var renderer = new $.ajatus.renderer.form(content_type, doc, true);
447                 $.ajatus.application_content_area.html(renderer.form);
448                 $.ajatus.forms.register.normal(renderer.form);
449             }
450         },
451         history_register: function() {
452             $.ajatus.history.add_dynamic_map(['create'], 2, '$.ajatus.views.system.create.history_render');
453         },
454         history_render: function() {            
455             var type = arguments[0];
456                         
457             $.ajatus.views.system.create.render(type);
458         }
459     });
461     $.ajatus.views.system.item = {
462         title: 'View item',
463         options: {
464         },
465         in_tabs: false,
466         dynamic_history: true
467     };
468     $.extend($.ajatus.views.system.item, {
469         render: function(doc)
470         {            
471             if (typeof doc._rev != 'undefined') {
472                 var doc = new $.ajatus.document.loader(doc._id, doc._rev);
473             } else {
474                 var doc = new $.ajatus.document.loader(doc._id);
475             }
476             
477             var content_type = $.ajatus.preferences.client.content_types[doc.value._type];
478             
479             $.ajatus.layout.body.set_class('view '+doc.value._type);
480             
481             var view_hash = '#view.'+doc.value._type+'.'+doc._id+'.'+doc._rev;
482             $.ajatus.history.update(view_hash);
483             $.ajatus.views.on_change(view_hash);
484             
485             $.ajatus.toolbar.add_item($.ajatus.i10n.get('Edit'), {
486                 icon: 'edit.png',
487                 action: function(){
488                     $.ajatus.views.system.edit.render(content_type, doc);
489                 },
490                 access_key: 'Ctrl+e'
491             });
493             if (doc.value.metadata.archived.val) {
494                 var unarch_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Restore'), 'undo.png', function(item, doc){
495                     doc._rev = null;
496                     $.ajatus.document.actions.execute("unarchive", doc);
497                     
498                     $.ajatus.toolbar.hide_item(item.id);
499                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive'), 'archive.png', function(item, doc, unarch_item_id){
500                         doc._rev = null;
501                         $.ajatus.document.actions.execute("archive", doc);
502                         
503                         $.ajatus.toolbar.hide_item(item.id);
504                         $.ajatus.toolbar.show_item(unarch_item_id);
505                     }, [doc, unarch_id], unarch_id);
506                 }, [doc]);
507             } else {
508                 var arch_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive'), 'archive.png', function(item, doc){
509                     doc._rev = null;
510                     $.ajatus.document.actions.execute("archive", doc);
511                     
512                     $.ajatus.toolbar.hide_item(item.id);
513                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Restore'), 'undo.png', function(item, doc, arch_item_id){
514                         doc._rev = null;
515                         $.ajatus.document.actions.execute("unarchive", doc);
516                         
517                         $.ajatus.toolbar.hide_item(item.id);
518                         $.ajatus.toolbar.show_item(arch_item_id);
519                     }, [doc, arch_id], arch_id);
520                 }, [doc]);
521             }
522             
523             if (doc.value.metadata.deleted.val) {
524                 var undel_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Undelete'), 'undo.png', function(item, doc){
525                     doc._rev = null;
526                     $.ajatus.document.actions.execute("undelete", doc);
527                     
528                     var d = doc;
529                     $.ajatus.toolbar.hide_item(item.id);
530                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Delete'), 'trash.png', function(item, doc, undel_item_id){
531                         doc._rev = null;
532                         $.ajatus.document.actions.execute("delete", doc);
533                         
534                         $.ajatus.toolbar.hide_item(item.id);
535                         $.ajatus.toolbar.show_item(undel_item_id);
536                     }, [d, undel_id], undel_id);
537                 }, [doc]);                
538             } else {                
539                 var del_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Delete'), 'trash.png', function(item, doc){
540                     doc._rev = null;
541                     $.ajatus.document.actions.execute("delete", doc);
543                     var d = doc;                    
544                     $.ajatus.toolbar.hide_item(item.id);
545                     $.ajatus.toolbar.add_item($.ajatus.i10n.get('Undelete'), 'undo.png', function(item, doc, del_item_id){
546                         doc._rev = null;
547                         $.ajatus.document.actions.execute("undelete", doc);
548                         
549                         $.ajatus.toolbar.hide_item(item.id);
550                         $.ajatus.toolbar.show_item(del_item_id);
551                     }, [d, del_id], del_id);
552                 }, [doc]);
553             }
555             $.ajatus.toolbar.show();
556             
557             var metadata_sb = new $.ajatus.renderer.blocks.sub('metadata_for_'+doc._id);
558             metadata_sb.set_title($.ajatus.i10n.get('Metadata'));
559             $.ajatus.views.on_change_actions.add('$.ajatus.renderer.blocks.clear_sub("'+metadata_sb.id+'");');
560             
561             var md_renderer = new $.ajatus.renderer.metadata(doc, metadata_sb.element);            
563             if (typeof doc.value.tags != 'undefined') {
564                 var rel_docs = $.ajatus.tags.related(doc.value.tags.val, [doc._id]);
566                 var related_sb = new $.ajatus.renderer.blocks.sub('related_objects');
567                 related_sb.set_title($.ajatus.i10n.get('Related Objects'));
568                 $.ajatus.views.on_change_actions.add('$.ajatus.renderer.blocks.clear_sub("'+related_sb.id+'");');
570                 var tinylist = new $.ajatus.renderer.tinylist(related_sb.element);
571                 tinylist.render_items(rel_docs);
572             }
573             
574             if (   content_type.custom_renderer
575                 && typeof(content_type.custom_renderer['item']) == 'function')
576             {
577                 var renderer = content_type.custom_renderer.item();
578             } else {
579                 var renderer = new $.ajatus.renderer.item(doc);
580             }
581         },
582         history_register: function() {
583             $.ajatus.history.add_dynamic_map(['view'], 3, '$.ajatus.views.system.item.history_render');
584         },
585         history_render: function() {
586             var type = arguments[0];
587             var id = arguments[1];
588             var rev = false;
590             if (   typeof arguments[2] != 'undefined'
591                 && arguments[2] != '')
592             {
593                 rev = arguments[2];
594             }
596             if (rev) {
597                 var data = {
598                     _id: id,
599                     _rev: rev,
600                     value: {
601                         _type: type
602                     }
603                 };                
604             } else {
605                 var data = {
606                     _id: id,
607                     value: {
608                         _type: type
609                     }
610                 };
611             }
613             if (typeof data['toSource'] != 'undefined') {
614                 data = data.toSource();
615             }
617             setTimeout("$.ajatus.views.system.item.render("+data+");", 200);
618         }
619     });
621 })(jQuery);