Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make filter expressions translatable #783

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions pywb/static/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ function RenderCalendar(init) {
};
// regex for extracting the filter constraints and filter mods to human explanation
this.filterRE = /filter([^a-z]+)([a-z]+):(.+)/i;
this.filterMods = {
'=': 'Contains',
'==': 'Matches Exactly',
'=~': 'Matches Regex',
'=!': 'Does Not Contains',
'=!=': 'Is Not',
'=!~': 'Does Not Begins With'
};
this.text = init.text;
this.versionString = null;
}
Expand Down Expand Up @@ -433,7 +425,6 @@ RenderCalendar.prototype.createContainers = function() {
return;
}
// create the advanced results query info DOM structure
var forString = ' for ';
var forElems;

if (this.queryInfo.searchParams.matchType) {
Expand Down Expand Up @@ -503,7 +494,7 @@ RenderCalendar.prototype.createContainers = function() {
{
tag: 'p',
className: 'text-center mb-0 mt-1',
innerText: 'Filtering by'
innerText: filteringBy
},
{
tag: 'ul',
Expand Down Expand Up @@ -950,7 +941,7 @@ RenderCalendar.prototype.niceFilterDisplay = function() {
filterList.push({
tag: 'li',
className: 'list-group-item',
innerText: match[2] + ' ' + this.filterMods[match[1]] + ' ' + match[3]
innerText: match[2] + ' ' + filterMods[match[1]] + ' "' + match[3] + '"'
});
}
}
Expand Down
51 changes: 31 additions & 20 deletions pywb/static/search.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
var dtRE = /^\d{4,14}$/;
var didSetWasValidated = false;
var showBadDateTimeClass = 'show-optional-bad-input';
var filterMods = {
'=': 'Contains',
'==': 'Matches Exactly',
'=~': 'Matches Regex',
'=!': 'Does Not Contains',
'=!=': 'Is Not',
'=!~': 'Does Not Begins With'
};

var elemIds = {
filtering: {
Expand Down Expand Up @@ -65,7 +57,7 @@ function makeCheckDateRangeChecker(dtInputId, dtBadNotice) {

function createAndAddNoFilter(filterList) {
var nothing = document.createElement('li');
nothing.innerText = 'No Filter';
nothing.innerText = noFilter;
nothing.id = elemIds.filtering.nothing;
filterList.appendChild(nothing);
}
Expand All @@ -78,19 +70,24 @@ function addFilter(event) {
if (!expr) return;
var filterExpr = 'filter' + modifier + by + ':' + expr;
var filterList = document.getElementById(elemIds.filtering.list);
var previousFilters = filterList.children;
for (var i = 0; i < previousFilters.length; ++i) {
var filterData = previousFilters[i].dataset;
if (filterData && filterData.filter && filterData.filter == filterExpr) return;
}
var filterNothing = document.getElementById(elemIds.filtering.nothing);
if (filterNothing) {
filterList.removeChild(filterNothing);
}
var li = document.createElement('li');
li.innerText =
'By ' +
by[0].toUpperCase() +
by.substr(1) +
' ' +
filterMods[modifier] +
' ' +
expr;
' "' +
expr +
'"';
li.dataset.filter = filterExpr;
var nukeButton = document.createElement('button');
nukeButton.type = 'button';
Expand All @@ -110,6 +107,7 @@ function addFilter(event) {
};
li.appendChild(nukeButton);
filterList.appendChild(li);
return true;
}

function clearFilters(event) {
Expand Down Expand Up @@ -166,6 +164,17 @@ function validateFields(form) {
}
}

function submitForm(event, form, searchURLInput) {
event.preventDefault();
event.stopPropagation();
var url = searchURLInput.value;
if (!url) {
validateFields(form);
return;
}
performQuery(url);
}

$(document).ready(function() {
$('[data-toggle="tooltip"]').tooltip({
container: 'body',
Expand All @@ -184,16 +193,18 @@ $(document).ready(function() {
var searchURLInput = document.getElementById(elemIds.url);
var form = document.getElementById(elemIds.form);
form.addEventListener('submit', function(event) {
event.preventDefault();
event.stopPropagation();
var url = searchURLInput.value;
if (!url) {
validateFields(form);
return;
}
performQuery(url);
submitForm(event, form, searchURLInput);
});
document.getElementById(elemIds.advancedOptions).onclick = function() {
validateFields(form);
}
var filteringExpression = document.getElementById(elemIds.filtering.expression);
filteringExpression.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
if (! addFilter()) {
submitForm(event, form, searchURLInput);
}
}
});
});
10 changes: 10 additions & 0 deletions pywb/templates/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ <h4 class="display-4 text-center text-sm-left p-0">{{ _('Search Results') }}</h4
},
};

var filterMods = {
'=': "{{ _('Contains') }}",
'==': "{{ _('Matches Exactly') }}",
'=~': "{{ _('Matches Regex') }}",
'=!': "{{ _('Does Not Contain') }}",
'=!=': "{{ _('Is Not') }}",
'=!~': "{{ _('Does Not Begin With') }}"
};
var filteringBy = "{{ _('Filtering by') }}";
var forString = " {{ _('for') }} ";
var renderCal = new RenderCalendar({ prefix: "{{ prefix }}", staticPrefix: "{{ static_prefix }}", text: text });
renderCal.init();
</script>
Expand Down
20 changes: 15 additions & 5 deletions pywb/templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
{% block head %}
{{ super() }}
<script>
// TODO: cleanup
window.wb_prefix = "{{ wb_prefix }}";
var filterMods = {
'=': "{{ _('Contains') }}",
'==': "{{ _('Matches Exactly') }}",
'=~': "{{ _('Matches Regex') }}",
'=!': "{{ _('Does Not Contain') }}",
'=!=': "{{ _('Is Not') }}",
'=!~': "{{ _('Does Not Begin With') }}"
};
var noFilter = "{{ _('No Filter') }}";

// TODO: cleanup
window.wb_prefix = "{{ wb_prefix }}";
</script>
<script src="{{ static_prefix }}/search.js"></script>
{% endblock %}
Expand All @@ -24,14 +34,14 @@ <h4 class="display-4">
<label for="search-url" class="lead" aria-label="Search For Col">
{% set coll_title = metadata.title if metadata and metadata.title else coll %}
{% autoescape false %}
{% trans %}Search the {{ coll_title }} collection by url: {% endtrans %}
{% trans %}Search the {{ coll_title }} collection by url:{% endtrans %}
{% endautoescape %}
</label>
<input aria-label="url" aria-required="true" class="form-control form-control-lg" id="search-url"
name="search" placeholder="{{ _('Enter a URL to search for') }}"
title="{{ _('Enter a URL to search for') }}" type="search" required/>
<div class="invalid-feedback">
{% trans %}'Please enter a URL{% endtrans %}
{% trans %}Please enter a URL{% endtrans %}
</div>
</div>
</div>
Expand Down Expand Up @@ -131,7 +141,7 @@ <h4 class="display-4">
<option value="=~">{% trans %}Matches Regex{% endtrans %}</option>
<option value="=!">{% trans %}Does Not Contain{% endtrans %}</option>
<option value="=!=">{% trans %}Is Not{% endtrans %}</option>
<option value="=!~">{% trans %}Does Not Begins With{% endtrans %}</option>
<option value="=!~">{% trans %}Does Not Begin With{% endtrans %}</option>
</select>
</div>
<div class="row">
Expand Down