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
.widgets
= $.ajatus
.widgets
|| {};
18 $.ajatus
.widgets
.core
= typeof($.ajatus
.widgets
.core
) == 'undefined' ? {} : $.ajatus
.widgets
.core
;
20 $.ajatus
.widgets
.core
.tags
= {
29 get_create_tpl: function(name
, default_value
)
32 if (default_value
!= '') {
33 this.assign_tags
[idx
] = default_value
;
36 if ($.ajatus
.tags
.active
!= '') {
37 this.assign_tags
[idx
] = $.ajatus
.tags
.active
;
41 'input', { type
: 'hidden', name
: 'widget['+name
+':name]', value
: this.name
}, '',
42 'input', { type
: 'hidden', name
: 'widget['+name
+':config]', value
: $.ajatus
.converter
.toJSON(this.settings
) }, '',
43 'input', { type
: 'text', name
: name
, value
: '' }, ''
46 get_edit_tpl: function(name
, data
)
48 data
.val
= this.value_on_edit(data
.val
);
50 'input', { type
: 'hidden', name
: 'widget['+name
+':name]', value
: this.name
}, '',
51 'input', { type
: 'hidden', name
: 'widget['+name
+':config]', value
: $.ajatus
.converter
.toJSON(this.settings
) }, '',
52 'input', { type
: 'hidden', name
: 'widget['+name
+':prev_val]', value
: $.ajatus
.converter
.toJSON(data
.val
) }, '',
53 'input', { type
: 'text', name
: name
, value
: '' }, ''
56 get_view_tpl: function(name
, data
)
58 data
.val
= this.value_on_view(data
.val
);
61 'div', { className
: data
._id
+'_element_'+name
+'_value' }, [
62 'ul', { className
: 'tag_list' }, data
.val
,
63 'br', { className
: 'clear_fix' }, ''
67 set_config: function(config
)
69 this.settings
= $.extend(this.settings
, config
);
71 value_on_save: function(value
, prev_val
)
75 var inputs
= $(':input[name*=__tags_widget_selections]');
76 $.each(inputs
, function(i
,n
){
84 tag_arr
= $.grep(tag_arr
, function(n
,i
){
87 tag_arr
= $.unique(tag_arr
);
89 this._update_system_tags(tag_arr
, prev_val
);
92 $.each(tag_arr
, function(i
,t
){
95 var tag_parts
= t
.split(':');
96 context_key
= tag_parts
[0];
104 value_on_edit: function(value
)
107 $.each(value
, function(i
,t
){
108 self
.assign_tags
[self
.assign_tags
.length
|| i
] = t
;
113 // Type = list / item
114 value_on_view: function(value
, type
)
116 if (typeof type
== 'undefined') {
123 if (value
.length
> 0) {
124 $.each(value
, function(i
,v
){
125 var tag
= $.ajatus
.tags
.get(v
);
127 if ( typeof(tag
.value
.title
.widget
.config
['context']) != 'undefined'
128 && tag
.value
.title
.widget
.config
['context'] != '')
130 tag_title
+= tag
.value
.title
.widget
.config
.context
+ ':';
132 tag_title
+= tag
.value
.title
.val
;
134 if (type
== 'plain') {
135 tags_str
+= tag_title
+ _self
.settings
.separator
;
137 var tpl
= _self
.style_tag_on_view(tag_title
, tag
.value
.title
.widget
.config
.color
, type
);
138 $.each(tpl
, function(tidx
, tp
){
145 if (type
== 'plain') {
151 style_tag_on_view: function(title
, color
, type
) {
152 if ( typeof color
== 'undefined'
155 var color
= $.ajatus
.widgets
.core
.tag
.settings
.color
;
158 if (type
== 'list') {
160 'span', { className
: 'tag' }, title
165 'li', { className
: 'tag', style
: 'background-color: #'+color
+';' }, title
170 $.ajatus
.events
.lock_pool
.increase();
172 $.ajatus
.layout
.styles
.load($.ajatus
.preferences
.client
.theme_url
+ 'css/widgets/tags.css');
173 $.ajatus
.layout
.styles
.load($.ajatus
.preferences
.client
.theme_url
+ 'css/widgets/includes/tags.css');
175 var tags_url
= $.ajatus
.preferences
.client
.application_url
+ 'js/widgets/includes/tags.js';
176 $.ajatus
.utils
.load_script(tags_url
, "$.ajatus.events.lock_pool.decrease", []);
178 init: function(holder
, form_mode
)
181 this.init_form_mode(holder
);
183 this.init_view_mode(holder
);
186 init_form_mode: function(holder
)
188 var element
= $('input[@type=text]', holder
);
191 element
.tags(this.settings
.widget
);
193 $.each(this.assign_tags
, function(i
,t
) {
194 if (typeof t
== "object") {
197 var tag
= $.ajatus
.tags
.get(t
);
199 element
.tags_add_item(tag
, true);
204 init_view_mode: function(holder
)
207 _update_system_tags: function(new_tags
, prev_tags
)
211 var on_success = function(data
) {
213 var msg
= $.ajatus
.elements
.messages
.create(
215 'Created new tag "'+data
.id
+'".'
218 $.ajatus
.debug("saved tag: "+data
.id
);
223 var dc
= $.jqCouch
.connection('doc', on_success
);
225 $.each(new_tags
, function(i
,t
){
226 var context_key
= '';
228 var tag_parts
= t
.split(':');
229 context_key
= tag_parts
[0];
233 $.ajatus
.tags
.create(t
, context_key
, dc
);
236 $.ajatus
.views
.on_change_actions
.add('$.ajatus.tags.refresh_cache();');