Use Api.js to handle api calls to gitlab
This commit is contained in:
parent
a163135cb5
commit
3cdac0b934
3 changed files with 70 additions and 42 deletions
53
app/assets/javascripts/api.js.coffee
Normal file
53
app/assets/javascripts/api.js.coffee
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
@Api =
|
||||||
|
users_path: "/api/:version/users.json"
|
||||||
|
user_path: "/api/:version/users/:id.json"
|
||||||
|
notes_path: "/api/:version/projects/:id/notes.json"
|
||||||
|
|
||||||
|
# Get 20 (depends on api) recent notes
|
||||||
|
# and sort the ascending from oldest to newest
|
||||||
|
notes: (project_id, callback) ->
|
||||||
|
url = Api.buildUrl(Api.notes_path)
|
||||||
|
url = url.replace(':id', project_id)
|
||||||
|
|
||||||
|
$.ajax(
|
||||||
|
url: url,
|
||||||
|
data:
|
||||||
|
private_token: gon.api_token
|
||||||
|
gfm: true
|
||||||
|
recent: true
|
||||||
|
dataType: "json"
|
||||||
|
).done (notes) ->
|
||||||
|
notes.sort (a, b) ->
|
||||||
|
return a.id - b.id
|
||||||
|
callback(notes)
|
||||||
|
|
||||||
|
user: (user_id, callback) ->
|
||||||
|
url = Api.buildUrl(Api.user_path)
|
||||||
|
url = url.replace(':id', user_id)
|
||||||
|
|
||||||
|
$.ajax(
|
||||||
|
url: url
|
||||||
|
data:
|
||||||
|
private_token: gon.api_token
|
||||||
|
dataType: "json"
|
||||||
|
).done (user) ->
|
||||||
|
callback(user)
|
||||||
|
|
||||||
|
# Return users list. Filtered by query
|
||||||
|
# Only active users retrieved
|
||||||
|
users: (query, callback) ->
|
||||||
|
url = Api.buildUrl(Api.users_path)
|
||||||
|
|
||||||
|
$.ajax(
|
||||||
|
url: url
|
||||||
|
data:
|
||||||
|
private_token: gon.api_token
|
||||||
|
search: query
|
||||||
|
per_page: 20
|
||||||
|
active: true
|
||||||
|
dataType: "json"
|
||||||
|
).done (users) ->
|
||||||
|
callback(users)
|
||||||
|
|
||||||
|
buildUrl: (url) ->
|
||||||
|
return url.replace(':version', gon.api_version)
|
|
@ -18,34 +18,19 @@ $ ->
|
||||||
placeholder: "Search for a user"
|
placeholder: "Search for a user"
|
||||||
multiple: $('.ajax-users-select').hasClass('multiselect')
|
multiple: $('.ajax-users-select').hasClass('multiselect')
|
||||||
minimumInputLength: 0
|
minimumInputLength: 0
|
||||||
ajax: # instead of writing the function to execute the request we use Select2's convenient helper
|
query: (query) ->
|
||||||
url: "/api/" + gon.api_version + "/users.json"
|
Api.users query.term, (users) ->
|
||||||
dataType: "json"
|
data = { results: users }
|
||||||
data: (term, page) ->
|
query.callback(data)
|
||||||
search: term # search term
|
|
||||||
per_page: 10
|
|
||||||
active: true
|
|
||||||
private_token: gon.api_token
|
|
||||||
|
|
||||||
results: (data, page) -> # parse the results into the format expected by Select2.
|
|
||||||
# since we are using custom formatting functions we do not need to alter remote JSON data
|
|
||||||
results: data
|
|
||||||
|
|
||||||
initSelection: (element, callback) ->
|
initSelection: (element, callback) ->
|
||||||
id = $(element).val()
|
id = $(element).val()
|
||||||
if id isnt ""
|
if id isnt ""
|
||||||
$.ajax(
|
Api.user(id, callback)
|
||||||
"/api/" + gon.api_version + "/users/" + id + ".json",
|
|
||||||
dataType: "json"
|
|
||||||
data:
|
|
||||||
private_token: gon.api_token
|
|
||||||
).done (data) ->
|
|
||||||
callback data
|
|
||||||
|
|
||||||
|
|
||||||
formatResult: userFormatResult # omitted for brevity, see the source of this page
|
formatResult: userFormatResult
|
||||||
formatSelection: userFormatSelection # omitted for brevity, see the source of this page
|
formatSelection: userFormatSelection
|
||||||
dropdownCssClass: "ajax-users-dropdown" # apply css that makes the dropdown taller
|
dropdownCssClass: "ajax-users-dropdown"
|
||||||
escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
|
escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results
|
||||||
m
|
m
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
@Wall =
|
@Wall =
|
||||||
note_ids: []
|
note_ids: []
|
||||||
notes_path: null
|
|
||||||
notes_params: null
|
|
||||||
project_id: null
|
project_id: null
|
||||||
|
|
||||||
init: (project_id) ->
|
init: (project_id) ->
|
||||||
Wall.project_id = project_id
|
Wall.project_id = project_id
|
||||||
Wall.notes_path = "/api/" + gon.api_version + "/projects/" + project_id + "/notes.json"
|
|
||||||
Wall.getContent()
|
Wall.getContent()
|
||||||
Wall.initRefresh()
|
Wall.initRefresh()
|
||||||
Wall.initForm()
|
Wall.initForm()
|
||||||
|
@ -15,17 +12,10 @@
|
||||||
# Gets an initial set of notes.
|
# Gets an initial set of notes.
|
||||||
#
|
#
|
||||||
getContent: ->
|
getContent: ->
|
||||||
$.ajax
|
Api.notes Wall.project_id, (notes) ->
|
||||||
url: Wall.notes_path,
|
$.each notes, (i, note) ->
|
||||||
data:
|
# render note if it not present in loaded list
|
||||||
private_token: gon.api_token
|
# or skip if rendered
|
||||||
gfm: true
|
|
||||||
recent: true
|
|
||||||
dataType: "json"
|
|
||||||
success: (notes) ->
|
|
||||||
notes.sort (a, b) ->
|
|
||||||
return a.id - b.id
|
|
||||||
$.each notes, (i, note)->
|
|
||||||
if $.inArray(note.id, Wall.note_ids) == -1
|
if $.inArray(note.id, Wall.note_ids) == -1
|
||||||
Wall.note_ids.push(note.id)
|
Wall.note_ids.push(note.id)
|
||||||
Wall.renderNote(note)
|
Wall.renderNote(note)
|
||||||
|
|
Loading…
Reference in a new issue