]> git.openstreetmap.org Git - osqa.git/blob - forum/skins/default/media/js/com.cnprog.tag_selector.js
e3279e6575ff5bc57a779feb6bfa79df8a470f8a
[osqa.git] / forum / skins / default / media / js / com.cnprog.tag_selector.js
1 //var scriptUrl, interestingTags, ignoredTags, tags, $;
2 function pickedTags(){
3
4     var sendAjax = function(tagname, reason, action, callback){
5         var url = scriptUrl;
6         if (action == 'add'){
7             url += $.i18n._('mark-tag/');
8             if (reason == 'good'){
9                 url += $.i18n._('interesting/');
10             }
11             else {
12                 url += $.i18n._('ignored/');
13             }
14         }
15         else {
16             url += $.i18n._('unmark-tag/');
17         }
18         url = url + tagname + '/';
19
20         var call_settings = {
21             type:'POST',
22             url:url
23         };
24         if (callback !== false){
25             call_settings.success = callback;
26         }
27         $.ajax(call_settings);
28     };
29
30
31     var unpickTag = function(from_target ,tagname, reason, send_ajax){
32         //send ajax request to delete tag
33         var deleteTagLocally = function(){
34             from_target[tagname].remove();
35             delete from_target[tagname];
36         };
37         if (send_ajax){
38             sendAjax(tagname,reason,'remove',deleteTagLocally);
39         }
40         else {
41             deleteTagLocally();
42         }
43
44     };
45
46     var setupTagDeleteEvents = function(obj,tag_store,tagname,reason,send_ajax){
47         obj.unbind('mouseover').bind('mouseover', function(){
48             $(this).attr('src', mediaUrl('media/images/close-small-hover.png'));
49         });
50         obj.unbind('mouseout').bind('mouseout', function(){
51             $(this).attr('src', mediaUrl('media/images/close-small-dark.png'));
52         });
53         obj.click( function(){
54             unpickTag(tag_store,tagname,reason,send_ajax);
55         });
56     };
57
58     var handlePickedTag = function(obj,reason){
59         var tagname = $.trim($(obj).prev().attr('value'));
60         var to_target = interestingTags;
61         var from_target = ignoredTags;
62         var to_tag_container;
63         if (reason == 'bad'){
64             to_target = ignoredTags;
65             from_target = interestingTags;
66             to_tag_container = $('div .tags.ignored');
67         }
68         else if (reason != 'good'){
69             return;
70         }
71         else {
72             to_tag_container = $('div .tags.interesting');
73         }
74
75         if (tagname in from_target){
76             unpickTag(from_target,tagname,reason,false);
77         }
78
79         if (!(tagname in to_target)){
80             //send ajax request to pick this tag
81
82             sendAjax(tagname,reason,'add',function(){
83                 var new_tag = $('<span></span>');
84                 new_tag.addClass('deletable-tag');
85                 var tag_link = $('<a></a>');
86                 tag_link.attr('rel','tag');
87                 tag_link.attr('href', scriptUrl + $.i18n._('tags/') + tagname);
88                 tag_link.html(tagname);
89                 var del_link = $('<img></img>');
90                 del_link.addClass('delete-icon');
91                 del_link.attr('src', mediaUrl('/media/images/close-small-dark.png'));
92
93                 setupTagDeleteEvents(del_link, to_target, tagname, reason, true);
94
95                 new_tag.append(tag_link);
96                 new_tag.append(del_link);
97                 to_tag_container.append(new_tag);
98
99                 to_target[tagname] = new_tag;
100             });
101         }
102     };
103
104     var collectPickedTags = function(){
105         var good_prefix = 'interesting-tag-';
106         var bad_prefix = 'ignored-tag-';
107         var good_re = RegExp('^' + good_prefix);
108         var bad_re = RegExp('^' + bad_prefix);
109         interestingTags = {};
110         ignoredTags = {};
111         $('.deletable-tag').each(
112             function(i,item){
113                 var item_id = $(item).attr('id');
114                 var tag_name, tag_store;
115                 if (good_re.test(item_id)){
116                     tag_name = item_id.replace(good_prefix,'');
117                     tag_store = interestingTags;
118                     reason = 'good';
119                 }
120                 else if (bad_re.test(item_id)){
121                     tag_name = item_id.replace(bad_prefix,'');
122                     tag_store = ignoredTags;
123                     reason = 'bad';
124                 } 
125                 else {
126                     return;
127                 }
128                 tag_store[tag_name] = $(item);
129                 setupTagDeleteEvents($(item).find('img'),tag_store,tag_name,reason,true);
130             }
131         );
132     };
133
134     var setupHideIgnoredQuestionsControl = function(){
135         $('#hideIgnoredTagsCb').unbind('click').click(function(){
136             $.ajax({
137                         type: 'POST',
138                         dataType: 'json',
139                         cache: false,
140                         url: scriptUrl + $.i18n._('command/'),
141                         data: {command:'toggle-ignored-questions'}
142                     });
143         });
144     };
145     return {
146         init: function(){
147             collectPickedTags();
148             setupHideIgnoredQuestionsControl();
149             $("#interestingTagInput, #ignoredTagInput").autocomplete(tags, {
150                 minChars: 1,
151                 matchContains: true,
152                 max: 20,
153                 multiple: true,
154                 multipleSeparator: " ",
155                 formatItem: function(row, i, max) {
156                     return row.n + " ("+ row.c +")";
157                 },
158                 formatResult: function(row, i, max){
159                     return row.n;
160                 }
161
162             });
163             $("#interestingTagAdd").click(function(){handlePickedTag(this,'good');});
164             $("#ignoredTagAdd").click(function(){handlePickedTag(this,'bad');});
165         }
166     };
167 }
168
169 $(document).ready( function(){
170     pickedTags().init();
171 });