Better checking for value changes
[ajatus.git] / js / widgets / tags.js
blob0a807bd5e01abc0efd2fab78b3381edf5e67fa41
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.widgets = $.ajatus.widgets || {};
18     $.ajatus.widgets.core = typeof($.ajatus.widgets.core) == 'undefined' ? {} : $.ajatus.widgets.core;
20     $.ajatus.widgets.core.tags = {
21         name: 'tags',
22         settings: {
23             separator: ' ',
24             context_key: '',
25             widget: {}
26         },
27         assign_tags: null,
28         
29         get_create_tpl: function(name, default_value)
30         {
31             if (this.assign_tags == null) {
32                 this.assign_tags = [];
33             }
34             
35             var idx = 0;
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);
40                         if (tag) {
41                             this.assign_tags[idx] = tag;
42                             idx += 1;
43                         }
44                     }
45                 });
46             }
47             if (typeof $.ajatus.tags.active == 'object') {
48                 this.assign_tags[idx] = $.ajatus.tags.active;
49             }
50             
51             return [
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: '' }, ''
56             ];
57         },
58         get_edit_tpl: function(name, data)
59         {
60             data.val = this.value_on_edit(data.val);
61             return [
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: '' }, ''
67             ];
68         },
69         get_view_tpl: function(name, data)
70         {
71             data.val = this.value_on_view(data.val);
73             return [
74                 'div', { className: data._id+'_element_'+name+'_value' }, [
75                     'ul', { className: 'tag_list' }, data.val,
76                     'br', { className: 'clear_fix' }, ''
77                 ]
78             ];
79         },
80         set_config: function(config)
81         {
82             this.settings = $.extend(this.settings, config);
83         },
84         value_on_save: function(value, prev_val)
85         {
86             var tag_arr = [];
87             var tag_ids = [];
88             
89             var id_inputs = $(':input[name*=__tags_widget_selections]');
90             
91             $.each(id_inputs, function(i,n){                
92                 tag_arr.push($.ajatus.converter.parseJSON(n.value))
93             });
94             
95             if (   typeof prev_val == 'undefined'
96                 || !prev_val)
97             {
98                 var prev_val = [];
99             }
101             $.each(tag_arr, function(i,t){
102                 if (typeof t.id != 'undefined') {
103                     tag_ids.push(t.id);
104                 }
105             });
106             
107             tag_arr = $.grep(tag_arr, function(n,i){
108                 return n != '' && (typeof n.id == 'undefined' || n.id == '');
109             });
110             
111             var saved_tag_ids = this._update_system_tags(tag_arr, prev_val);
112             
113             $.each(saved_tag_ids, function(i,t){
114                 tag_ids.push(t);
115             });
116             
117             return tag_ids;
118         },
119         has_data_changed: function(value, prev_objects) {
120             var prev_value = [];
121             $.each(prev_objects, function(i, n){
122                 prev_value.push(n.id);
123             });
124             
125             var data_changed = false;
126             
127             if (   value.length == 0
128                 && prev_value.length == 0)
129             {
130                 return false;
131             }
132             
133             
134             
135             if (   value.length > 0
136                 && prev_value.length == 0)
137             {
138                 return true;
139             }
140             if (   prev_value.length > 0
141                 && value.length == 0)
142             {
143                 return true;
144             }
145             
146             $.each(value, function(i,n){
147                 var match_key = $.inArray(n, prev_value);
148                 if (match_key == -1) {
149                     data_changed = true;
150                 }
151             });
152             
153             if (data_changed) {
154                 return true;
155             }
156             
157             $.each(prev_value, function(i,n){
158                 var match_key = $.inArray(n, value);
159                 if (match_key == -1) {
160                     data_changed = true;
161                 }
162             });
163             
164             return data_changed;
165         },
166         value_on_edit: function(value)
167         {            
168             var self = this;
169             if (self.assign_tags == null) {
170                 self.assign_tags = [];
171             }
172             $.each(value, function(i,t){                
173                 self.assign_tags[self.assign_tags.length || i] = $.ajatus.tags.get(t);
174             });
175             
176             return self.assign_tags;
177         },
178         // Type = list / item
179         value_on_view: function(value, type)
180         {
181             if (typeof type == 'undefined') {
182                 var type = 'item';
183             }
184             
185             var tags = [];
186             var tags_str = '';
187             var _self = this;
188             if (value.length > 0) {
189                 $.each(value, function(i,v){
190                     var tag = $.ajatus.tags.get(v);
191                     var tag_title = '';
192                     if (   typeof(tag.value.title.widget.config['context']) != 'undefined'
193                         && tag.value.title.widget.config['context'] != '')
194                     {
195                         tag_title += tag.value.title.widget.config.context + ':';
196                     }
197                     
198                     tag_title += tag.value.title.val;
199                     
200                     if (   typeof(tag.value.title.widget.config['value']) != 'undefined'
201                         && tag.value.title.widget.config['value'] != '')
202                     {
203                         tag_title += '=' + tag.value.title.widget.config.value;
204                     }
205                                         
206                     if (type == 'plain') {
207                         tags_str += tag_title + _self.settings.separator;
208                     } else {
209                         var tpl = _self.style_tag_on_view(tag_title, tag.value.title.widget.config.color, type);
210                         $.each(tpl, function(tidx, tp){
211                             tags.push(tp);
212                         });                        
213                     }
214                 });
215             }
216             
217             if (type == 'plain') {
218                 return tags_str;
219             } else {
220                 return tags;
221             }
222         },
223         style_tag_on_view: function(title, color, type) {            
224             if (   typeof color == 'undefined'
225                 || color == '')
226             {
227                 var color = $.ajatus.widgets.core.tag.settings.color;
228             }
229             
230             if (type == 'list') {
231                 return [
232                     'span', { className: 'tag' }, title
233                 ];
234             }
235             
236             return [
237                 'li', { className: 'tag', style: 'background-color: #'+color+';' }, title
238             ];
239         },
240         loaded: function()
241         {
242             $.ajatus.events.lock_pool.increase();
243             
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');
246             
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", []);
249         },
250         init: function(holder, form_mode)
251         {
252             if (form_mode) {
253                 this.init_form_mode(holder);
254             } else {
255                 this.init_view_mode(holder);                
256             }
257         },
258         init_form_mode: function(holder)
259         {
260             var element = $('input[@type=text]', holder);
261             
262             if (this.assign_tags == null) {
263                 this.assign_tags = [];
264             }
265             
266             if (element) {
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);
272                     } else {
273                         tag = $.ajatus.tags.get(tag);
274                         if (tag) {
275                             element.tags_add_item(tag, true);
276                         }                        
277                     }
278                 });
279             }
280         },
281         init_view_mode: function(holder)
282         {
283         },
284         _update_system_tags: function(new_tags, prev_tags)
285         {
286             var _self = this;
287             var tag_ids = [];
288             
289             if (   !new_tags
290                 || new_tags.length <= 0)
291             {
292                 return tag_ids;
293             }
294             
295             var on_success = function(data) {
296                 if (data.ok) {
297                     var msg = $.ajatus.elements.messages.create(
298                         $.ajatus.i10n.get('New tag created'),
299                         $.ajatus.i10n.get("Created new tag '%s'", [data.id])
300                     );
302                     $.ajatus.debug("saved tag: "+data.id);
303                     tag_ids.push(data.id);
304                 }
305                 
306                 return data;
307             };
308             var dc = $.jqCouch.connection('doc', on_success);
309             
310             $.each(new_tags, function(i,d){                
311                 var t = d.title;
312                 
313                 var context_key = '';
314                 if (t.match(/:/)) {
315                     var tag_parts = t.split(':');
316                     context_key = tag_parts[0];
317                     t = tag_parts[1];
318                 }
319                 var value = '';
320                 if (t.match(/\=/)) {
321                     var tag_parts = t.split('=');
322                     value = tag_parts[1];
323                     t = tag_parts[0];
324                 }
325                 
326                 var pd = {
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
330                 }
331                 
332                 var data = {
333                     color: pd.color,
334                     context: pd.context,
335                     value: pd.value
336                 };
337                 $.ajatus.tags.create(t, data, dc);
338             });
339             
340             $.ajatus.views.on_change_actions.add('$.ajatus.tags.refresh_cache();');
341             
342             return tag_ids;
343         }
344     };
346 })(jQuery);