Removed history enabled checks as it is required by core
[ajatus.git] / js / ajatus.views.js
blob72b94e702b0ca0d52dbe2e68d5fbf5d4246a3774
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         
24         $.each($.ajatus.views.system.available, function(i,sv){
25             var view = $.ajatus.views.system[sv];
26             if (   view.in_tabs
27                 && view.tab)
28             {
29                 var view_hash = '#view.'+sv;
30                 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(view.title)+'</span></a></li>');
31                 
32                 if (   typeof(view.application_tab) != 'undefined'
33                     && view.application_tab)
34                 {
35                     tab.appendTo(app_tab_holder);                                        
36                 } else {
37                     tab.appendTo(views_tab_holder);
38                 }
39                 $.ajatus.tabs.prepare(tab);
40                 
41                 if (   $.ajatus.history
42                     && view.history_support) {
43                     $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render();');
44                 } else {
45                     tab.bind('click',view.tab.on_click);
46                 }
47             }
48             
49             if (typeof view['statics'] != 'undefined') {
50                 $.ajatus.views.install_statics(sv, view);
51             }
52             
53             if (   $.ajatus.history
54                 && view.dynamic_history
55                 && typeof view.history_register == 'function')
56             {
57                 view.history_register();
58             }
59             // $.ajatus.history.add_map(view_hash, '$.ajatus.views.system.'+sv+'.render();');
60         });
61         $('li:first', views_tab_holder).addClass('tabs-selected');
62         
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);
69         
70         $.ajatus.history.add_map(view_hash, '$.ajatus.preferences.view.render();');
71         
72         $.each($.ajatus.preferences.client.content_types, function(key,type){
73             if (   type.in_tabs
74                 && type.tab)
75             {
76                 var view_hash = '#view.'+key;
77                 var tab = $('<li><a href="'+view_hash+'"><span>'+$.ajatus.i10n.get(type.title)+'</span></a></li>');
78                 
79                 if (   typeof(type.application_tab) != 'undefined'
80                     && type.application_tab)
81                 {
82                     tab.appendTo(app_tab_holder);                                        
83                 } else {
84                     tab.appendTo(views_tab_holder);
85                 }
86                 
87                 $.ajatus.tabs.prepare(tab);
88                 if (   $.ajatus.history
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();');
91                 } else {
92                     tab.bind('click',type.tab.on_click);
93                 }
94             }
95             if (typeof type['statics'] != 'undefined') {
96                 $.ajatus.views.install_statics(key, type);
97             }
98         });
99         
100         if ($.ajatus.views['custom'])
101         {
102             $.ajatus.views.get_custom_views();          
103         }
104     };
105     
106     $.ajatus.views.get_custom_views = function()
107     {
108         if ($.ajatus.preferences.client.custom_views.length > 0)
109         {
110             $.each($.ajatus.preferences.client.custom_views, function(i,cv){
111                 $.ajatus.events.lock_pool.increase();
112                 
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);
116                     
117                     $.ajatus.events.lock_pool.decrease();
118                 });
119             });
120         }
121     };
122     
123     $.ajatus.views.install_statics = function(view_name, view)
124     {
125         var view_exists = false;
126         var existing_rev = null;
127         var vc = $.jqCouch.connection('view');
128         
129         var static_views = {
130             _id: '_design/' + view_name,
131             views: {}
132         };
133         
134         $.each(view.statics, function(name,fn){
135             if (   name
136                 && fn)
137             {
138                 if (typeof(fn) == 'string') {
139                     fn = $.ajatus.views.generate(fn);
140                 }
142                 static_views.views[name] = fn;
143             }
144         });
145         
146         vc.exists($.ajatus.preferences.client.content_database, view_name, function(data){
147             view_exists = true;
148             existing_rev = data._rev;
149         });
150         
151         if (   !view_exists
152             || view.update_statics)
153         {
154             if (   view_exists
155                 && view.update_statics)
156             {
157                 static_views['_rev'] = existing_rev;
158             }
159             vc.save($.ajatus.preferences.client.content_database, static_views);
160         }
161     };
162     
163     $.ajatus.views.on_change_calls = {
164         called: {}
165     };
166     $.ajatus.views.on_change_actions = {
167         list: [],
168         add: function(action) {
169             $.ajatus.views.on_change_actions.list.push(action);
170         },
171         run: function() {
172             $.each($.ajatus.views.on_change_actions.list, function(i,a){
173                 eval(a);
174             });
175             $.ajatus.views.on_change_actions.list = [];
176         }
177     }
178     $.ajatus.views.on_change = function(new_view)
179     {
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)
183         {
184             $.ajatus.views.on_change_calls.called[new_view] = 0;
185         }
186         
187         // $.ajatus.debug('$.ajatus.views.on_change_calls.called['+new_view+']: '+$.ajatus.views.on_change_calls.called[new_view]);
188         
189         if ($.ajatus.views.on_change_calls.called[new_view] < 2) {
190             $.ajatus.views.on_change_calls.called[new_view] += 1;
191             
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();
198         }
199     };
200     
201     $.ajatus.views.generate = function(fn_str)
202     {
203         var fn = "function(){var doc = arguments[0]; "+fn_str+"}";
204         return eval('(' + fn + ')');
205     }
206     
207 })(jQuery);