1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
odoo.define('website_forum.website_forum', function (require) {
'use strict';
const dom = require('web.dom');
var core = require('web.core');
var weDefaultOptions = require('web_editor.wysiwyg.default_options');
var wysiwygLoader = require('web_editor.loader');
var publicWidget = require('web.public.widget');
var session = require('web.session');
var qweb = core.qweb;
var _t = core._t;
publicWidget.registry.websiteForum = publicWidget.Widget.extend({
selector: '.website_forum',
xmlDependencies: ['/website_forum/static/src/xml/website_forum_share_templates.xml'],
events: {
'click .karma_required': '_onKarmaRequiredClick',
'mouseenter .o_js_forum_tag_follow': '_onTagFollowBoxMouseEnter',
'mouseleave .o_js_forum_tag_follow': '_onTagFollowBoxMouseLeave',
'mouseenter .o_forum_user_info': '_onUserInfoMouseEnter',
'mouseleave .o_forum_user_info': '_onUserInfoMouseLeave',
'mouseleave .o_forum_user_bio_expand': '_onUserBioExpandMouseLeave',
'click .flag:not(.karma_required)': '_onFlagAlertClick',
'click .vote_up:not(.karma_required), .vote_down:not(.karma_required)': '_onVotePostClick',
'click .o_js_validation_queue a[href*="/validate"]': '_onValidationQueueClick',
'click .o_wforum_validate_toggler:not(.karma_required)': '_onAcceptAnswerClick',
'click .o_wforum_favourite_toggle': '_onFavoriteQuestionClick',
'click .comment_delete': '_onDeleteCommentClick',
'click .js_close_intro': '_onCloseIntroClick',
'submit .js_wforum_submit_form:has(:not(.karma_required).o_wforum_submit_post)': '_onSubmitForm',
},
/**
* @override
*/
start: function () {
var self = this;
this.lastsearch = [];
// float-left class messes up the post layout OPW 769721
$('span[data-oe-model="forum.post"][data-oe-field="content"]').find('img.float-left').removeClass('float-left');
// welcome message action button
var forumLogin = _.string.sprintf('%s/web?redirect=%s',
window.location.origin,
escape(window.location.href)
);
$('.forum_register_url').attr('href', forumLogin);
// Initialize forum's tooltips
this.$('[data-toggle="tooltip"]').tooltip({delay: 0});
this.$('[data-toggle="popover"]').popover({offset: 8});
$('input.js_select2').select2({
tags: true,
tokenSeparators: [',', ' ', '_'],
maximumInputLength: 35,
minimumInputLength: 2,
maximumSelectionSize: 5,
lastsearch: [],
createSearchChoice: function (term) {
if (_.filter(self.lastsearch, function (s) {
return s.text.localeCompare(term) === 0;
}).length === 0) {
//check Karma
if (parseInt($('#karma').val()) >= parseInt($('#karma_edit_retag').val())) {
return {
id: '_' + $.trim(term),
text: $.trim(term) + ' *',
isNew: true,
};
}
}
},
formatResult: function (term) {
if (term.isNew) {
return '<span class="badge badge-primary">New</span> ' + _.escape(term.text);
} else {
return _.escape(term.text);
}
},
ajax: {
url: '/forum/get_tags',
dataType: 'json',
data: function (term) {
return {
query: term,
limit: 50,
};
},
results: function (data) {
var ret = [];
_.each(data, function (x) {
ret.push({
id: x.id,
text: x.name,
isNew: false,
});
});
self.lastsearch = ret;
return {results: ret};
}
},
// Take default tags from the input value
initSelection: function (element, callback) {
var data = [];
_.each(element.data('init-value'), function (x) {
data.push({id: x.id, text: x.name, isNew: false});
});
element.val('');
callback(data);
},
});
_.each($('textarea.o_wysiwyg_loader'), function (textarea) {
var $textarea = $(textarea);
var editorKarma = $textarea.data('karma') || 0; // default value for backward compatibility
var $form = $textarea.closest('form');
var hasFullEdit = parseInt($("#karma").val()) >= editorKarma;
// Warning: Do not activate any option that adds inline style.
// Because the style is deleted after save.
var toolbar = [
['style', ['style']],
['font', ['bold', 'italic', 'underline', 'clear']],
['para', ['ul', 'ol', 'paragraph']],
['table', ['table']],
];
if (hasFullEdit) {
toolbar.push(['insert', ['link', 'picture']]);
}
toolbar.push(['history', ['undo', 'redo']]);
var options = {
height: 200,
minHeight: 80,
toolbar: toolbar,
styleWithSpan: false,
styleTags: _.without(weDefaultOptions.styleTags, 'h1', 'h2', 'h3'),
recordInfo: {
context: self._getContext(),
res_model: 'forum.post',
res_id: +window.location.pathname.split('-').pop(),
},
disableFullMediaDialog: true,
disableResizeImage: true,
};
if (!hasFullEdit) {
options.plugins = {
LinkPlugin: false,
MediaPlugin: false,
};
}
wysiwygLoader.load(self, $textarea[0], options).then(wysiwyg => {
// float-left class messes up the post layout OPW 769721
$form.find('.note-editable').find('img.float-left').removeClass('float-left');
// o_we_selected_image has not always been removed when
// saving a post so we need the line below to remove it if it is present.
$form.find('.note-editable').find('img.o_we_selected_image').removeClass('o_we_selected_image');
$form.on('click', 'button, .a-submit', () => {
$form.find('.note-editable').find('img.o_we_selected_image').removeClass('o_we_selected_image');
wysiwyg.save();
});
});
});
_.each(this.$('.o_wforum_bio_popover'), authorBox => {
$(authorBox).popover({
trigger: 'hover',
offset: 10,
animation: false,
html: true,
}).popover('hide').data('bs.popover').tip.classList.add('o_wforum_bio_popover_container');
});
this.$('#post_reply').on('shown.bs.collapse', function (e) {
const replyEl = document.querySelector('#post_reply');
const scrollingElement = dom.closestScrollable(replyEl.parentNode);
dom.scrollTo(replyEl, {
forcedOffset: $(scrollingElement).innerHeight() - $(replyEl).innerHeight(),
});
});
return this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
*
* @override
* @param {Event} ev
*/
_onSubmitForm: function (ev) {
let validForm = true;
let $form = $(ev.currentTarget);
let $title = $form.find('input[name=post_name]');
let $textarea = $form.find('textarea[name=content]');
// It's not really in the textarea that the user write at first
let textareaContent = $form.find('.o_wysiwyg_wrapper .note-editable.panel-body').text().trim();
if ($title.length && $title[0].required) {
if ($title.val()) {
$title.removeClass('is-invalid');
} else {
$title.addClass('is-invalid');
validForm = false;
}
}
// Because the textarea is hidden, we add the red or green border to its container
if ($textarea[0] && $textarea[0].required) {
let $textareaContainer = $form.find('.o_wysiwyg_wrapper .note-editor.panel.panel-default');
if (!textareaContent.length) {
$textareaContainer.addClass('border border-danger rounded-top');
validForm = false;
} else {
$textareaContainer.removeClass('border border-danger rounded-top');
}
}
if (validForm) {
// Stores social share data to display modal on next page.
if ($form.has('.oe_social_share_call').length) {
sessionStorage.setItem('social_share', JSON.stringify({
targetType: $(ev.currentTarget).find('.o_wforum_submit_post').data('social-target-type'),
}));
}
} else {
ev.preventDefault();
setTimeout(function() {
var $buttons = $(ev.currentTarget).find('button[type="submit"], a.a-submit');
_.each($buttons, function (btn) {
let $btn = $(btn);
$btn.find('i').remove();
$btn.prop('disabled', false);
});
}, 0);
}
},
/**
* @private
* @param {Event} ev
*/
_onKarmaRequiredClick: function (ev) {
var $karma = $(ev.currentTarget);
var karma = $karma.data('karma');
var forum_id = $('#wrapwrap').data('forum_id');
if (!karma) {
return;
}
ev.preventDefault();
var msg = karma + ' ' + _t("karma is required to perform this action. ");
var title = _t("Karma Error");
if (forum_id) {
msg += '<a class="alert-link" href="/forum/' + forum_id + '/faq">' + _t("Read the guidelines to know how to gain karma.") + '</a>';
}
if (session.is_website_user) {
msg = _t("Sorry you must be logged in to perform this action");
title = _t("Access Denied");
}
this.call('crash_manager', 'show_warning', {
message: msg,
title: title,
}, {
sticky: false,
});
},
/**
* @private
* @param {Event} ev
*/
_onTagFollowBoxMouseEnter: function (ev) {
$(ev.currentTarget).find('.o_forum_tag_follow_box').stop().fadeIn().css('display', 'block');
},
/**
* @private
* @param {Event} ev
*/
_onTagFollowBoxMouseLeave: function (ev) {
$(ev.currentTarget).find('.o_forum_tag_follow_box').stop().fadeOut().css('display', 'none');
},
/**
* @private
* @param {Event} ev
*/
_onUserInfoMouseEnter: function (ev) {
$(ev.currentTarget).parent().find('.o_forum_user_bio_expand').delay(500).toggle('fast');
},
/**
* @private
* @param {Event} ev
*/
_onUserInfoMouseLeave: function (ev) {
$(ev.currentTarget).parent().find('.o_forum_user_bio_expand').clearQueue();
},
/**
* @private
* @param {Event} ev
*/
_onUserBioExpandMouseLeave: function (ev) {
$(ev.currentTarget).fadeOut('fast');
},
/**
* @private
* @param {Event} ev
*/
_onFlagAlertClick: function (ev) {
var self = this;
ev.preventDefault();
var $link = $(ev.currentTarget);
this._rpc({
route: $link.data('href') || ($link.attr('href') !== '#' && $link.attr('href')) || $link.closest('form').attr('action'),
}).then(function (data) {
if (data.error) {
var message;
if (data.error === 'anonymous_user') {
message = _t("Sorry you must be logged to flag a post");
} else if (data.error === 'post_already_flagged') {
message = _t("This post is already flagged");
} else if (data.error === 'post_non_flaggable') {
message = _t("This post can not be flagged");
}
self.call('crash_manager', 'show_warning', {
message: message,
title: _t("Access Denied"),
}, {
sticky: false,
});
} else if (data.success) {
var elem = $link;
if (data.success === 'post_flagged_moderator') {
elem.data('href') && elem.html(' Flagged');
var c = parseInt($('#count_flagged_posts').html(), 10);
c++;
$('#count_flagged_posts').html(c);
} else if (data.success === 'post_flagged_non_moderator') {
elem.data('href') && elem.html(' Flagged');
var forumAnswer = elem.closest('.forum_answer');
forumAnswer.fadeIn(1000);
forumAnswer.slideUp(1000);
}
}
});
},
/**
* @private
* @param {Event} ev
*/
_onVotePostClick: function (ev) {
var self = this;
ev.preventDefault();
var $btn = $(ev.currentTarget);
this._rpc({
route: $btn.data('href'),
}).then(function (data) {
if (data.error) {
var message;
if (data.error === 'own_post') {
message = _t('Sorry, you cannot vote for your own posts');
} else if (data.error === 'anonymous_user') {
message = _t('Sorry you must be logged to vote');
}
self.call('crash_manager', 'show_warning', {
message: message,
title: _t("Access Denied"),
}, {
sticky: false,
});
} else {
var $container = $btn.closest('.vote');
var $items = $container.children();
var $voteUp = $items.filter('.vote_up');
var $voteDown = $items.filter('.vote_down');
var $voteCount = $items.filter('.vote_count');
var userVote = parseInt(data['user_vote']);
$voteUp.prop('disabled', userVote === 1);
$voteDown.prop('disabled', userVote === -1);
$items.removeClass('text-success text-danger text-muted o_forum_vote_animate');
void $container[0].offsetWidth; // Force a refresh
if (userVote === 1) {
$voteUp.addClass('text-success');
$voteCount.addClass('text-success');
$voteDown.removeClass('karma_required');
}
if (userVote === -1) {
$voteDown.addClass('text-danger');
$voteCount.addClass('text-danger');
$voteUp.removeClass('karma_required');
}
if (userVote === 0) {
if (!$voteDown.data('can-downvote')) {
$voteDown.addClass('karma_required');
}
if (!$voteUp.data('can-upvote')) {
$voteUp.addClass('karma_required');
}
}
$voteCount.html(data['vote_count']).addClass('o_forum_vote_animate');
}
});
},
/**
* @private
* @param {Event} ev
*/
_onValidationQueueClick: function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
$link.parents('.post_to_validate').hide();
$.get($link.attr('href')).then(() => {
var left = $('.o_js_validation_queue:visible').length;
var type = $('h2.o_page_header a.active').data('type');
$('#count_post').text(left);
$('#moderation_tools a[href*="/' + type + '_"]').find('strong').text(left);
if (!left) {
this.$('.o_caught_up_alert').removeClass('d-none');
}
}, function () {
$link.parents('.o_js_validation_queue > div').addClass('bg-danger text-white').css('background-color', '#FAA');
$link.parents('.post_to_validate').show();
});
},
/**
* @private
* @param {Event} ev
*/
_onAcceptAnswerClick: function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
var target = $link.data('target');
this._rpc({
route: $link.data('href'),
}).then(data => {
if (data.error) {
if (data.error === 'anonymous_user') {
var message = _t("Sorry, anonymous users cannot choose correct answer.");
}
this.call('crash_manager', 'show_warning', {
message: message,
title: _t("Access Denied"),
}, {
sticky: false,
});
} else {
_.each(this.$('.forum_answer'), answer => {
var $answer = $(answer);
var isCorrect = $answer.is(target) ? data : false;
var $toggler = $answer.find('.o_wforum_validate_toggler');
var newHelper = isCorrect ? $toggler.data('helper-decline') : $toggler.data('helper-accept');
$answer.toggleClass('o_wforum_answer_correct', isCorrect);
$toggler.tooltip('dispose')
.attr('data-original-title', newHelper)
.tooltip({delay: 0});
});
}
});
},
/**
* @private
* @param {Event} ev
*/
_onFavoriteQuestionClick: function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
this._rpc({
route: $link.data('href'),
}).then(function (data) {
$link.toggleClass('o_wforum_gold fa-star', data)
.toggleClass('fa-star-o text-muted', !data);
});
},
/**
* @private
* @param {Event} ev
*/
_onDeleteCommentClick: function (ev) {
ev.preventDefault();
var $link = $(ev.currentTarget);
var $container = $link.closest('.o_wforum_post_comments_container');
this._rpc({
route: $link.closest('form').attr('action'),
}).then(function () {
$link.closest('.o_wforum_post_comment').remove();
var count = $container.find('.o_wforum_post_comment').length;
if (count) {
$container.find('.o_wforum_comments_count').text(count);
} else {
$container.find('.o_wforum_comments_count_header').remove();
}
});
},
/**
* @private
* @param {Event} ev
*/
_onCloseIntroClick: function (ev) {
ev.preventDefault();
document.cookie = 'forum_welcome_message = false';
$('.forum_intro').slideUp();
return true;
},
});
publicWidget.registry.websiteForumSpam = publicWidget.Widget.extend({
selector: '.o_wforum_moderation_queue',
xmlDependencies: ['/website_forum/static/src/xml/website_forum_share_templates.xml'],
events: {
'click .o_wforum_select_all_spam': '_onSelectallSpamClick',
'click .o_wforum_mark_spam': 'async _onMarkSpamClick',
'input #spamSearch': '_onSpamSearchInput',
},
/**
* @override
*/
start: function () {
this.spamIDs = this.$('.modal').data('spam-ids');
return this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param {Event} ev
*/
_onSelectallSpamClick: function (ev) {
var $spamInput = this.$('.modal .tab-pane.active input');
$spamInput.prop('checked', true);
},
/**
* @private
* @param {Event} ev
*/
_onSpamSearchInput: function (ev) {
var self = this;
var toSearch = $(ev.currentTarget).val();
return this._rpc({
model: 'forum.post',
method: 'search_read',
args: [
[['id', 'in', self.spamIDs],
'|',
['name', 'ilike', toSearch],
['content', 'ilike', toSearch]],
['name', 'content']
],
kwargs: {}
}).then(function (o) {
_.each(o, function (r) {
r.content = $('<p>' + $(r.content).html() + '</p>').text().substring(0, 250);
});
self.$('div.post_spam').html(qweb.render('website_forum.spam_search_name', {
posts: o,
}));
});
},
/**
* @private
* @param {Event} ev
*/
_onMarkSpamClick: function (ev) {
var key = this.$('.modal .tab-pane.active').data('key');
var $inputs = this.$('.modal .tab-pane.active input.custom-control-input:checked');
var values = _.map($inputs, function (o) {
return parseInt(o.value);
});
return this._rpc({model: 'forum.post',
method: 'mark_as_offensive_batch',
args: [this.spamIDs, key, values],
}).then(function () {
window.location.reload();
});
},
});
});
|