4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
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
16 $.ajatus = $.ajatus || {};
20 $.ajatus.types.init = function(on_ready)
22 if (typeof on_ready != 'function') {
23 var on_ready = function(){return;};
26 $.ajatus.events.named_lock_pool.increase('init_types');
27 $.ajatus.extensions.lock = new $.ajatus.events.lock({
29 validate: function(){return $.ajatus.events.named_lock_pool.count('init_types') == 0;},
36 $.each($.ajatus.preferences.client.types.system, function(i,type){
37 var type_url = $.ajatus.preferences.client.application_url + 'js/content_types/'+type+'.js';
39 $.ajatus.events.named_lock_pool.increase('init_types');
41 $.ajatus.utils.load_script(type_url, "$.ajatus.types.type_loaded", [type]);
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';
47 $.ajatus.events.named_lock_pool.increase('init_types');
49 $.ajatus.utils.load_script(type_url, "$.ajatus.types.type_loaded", [type]);
52 $.ajatus.events.named_lock_pool.decrease('init_types');
55 $.ajatus.types.type_loaded = function(type)
57 // $.ajatus.preferences.client.content_types[type] = new $.ajatus.content_type[type]();
59 $.ajatus.types.prepare_type(type);
61 $.ajatus.events.named_lock_pool.decrease('init_types');
64 $.ajatus.types.prepare_type = function(type) {
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;
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 = {
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+'") {';
84 if (typeof $.ajatus.preferences.client.content_types[type]['view_footer'] == 'undefined') {
85 $.ajatus.preferences.client.content_types[type].view_footer = '}';
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';
95 $.ajatus.preferences.client.content_types[type].list_map = list_map;
98 if (typeof $.ajatus.preferences.client.content_types[type]['gen_views'] != 'undefined') {
99 $.ajatus.preferences.client.content_types[type]['gen_views']();
102 if (typeof $.ajatus.preferences.client.content_types[type]['listen_signals'] == 'undefined') {
103 $.ajatus.preferences.client.content_types[type].listen_signals = [
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);
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')
127 $.ajatus.preferences.client.content_types[type].statics = {
128 list: $.ajatus.preferences.client.content_types[type].views.list
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]();
139 $.ajatus.debug('Undefined view '+view_name, type);
142 var app_tab_holder = $('#tabs-application ul');
143 app_tab_holder.html('');
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>');
150 tab.appendTo(app_tab_holder);
151 $.ajatus.tabs.prepare(tab);
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]);
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)
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',
176 $.ajatus.views.system.create.render(self);
180 $.ajatus.toolbar.show();
183 if ( $.ajatus.tags.active != ''
184 && typeof self.views['list_at'] != 'undefined')
186 doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
190 doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
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]));
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,
207 id: self.name + '_list_holder',
208 pool: self.pool_settings
212 $.each(data.rows, function(i,doc){
213 var doc = new $.ajatus.document(doc);
214 renderer.render_item(doc, i);
217 renderer.items_added();
218 renderer.enable_sorting();
223 if ( $.ajatus.tags.active != ''
224 && typeof self.views['list_at'] != 'undefined')
226 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
230 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
235 this.pool_settings.settings.use_db = true;
236 if ( $.ajatus.tags.active != ''
237 && typeof self.views['list_at'] != 'undefined')
239 this.pool_settings.settings.db = {
240 "temp": self.views['list_at']
243 this.pool_settings.settings.db = {
244 "static": self.name+'/list'
248 var on_success = function(data) {
249 var renderer = new $.ajatus.renderer.list(
250 $.ajatus.application_content_area,
253 id: self.name + '_list_holder',
254 pool: self.pool_settings
258 $.each(data.rows, function(i,doc){
259 var doc = new $.ajatus.document(doc);
260 renderer.render_item(doc, i);
263 renderer.items_added(doc_count);
268 if ( $.ajatus.tags.active != ''
269 && typeof self.views['list_at'] != 'undefined')
271 var first_doc = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
276 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
278 startkey: first_doc.key,
279 startkey_docid: first_doc.id,
280 count: $.ajatus.renderer_defaults.items_per_page
283 var first_doc = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
288 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
290 startkey: first_doc.key,
291 startkey_docid: first_doc.id,
292 count: $.ajatus.renderer_defaults.items_per_page
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')
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('');
311 if ( $.ajatus.tags.active != ''
312 && typeof self.views['list_at'] != 'undefined')
314 doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
318 doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
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])
332 var get_items = function(on_success) {
333 if ( $.ajatus.tags.active != ''
334 && typeof self.views['list_at'] != 'undefined')
336 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
340 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
346 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Export %s', [self.name], self.name), {
349 $.ajatus.views.export_view();
353 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive visible'), {
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);
362 get_items(on_success);
366 $.ajatus.toolbar.show();
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;
374 var renderer = new $.ajatus.renderer.list(
375 $.ajatus.application_content_area,
378 id: self.name + '_export_list_holder',
380 headers: list_headers,
381 title: 'Export ' + self.title
385 $.each(data.rows, function(i,doc){
386 var doc = new $.ajatus.document(doc);
387 renderer.render_item(doc, i);
390 renderer.items_added();
391 renderer.enable_sorting();
396 get_items(on_success);