diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website_slides/static/src/js/slides_course_join.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_slides/static/src/js/slides_course_join.js')
| -rw-r--r-- | addons/website_slides/static/src/js/slides_course_join.js | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/addons/website_slides/static/src/js/slides_course_join.js b/addons/website_slides/static/src/js/slides_course_join.js new file mode 100644 index 00000000..0817f372 --- /dev/null +++ b/addons/website_slides/static/src/js/slides_course_join.js @@ -0,0 +1,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 +}; + +}); |
