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) {
65 if (typeof $.ajatus.preferences.client.content_types[type] == 'undefined') {
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;
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 = {
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+'") {';
86 if (typeof $.ajatus.preferences.client.content_types[type]['view_footer'] == 'undefined') {
87 $.ajatus.preferences.client.content_types[type].view_footer = '}';
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';
97 $.ajatus.preferences.client.content_types[type].list_map = list_map;
100 if (typeof $.ajatus.preferences.client.content_types[type]['gen_views'] != 'undefined') {
101 $.ajatus.preferences.client.content_types[type]['gen_views']();
104 if (typeof $.ajatus.preferences.client.content_types[type]['listen_signals'] == 'undefined') {
105 $.ajatus.preferences.client.content_types[type].listen_signals = [];
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');
111 if (typeof $.ajatus.preferences.client.content_types[type]['on_signal'] == 'undefined') {
112 $.ajatus.preferences.client.content_types[type].on_signal = {};
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);
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')
132 $.ajatus.preferences.client.content_types[type].statics = {
133 list: $.ajatus.preferences.client.content_types[type].views.list
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]();
144 $.ajatus.debug('Undefined view '+view_name, type);
147 var app_tab_holder = $('#tabs-application ul');
148 app_tab_holder.html('');
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>');
155 tab.appendTo(app_tab_holder);
156 $.ajatus.tabs.prepare(tab);
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]);
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)
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))
182 $.ajatus.toolbar.add_item($.ajatus.i10n.get('New %s', [self.name]), {
183 icon: self.name+'-new.png',
185 $.ajatus.views.system.create.render(self);
189 $.ajatus.toolbar.show();
192 if ( $.ajatus.tags.active != ''
193 && typeof self.views['list_at'] != 'undefined')
195 doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
199 doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
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]));
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,
216 id: self.name + '_list_holder',
217 pool: self.pool_settings
221 $.each(data.rows, function(i,doc){
222 var doc = new $.ajatus.document(doc);
223 renderer.render_item(doc, i);
226 renderer.items_added();
227 renderer.enable_sorting();
232 if ( $.ajatus.tags.active != ''
233 && typeof self.views['list_at'] != 'undefined')
235 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
239 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
244 this.pool_settings.settings.use_db = true;
245 if ( $.ajatus.tags.active != ''
246 && typeof self.views['list_at'] != 'undefined')
248 this.pool_settings.settings.db = {
249 "temp": self.views['list_at']
252 this.pool_settings.settings.db = {
253 "static": self.name+'/list'
257 var on_success = function(data) {
258 var renderer = new $.ajatus.renderer.list(
259 $.ajatus.application_content_area,
262 id: self.name + '_list_holder',
263 pool: self.pool_settings
267 $.each(data.rows, function(i,doc){
268 var doc = new $.ajatus.document(doc);
269 renderer.render_item(doc, i);
272 renderer.items_added(doc_count);
277 if ( $.ajatus.tags.active != ''
278 && typeof self.views['list_at'] != 'undefined')
280 var first_doc = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
285 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
287 startkey: first_doc.key,
288 startkey_docid: first_doc.id,
289 count: $.ajatus.renderer_defaults.items_per_page
292 var first_doc = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
297 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
299 startkey: first_doc.key,
300 startkey_docid: first_doc.id,
301 count: $.ajatus.renderer_defaults.items_per_page
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')
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('');
320 if ( $.ajatus.tags.active != ''
321 && typeof self.views['list_at'] != 'undefined')
323 doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
327 doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
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])
341 var get_items = function(on_success) {
342 if ( $.ajatus.tags.active != ''
343 && typeof self.views['list_at'] != 'undefined')
345 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
349 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
355 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Export %s', [self.name], self.name), {
358 $.ajatus.views.export_view();
362 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive visible'), {
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);
374 get_items(on_success);
379 $.ajatus.toolbar.show();
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;
387 var renderer = new $.ajatus.renderer.list(
388 $.ajatus.application_content_area,
391 id: self.name + '_export_list_holder',
393 headers: list_headers,
394 title: 'Export ' + self.title
398 $.each(data.rows, function(i,doc){
399 var doc = new $.ajatus.document(doc);
400 renderer.render_item(doc, i);
403 renderer.items_added();
404 renderer.enable_sorting();
409 get_items(on_success);