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
|| {};
19 * Holds all tag methods
20 * @accessor {String} cache Holds all cached tag names
21 * @accessor {String} active Currently active tag name
29 $.extend($.ajatus
.tags
, {
32 * Initializes the tags
33 * @see ajatus.core.init
34 * @see ajatus.tags.load
35 * @see ajatus.tags.update_tag_list
38 $.ajatus
.events
.lock_pool
.increase();
40 $.ajatus
.events
.lock_pool
.decrease();
41 $.ajatus
.tags
.update_app_tag_list();
44 $.ajatus
.tags
.used_contexts
= [];
45 $.ajatus
.tags
.cache
= [];
47 var on_success = function(data
) {
48 if (data
.total_rows
<= 0) {
52 $.each(data
.rows
, function(i
,n
){
53 if ( typeof(n
.value
.title
.widget
.config
['context']) != 'undefined'
54 && n
.value
.title
.widget
.config
['context'] != '')
56 var found
= $.inArray(n
.value
.title
.widget
.config
['context'], $.ajatus
.tags
.used_contexts
);
58 $.ajatus
.tags
.used_contexts
.push(n
.value
.title
.widget
.config
['context']);
61 $.ajatus
.tags
.cache
.push(n
);
67 $.jqCouch
.connection('view', on_success
).temp($.ajatus
.preferences
.client
.tags_database
, function(doc
){
68 if ( doc
.value
.metadata
.archived
.val
== ""
69 && doc
.value
.metadata
.deleted
.val
!= 1)
71 map(doc
._id
, doc
.value
);
75 refresh_cache: function() {
77 $.ajatus
.tags
.update_cache();
79 update_cache: function() {
80 $.ajatus
.tags
.update_app_tag_list();
81 if ($.ajatus
.tags
.active
!= '') {
82 $.ajatus
.tags
.set_active($.ajatus
.tags
.active
);
85 set_active: function(tag
) {
86 var select
= $('#application-tags select');
87 $('option', select
).each(function(i
,o
){
89 o
.selected
= 'selected';
92 $.ajatus
.tags
.active
= tag
;
94 update_app_tag_list: function() {
95 var select
= $('#application-tags select');
99 var def_opt_grp
= $('<optgroup label="' + $.ajatus
.i10n
.get('No context') + '" />');
100 def_opt_grp
.appendTo(select
);
102 $.each($.ajatus
.tags
.used_contexts
, function(i
){
103 var opt_grp
= $('<optgroup label="' + this + '" />');
104 opt_grp
.prependTo(select
);
105 opt_groups
[this] = opt_grp
;
108 $.each($.ajatus
.tags
.cache
, function(i
){
109 var opt
= $('<option>'+this.value
.title
.val
+'</option>').attr({
113 if ( typeof(this.value
.title
.widget
.config
['context']) != 'undefined'
114 && this.value
.title
.widget
.config
['context'] != '')
116 var context
= this.value
.title
.widget
.config
.context
;
117 opt
.appendTo(opt_groups
[context
]);
119 opt
.appendTo(def_opt_grp
);
123 var def
= $('<option />').attr({
126 }).html($.ajatus
.i10n
.get('Global Tag') + ' ');
127 def
.prependTo(select
);
128 select
.bind('change', function(e
){
129 $.ajatus
.tags
.set_active(e
.currentTarget
.value
);
132 search: function(query
, context
, max_res
) {
133 // console.log("Search tags with: "+query);
136 var context_key
= false;
138 if (typeof max_res
!= 'number') {
142 query
= $.ajatus
.tags
._prepare_query(query
);
144 var on_success = function(data
) {
145 if (data
.total_rows
== 0) {
149 $.each(data
.rows
, function(i
,n
){
150 var doc
= new $.ajatus
.document(n
);
157 var rel_view
= 'if (doc.value.title.val.match(/\\\\b'+query
+'(.*?)\\\\b/) ';
158 if ( typeof context
!= 'undefined'
161 rel_view
+= '&& typeof(doc.value.title.widget.config["context"]) != "undefined" ';
162 rel_view
+= '&& doc.value.title.widget.config.context == "+context+" ';
164 rel_view
+= '&& doc.value.metadata.archived.val == "" && doc.value.metadata.deleted.val != 1){';
165 rel_view
+= 'map( doc._id, doc.value );}';
167 $.jqCouch
.connection('view', on_success
).temp($.ajatus
.preferences
.client
.tags_database
,
168 "$.ajatus.views.generate('"+rel_view
+"')", {
175 search_cache: function(query
, context
, max_res
) {
176 // console.log("Search tags cache with: "+query);
179 query
= $.ajatus
.tags
._prepare_query(query
);
180 var re
= new RegExp("\\b"+query
+"(.*?)\\b");
182 $.each($.ajatus
.tags
.cache
, function(i
, tag
){
183 if (tag
.id
.toString().match(re
)) {
184 if ( typeof context
!= 'undefined'
187 if ( typeof(tag
.value
.title
.widget
.config
['context']) != 'undefined'
188 && tag
.value
.title
.widget
.config
['context'] == context
)
200 _prepare_query: function(query
) {
201 query
= query
.replace(/[\(|\)|\.|\?|\;|\/]/, '');
202 query
= query
.replace('\\', '');
203 query
= query
.replace('*', '(.*?)');
207 get: function(tag
, rev
, skip_cache
, callback
) {
209 if (typeof skip_cache
== 'undefined') {
210 var skip_cache
= false;
213 if ( typeof rev
== 'undefined'
216 $.each($.ajatus
.tags
.cache
, function(i
,t
){
227 var on_success = function(data
) {
228 var doc
= new $.ajatus
.document(data
);
231 if (typeof callback
== 'function') {
232 var fn
= eval('('+callback
+')');
233 fn
.apply(fn
, [result
]);
240 if (typeof rev
!= 'undefined') {
244 $.jqCouch
.connection('doc', on_success
).get($.ajatus
.preferences
.client
.tags_database
+ '/' + tag
, args
);
248 exists: function(tag
) {
250 $.each($.ajatus
.tags
.cache
, function(i
,t
){
253 var found
= $.inArray(tag
, tag_ids
);
259 create: function(name
, context
, dc
) {
261 if ($.ajatus
.tags
.exists(name
)) {
265 if (typeof dc
== 'undefined') {
266 var on_success = function(data
) {
268 var msg
= $.ajatus
.elements
.messages
.create(
270 'Created new tag "'+data
.id
+'".'
273 $.ajatus
.debug("saved tag: "+data
.id
);
275 var tag
= $.ajatus
.tags
.get(data
.id
, data
.rev
, true);
276 $.ajatus
.tags
.cache
.push(tag
);
282 var dc
= $.jqCouch
.connection('doc', on_success
);
283 $.ajatus
.views
.on_change_actions
.add('$.ajatus.tags.update_cache();');
296 context
: (context
|| '')
302 tag
= new $.ajatus
.document(tag
);
304 var now
= $.ajatus
.formatter
.date
.js_to_iso8601(new Date());
307 creator
: $.ajatus
.preferences
.local
.user
.email
,
309 revisor
: $.ajatus
.preferences
.local
.user
.email
311 tag
= $.ajatus
.document
.modify_metadata(tag
, new_metadata
);
312 dc
.save($.ajatus
.preferences
.client
.tags_database
, tag
);
316 remove: function(tag
, dc
) {
317 if (typeof dc
== 'undefined') {
318 var on_success = function(data
) {
319 var msg
= $.ajatus
.elements
.messages
.create(
320 $.ajatus
.i10n
.get('Object deleted'),
321 $.ajatus
.i10n
.get('Tag %s removed from Ajatus.', [tag
])
324 var dc
= $.jqCouch
.connection('doc', on_success
);
327 dc
.del($.ajatus
.preferences
.client
.tags_database
+ '/' + tag
);
329 related: function(tags
, skip_docs
) {
332 if (typeof skip_docs
!= 'object') {
336 var on_success = function(data
) {
337 if (data
.total_rows
== 0) {
341 $.each(data
.rows
, function(i
,n
){
342 if ($.ajatus
.utils
.array
.has_match(tags
, n
.value
.tags
.val
)) {
343 if (skip_docs
.length
> 0) {
344 if ($.inArray(n
.id
, skip_docs
) == -1) {
345 var doc
= new $.ajatus
.document(n
);
349 var doc
= new $.ajatus
.document(n
);
358 var rel_view
= 'if (typeof(doc.value.tags) != "undefined" && doc.value.tags.val.length > 0 ';
359 rel_view
+= '&& doc.value.metadata.archived.val == "" && doc.value.metadata.deleted.val != 1){';
360 rel_view
+= 'map( doc.value._type, {"_id": doc._id, "_rev": doc._rev, "_type": doc.value._type,';
361 rel_view
+= '"title": doc.value.title,';
363 $.each($.ajatus
.preferences
.client
.content_types
, function(key
,type
){
364 if (type
['title_item']) {
365 $.each(type
['title_item'], function(i
,part
){
366 if (type
.schema
[part
]) {
367 rel_view
+= '"'+part
+'": doc.value.'+part
+',';
373 rel_view
+= '"tags": doc.value.tags })}';
375 $.jqCouch
.connection('view', on_success
).temp($.ajatus
.preferences
.client
.content_database
, "$.ajatus.views.generate('"+rel_view
+"')");