summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/wall.js.coffee
blob: c8fc960e1749934398650658d45f4f91cfe1864e (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
73
74
75
76
77
78
79
80
81
82
83
84
85
class Wall
  constructor: (project_id) ->
    @project_id = project_id
    @note_ids = []
    @getContent()
    @initRefresh()
    @initForm()
  
  # 
  # Gets an initial set of notes.
  # 
  getContent: ->
    Api.notes @project_id, (notes) =>
      $.each notes, (i, note) =>
        # render note if it not present in loaded list
        # or skip if rendered
        if $.inArray(note.id, @note_ids) == -1
          @note_ids.push(note.id)
          @renderNote(note)
          @scrollDown()
          $("abbr.timeago").timeago()

  initRefresh: ->
    setInterval =>
      @refresh()
    , 10000

  refresh: ->
    @getContent()

  scrollDown: ->
    notes = $('ul.notes')
    $('body, html').scrollTop(notes.height())

  initForm: ->
    form = $('.wall-note-form')
    form.find("#target_type").val('wall')

    form.on 'ajax:success', =>
      @refresh()
      form.find(".js-note-text").val("").trigger("input")
    
    form.on 'ajax:complete', ->
      form.find(".js-comment-button").removeAttr('disabled')
      form.find(".js-comment-button").removeClass('disabled')

    form.on "click", ".js-choose-note-attachment-button", ->
      form.find(".js-note-attachment-input").click()

    form.on "change", ".js-note-attachment-input", ->
      filename = $(this).val().replace(/^.*[\\\/]/, '')
      form.find(".js-attachment-filename").text(filename)
    
    form.find('.note_text').keydown (e) ->
      if e.ctrlKey && e.keyCode == 13
        form.find('.js-comment-button').submit()

    form.show()
  
  renderNote: (note) ->
    template = @noteTemplate()
    template = template.replace('{{author_name}}', note.author.name)
    template = template.replace('{{created_at}}', note.created_at)
    template = template.replace('{{text}}', linkify(sanitize(note.body)))

    if note.attachment
      file = '<i class="icon-paper-clip"/><a href="/files/note/' + note.id + '/' + note.attachment + '">' + note.attachment + '</a>'
    else
      file = ''
    template = template.replace('{{file}}', file)


    $('ul.notes').append(template)

  noteTemplate: ->
    return '<li>
      <strong class="wall-author">{{author_name}}</strong>
      <span class="wall-text">
        {{text}}
        <span class="wall-file">{{file}}</span>
      </span>
      <abbr class="timeago" title="{{created_at}}">{{created_at}}</abbr>
    </li>'

@Wall = Wall