[FIX] website_forum: fixed issue with user vote badly taken into account when upvotin...
[odoo/odoo.git] / addons / website_forum / static / src / js / website_forum.js
1 $(document).ready(function () {
2
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 answering questions or 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         return true;
54     });
55
56     $('.accept_answer').not('.karma_required').on('click', function (ev) {
57         ev.preventDefault();
58         var $link = $(ev.currentTarget);
59         openerp.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
60             if (data['error']) {
61                 if (data['error'] == 'anonymous_user') {
62                     var $warning = $('<div class="alert alert-danger alert-dismissable" id="correct_answer_alert" style="position:absolute; margin-top: -30px; margin-left: 90px;">'+
63                         '<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
64                         'Sorry, anonymous users cannot choose correct answer.'+
65                         '</div>');
66                 }
67                 correct_answer_alert = $link.parent().find("#correct_answer_alert");
68                 if (correct_answer_alert.length == 0) {
69                     $link.parent().append($warning);
70                 }
71             } else {
72                 if (data) {
73                     $link.addClass("oe_answer_true").removeClass('oe_answer_false');
74                 } else {
75                     $link.removeClass("oe_answer_true").addClass('oe_answer_false');
76                 }
77             }
78         });
79         return true;
80     });
81
82     $('.favourite_question').on('click', function (ev) {
83         ev.preventDefault();
84         var $link = $(ev.currentTarget);
85         openerp.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
86             if (data) {
87                 $link.addClass("forum_favourite_question")
88             } else {
89                 $link.removeClass("forum_favourite_question")
90             }
91         });
92         return true;
93     });
94
95     $('.comment_delete').on('click', function (ev) {
96         ev.preventDefault();
97         var $link = $(ev.currentTarget);
98         openerp.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
99             $link.parents('.comment').first().remove();
100         });
101         return true;
102     });
103
104     $('.notification_close').on('click', function (ev) {
105         ev.preventDefault();
106         var $link = $(ev.currentTarget);
107         openerp.jsonRpc("/forum/notification_read", 'call', {
108             'notification_id': $link.attr("id")})
109         return true;
110     });
111
112     if($('input.load_tags').length){
113         var tags = $("input.load_tags").val();
114         $("input.load_tags").val("");
115         set_tags(tags);
116     };
117
118     function set_tags(tags) {
119         $("input.load_tags").textext({
120             plugins: 'tags focus autocomplete ajax',
121             tagsItems: tags.split(","),
122             //Note: The following list of keyboard keys is added. All entries are default except {32 : 'whitespace!'}.
123             keys: {8: 'backspace', 9: 'tab', 13: 'enter!', 27: 'escape!', 37: 'left', 38: 'up!', 39: 'right',
124                 40: 'down!', 46: 'delete', 108: 'numpadEnter', 32: 'whitespace!'},
125             ajax: {
126                 url: '/forum/get_tags',
127                 dataType: 'json',
128                 cacheResults: true
129             }
130         });
131         // Adds: create tags on space + blur
132         $("input.load_tags").on('whitespaceKeyDown blur', function () {
133             $(this).textext()[0].tags().addTags([ $(this).val() ]);
134             $(this).val("");
135         });
136         $("input.load_tags").on('isTagAllowed', function(e, data) {
137             if (_.indexOf($(this).textext()[0].tags()._formData, data.tag) != -1) {
138                 data.result = false;
139             }
140         });
141     }
142
143     if ($('textarea.load_editor').length) {
144         var editor = CKEDITOR.instances['content'];
145         editor.on('instanceReady', CKEDITORLoadComplete);
146     }
147 });
148
149 function IsKarmaValid(eventNumber,minKarma){
150     "use strict";
151     if(parseInt($("#karma").val()) >= minKarma){
152         CKEDITOR.tools.callFunction(eventNumber,this);
153         return false;
154     } else {
155         alert("Sorry you need more than " + minKarma + " Karma.");
156     }
157 }
158
159 function CKEDITORLoadComplete(){
160     "use strict";
161     $('.cke_button__link').attr('onclick','IsKarmaValid(33,30)');
162     $('.cke_button__unlink').attr('onclick','IsKarmaValid(37,30)');
163     $('.cke_button__image').attr('onclick','IsKarmaValid(41,30)');
164 }