debian-mirror-gitlab/app/assets/javascripts/gfm_auto_complete.js.coffee

131 lines
3.3 KiB
CoffeeScript
Raw Normal View History

2014-09-02 18:07:02 +05:30
# Creates the variables for setting up GFM auto-completion
window.GitLab ?= {}
GitLab.GfmAutoComplete =
2016-06-02 11:05:42 +05:30
dataLoading: false
2014-09-02 18:07:02 +05:30
dataSource: ''
2015-09-11 14:41:01 +05:30
2014-09-02 18:07:02 +05:30
# Emoji
Emoji:
2015-09-11 14:41:01 +05:30
template: '<li>${name} <img alt="${name}" height="20" src="${path}" width="20" /></li>'
2014-09-02 18:07:02 +05:30
# Team Members
Members:
2015-09-11 14:41:01 +05:30
template: '<li>${username} <small>${title}</small></li>'
2014-09-02 18:07:02 +05:30
# Issues and MergeRequests
Issues:
2015-09-11 14:41:01 +05:30
template: '<li><small>${id}</small> ${title}</li>'
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
# Milestones
Milestones:
template: '<li>${title}</li>'
2014-09-02 18:07:02 +05:30
# Add GFM auto-completion to all input fields, that accept GFM input.
2016-06-02 11:05:42 +05:30
setup: (wrap) ->
@input = $('.js-gfm-input')
# destroy previous instances
@destroyAtWho()
# set up instances
@setupAtWho()
if @dataSource
if !@dataLoading
@dataLoading = true
# We should wait until initializations are done
# and only trigger the last .setup since
# The previous .dataSource belongs to the previous issuable
# and the last one will have the **proper** .dataSource property
# TODO: Make this a singleton and turn off events when moving to another page
setTimeout( =>
fetch = @fetchData(@dataSource)
fetch.done (data) =>
@dataLoading = false
@loadData(data)
, 1000)
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
setupAtWho: ->
2014-09-02 18:07:02 +05:30
# Emoji
2016-06-02 11:05:42 +05:30
@input.atwho
2014-09-02 18:07:02 +05:30
at: ':'
2015-09-11 14:41:01 +05:30
displayTpl: @Emoji.template
insertTpl: ':${name}:'
2014-09-02 18:07:02 +05:30
# Team Members
2016-06-02 11:05:42 +05:30
@input.atwho
2014-09-02 18:07:02 +05:30
at: '@'
2015-09-11 14:41:01 +05:30
displayTpl: @Members.template
insertTpl: '${atwho-at}${username}'
searchKey: 'search'
2014-09-02 18:07:02 +05:30
callbacks:
2015-09-11 14:41:01 +05:30
beforeSave: (members) ->
$.map members, (m) ->
2015-09-11 14:41:01 +05:30
title = m.name
title += " (#{m.count})" if m.count
username: m.username
title: sanitize(title)
search: sanitize("#{m.username} #{m.name}")
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
@input.atwho
2014-09-02 18:07:02 +05:30
at: '#'
alias: 'issues'
2015-09-11 14:41:01 +05:30
searchKey: 'search'
displayTpl: @Issues.template
insertTpl: '${atwho-at}${id}'
2014-09-02 18:07:02 +05:30
callbacks:
2015-09-11 14:41:01 +05:30
beforeSave: (issues) ->
$.map issues, (i) ->
2015-09-11 14:41:01 +05:30
id: i.iid
title: sanitize(i.title)
search: "#{i.iid} #{i.title}"
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
@input.atwho
at: '%'
alias: 'milestones'
searchKey: 'search'
displayTpl: @Milestones.template
insertTpl: '${atwho-at}"${title}"'
callbacks:
beforeSave: (milestones) ->
$.map milestones, (m) ->
id: m.iid
title: sanitize(m.title)
search: "#{m.title}"
@input.atwho
2014-09-02 18:07:02 +05:30
at: '!'
alias: 'mergerequests'
2015-09-11 14:41:01 +05:30
searchKey: 'search'
displayTpl: @Issues.template
insertTpl: '${atwho-at}${id}'
2014-09-02 18:07:02 +05:30
callbacks:
2015-09-11 14:41:01 +05:30
beforeSave: (merges) ->
$.map merges, (m) ->
2015-09-11 14:41:01 +05:30
id: m.iid
title: sanitize(m.title)
search: "#{m.iid} #{m.title}"
2014-09-02 18:07:02 +05:30
2016-06-02 11:05:42 +05:30
destroyAtWho: ->
@input.atwho('destroy')
fetchData: (dataSource) ->
$.getJSON(dataSource)
loadData: (data) ->
# load members
@input.atwho 'load', '@', data.members
# load issues
@input.atwho 'load', 'issues', data.issues
# load milestones
@input.atwho 'load', 'milestones', data.milestones
# load merge requests
@input.atwho 'load', 'mergerequests', data.mergerequests
# load emojis
@input.atwho 'load', ':', data.emojis