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') {
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;
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 = {
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']);
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'
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')
100 $.ajatus.preferences.client.content_types[type]['title_item'] = [
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+'") {';
108 if (typeof $.ajatus.preferences.client.content_types[type]['view_footer'] == 'undefined') {
109 $.ajatus.preferences.client.content_types[type].view_footer = '}';
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';
119 $.ajatus.preferences.client.content_types[type].list_map = list_map;
122 if (typeof $.ajatus.preferences.client.content_types[type]['gen_views'] != 'undefined') {
123 $.ajatus.preferences.client.content_types[type]['gen_views']();
126 if (typeof $.ajatus.preferences.client.content_types[type]['listen_signals'] == 'undefined') {
127 $.ajatus.preferences.client.content_types[type].listen_signals = [];
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');
133 if (typeof $.ajatus.preferences.client.content_types[type]['on_signal'] == 'undefined') {
134 $.ajatus.preferences.client.content_types[type].on_signal = {};
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);
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')
154 $.ajatus.preferences.client.content_types[type].statics = {
155 list: $.ajatus.preferences.client.content_types[type].views.list
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]();
166 $.ajatus.debug('Undefined view '+view_name, type);
169 var app_tab_holder = $('#tabs-application ul');
170 app_tab_holder.html('');
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>');
177 tab.appendTo(app_tab_holder);
178 $.ajatus.tabs.prepare(tab);
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]);
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)
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();
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))
207 $.ajatus.toolbar.add_item($.ajatus.i10n.get('New %s', [self.name]), {
208 icon: self.name+'-new.png',
210 $.ajatus.views.system.create.render(self);
214 $.ajatus.toolbar.show();
217 if ( $.ajatus.tags.active != ''
218 && typeof self.views['list_at'] != 'undefined')
220 doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
224 doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
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]));
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,
241 id: self.name + '_list_holder',
242 pool: self.pool_settings
246 $.each(data.rows, function(i,doc){
247 var doc = new $.ajatus.document(doc);
248 renderer.render_item(doc, i);
251 renderer.items_added();
252 renderer.enable_sorting();
258 if ( $.ajatus.tags.active != ''
259 && typeof self.views['list_at'] != 'undefined')
261 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
265 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
270 this.pool_settings.settings.use_db = true;
271 if ( $.ajatus.tags.active != ''
272 && typeof self.views['list_at'] != 'undefined')
274 this.pool_settings.settings.db = {
275 "temp": self.views['list_at']
278 this.pool_settings.settings.db = {
279 "static": self.name+'/list'
283 var on_success = function(data) {
284 var renderer = new $.ajatus.renderer.list(
285 $.ajatus.application_content_area,
288 id: self.name + '_list_holder',
289 pool: self.pool_settings
293 $.each(data.rows, function(i,doc){
294 var doc = new $.ajatus.document(doc);
295 renderer.render_item(doc, i);
298 renderer.items_added(doc_count);
304 if ( $.ajatus.tags.active != ''
305 && typeof self.views['list_at'] != 'undefined')
307 var first_doc = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
312 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
314 startkey: first_doc.key,
315 startkey_docid: first_doc.id,
316 count: $.ajatus.renderer_defaults.items_per_page
319 var first_doc = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
324 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
326 startkey: '"'+first_doc.key+'"',
327 startkey_docid: first_doc.id,
328 count: $.ajatus.renderer_defaults.items_per_page
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')
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();
345 $.ajatus.active_type = self;
346 $.ajatus.layout.body.set_class('export '+self.name);
347 $.ajatus.application_content_area.html('');
350 if ( $.ajatus.tags.active != ''
351 && typeof self.views['list_at'] != 'undefined')
353 doc_count = $.jqCouch.connection('view').temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
357 doc_count = $.jqCouch.connection('view').get($.ajatus.preferences.client.content_database, self.name+'/list', {
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])
371 var get_items = function(on_success) {
372 if ( $.ajatus.tags.active != ''
373 && typeof self.views['list_at'] != 'undefined')
375 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views['list_at'], {
379 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
385 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Export %s', [self.name], self.name), {
388 $.ajatus.views.export_view();
392 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive visible'), {
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);
404 get_items(on_success);
409 $.ajatus.toolbar.show();
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;
417 var renderer = new $.ajatus.renderer.list(
418 $.ajatus.application_content_area,
421 id: self.name + '_export_list_holder',
423 headers: list_headers,
424 title: 'Export ' + self.title
428 $.each(data.rows, function(i,doc){
429 var doc = new $.ajatus.document(doc);
430 renderer.render_item(doc, i);
433 renderer.items_added();
434 renderer.enable_sorting();
441 get_items(on_success);