Split search filter columns into a component

(cherry picked from commit 84732f29a5)
This commit is contained in:
syeopite 2021-06-26 14:51:43 -07:00
parent 3e09dcaada
commit 9a18699746
No known key found for this signature in database
GPG key ID: 6FA616E5A5294A82
3 changed files with 113 additions and 129 deletions

View file

@ -0,0 +1,36 @@
<%
# Almost all of the filter columns are basically the same so we'll define it here as a component.
# Uses five values
# - filter_params: The URL params containing only the search filters.
# - base_url: The path to the /search endpoint of Invidious with the current search query passed inn
# - operators: containing all of the search filters currently applied
# - filters: the array of filters within the column we're creating
# - filter_name: name of the filter
# - selected_default: default selected filter
-%>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-<%= filter_name %>">
<b><%= translate(locale, filter_name) %></b>
<hr/>
<ul class="pure-menu-list">
<% filters.each do | filter | %>
<li class="pure-menu-item">
<% if operators.fetch(filter_name, selected_default) == filter %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&#{filter_name}=[a-z]+/, "")}"%>">
<b><%= translate(locale, filter) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<a href="<%= base_url + "#{filter_params.gsub(/&#{filter_name}=[a-z]+/, "")}&#{filter_name}=#{filter}"%>">
<%= translate(locale, filter) %>
</a>
<% end %>
</li>
<% end %>
</ul>
</div>

View file

