[FIX] website_forum: use other attr than value to init select2
[odoo/odoo.git] / addons / website_forum / static / src / js / website_forum.js
1     openerp.website.if_dom_contains('.website_forum', function () {
2         $('.karma_required').on('click', function (ev) {
3             var karma = $(ev.currentTarget).data('karma');
4             if (karma) {
5                 ev.preventDefault();
6                 var $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="karma_alert">'+
7                     '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
8                     karma + ' karma is required to perform this action. You can earn karma by having '+
9                             'your answers upvoted by the community.</div>');
10                 var vote_alert = $(ev.currentTarget).parent().find("#vote_alert");
11                 if (vote_alert.length == 0) {
12                     $(ev.currentTarget).parent().append($warning);
13                 }
14             }
15         });
16
17         $('.vote_up,.vote_down').not('.karma_required').on('click', function (ev) {
18             ev.preventDefault();
19             var $link = $(ev.currentTarget);
20             openerp.jsonRpc($link.data('href'), 'call', {})
21                 .then(function (data) {
22                     if (data['error']){
23                         if (data['error'] == 'own_post'){
24                             var $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="vote_alert">'+
25                                 '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
26                                 'Sorry, you cannot vote for your own posts'+
27                                 '</div>');
28                         } else if (data['error'] == 'anonymous_user'){
29                             var $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="vote_alert">'+
30                                 '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
31                                 'Sorry you must be logged to vote'+
32                                 '</div>');
33                         }
34                         vote_alert = $link.parent().find("#vote_alert");
35                         if (vote_alert.length == 0) {
36                             $link.parent().append($warning);
37                         }
38                     } else {
39                         $link.parent().find("#vote_count").html(data['vote_count']);
40                         if (data['user_vote'] == 0) {
41                             $link.parent().find(".text-success").removeClass("text-success");
42                             $link.parent().find(".text-warning").removeClass("text-warning");
43                         } else {
44                             if (data['user_vote'] == 1) {
45                                 $link.addClass("text-success");
46                             } else {
47                                 $link.addClass("text-warning");
48                             }
49                         }
50                     }
51                 });
52         });
53
54         $('.accept_answer').not('.karma_required').on('click', function (ev) {
55             ev.preventDefault();
56             var $link = $(ev.currentTarget);
57             openerp.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
58                 if (data['error']) {
59                     if (data['error'] == 'anonymous_user') {
60                         var $warning = $('<div class="alert alert-danger alert-dismissable" id="correct_answer_alert" style="position:absolute; margin-top: -30px; margin-left: 90px;">'+
61                             '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
62                             'Sorry, anonymous users cannot choose correct answer.'+
63                             '</div>');
64                     }
65                     correct_answer_alert = $link.parent().find("#correct_answer_alert");
66                     if (correct_answer_alert.length == 0) {
67                         $link.parent().append($warning);
68                     }
69                 } else {
70                     if (data) {
71                         $link.addClass("oe_answer_true").removeClass('oe_answer_false');
72                     } else {
73                         $link.removeClass("oe_answer_true").addClass('oe_answer_false');
74                     }
75                 }
76             });
77         });
78
79         $('.favourite_question').on('click', function (ev) {
80             ev.preventDefault();
81             var $link = $(ev.currentTarget);
82             openerp.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
83                 if (data) {
84                     $link.addClass("forum_favourite_question")
85                 } else {
86                     $link.removeClass("forum_favourite_question")
87                 }
88             });
89         });
90
91         $('.comment_delete').on('click', function (ev) {
92             ev.preventDefault();
93             var $link = $(ev.currentTarget);
94             openerp.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
95                 $link.parents('.comment').first().remove();
96             });
97         });
98
99         $('.notification_close').on('click', function (ev) {
100             ev.preventDefault();
101             var $link = $(ev.currentTarget);
102             openerp.jsonRpc("/forum/notification_read", 'call', {
103                 'notification_id': $link.attr("id")});
104         });
105
106         $('.send_validation_email').on('click', function (ev) {
107             ev.preventDefault();
108             var $link = $(ev.currentTarget);
109             openerp.jsonRpc("/forum/send_validation_email", 'call', {
110                 'forum_id': $link.attr('forum-id'),
111             }).then(function (data) {
112                 if (data) {
113                     $('button.validation_email_close').click();
114                 }
115             });
116         });
117
118         $('.validated_email_close').on('click', function (ev) {
119             openerp.jsonRpc("/forum/validate_email/close", 'call', {});
120         });
121
122
123         $('input.js_select2').select2({
124             tags: true,
125             tokenSeparators: [",", " ", "_"],
126             maximumInputLength: 35,
127             minimumInputLength: 2,
128             maximumSelectionSize: 5,
129             lastsearch: [],
130             createSearchChoice: function (term) {
131                 if ($(lastsearch).filter(function () { return this.text.localeCompare(term) === 0;}).length === 0) {
132                     //check Karma
133                     if (parseInt($("#karma").val()) >= parseInt($("#karma_retag").val())) {
134                         return {
135                             id: "_" + $.trim(term),
136                             text: $.trim(term) + ' *',
137                             isNew: true,
138                         };
139                     }
140                     
141                 }
142             },
143             formatResult: function(term) {
144                 if (term.isNew) {
145                     return '<span class="label label-primary">New</span> ' + _.escape(term.text);
146                 }
147                 else {
148                     return _.escape(term.text);
149                 }
150             },
151             ajax: {
152                 url: '/forum/get_tags',
153                 dataType: 'json',
154                 data: function(term, page) {
155                     return {
156                         q: term,
157                         t: 'select2',
158                         l: 50
159                     };
160                 },
161                 results: function(data, page) {
162                     var ret = [];
163                     _.each(data, function(x) {
164                         ret.push({ id: x.id, text: x.name, isNew: false });
165                     });
166                     lastsearch = ret;
167                     return { results: ret };
168                 }
169             },
170
171             // Take default tags from the input value
172             initSelection: function (element, callback) {
173                 var data = [];
174                 _.each(element.data('init-value'), function(x) {
175                     data.push({ id: x.id, text: x.name, isNew: false });
176                 });
177                 element.val('');
178                 callback(data);
179             },
180         });
181
182         if($('input.load_tags').length){
183             var tags = $("input.load_tags").val();
184             $("input.load_tags").val("");
185             set_tags(tags);
186         };
187
188         function set_tags(tags) {
189             $("input.load_tags").textext({
190                 plugins: 'tags focus autocomplete ajax',
191                 tagsItems: tags.split(","),
192                 //Note: The following list of keyboard keys is added. All entries are default except {32 : 'whitespace!'}.
193                 keys: {8: 'backspace', 9: 'tab', 13: 'enter!', 27: 'escape!', 37: 'left', 38: 'up!', 39: 'right',
194                     40: 'down!', 46: 'delete', 108: 'numpadEnter', 32: 'whitespace!'},
195                 ajax: {
196                     url: '/forum/get_tags',
197                     dataType: 'json',
198                     cacheResults: true
199                 }
200             });
201             // Adds: create tags on space + blur
202             $("input.load_tags").on('whitespaceKeyDown blur', function () {
203                 $(this).textext()[0].tags().addTags([ $(this).val() ]);
204                 $(this).val("");
205             });
206             $("input.load_tags").on('isTagAllowed', function(e, data) {
207                 if (_.indexOf($(this).textext()[0].tags()._formData, data.tag) != -1) {
208                     data.result = false;
209                 }
210             });
211         }
212         //END-TODO Remove in master
213
214         if ($('textarea.load_editor').length) {
215             var editor = CKEDITOR.instances['content'];
216             editor.on('instanceReady', CKEDITORLoadComplete);
217         }
218
219         function CKEDITORLoadComplete(){
220             "use strict";
221             $('.cke_button__link').attr('onclick','website_forum_IsKarmaValid(33,30)');
222             $('.cke_button__unlink').attr('onclick','website_forum_IsKarmaValid(37,30)');
223             $('.cke_button__image').attr('onclick','website_forum_IsKarmaValid(41,30)');
224         }
225     });
226
227    function website_forum_IsKarmaValid(eventNumber, minKarma){
228         "use strict";
229         if(parseInt($("#karma").val()) >= minKarma){
230             CKEDITOR.tools.callFunction(eventNumber, this);
231             return false;
232         } else {
233             alert("Sorry you need more than " + minKarma + " Karma.");
234         }
235     }