您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Feedback.js 8.0KB

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