wall.js.coffee 2.33 KB
Newer Older
1
2
3
4
5
6
7
8
@Wall =
  note_ids: []
  project_id: null

  init: (project_id) ->
    Wall.project_id = project_id
    Wall.getContent()
    Wall.initRefresh()
9
    Wall.initForm()
10
11
12
13
14
  
  # 
  # Gets an initial set of notes.
  # 
  getContent: ->
15
16
17
18
19
20
21
22
23
    Api.notes Wall.project_id, (notes) ->
      $.each notes, (i, note) ->
        # render note if it not present in loaded list
        # or skip if rendered
        if $.inArray(note.id, Wall.note_ids) == -1
          Wall.note_ids.push(note.id)
          Wall.renderNote(note)
          Wall.scrollDown()
          $("abbr.timeago").timeago()
24
25
26
27
28
29

  initRefresh: ->
    setInterval("Wall.refresh()", 10000)

  refresh: ->
    Wall.getContent()
30
31
32

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

  initForm: ->
36
    form = $('.wall-note-form')
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    form.find("#target_type").val('wall')

    form.on 'ajax:success', ->
      Wall.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)
    
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
62
63
64
    template = Wall.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)))
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>'