/* global $, APP, interfaceConfig, AJS */ /* jshint -W101 */ const selector = '#aui-feedback-dialog'; /** * Toggles the appropriate css class for the given number of stars, to * indicate that those stars have been clicked/selected. * * @param starCount the number of stars, for which to toggle the css class */ let toggleStars = function(starCount) { $('#stars > a').each(function(index, el) { if (index <= starCount) { el.classList.add("starHover"); } else el.classList.remove("starHover"); }); }; /** * Constructs the html for the detailed feedback window. * * @returns {string} the contructed html string */ let constructDetailedFeedbackHtml = function() { return `

${APP.translation.translateString("dialog.sorryFeedback")}



`; }; /** * Constructs the html for the rated feedback window. * * @returns {string} the contructed html string */ let createRateFeedbackHTML = function (Feedback) { let rateExperience = APP.translation.translateString('dialog.rateExperience'), feedbackHelp = APP.translation.translateString('dialog.feedbackHelp'), feedbackQuestion = (Feedback.feedbackScore < 0) ? `


${APP.translation.translateString('dialog.feedbackQuestion')}

` : ''; let starClassName = (interfaceConfig.ENABLE_FEEDBACK_ANIMATION) ? "icon-star shake-rotate" : "icon-star"; return `
${feedbackQuestion}
`; }; /** * Callback for Rate Feedback * * @param Feedback */ let onLoadRateFunction = function (Feedback) { $('#stars > a').each((index, el) => { el.onmouseover = function(){ toggleStars(index); }; el.onmouseleave = function(){ toggleStars(Feedback.feedbackScore - 1); }; el.onclick = function(){ Feedback.feedbackScore = index + 1; // If the feedback is less than 3 stars we're going to // ask the user for more information. if (Feedback.feedbackScore > 3) { APP.conference.sendFeedback(Feedback.feedbackScore, ""); Feedback.hide(); } else { Feedback.setState('detailed_feedback'); } }; }); // Init stars to correspond to previously entered feedback. if (Feedback.feedbackScore > 0) { toggleStars(Feedback.feedbackScore - 1); } }; /** * Callback for Detailed Feedback * * @param Feedback */ let onLoadDetailedFunction = function(Feedback) { let submitBtn = Feedback.$el.find('#dialog-submit-button'); let closeBtn = Feedback.$el.find('#dialog-close-button'); if (submitBtn && submitBtn.length) { submitBtn.on('click', (e) => { e.preventDefault(); Feedback.onFeedbackSubmitted(); }); } if (closeBtn && closeBtn.length) { closeBtn.on('click', (e) => { e.preventDefault(); Feedback.hide(); }); } $('#feedbackTextArea').focus(); }; /** * @class Dialog * */ export default class Dialog { constructor(options) { this.feedbackScore = -1; this.onCloseCallback = null; this.states = { rate_feedback: { getHtml: createRateFeedbackHTML, onLoad: onLoadRateFunction }, detailed_feedback: { getHtml: constructDetailedFeedbackHtml, onLoad: onLoadDetailedFunction } }; this.state = options.state || 'rate_feedback'; this.window = AJS.dialog2(selector, { closeOnOutsideClick: true }); this.$el = this.window.$el; this.setState(); } setState(state) { let newState = state || this.state; let htmlStr = this.states[newState].getHtml(this); this.$el.html(htmlStr); this.states[newState].onLoad(this); } show(cb) { this.setState('rate_feedback'); if (typeof cb == 'function') { this.onCloseCallback = cb; } this.window.show(); } hide() { this.window.hide(); if (this.onCloseCallback) { this.onCloseCallback(); this.onCloseCallback = null; } } onFeedbackSubmitted() { let message = this.$el.find('textarea').val(); let self = this; if (message && message.length > 0) { APP.conference.sendFeedback( self.feedbackScore, message); } this.hide(); } }