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

Add suggested lowercase query #1979

Merged
merged 1 commit into from
Dec 11, 2019
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
170 changes: 108 additions & 62 deletions src/js/widgets/list_of_things/paginated_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ define([
'./item_view',
'analytics',
'mathjax',
'hbs!js/wraps/widget/loading/template'
],

function (
'hbs!js/wraps/widget/loading/template',
], function(
_,
Marionette,
Backbone,
Expand All @@ -43,12 +41,12 @@ function (
loadingTemplate
) {
/**
* A simple model that holds attributes of the
* paginated view. Changes in this model are
* propagated to the view
*/
* A simple model that holds attributes of the
* paginated view. Changes in this model are
* propagated to the view
*/
var MainViewModel = Backbone.Model.extend({
defaults: function () {
defaults: function() {
return {
mainResults: false,
title: undefined,
Expand All @@ -59,52 +57,71 @@ function (
showSidebars: true,
pagination: true,
start: 0,
highlightsLoaded: false
highlightsLoaded: false,
};
}
},
});

const getSuggestedQuery = (_query) => {
const query = _query.clone();
const q = query.get('q');

// if the whole query is uppercase, lower it
if (q[0] === q[0].toUpperCase()) {
query.set('q', q[0].toLowerCase());
query.unset('fl');
return {
url: '#search/' + query.url(),
query: query.get('q'),
};
}
};

var EmptyView = Marionette.ItemView.extend({
template: function (data) {
template: function(data) {
if (data.query) {
return EmptyViewTemplate(data);
} if (data.error) {
}
if (data.error) {
return ErrorViewTemplate(data);
}
return loadingTemplate(_.extend(data, {
widgetLoadingSize: 'big',
hideCloseButton: true
}));
}
return loadingTemplate(
_.extend(data, {
widgetLoadingSize: 'big',
hideCloseButton: true,
})
);
},
});

/**
* This is the main view of the list of things. A composite
* view that holds collection of items.
*/
/**
* This is the main view of the list of things. A composite
* view that holds collection of items.
*/
var ListOfThingsView = Marionette.CompositeView.extend({

childView: ItemView,
emptyView: EmptyView,

initialize: function (options) {
initialize: function(options) {
this.model = new MainViewModel();
},

serializeData: function () {
serializeData: function() {
var data = this.model.toJSON();
// if it's an abstract page list with an 'export to results page'
// option, provide the properly escaped url
if (data.queryOperator) {
data.queryURL = '#search/q=' + data.queryOperator + '(';
data.queryURL += encodeURIComponent('bibcode:' + data.bibcode) + ')';
if (data.removeSelf) data.queryURL += encodeURIComponent(' -bibcode:' + data.bibcode);
if (data.sortOrder) data.queryURL += '&sort=' + encodeURIComponent(data.sortOrder);
if (data.removeSelf)
data.queryURL += encodeURIComponent(' -bibcode:' + data.bibcode);
if (data.sortOrder)
data.queryURL += '&sort=' + encodeURIComponent(data.sortOrder);
}
return data;
},

onRender: function () {
onRender: function() {
if (MathJax) {
MathJax.Hub.Queue(['Typeset', MathJax.Hub, this.el]);
}
Expand All @@ -114,7 +131,7 @@ function (

alreadyRendered: false,

emptyViewOptions: function (model) {
emptyViewOptions: function(model) {
var query = this.model.get('query');
var isTugboat = this.model.get('isTugboat');
var error = this.model.get('error');
Expand All @@ -123,19 +140,21 @@ function (
if (_.isArray(query)) {
model.set({
query: query[0],
showTugboatMessage: isTugboat
showTugboatMessage: isTugboat,
suggestedQuery: getSuggestedQuery(this.model.get('currentQuery')),
});
} else if (_.has('query', query)) {
model.set({
query: query.query[0],
showTugboatMessage: isTugboat
showTugboatMessage: isTugboat,
suggestedQuery: getSuggestedQuery(this.model.get('currentQuery')),
});
} else if (error) {
model.set('error', error);
}

return {
model: model
model: model,
};
},

Expand All @@ -149,13 +168,17 @@ function (
'click #backToTopBtn': 'goToTop',
'click a.page-control': 'changePageWithButton',
'keyup input.page-control': 'tabOrEnterChangePageWithInput',
'change #per-page-select': 'changePerPage'
'change #per-page-select': 'changePerPage',
},

toggleHighlights: function (e) {
toggleHighlights: function(e) {
var state = this.model.get('showHighlights');
state = _.isBoolean(state) && state
? 'closed' : (state === 'open') ? 'closed' : 'open';
state =
_.isBoolean(state) && state
? 'closed'
: state === 'open'
? 'closed'
: 'open';

this.model.set('showHighlights', state);
if (!this.model.get('highlightsLoaded')) {
Expand All @@ -164,7 +187,7 @@ function (
}
},

toggleAbstract: function () {
toggleAbstract: function() {
if (this.model.get('showAbstract') == 'open') {
this.model.set('showAbstract', 'closed');
} else if (this.model.get('showAbstract') == 'closed') {
Expand All @@ -173,80 +196,103 @@ function (
}
},

toggleShowSidebars: function () {
toggleShowSidebars: function() {
var val = !this.model.get('showSidebars');
this.model.set('showSidebars', val);
analytics('send', 'event', 'interaction', 'sidebars-toggled-' + val ? 'on' : 'off');
analytics(
'send',
'event',
'interaction',
'sidebars-toggled-' + val ? 'on' : 'off'
);
},

goToBottom: function () {
$('#app-container')
.animate({ scrollTop: this.$el.outerHeight() }, 'fast');
goToBottom: function() {
$('#app-container').animate(
{ scrollTop: this.$el.outerHeight() },
'fast'
);
},

goToTop: function () {
$('#app-container')
.animate({ scrollTop: 0 }, 'fast');
goToTop: function() {
$('#app-container').animate({ scrollTop: 0 }, 'fast');
},

modelEvents: {
'change': 'render',
change: 'render',
'change:showHighlights': 'toggleChildrenHighlights',
'change:showAbstract': 'toggleChildrenAbstracts'
'change:showAbstract': 'toggleChildrenAbstracts',
},

collectionEvents: {
'reset': 'onResetCollection'
reset: 'onResetCollection',
},

template: ResultsContainerTemplate,

onResetCollection: function () {
onResetCollection: function() {
this.model.set('highlightsLoaded', false);
},

/**
* Displays the are inside of every item-view
* with details (this place is normally hidden
* by default)
*/
toggleChildrenHighlights: function () {
* Displays the are inside of every item-view
* with details (this place is normally hidden
* by default)
*/
toggleChildrenHighlights: function() {
var show = this.model.get('showHighlights');
this.collection.invoke('set', 'showHighlights', show === 'open');
},

toggleChildrenAbstracts: function () {
toggleChildrenAbstracts: function() {
var show = this.model.get('showAbstract');
this.collection.invoke('set', 'showAbstract', show === 'open');
},

changePageWithButton: function (e) {
changePageWithButton: function(e) {
var $target = $(e.currentTarget);
if ($target.parent().hasClass('disabled')) return;
var transform = $target.hasClass('next-page') ? 1 : -1;
var pageVal = this.model.get('page') + transform;
this.trigger('pagination:select', pageVal);

if (this.resultsWidget) { analytics('send', 'event', 'interaction', 'results-list-pagination', pageVal); }
if (this.resultsWidget) {
analytics(
'send',
'event',
'interaction',
'results-list-pagination',
pageVal
);
}
return false;
},

tabOrEnterChangePageWithInput: function (e) {
tabOrEnterChangePageWithInput: function(e) {
// subtract one since pages are 0 indexed
var pageVal = parseInt($(e.target).val() - 1);
// enter or tab
if (e.keyCode == 13 || e.keyCode == 9) {
this.trigger('pagination:select', pageVal);
}

if (this.resultsWidget) { analytics('send', 'event', 'interaction', 'results-list-pagination', pageVal); }
if (this.resultsWidget) {
analytics(
'send',
'event',
'interaction',
'results-list-pagination',
pageVal
);
}
},

changePerPage: function (e) {
changePerPage: function(e) {
var val = parseInt(e.currentTarget ? e.currentTarget.value : 25);
val !== this.model.get('perPage') && this.trigger('pagination:changePerPage', val);
val !== this.model.get('perPage') &&
this.trigger('pagination:changePerPage', val);
return false;
}
},
});

return ListOfThingsView;
Expand Down
60 changes: 41 additions & 19 deletions src/js/widgets/list_of_things/templates/empty-view-template.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
<div class="s-empty-view-msg">
<div class="h4">
Sorry no results were found for <code>{{query}}</code>
</div>
<div class="h4">Sorry no results were found for <code>{{query}}</code></div>
{{#if showTugboatMessage}}
<div>
Using the ADS classic query translator? view the <a href="//adsabs.github.io/help/faq/#classic-search-translator" rel="noopener" target="_blank">docs</a>.
</div>
<div>
Using the ADS classic query translator? view the
<a
href="//adsabs.github.io/help/faq/#classic-search-translator"
rel="noopener"
target="_blank"
>docs</a
>.
</div>
{{else}}
<ul>
<li>Try broadening your search</li>
<li>Disable any filters that may be applied</li>
<li><a href="#">Check out some examples</a></li>
<li><a href="https://adsabs.github.io/help/search/search-syntax" target="_blank" rel="noopener">Read our help pages</a></li>
</ul>
<span>
Not seeing something that should be here? Let us know!
<button class="btn btn-primary-faded results-feedback-button" data-toggle="modal" data-target="#feedback-modal" data-feedback-view="general">
<i class="fa fa-comment"></i>
Leave Feedback
</button>
</span>
<ul>
{{#if suggestedQuery}}
<li>
Try an alternate query:
<a href="{{suggestedQuery.url}}">{{suggestedQuery.query}}</a>
</li>
{{/if}}
<li>Try broadening your search</li>
<li>Disable any filters that may be applied</li>
<li><a href="#">Check out some examples</a></li>
<li>
<a
href="https://adsabs.github.io/help/search/search-syntax"
target="_blank"
rel="noopener"
>Read our help pages</a
>
</li>
</ul>
<span>
Not seeing something that should be here? Let us know!
<button
class="btn btn-primary-faded results-feedback-button"
data-toggle="modal"
data-target="#feedback-modal"
data-feedback-view="general"
>
<i class="fa fa-comment"></i>
Leave Feedback
</button>
</span>
{{/if}}
</div>
7 changes: 5 additions & 2 deletions src/js/widgets/list_of_things/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,11 @@ define([
this.view.model.set('query', false);
} else {
var params = apiResponse.get('responseHeader.params');
this.view.model.set('isTugboat', !!params['__tb']);
this.view.model.set('query', this._getCurrentQueryString(apiResponse));
this.view.model.set({
isTugboat: !!params['__tb'],
query: this._getCurrentQueryString(apiResponse),
currentQuery: apiResponse.getApiQuery()
});
}

// XXX:rca - hack, to be solved later
Expand Down