diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js index 568caedabe5..fb7ab69c18c 100644 --- a/addons/website_forum/static/src/js/website_forum.js +++ b/addons/website_forum/static/src/js/website_forum.js @@ -1,23 +1,153 @@ odoo.define('website_forum.website_forum', function (require) { - 'use strict'; +'use strict'; - require('web.dom_ready'); - var ajax = require('web.ajax'); - var core = require('web.core'); +var sAnimations = require('website.content.snippets.animation'); +var ajax = require('web.ajax'); +var core = require('web.core'); - var _t = core._t; +var _t = core._t; - var lastsearch; +var lastsearch; - if (!$('.website_forum').length) { - return $.Deferred().reject("DOM doesn't contain '.website_forum'"); - } +sAnimations.registry.websiteForum = sAnimations.Class.extend({ + selector: '.website_forum', + read_events: { + 'click .karma_required': '_onKarmaRequired', + 'hover .o_js_forum_tag_follow': '_onHoverTagFollowBox', + 'click .o_forum_profile_pic_edit': '_onEditProfilePic', + 'change .o_forum_file_upload': '_onChangeFileUpload', + 'click .o_forum_profile_pic_clear': '_onProfilePicClear', + 'hover .o_forum_user_info': '_onHoverUserInfo', + 'hover .o_forum_user_bio_expand': '_onHoverUserBioExpand', + 'click .flag:not(.karma_required)': '_onFlagAlert', + 'click .vote_up,.vote_down:not(.karma_required)': '_onVotePost', + 'click .o_js_validation_queue a[href*="/validate"]': '_onValidationQueue', + 'click .accept_answer:not(.karma_required)': '_onAcceptAnswer', + 'click .favourite_question': '_onFavQuestion', + 'click .comment_delete': '_onDeleteComment', + 'click .notification_close': '_onCloseNotification', + 'click .send_validation_email': '_onSendValidationEmail', + 'click .validated_email_close': '_onCloseValidatedEmail', + 'click .js_close_intro': '_onCloseIntro', + 'change .link_url, .o_forum_post_link': '_onCheckPostURL', + }, - // 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'); + /** + * @override + * @param {Object} parent + */ + start: function (parent) { + // 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'); - $("[data-toggle='popover']").popover(); - $('.karma_required').on('click', function (ev) { + // welcome message action button + var forum_login = _.string.sprintf('%s/web?redirect=%s', + window.location.origin, escape(window.location.href)); + $('.forum_register_url').attr('href',forum_login); + + $('input.js_select2').select2({ + tags: true, + tokenSeparators: [",", " ", "_"], + maximumInputLength: 35, + minimumInputLength: 2, + maximumSelectionSize: 5, + lastsearch: [], + createSearchChoice: function (term) { + if ($(lastsearch).filter(function () { return this.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 'New ' + _.escape(term.text); + } + else { + return _.escape(term.text); + } + }, + ajax: { + url: '/forum/get_tags', + dataType: 'json', + data: function (term) { + return { + q: term, + l: 50 + }; + }, + results: function (data) { + var ret = []; + _.each(data, function (x) { + ret.push({ id: x.id, text: x.name, isNew: false }); + }); + 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); + }, + }); + + $('textarea.load_editor').each(function () { + var $textarea = $(this); + var editor_karma = $textarea.data('karma') || 30; // default value for backward compatibility + if (!$textarea.val().match(/\S/)) { + $textarea.val("


"); + } + var $form = $textarea.closest('form'); + var toolbar = [ + ['style', ['style']], + ['font', ['bold', 'italic', 'underline', 'clear']], + ['para', ['ul', 'ol', 'paragraph']], + ['table', ['table']], + ['history', ['undo', 'redo']], + ]; + if (parseInt($("#karma").val()) >= editor_karma) { + toolbar.push(['insert', ['link', 'picture']]); + } + $textarea.summernote({ + height: 150, + toolbar: toolbar, + styleWithSpan: false + }); + + // float-left class messes up the post layout OPW 769721 + $form.find('.note-editable').find('img.float-left').removeClass('float-left'); + $form.on('click', 'button, .a-submit', function () { + $textarea.html($form.find('.note-editable').code()); + }); + }); + + // Check all answer status to add text-success on answer if needed + $('.accept_answer').each(function () { + if ($(this).hasClass('oe_answer_true')) { + $(this).addClass('text-success'); + } + }); + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + * @param {Object} ev + */ + _onKarmaRequired: function (ev) { var karma = $(ev.currentTarget).data('karma'); if (karma) { ev.preventDefault(); @@ -34,34 +164,46 @@ odoo.define('website_forum.website_forum', function (require) { } $(ev.currentTarget).after($warning); } - }); - - $('.o_js_forum_tag_follow').hover( - function(event) { + }, + /** + * @private + * @param {Object} ev + */ + _onHoverTagFollowBox: function (ev) { + $(function (ev) { $(this).find('.o_forum_tag_follow_box').stop().fadeIn().css('display','block'); - }, - function(event) { + }); + $(function (ev) { $(this).find('.o_forum_tag_follow_box').stop().fadeOut().css('display','none'); - }); - - $('.o_forum_profile_pic_edit').on('click', function(ev) { + }); + }, + /** + * @private + * @param {Object} ev + */ + _onEditProfilePic: function (ev) { ev.preventDefault(); $(this).closest('form').find('.o_forum_file_upload').trigger('click'); - }); - - $('.o_forum_file_upload').on('change', function() { + }, + /** + * @private + * @param {Object} ev + */ + _onChangeFileUpload: function (ev) { if (this.files.length) { var $form = $(this).closest('form'); var reader = new window.FileReader(); - reader.onload = function(ev) { + reader.onload = function (ev) { $form.find('.o_forum_avatar_img').attr('src', ev.target.result); }; reader.readAsDataURL(this.files[0]); $form.find('#forum_clear_image').remove(); } - }); - - $('.o_forum_profile_pic_clear').click(function() { + }, + /** + * @private + */ + _onProfilePicClear: function () { var $form = $(this).closest('form'); $form.find('.o_forum_avatar_img').attr("src", "/web/static/src/img/placeholder.png"); $form.append($('', { @@ -69,43 +211,49 @@ odoo.define('website_forum.website_forum', function (require) { id: 'forum_clear_image', type: 'hidden', })); - }); - - // Extended user biography toogle - $('.o_forum_user_info').hover( - function () { + }, + /** + * @private + */ + _onHoverUserInfo: function () { + $(function () { $(this).parent().find('.o_forum_user_bio_expand').delay(500).toggle('fast'); - }, - function () { + }); + $(function () { $(this).parent().find('.o_forum_user_bio_expand').clearQueue(); - } - ); - - $('.o_forum_user_bio_expand').hover( - function () {}, - function () { + }); + }, + /** + * @private + */ + _onHoverUserBioExpand: function () { + $(function () {}); + $(function () { $(this).fadeOut('fast'); - } - ); - - $('.flag').not('.karma_required').on('click', function (ev) { + }); + }, + /** + * @private + * @param {Object} ev + */ + _onFlagAlert: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc($link.data('href'), 'call', {}) .then(function (data) { - if(data.error) { + if (data.error) { var $warning; - if(data.error === 'anonymous_user') { + if (data.error === 'anonymous_user') { $warning = $('
'+ ''+ _t('Sorry you must be logged to flag a post') + '
'); - } else if(data.error === 'post_already_flagged') { + } else if (data.error === 'post_already_flagged') { $warning = $('
'+ ''+ _t('This post is already flagged') + '
'); - } else if(data.error === 'post_non_flaggable') { + } else if (data.error === 'post_non_flaggable') { $warning = $('
'+ ''+ _t('This post can not be flagged') + @@ -115,14 +263,14 @@ odoo.define('website_forum.website_forum', function (require) { if (flag_alert.length === 0) { $link.parent().append($warning); } - } else if(data.success) { + } else if (data.success) { var elem = $link; - if(data.success === 'post_flagged_moderator') { + if (data.success === 'post_flagged_moderator') { 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') { + } else if (data.success === 'post_flagged_non_moderator') { elem.html(' Flagged'); var forum_answer = elem.closest('.forum_answer'); forum_answer.fadeIn(1000); @@ -130,9 +278,12 @@ odoo.define('website_forum.website_forum', function (require) { } } }); - }); - - $('.vote_up,.vote_down').not('.karma_required').on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onVotePost: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc($link.data('href'), 'call', {}) @@ -141,12 +292,12 @@ odoo.define('website_forum.website_forum', function (require) { var $warning; if (data.error === 'own_post'){ $warning = $('
'+ - ''+ + ''+ _t('Sorry, you cannot vote for your own posts') + '
'); } else if (data.error === 'anonymous_user'){ $warning = $('
'+ - ''+ + ''+ _t('Sorry you must be logged to vote') + '
'); } @@ -168,28 +319,33 @@ odoo.define('website_forum.website_forum', function (require) { } } }); - }); - - $(".o_js_validation_queue a[href*='/validate']").on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onValidationQueue: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); var self = $(this); $(this).parents('.post_to_validate').hide(); $.get($link.attr('href')) - .fail(function() { + .fail(function () { self.parents('.o_js_validation_queue > div').addClass('bg-danger text-white').css('background-color', '#FAA'); self.parents('.post_to_validate').show(); }) - .done(function() { + .done(function () { 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); }); - - }); - - $('.accept_answer').not('.karma_required').on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onAcceptAnswer: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc($link.data('href'), 'call', {}).then(function (data) { @@ -206,39 +362,63 @@ odoo.define('website_forum.website_forum', function (require) { } } else { if (data) { - $(".oe_answer_true").addClass('oe_answer_false').removeClass("oe_answer_true"); $link.addClass("oe_answer_true").removeClass('oe_answer_false'); } else { $link.removeClass("oe_answer_true").addClass('oe_answer_false'); } } }); - }); - - $('.favourite_question').on('click', function (ev) { + this._onCheckAnswerStatus(ev); + }, + /** + * @private + * @param {Object} ev + */ + _onCheckAnswerStatus: function (ev) { + var $link = $(ev.currentTarget); + if ($link.hasClass('oe_answer_true')) { + $link.removeClass('text-success'); + } else { + $link.addClass('text-success'); + } + }, + /** + * @private + * @param {Object} ev + */ + _onFavQuestion: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc($link.data('href'), 'call', {}).then(function (data) { $link.toggleClass("forum_favourite_question", !!data); }); - }); - - $('.comment_delete').on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onDeleteComment: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc($link.closest('form').attr('action'), 'call', {}).then(function () { $link.parents('.comment').first().remove(); }); - }); - - $('.notification_close').on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onCloseNotification: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc("/forum/notification_read", 'call', { 'notification_id': $link.attr("id")}); - }); - - $('.send_validation_email').on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onSendValidationEmail: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); ajax.jsonRpc("/forum/send_validation_email", 'call', { @@ -248,35 +428,37 @@ odoo.define('website_forum.website_forum', function (require) { $('button.validation_email_close').click(); } }); - }); - - $('.validated_email_close').on('click', function (ev) { + }, + /** + * @private + */ + _onCloseValidatedEmail: function () { ajax.jsonRpc("/forum/validate_email/close", 'call', {}); - }); - - // welcome message action button - var forum_login = _.string.sprintf('%s/web?redirect=%s', - window.location.origin, escape(window.location.href)); - $('.forum_register_url').attr('href',forum_login); - - $('.js_close_intro').on('click', function (ev) { + }, + /** + * @private + * @param {Object} ev + */ + _onCloseIntro: function (ev) { ev.preventDefault(); document.cookie = "forum_welcome_message = false"; $('.forum_intro').slideUp(); return true; - }); - - $('.link_url, .o_forum_post_link').on('change', function (ev) { // keep .link_url for compat + }, + /** + * @private + * @param {Object} ev + */ + _onCheckPostURL: function (ev) { ev.preventDefault(); var $link = $(ev.currentTarget); - var display_error = function(){ + var display_error = function (){ var $warning = $('
'+ ''+ 'Please enter valid URL. Example: http://www.odoo.com'+ '
'); $link.parent().append($warning); $link.parents('form').find('button')[0].disabled = true; - }; var url = $link.val(); if (url.search("^http(s?)://.*")) { @@ -321,7 +503,7 @@ odoo.define('website_forum.website_forum', function (require) { "$", "i" ); - if(regex.test(url)){ + if (regex.test(url)){ ajax.jsonRpc("/forum/get_url_title", 'call', {'url': url}).then(function (data) { if (data) { $("input[name='post_name']")[0].value = data; @@ -334,92 +516,7 @@ odoo.define('website_forum.website_forum', function (require) { } else { display_error(); } - }); - - $('input.js_select2').select2({ - tags: true, - tokenSeparators: [",", " ", "_"], - maximumInputLength: 35, - minimumInputLength: 2, - maximumSelectionSize: 5, - lastsearch: [], - createSearchChoice: function (term) { - if ($(lastsearch).filter(function () { return this.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 'New ' + _.escape(term.text); - } - else { - return _.escape(term.text); - } - }, - ajax: { - url: '/forum/get_tags', - dataType: 'json', - data: function(term) { - return { - q: term, - l: 50 - }; - }, - results: function(data) { - var ret = []; - _.each(data, function(x) { - ret.push({ id: x.id, text: x.name, isNew: false }); - }); - 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); - }, - }); - - $('textarea.load_editor').each(function () { - var $textarea = $(this); - var editor_karma = $textarea.data('karma') || 30; // default value for backward compatibility - if (!$textarea.val().match(/\S/)) { - $textarea.val("


"); - } - var $form = $textarea.closest('form'); - var toolbar = [ - ['style', ['style']], - ['font', ['bold', 'italic', 'underline', 'clear']], - ['para', ['ul', 'ol', 'paragraph']], - ['table', ['table']], - ['history', ['undo', 'redo']], - ]; - if (parseInt($("#karma").val()) >= editor_karma) { - toolbar.push(['insert', ['link', 'picture']]); - } - $textarea.summernote({ - height: 150, - toolbar: toolbar, - styleWithSpan: false - }); - - // float-left class messes up the post layout OPW 769721 - $form.find('.note-editable').find('img.float-left').removeClass('float-left'); - $form.on('click', 'button, .a-submit', function () { - $textarea.html($form.find('.note-editable').code()); - }); - }); + }, +}); }); diff --git a/addons/website_forum/static/src/js/website_forum.share.js b/addons/website_forum/static/src/js/website_forum.share.js index e55685fc8d7..8c694304517 100644 --- a/addons/website_forum/static/src/js/website_forum.share.js +++ b/addons/website_forum/static/src/js/website_forum.share.js @@ -1,36 +1,48 @@ odoo.define('website_forum.share', function (require) { 'use strict'; -require('web.dom_ready'); +var sAnimations = require('website.content.snippets.animation'); var core = require('web.core'); -var base = require('web_editor.base'); -var sAnimation = require('website.content.snippets.animation'); - var qweb = core.qweb; -if (!$('.website_forum').length) { - return $.Deferred().reject("DOM doesn't contain '.website_forum'"); -} - -// FIXME There is no reason to inherit from socialShare here -var ForumShare = sAnimation.registry.socialShare.extend({ - xmlDependencies: sAnimation.registry.socialShare.prototype.xmlDependencies +var ForumShare = sAnimations.registry.socialShare.extend({ + xmlDependencies: sAnimations.registry.socialShare.prototype.xmlDependencies .concat(['/website_forum/static/src/xml/website_forum_share_templates.xml']), - read_events: {}, + /** + * @override + * @param {Object} parent + * @param {Boolean} editableMode + * @param {String} targetType + */ init: function (parent, editableMode, targetType) { this._super.apply(this, arguments); this.targetType = targetType; }, - start: function () { + /** + * @override + * @param {Object} parent + */ + start: function (parent) { var def = this._super.apply(this, arguments); this._onMouseEnter(); return def; }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + */ _bindSocialEvent: function () { this._super.apply(this, arguments); $('.oe_share_bump').click($.proxy(this._postBump, this)); }, + /** + * @private + */ _render: function () { if (!this.targetType) { this._super.apply(this, arguments); @@ -41,6 +53,9 @@ var ForumShare = sAnimation.registry.socialShare.extend({ $('#oe_social_share_modal').modal('show'); } }, + /** + * @private + */ _postBump: function () { this._rpc({ // FIXME route: '/forum/post/bump', @@ -49,27 +64,48 @@ var ForumShare = sAnimation.registry.socialShare.extend({ }, }); }, + }); -base.ready().then(function () { - // Store social share data to display modal on next page - $(document.body).on('click', ':not(.karma_required).oe_social_share_call', function () { +sAnimations.registry.websiteForumShare = sAnimations.Class.extend({ + selector: '.website_forum', + read_events: { + 'click :not(.karma_required).oe_social_share_call': '_onBody' + }, + + /** + * @override + * @param {Object} parent + */ + start: function (parent) { + // Retrieve stored social data + if (sessionStorage.getItem('social_share')) { + var socialData = JSON.parse(sessionStorage.getItem('social_share')); + (new ForumShare(null, false, socialData.targetType)).attachTo($(document.body)); + sessionStorage.removeItem('social_share'); + } + // Display an alert if post has no reply and is older than 10 days + var $questionContainer = $('.oe_js_bump'); + if ($questionContainer.length) { + new ForumShare(null, false, 'social-alert').attachTo($questionContainer); + } + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * Store social share data to display modal on next page + * + * @override + */ + _onBody: function () { sessionStorage.setItem('social_share', JSON.stringify({ targetType: $(this).data('social-target-type'), })); - }); + }, - // Retrieve stored social data - if (sessionStorage.getItem('social_share')) { - var socialData = JSON.parse(sessionStorage.getItem('social_share')); - (new ForumShare(null, false, socialData.targetType)).attachTo($(document.body)); - sessionStorage.removeItem('social_share'); - } +}); - // Display an alert if post has no reply and is older than 10 days - var $questionContainer = $('.oe_js_bump'); - if ($questionContainer.length) { - new ForumShare(null, false, 'social-alert').attachTo($questionContainer); - } -}); });