Safety check to see if the type exists before editing
[ajatus.git] / js / ajatus.types.js
blobc32d664a2fb52305b60d1f20dfe325ffc2a74705
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         if (typeof $.ajatus.preferences.client.content_types[type] == 'undefined') {
66             return;
67         }
68         //Set some default variables
69         if (typeof $.ajatus.preferences.client.content_types[type]['on_frontpage'] == 'undefined') {
70             $.ajatus.preferences.client.content_types[type].on_frontpage = true;
71         }
72         
73         // Set default pool settings
74         if (typeof $.ajatus.preferences.client.content_types[type]['pool_settings'] == 'undefined') {
75             $.ajatus.preferences.client.content_types[type].pool_settings = {
76                 enabled: true,
77                 settings: {
78                     type: 'scroll'
79                 }
80             };
81         }
82         
83         if (typeof $.ajatus.preferences.client.content_types[type]['view_header'] == 'undefined') {
84             $.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+'") {';
85         }
86         if (typeof $.ajatus.preferences.client.content_types[type]['view_footer'] == 'undefined') {
87             $.ajatus.preferences.client.content_types[type].view_footer = '}';
88         }
89         
90         if (typeof $.ajatus.preferences.client.content_types[type]['list_map'] == 'undefined') {
91             var list_map = 'map( doc.value.metadata.created.val, {"_type": doc.value._type,';
92             list_map += '"tags": doc.value.tags,';
93             list_map += '"creator": doc.value.metadata.creator,';
94             list_map += '"created": doc.value.metadata.created';
95             list_map += '});';
96             
97             $.ajatus.preferences.client.content_types[type].list_map = list_map;
98         }
99         
100         if (typeof $.ajatus.preferences.client.content_types[type]['gen_views'] != 'undefined') {
101             $.ajatus.preferences.client.content_types[type]['gen_views']();
102         }
103         
104         if (typeof $.ajatus.preferences.client.content_types[type]['listen_signals'] == 'undefined') {
105             $.ajatus.preferences.client.content_types[type].listen_signals = [];
106         }
107         if ($.inArray('active_tag_changed', $.ajatus.preferences.client.content_types[type].listen_signals) == -1) {
108             $.ajatus.preferences.client.content_types[type].listen_signals.push('active_tag_changed');            
109         }
110         
111         if (typeof $.ajatus.preferences.client.content_types[type]['on_signal'] == 'undefined') {
112             $.ajatus.preferences.client.content_types[type].on_signal = {};
113         }
114         if (typeof $.ajatus.preferences.client.content_types[type].on_signal['active_tag_changed'] == 'undefined') {
115             $.ajatus.preferences.client.content_types[type].on_signal['active_tag_changed'] = function() {
116                 var list_at = $.ajatus.preferences.client.content_types[type].view_header;
117                 list_at += 'var hm = false;';
118                 list_at += 'for (var i=0;i<doc.value.tags.val.length;i++) {';
119                 list_at += 'if (doc.value.tags.val[i] == "'+$.ajatus.tags.active.id+'") { hm = true; }';
120                 list_at += '} if (hm == true) {';
121                 list_at += $.ajatus.preferences.client.content_types[type].list_map;
122                 list_at += '}' + $.ajatus.preferences.client.content_types[type].view_footer;
124                 $.ajatus.preferences.client.content_types[type].views['list_at'] = $.ajatus.views.generate(list_at);
125             }
126         }
127         
128         if (typeof $.ajatus.preferences.client.content_types[type]['statics'] == 'undefined') {
129             if (   typeof $.ajatus.preferences.client.content_types[type]['views'] != 'undefined'
130                 && typeof $.ajatus.preferences.client.content_types[type]['views']['list'] != 'undefined')
131             {
132                 $.ajatus.preferences.client.content_types[type].statics = {
133                     list: $.ajatus.preferences.client.content_types[type].views.list
134                 };
135             }
136         }
137         
138         if (typeof $.ajatus.preferences.client.content_types[type]['render'] != 'function') {
139             $.ajatus.preferences.client.content_types[type].render = function(view_name) {
140                 var self = $.ajatus.preferences.client.content_types[type];
141                 if (typeof self['render_'+view_name] == 'function') {
142                     self['render_'+view_name]();
143                 } else {
144                     $.ajatus.debug('Undefined view '+view_name, type);
145                 }
146                 
147                 var app_tab_holder = $('#tabs-application ul');
148                 app_tab_holder.html('');
149                 
150                 if (typeof self.additional_views != 'undefined') {                    
151                     $.each(self.additional_views, function(name, data){
152                         var view_hash = '#'+data.hash_key+'.'+type;                    
153                         var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(data.title)+'</span></a></li>');
154                     
155                         tab.appendTo(app_tab_holder);
156                         $.ajatus.tabs.prepare(tab);
157                     });
158                 }
159             };
160         }
161         
162         $.each($.ajatus.preferences.client.content_types[type].listen_signals, function(i,name){
163             if (typeof $.ajatus.preferences.client.content_types[type].on_signal[name] != 'undefined') {
164                 $.ajatus.events.signals.add_listener(name, $.ajatus.preferences.client.content_types[type].on_signal[name]);
165             }
166         });
167         
168         // Set default renderer
169         if (   typeof $.ajatus.preferences.client.content_types[type]['render_list'] != 'function'
170             && $.ajatus.preferences.client.content_types[type]['in_tabs'] == true)
171         {
172             $.ajatus.preferences.client.content_types[type].render_list = function() {
173                 var self = $.ajatus.preferences.client.content_types[type];
174                 $.ajatus.active_type = self;
175                 $.ajatus.layout.body.set_class('list '+self.name);
176                 $.ajatus.application_content_area.html('');
178                 $.ajatus.layout.title.update({
179                     view: $.ajatus.i10n.plural($.ajatus.i10n.get(self.title))
180                 });
182                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('New %s', [self.name]), {
183                     icon: self.name+'-new.png',
184                     action: function(){
185                         $.ajatus.views.system.create.render(self);
186                     },
187                     access_key: 'Ctrl+n'
188                 });
189                 $.ajatus.toolbar.show();
190                 
191                 var doc_count = 0;
192                 if (   $.ajatus.tags.active != ''
193                     && typeof self.views['list_at'] != 'undefined')
194                 {
195                     doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
196                         count: 0
197                     }).total_rows;
198                 } else {
199                     doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
200                         count: 0
201                     }).total_rows;                    
202                 }
204                 if (doc_count == 0) {
205                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get(self.title));
206                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
207                     return;
208                 }
210                 if (doc_count < $.ajatus.renderer_defaults.use_db_after) {
211                     var on_success = function(data) {
212                         var renderer = new $.ajatus.renderer.list(
213                             $.ajatus.application_content_area,
214                             self,
215                             {
216                                 id: self.name + '_list_holder',
217                                 pool: self.pool_settings
218                             }
219                         );
221                         $.each(data.rows, function(i,doc){
222                             var doc = new $.ajatus.document(doc);
223                             renderer.render_item(doc, i);
224                         });
226                         renderer.items_added();
227                         renderer.enable_sorting();
229                         return data;
230                     };
232                     if (   $.ajatus.tags.active != ''
233                         && typeof self.views['list_at'] != 'undefined')
234                     {
235                         $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
236                             descending: true
237                         });
238                     } else {
239                         $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
240                             descending: true
241                         });                        
242                     }
243                 } else {
244                     this.pool_settings.settings.use_db = true;
245                     if (   $.ajatus.tags.active != ''
246                         && typeof self.views['list_at'] != 'undefined')
247                     {
248                         this.pool_settings.settings.db = {
249                             "temp": self.views['list_at']
250                         };
251                     } else {
252                         this.pool_settings.settings.db = {
253                             "static": self.name+'/list'
254                         };                        
255                     }
257                     var on_success = function(data) {
258                         var renderer = new $.ajatus.renderer.list(
259                             $.ajatus.application_content_area,
260                             self,
261                             {
262                                 id: self.name + '_list_holder',
263                                 pool: self.pool_settings
264                             }
265                         );
266                         
267                         $.each(data.rows, function(i,doc){
268                             var doc = new $.ajatus.document(doc);
269                             renderer.render_item(doc, i);
270                         });
271                         
272                         renderer.items_added(doc_count);
274                         return data;
275                     };
277                     if (   $.ajatus.tags.active != ''
278                         && typeof self.views['list_at'] != 'undefined')
279                     {
280                         var first_doc = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
281                             descending: true,
282                             count: 1
283                         }).rows[0];
285                         $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
286                             descending: true,
287                             startkey: first_doc.key,
288                             startkey_docid: first_doc.id,
289                             count: $.ajatus.renderer_defaults.items_per_page
290                         });
291                     } else {
292                         var first_doc = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
293                             descending: true,
294                             count: 1
295                         }).rows[0];
297                         $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
298                             descending: true,
299                             startkey: first_doc.key,
300                             startkey_docid: first_doc.id,
301                             count: $.ajatus.renderer_defaults.items_per_page
302                         });
303                     }
304                 }
305             };
306         }
307         
308         // Set default export renderer if needed
309         if (   typeof $.ajatus.preferences.client.content_types[type]['additional_views'] != 'undefined'
310             && typeof $.ajatus.preferences.client.content_types[type]['additional_views']['export'] != 'undefined'
311             && typeof $.ajatus.preferences.client.content_types[type]['render_export'] != 'function')
312         {
313             $.ajatus.preferences.client.content_types[type].render_export = function() {
314                 var self = $.ajatus.preferences.client.content_types[type];
315                 $.ajatus.active_type = self;
316                 $.ajatus.layout.body.set_class('export '+self.name);
317                 $.ajatus.application_content_area.html('');
318                                 
319                 var doc_count = 0;
320                 if (   $.ajatus.tags.active != ''
321                     && typeof self.views['list_at'] != 'undefined')
322                 {
323                     doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
324                         count: 0
325                     }).total_rows;
326                 } else {
327                     doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
328                         count: 0
329                     }).total_rows;                    
330                 }
332                 if (doc_count == 0) {
333                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get(self.title));
334                     var msg = $.ajatus.elements.messages.static(
335                         $.ajatus.i10n.get('Empty results'),
336                         $.ajatus.i10n.get('Nothing to export') + '<br />' + $.ajatus.i10n.get('No %s found', [key])
337                     );
338                     return;
339                 }
340                 
341                 var get_items = function(on_success) {
342                     if (   $.ajatus.tags.active != ''
343                         && typeof self.views['list_at'] != 'undefined')
344                     {
345                         $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
346                             descending: true
347                         });
348                     } else {
349                         $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
350                             descending: true
351                         });                        
352                     }
353                 };
354                 
355                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Export %s', [self.name], self.name), {
356                     icon: 'export.png',
357                     action: function(){
358                         $.ajatus.views.export_view();
359                     },
360                     access_key: 'Ctrl+x'
361                 });
362                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive visible'), {
363                     icon: 'archive.png',
364                     action: function(){
365                         var answer = confirm($.ajatus.i10n.get("Archiving all items in view. Are you sure?"));
366                         if (answer == true) {
367                             var on_success = function(data) {
368                                 $.each(data.rows, function(i,doc){
369                                     doc = new $.ajatus.document(doc);
370                                     $.ajatus.document.actions.execute("archive", doc);
371                                 });
372                             };
373                             
374                             get_items(on_success);
375                         }
376                     },
377                     access_key: 'Ctrl+a'
378                 });
379                 $.ajatus.toolbar.show();
380                 
381                 var on_success = function(data) {
382                     var list_headers = self.list_headers;
383                     if (typeof self.export_list_headers != 'undefined') {
384                         list_headers = self.export_list_headers;
385                     }
386                     
387                     var renderer = new $.ajatus.renderer.list(
388                         $.ajatus.application_content_area,
389                         self,
390                         {
391                             id: self.name + '_export_list_holder',
392                             show_actions: false,
393                             headers: list_headers,
394                             title: 'Export ' + self.title
395                         }
396                     );
398                     $.each(data.rows, function(i,doc){
399                         var doc = new $.ajatus.document(doc);
400                         renderer.render_item(doc, i);
401                     });
403                     renderer.items_added();
404                     renderer.enable_sorting();
406                     return data;
407                 };
409                 get_items(on_success);
410             }
411         }
412     }
413     
414 })(jQuery);