Added icon value
[ajatus.git] / js / ajatus.types.js
blob7a6988bb4765181a5526596a05ae40c2a8110737
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]['statics'] == 'undefined') {
82             if (   typeof $.ajatus.preferences.client.content_types[type]['views'] != 'undefined'
83                 && typeof $.ajatus.preferences.client.content_types[type]['views']['list'] != 'undefined')
84             {
85                 $.ajatus.preferences.client.content_types[type].statics = {
86                     list: $.ajatus.preferences.client.content_types[type].views.list
87                 };
88             }
89         }
90         
91         // Set default renderer
92         if (   typeof $.ajatus.preferences.client.content_types[type]['render'] != 'function'
93             && $.ajatus.preferences.client.content_types[type]['in_tabs'] == true)
94         {
95             $.ajatus.preferences.client.content_types[type].render = function() {
96                 var self = $.ajatus.preferences.client.content_types[type];
97                 $.ajatus.active_type = self;
98                 $.ajatus.layout.body.set_class('list '+self.name);
99                 $.ajatus.application_content_area.html('');
101                 $.ajatus.toolbar.add_item($.ajatus.i10n.get('New %s', [self.name]), {
102                     icon: self.name+'-new.png',
103                     action: function(){
104                         $.ajatus.views.system.create.render(self);
105                     },
106                     access_key: 'Ctrl+n'
107                 });
108                 $.ajatus.toolbar.show();
110                 var doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
111                     count: 0
112                 }).total_rows;
114                 if (doc_count == 0) {
115                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get(self.title));
116                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
117                     return;
118                 }
120                 if (doc_count < $.ajatus.renderer_defaults.use_db_after) {
121                     var on_success = function(data) {
122                         var renderer = new $.ajatus.renderer.list(
123                             $.ajatus.application_content_area,
124                             self,
125                             {
126                                 id: self.name + '_list_holder',
127                                 pool: self.pool_settings
128                             }
129                         );
131                         $.each(data.rows, function(i,doc){
132                             var doc = new $.ajatus.document(doc);
133                             if ($.ajatus.tags.active != '') {
134                                 if ($.ajatus.utils.array.has_match($.ajatus.tags.active._id, doc.value.tags.val)) {
135                                     renderer.render_item(doc, i);  
136                                 }
137                             } else {
138                                 renderer.render_item(doc, i);
139                             }
140                         });
141                         renderer.items_added();
142                         renderer.enable_sorting();
144                         return data;
145                     };
147                     $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
148                         descending: true
149                     });
150                 } else {
151                     this.pool_settings.settings.use_db = true;
152                     this.pool_settings.settings.db = {
153                         "static": self.name+'/list'
154                     };
156                     var on_success = function(data) {
157                         var renderer = new $.ajatus.renderer.list(
158                             $.ajatus.application_content_area,
159                             self,
160                             {
161                                 id: self.name + '_list_holder',
162                                 pool: self.pool_settings
163                             }
164                         );
166                         $.each(data.rows, function(i,doc){
167                             var doc = new $.ajatus.document(doc);
168                             if ($.ajatus.tags.active != '') {
169                                 if ($.ajatus.utils.array.has_match($.ajatus.tags.active._id, doc.value.tags.val)) {
170                                     renderer.render_item(doc, i);                            
171                                 }
172                             } else {
173                                 renderer.render_item(doc, i);
174                             }
175                         });
176                         renderer.items_added(doc_count);
178                         return data;
179                     };
181                     var first_doc = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
182                         descending: true,
183                         count: 1
184                     }).rows[0];
186                     $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
187                         descending: true,
188                         startkey: first_doc.key,
189                         startkey_docid: first_doc.id,
190                         count: $.ajatus.renderer_defaults.items_per_page
191                     });
192                 }
193             };
194         }
195     }
196     
197 })(jQuery);