Added tag type.
[ajatus.git] / js / ajatus.views.js
blob56ec48dfa38e521a671a5a8112ca5c52146247b6
1 /*
2  * This file is part of
3  *
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
12  * 
13  */
15 (function($){
16     $.ajatus = $.ajatus || {};
17     $.ajatus.views = $.ajatus.views || {};
18     
19     $.ajatus.views.init = function()
20     {
21         var views_tab_holder = $('#tabs-views ul');
22         var app_tab_holder = $('#tabs-application ul');
23         var app_views_holder = $('#application-views ul');
24         
25         $.each($.ajatus.views.system.available, function(i,sv){
26             var view = $.ajatus.views.system[sv];
27             if (   view.in_tabs
28                 && view.tab)
29             {
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)
36                 {                    
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);
40                     }
41                 } else {                    
42                     tab.appendTo(views_tab_holder);
43                 }
44                 $.ajatus.tabs.prepare(tab);
45                 
46                 if (   $.ajatus.history
47                     && view.history_support) {
48                     $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render("list");');
49                 } else {
50                     tab.bind('click',view.tab.on_click);
51                 }
52                 
53                 // if (   typeof view.has_additional_views != 'undefined'
54                 //     && view.has_additional_views
55                 //     && typeof view.generate_additional_views == 'function')
56                 // {
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>');
63                 //         }
64                 //         tab.appendTo(app_tab_holder);
65                 // 
66                 //         $.ajatus.history.add_map(data.view_hash, '$.ajatus.history.update("'+data.view_hash+'");$.ajatus.views.system.'+sv+'.render("'+name+'");');
67                 //     });
68                 // }
69             }
70             
71             if (typeof view['statics'] != 'undefined') {
72                 $.ajatus.views.install_statics(sv, view);
73             }
74             
75             if (   $.ajatus.history
76                 && view.dynamic_history
77                 && typeof view.history_register == 'function')
78             {
79                 view.history_register();
80             }
81             // $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render();');
82         });
83         $('li:first', views_tab_holder).addClass('tabs-selected');
84         
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);
91         
92         $.ajatus.history.add_map(view_hash, '$.ajatus.preferences.view.render("edit");');
93         
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");');
102             
103             $.each($.ajatus.development.subview.available, function(i, name){
104                 var view = $.ajatus.development.subview[name];
105                 
106                 var vhash = '#view.development.'+name;
107                 if (typeof view.view_hash != 'undefined') {
108                     vhash = view.view_hash;
109                 }
110                 
111                 $.ajatus.history.add_map(vhash, '$.ajatus.history.update("'+vhash+'");$.ajatus.development.view.render("'+name+'");');
112             });
113         }
114         
115         $.each($.ajatus.preferences.client.content_types, function(key,type){
116             if (   type.in_tabs
117                 && type.tab)
118             {
119                 var view_hash = '#view.'+key;
120                 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(type.title)+'</span></a></li>');
121                 
122                 tab.appendTo(views_tab_holder);
123                 
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");');
128                 } else {
129                     tab.bind('click',type.tab.on_click);
130                 }
131             }
132             
133             if (typeof type.additional_views != 'undefined') {
134                 $.each(type.additional_views, function(name, data){
135                     var view_hash = '#'+data.hash_key+'.'+key;
136                     
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+'");');
138                 });
139             }
140             
141             if (typeof type['statics'] != 'undefined') {
142                 $.ajatus.views.install_statics(key, type);
143             }
144         });
145         
146         if ($.ajatus.views['custom'])
147         {
148             $.ajatus.views.get_custom_views();          
149         }
150     };
151     
152     $.ajatus.views.get_custom_views = function()
153     {
154         if ($.ajatus.preferences.client.custom_views.length > 0)
155         {
156             $.each($.ajatus.preferences.client.custom_views, function(i,cv){
157                 $.ajatus.events.lock_pool.increase();
158                 
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);
162                     
163                     $.ajatus.events.lock_pool.decrease();
164                 });
165             });
166         }
167     };
168     
169     $.ajatus.views.install_statics = function(view_name, view)
170     {
171         var view_exists = false;
172         var existing_rev = null;
173         var vc = $.jqCouch.connection('view');
174         
175         var static_views = {
176             _id: '_design/' + view_name,
177             views: {}
178         };
179         
180         $.each(view.statics, function(name,fn){
181             if (   name
182                 && fn)
183             {
184                 if (typeof(fn) == 'string') {
185                     fn = $.ajatus.views.generate(fn);
186                 }
188                 static_views.views[name] = fn;
189             }
190         });
191         
192         vc.exists($.ajatus.preferences.client.content_database, view_name, function(data){
193             view_exists = true;
194             existing_rev = data._rev;
195         });
196         
197         var update = view.update_statics;
198         if ($.ajatus.maintenance.recreate['views']) {
199             update = true;
200         }
201         
202         if (   !view_exists
203             || update)
204         {
205             if (   view_exists
206                 && update)
207             {
208                 static_views['_rev'] = existing_rev;
209             }
210             vc.save($.ajatus.preferences.client.content_database, static_views);
211         }
212     };
213     
214     $.ajatus.views.on_change_calls = {
215         called: {}
216     };
217     $.ajatus.views.on_change_actions = {
218         list: [],
219         add: function(action) {
220             if ($.inArray(action, $.ajatus.views.on_change_actions.list) == -1) {
221                 $.ajatus.views.on_change_actions.list.push(action);
222             }
223         },
224         run: function() {
225             $.each($.ajatus.views.on_change_actions.list, function(i,a){
226                 eval(a);
227             });
228             $.ajatus.views.on_change_actions.list = [];
229         }
230     }
231     $.ajatus.views.on_change = function(new_view, keep_app_tabs)
232     {
233         // console.log('$.ajatus.views.on_change('+new_view+')');
234         
235         if (typeof keep_app_tabs == 'undefined') {
236             keep_app_tabs = false;
237         }
238         
239         if (   typeof($.ajatus.views.on_change_calls.called[new_view]) == 'undefined'
240             || $.ajatus.views.on_change_calls.called[new_view] == 2)
241         {
242             $.ajatus.views.on_change_calls.called[new_view] = 0;
243         }
244         
245         // $.ajatus.debug('$.ajatus.views.on_change_calls.called['+new_view+']: '+$.ajatus.views.on_change_calls.called[new_view]);
246         
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('');                
253             }
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();
260             
261             $.ajatus.events.signals.trigger('view_on_change', {
262                 new_view: new_view
263             });
264         }
265     };
266     
267     $.ajatus.views.generate = function(fn_str)
268     {
269         var fn = "function(){var doc = arguments[0]; "+fn_str+"}";
270         try {
271             return eval('(' + fn + ')');
272         } catch(e) {
273             return function(){};
274         }
275         
276     }
277     
278     $.ajatus.views.export_view = function(table, output_type) {
279         if (typeof output_type == 'undefined') {
280             var output_type = 'csv';
281         }
282         if (typeof table == 'undefined') {
283             var table = $('table.listing', $.ajatus.application_content_area)[0];
284         }
285         
286         var popup = window.open('exported.csv', 'Ajatus CRM - Export', 'width=765,height=480,resizable=false,scrollbars=no');
287         var csvout = popup.document;
288         
289         var line_sep = "\n";
290         if ($.browser.safari) {
291             line_sep = "<br>";
292         }
293         
294         if (output_type == 'csv') {
295             var cntrows = 0;
296             var tempdata = "";
297         
298             var numofRows = table.rows.length-1;
299             var numofCells = table.rows[0].cells.length-1;
300         
301             var rowcsv = [numofRows];
302             
303             for ( var r = 0; r <= numofRows; r++) {
304                 var c =0;
305                 tempdata = "";
306                 for (c == 0; c<=numofCells; c++) {
307                     if (c != numofCells) {
308                         tempdata += $.trim($(table.rows[r].cells[c]).text()) + ",";
309                     } else {
310                         tempdata += $.trim($(table.rows[r].cells[c]).text()) + line_sep;
311                     }
312                 }
313                 rowcsv[r] = tempdata;
314             }
315             
316             csvout.open("text/comma-separated-values");
317             
318             for (var rowcnt = 0; rowcnt <= rowcsv.length-1; rowcnt++) {
319                 csvout.write(rowcsv[rowcnt]);
320             }
321             
322             csvout.close();
323         }
324     }
325     
326 })(jQuery);