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', 'tags', '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']
56 || type.on_frontpage == false) {
60 var on_success = function(data) {
61 if (data.total_rows == 0) {
65 var pool_settings = $.ajatus.views.system.frontpage.pool_settings;
66 if (data.total_rows >= $.ajatus.views.system.frontpage.options.max_items_before_pool) {
67 pool_settings.enabled = true;
70 var renderer = new $.ajatus.renderer.list(
71 $.ajatus.application_content_area,
74 id: type.name + '_list_holder',
80 $.each(data.rows, function(i,doc){
81 var doc = new $.ajatus.document(doc);
82 renderer.render_item(doc, i);
88 if ( $.ajatus.tags.active != ''
89 && typeof type.views['list_at'] != 'undefined')
91 $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, type.views['list_at'], {
92 count: $.ajatus.views.system.frontpage.options.group_item_count,
96 $.jqCouch.connection('view', on_success).get($.ajatus.preferences.client.content_database, type.name+'/list', {
97 count: $.ajatus.views.system.frontpage.options.group_item_count,
104 $.ajatus.views.system.tags = {
109 application_tab: true,
110 history_support: true,
114 on_click: function(e) {
115 $.ajatus.tabs.on_click(e);
116 $.ajatus.views.system.tags.render();
121 $.ajatus.application_content_area.html('');
122 $.ajatus.layout.body.set_class('tags');
123 $.ajatus.views.on_change("#view.tags");
125 $.ajatus.toolbar.init();
126 $.ajatus.toolbar.show();
128 $.ajatus.views.system.tags.get_items();
131 render_results: function(tags, items) {
132 var type = $.ajatus.preferences.client.content_types['tag'];
134 var renderer = new $.ajatus.renderer.list(
135 $.ajatus.application_content_area,
138 id: 'tags_list_holder',
140 'title', 'context', 'value', 'created', 'creator', 'used'
149 $.each(tags, function(i,doc){
150 var doc = new $.ajatus.document(doc);
151 doc.value['used'] = {
152 val: (typeof items[doc._id] != 'undefined' ? items[doc._id].length : 0)
154 renderer.render_item(doc, x);
158 renderer.enable_sorting();
160 $.ajatus.views.system.tags.get_duplicates(items);
163 render_duplicates: function(rows, items) {
164 var type = $.ajatus.preferences.client.content_types['tag'];
166 var renderer = new $.ajatus.renderer.list(
167 $.ajatus.application_content_area,
170 id: 'tag_dups_list_holder',
171 title: $.ajatus.i10n.get("Duplicates"),
173 'title', 'context', 'value', 'created', 'creator', 'used'
183 var duplicate_ids = [];
190 $.each (rows, function(i,doc){
191 var doc = new $.ajatus.document(doc);
192 // console.log(doc._id);
193 // console.log(doc.key);
194 // console.log(doc.value.context.val);
195 // console.log(doc.value.value.val);
197 // if (typeof tags[doc.key] == 'undefined') {
199 // contexts: [doc.value.context.val],
200 // values: [doc.value.value.val],
204 // tags[doc.key].contexts.push(doc.value.context.val);
205 // tags[doc.key].values.push(doc.value.value.val);
206 // tags[doc.key].docs.push(doc);
209 var tag_hash = $.ajatus.utils.md5.encode(doc.value.context.val + ':' + doc.key + '=' + doc.value.value.val);
210 hashes.push(tag_hash);
216 var match_found = false;
218 var chashes = hashes;
219 for (var i=0; i<hashes.length;i++) {
222 var matched = $.inArray(key, chashes);
224 duplicate_ids.push(ids[i]);
230 // console.log("match_found: "+match_found);
231 // console.log("match_count: "+match_count);
232 // console.log("duplicate_ids: ");
233 // console.log(duplicate_ids);
236 $.each(tags, function(i,d){
237 if ($.inArray(i, duplicate_ids) != -1) {
239 val: (typeof items[d._id] != 'undefined' ? items[d._id].length : 0)
241 renderer.render_item(d, x);
246 renderer.enable_sorting();
249 get_items: function(on_success) {
250 var conn_on_success = function(data) {
251 if (data.total_rows == 0) {
252 var key = $.ajatus.i10n.plural($.ajatus.i10n.get('tag'));
253 var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
260 $.each(data.rows, function(i,n){
264 if (typeof items[n.key[0]] == 'undefined') {
265 items[n.key[0]] = [];
267 items[n.key[0]].push(n);
271 if (typeof on_success == 'undefined') {
272 on_success = $.ajatus.views.system.tags.render_results;
275 on_success(tags, items);
280 $.jqCouch.connection('view', conn_on_success).get($.ajatus.preferences.client.content_database, 'tag/list_with_docs', {
281 // startkey: ["ED862081B2FBF46FB0CDBBE848ED6856", 0],
282 // endkey: ["ED862081B2FBF46FB0CDBBE848ED6856", 1]
286 get_duplicates: function(items) {
287 var conn_on_success = function(data) {
288 if (data.total_rows == 0) {
289 var key = $.ajatus.i10n.plural($.ajatus.i10n.get('dublicate'));
290 var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
294 if (typeof on_success == 'undefined') {
295 on_success = $.ajatus.views.system.tags.render_duplicates;
298 on_success(data.rows, items);
303 $.jqCouch.connection('view', conn_on_success).get($.ajatus.preferences.client.content_database, 'tag/list_by_title', {
308 $.ajatus.views.system.trash = {
314 application_tab: true,
315 history_support: true,
319 on_click: function(e)
321 $.ajatus.tabs.on_click(e);
322 $.ajatus.views.system.trash.render();
326 get_items: function(on_success) {
327 var trash_view = 'if (doc.value.metadata.deleted.val == true) {';
328 trash_view += 'map( doc.value.metadata.revised, {"_type": doc.value._type,';
329 trash_view += '"title": doc.value.title,';
331 $.each($.ajatus.preferences.client.content_types, function(key,type){
332 if (type['title_item']) {
333 $.each(type['title_item'], function(i,part){
334 if (type.schema[part]) {
335 trash_view += '"'+part+'": doc.value.'+part+',';
341 trash_view += '"revised": doc.value.metadata.revised,"by": doc.value.metadata.revisor,"tags": doc.value.tags})}';
343 var conn_on_success = function(data) {
344 if (data.total_rows == 0) {
345 var key = $.ajatus.i10n.plural($.ajatus.i10n.get('deleted item'));
346 var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
350 if (typeof on_success == 'undefined') {
351 on_success = $.ajatus.views.system.trash.render_results;
354 on_success(data.rows);
358 $.jqCouch.connection('view', conn_on_success).temp($.ajatus.preferences.client.content_database, "$.ajatus.views.generate('"+trash_view+"')", {
359 count: $.ajatus.views.system.trash.options.group_item_count,
366 $.ajatus.application_content_area.html('');
367 $.ajatus.layout.body.set_class('trash');
368 $.ajatus.views.on_change("#view.trash");
370 $.ajatus.toolbar.init();
371 $.ajatus.toolbar.show();
373 $.ajatus.views.system.trash.get_items();
376 render_results: function(rows)
378 var result_count = {};
379 var show_empty_trash = false;
381 $.each($.ajatus.preferences.client.content_types, function(key,type){
382 if (!type.views['list']) {
386 result_count[type.name] = 0;
388 $.each(rows, function(i,doc){
389 if (doc.value._type == type.name) {
390 result_count[type.name] += 1;
394 if (result_count[type.name] == 0) {
398 show_empty_trash = true;
400 var renderer = new $.ajatus.renderer.list(
401 $.ajatus.application_content_area,
404 id: type.name + '_list_holder',
408 'title', 'revised', 'by'
418 $.each(rows, function(i,doc){
419 if (doc.value._type == type.name) {
420 var doc = new $.ajatus.document(doc);
422 renderer.set_actions(
426 title: $.ajatus.i10n.get('View'),
428 click_action: '$.ajatus.views.system.item.render(doc);'
432 title: $.ajatus.i10n.get('Undelete'),
434 click_action: '$.ajatus.document.actions.execute("undelete", doc);'
437 name: 'delete_final',
438 title: $.ajatus.i10n.get('Delete'),
440 click_action: '$.ajatus.document.actions.execute("delete_final", doc);'
444 renderer.render_item(doc, i);
448 renderer.enable_sorting();
451 if (show_empty_trash) {
452 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Empty trash'), 'trash.png', function(){
453 $.ajatus.views.system.trash.empty();
458 var answer = confirm($.ajatus.i10n.get("Deleting all items in trash. Are you sure?"));
460 $.ajatus.views.system.trash.get_items($.ajatus.views.system.trash.execute_empty);
463 execute_empty: function(rows) {
464 $.each(rows, function(i,doc){
465 doc = new $.ajatus.document(doc);
466 $.ajatus.document.actions.execute("delete_final", doc);
470 $.ajatus.views.system.edit = {
475 dynamic_history: true
477 $.extend($.ajatus.views.system.edit, {
478 render: function(type, doc, use_given_doc)
480 if (typeof use_given_doc == 'undefined') {
481 use_given_doc = false;
484 var content_type = type;
486 if (! use_given_doc) {
487 var doc = new $.ajatus.document.loader(doc._id, doc._rev);
491 content_type = $.ajatus.preferences.client.content_types[doc.value._type];
494 if (typeof(type) == 'string') {
495 content_type = $.ajatus.preferences.client.content_types[type];
498 $.ajatus.layout.body.set_class('edit '+content_type.name);
500 var view_hash = '#edit.'+doc.value._type+'.'+doc._id+'.'+doc._rev;
501 $.ajatus.history.update(view_hash);
502 $.ajatus.views.on_change(view_hash);
504 var latest_rev = $.ajatus.document.revisions.get(doc).active.rev;
506 if ( content_type.custom_renderer
507 && typeof(content_type.custom_renderer['edit']) == 'function')
509 var renderer = content_type.custom_renderer.edit(doc, latest_rev);
511 var renderer = new $.ajatus.renderer.form(content_type, doc, false, latest_rev);
512 $.ajatus.application_content_area.html(renderer.form);
513 $.ajatus.forms.register.normal(renderer.form);
516 history_register: function() {
517 $.ajatus.history.add_dynamic_map(['edit'], 3, '$.ajatus.views.system.edit.history_render');
519 history_render: function() {
520 var type = arguments[0];
521 var id = arguments[1];
524 if ( typeof arguments[2] != 'undefined'
525 && arguments[2] != '')
530 var doc = new $.ajatus.document.loader(id, rev);
531 $.ajatus.views.system.edit.render(type, doc);
535 $.ajatus.views.system.create = {
540 dynamic_history: true
542 $.extend($.ajatus.views.system.create, {
543 render: function(type, doc)
545 var content_type = type;
547 if (typeof(type) == 'string') {
548 content_type = $.ajatus.preferences.client.content_types[type];
551 $.ajatus.layout.body.set_class('create '+content_type.name);
553 var view_hash = '#create.'+content_type.name;
555 $.ajatus.history.update(view_hash);
557 $.ajatus.views.on_change(view_hash);
559 if ( content_type.custom_renderer
560 && typeof(content_type.custom_renderer['create']) == 'function')
562 var renderer = content_type.custom_renderer.create();
564 var renderer = new $.ajatus.renderer.form(content_type, doc, true);
565 $.ajatus.application_content_area.html(renderer.form);
566 $.ajatus.forms.register.normal(renderer.form);
569 history_register: function() {
570 $.ajatus.history.add_dynamic_map(['create'], 2, '$.ajatus.views.system.create.history_render');
572 history_render: function() {
573 var type = arguments[0];
575 $.ajatus.views.system.create.render(type);
579 $.ajatus.views.system.item = {
584 dynamic_history: true
586 $.extend($.ajatus.views.system.item, {
587 render: function(doc)
589 if (typeof doc._rev != 'undefined') {
590 var doc = new $.ajatus.document.loader(doc._id, doc._rev);
592 var doc = new $.ajatus.document.loader(doc._id);
595 var content_type = $.ajatus.preferences.client.content_types[doc.value._type];
597 $.ajatus.layout.body.set_class('view '+doc.value._type);
599 var view_hash = '#view.'+doc.value._type+'.'+doc._id+'.'+doc._rev;
600 $.ajatus.history.update(view_hash);
601 $.ajatus.views.on_change(view_hash);
603 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Edit'), {
606 $.ajatus.views.system.edit.render(content_type, doc);
611 if (doc.value.metadata.archived.val) {
612 var unarch_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Restore'), 'undo.png', function(item, doc){
614 $.ajatus.document.actions.execute("unarchive", doc);
616 $.ajatus.toolbar.hide_item(item.id);
617 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive'), 'archive.png', function(item, doc, unarch_item_id){
619 $.ajatus.document.actions.execute("archive", doc);
621 $.ajatus.toolbar.hide_item(item.id);
622 $.ajatus.toolbar.show_item(unarch_item_id);
623 }, [doc, unarch_id], unarch_id);
626 var arch_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Archive'), 'archive.png', function(item, doc){
628 $.ajatus.document.actions.execute("archive", doc);
630 $.ajatus.toolbar.hide_item(item.id);
631 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Restore'), 'undo.png', function(item, doc, arch_item_id){
633 $.ajatus.document.actions.execute("unarchive", doc);
635 $.ajatus.toolbar.hide_item(item.id);
636 $.ajatus.toolbar.show_item(arch_item_id);
637 }, [doc, arch_id], arch_id);
641 if (doc.value.metadata.deleted.val) {
642 var undel_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Undelete'), 'undo.png', function(item, doc){
644 $.ajatus.document.actions.execute("undelete", doc);
647 $.ajatus.toolbar.hide_item(item.id);
648 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Delete'), 'trash.png', function(item, doc, undel_item_id){
650 $.ajatus.document.actions.execute("delete", doc);
652 $.ajatus.toolbar.hide_item(item.id);
653 $.ajatus.toolbar.show_item(undel_item_id);
654 }, [d, undel_id], undel_id);
657 var del_id = $.ajatus.toolbar.add_item($.ajatus.i10n.get('Delete'), 'trash.png', function(item, doc){
659 $.ajatus.document.actions.execute("delete", doc);
662 $.ajatus.toolbar.hide_item(item.id);
663 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Undelete'), 'undo.png', function(item, doc, del_item_id){
665 $.ajatus.document.actions.execute("undelete", doc);
667 $.ajatus.toolbar.hide_item(item.id);
668 $.ajatus.toolbar.show_item(del_item_id);
669 }, [d, del_id], del_id);
673 $.ajatus.toolbar.show();
675 var metadata_sb = new $.ajatus.renderer.blocks.sub('metadata_for_'+doc._id);
676 metadata_sb.set_title($.ajatus.i10n.get('Metadata'));
677 $.ajatus.views.on_change_actions.add('$.ajatus.renderer.blocks.clear_sub("'+metadata_sb.id+'");');
679 var md_renderer = new $.ajatus.renderer.metadata(doc, metadata_sb.element);
681 if (typeof doc.value.tags != 'undefined') {
682 var rel_docs = $.ajatus.tags.related(doc.value.tags.val, [doc._id]);
684 var related_sb = new $.ajatus.renderer.blocks.sub('related_objects');
685 related_sb.set_title($.ajatus.i10n.get('Related Objects'));
686 $.ajatus.views.on_change_actions.add('$.ajatus.renderer.blocks.clear_sub("'+related_sb.id+'");');
688 var tinylist = new $.ajatus.renderer.tinylist(related_sb.element);
689 tinylist.render_items(rel_docs);
692 if ( content_type.custom_renderer
693 && typeof(content_type.custom_renderer['item']) == 'function')
695 var renderer = content_type.custom_renderer.item();
697 var renderer = new $.ajatus.renderer.item(doc);
700 history_register: function() {
701 $.ajatus.history.add_dynamic_map(['view'], 3, '$.ajatus.views.system.item.history_render');
703 history_render: function() {
704 var type = arguments[0];
705 var id = arguments[1];
708 if ( typeof arguments[2] != 'undefined'
709 && arguments[2] != '')
731 if (typeof data['toSource'] != 'undefined') {
732 data = data.toSource();
735 setTimeout("$.ajatus.views.system.item.render("+data+");", 200);