Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Feedback.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Created by ystamcheva on 2/10/15.
  3. */
  4. /* jshint -W101 */
  5. var messageHandler = require("./util/MessageHandler");
  6. var callStats = require("../statistics/CallStats");
  7. var APP = require("../../app");
  8. /**
  9. * Constructs the html for the overall feedback window.
  10. *
  11. * @returns {string} the constructed html string
  12. */
  13. var constructOverallFeedbackHtml = function() {
  14. var feedbackQuestion = (Feedback.feedbackScore < 0)
  15. ? '<br/><br/>' + APP.translation
  16. .translateString("dialog.feedbackQuestion")
  17. : '';
  18. var message = '<div class="feedback"><div>' +
  19. '<div class="feedbackTitle">' +
  20. APP.translation.translateString("dialog.thankYou",
  21. {appName:interfaceConfig.APP_NAME}) +
  22. '</div>' +
  23. feedbackQuestion +
  24. '</div><br/><br/>' +
  25. '<div id="stars">' +
  26. '<a><i class="fa fa-star-o fa fa-star"></i></a>' +
  27. '<a><i class="fa fa-star-o fa fa-star"></i></a>' +
  28. '<a><i class="fa fa-star-o fa fa-star"></i></a>' +
  29. '<a><i class="fa fa-star-o fa fa-star"></i></a>' +
  30. '<a><i class="fa fa-star-o fa fa-star"></i></a>' +
  31. '</div></div>';
  32. return message;
  33. };
  34. /**
  35. * Constructs the html for the detailed feedback window.
  36. *
  37. * @returns {string} the contructed html string
  38. */
  39. var constructDetailedFeedbackHtml = function() {
  40. // Construct the html, which will be served as a dialog message.
  41. var message = '<div class="feedback">' +
  42. '<div class="feedbackTitle">' +
  43. APP.translation.translateString("dialog.sorryFeedback") +
  44. '</div><br/><br/>' +
  45. '<div class="feedbackDetails">' +
  46. '<textarea id="feedbackTextArea" rows="10" cols="50" autofocus>' +
  47. '</textarea>' +
  48. '</div></div>';
  49. return message;
  50. };
  51. /**
  52. * The callback function corresponding to the openFeedbackWindow parameter.
  53. *
  54. * @type {function}
  55. */
  56. var feedbackWindowCallback = null;
  57. /**
  58. * Defines all methods in connection to the Feedback window.
  59. *
  60. * @type {{feedbackScore: number, openFeedbackWindow: Function,
  61. * toggleStars: Function, hoverStars: Function, unhoverStars: Function}}
  62. */
  63. var Feedback = {
  64. /**
  65. * The feedback score. -1 indicates no score has been given for now.
  66. */
  67. feedbackScore: -1,
  68. /**
  69. * Opens the feedback window.
  70. */
  71. openFeedbackWindow: function (callback) {
  72. feedbackWindowCallback = callback;
  73. // Add all mouse and click listeners.
  74. var onLoadFunction = function (event) {
  75. $('#stars >a').each(function(index) {
  76. // On star mouse over.
  77. $(this).get(0).onmouseover = function(){
  78. Feedback.hoverStars(index);
  79. };
  80. // On star mouse leave.
  81. $(this).get(0).onmouseleave = function(){
  82. Feedback.unhoverStars(index);
  83. };
  84. // On star click.
  85. $(this).get(0).onclick = function(){
  86. Feedback.toggleStars(index);
  87. Feedback.feedbackScore = index+1;
  88. // If the feedback is less than 3 stars we're going to
  89. // ask the user for more information.
  90. if (Feedback.feedbackScore > 3) {
  91. callStats.sendFeedback(Feedback.feedbackScore, "");
  92. if (feedbackWindowCallback)
  93. feedbackWindowCallback();
  94. else
  95. APP.UI.messageHandler.closeDialog();
  96. }
  97. else {
  98. feedbackDialog.goToState('detailed_feedback');
  99. }
  100. };
  101. // Initialise stars to correspond to previously entered feedback.
  102. if (Feedback.feedbackScore > 0
  103. && index < Feedback.feedbackScore) {
  104. Feedback.hoverStars(index);
  105. Feedback.toggleStars(index);
  106. }
  107. });
  108. };
  109. // Defines the different states of the feedback window.
  110. var states = {
  111. overall_feedback: {
  112. html: constructOverallFeedbackHtml(),
  113. persistent: true,
  114. buttons: {},
  115. closeText: '',
  116. focus: "div[id='stars']",
  117. position: {width: 500}
  118. },
  119. detailed_feedback: {
  120. html: constructDetailedFeedbackHtml(),
  121. buttons: {"Submit": true, "Cancel": false},
  122. closeText: '',
  123. focus: "textarea[id='feedbackTextArea']",
  124. position: {width: 500},
  125. submit: function(e,v,m,f) {
  126. e.preventDefault();
  127. if (v) {
  128. var feedbackDetails
  129. = document.getElementById("feedbackTextArea").value;
  130. if (feedbackDetails && feedbackDetails.length > 0)
  131. callStats.sendFeedback( Feedback.feedbackScore,
  132. feedbackDetails);
  133. if (feedbackWindowCallback)
  134. feedbackWindowCallback();
  135. else
  136. APP.UI.messageHandler.closeDialog();
  137. } else {
  138. // User cancelled
  139. if (feedbackWindowCallback)
  140. feedbackWindowCallback();
  141. else
  142. APP.UI.messageHandler.closeDialog();
  143. }
  144. }
  145. }
  146. };
  147. // Create the feedback dialog.
  148. var feedbackDialog
  149. = APP.UI.messageHandler.openDialogWithStates(
  150. states,
  151. { persistent: true,
  152. buttons: {},
  153. closeText: '',
  154. loaded: onLoadFunction,
  155. position: {width: 500}}, null);
  156. },
  157. /**
  158. * Toggles the appropriate css class for the given number of stars, to
  159. * indicate that those stars have been clicked/selected.
  160. *
  161. * @param starCount the number of stars, for which to toggle the css class
  162. */
  163. toggleStars: function (starCount)
  164. {
  165. $('#stars >a >i').each(function(index) {
  166. if (index <= starCount) {
  167. $(this).removeClass("fa-star-o");
  168. }
  169. else
  170. $(this).addClass("fa-star-o");
  171. });
  172. },
  173. /**
  174. * Toggles the appropriate css class for the given number of stars, to
  175. * indicate that those stars have been hovered.
  176. *
  177. * @param starCount the number of stars, for which to toggle the css class
  178. */
  179. hoverStars: function (starCount)
  180. {
  181. $('#stars >a >i').each(function(index) {
  182. if (index <= starCount)
  183. $(this).addClass("starHover");
  184. });
  185. },
  186. /**
  187. * Toggles the appropriate css class for the given number of stars, to
  188. * indicate that those stars have been un-hovered.
  189. *
  190. * @param starCount the number of stars, for which to toggle the css class
  191. */
  192. unhoverStars: function (starCount)
  193. {
  194. $('#stars >a >i').each(function(index) {
  195. if (index <= starCount && $(this).hasClass("fa-star-o"))
  196. $(this).removeClass("starHover");
  197. });
  198. }
  199. };
  200. // Exports the Feedback class.
  201. module.exports = Feedback;