comments.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. var Comments;
  3. (function (Comments) {
  4. var Controller = (function () {
  5. function Controller() {
  6. this.comments = new CommentsContainer;
  7. this.renderedCount = 0;
  8. var showAll = $('.comments__show-all');
  9. this.renderedCount = showAll.parent().find('.comments__comment').length;
  10. this.loadUrl = showAll.data('load-url');
  11. showAll.on('click', this.loadComments.bind(this));
  12. $('.comments__add-popup-close').click(function (e) {
  13. e.preventDefault();
  14. $('.comments__add-popup').hide();
  15. $('.comments__add-back-log').hide();
  16. });
  17. this.form = new CommentForm(this);
  18. }
  19. Controller.prototype.loadComments = function () {
  20. $.ajax({
  21. url: this.loadUrl,
  22. dataType: 'json'
  23. }).done((function (data) {
  24. var $button = $('.comments__show-all');
  25. for (var i = 0; i < data.length; i++) {
  26. if (data.hasOwnProperty(i) && !this.comments.hasId(data[i].id)) {
  27. var comment = new Comment(data[i]);
  28. this.comments.push(comment);
  29. comment.rendered.insertBefore($button);
  30. }
  31. }
  32. $button.remove();
  33. }).bind(this));
  34. };
  35. Controller.prototype.pushComments = function (data) {
  36. for (var i = 0; i < data.length; i++) {
  37. if (data.hasOwnProperty(i)) {
  38. var comment = new Comment(data[i]);
  39. this.comments.push(comment);
  40. }
  41. }
  42. };
  43. Controller.prototype.addComment = function (data) {
  44. var comment = new Comment(data);
  45. this.comments.push(comment);
  46. if (data.answeredTo)
  47. comment.rendered.insertAfter($('.comments').find('[data-comment-id=' + data.answerTo + ']'));
  48. else
  49. comment.rendered.insertBefore($('.comments__show-all'));
  50. };
  51. return Controller;
  52. }());
  53. Comments.Controller = Controller;
  54. var CommentsContainer = (function () {
  55. function CommentsContainer() {
  56. }
  57. CommentsContainer.prototype.push = function () {
  58. var args = [];
  59. for (var _i = 0; _i < arguments.length; _i++) {
  60. args[_i] = arguments[_i];
  61. }
  62. return Array.prototype.push.apply(this, args);
  63. };
  64. CommentsContainer.prototype.hasId = function (id) {
  65. var list = Object.keys(this);
  66. for (var i in list) {
  67. if (this.hasOwnProperty(i)) {
  68. if (this[i].id === id) {
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. };
  75. return CommentsContainer;
  76. }());
  77. var CommentForm = (function () {
  78. function CommentForm(owner) {
  79. this.owner = owner;
  80. $('.comments__add-link').on('click', this.onAddClick.bind(this));
  81. $('.comments').on('click', '.comments__comment-reply', this.onAddClick.bind(this));
  82. this.form = $('.comments__add-popup-form');
  83. this.form.on('submit', this.submitFrom.bind(this));
  84. this.owner.postEndpointUrl = this.form.attr('action');
  85. this.formData = {};
  86. }
  87. CommentForm.prototype.onAddClick = function (event) {
  88. event.preventDefault();
  89. $('.comments__add-error').remove();
  90. $('.comments__add-popup').show();
  91. $('.comments__add-back-log').show();
  92. var target = $(event.target);
  93. var targetId = 0;
  94. if (target.hasClass('comments__comment-reply')) {
  95. targetId = target.parents('.comments__comment').data('comment-id');
  96. }
  97. this.formData = { answerTo: targetId };
  98. };
  99. CommentForm.prototype.isRulesAccepted = function () {
  100. return $('#acceptRules').is(':checked');
  101. };
  102. CommentForm.prototype.submitFrom = function (e) {
  103. e.preventDefault();
  104. $('.comments__add-error').remove();
  105. if (!this.isRulesAccepted()) {
  106. this.form.prepend($('<p class="comments__add-error">Необходимо дать согласие на обработку персональных данных.</p>'));
  107. return false;
  108. }
  109. var data = this.form.serializeArray();
  110. for (var i in data) {
  111. if (data.hasOwnProperty(i)) {
  112. this.formData[data[i].name] = data[i].value;
  113. }
  114. }
  115. console.dir({ UserComments: this.formData });
  116. $.ajax({
  117. url: this.owner.postEndpointUrl,
  118. data: { UserComments: this.formData },
  119. dataType: 'json',
  120. type: 'post'
  121. }).done((function (data) {
  122. console.dir(data);
  123. if (data.errors) {
  124. for (var fieldName in data.errors) {
  125. if (data.errors.hasOwnProperty(fieldName))
  126. $('<span class="comments__add-error">' + data.errors[fieldName][0] + '</span>').insertAfter(this.form.find('[name=' + fieldName + ']'));
  127. }
  128. }
  129. else {
  130. //this.form.parent().find('.comments__add-popup-close').click();
  131. this.form.html('<p>Ваш комментарий успешно отправлен. После одобрения модератором он появится на сайте.</p>');
  132. setTimeout((function () {
  133. this.form.parent().find('.comments__add-popup-close').click();
  134. }).bind(this), 3000);
  135. //this.owner.addComment(data.result);
  136. }
  137. }).bind(this)).fail((function (xhr) {
  138. console.dir(xhr.responseText);
  139. }).bind(this)).always((function () {
  140. this.formData = [];
  141. }).bind(this));
  142. return true;
  143. };
  144. return CommentForm;
  145. }());
  146. var Comment = (function () {
  147. function Comment(data) {
  148. this.answerTo = 0;
  149. this.id = data.id;
  150. this.userName = data.userName;
  151. this.date = data.datetime;
  152. this.text = data.text;
  153. this.level = data.answerTo == 0 ? 1 : 2;
  154. this.answerTo = data.answerTo;
  155. }
  156. Object.defineProperty(Comment.prototype, "template", {
  157. get: function () {
  158. if (this._template !== undefined)
  159. return this._template;
  160. this._template = $('<div class="comments__comment"></div>');
  161. this._template.append('<div class="comments__comment-text"></div>');
  162. var info = $('<div class="comments__comment-info"></div>');
  163. info.append('<div class="comments__comment-name"></div>');
  164. info.append('<div class="comments__comment-date"></div>');
  165. info.append('<div class="comments__comment-reply">Ответить</div>');
  166. this._template.append(info);
  167. return this._template;
  168. },
  169. enumerable: true,
  170. configurable: true
  171. });
  172. Object.defineProperty(Comment.prototype, "rendered", {
  173. get: function () {
  174. if (this._rendered !== undefined)
  175. return this._rendered;
  176. this._rendered = this.template.clone();
  177. return this._rendered;
  178. },
  179. enumerable: true,
  180. configurable: true
  181. });
  182. Object.defineProperty(Comment.prototype, "text", {
  183. set: function (value) {
  184. this._text = value;
  185. this.rendered.find('.comments__comment-text').html(value);
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. Object.defineProperty(Comment.prototype, "userName", {
  191. set: function (value) {
  192. this._userName = value;
  193. this.rendered.find('.comments__comment-name').html(value);
  194. },
  195. enumerable: true,
  196. configurable: true
  197. });
  198. Object.defineProperty(Comment.prototype, "date", {
  199. set: function (value) {
  200. this._date = value;
  201. var dateObject = new Date(value);
  202. this.rendered.find('.comments__comment-date').html(dateObject.toLocaleString("ru", {
  203. year: 'numeric',
  204. month: 'long',
  205. day: 'numeric',
  206. hour: '2-digit',
  207. minute: '2-digit'
  208. }).replace(' г.', ''));
  209. },
  210. enumerable: true,
  211. configurable: true
  212. });
  213. Object.defineProperty(Comment.prototype, "id", {
  214. get: function () {
  215. return this._id;
  216. },
  217. set: function (value) {
  218. this._id = value;
  219. this.rendered.data('comment-id', value);
  220. },
  221. enumerable: true,
  222. configurable: true
  223. });
  224. Object.defineProperty(Comment.prototype, "level", {
  225. set: function (value) {
  226. this._level = value;
  227. this.rendered.addClass('comments__comment--level' + value);
  228. },
  229. enumerable: true,
  230. configurable: true
  231. });
  232. return Comment;
  233. }());
  234. })(Comments || (Comments = {}));