comments.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.submitFrom = function (e) {
  100. e.preventDefault();
  101. var data = this.form.serializeArray();
  102. for (var i in data) {
  103. if (data.hasOwnProperty(i)) {
  104. this.formData[data[i].name] = data[i].value;
  105. }
  106. }
  107. $('.comments__add-error').remove();
  108. console.dir({ UserComments: this.formData });
  109. $.ajax({
  110. url: this.owner.postEndpointUrl,
  111. data: { UserComments: this.formData },
  112. dataType: 'json',
  113. type: 'post'
  114. }).done((function (data) {
  115. console.dir(data);
  116. if (data.errors) {
  117. for (var fieldName in data.errors) {
  118. if (data.errors.hasOwnProperty(fieldName))
  119. $('<span class="comments__add-error">' + data.errors[fieldName][0] + '</span>').insertAfter(this.form.find('[name=' + fieldName + ']'));
  120. }
  121. }
  122. else {
  123. //this.form.parent().find('.comments__add-popup-close').click();
  124. this.form.html('<p>Ваш комментарий успешно отправлен. После одобрения модератором он появится на сайте.</p>');
  125. setTimeout((function () {
  126. this.form.parent().find('.comments__add-popup-close').click();
  127. }).bind(this), 3000);
  128. //this.owner.addComment(data.result);
  129. }
  130. }).bind(this)).fail((function (xhr) {
  131. console.dir(xhr.responseText);
  132. }).bind(this)).always((function () {
  133. this.formData = [];
  134. }).bind(this));
  135. };
  136. return CommentForm;
  137. }());
  138. var Comment = (function () {
  139. function Comment(data) {
  140. this.answerTo = 0;
  141. this.id = data.id;
  142. this.userName = data.userName;
  143. this.date = data.datetime;
  144. this.text = data.text;
  145. this.level = data.answerTo == 0 ? 1 : 2;
  146. this.answerTo = data.answerTo;
  147. }
  148. Object.defineProperty(Comment.prototype, "template", {
  149. get: function () {
  150. if (this._template !== undefined)
  151. return this._template;
  152. this._template = $('<div class="comments__comment"></div>');
  153. this._template.append('<div class="comments__comment-text"></div>');
  154. var 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. enumerable: true,
  162. configurable: true
  163. });
  164. Object.defineProperty(Comment.prototype, "rendered", {
  165. get: function () {
  166. if (this._rendered !== undefined)
  167. return this._rendered;
  168. this._rendered = this.template.clone();
  169. return this._rendered;
  170. },
  171. enumerable: true,
  172. configurable: true
  173. });
  174. Object.defineProperty(Comment.prototype, "text", {
  175. set: function (value) {
  176. this._text = value;
  177. this.rendered.find('.comments__comment-text').html(value);
  178. },
  179. enumerable: true,
  180. configurable: true
  181. });
  182. Object.defineProperty(Comment.prototype, "userName", {
  183. set: function (value) {
  184. this._userName = value;
  185. this.rendered.find('.comments__comment-name').html(value);
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. Object.defineProperty(Comment.prototype, "date", {
  191. set: function (value) {
  192. this._date = value;
  193. var dateObject = new Date(value);
  194. this.rendered.find('.comments__comment-date').html(dateObject.toLocaleString("ru", {
  195. year: 'numeric',
  196. month: 'long',
  197. day: 'numeric',
  198. hour: '2-digit',
  199. minute: '2-digit'
  200. }).replace(' г.', ''));
  201. },
  202. enumerable: true,
  203. configurable: true
  204. });
  205. Object.defineProperty(Comment.prototype, "id", {
  206. get: function () {
  207. return this._id;
  208. },
  209. set: function (value) {
  210. this._id = value;
  211. this.rendered.data('comment-id', value);
  212. },
  213. enumerable: true,
  214. configurable: true
  215. });
  216. Object.defineProperty(Comment.prototype, "level", {
  217. set: function (value) {
  218. this._level = value;
  219. this.rendered.addClass('comments__comment--level' + value);
  220. },
  221. enumerable: true,
  222. configurable: true
  223. });
  224. return Comment;
  225. }());
  226. })(Comments || (Comments = {}));