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

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