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