summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/index/hootbox.js
blob: c874e743ac341fb430de25c5658682eae0e6bc6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var HootBox = FormView.extend({
  el: "#hootbox",

  events: {},

  action: "/api/thread/1/comment",

  initialize: function () {
    this.__super__.initialize.call(this);
    this.template = this.$(".template").html();
    this.$hoots = this.$("#hoots");
    this.$comment = this.$("[name=comment]");
  },

  show: function () {
    this.$el.show();
  },

  hide: function () {
    this.$el.hide();
  },

  load: function (comments) {
    if (!comments || (!comments.length && !this.options.required)) {
      this.$el.hide();
      return;
    }
    comments.forEach(this.appendComment.bind(this));
  },

  parse: function (comment) {
    console.log(comment);
    var t = this.template
      .replace(/{{image}}/g, profile_image(comment.username))
      .replace(/{{username}}/g, comment.username)
      .replace(/{{comment}}/g, tidy_urls(comment.comment, true));
    var $t = $(t);
    return $t;
  },

  prependComment: function (comment) {
    var $el = this.parse(comment);
    this.$hoots.prepend($el);
  },

  appendComment: function (comment) {
    var $el = this.parse(comment);
    this.$hoots.append($el);
  },

  validate: function () {
    var errors = [];
    var comment = $("[name=comment]").val();
    if (!comment || !comment.length) {
      errors.push("Please enter a comment.");
    }
    return errors.length ? errors : null;
  },

  success: function (data) {
    this.prependComment(data.comment);
    this.$("[name=comment]").val("");
  },
});