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
|
odoo.define('website_slides.course.join.widget', function (require) {
'use strict';
var core = require('web.core');
var publicWidget = require('web.public.widget');
var _t = core._t;
var CourseJoinWidget = publicWidget.Widget.extend({
template: 'slide.course.join',
xmlDependencies: ['/website_slides/static/src/xml/slide_course_join.xml'],
events: {
'click .o_wslides_js_course_join_link': '_onClickJoin',
},
/**
*
* Overridden to add options parameters.
*
* @param {Object} parent
* @param {Object} options
* @param {Object} options.channel slide.channel information
* @param {boolean} options.isMember whether current user is member or not
* @param {boolean} options.publicUser whether current user is public or not
* @param {string} [options.joinMessage] the message to use for the simple join case
* when the course if free and the user is logged in, defaults to "Join Course".
* @param {Promise} [options.beforeJoin] a promise to execute before we redirect to
* another url within the join process (login / buy course / ...)
* @param {function} [options.afterJoin] a callback function called after the user has
* joined the course
*/
init: function (parent, options) {
this._super.apply(this, arguments);
this.channel = options.channel;
this.isMember = options.isMember;
this.publicUser = options.publicUser;
this.joinMessage = options.joinMessage || _t('Join Course'),
this.beforeJoin = options.beforeJoin || Promise.resolve();
this.afterJoin = options.afterJoin || function () {document.location.reload();};
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param {MouseEvent} ev
*/
_onClickJoin: function (ev) {
ev.preventDefault();
if (this.channel.channelEnroll !== 'invite') {
if (this.publicUser) {
this.beforeJoin().then(this._redirectToLogin.bind(this));
} else if (!this.isMember && this.channel.channelEnroll === 'public') {
this.joinChannel(this.channel.channelId);
}
}
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Builds a login page that then redirects to this slide page, or the channel if the course
* is not configured as public enroll type.
*
* @private
*/
_redirectToLogin: function () {
var url;
if (this.channel.channelEnroll === 'public') {
url = window.location.pathname;
if (document.location.href.indexOf("fullscreen") !== -1) {
url += '?fullscreen=1';
}
} else {
url = `/slides/${this.channel.channelId}`;
}
document.location = _.str.sprintf('/web/login?redirect=%s', encodeURIComponent(url));
},
/**
* @private
* @param {Object} $el
* @param {String} message
*/
_popoverAlert: function ($el, message) {
$el.popover({
trigger: 'focus',
placement: 'bottom',
container: 'body',
html: true,
content: function () {
return message;
}
}).popover('show');
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @public
* @param {integer} channelId
*/
joinChannel: function (channelId) {
var self = this;
this._rpc({
route: '/slides/channel/join',
params: {
channel_id: channelId,
},
}).then(function (data) {
if (!data.error) {
self.afterJoin();
} else {
if (data.error === 'public_user') {
var message = _t('Please <a href="/web/login?redirect=%s">login</a> to join this course');
var signupAllowed = data.error_signup_allowed || false;
if (signupAllowed) {
message = _t('Please <a href="/web/signup?redirect=%s">create an account</a> to join this course');
}
self._popoverAlert(self.$el, _.str.sprintf(message, (document.URL)));
} else if (data.error === 'join_done') {
self._popoverAlert(self.$el, _t('You have already joined this channel'));
} else {
self._popoverAlert(self.$el, _t('Unknown error'));
}
}
});
},
});
publicWidget.registry.websiteSlidesCourseJoin = publicWidget.Widget.extend({
selector: '.o_wslides_js_course_join_link',
/**
* @override
* @param {Object} parent
*/
start: function () {
var self = this;
var proms = [this._super.apply(this, arguments)];
var data = self.$el.data();
var options = {channel: {channelEnroll: data.channelEnroll, channelId: data.channelId}};
$('.o_wslides_js_course_join').each(function () {
proms.push(new CourseJoinWidget(self, options).attachTo($(this)));
});
return Promise.all(proms);
},
});
return {
courseJoinWidget: CourseJoinWidget,
websiteSlidesCourseJoin: publicWidget.registry.websiteSlidesCourseJoin
};
});
|