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