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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 rated feedback window.
  20. *
  21. * @returns {string} the contructed html string
  22. */
  23. let createRateFeedbackHTML = function (Feedback) {
  24. let rateExperience
  25. = APP.translation.translateString('dialog.rateExperience'),
  26. feedbackHelp = APP.translation.translateString('dialog.feedbackHelp');
  27. let starClassName = (interfaceConfig.ENABLE_FEEDBACK_ANIMATION)
  28. ? "icon-star shake-rotate"
  29. : "icon-star";
  30. return `
  31. <div class="aui-dialog2-content feedback__content">
  32. <form action="javascript:false;" onsubmit="return false;">
  33. <div class="feedback__rating">
  34. <h2>${ rateExperience }</h2>
  35. <p class="star-label">&nbsp;</p>
  36. <div id="stars" class="feedback-stars">
  37. <a class="star-btn">
  38. <i class=${ starClassName }></i>
  39. </a>
  40. <a class="star-btn">
  41. <i class=${ starClassName }></i>
  42. </a>
  43. <a class="star-btn">
  44. <i class=${ starClassName }></i>
  45. </a>
  46. <a class="star-btn">
  47. <i class=${ starClassName }></i>
  48. </a>
  49. <a class="star-btn">
  50. <i class=${ starClassName }></i>
  51. </a>
  52. </div>
  53. <p>&nbsp;</p>
  54. <p>${ feedbackHelp }</p>
  55. </div>
  56. <textarea id="feedbackTextArea" rows="10" cols="40" autofocus></textarea>
  57. </form>
  58. <footer class="aui-dialog2-footer feedback__footer">
  59. <div class="aui-dialog2-footer-actions">
  60. <button id="dialog-close-button" class="aui-button aui-button_close">Close</button>
  61. <button id="dialog-submit-button" class="aui-button aui-button_submit">Submit</button>
  62. </div>
  63. </footer>
  64. </div>
  65. `;
  66. };
  67. /**
  68. * Callback for Rate Feedback
  69. *
  70. * @param Feedback
  71. */
  72. let onLoadRateFunction = function (Feedback) {
  73. $('#stars > a').each((index, el) => {
  74. el.onmouseover = function(){
  75. toggleStars(index);
  76. };
  77. el.onmouseleave = function(){
  78. toggleStars(Feedback.feedbackScore - 1);
  79. };
  80. el.onclick = function(){
  81. Feedback.feedbackScore = index + 1;
  82. };
  83. });
  84. // Init stars to correspond to previously entered feedback.
  85. if (Feedback.feedbackScore > 0) {
  86. toggleStars(Feedback.feedbackScore - 1);
  87. }
  88. if (Feedback.feedbackText && Feedback.feedbackText.length > 0)
  89. $('#feedbackTextArea').text(Feedback.feedbackText);
  90. let submitBtn = Feedback.$el.find('#dialog-submit-button');
  91. let closeBtn = Feedback.$el.find('#dialog-close-button');
  92. if (submitBtn && submitBtn.length) {
  93. submitBtn.on('click', (e) => {
  94. e.preventDefault();
  95. Feedback.onFeedbackSubmitted();
  96. });
  97. }
  98. if (closeBtn && closeBtn.length) {
  99. closeBtn.on('click', (e) => {
  100. e.preventDefault();
  101. Feedback.hide();
  102. });
  103. }
  104. $('#feedbackTextArea').focus();
  105. };
  106. /**
  107. * @class Dialog
  108. *
  109. */
  110. export default class Dialog {
  111. constructor(options) {
  112. this.feedbackScore = -1;
  113. this.feedbackText = null;
  114. this.submitted = false;
  115. this.onCloseCallback = null;
  116. this.states = {
  117. rate_feedback: {
  118. getHtml: createRateFeedbackHTML,
  119. onLoad: onLoadRateFunction
  120. }
  121. };
  122. this.state = options.state || 'rate_feedback';
  123. this.window = AJS.dialog2(selector, {
  124. closeOnOutsideClick: true
  125. });
  126. this.$el = this.window.$el;
  127. AJS.dialog2(selector).on("hide", function() {
  128. if (this.onCloseCallback) {
  129. this.onCloseCallback();
  130. this.onCloseCallback = null;
  131. }
  132. }.bind(this));
  133. this.setState();
  134. }
  135. setState(state) {
  136. let newState = state || this.state;
  137. let htmlStr = this.states[newState].getHtml(this);
  138. this.$el.html(htmlStr);
  139. this.states[newState].onLoad(this);
  140. }
  141. show(cb) {
  142. this.setState('rate_feedback');
  143. if (typeof cb == 'function') {
  144. this.onCloseCallback = cb;
  145. }
  146. this.window.show();
  147. }
  148. hide() {
  149. this.window.hide();
  150. }
  151. onFeedbackSubmitted() {
  152. let message = this.$el.find('textarea').val();
  153. let self = this;
  154. if (message && message.length > 0) {
  155. self.feedbackText = message;
  156. }
  157. APP.conference.sendFeedback(self.feedbackScore,
  158. self.feedbackText);
  159. // TO DO: make sendFeedback return true or false.
  160. self.submitted = true;
  161. this.hide();
  162. }
  163. }