Merge branch 'master' of git+ssh://repo.or.cz/srv/git/ajatus
[ajatus.git] / js / ajatus.types.js
blob7a6988bb4765181a5526596a05ae40c2a8110737
1 /*
2 * This file is part of
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
15 (function($){
16 $.ajatus = $.ajatus || {};
18 $.ajatus.types = {};
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({
28 watch: {
29 validate: function(){return $.ajatus.events.named_lock_pool.count('init_types') == 0;},
30 interval: 200,
31 safety_runs: 0
33 on_release: on_ready
34 });
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]);
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';
47 $.ajatus.events.named_lock_pool.increase('init_types');
49 $.ajatus.utils.load_script(type_url, "$.ajatus.types.type_loaded", [type]);
50 });
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 = {
74 enabled: true,
75 settings: {
76 type: 'scroll'
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')
85 $.ajatus.preferences.client.content_types[type].statics = {
86 list: $.ajatus.preferences.client.content_types[type].views.list
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)
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);
106 access_key: 'Ctrl+n'
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;
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,
126 id: self.name + '_list_holder',
127 pool: self.pool_settings
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);
137 } else {
138 renderer.render_item(doc, i);
141 renderer.items_added();
142 renderer.enable_sorting();
144 return data;
147 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, self.name+'/list', {
148 descending: true
150 } else {
151 this.pool_settings.settings.use_db = true;
152 this.pool_settings.settings.db = {
153 "static": self.name+'/list'
156 var on_success = function(data) {
157 var renderer = new $.ajatus.renderer.list(
158 $.ajatus.application_content_area,
159 self,
161 id: self.name + '_list_holder',
162 pool: self.pool_settings
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);
172 } else {
173 renderer.render_item(doc, i);
176 renderer.items_added(doc_count);
178 return data;
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
197 })(jQuery);