summaryrefslogtreecommitdiff
path: root/addons/website_forum
diff options
context:
space:
mode:
authorqsm-odoo <qsm@odoo.com>2017-06-29 16:38:34 +0200
committerqsm-odoo <qsm@odoo.com>2017-08-16 11:04:14 +0200
commit2972976962617d4b8a0113bae58c640ab41cdff8 (patch)
treec27d107b597dec85ad1ecd02553982dc0da657de /addons/website_forum
parent4f925af06fd3cebfcfb56eb947ab68c8a2ca4490 (diff)
[REF] web_editor, website, *: complete refactoring
* mass_mailing, payment, point_of_sale, portal, survey, web, web_tour, website_blog, website_crm_partner_assign, website_event, website_event_questions, website_form, website_forum, website_gengo, website_hr_recruitment, website_links, website_mail, website_mail_channel, website_mass_mailing, website_quote, website_sale, website_sale_options, website_slides, website_twitter This commit reviews the whole "JS side" of the web_editor and website apps. This is a first step to be able to improve them with new and better functionnalities; this commit is not supposed to change any visual behavior. The main goal was to achieve a structure similar to the backend one. Now, the frontend side also has a root widget (like the WebClient) and all other widgets are attached to it one way or another. This allows the benefits of using the 'trigger_up' functionnality for example. As RPC are now mainly done with the `this._rpc` functionnality (being possible thanks to the parent hierarchy), the frontend will also be possible to test thanks to QUnit in a future update (besides the "text" editor side which still requires a refactoring to be able to do that). --- Here are some of the changes: (-) conventions and documentation The code has been updated to follow JS conventions and a lot of code has been commented (around +2000 lines of comment). This also means that lots of functions have been renamed to use camelCase or simply to make their name understandable. See https://github.com/odoo/odoo/wiki/Javascript-coding-guidelines. (-) deprecated: web_editor.base The "web_editor.base" module has been split and does not force the modules which require it to wait for DOM ready anymore. This was indeed slowing loading times, but also prevented to use some modules in some contexts (see the LESS editor use in web_studio which is the subject of another task). Now the "editor context" can be got thanks to the "web_editor.context" JS module with its "get" function. The "web_editor.base" module should probably not be used anymore (see its code and recent updates). (-) new: web.dom_ready If a JS module should wait for the DOM to be ready to be executed, a new JS module has been created: "web.dom_ready". This should always be used in a module which only want to instantiate stuff. Do not extend (or worst, include) classes after DOM ready. (-) website.website The "website.website" module has been split. "website.website" does not return anything useful anymore, it just initialize some miscellaneous stuff, without waiting for the DOM to be ready. You might want to check "website.utils", "website.content.compatibility" or `WebsiteRoot`. Also `website.form` has been deleted (use `this._rpc`), so has been `website.error`. `website.prompt` will be removed in a future update to be replaced by `Dialog.prompt`. (-) widgets are great Many classes which were not widgets are now widgets. This allows them to use the 'events', the 'xmlDependencies' and the 'this._rpc' features for example. Here are some of the main ones: - Snippet options: these were classes with a `$el` for the menu element and `$target` for the customized element. This is still the case but following standard `Widget` structure (one exception: using `this.$(...)` searches in the `$target` as before this update). - Snippet animations: instead of class instances with a `$target` element which can be `start` and `stop`, these are now standard widgets which can be `start` and `destroy`. `this.$target` is an alias to `this.$el` for ease of compatibility. - Snippet editors: instead of class instances in charge of an editor overlay, these are now widgets. Each "child" snippet editor is properly attached as a "child", which allows editors to communicate and to be properly destroyed. (-) root widgets and website navbar The frontend is different of the backend. In the backend, the page has an empty <body/> element and all the components are instantiated from parent to children (i.e. the `WebClient` is instantiated and is in charge of instantiating the `ControlPanel`, etc). The frontend cannot work like that on page loadings as they are way more frequent than in the backend and we do not want them to flicker. A frontend page is loaded as a <body/> element which already contains the website navbar and its menus and the whole content page. JS code has to be "attached" to these existing elements. This is possible thanks to the `RootWidget` instances and the specialized `WebsiteRoot`, `IframeRoot` and `WebsiteNavbar` (see code for details). (-) lazy loading No more (or at least a lot less) XML/JS has to be loaded on page loading, thanks to the use of the `Widget.xmlDependencies` feature. XML which have to be lazy loaded is loaded only on related Widget instantiation if necessary, which allows to execute a lot of JS code before the DOM is ready and to start many widgets on DOM ready (not later). A visual benefit of this is clicking on the 'edit' button as soon as it is possible: before this commit, this was sometimes not doing anything as event handlers were not binded yet. Still a possible exception: loading the session and locales. This may be asynchronous stuff which is still required before widget instantiations but this will be the subject of another task. (-) deprecated code and code location More than reviewing code and organizing it, many apparent dead code was removed. More importantly, mislocated code was put in the right app. This is the case for snippet animations which is a concept for website apps but was defined in the web_editor app, or some translation concepts which were part of website but should have been part of web_editor. --- There are probably more things to say about this commit but I will let the comments speak for those.
Diffstat (limited to 'addons/website_forum')
-rw-r--r--addons/website_forum/controllers/main.py4
-rw-r--r--addons/website_forum/data/forum_demo.xml2
-rw-r--r--addons/website_forum/static/src/js/website_forum.editor.js37
-rw-r--r--addons/website_forum/static/src/js/website_forum.js57
-rw-r--r--addons/website_forum/static/src/js/website_forum.share.js85
-rw-r--r--addons/website_forum/views/website_forum.xml2
6 files changed, 103 insertions, 84 deletions
diff --git a/addons/website_forum/controllers/main.py b/addons/website_forum/controllers/main.py
index 6ee21087d80..1344f641f3f 100644
--- a/addons/website_forum/controllers/main.py
+++ b/addons/website_forum/controllers/main.py
@@ -83,7 +83,7 @@ class WebsiteForum(http.Controller):
forums = request.env['forum.forum'].search([])
return request.render("website_forum.forum_all", {'forums': forums})
- @http.route('/forum/new', type='http', auth="user", methods=['POST'], website=True)
+ @http.route('/forum/new', type='json', auth="user", methods=['POST'], website=True)
def forum_create(self, forum_name="New Forum", add_menu=False):
forum_id = request.env['forum.forum'].create({'name': forum_name})
if add_menu:
@@ -93,7 +93,7 @@ class WebsiteForum(http.Controller):
'parent_id': request.website.menu_id.id,
'website_id': request.website.id,
})
- return request.redirect("/forum/%s" % slug(forum_id))
+ return "/forum/%s" % slug(forum_id)
@http.route('/forum/notification_read', type='json', auth="user", methods=['POST'], website=True)
def notification_read(self, **kwargs):
diff --git a/addons/website_forum/data/forum_demo.xml b/addons/website_forum/data/forum_demo.xml
index 4ec354c5766..3a3bd5a8236 100644
--- a/addons/website_forum/data/forum_demo.xml
+++ b/addons/website_forum/data/forum_demo.xml
@@ -62,7 +62,7 @@ In simple words does Odoo became CMS+ERP platform?</p>]]></field>
<record id="answer_1" model="forum.post">
<field name="forum_id" ref="website_forum.forum_help"/>
<field name="content"><![CDATA[
-<p><span class="oe_social_share" data-social="twitter linkedin facebook" data-hashtags="#odoo #CMS">Odoo v8 (next release) provides a web module and an e-commerce module: www.odoo.com/page/website-builder</span>
+<p>Odoo v8 (next release) provides a web module and an e-commerce module: www.odoo.com/page/website-builder
The CMS editor in Odoo web is nice but I prefer Drupal for customization and there is a Drupal module for Odoo. I think WP is better than Odoo web too.
</p>]]></field>
<field name="parent_id" ref="question_1"/>
diff --git a/addons/website_forum/static/src/js/website_forum.editor.js b/addons/website_forum/static/src/js/website_forum.editor.js
index cecdc2df992..895e11ea596 100644
--- a/addons/website_forum/static/src/js/website_forum.editor.js
+++ b/addons/website_forum/static/src/js/website_forum.editor.js
@@ -2,14 +2,29 @@ odoo.define('website_forum.editor', function (require) {
"use strict";
var core = require('web.core');
-var contentMenu = require('website.contentMenu');
-var website = require('website.website');
+var wUtils = require('website.utils');
+var WebsiteNewMenu = require("website.newMenu");
var _t = core._t;
-contentMenu.TopBar.include({
- new_forum: function() {
- website.prompt({
+WebsiteNewMenu.include({
+ actions: _.extend({}, WebsiteNewMenu.prototype.actions || {}, {
+ new_forum: '_createNewForum',
+ }),
+
+ //----------------------------------------------------------------------
+ // Actions
+ //----------------------------------------------------------------------
+
+ /**
+ * Asks the user information about a new forum to create, then creates it
+ * and redirects the user to this new forum.
+ *
+ * @private
+ */
+ _createNewForum: function () {
+ var self = this;
+ wUtils.prompt({
id: "editor_new_forum",
window_title: _t("New Forum"),
input: "Forum Name",init: function () {
@@ -27,12 +42,16 @@ contentMenu.TopBar.include({
}
}).then(function (forum_name, field, $dialog) {
var add_menu = ($dialog.find('input[type="checkbox"]').is(':checked'));
- website.form('/forum/new', 'POST', {
- forum_name: forum_name,
- add_menu: add_menu || ""
+ self._rpc({
+ route: '/forum/new',
+ params: {
+ forum_name: forum_name,
+ add_menu: add_menu || "",
+ },
+ }).then(function (url) {
+ window.location.href = url;
});
});
},
});
-
});
diff --git a/addons/website_forum/static/src/js/website_forum.js b/addons/website_forum/static/src/js/website_forum.js
index f50b2c6ac1d..1fe31c2498d 100644
--- a/addons/website_forum/static/src/js/website_forum.js
+++ b/addons/website_forum/static/src/js/website_forum.js
@@ -1,32 +1,32 @@
odoo.define('website_forum.website_forum', function (require) {
-'use strict';
+ 'use strict';
-var ajax = require('web.ajax');
-var core = require('web.core');
-var website = require('website.website');
+ require('web.dom_ready');
+ 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'");
-}
+ if (!$('.website_forum').length) {
+ return $.Deferred().reject("DOM doesn't contain '.website_forum'");
+ }
$("[data-toggle='popover']").popover();
$('.karma_required').on('click', function (ev) {
var karma = $(ev.currentTarget).data('karma');
if (karma) {
ev.preventDefault();
- var msg = karma + ' ' + _t(' karma is required to perform this action. You can earn karma by having your answers upvoted by the community.')
+ var msg = karma + ' ' + _t(' karma is required to perform this action. You can earn karma by having your answers upvoted by the community.');
if ($('a[href*="/login"]').length) {
msg = _t('Sorry you must be logged in to perform this action');
- };
+ }
var $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="karma_alert">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
msg + '</div>');
var vote_alert = $(ev.currentTarget).parent().find("#vote_alert");
- if (vote_alert.length == 0) {
+ if (vote_alert.length === 0) {
$(ev.currentTarget).parent().append($warning);
}
}
@@ -48,7 +48,7 @@ if(!$('.website_forum').length) {
$('.o_forum_file_upload').on('change', function() {
if (this.files.length) {
var $form = $(this).closest('form');
- var reader = new FileReader();
+ var reader = new window.FileReader();
reader.onload = function(ev) {
$form.find('.o_forum_avatar_img').attr('src', ev.target.result);
};
@@ -91,17 +91,17 @@ if(!$('.website_forum').length) {
.then(function (data) {
if(data.error) {
var $warning;
- if(data.error == 'anonymous_user') {
+ if(data.error === 'anonymous_user') {
$warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="flag_alert">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
_t('Sorry you must be logged to flag a post') +
'</div>');
- } else if(data.error == 'post_already_flagged') {
+ } else if(data.error === 'post_already_flagged') {
$warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="flag_alert">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
_t('This post is already flagged') +
'</div>');
- } else if(data.error == 'post_non_flaggable') {
+ } else if(data.error === 'post_non_flaggable') {
$warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="flag_alert">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
_t('This post can not be flagged') +
@@ -113,12 +113,12 @@ if(!$('.website_forum').length) {
}
} 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);
@@ -134,13 +134,14 @@ if(!$('.website_forum').length) {
ajax.jsonRpc($link.data('href'), 'call', {})
.then(function (data) {
if (data.error){
- if (data.error == 'own_post'){
- var $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="vote_alert">'+
+ var $warning;
+ if (data.error === 'own_post'){
+ $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="vote_alert">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
_t('Sorry, you cannot vote for your own posts') +
'</div>');
- } else if (data.error == 'anonymous_user'){
- var $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="vote_alert">'+
+ } else if (data.error === 'anonymous_user'){
+ $warning = $('<div class="alert alert-danger alert-dismissable oe_forum_alert" id="vote_alert">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
_t('Sorry you must be logged to vote') +
'</div>');
@@ -189,14 +190,14 @@ if(!$('.website_forum').length) {
var $link = $(ev.currentTarget);
ajax.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
if (data.error) {
- if (data.error == 'anonymous_user') {
+ if (data.error === 'anonymous_user') {
var $warning = $('<div class="alert alert-danger alert-dismissable" id="correct_answer_alert" style="position:absolute; margin-top: -30px; margin-left: 90px;">'+
'<button type="button" class="close notification_close" data-dismiss="alert" aria-hidden="true">&times;</button>'+
_t('Sorry, anonymous users cannot choose correct answer.') +
'</div>');
}
var correct_answer_alert = $link.parent().find("#correct_answer_alert");
- if (correct_answer_alert.length == 0) {
+ if (correct_answer_alert.length === 0) {
$link.parent().append($warning);
}
} else {
@@ -214,11 +215,7 @@ if(!$('.website_forum').length) {
ev.preventDefault();
var $link = $(ev.currentTarget);
ajax.jsonRpc($link.data('href'), 'call', {}).then(function (data) {
- if (data) {
- $link.addClass("forum_favourite_question")
- } else {
- $link.removeClass("forum_favourite_question")
- }
+ $link.toggleClass("forum_favourite_question", !!data);
});
});
@@ -418,4 +415,4 @@ if(!$('.website_forum').length) {
});
});
-}); \ No newline at end of file
+});
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 5033a0ea624..e55685fc8d7 100644
--- a/addons/website_forum/static/src/js/website_forum.share.js
+++ b/addons/website_forum/static/src/js/website_forum.share.js
@@ -1,72 +1,75 @@
odoo.define('website_forum.share', function (require) {
'use strict';
-var ajax = require('web.ajax');
+require('web.dom_ready');
var core = require('web.core');
var base = require('web_editor.base');
-var SocialShare = require('website.share');
-var website = require('website.website');
-var qweb = core.qweb;
-var tmpl = ajax.loadXML('/website_forum/static/src/xml/website_forum_share_templates.xml', qweb);
+var sAnimation = require('website.content.snippets.animation');
+var qweb = core.qweb;
-if(!$('.website_forum').length) {
+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
+ .concat(['/website_forum/static/src/xml/website_forum_share_templates.xml']),
+ read_events: {},
-var ForumShare = SocialShare.extend({
- init: function (parent, target_type) {
- this.target_type = target_type;
- this._super(parent);
+ init: function (parent, editableMode, targetType) {
+ this._super.apply(this, arguments);
+ this.targetType = targetType;
},
- bind_events: function () {
+ start: function () {
+ var def = this._super.apply(this, arguments);
+ this._onMouseEnter();
+ return def;
+ },
+ _bindSocialEvent: function () {
this._super.apply(this, arguments);
- $('.oe_share_bump').click($.proxy(this.post_bump, this));
+ $('.oe_share_bump').click($.proxy(this._postBump, this));
},
- renderElement: function () {
- if (! this.target_type) {
- this._super();
- }
- else if (this.target_type == 'social-alert') {
- $('.row .question').before(qweb.render('website.social_alert', {medias: this.social_list}));
- }
- else {
- this.template = 'website.social_modal';
- $('body').append(qweb.render(this.template, {medias: this.social_list, target_type: this.target_type}));
+ _render: function () {
+ if (!this.targetType) {
+ this._super.apply(this, arguments);
+ } else if (this.targetType === 'social-alert') {
+ $('.row .question').before(qweb.render('website.social_alert', {medias: this.socialList}));
+ } else {
+ $('body').append(qweb.render('website.social_modal', {medias: this.socialList, target_type: this.targetType}));
$('#oe_social_share_modal').modal('show');
}
},
- post_bump: function () {
- ajax.jsonRpc('/forum/post/bump', 'call', {
- 'post_id': this.element.data('id'),
+ _postBump: function () {
+ this._rpc({ // FIXME
+ route: '/forum/post/bump',
+ params: {
+ post_id: this.element.data('id'),
+ },
});
- }
+ },
});
-$.when(base.ready(), tmpl).done(function() {
-
+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() {
- var social_data = {};
- social_data['target_type'] = $(this).data('social-target-type');
- sessionStorage.setItem('social_share', JSON.stringify(social_data));
+ $(document.body).on('click', ':not(.karma_required).oe_social_share_call', function () {
+ sessionStorage.setItem('social_share', JSON.stringify({
+ targetType: $(this).data('social-target-type'),
+ }));
});
// Retrieve stored social data
- if(sessionStorage.getItem('social_share')){
- var social_data = JSON.parse(sessionStorage.getItem('social_share'));
- new ForumShare($(this), social_data['target_type']);
+ 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
- if ($('.oe_js_bump').length) {
- var $question_container = $('.oe_js_bump');
- new ForumShare($question_container, 'social-alert');
+ var $questionContainer = $('.oe_js_bump');
+ if ($questionContainer.length) {
+ new ForumShare(null, false, 'social-alert').attachTo($questionContainer);
}
-
});
-return {};
-
});
diff --git a/addons/website_forum/views/website_forum.xml b/addons/website_forum/views/website_forum.xml
index ce9f37dcf2d..2bd5e38cdc3 100644
--- a/addons/website_forum/views/website_forum.xml
+++ b/addons/website_forum/views/website_forum.xml
@@ -1561,7 +1561,7 @@
<!-- User Navbar -->
<template id="content_new_forum" inherit_id="website.user_navbar">
- <xpath expr="//div[@id='o_website_add_page_modal']//ul" position="inside">
+ <xpath expr="//div[@id='o_new_content_menu_choices']//ul" position="inside">
<li groups="website.group_website_designer"><a href="#" data-action="new_forum"><i class="fa fa-comment"/><p>New Forum</p></a></li>
</xpath>
</template>