Updated Ajatus to use the new jqCouch library
[ajatus.git] / js / content_types / note.js
blob1f392e67b14986e89f2a1b4db85bd0c3c64240fd
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.content_type = $.ajatus.content_type || {};
18     
19     $.ajatus.content_type.note = function()
20     {
21         var self = this;
22         this.name = 'note';
23         this.title = 'Note';
24         this.in_tabs = true;
25         this.history_support = true;
26         this.enable_additionals = true;
27         
28         this.max_items_before_pool = $.ajatus.renderer_defaults.max_items_before_pool;
29         this.pool_settings = {
30             enabled: false
31         };
32         
33         this.schema = {
34             title: {
35                 label: 'Title',
36                 widget: {
37                     name: 'text',
38                     config: {}
39                 },
40                 def_val: '',
41                 required: true
42             },
43             description: {
44                 label: 'Description',
45                 widget: {
46                     name: 'wiki',
47                     config: {}
48                 },
49                 def_val: ''
50             },
51             tags: {
52                 label: 'Tags',
53                 widget: {
54                     name: 'tags',
55                     config: {}
56                 },
57                 def_val: []
58             }
59         };
60         this.original_schema = $.ajatus.utils.object.clone(this.schema);
61         
62         this.views = {
63             all: function(doc) {
64                 if (doc.value._type == 'note')
65                 {
66                     return doc.value;
67                 }
68             },
69             list: function(doc) {
70                 if (   doc.value._type == 'note'
71                     && doc.value.metadata.archived.val == ''
72                     && doc.value.metadata.deleted.val != 1)
73                 {
74                     map(doc.value.metadata.created, {
75                         "_type": doc.value._type,
76                         "title": doc.value.title,
77                         "Created": doc.value.metadata.created,
78                         "tags": doc.value.tags
79                     });
80                 }
81             }
82         };
83         
84         this.list_headers = [
85             'title', 'Created'
86         ];
87         
88         this.tab = {
89             on_click: function(e)
90             {   
91                 $.ajatus.tabs.on_click(e);
92                 this.render();
93             }
94         };
95         
96         this.render = function() {
97             $.ajatus.active_type = self;
98             $.ajatus.layout.body.set_class('list '+self.name);
99             $.ajatus.application_content_area.html('');
100             
101             var on_success = function(data) {
102                 if (data.total_rows == 0) {
103                     var key = $.ajatus.i10n.plural($.ajatus.i10n.get(self.title));
104                     var msg = $.ajatus.elements.messages.static($.ajatus.i10n.get('Empty results'), $.ajatus.i10n.get('No %s found', [key]));
105                     return;
106                 }
107                 
108                 var pool_settings = self.pool_settings;
109                 if (data.total_rows >= self.max_items_before_pool)
110                 {
111                     pool_settings.enabled = true;
112                 }
113                 
114                 var renderer = new $.ajatus.renderer.list(
115                     $.ajatus.application_content_area,
116                     self,
117                     {
118                         id: self.name + '_list_holder',
119                         pool: pool_settings
120                     }
121                 );
122                 
123                 $.each(data.rows, function(i,doc){
124                     var doc = new $.ajatus.document(doc);
125                     if ($.ajatus.tags.active != '') {
126                         if ($.ajatus.utils.array.has_match($.ajatus.tags.active, doc.value.tags.val)) {
127                             renderer.render_item(doc, i);                            
128                         }
129                     } else {
130                         renderer.render_item(doc, i);
131                     }
132                 });
133                 
134                 renderer.enable_sorting();
135                 
136                 return data;
137             };
138             
139             $.jqCouch.connection('view', on_success).temp($.ajatus.preferences.client.content_database, self.views.list, {
140                 descending: true
141             });
142         };
143     };
144     
145     $.ajatus.preferences.client_defaults.content_types['note'] = new $.ajatus.content_type.note();
147 })(jQuery);