Commit dcea191314c778331305d0926d6852fe04477115
1 parent
0759dd45
Exists in
master
and in
4 other branches
Select2 tag for namespaces with ajax loading
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
3 changed files
with
48 additions
and
0 deletions
Show diff stats
app/assets/javascripts/api.js.coffee
... | ... | @@ -2,6 +2,7 @@ |
2 | 2 | users_path: "/api/:version/users.json" |
3 | 3 | user_path: "/api/:version/users/:id.json" |
4 | 4 | notes_path: "/api/:version/projects/:id/notes.json" |
5 | + namespaces_path: "/api/:version/namespaces.json" | |
5 | 6 | |
6 | 7 | # Get 20 (depends on api) recent notes |
7 | 8 | # and sort the ascending from oldest to newest |
... | ... | @@ -49,6 +50,20 @@ |
49 | 50 | ).done (users) -> |
50 | 51 | callback(users) |
51 | 52 | |
53 | + # Return namespaces list. Filtered by query | |
54 | + namespaces: (query, callback) -> | |
55 | + url = Api.buildUrl(Api.namespaces_path) | |
56 | + | |
57 | + $.ajax( | |
58 | + url: url | |
59 | + data: | |
60 | + private_token: gon.api_token | |
61 | + search: query | |
62 | + per_page: 20 | |
63 | + dataType: "json" | |
64 | + ).done (namespaces) -> | |
65 | + callback(namespaces) | |
66 | + | |
52 | 67 | buildUrl: (url) -> |
53 | 68 | url = gon.relative_url_root + url if gon.relative_url_root? |
54 | 69 | return url.replace(':version', gon.api_version) | ... | ... |
... | ... | @@ -0,0 +1,24 @@ |
1 | +$ -> | |
2 | + namespaceFormatResult = (namespace) -> | |
3 | + markup = "<div class='namespace-result'>" | |
4 | + markup += "<span class='namespace-kind'>" + namespace.kind + "</span>" | |
5 | + markup += "<span class='namespace-path'>" + namespace.path + "</span>" | |
6 | + markup += "</div>" | |
7 | + markup | |
8 | + | |
9 | + formatSelection = (namespace) -> | |
10 | + namespace.kind + ": " + namespace.path | |
11 | + | |
12 | + $('.ajax-namespace-select').each (i, select) -> | |
13 | + $(select).select2 | |
14 | + placeholder: "Search for namespace" | |
15 | + multiple: $(select).hasClass('multiselect') | |
16 | + minimumInputLength: 0 | |
17 | + query: (query) -> | |
18 | + Api.namespaces query.term, (namespaces) -> | |
19 | + data = { results: namespaces } | |
20 | + query.callback(data) | |
21 | + | |
22 | + dropdownCssClass: "ajax-namespace-dropdown" | |
23 | + formatResult: namespaceFormatResult | |
24 | + formatSelection: formatSelection | ... | ... |
app/helpers/namespaces_helper.rb
... | ... | @@ -16,4 +16,13 @@ module NamespacesHelper |
16 | 16 | |
17 | 17 | grouped_options_for_select(options, selected) |
18 | 18 | end |
19 | + | |
20 | + def namespace_select_tag(id, opts = {}) | |
21 | + css_class = "ajax-namespace-select " | |
22 | + css_class << "multiselect " if opts[:multiple] | |
23 | + css_class << (opts[:class] || '') | |
24 | + value = opts[:selected] || '' | |
25 | + | |
26 | + hidden_field_tag(id, value, class: css_class) | |
27 | + end | |
19 | 28 | end | ... | ... |