diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-11-15 17:25:09 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-11-15 17:25:09 +0400 |
commit | dcea191314c778331305d0926d6852fe04477115 (patch) | |
tree | 53be24b9be56998e6ca963db4c86394e39d1132c /app | |
parent | 0759dd4513c0190b80058d4851e2bde36cbaede6 (diff) |
Select2 tag for namespaces with ajax loading
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/api.js.coffee | 15 | ||||
-rw-r--r-- | app/assets/javascripts/namespace_select.js.coffee | 24 | ||||
-rw-r--r-- | app/helpers/namespaces_helper.rb | 9 |
3 files changed, 48 insertions, 0 deletions
diff --git a/app/assets/javascripts/api.js.coffee b/app/assets/javascripts/api.js.coffee index a36d944cbcb..5f4a38ebbd0 100644 --- a/app/assets/javascripts/api.js.coffee +++ b/app/assets/javascripts/api.js.coffee @@ -2,6 +2,7 @@ users_path: "/api/:version/users.json" user_path: "/api/:version/users/:id.json" notes_path: "/api/:version/projects/:id/notes.json" + namespaces_path: "/api/:version/namespaces.json" # Get 20 (depends on api) recent notes # and sort the ascending from oldest to newest @@ -49,6 +50,20 @@ ).done (users) -> callback(users) + # Return namespaces list. Filtered by query + namespaces: (query, callback) -> + url = Api.buildUrl(Api.namespaces_path) + + $.ajax( + url: url + data: + private_token: gon.api_token + search: query + per_page: 20 + dataType: "json" + ).done (namespaces) -> + callback(namespaces) + buildUrl: (url) -> url = gon.relative_url_root + url if gon.relative_url_root? return url.replace(':version', gon.api_version) diff --git a/app/assets/javascripts/namespace_select.js.coffee b/app/assets/javascripts/namespace_select.js.coffee new file mode 100644 index 00000000000..00d135d1449 --- /dev/null +++ b/app/assets/javascripts/namespace_select.js.coffee @@ -0,0 +1,24 @@ +$ -> + namespaceFormatResult = (namespace) -> + markup = "<div class='namespace-result'>" + markup += "<span class='namespace-kind'>" + namespace.kind + "</span>" + markup += "<span class='namespace-path'>" + namespace.path + "</span>" + markup += "</div>" + markup + + formatSelection = (namespace) -> + namespace.kind + ": " + namespace.path + + $('.ajax-namespace-select').each (i, select) -> + $(select).select2 + placeholder: "Search for namespace" + multiple: $(select).hasClass('multiselect') + minimumInputLength: 0 + query: (query) -> + Api.namespaces query.term, (namespaces) -> + data = { results: namespaces } + query.callback(data) + + dropdownCssClass: "ajax-namespace-dropdown" + formatResult: namespaceFormatResult + formatSelection: formatSelection diff --git a/app/helpers/namespaces_helper.rb b/app/helpers/namespaces_helper.rb index f7979c8b641..c363c7ffd74 100644 --- a/app/helpers/namespaces_helper.rb +++ b/app/helpers/namespaces_helper.rb @@ -16,4 +16,13 @@ module NamespacesHelper grouped_options_for_select(options, selected) end + + def namespace_select_tag(id, opts = {}) + css_class = "ajax-namespace-select " + css_class << "multiselect " if opts[:multiple] + css_class << (opts[:class] || '') + value = opts[:selected] || '' + + hidden_field_tag(id, value, class: css_class) + end end |