Changed the accesskey for mass archive
[ajatus.git] / js / ajatus.types.js
blob38805949f11a7e240fc8d63c48261e12cda7da60
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 || {};
18     $.ajatus.types = {};
20     $.ajatus.types.init = function(on_ready)
21     {
22         if (typeof on_ready != 'function') {
23             var on_ready = function(){return;};
24         }
25         
26         $.ajatus.events.named_lock_pool.increase('init_types');
27         $.ajatus.extensions.lock = new $.ajatus.events.lock({
28                 watch: {
29                     validate: function(){return $.ajatus.events.named_lock_pool.count('init_types') == 0;},
30                     interval: 200,
31                 safety_runs: 0
32                 },
33                 on_release: on_ready
34             });
35             
36         $.each($.ajatus.preferences.client.types.system, function(i,type){
37             var type_url = $.ajatus.preferences.client.application_url + 'js/content_types/'+type+'.js';
38             
39             $.ajatus.events.named_lock_pool.increase('init_types');
40             
41             $.ajatus.utils.load_script(type_url, "$.ajatus.types.type_loaded", [type]);
42         });
44         $.each($.ajatus.preferences.client.types.in_use, function(i,type){
45             var type_url = $.ajatus.preferences.client.application_url + 'js/content_types/'+type+'.js';
46             
47             $.ajatus.events.named_lock_pool.increase('init_types');
48             
49             $.ajatus.utils.load_script(type_url, "$.ajatus.types.type_loaded", [type]);
50         });        
52         $.ajatus.events.named_lock_pool.decrease('init_types');
53     }
54     
55     $.ajatus.types.type_loaded = function(type)
56     {
57         // $.ajatus.preferences.client.content_types[type] = new $.ajatus.content_type[type]();
58         
59         $.ajatus.types.prepare_type(type);
60         
61         $.ajatus.events.named_lock_pool.decrease('init_types');
62     }
63     
64     $.ajatus.types.prepare_type = function(type) {
65         
66         //Set some default variables
67         if (typeof $.ajatus.preferences.client.content_types[type]['on_frontpage'] == 'undefined') {
68             $.ajatus.preferences.client.content_types[type].on_frontpage = true;
69         }
70         
71         // Set default pool settings
72         if (typeof $.ajatus.preferences.client.content_types[type]['pool_settings'] == 'undefined') {
73             $.ajatus.preferences.client.content_types[type].pool_settings = {
74                 enabled: true,
75                 settings: {
76                     type: 'scroll'
77                 }
78             };
79         }
80         
81         if (typeof $.ajatus.preferences.client.content_types[type]['view_header'] == 'undefined') {
82             $.ajatus.preferences.client.content_types[type].view_header = 'if (doc.value.metadata.deleted.val == false && doc.value.metadata.archived.val == false && doc.value._type == "'+type+'") {';
83         }
84         if (typeof $.ajatus.preferences.client.content_types[type]['view_footer'] == 'undefined') {
85             $.ajatus.preferences.client.content_types[type].view_footer = '}';
86         }
87         
88         if (typeof $.ajatus.preferences.client.content_types[type]['list_map'] == 'undefined') {
89             var list_map = 'map( doc.value.metadata.created.val, {"_type": doc.value._type,';
90             list_map += '"tags": doc.value.tags,';
91             list_map += '"creator": doc.value.metadata.creator,';
92             list_map += '"created": doc.value.metadata.created';
93             list_map += '});';
94             
95             $.ajatus.preferences.client.content_types[type].list_map = list_map;
96         }
97         
98         if (typeof $.ajatus.preferences.client.content_types[type]['gen_views'] != 'undefined') {
99             $.ajatus.preferences.client.content_types[type]['gen_views']();
100         }
101         
102         if (typeof $.ajatus.preferences.client.content_types[type]['listen_signals'] == 'undefined') {
103             $.ajatus.preferences.client.content_types[type].listen_signals = [
104                 'active_tag_changed'
105             ];
106         }
107         if (typeof $.ajatus.preferences.client.content_types[type]['on_signal'] == 'undefined') {
108             $.ajatus.preferences.client.content_types[type].on_signal = {
109                 active_tag_changed: function() {
110                     var list_at = $.ajatus.preferences.client.content_types[type].view_header;
111                     list_at += 'var hm = false;';
112                     list_at += 'for (var i=0;i<doc.value.tags.val.length;i++) {';
113                     list_at += 'if (doc.value.tags.val[i] == "'+$.ajatus.tags.active.id+'") { hm = true; }';
114                     list_at += '} if (hm == true) {';
115                     list_at += $.ajatus.preferences.client.content_types[type].list_map;
116                     list_at += '}' + $.ajatus.preferences.client.content_types[type].view_footer;
118                     $.ajatus.preferences.client.content_types[type].views['list_at'] = $.ajatus.views.generate(list_at);
119                 }
120             };
121         }
122         
123         if (typeof $.ajatus.preferences.client.content_types[type]['statics'] == 'undefined') {
124             if (   typeof $.ajatus.preferences.client.content_types[type]['views'] != 'undefined'
125                 && typeof $.ajatus.preferences.client.content_types[type]['views']['list'] != 'undefined')
126             {
127                 $.ajatus.preferences.client.content_types[type].statics = {
128                     list: $.ajatus.preferences.client.content_types[type].views.list
129                 };
130             }
131         }
132         
133         if (typeof $.ajatus.preferences.client.content_types[type]['render'] != 'function') {
134             $.ajatus.preferences.client.content_types[type].render = function(view_name) {
135                 var self = $.ajatus.preferences.client.content_types[type];
136                 if (typeof self['render_'+view_name] == 'function') {
137                     self['render_'+view_name]();
138                 } else {
139                     $.ajatus.debug('Undefined view '+view_name, type);
140                 }
141                 
142                 var app_tab_holder = $('#tabs-application ul');
143                 app_tab_holder.html('');
144                 
145                 if (typeof self.additional_views != 'undefined') {                    
146                     $.each(self.additional_views, function(name, data){
147                         var view_hash = '#'+data.hash_key+'.'+type;                    
148                         var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(data.title)+'</span></a></li>');
149                     
150                         tab.appendTo(app_tab_holder);
151                         $.ajatus.tabs.prepare(tab);
152                     });
153                 }
154             };
155         }
156         
157         $.each($.ajatus.preferences.client.content_types[type].listen_signals, function(i,name){
158             if (typeof $.ajatus.preferences.client.content_types[type].on_signal[name] != 'undefined') {
159                 $.ajatus.events.signals.add_listener(name, $.ajatus.preferences.client.content_types[type].on_signal[name]);
160             }
161         });
162         
163         // Set default renderer
164         if (   typeof $.ajatus.preferences.client.content_types[type]['render_list'] != 'function'
165             && $.ajatus.preferences.client.content_types[type]['in_tabs'] == true)
166         {
167             $.ajatus.preferences.client.content_types[type].render_list = function() {
168                 var self = $.ajatus.preferences.client.content_types[type];
169                 $.ajatus.active_type = self;
170                 $.ajatus.layout.body.set_class('list '+self.name);
171                 $.ajatus.application_content_area.html('');
173                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('New %s', [self.name]), {
174                     icon: self.name+'-new.png',
175                     action: function(){
176                         $.ajatus.views.system.create.render(self);
177                     },
178                     access_key: 'Ctrl+n'
179                 });
180                 $.ajatus.toolbar.show();
181                 
182                 var doc_count = 0;
183                 if (   $.ajatus.tags.active != ''
184                     && typeof self.views['list_at'] != 'undefined')
185                 {
186                     doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
187                         count: 0
188                     }).total_rows;
189                 } else {
190                     doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
191                         count: 0
192                     }).total_rows;                    
193                 }
195                 if (doc_count == 0) {
196                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get(self.title));
197                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
198                     return;
199                 }
201                 if (doc_count < $.ajatus.renderer_defaults.use_db_after) {
202                     var on_success = function(data) {
203                         var renderer = new $.ajatus.renderer.list(
204                             $.ajatus.application_content_area,
205                             self,
206                             {
207                                 id: self.name + '_list_holder',
208                                 pool: self.pool_settings
209                             }
210                         );
212                         $.each(data.rows, function(i,doc){
213                             var doc = new $.ajatus.document(doc);
214                             renderer.render_item(doc, i);
215                         });
217                         renderer.items_added();
218                         renderer.enable_sorting();
220                         return data;
221                     };
223                     if (   $.ajatus.tags.active != ''
224                         && typeof self.views['list_at'] != 'undefined')
225                     {
226                         $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
227                             descending: true
228                         });
229                     } else {
230                         $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
231                             descending: true
232                         });                        
233                     }
234                 } else {
235                     this.pool_settings.settings.use_db = true;
236                     if (   $.ajatus.tags.active != ''
237                         && typeof self.views['list_at'] != 'undefined')
238                     {
239                         this.pool_settings.settings.db = {
240                             "temp": self.views['list_at']
241                         };
242                     } else {
243                         this.pool_settings.settings.db = {
244                             "static": self.name+'/list'
245                         };                        
246                     }
248                     var on_success = function(data) {
249                         var renderer = new $.ajatus.renderer.list(
250                             $.ajatus.application_content_area,
251                             self,
252                             {
253                                 id: self.name + '_list_holder',
254                                 pool: self.pool_settings
255                             }
256                         );
257                         
258                         $.each(data.rows, function(i,doc){
259                             var doc = new $.ajatus.document(doc);
260                             renderer.render_item(doc, i);
261                         });
262                         
263                         renderer.items_added(doc_count);
265                         return data;
266                     };
268                     if (   $.ajatus.tags.active != ''
269                         && typeof self.views['list_at'] != 'undefined')
270                     {
271                         var first_doc = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
272                             descending: true,
273                             count: 1
274                         }).rows[0];
276                         $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
277                             descending: true,
278                             startkey: first_doc.key,
279                             startkey_docid: first_doc.id,
280                             count: $.ajatus.renderer_defaults.items_per_page
281                         });
282                     } else {
283                         var first_doc = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
284                             descending: true,
285                             count: 1
286                         }).rows[0];
288                         $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
289                             descending: true,
290                             startkey: first_doc.key,
291                             startkey_docid: first_doc.id,
292                             count: $.ajatus.renderer_defaults.items_per_page
293                         });
294                     }
295                 }
296             };
297         }
298         
299         // Set default export renderer if needed
300         if (   typeof $.ajatus.preferences.client.content_types[type]['additional_views'] != 'undefined'
301             && typeof $.ajatus.preferences.client.content_types[type]['additional_views']['export'] != 'undefined'
302             && typeof $.ajatus.preferences.client.content_types[type]['render_export'] != 'function')
303         {
304             $.ajatus.preferences.client.content_types[type].render_export = function() {
305                 var self = $.ajatus.preferences.client.content_types[type];
306                 $.ajatus.active_type = self;
307                 $.ajatus.layout.body.set_class('export '+self.name);
308                 $.ajatus.application_content_area.html('');
309                                 
310                 var doc_count = 0;
311                 if (   $.ajatus.tags.active != ''
312                     && typeof self.views['list_at'] != 'undefined')
313                 {
314                     doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
315                         count: 0
316                     }).total_rows;
317                 } else {
318                     doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
319                         count: 0
320                     }).total_rows;                    
321                 }
323                 if (doc_count == 0) {
324                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get(self.title));
325                     var msg = $.ajatus.elements.messages.static(
326                         $.ajatus.i10n.get('Empty results'),
327                         $.ajatus.i10n.get('Nothing to export') + '<br />' + $.ajatus.i10n.get('No %s found', [key])
328                     );
329                     return;
330                 }
331                 
332                 var get_items = function(on_success) {
333                     if (   $.ajatus.tags.active != ''
334                         && typeof self.views['list_at'] != 'undefined')
335                     {
336                         $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
337                             descending: true
338                         });
339                     } else {
340                         $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
341                             descending: true
342                         });                        
343                     }
344                 };
345                 
346                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Export %s', [self.name], self.name), {
347                     icon: 'export.png',
348                     action: function(){
349                         $.ajatus.views.export_view();
350                     },
351                     access_key: 'Ctrl+x'
352                 });
353                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive visible'), {
354                     icon: 'archive.png',
355                     action: function(){
356                         var on_success = function(data) {
357                             $.each(data.rows, function(i,doc){
358                                 doc = new $.ajatus.document(doc);
359                                 $.ajatus.document.actions.execute("archive", doc);
360                             });
361                         };
362                         get_items(on_success);
363                     },
364                     access_key: 'Ctrl+a'
365                 });
366                 $.ajatus.toolbar.show();
367                 
368                 var on_success = function(data) {
369                     var list_headers = self.list_headers;
370                     if (typeof self.export_list_headers != 'undefined') {
371                         list_headers = self.export_list_headers;
372                     }
373                     
374                     var renderer = new $.ajatus.renderer.list(
375                         $.ajatus.application_content_area,
376                         self,
377                         {
378                             id: self.name + '_export_list_holder',
379                             show_actions: false,
380                             headers: list_headers,
381                             title: 'Export ' + self.title
382                         }
383                     );
385                     $.each(data.rows, function(i,doc){
386                         var doc = new $.ajatus.document(doc);
387                         renderer.render_item(doc, i);
388                     });
390                     renderer.items_added();
391                     renderer.enable_sorting();
393                     return data;
394                 };
396                 get_items(on_success);
397             }
398         }
399     }
400     
401 })(jQuery);