/common/_static/helpfulness.js

https://gitlab.com/mayakarya/Docs · JavaScript · 262 lines · 228 code · 30 blank · 4 comment · 23 complexity · 9607cf6d613ded16f3b7ded02f365be7 MD5 · raw file

  1. /* msc-helpfulness.js - start */
  2. ;(function ($)
  3. {
  4. $.Helpfulness = function (options)
  5. {
  6. var documentId = $("[data-documentid]").attr("data-documentid");
  7. var docId = typeof (READTHEDOCS_DATA) !== 'undefined' ? READTHEDOCS_DATA.docroot + READTHEDOCS_DATA.page : options.domain !== "" ? window.location.href : "";
  8. return {
  9. targetDocumentId: documentId,
  10. targetPickerId: $("[data-pickerid]").attr("data-pickerid"),
  11. targetDocId: docId,
  12. targetKeyId: documentId || docId || 0,
  13. $container: $(".helpfulness-container"),
  14. $content: $(".helpfulness"),
  15. $positionContainer: $("#helpful-position"),
  16. shouldShow: false,
  17. closed: false,
  18. completed: false,
  19. init: function ()
  20. {
  21. if (this.$container == null)
  22. {
  23. return;
  24. }
  25. this.initHandlers();
  26. this.initLoad();
  27. },
  28. initHandlers: function ()
  29. {
  30. var _this = this;
  31. $("#helpfulness-btn-yes").click(function ()
  32. {
  33. _this.submitYes();
  34. });
  35. $("#helpfulness-btn-no").click(function ()
  36. {
  37. $(".helpfulness-form").hide();
  38. $(".helpfulness-form-no").show();
  39. });
  40. $("#txt-helpfulness").attr("maxlength", options.maxCharacters);
  41. $("#helpfulness-characters-left").prepend(options.maxCharacters + " ");
  42. $("#txt-helpfulness").on('input propertychange paste', function ()
  43. {
  44. var left = options.maxCharacters - $(this).val().length;
  45. if (left < 0)
  46. {
  47. left = 0;
  48. }
  49. $("#helpfulness-characters-left").text(left + " character" + (left === 1 ? "" : "s") + " remaining");
  50. });
  51. $("#helpfulness-btn-submit").click(function ()
  52. {
  53. var reason = $("#txt-helpfulness").val().substring(0, options.maxCharacters);
  54. _this.submitNo(reason);
  55. });
  56. $("#helpfulness-btn-skip").click(function ()
  57. {
  58. _this.submitNo("");
  59. });
  60. $(".helpfulness-close").click(function ()
  61. {
  62. _this.closed = true;
  63. $("#helpful").slideUp('fast');
  64. });
  65. $(document).keyup(function (e)
  66. {
  67. if (e.keyCode === 27)
  68. {
  69. _this.closed = true;
  70. $("#helpful").slideUp('fast');
  71. }
  72. });
  73. },
  74. initLoad: function ()
  75. {
  76. if (this.getStorage(this.targetKeyId) === null)
  77. {
  78. this.shouldShow = true;
  79. this.show();
  80. }
  81. $.ajax({
  82. context: this,
  83. url: options.domain + "/umbraco/api/helpfulnessapi/reportsummarybyid",
  84. type: "POST",
  85. data: {
  86. contentId: this.targetDocumentId,
  87. docId: this.targetDocId
  88. },
  89. success: function (data)
  90. {
  91. if (data !== null && data !== "")
  92. {
  93. if (options.isDocs && $("#helpfulness-data").length === 0)
  94. {
  95. $("h1").after("<div id='helpfulness-data' class='helpful-text'>" + data + "</div>");
  96. }
  97. else
  98. {
  99. $("#helpfulness-data").text(data).css("visibility", "visible");
  100. }
  101. }
  102. }
  103. });
  104. },
  105. show: function ()
  106. {
  107. if (!this.closed && !this.completed && this.shouldShow && !this.$container.is(":visible"))
  108. {
  109. this.$container.show();
  110. }
  111. },
  112. submitYes: function()
  113. {
  114. if (this.targetKeyId)
  115. {
  116. $(".helpfulness-form").hide();
  117. this.showMessage(".processing");
  118. $.ajax({
  119. context: this,
  120. url: options.domain + "/umbraco/api/helpfulnessapi/submityes",
  121. type: "POST",
  122. data: {
  123. pickerId: this.targetPickerId,
  124. contentId: this.targetDocumentId,
  125. docId: this.targetDocId
  126. },
  127. success: function (data)
  128. {
  129. if (data !== null && data === "success")
  130. {
  131. this.showMessage(".success");
  132. this.setStorage(this.targetKeyId);
  133. this.completed = true;
  134. this.initLoad();
  135. }
  136. else
  137. {
  138. this.showMessage(".helpfulness-error");
  139. }
  140. },
  141. error: function ()
  142. {
  143. this.showMessage(".helpfulness-error");
  144. }
  145. });
  146. }
  147. },
  148. submitNo: function (value)
  149. {
  150. if (this.targetKeyId)
  151. {
  152. $(".helpfulness-form-no").hide();
  153. this.showMessage(".processing");
  154. $.ajax({
  155. context: this,
  156. url: options.domain + "/umbraco/api/helpfulnessapi/submitno",
  157. type: "POST",
  158. data: {
  159. pickerId: this.targetPickerId,
  160. contentId: this.targetDocumentId,
  161. docId: this.targetDocId,
  162. reason: value
  163. },
  164. success: function (data)
  165. {
  166. if (data !== null && data === "success")
  167. {
  168. this.showMessage(".success");
  169. this.setStorage(this.targetKeyId);
  170. this.completed = true;
  171. this.initLoad();
  172. }
  173. else
  174. {
  175. this.showMessage(".helpfulness-error");
  176. }
  177. },
  178. error: function ()
  179. {
  180. this.showMessage(".helpfulness-error");
  181. }
  182. });
  183. }
  184. },
  185. getStorage: function (contentId)
  186. {
  187. if (window.localStorage)
  188. {
  189. return localStorage.getItem("helpfulness-" + contentId);
  190. }
  191. return null;
  192. },
  193. setStorage: function (contentId)
  194. {
  195. if (window.localStorage)
  196. {
  197. try
  198. {
  199. localStorage.setItem("helpfulness-" + contentId, 1);
  200. return true;
  201. }
  202. catch (e)
  203. {
  204. return false;
  205. }
  206. }
  207. return false;
  208. },
  209. showMessage: function (message)
  210. {
  211. this.$content.children(".messages").hide();
  212. this.$content.children(message).show();
  213. }
  214. };
  215. };
  216. })(jQuery);
  217. $(function ()
  218. {
  219. $.Helpfulness({
  220. maxCharacters: 1000,
  221. alwaysFixed: true,
  222. domain: "https://www.asp.net",
  223. isDocs: true
  224. }).init();
  225. });
  226. /* msc-helpfulness.js - end */
  227. /* msc-helpfulness.js overrides - start */
  228. $(function ()
  229. {
  230. $("#helpful").insertBefore("footer hr");
  231. });
  232. /* msc-helpfulness.js overrides - end */