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');
23 var app_views_holder = $('#application-views ul');
25 $.each($.ajatus.views.system.available, function(i,sv){
26 var view = $.ajatus.views.system[sv];
30 var view_hash = '#view.'+sv;
32 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(view.title)+'</span></a></li>');
34 if ( typeof(view.application_view) != 'undefined'
35 && view.application_view)
37 if (typeof view.icon != 'undefined') {
38 tab = $('<li><a href="'+view_hash+'" title="'+$.ajatus.i10n.get(view.title)+'"><img src="'+ $.ajatus.preferences.client.theme_icons_url + view.icon + '" /></a></li>');
39 tab.appendTo(app_views_holder);
42 tab.appendTo(views_tab_holder);
44 $.ajatus.tabs.prepare(tab);
47 && view.history_support) {
48 $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render("list");');
50 tab.bind('click',view.tab.on_click);
53 // if ( typeof view.has_additional_views != 'undefined'
54 // && view.has_additional_views
55 // && typeof view.generate_additional_views == 'function')
57 // app_tab_holder.html('');
58 // var addviews = view.generate_additional_views();
59 // $.each(addviews, function(name, data){
60 // var tab = $('<li><a href="'+data.view_hash+'"><span>'+$.ajatus.i10n.get(data.title)+'</span></a></li>');
61 // if (typeof data.icon != 'undefined') {
62 // tab = $('<li class="iconified"><a href="'+data.view_hash+'"><img src="'+ $.ajatus.preferences.client.theme_icons_url + data.icon + '" alt="'+$.ajatus.i10n.get(data.title)+'"/></a></li>');
64 // tab.appendTo(app_tab_holder);
66 // $.ajatus.history.add_map(data.view_hash, '$.ajatus.history.update("'+data.view_hash+'");$.ajatus.views.system.'+sv+'.render("'+name+'");');
71 if (typeof view['statics'] != 'undefined') {
72 $.ajatus.views.install_statics(sv, view);
76 && view.dynamic_history
77 && typeof view.history_register == 'function')
79 view.history_register();
81 // $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render();');
83 $('li:first', views_tab_holder).addClass('tabs-selected');
85 // Add preferences view
86 var pref_view = $.ajatus.preferences.view;
87 var view_hash = '#view.preferences';
88 var tab = $('<li><a href="'+view_hash+'" title="'+$.ajatus.i10n.get(pref_view.title)+'"><img src="'+ $.ajatus.preferences.client.theme_icons_url + pref_view.icon + '" /></a></li>');
89 tab.appendTo(app_views_holder);
90 $.ajatus.tabs.prepare(tab);
92 $.ajatus.history.add_map(view_hash, '$.ajatus.preferences.view.render("edit");');
94 if ($.ajatus.preferences.client.developer_tools) {
95 var dev_view = $.ajatus.development.view;
96 var view_hash = '#view.development';
97 var tab = $('<li><a href="'+view_hash+'" title="'+$.ajatus.i10n.get(dev_view.title)+'"><img src="'+ $.ajatus.preferences.client.theme_icons_url + dev_view.icon + '" /></a></li>');
98 tab.appendTo(app_views_holder);
99 $.ajatus.tabs.prepare(tab);
101 $.ajatus.history.add_map(view_hash, '$.ajatus.development.view.render("frontpage");');
103 $.each($.ajatus.development.subview.available, function(i, name){
104 var view = $.ajatus.development.subview[name];
106 var vhash = '#view.development.'+name;
107 if (typeof view.view_hash != 'undefined') {
108 vhash = view.view_hash;
111 $.ajatus.history.add_map(vhash, '$.ajatus.history.update("'+vhash+'");$.ajatus.development.view.render("'+name+'");');
115 $.each($.ajatus.preferences.client.content_types, function(key,type){
119 var view_hash = '#view.'+key;
120 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(type.title)+'</span></a></li>');
122 tab.appendTo(views_tab_holder);
124 $.ajatus.tabs.prepare(tab);
125 if ( $.ajatus.history
126 && type.history_support) {
127 $.ajatus.history.add_map(view_hash, '$.ajatus.history.update("'+view_hash+'");$.ajatus.views.on_change("'+view_hash+'");$.ajatus.preferences.client.content_types["'+key+'"].render("list");');
129 tab.bind('click',type.tab.on_click);
133 if (typeof type.additional_views != 'undefined') {
134 $.each(type.additional_views, function(name, data){
135 var view_hash = '#'+data.hash_key+'.'+key;
137 $.ajatus.history.add_map(view_hash, '$.ajatus.history.update("'+view_hash+'");$.ajatus.views.on_change("'+view_hash+'");$.ajatus.preferences.client.content_types["'+key+'"].render("'+name+'");');
141 if (typeof type['statics'] != 'undefined') {
142 $.ajatus.views.install_statics(key, type);
146 if ($.ajatus.views['custom'])
148 $.ajatus.views.get_custom_views();
152 $.ajatus.views.get_custom_views = function()
154 if ($.ajatus.preferences.client.custom_views.length > 0)
156 $.each($.ajatus.preferences.client.custom_views, function(i,cv){
157 $.ajatus.events.lock_pool.increase();
159 $.getScript($.ajatus.preferences.client.application_url + 'js/views/custom/'+cv+'.js', function(){
160 $.ajatus.views.custom.available[cv] = $.ajatus.views.custom[cv];
161 $.ajatus.views.custom.init(cv);
163 $.ajatus.events.lock_pool.decrease();
169 $.ajatus.views.install_statics = function(view_name, view)
171 var view_exists = false;
172 var existing_rev = null;
173 var vc = $.jqCouch.connection('view');
176 _id: '_design/' + view_name,
180 $.each(view.statics, function(name,fn){
184 if (typeof(fn) == 'string') {
185 fn = $.ajatus.views.generate(fn);
188 static_views.views[name] = fn;
192 vc.exists($.ajatus.preferences.client.content_database, view_name, function(data){
194 existing_rev = data._rev;
197 var update = view.update_statics;
198 if ($.ajatus.maintenance.recreate['views']) {
208 static_views['_rev'] = existing_rev;
210 vc.save($.ajatus.preferences.client.content_database, static_views);
214 $.ajatus.views.on_change_calls = {
217 $.ajatus.views.on_change_actions = {
219 add: function(action) {
220 if ($.inArray(action, $.ajatus.views.on_change_actions.list) == -1) {
221 $.ajatus.views.on_change_actions.list.push(action);
225 $.each($.ajatus.views.on_change_actions.list, function(i,a){
228 $.ajatus.views.on_change_actions.list = [];
231 $.ajatus.views.on_change = function(new_view, keep_app_tabs)
233 // console.log('$.ajatus.views.on_change('+new_view+')');
235 if (typeof keep_app_tabs == 'undefined') {
236 keep_app_tabs = false;
239 if ( typeof($.ajatus.views.on_change_calls.called[new_view]) == 'undefined'
240 || $.ajatus.views.on_change_calls.called[new_view] == 2)
242 $.ajatus.views.on_change_calls.called[new_view] = 0;
245 // $.ajatus.debug('$.ajatus.views.on_change_calls.called['+new_view+']: '+$.ajatus.views.on_change_calls.called[new_view]);
247 if ($.ajatus.views.on_change_calls.called[new_view] < 2) {
248 $.ajatus.views.on_change_calls.called[new_view] += 1;
250 // $.ajatus.forms.clear_data();
251 if (! keep_app_tabs) {
252 $('#tabs-application ul').html('');
254 $.ajatus.events.named_lock_pool.clear('unsaved');
255 $.ajatus.elements.messages.clear();
256 $.ajatus.toolbar.clear();
257 $.ajatus.views.on_change_actions.run();
259 $.ajatus.document.actions.empty_pool();
261 $.ajatus.events.signals.trigger('view_on_change', {
267 $.ajatus.views.generate = function(fn_str)
269 var fn = "function(){var doc = arguments[0]; "+fn_str+"}";
271 return eval('(' + fn + ')');
278 $.ajatus.views.export_view = function(table, output_type) {
279 if (typeof output_type == 'undefined') {
280 var output_type = 'csv';
282 if (typeof table == 'undefined') {
283 var table = $('table.listing', $.ajatus.application_content_area)[0];
286 var popup = window.open('exported.csv', 'Ajatus CRM - Export', 'width=765,height=480,resizable=false,scrollbars=no');
287 var csvout = popup.document;
290 if ($.browser.safari) {
294 if (output_type == 'csv') {
298 var numofRows = table.rows.length-1;
299 var numofCells = table.rows[0].cells.length-1;
301 var rowcsv = [numofRows];
303 for ( var r = 0; r <= numofRows; r++) {
306 for (c == 0; c<=numofCells; c++) {
307 if (c != numofCells) {
308 tempdata += $.trim($(table.rows[r].cells[c]).text()) + ",";
310 tempdata += $.trim($(table.rows[r].cells[c]).text()) + line_sep;
313 rowcsv[r] = tempdata;
316 csvout.open("text/comma-separated-values");
318 for (var rowcnt = 0; rowcnt <= rowcsv.length-1; rowcnt++) {
319 csvout.write(rowcsv[rowcnt]);