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)
31 if (this.assign_tags == null) {
32 this.assign_tags = [];
36 if (typeof default_value == 'object') {
37 $.each(default_value, function(i,dv){
38 if (typeof dv.id != 'undefined') {
39 var tag = $.ajatus.tags.get(dv.id);
41 this.assign_tags[idx] = tag;
47 if (typeof $.ajatus.tags.active == 'object') {
48 this.assign_tags[idx] = $.ajatus.tags.active;
52 'input', { type: 'hidden', name: 'widget['+name+':name]', value: this.name }, '',
53 'input', { type: 'hidden', name: 'widget['+name+':config]', value: $.ajatus.converter.toJSON(this.settings) }, '',
54 'input', { type: 'hidden', name: 'widget['+name+':required]', value: this.required }, '',
55 'input', { type: 'text', name: name, value: '' }, ''
58 get_edit_tpl: function(name, data)
60 data.val = this.value_on_edit(data.val);
62 'input', { type: 'hidden', name: 'widget['+name+':name]', value: this.name }, '',
63 'input', { type: 'hidden', name: 'widget['+name+':config]', value: $.ajatus.converter.toJSON(this.settings) }, '',
64 'input', { type: 'hidden', name: 'widget['+name+':required]', value: this.required }, '',
65 'input', { type: 'hidden', name: 'widget['+name+':prev_val]', value: $.ajatus.converter.toJSON(data.val) }, '',
66 'input', { type: 'text', name: name, value: '' }, ''
69 get_view_tpl: function(name, data)
71 data.val = this.value_on_view(data.val);
74 'div', { className: data._id+'_element_'+name+'_value' }, [
75 'ul', { className: 'tag_list' }, data.val,
76 'br', { className: 'clear_fix' }, ''
80 set_config: function(config)
82 this.settings = $.extend(this.settings, config);
84 value_on_save: function(value, prev_val)
89 var id_inputs = $(':input[name*=__tags_widget_selections]');
91 $.each(id_inputs, function(i,n){
92 tag_arr.push($.ajatus.converter.parseJSON(n.value))
95 if ( typeof prev_val == 'undefined'
101 $.each(tag_arr, function(i,t){
102 if (typeof t.id != 'undefined') {
107 tag_arr = $.grep(tag_arr, function(n,i){
108 return n != '' && (typeof n.id == 'undefined' || n.id == '');
111 var saved_tag_ids = this._update_system_tags(tag_arr, prev_val);
113 $.each(saved_tag_ids, function(i,t){
119 has_data_changed: function(value, prev_objects) {
121 $.each(prev_objects, function(i, n){
122 prev_value.push(n.id);
125 var data_changed = false;
127 if ( value.length == 0
128 && prev_value.length == 0)
135 if ( value.length > 0
136 && prev_value.length == 0)
140 if ( prev_value.length > 0
141 && value.length == 0)
146 $.each(value, function(i,n){
147 var match_key = $.inArray(n, prev_value);
148 if (match_key == -1) {
157 $.each(prev_value, function(i,n){
158 var match_key = $.inArray(n, value);
159 if (match_key == -1) {
166 value_on_edit: function(value)
169 if (self.assign_tags == null) {
170 self.assign_tags = [];
172 $.each(value, function(i,t){
173 self.assign_tags[self.assign_tags.length || i] = $.ajatus.tags.get(t);
176 return self.assign_tags;
178 // Type = list / item
179 value_on_view: function(value, type)
181 if (typeof type == 'undefined') {
188 if (value.length > 0) {
189 $.each(value, function(i,v){
190 var tag = $.ajatus.tags.get(v);
192 if ( typeof(tag.value.title.widget.config['context']) != 'undefined'
193 && tag.value.title.widget.config['context'] != '')
195 tag_title += tag.value.title.widget.config.context + ':';
198 tag_title += tag.value.title.val;
200 if ( typeof(tag.value.title.widget.config['value']) != 'undefined'
201 && tag.value.title.widget.config['value'] != '')
203 tag_title += '=' + tag.value.title.widget.config.value;
206 if (type == 'plain') {
207 tags_str += tag_title + _self.settings.separator;
209 var tpl = _self.style_tag_on_view(tag_title, tag.value.title.widget.config.color, type);
210 $.each(tpl, function(tidx, tp){
217 if (type == 'plain') {
223 style_tag_on_view: function(title, color, type) {
224 if ( typeof color == 'undefined'
227 var color = $.ajatus.widgets.core.tag.settings.color;
230 if (type == 'list') {
232 'span', { className: 'tag' }, title
237 'li', { className: 'tag', style: 'background-color: #'+color+';' }, title
242 $.ajatus.events.lock_pool.increase();
244 $.ajatus.layout.styles.load($.ajatus.preferences.client.theme_url + 'css/widgets/tags.css');
245 $.ajatus.layout.styles.load($.ajatus.preferences.client.theme_url + 'css/widgets/includes/tags.css');
247 var tags_url = $.ajatus.preferences.client.application_url + 'js/widgets/includes/tags.js';
248 $.ajatus.utils.load_script(tags_url, "$.ajatus.events.lock_pool.decrease", []);
250 init: function(holder, form_mode)
253 this.init_form_mode(holder);
255 this.init_view_mode(holder);
258 init_form_mode: function(holder)
260 var element = $('input[@type=text]', holder);
262 if (this.assign_tags == null) {
263 this.assign_tags = [];
267 element.tags(this.settings.widget);
269 $.each(this.assign_tags, function(i,tag) {
270 if (typeof tag == "object") {
271 element.tags_add_item(tag, true);
273 tag = $.ajatus.tags.get(tag);
275 element.tags_add_item(tag, true);
281 init_view_mode: function(holder)
284 _update_system_tags: function(new_tags, prev_tags)
290 || new_tags.length <= 0)
295 var on_success = function(data) {
297 var msg = $.ajatus.elements.messages.create(
298 $.ajatus.i10n.get('New tag created'),
299 $.ajatus.i10n.get("Created new tag '%s'", [data.id])
302 $.ajatus.debug("saved tag: "+data.id);
303 tag_ids.push(data.id);
308 var dc = $.jqCouch.connection('doc', on_success);
310 $.each(new_tags, function(i,d){
313 var context_key = '';
315 var tag_parts = t.split(':');
316 context_key = tag_parts[0];
321 var tag_parts = t.split('=');
322 value = tag_parts[1];
327 color: (typeof d.color != 'undefined') ? d.color : '',
328 context: (typeof d.context != 'undefined') ? d.context : context_key,
329 value: (typeof d.value != 'undefined') ? d.value : value
337 $.ajatus.tags.create(t, data, dc);
340 $.ajatus.views.on_change_actions.add('$.ajatus.tags.refresh_cache();');