You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FeedbackWindow.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* global $, APP, interfaceConfig, AJS */
  2. /* jshint -W101 */
  3. const selector = '#aui-feedback-dialog';
  4. /**
  5. * Toggles the appropriate css class for the given number of stars, to
  6. * indicate that those stars have been clicked/selected.
  7. *
  8. * @param starCount the number of stars, for which to toggle the css class
  9. */
  10. let toggleStars = function(starCount) {
  11. $('#stars > a').each(function(index, el) {
  12. if (index <= starCount) {
  13. el.classList.add("starHover");
  14. } else
  15. el.classList.remove("starHover");
  16. });
  17. };
  18. /**
  19. * Constructs the html for the detailed feedback window.
  20. *
  21. * @returns {string} the contructed html string
  22. */
  23. let constructDetailedFeedbackHtml = function() {
  24. return `
  25. <div class="aui-dialog2-content feedback__content">
  26. <div class="feedback__details">
  27. <p>${APP.translation.translateString("dialog.sorryFeedback")}</p>
  28. <br/><br/>
  29. <textarea id="feedbackTextArea" rows="10" cols="50" autofocus></textarea>
  30. </div>
  31. </div>
  32. <footer class="aui-dialog2-footer feedback__footer">
  33. <div class="aui-dialog2-footer-actions">
  34. <button id="dialog-close-button" class="aui-button aui-button_close">Close</button>
  35. <button id="dialog-submit-button" class="aui-button aui-button_submit">Submit</button>
  36. </div>
  37. </footer>
  38. `;
  39. };
  40. /**
  41. * Constructs the html for the rated feedback window.
  42. *
  43. * @returns {string} the contructed html string
  44. */
  45. let createRateFeedbackHTML = function (Feedback) {
  46. var rateExperience = APP.translation.translateString('dialog.rateExperience'),
  47. feedbackHelp = APP.translation.translateString('dialog.feedbackHelp'),
  48. feedbackQuestion = (Feedback.feedbackScore < 0)
  49. ? `<p><br/>${APP.translation.translateString('dialog.feedbackQuestion')}</p>`
  50. : '';
  51. return `
  52. <div class="aui-dialog2-content feedback__content">
  53. ${feedbackQuestion}
  54. <form action="javascript:false;" onsubmit="return false;">
  55. <div class="feedback__rating">
  56. <h2>${ rateExperience }</h2>
  57. <p class="star-label">&nbsp;</p>
  58. <div id="stars" class="feedback-stars">
  59. <a class="star-btn">
  60. <i class="icon-star shake-rotate"></i>
  61. </a>
  62. <a class="star-btn">
  63. <i class="icon-star shake-rotate"></i>
  64. </a>
  65. <a class="star-btn">
  66. <i class="icon-star shake-rotate"></i>
  67. </a>
  68. <a class="star-btn">
  69. <i class="icon-star shake-rotate"></i>
  70. </a>
  71. <a class="star-btn">
  72. <i class="icon-star shake-rotate"></i>
  73. </a>
  74. </div>
  75. <p>&nbsp;</p>
  76. <p>${ feedbackHelp }</p>
  77. </div>
  78. </form>
  79. </div>
  80. `;
  81. };
  82. /**
  83. * Callback for Rate Feedback
  84. *
  85. * @param Feedback
  86. */
  87. let onLoadRateFunction = function (Feedback) {
  88. $('#stars > a').each((index, el) => {
  89. el.onmouseover = function(){
  90. toggleStars(index);
  91. };
  92. el.onmouseleave = function(){
  93. toggleStars(Feedback.feedbackScore - 1);
  94. };
  95. el.onclick = function(){
  96. Feedback.feedbackScore = index + 1;
  97. // If the feedback is less than 3 stars we're going to
  98. // ask the user for more information.
  99. if (Feedback.feedbackScore > 3) {
  100. APP.conference.sendFeedback(Feedback.feedbackScore, "");
  101. Feedback.hide();
  102. } else {
  103. Feedback.setState('detailed_feedback');
  104. }
  105. };
  106. });
  107. // Init stars to correspond to previously entered feedback.
  108. if (Feedback.feedbackScore > 0) {
  109. toggleStars(Feedback.feedbackScore - 1);
  110. }
  111. };
  112. /**
  113. * Callback for Detailed Feedback
  114. *
  115. * @param Feedback
  116. */
  117. let onLoadDetailedFunction = function(Feedback) {
  118. let submitBtn = Feedback.$el.find('#dialog-submit-button');
  119. let closeBtn = Feedback.$el.find('#dialog-close-button');
  120. if (submitBtn && submitBtn.length) {
  121. submitBtn.on('click', (e) => {
  122. e.preventDefault();
  123. Feedback.onFeedbackSubmitted();
  124. });
  125. }
  126. if (closeBtn && closeBtn.length) {
  127. closeBtn.on('click', (e) => {
  128. e.preventDefault();
  129. Feedback.hide();
  130. });
  131. }
  132. };
  133. /**
  134. * @class Dialog
  135. *
  136. */
  137. export default class Dialog {
  138. constructor(options) {
  139. this.feedbackScore = -1;
  140. this.onCloseCallback = null;
  141. this.states = {
  142. rate_feedback: {
  143. getHtml: createRateFeedbackHTML,
  144. onLoad: onLoadRateFunction
  145. },
  146. detailed_feedback: {
  147. getHtml: constructDetailedFeedbackHtml,
  148. onLoad: onLoadDetailedFunction
  149. }
  150. };
  151. this.state = options.state || 'rate_feedback';
  152. this.window = AJS.dialog2(selector, {
  153. closeOnOutsideClick: true
  154. });
  155. this.$el = this.window.$el;
  156. this.setState();
  157. }
  158. setState(state) {
  159. let newState = state || this.state;
  160. let htmlStr = this.states[newState].getHtml(this);
  161. this.$el.html(htmlStr);
  162. this.states[newState].onLoad(this);
  163. }
  164. show(cb) {
  165. this.setState('rate_feedback');
  166. if (typeof cb == 'function') {
  167. this.onCloseCallback = cb;
  168. }
  169. this.window.show();
  170. }
  171. hide() {
  172. this.window.hide();
  173. if (this.onCloseCallback) {
  174. this.onCloseCallback();
  175. this.onCloseCallback = null;
  176. }
  177. }
  178. onFeedbackSubmitted() {
  179. let message = this.$el.find('textarea').val();
  180. let self = this;
  181. if (message && message.length > 0) {
  182. APP.conference.sendFeedback(
  183. self.feedbackScore,
  184. message);
  185. }
  186. this.hide();
  187. }
  188. }