comments.ts 7.7 KB

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