From 4cdaf7bcdfdb983b1f0648af2b8eb527e8118b7f Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 14 Dec 2020 17:40:41 -0600 Subject: [PATCH] autocomplete: update html data attributes. * Remove the `source` and `weight` html data attributes (no longer used). * Make the `type` html data attribute properly indicate the completion type. Valid types: `tag`, `tag-alias`, `tag-abbreviation`, `tag-autocorrect`, `tag-other-name`. --- app/javascript/src/javascripts/autocomplete.js.erb | 6 ++---- app/javascript/src/styles/common/autocomplete.scss | 2 +- app/logical/autocomplete_service.rb | 10 ++++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/javascript/src/javascripts/autocomplete.js.erb b/app/javascript/src/javascripts/autocomplete.js.erb index 2d4818a95..de025cd66 100644 --- a/app/javascript/src/javascripts/autocomplete.js.erb +++ b/app/javascript/src/javascripts/autocomplete.js.erb @@ -180,10 +180,8 @@ Autocomplete.render_item = function(list, item) { $link.append($post_count); } - if (item.type === "tag") { + if (/^tag/.test(item.type)) { $link.addClass("tag-type-" + item.category); - } else if (item.type === "tag_autocorrect") { - $link.addClass(`tag-type-${item.category} tag-type-autocorrect`); } else if (item.type === "user") { var level_class = "user-" + item.level.toLowerCase(); $link.addClass(level_class); @@ -194,7 +192,7 @@ Autocomplete.render_item = function(list, item) { var $menu_item = $("
").append($link); var $list_item = $("
  • ").data("item.autocomplete", item).append($menu_item); - var data_attributes = ["type", "source", "antecedent", "value", "category", "post_count", "weight"]; + var data_attributes = ["type", "antecedent", "value", "category", "post_count"]; data_attributes.forEach(attr => { $list_item.attr(`data-autocomplete-${attr.replace(/_/g, "-")}`, item[attr]); }); diff --git a/app/javascript/src/styles/common/autocomplete.scss b/app/javascript/src/styles/common/autocomplete.scss index 44158a614..e8a30e453 100644 --- a/app/javascript/src/styles/common/autocomplete.scss +++ b/app/javascript/src/styles/common/autocomplete.scss @@ -22,7 +22,7 @@ color: var(--autocomplete-arrow-color); } - a.tag-type-autocorrect .autocomplete-antecedent { + li[data-autocomplete-type="tag-autocorrect"] .autocomplete-antecedent { text-decoration: dotted underline; } } diff --git a/app/logical/autocomplete_service.rb b/app/logical/autocomplete_service.rb index 2d4f5d614..845376885 100644 --- a/app/logical/autocomplete_service.rb +++ b/app/logical/autocomplete_service.rb @@ -92,7 +92,9 @@ class AutocompleteService tags = Tag.from(union).order(post_count: :desc).limit(limit).includes(:consequent_aliases) tags.map do |tag| - { type: "tag", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: tag.tag_alias_for_pattern(string)&.antecedent_name } + antecedent = tag.tag_alias_for_pattern(string)&.antecedent_name + type = antecedent.present? ? "tag-alias" : "tag" + { type: type, label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: antecedent } end end @@ -100,7 +102,7 @@ class AutocompleteService tags = Tag.nonempty.abbreviation_matches(string).order(post_count: :desc).limit(limit) tags.map do |tag| - { type: "tag", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: "/" + tag.abbreviation } + { type: "tag-abbreviation", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: "/" + tag.abbreviation } end end @@ -109,7 +111,7 @@ class AutocompleteService tags = Tag.nonempty.autocorrect_matches(string).limit(limit) tags.map do |tag| - { type: "tag_autocorrect", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: string } + { type: "tag-autocorrect", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: string } end end @@ -124,7 +126,7 @@ class AutocompleteService tags.map do |tag| other_names = tag.artist&.other_names.to_a + tag.wiki_page&.other_names.to_a antecedent = other_names.find { |other_name| other_name.ilike?(string) } - { type: "tag", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: antecedent } + { type: "tag-other-name", label: tag.pretty_name, value: tag.name, category: tag.category, post_count: tag.post_count, antecedent: antecedent } end end