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 || {};
17 $.ajatus.views = $.ajatus.views || {};
19 $.ajatus.views.init = function()
21 var views_tab_holder = $('#tabs-views ul');
22 var app_tab_holder = $('#tabs-application ul');
24 $.each($.ajatus.views.system.available, function(i,sv){
25 var view = $.ajatus.views.system[sv];
29 var view_hash = '#view.'+sv;
30 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(view.title)+'</span></a></li>');
32 if ( typeof(view.application_tab) != 'undefined'
33 && view.application_tab)
35 tab.appendTo(app_tab_holder);
37 tab.appendTo(views_tab_holder);
39 $.ajatus.tabs.prepare(tab);
42 && view.history_support) {
43 $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render();');
45 tab.bind('click',view.tab.on_click);
49 if (typeof view['statics'] != 'undefined') {
50 $.ajatus.views.install_statics(sv, view);
54 && view.dynamic_history
55 && typeof view.history_register == 'function')
57 view.history_register();
59 // $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render();');
61 $('li:first', views_tab_holder).addClass('tabs-selected');
63 // Position preferences view tab
64 var pref_view = $.ajatus.preferences.view;
65 var view_hash = '#view.preferences';
66 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(pref_view.title)+'</span></a></li>');
67 tab.prependTo(app_tab_holder);
68 $.ajatus.tabs.prepare(tab);
70 $.ajatus.history.add_map(view_hash, '$.ajatus.preferences.view.render();');
72 $.each($.ajatus.preferences.client.content_types, function(key,type){
76 var view_hash = '#view.'+key;
77 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(type.title)+'</span></a></li>');
79 if ( typeof(type.application_tab) != 'undefined'
80 && type.application_tab)
82 tab.appendTo(app_tab_holder);
84 tab.appendTo(views_tab_holder);
87 $.ajatus.tabs.prepare(tab);
89 && type.history_support) {
90 $.ajatus.history.add_map(view_hash, '$.ajatus.history.update("'+view_hash+'");$.ajatus.views.on_change("'+view_hash+'");$.ajatus.preferences.client.content_types["'+key+'"].render();');
92 tab.bind('click',type.tab.on_click);
95 if (typeof type['statics'] != 'undefined') {
96 $.ajatus.views.install_statics(key, type);
100 if ($.ajatus.views['custom'])
102 $.ajatus.views.get_custom_views();
106 $.ajatus.views.get_custom_views = function()
108 if ($.ajatus.preferences.client.custom_views.length > 0)
110 $.each($.ajatus.preferences.client.custom_views, function(i,cv){
111 $.ajatus.events.lock_pool.increase();
113 $.getScript($.ajatus.preferences.client.application_url + 'js/views/custom/'+cv+'.js', function(){
114 $.ajatus.views.custom.available[cv] = $.ajatus.views.custom[cv];
115 $.ajatus.views.custom.init(cv);
117 $.ajatus.events.lock_pool.decrease();
123 $.ajatus.views.install_statics = function(view_name, view)
125 var view_exists = false;
126 var existing_rev = null;
127 var vc = $.jqCouch.connection('view');
130 _id: '_design/' + view_name,
134 $.each(view.statics, function(name,fn){
138 if (typeof(fn) == 'string') {
139 fn = $.ajatus.views.generate(fn);
142 static_views.views[name] = fn;
146 vc.exists($.ajatus.preferences.client.content_database, view_name, function(data){
148 existing_rev = data._rev;
152 || view.update_statics)
155 && view.update_statics)
157 static_views['_rev'] = existing_rev;
159 vc.save($.ajatus.preferences.client.content_database, static_views);
163 $.ajatus.views.on_change_calls = {
166 $.ajatus.views.on_change_actions = {
168 add: function(action) {
169 $.ajatus.views.on_change_actions.list.push(action);
172 $.each($.ajatus.views.on_change_actions.list, function(i,a){
175 $.ajatus.views.on_change_actions.list = [];
178 $.ajatus.views.on_change = function(new_view)
180 // $.ajatus.debug('$.ajatus.views.on_change('+new_view+')');
181 if ( typeof($.ajatus.views.on_change_calls.called[new_view]) == 'undefined'
182 || $.ajatus.views.on_change_calls.called[new_view] == 2)
184 $.ajatus.views.on_change_calls.called[new_view] = 0;
187 // $.ajatus.debug('$.ajatus.views.on_change_calls.called['+new_view+']: '+$.ajatus.views.on_change_calls.called[new_view]);
189 if ($.ajatus.views.on_change_calls.called[new_view] < 2) {
190 $.ajatus.views.on_change_calls.called[new_view] += 1;
192 $.ajatus.events.named_lock_pool.clear('unsaved');
193 $.ajatus.elements.messages.clear();
194 $.ajatus.toolbar.clear();
195 $.ajatus.views.on_change_actions.run();
197 $.ajatus.document.actions.empty_pool();
201 $.ajatus.views.generate = function(fn_str)
203 var fn = "function(){var doc = arguments[0]; "+fn_str+"}";
204 return eval('(' + fn + ')');