wall.js.coffee 2.3 KB
Newer Older
1
2
3
4
5
6
7
class Wall
  constructor: (project_id) ->
    @project_id = project_id
    @note_ids = []
    @getContent()
    @initRefresh()
    @initForm()
8
9
10
11
12
  
  # 
  # Gets an initial set of notes.
  # 
  getContent: ->
13
14
    Api.notes @project_id, (notes) =>
      $.each notes, (i, note) =>
15
16
        # render note if it not present in loaded list
        # or skip if rendered
17
18
19
20
        if $.inArray(note.id, @note_ids) == -1
          @note_ids.push(note.id)
          @renderNote(note)
          @scrollDown()
21
          $("abbr.timeago").timeago()
22
23

  initRefresh: ->
24
25
26
    setInterval =>
      @refresh()
    , 10000
27
28

  refresh: ->
29
    @getContent()
30
31
32

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

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

39
40
    form.on 'ajax:success', =>
      @refresh()
41
42
43
44
45
46
47
48
49
50
51
52
53
      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)
    
54
55
56
57
    form.find('.note_text').keydown (e) ->
      if e.ctrlKey && e.keyCode == 13
        form.find('.js-comment-button').submit()

58
    form.show()
59
60
  
  renderNote: (note) ->
61
    template = @noteTemplate()
62
    template = template.replace('{{author_name}}', note.author.name)
63
    template = template.replace(/{{created_at}}/g, note.created_at)
64
    template = template.replace('{{text}}', linkify(sanitize(note.body)))
65
66

    if note.attachment
67
68
69
70
71
72
73
      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)
74

75
76
77
78
79
80
81
82
83
  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>'
84
85

@Wall = Wall