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
.system
= {
21 'frontpage', 'edit', 'create', 'item', 'trash'
24 $.ajatus
.views
.system
.frontpage
= {
28 max_items_before_pool
: 3
31 history_support
: true,
43 $.ajatus
.tabs
.on_click(e
);
44 $.ajatus
.views
.system
.frontpage
.render();
50 $.ajatus
.application_content_area
.html('');
51 $.ajatus
.layout
.body
.set_class('frontpage');
52 $.ajatus
.views
.on_change("#view.frontpage");
54 $.each($.ajatus
.preferences
.client
.content_types
, function(key
,type
){
55 if (! type
.views
['list']) {
59 var on_success = function(data
) {
60 if (data
.total_rows
== 0) {
64 var pool_settings
= $.ajatus
.views
.system
.frontpage
.pool_settings
;
65 if (data
.total_rows
>= $.ajatus
.views
.system
.frontpage
.options
.max_items_before_pool
) {
66 pool_settings
.enabled
= true;
69 var renderer
= new $.ajatus
.renderer
.list(
70 $.ajatus
.application_content_area
,
73 id
: type
.name
+ '_list_holder',
79 $.each(data
.rows
, function(i
,doc
){
80 var doc
= new $.ajatus
.document(doc
);
81 if ($.ajatus
.tags
.active
!= '') {
82 if ($.ajatus
.utils
.array
.has_match($.ajatus
.tags
.active
, doc
.value
.tags
.val
)) {
83 renderer
.render_item(doc
, i
);
86 renderer
.render_item(doc
, i
);
93 $.jqCouch
.connection('view', on_success
).temp($.ajatus
.preferences
.client
.content_database
, type
.views
.list
, {
94 count
: $.ajatus
.views
.system
.frontpage
.options
.group_item_count
,
100 $.ajatus
.views
.system
.tags
= {
107 on_click: function(e
) {
108 $.ajatus
.tabs
.on_click(e
);
109 $.ajatus
.views
.system
.tags
.render();
114 $.ajatus
.application_content_area
.html('');
116 var orphaned_view
= $.ajatus
.views
.generate('');
119 $.ajatus
.views
.system
.trash
= {
125 application_tab
: true,
126 history_support
: true,
129 on_click: function(e
)
131 $.ajatus
.tabs
.on_click(e
);
132 $.ajatus
.views
.system
.trash
.render();
136 get_items: function(on_success
) {
137 var trash_view
= 'if (doc.value.metadata.deleted.val == 1) {';
138 trash_view
+= 'map( doc.value.metadata.revised, {"_type": doc.value._type,';
139 trash_view
+= '"title": doc.value.title,';
141 $.each($.ajatus
.preferences
.client
.content_types
, function(key
,type
){
142 if (type
['title_item']) {
143 $.each(type
['title_item'], function(i
,part
){
144 if (type
.schema
[part
]) {
145 trash_view
+= '"'+part
+'": doc.value.'+part
+',';
151 trash_view
+= '"revised": doc.value.metadata.revised,"by": doc.value.metadata.revisor,"tags": doc.value.tags})}';
153 var conn_on_success = function(data
) {
154 if (data
.total_rows
== 0) {
155 var key
= $.ajatus
.i10n
.plural($.ajatus
.i10n
.get('deleted item'));
156 var msg
= $.ajatus
.elements
.messages
.static($.ajatus
.i10n
.get('Empty results'), $.ajatus
.i10n
.get('No %s found', [key
]));
160 if (typeof on_success
== 'undefined') {
161 on_success
= $.ajatus
.views
.system
.trash
.render_results
;
164 on_success(data
.rows
);
168 $.jqCouch
.connection('view', conn_on_success
).temp($.ajatus
.preferences
.client
.content_database
, "$.ajatus.views.generate('"+trash_view
+"')", {
169 count
: $.ajatus
.views
.system
.trash
.options
.group_item_count
,
176 $.ajatus
.application_content_area
.html('');
177 $.ajatus
.layout
.body
.set_class('trash');
178 $.ajatus
.views
.on_change("#view.trash");
180 $.ajatus
.toolbar
.init();
181 $.ajatus
.toolbar
.show();
183 $.ajatus
.views
.system
.trash
.get_items();
186 render_results: function(rows
)
188 var result_count
= {};
189 var show_empty_trash
= false;
191 $.each($.ajatus
.preferences
.client
.content_types
, function(key
,type
){
192 if (!type
.views
['list']) {
196 result_count
[type
.name
] = 0;
198 $.each(rows
, function(i
,doc
){
199 if (doc
.value
._type
== type
.name
) {
200 result_count
[type
.name
] += 1;
204 if (result_count
[type
.name
] == 0) {
208 show_empty_trash
= true;
210 var renderer
= new $.ajatus
.renderer
.list(
211 $.ajatus
.application_content_area
,
214 id
: type
.name
+ '_list_holder',
218 'title', 'revised', 'by'
228 $.each(rows
, function(i
,doc
){
229 if (doc
.value
._type
== type
.name
) {
230 var doc
= new $.ajatus
.document(doc
);
232 renderer
.set_actions(
236 title
: $.ajatus
.i10n
.get('Undelete'),
237 click_action
: '$.ajatus.document.actions.execute("undelete", doc);'
240 name
: 'delete_final',
241 title
: $.ajatus
.i10n
.get('Delete'),
242 click_action
: '$.ajatus.document.actions.execute("delete_final", doc);'
246 if ($.ajatus
.tags
.active
!= '') {
247 if ($.ajatus
.utils
.array
.has_match($.ajatus
.tags
.active
, doc
.value
.tags
.val
)) {
248 renderer
.render_item(doc
, i
);
251 renderer
.render_item(doc
, i
);
256 renderer
.enable_sorting();
259 if (show_empty_trash
) {
260 $.ajatus
.toolbar
.add_item($.ajatus
.i10n
.get('Empty trash'), 'images/icons/trash.png', function(){
261 $.ajatus
.views
.system
.trash
.empty();
266 $.ajatus
.views
.system
.trash
.get_items($.ajatus
.views
.system
.trash
.execute_empty
);
268 execute_empty: function(rows
) {
269 $.each(rows
, function(i
,doc
){
270 doc
= new $.ajatus
.document(doc
);
271 $.ajatus
.document
.actions
.execute("delete_final", doc
);
275 $.ajatus
.views
.system
.edit
= {
280 dynamic_history
: true
282 $.extend($.ajatus
.views
.system
.edit
, {
283 render: function(type
, doc
)
285 var content_type
= type
;
287 var doc
= new $.ajatus
.document
.loader(doc
._id
, doc
._rev
);
290 content_type
= $.ajatus
.preferences
.client
.content_types
[doc
.value
._type
];
293 if (typeof(type
) == 'string') {
294 content_type
= $.ajatus
.preferences
.client
.content_types
[type
];
297 $.ajatus
.layout
.body
.set_class('edit '+content_type
.name
);
299 var view_hash
= '#edit.'+doc
.value
._type
+'.'+doc
._id
;
300 $.ajatus
.history
.update(view_hash
);
301 $.ajatus
.views
.on_change(view_hash
);
303 var renderer
= new $.ajatus
.renderer
.form(content_type
, doc
);
304 $.ajatus
.application_content_area
.html(renderer
.form
);
305 $.ajatus
.forms
.register
.normal(renderer
.form
);
307 history_register: function() {
309 $.ajatus
.history
.add_dynamic_map(['edit'], mods
, '$.ajatus.views.system.edit.history_render');
311 history_render: function() {
312 var type
= arguments
[0];
313 var id
= arguments
[1];
315 var doc
= new $.ajatus
.document
.loader(id
);
316 $.ajatus
.views
.system
.edit
.render(type
, doc
);
320 $.ajatus
.views
.system
.create
= {
325 dynamic_history
: true
327 $.extend($.ajatus
.views
.system
.create
, {
328 render: function(type
)
330 var content_type
= type
;
332 if (typeof(type
) == 'string') {
333 content_type
= $.ajatus
.preferences
.client
.content_types
[type
];
336 $.ajatus
.layout
.body
.set_class('create '+content_type
.name
);
338 var view_hash
= '#create.'+content_type
.name
;
340 if ( $.ajatus
.history
341 && $.ajatus
.history
.enabled
)
343 $.ajatus
.history
.update(view_hash
);
346 $.ajatus
.views
.on_change(view_hash
);
348 if ( content_type
.custom_renderer
349 && typeof(content_type
.custom_renderer
['create']) == 'function')
351 var renderer
= content_type
.custom_renderer
.create();
353 var renderer
= new $.ajatus
.renderer
.form(content_type
);
354 $.ajatus
.application_content_area
.html(renderer
.form
);
355 $.ajatus
.forms
.register
.normal(renderer
.form
);
358 history_register: function() {
360 $.ajatus
.history
.add_dynamic_map(['create'], mods
, '$.ajatus.views.system.create.history_render');
362 history_render: function() {
363 var type
= arguments
[0];
365 $.ajatus
.views
.system
.create
.render(type
);
369 $.ajatus
.views
.system
.item
= {
374 dynamic_history
: true
376 $.extend($.ajatus
.views
.system
.item
, {
377 render: function(doc
)
379 if (typeof doc
._rev
!= 'undefined') {
380 var doc
= new $.ajatus
.document
.loader(doc
._id
, doc
._rev
);
382 var doc
= new $.ajatus
.document
.loader(doc
._id
);
385 var content_type
= $.ajatus
.preferences
.client
.content_types
[doc
.value
._type
];
387 $.ajatus
.layout
.body
.set_class('view '+doc
.value
._type
);
389 var view_hash
= '#view.'+doc
.value
._type
+'.'+doc
._id
;
390 $.ajatus
.history
.update(view_hash
);
391 $.ajatus
.views
.on_change(view_hash
);
393 $.ajatus
.toolbar
.add_item($.ajatus
.i10n
.get('Edit'), 'images/icons/edit.png', function(){
394 $.ajatus
.views
.system
.edit
.render(content_type
, doc
);
397 $.ajatus
.toolbar
.show();
399 var rel_docs
= $.ajatus
.tags
.related(doc
.value
.tags
.val
, [doc
._id
]);
401 var sub_block
= new $.ajatus
.renderer
.blocks
.sub('related_objects');
402 sub_block
.set_title($.ajatus
.i10n
.get('Related Objects'));
403 $.ajatus
.views
.on_change_actions
.add('$.ajatus.renderer.blocks.clear_sub("'+sub_block
.id
+'");');
405 var tinylist
= new $.ajatus
.renderer
.tinylist(sub_block
.element
);
406 tinylist
.render_items(rel_docs
);
408 if ( content_type
.custom_renderer
409 && typeof(content_type
.custom_renderer
['item']) == 'function')
411 var renderer
= content_type
.custom_renderer
.item();
413 var renderer
= new $.ajatus
.renderer
.item(doc
);
416 history_register: function() {
418 $.ajatus
.history
.add_dynamic_map(['view'], mods
, '$.ajatus.views.system.item.history_render');
420 history_render: function() {
421 var type
= arguments
[0];
422 var id
= arguments
[1];
430 setTimeout("$.ajatus.views.system.item.render("+data
.toSource()+");", 200);