gitlab-org--gitlab-foss/app/assets/javascripts/wall.js.coffee

86 lines
2.3 KiB
CoffeeScript
Raw Normal View History

2013-05-02 07:36:53 -04:00
class Wall
constructor: (project_id) ->
@project_id = project_id
@note_ids = []
@getContent()
@initRefresh()
@initForm()
2013-03-19 06:35:55 -04:00
#
# Gets an initial set of notes.
#
getContent: ->
2013-05-02 07:36:53 -04:00
Api.notes @project_id, (notes) =>
$.each notes, (i, note) =>
# render note if it not present in loaded list
# or skip if rendered
2013-05-02 07:36:53 -04:00
if $.inArray(note.id, @note_ids) == -1
@note_ids.push(note.id)
@renderNote(note)
@scrollDown()
$("abbr.timeago").timeago()
2013-03-19 06:35:55 -04:00
initRefresh: ->
2013-05-02 07:36:53 -04:00
setInterval =>
@refresh()
, 10000
2013-03-19 06:35:55 -04:00
refresh: ->
2013-05-02 07:36:53 -04:00
@getContent()
2013-03-19 07:28:29 -04:00
scrollDown: ->
notes = $('ul.notes')
$('body, html').scrollTop(notes.height())
2013-03-19 07:28:29 -04:00
initForm: ->
2013-03-19 08:39:32 -04:00
form = $('.wall-note-form')
2013-03-19 07:28:29 -04:00
form.find("#target_type").val('wall')
2013-05-02 07:36:53 -04:00
form.on 'ajax:success', =>
@refresh()
2013-03-19 07:28:29 -04:00
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)
2013-03-20 03:53:39 -04:00
form.find('.note_text').keydown (e) ->
if e.ctrlKey && e.keyCode == 13
form.find('.js-comment-button').submit()
2013-03-19 07:28:29 -04:00
form.show()
2013-03-19 08:39:32 -04:00
renderNote: (note) ->
2013-05-02 07:36:53 -04:00
template = @noteTemplate()
template = template.replace('{{author_name}}', note.author.name)
template = template.replace(/{{created_at}}/g, note.created_at)
2013-05-24 02:49:49 -04:00
template = template.replace('{{text}}', simpleFormat(note.body))
2013-03-19 08:39:32 -04:00
if note.attachment
file = '<i class="icon-paper-clip"/><a href="' + gon.relative_url_root + '/files/note/' + note.id + '/' + note.attachment + '">' + note.attachment + '</a>'
else
file = ''
template = template.replace('{{file}}', file)
$('ul.notes').append(template)
2013-03-19 08:39:32 -04:00
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>'
2013-05-02 07:36:53 -04:00
@Wall = Wall