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
|
odoo.define('website_forum.editor', function (require) {
"use strict";
var core = require('web.core');
var WebsiteNewMenu = require('website.newMenu');
var Dialog = require('web.Dialog');
var _t = core._t;
var ForumCreateDialog = Dialog.extend({
xmlDependencies: Dialog.prototype.xmlDependencies.concat(
['/website_forum/static/src/xml/website_forum_templates.xml']
),
template: 'website_forum.add_new_forum',
events: _.extend({}, Dialog.prototype.events, {
'change input[name="privacy"]': '_onPrivacyChanged',
}),
/**
* @override
* @param {Object} parent
* @param {Object} options
*/
init: function (parent, options) {
options = _.defaults(options || {}, {
title: _t("New Forum"),
size: 'medium',
buttons: [
{
text: _t("Create"),
classes: 'btn-primary',
click: this.onCreateClick.bind(this),
},
{
text: _t("Discard"),
close: true
},
]
});
this._super(parent, options);
},
start: function () {
var self = this;
return this._super.apply(this, arguments).then(function () {
var $input = self.$('#group_id');
$input.select2({
width: '100%',
allowClear: true,
formatNoMatches: false,
multiple: false,
selection_data: false,
fill_data: function (query, data) {
var that = this;
var tags = {results: []};
_.each(data, function (obj) {
if (that.matcher(query.term, obj.display_name)) {
tags.results.push({id: obj.id, text: obj.display_name});
}
});
query.callback(tags);
},
query: function (query) {
var that = this;
// fetch data only once and store it
if (!this.selection_data) {
self._rpc({
model: 'res.groups',
method: 'search_read',
args: [[], ['display_name']],
}).then(function (data) {
that.fill_data(query, data);
that.selection_data = data;
});
} else {
this.fill_data(query, this.selection_data);
}
}
});
});
},
onCreateClick: function () {
var $dialog = this.$el;
var $forumName = $dialog.find('input[name=forum_name]');
if (!$forumName.val()) {
$forumName.addClass('border-danger');
return;
}
var $forumPrivacyGroup = $dialog.find('input[name=group_id]');
var forumPrivacy = $dialog.find('input:radio[name=privacy]:checked').val();
if (forumPrivacy === 'private' && !$forumPrivacyGroup.val()) {
this.$("#group-required").removeClass('d-none');
return;
}
var addMenu = ($dialog.find('input[type="checkbox"]').is(':checked'));
var forumMode = $dialog.find('input:radio[name=mode]:checked').val();
return this._rpc({
route: '/forum/new',
params: {
forum_name: $forumName.val(),
forum_mode: forumMode,
forum_privacy: forumPrivacy,
forum_privacy_group: $forumPrivacyGroup.val(),
add_menu: addMenu || "",
},
}).then(function (url) {
window.location.href = url;
return new Promise(function () {});
});
},
/**
* @private
*/
_onPrivacyChanged: function (ev) {
this.$('.show_visibility_group').toggleClass('d-none', ev.target.value !== 'private');
},
});
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
* @returns {Promise} Unresolved if there is a redirection
*/
_createNewForum: function () {
var self = this;
var def = new Promise(function (resolve) {
var dialog = new ForumCreateDialog(self, {});
dialog.open();
dialog.on('closed', self, resolve);
});
return def;
},
});
});
|