comments.ts 7.4 KB

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