[FIX] links and threads
[odoo/odoo.git] / addons / website_forum / static / src / js / website_forum.js
1     openerp.website.if_dom_contains('.website_forum', function () {
2         $("[data-toggle='popover']").popover();
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         $('.js_close_intro').on('click', function (ev) {
125             ev.preventDefault();
126             document.cookie = "no_introduction_message = false";
127             return true;
128         });
129
130         $('.link_url').on('change', function (ev) {
131             ev.preventDefault();
132             var $link = $(ev.currentTarget);
133             if ($link.attr("value").search("^http(s?)://.*")) {
134                 var $warning = $('<div class="alert alert-danger alert-dismissable" style="position:absolute; margin-top: -180px; margin-left: 90px;">'+
135                     '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
136                     'Please enter valid URL. Example: http://www.odoo.com'+
137                     '</div>');
138                 $link.parent().append($warning);
139                 $("button#btn_post_your_article")[0].disabled = true;
140             } else {
141                 openerp.jsonRpc("/forum/get_url_title", 'call', {'url': $link.attr("value")}).then(function (data) {
142                     $("input[name='post_name']")[0].value = data;
143                     $('button#btn_post_your_article').prop('disabled', false);
144                 });
145             }
146         });
147
148         $('input.js_select2').select2({
149             tags: true,
150             tokenSeparators: [",", " ", "_"],
151             maximumInputLength: 35,
152             minimumInputLength: 2,
153             maximumSelectionSize: 5,
154             lastsearch: [],
155             createSearchChoice: function (term) {
156                 if ($(lastsearch).filter(function () { return this.text.localeCompare(term) === 0;}).length === 0) {
157                     //check Karma
158                     if (parseInt($("#karma").val()) >= parseInt($("#karma_retag").val())) {
159                         return {
160                             id: "_" + $.trim(term),
161                             text: $.trim(term) + ' *',
162                             isNew: true,
163                         };
164                     }
165                 }
166             },
167             formatResult: function(term) {
168                 if (term.isNew) {
169                     return '<span class="label label-primary">New</span> ' + _.escape(term.text);
170                 }
171                 else {
172                     return _.escape(term.text);
173                 }
174             },
175             ajax: {
176                 url: '/forum/get_tags',
177                 dataType: 'json',
178                 data: function(term, page) {
179                     return {
180                         q: term,
181                         l: 50
182                     };
183                 },
184                 results: function(data, page) {
185                     var ret = [];
186                     _.each(data, function(x) {
187                         ret.push({ id: x.id, text: x.name, isNew: false });
188                     });
189                     lastsearch = ret;
190                     return { results: ret };
191                 }
192             },
193
194             // Take default tags from the input value
195             initSelection: function (element, callback) {
196                 var data = [];
197                 _.each(JSON.parse(element.val()), function(x) {
198                     data.push({ id: x.id, text: x.name, isNew: false });
199                 });
200                 element.val('');
201                 callback(data);
202             },
203         });
204
205         if ($('textarea.load_editor').length) {
206             $('textarea.load_editor').each(function () {
207                 if (this['id']) {
208                     CKEDITOR.replace(this['id']).on('instanceReady', CKEDITORLoadComplete);
209                 }
210             });
211         }
212
213         function IsKarmaValid(eventNumber,minKarma){
214             "use strict";
215             if(parseInt($("#karma").val()) >= minKarma){
216                 CKEDITOR.tools.callFunction(eventNumber,this);
217                 return false;
218             } else {
219                 alert("Sorry you need more than " + minKarma + " Karma.");
220             }
221         }
222
223         function CKEDITORLoadComplete(){
224             "use strict";
225             $('.cke_button__link').on('click', function() { IsKarmaValid(33,30); });
226             $('.cke_button__unlink').on('click', function() { IsKarmaValid(37,30); });
227             $('.cke_button__image').on('click', function() { IsKarmaValid(41,30); });
228         }
229     });
230