Commit 3cdac0b93440db5a3b0871a06be53bf8767b7dc8
1 parent
a163135c
Exists in
master
and in
4 other branches
Use Api.js to handle api calls to gitlab
Showing
3 changed files
with
70 additions
and
42 deletions
Show diff stats
... | ... | @@ -0,0 +1,53 @@ |
1 | +@Api = | |
2 | + users_path: "/api/:version/users.json" | |
3 | + user_path: "/api/:version/users/:id.json" | |
4 | + notes_path: "/api/:version/projects/:id/notes.json" | |
5 | + | |
6 | + # Get 20 (depends on api) recent notes | |
7 | + # and sort the ascending from oldest to newest | |
8 | + notes: (project_id, callback) -> | |
9 | + url = Api.buildUrl(Api.notes_path) | |
10 | + url = url.replace(':id', project_id) | |
11 | + | |
12 | + $.ajax( | |
13 | + url: url, | |
14 | + data: | |
15 | + private_token: gon.api_token | |
16 | + gfm: true | |
17 | + recent: true | |
18 | + dataType: "json" | |
19 | + ).done (notes) -> | |
20 | + notes.sort (a, b) -> | |
21 | + return a.id - b.id | |
22 | + callback(notes) | |
23 | + | |
24 | + user: (user_id, callback) -> | |
25 | + url = Api.buildUrl(Api.user_path) | |
26 | + url = url.replace(':id', user_id) | |
27 | + | |
28 | + $.ajax( | |
29 | + url: url | |
30 | + data: | |
31 | + private_token: gon.api_token | |
32 | + dataType: "json" | |
33 | + ).done (user) -> | |
34 | + callback(user) | |
35 | + | |
36 | + # Return users list. Filtered by query | |
37 | + # Only active users retrieved | |
38 | + users: (query, callback) -> | |
39 | + url = Api.buildUrl(Api.users_path) | |
40 | + | |
41 | + $.ajax( | |
42 | + url: url | |
43 | + data: | |
44 | + private_token: gon.api_token | |
45 | + search: query | |
46 | + per_page: 20 | |
47 | + active: true | |
48 | + dataType: "json" | |
49 | + ).done (users) -> | |
50 | + callback(users) | |
51 | + | |
52 | + buildUrl: (url) -> | |
53 | + return url.replace(':version', gon.api_version) | ... | ... |
app/assets/javascripts/users_select.js.coffee
... | ... | @@ -18,34 +18,19 @@ $ -> |
18 | 18 | placeholder: "Search for a user" |
19 | 19 | multiple: $('.ajax-users-select').hasClass('multiselect') |
20 | 20 | minimumInputLength: 0 |
21 | - ajax: # instead of writing the function to execute the request we use Select2's convenient helper | |
22 | - url: "/api/" + gon.api_version + "/users.json" | |
23 | - dataType: "json" | |
24 | - data: (term, page) -> | |
25 | - search: term # search term | |
26 | - per_page: 10 | |
27 | - active: true | |
28 | - private_token: gon.api_token | |
29 | - | |
30 | - results: (data, page) -> # parse the results into the format expected by Select2. | |
31 | - # since we are using custom formatting functions we do not need to alter remote JSON data | |
32 | - results: data | |
21 | + query: (query) -> | |
22 | + Api.users query.term, (users) -> | |
23 | + data = { results: users } | |
24 | + query.callback(data) | |
33 | 25 | |
34 | 26 | initSelection: (element, callback) -> |
35 | 27 | id = $(element).val() |
36 | 28 | if id isnt "" |
37 | - $.ajax( | |
38 | - "/api/" + gon.api_version + "/users/" + id + ".json", | |
39 | - dataType: "json" | |
40 | - data: | |
41 | - private_token: gon.api_token | |
42 | - ).done (data) -> | |
43 | - callback data | |
29 | + Api.user(id, callback) | |
44 | 30 | |
45 | 31 | |
46 | - formatResult: userFormatResult # omitted for brevity, see the source of this page | |
47 | - formatSelection: userFormatSelection # omitted for brevity, see the source of this page | |
48 | - dropdownCssClass: "ajax-users-dropdown" # apply css that makes the dropdown taller | |
32 | + formatResult: userFormatResult | |
33 | + formatSelection: userFormatSelection | |
34 | + dropdownCssClass: "ajax-users-dropdown" | |
49 | 35 | escapeMarkup: (m) -> # we do not want to escape markup since we are displaying html in results |
50 | 36 | m |
51 | - | ... | ... |
app/assets/javascripts/wall.js.coffee
1 | 1 | @Wall = |
2 | 2 | note_ids: [] |
3 | - notes_path: null | |
4 | - notes_params: null | |
5 | 3 | project_id: null |
6 | 4 | |
7 | 5 | init: (project_id) -> |
8 | 6 | Wall.project_id = project_id |
9 | - Wall.notes_path = "/api/" + gon.api_version + "/projects/" + project_id + "/notes.json" | |
10 | 7 | Wall.getContent() |
11 | 8 | Wall.initRefresh() |
12 | 9 | Wall.initForm() |
... | ... | @@ -15,22 +12,15 @@ |
15 | 12 | # Gets an initial set of notes. |
16 | 13 | # |
17 | 14 | getContent: -> |
18 | - $.ajax | |
19 | - url: Wall.notes_path, | |
20 | - data: | |
21 | - private_token: gon.api_token | |
22 | - gfm: true | |
23 | - recent: true | |
24 | - dataType: "json" | |
25 | - success: (notes) -> | |
26 | - notes.sort (a, b) -> | |
27 | - return a.id - b.id | |
28 | - $.each notes, (i, note)-> | |
29 | - if $.inArray(note.id, Wall.note_ids) == -1 | |
30 | - Wall.note_ids.push(note.id) | |
31 | - Wall.renderNote(note) | |
32 | - Wall.scrollDown() | |
33 | - $("abbr.timeago").timeago() | |
15 | + Api.notes Wall.project_id, (notes) -> | |
16 | + $.each notes, (i, note) -> | |
17 | + # render note if it not present in loaded list | |
18 | + # or skip if rendered | |
19 | + if $.inArray(note.id, Wall.note_ids) == -1 | |
20 | + Wall.note_ids.push(note.id) | |
21 | + Wall.renderNote(note) | |
22 | + Wall.scrollDown() | |
23 | + $("abbr.timeago").timeago() | |
34 | 24 | |
35 | 25 | initRefresh: -> |
36 | 26 | setInterval("Wall.refresh()", 10000) | ... | ... |