summaryrefslogtreecommitdiff
path: root/public/assets/js/lib/views/details/commentform.js
blob: 708657d37e46a6130ec7283ff59af1bc84e2a016 (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
65
66
67
68
69
70
71
72
var CommentForm = FormView.extend({
  el: "#comment_form",

  events: {
    "focus textarea": "focus",
    "mouseup input[type=file]": "focus",
    "keydown textarea": "keydown",
  },

  action: "",

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

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

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

  load: function (thread) {
    this.action = "/api/thread/" + thread.id + "/comment";
    this.$comment.addClass("empty");
    if (thread.settings.noupload) {
      this.$("[type=file]").hide();
    }
  },

  keydown: function (e) {
    if ((e.ctrlKey || e.metaKey || e.altKey) && e.keyCode == 83) {
      // "s" key
      e.preventDefault();
      e.stopPropagation();
      this.save();
    }
  },

  focus: function () {
    this.$el.addClass("focused");
    this.$comment.removeClass("empty");
    $("[name=comment]").prop("required", false);
  },

  validate: function () {
    var errors = [];
    var comment = this.$("[name=comment]").val();
    var files = this.$("[name=files]").val();
    console.log(comment, files);
    if ((!comment || !comment.length) && !files) {
      errors.push("Please enter a comment or add some files.");
    }
    return errors.length ? errors : null;
  },

  success: function (data) {
    $("[name=comment").val("");
    $("[name=files").val("");
    // window.location.reload()
    // console.log(this);
    // console.log(this.parent);
    console.log(data);
    if (this.opt.onSubmit) {
      this.opt.onSubmit(data);
    }
  },
});