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