comments.js 8.8 KB

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