@ -0,0 +1,76 @@
<% operators = operators.not_nil! %>
<details id="filters">
<summary class="simulated_a">
<h3 style="display:inline"> <%= translate(locale, "filter") %> </h3>
</summary>
<div id="filters" class="pure-g h-box">
<% # Grabs all search filters. This is to make sure we don't accidently overwrite something within the
# search query later on.
-%>
<% filter_params = env.request.query_params.to_s.gsub(/q=.+?(?=&|$)/, "") %>
<% base_url = "/search?q=#{URI.encode_www_form(query.not_nil!)}" -%>
<% filters = ["hour", "today", "week", "month", "year"] %>
<% filter_name = "date" %>
<% selected_default = "NotNeeded" %>
<%= rendered "components/search-filters/filter-column" -%>
<% filters = ["video", "channel", "playlist", "movie", "show"] %>
<% filter_name = "content_type" %>
<% selected_default = "NotNeeded" %>
<%= rendered "components/search-filters/filter-column" -%>
<% filters = ["short", "long"] %>
<% filter_name = "duration" %>
<%= rendered "components/search-filters/filter-column" -%>
<%
# Since the feature filter column is unqiue (multiple filters can be applied),
# we'll have to define it manually.
%>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-features">
<b><%= translate(locale, "features") %></b>
<hr/>
<ul class="pure-menu-list">
<% ["hd", "subtitles", "creative_commons", "3d", "live", "purchased", "4k", "360", "location", "hdr"].each do |feature| %>
<li class="pure-menu-item">
<% if operators.fetch("features", "all").includes?(feature) %>
<% if operators["features"].split(",").size == 1 %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&features=[a-z]+/, "")}"%>">
<b><%= translate(locale, feature) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<% data = filter_params.match(/.*features=.*(#{feature}(%2C|&|$)).*/).not_nil! %>
<% start = data.begin(1) %>
<% last = data.end(1) -%>
<a style="color: inherit;" href="<%= base_url + "#{filter_params[0...start] + filter_params[last..-1]}"%>">
<b><%= translate(locale, feature) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% end %>
<% elsif operators.has_key?("features") %>
<a href="<%= base_url + filter_params.gsub(/features=/, "features=#{feature},")%>">
<%= translate(locale, feature) %>
</a>
<% else %>
<a href="<%= "#{base_url}#{filter_params}&features=#{feature}"%>">
<%= translate(locale, feature) %>
</a>
<% end %>
</li>
<% end %>
</ul>
</div>
<% filters = ["relevance", "rating", "date", "views"] %>
<% filter_name = "sort" %>
<% selected_default = "relevance" %>
<%= rendered "components/search-filters/filter-column" -%>
</div>
</details>

View file

@ -11,135 +11,7 @@
<a href="/redirect?referer=<%= env.get?("current_page") %>"><%= translate(locale, "Broken? Try another Invidious Instance!") %></a>
</h3>
<% else %>
<details id="filters">
<summary>
<h3 style="display:inline"> <%= translate(locale, "filter") %> </h3>
</summary>
<div id="filters" class="pure-g h-box">
<!-- Grabs all search filters. This is to make sure we don't accidently overwrite something within the
search query later on-->
<% filter_params = env.request.query_params.to_s.gsub(/q=.+?(?=&)/, "") %>
<% base_url = "/search?q=#{URI.encode_www_form(query.not_nil!)}" %>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-date">
<b><%= translate(locale, "date") %></b>
<hr/>
<ul class="pure-menu-list">
<% ["hour", "today", "week", "month", "year"].each do |date| %>
<li class="pure-menu-item">
<% if operator_hash.fetch("date", "all") == date %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&date=[a-z]+/, "")}"%>">
<b><%= translate(locale, date) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<a href="<%= base_url + "#{filter_params.gsub(/&date=[a-z]+/, "")}&date=#{date}"%>">
<%= translate(locale, date) %>
</a>
<% end %>
</li>
<% end %>
</ul>
</div>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-content_type">
<b><%= translate(locale, "content_type") %></b>
<hr/>
<ul class="pure-menu-list">
<% ["video", "channel", "playlist", "movie", "show"].each do |content_type| %>
<li class="pure-menu-item">
<% if operator_hash.fetch("content_type", "all") == content_type %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&content_type=[a-z]+/, "")}"%>">
<b><%= translate(locale, content_type) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<a href="<%= base_url + "#{filter_params.gsub(/&content_type=[a-z]+/, "")}&content_type=#{content_type}"%>">
<%= translate(locale, content_type) %>
</a>
<% end %>
</li>
<% end %>
</ul>
</div>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-duration">
<b><%= translate(locale, "duration") %></b>
<hr/>
<ul class="pure-menu-list">
<% ["short", "long"].each do |duration| %>
<li class="pure-menu-item">
<% if operator_hash.fetch("duration", "all") == duration %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&duration=[a-z]+/, "")}"%>">
<b><%= translate(locale, duration) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<a href="<%= base_url + "#{filter_params.gsub(/&duration=[a-z]+/, "")}&duration=#{duration}"%>">
<%= translate(locale, duration) %>
</a>
<% end %>
</li>
<% end %>
</ul>
</div>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-features">
<b><%= translate(locale, "features") %></b>
<hr/>
<ul class="pure-menu-list">
<% ["hd", "subtitles", "creative_commons", "3d", "live", "purchased", "4k", "360", "location", "hdr"].each do |feature| %>
<li class="pure-menu-item">
<% if operator_hash.fetch("features", "all").includes?(feature) %>
<% if operator_hash["features"].split(",").size == 1 %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&features=[a-z]+/, "")}"%>">
<b><%= translate(locale, feature) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<% data = filter_params.match(/.*features=.*(#{feature}(%2C|&|$)).*/).not_nil! %>
<% start = data.begin(1) %>
<% last = data.end(1) %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params[0...start] + filter_params[last..-1]}"%>">
<b><%= translate(locale, feature) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% end %>
<% elsif operator_hash.has_key?("features") %>
<a href="<%= base_url + filter_params.gsub(/features=/, "features=#{feature},")%>">
<%= translate(locale, feature) %>
</a>
<% else %>
<a href="<%= "#{base_url}#{filter_params}&features=#{feature}"%>">
<%= translate(locale, feature) %>
</a>
<% end %>
</li>
<% end %>
</ul>
</div>
<div class="pure-u-1-3 pure-u-md-1-5 filter-catagory" id="filter-sort">
<b><%= translate(locale, "sort") %></b>
<hr/>
<% ["relevance", "rating", "date", "views"].each do |sort| %>
<div class="pure-u-1 pure-md-1-5">
<% if operator_hash.fetch("sort", "relevance") == sort %>
<a style="color: inherit;" href="<%= base_url + "#{filter_params.gsub(/&sort=[a-z]+/, "")}"%>">
<b><%= translate(locale, sort) %></b>
<i class="remove-filter icon ion-md-close"></i>
</a>
<% else %>
<a href="<%= base_url + "#{filter_params.gsub(/&sort=[a-z]+/, "")}&sort=#{sort}"%>">
<%= translate(locale, sort) %>
</a>
<% end %>
</div>
<% end %>
</div>
</details>
<%= rendered "components/search-filters/search-filters" %>
<% end %>
<% if count == 0 %>