From 8ca880c3b8d677366ed20dd1258a1b780cbd05a8 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Tue, 8 Mar 2016 09:09:39 +0000 Subject: [PATCH] Correctly fitlers remotely --- app/assets/javascripts/gl_dropdown.js.coffee | 36 ++++++++++++------- .../javascripts/labels_select.js.coffee | 2 +- .../javascripts/milestone_select.js.coffee | 4 +-- app/assets/javascripts/users_select.js.coffee | 35 ++++++++++++++---- app/helpers/dropdowns_helper.rb | 2 +- app/views/shared/issuable/_filter.html.haml | 8 +++++ 6 files changed, 64 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/gl_dropdown.js.coffee b/app/assets/javascripts/gl_dropdown.js.coffee index 8ab82dc2520..c00facd921d 100644 --- a/app/assets/javascripts/gl_dropdown.js.coffee +++ b/app/assets/javascripts/gl_dropdown.js.coffee @@ -1,22 +1,26 @@ class GitLabDropdownFilter BLUR_KEYCODES = [27, 40] - constructor: (@dropdown, @remote, @data, @callback) -> + constructor: (@dropdown, @remote, @query, @data, @callback) -> @input = @dropdown.find(".dropdown-input-field") # Key events + timeout = "" @input.on "keyup", (e) => - blur_field = @shouldBlur e.keyCode - search_text = @input.val() + clearTimeout timeout + timeout = setTimeout => + blur_field = @shouldBlur e.keyCode + search_text = @input.val() - if blur_field - @input.blur() + if blur_field + @input.blur() - if @remote - @remote search_text, (data) => - @callback(data) - else - @filter search_text + if @remote + @query search_text, (data) => + @callback(data) + else + @filter search_text + , 250 shouldBlur: (keyCode) -> return BLUR_KEYCODES.indexOf(keyCode) >= 0 @@ -38,7 +42,7 @@ class GitLabDropdownRemote @options.beforeSend() # Fetch the data by calling the data funcfion - @dataEndpoint (data) => + @dataEndpoint "", (data) => if @options.success @options.success(data) @@ -86,13 +90,14 @@ class GitLabDropdown # Init filiterable if @options.filterable - @filter = new GitLabDropdownFilter @dropdown, @options.query, => + @filter = new GitLabDropdownFilter @dropdown, @options.filterRemote, @options.data, => return @fullData , (data) => @parseData data # Event listeners $(@el).parent().on "shown.bs.dropdown", @opened + $(@el).parent().on "hidden.bs.dropdown", @hidden if @options.selectable @dropdown.on "click", "a", (e) -> @@ -124,6 +129,13 @@ class GitLabDropdown if @remote @remote.execute() + if @options.filterable + @dropdown.find(".dropdown-input-field").focus() + + hidden: => + if @options.filterable + @dropdown.find(".dropdown-input-field").blur().val("") + # Render the full menu renderMenu: (html) -> menu_html = "" diff --git a/app/assets/javascripts/labels_select.js.coffee b/app/assets/javascripts/labels_select.js.coffee index f79a8773533..56fe6dc9a7a 100644 --- a/app/assets/javascripts/labels_select.js.coffee +++ b/app/assets/javascripts/labels_select.js.coffee @@ -5,7 +5,7 @@ class @LabelsSelect selectedLabel = $(dropdown).data('selected') $(dropdown).glDropdown( - data: (callback) -> + data: (term, callback) -> Api.projectLabels 8, callback renderRow: (label) -> selected = if label.name is selectedLabel then "is-active" else "" diff --git a/app/assets/javascripts/milestone_select.js.coffee b/app/assets/javascripts/milestone_select.js.coffee index 184479e209c..4256158660a 100644 --- a/app/assets/javascripts/milestone_select.js.coffee +++ b/app/assets/javascripts/milestone_select.js.coffee @@ -5,11 +5,11 @@ class @MilestoneSelect selectedMilestone = $(dropdown).data('selected') $(dropdown).glDropdown( - data: (callback) -> + data: (term, callback) -> Api.milestones projectId, callback filterable: true search: - fields: ['name'] + fields: ['title'] selectable: true fieldName: $(dropdown).data('field-name') text: (milestone) -> diff --git a/app/assets/javascripts/users_select.js.coffee b/app/assets/javascripts/users_select.js.coffee index 2b5c2f43818..77db99a617f 100644 --- a/app/assets/javascripts/users_select.js.coffee +++ b/app/assets/javascripts/users_select.js.coffee @@ -6,20 +6,41 @@ class @UsersSelect $('.js-user-search').each (i, dropdown) => projectId = $(dropdown).data('project-id') showNullUser = $(dropdown).data('null-user') + showAnyUser = $(dropdown).data('any-user') + firstUser = $(dropdown).data('first-user') selectedId = $(dropdown).data('selected') $(dropdown).glDropdown( - data: (callback) => - @users "", (users) => - if showNullUser - users.unshift( - name: 'Unassigned', - id: 0 - ) + data: (term, callback) => + @users term, (users) => + if term.length is 0 + if firstUser + # Move current user to the front of the list + for obj, index in users + if obj.username == firstUser + users.splice(index, 1) + users.unshift(obj) + break + + if showNullUser + users.unshift( + name: 'Unassigned', + id: 0 + ) + + if showAnyUser + name = showAnyUser + name = 'Any User' if name == true + anyUser = { + name: name, + id: null + } + users.unshift(anyUser) # Send the data back callback users filterable: true + filterRemote: true search: fields: ['name', 'username'] selectable: true diff --git a/app/helpers/dropdowns_helper.rb b/app/helpers/dropdowns_helper.rb index 5e3ee734314..73b3e70f3ce 100644 --- a/app/helpers/dropdowns_helper.rb +++ b/app/helpers/dropdowns_helper.rb @@ -17,7 +17,7 @@ module DropdownsHelper output += content_tag :div, class: "dropdown-title" do title_output = content_tag(:span, title) - title_output += content_tag :button, class: "dropdown-title-button dropdown-menu-close", aria: {label: "close"} do + title_output += content_tag :button, class: "dropdown-title-button dropdown-menu-close", aria: {label: "close"}, type: "button" do icon('times') end.html_safe end diff --git a/app/views/shared/issuable/_filter.html.haml b/app/views/shared/issuable/_filter.html.haml index 402fc7535ba..0a8ec7cde5b 100644 --- a/app/views/shared/issuable/_filter.html.haml +++ b/app/views/shared/issuable/_filter.html.haml @@ -7,18 +7,26 @@ class: "check_all_issues left" .issues-other-filters .filter-item.inline + - if params[:author_id] + = hidden_field_tag(:author_id, params[:author_id]) = dropdown_tag("Author", toggle_class: "js-user-search", title: "Filter by author", filter: true, dropdown_class: "dropdown-menu-user dropdown-menu-selectable", placeholder: "Search authors", data: {any_user: "Any Author", first_user: true, current_user: true, project_id: @project.id, selected: params[:author_id], field_name: "author_id"}) .filter-item.inline + - if params[:assignee_id] + = hidden_field_tag(:assignee_id, params[:assignee_id]) = dropdown_tag("Assignee", toggle_class: "js-user-search", title: "Filter by assignee", filter: true, dropdown_class: "dropdown-menu-user dropdown-menu-selectable", placeholder: "Search assignee", data: {any_user: "Any Author", first_user: true, null_user: true, current_user: true, project_id: @project.id, selected: params[:assignee_id], field_name: "assignee_id"}) .filter-item.inline.milestone-filter + - if params[:milestone_title] + = hidden_field_tag(:milestone_title, params[:milestone_title]) = dropdown_tag("Milestone", title: "Filter by milestone", toggle_class: 'js-milestone-select', filter: true, dropdown_class: "dropdown-menu-selectable", placeholder: "Search milestones", data: {field_name: "milestone_title", selected: params[:milestone_title], project_id: @project.id}) .filter-item.inline.labels-filter + - if params[:label_name] + = hidden_field_tag(:label_name, params[:label_name]) = dropdown_tag("Label", title: "Filter by label", toggle_class: "js-label-select", filter: true, dropdown_class: "dropdown-menu-labels dropdown-menu-selectable", placeholder: "Search labels", footer_content: true, data: {field_name: "label_name", selected: params[:label_name], project_id: @project.id}) do %ul.dropdown-footer-list