Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Feedback.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* global $, 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. 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. * Initialise the Feedback functionality.
  70. */
  71. init: function () {
  72. // CallStats is the way we send feedback, so we don't have to initialise
  73. // if callstats isn't enabled.
  74. if (!this.isEnabled())
  75. return;
  76. $("div.feedbackButton").css("display", "block");
  77. $("#feedbackButton").click(function (event) {
  78. Feedback.openFeedbackWindow();
  79. });
  80. },
  81. /**
  82. * Indicates if the feedback functionality is enabled.
  83. *
  84. * @return true if the feedback functionality is enabled, false otherwise.
  85. */
  86. isEnabled: function() {
  87. // XXX callStats.isEnabled() indicates whether we are allowed to attempt
  88. // to integrate callstats.io. Whether our attempt was/is/will be
  89. // successful is a different question.
  90. return callStats.isEnabled();
  91. },
  92. /**
  93. * Opens the feedback window.
  94. */
  95. openFeedbackWindow: function (callback) {
  96. feedbackWindowCallback = callback;
  97. // Add all mouse and click listeners.
  98. var onLoadFunction = function (event) {
  99. $('#stars >a').each(function(index) {
  100. // On star mouse over.
  101. $(this).get(0).onmouseover = function(){
  102. Feedback.hoverStars(index);
  103. };
  104. // On star mouse leave.
  105. $(this).get(0).onmouseleave = function(){
  106. Feedback.unhoverStars(index);
  107. };
  108. // On star click.
  109. $(this).get(0).onclick = function(){
  110. Feedback.toggleStars(index);
  111. Feedback.feedbackScore = index+1;
  112. // If the feedback is less than 3 stars we're going to
  113. // ask the user for more information.
  114. if (Feedback.feedbackScore > 3) {
  115. callStats.sendFeedback(Feedback.feedbackScore, "");
  116. if (feedbackWindowCallback)
  117. feedbackWindowCallback();
  118. else
  119. APP.UI.messageHandler.closeDialog();
  120. }
  121. else {
  122. feedbackDialog.goToState('detailed_feedback');
  123. }
  124. };
  125. // Init stars to correspond to previously entered feedback.
  126. if (Feedback.feedbackScore > 0
  127. && index < Feedback.feedbackScore) {
  128. Feedback.hoverStars(index);
  129. Feedback.toggleStars(index);
  130. }
  131. });
  132. };
  133. // Defines the different states of the feedback window.
  134. var states = {
  135. overall_feedback: {
  136. html: constructOverallFeedbackHtml(),
  137. persistent: false,
  138. buttons: {},
  139. closeText: '',
  140. focus: "div[id='stars']",
  141. position: {width: 500}
  142. },
  143. detailed_feedback: {
  144. html: constructDetailedFeedbackHtml(),
  145. buttons: {"Submit": true, "Cancel": false},
  146. closeText: '',
  147. focus: "textarea[id='feedbackTextArea']",
  148. position: {width: 500},
  149. submit: function(e,v,m,f) {
  150. e.preventDefault();
  151. if (v) {
  152. var feedbackDetails
  153. = document.getElementById("feedbackTextArea").value;
  154. if (feedbackDetails && feedbackDetails.length > 0)
  155. callStats.sendFeedback( Feedback.feedbackScore,
  156. feedbackDetails);
  157. if (feedbackWindowCallback)
  158. feedbackWindowCallback();
  159. else
  160. APP.UI.messageHandler.closeDialog();
  161. } else {
  162. // User cancelled
  163. if (feedbackWindowCallback)
  164. feedbackWindowCallback();
  165. else
  166. APP.UI.messageHandler.closeDialog();
  167. }
  168. }
  169. }
  170. };
  171. // Create the feedback dialog.
  172. var feedbackDialog
  173. = APP.UI.messageHandler.openDialogWithStates(
  174. states,
  175. { persistent: false,
  176. buttons: {},
  177. closeText: '',
  178. loaded: onLoadFunction,
  179. position: {width: 500}}, null);
  180. },
  181. /**
  182. * Toggles the appropriate css class for the given number of stars, to
  183. * indicate that those stars have been clicked/selected.
  184. *
  185. * @param starCount the number of stars, for which to toggle the css class
  186. */
  187. toggleStars: function (starCount)
  188. {
  189. $('#stars >a >i').each(function(index) {
  190. if (index <= starCount) {
  191. $(this).removeClass("fa-star-o");
  192. }
  193. else
  194. $(this).addClass("fa-star-o");
  195. });
  196. },
  197. /**
  198. * Toggles the appropriate css class for the given number of stars, to
  199. * indicate that those stars have been hovered.
  200. *
  201. * @param starCount the number of stars, for which to toggle the css class
  202. */
  203. hoverStars: function (starCount)
  204. {
  205. $('#stars >a >i').each(function(index) {
  206. if (index <= starCount)
  207. $(this).addClass("starHover");
  208. });
  209. },
  210. /**
  211. * Toggles the appropriate css class for the given number of stars, to
  212. * indicate that those stars have been un-hovered.
  213. *
  214. * @param starCount the number of stars, for which to toggle the css class
  215. */
  216. unhoverStars: function (starCount)
  217. {
  218. $('#stars >a >i').each(function(index) {
  219. if (index <= starCount && $(this).hasClass("fa-star-o"))
  220. $(this).removeClass("starHover");
  221. });
  222. }
  223. };
  224. // Exports the Feedback class.
  225. module.exports = Feedback;