Skip to content

Commit

Permalink
Merge pull request #1054 from aholachek/persistent-bigquery
Browse files Browse the repository at this point in the history
added persistent library filter -- you can add a q
  • Loading branch information
aholachek authored Aug 8, 2016
2 parents c7d89ad + e45357f commit 4aa77fe
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 43 deletions.
22 changes: 18 additions & 4 deletions src/js/components/query_mediator.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ define(['underscore',
if ( bigquerySource ){
newQuery.set('__bigquerySource', bigquerySource[0])
}
if ( apiQuery.get('sort') ){
newQuery.set('sort', apiQuery.get('sort') )
}

that.startSearchCycle( newQuery, senderKey )
},
Expand All @@ -173,10 +176,7 @@ define(['underscore',
this.getBeeHive().getService("Api").request(request);

}
// pre-existing big query--maybe from faceting or url load
else if (apiQuery.get("__qid")){
this.startSearchCycle.apply(this, arguments);
}

// check if this is an "object:" query
else if (apiQuery.get("q")[0].indexOf("object:") > -1) {
// we have an "object:" query as part of the query
Expand Down Expand Up @@ -205,6 +205,20 @@ define(['underscore',
this.getBeeHive().getObject("AppStorage").clearSelectedPapers();
}

/*
if it's a pre-existing bigquery NOT supplanted by another bigquery,
and not forcibly clearing the current bigquery, then
keep it and just augment it rather than losing the bigquery
this is so that users can add q parameters to bigqueries, e.g. library exports
*/
if (this.mostRecentQuery.get('__qid') &&
!apiQuery.get('__qid') &&
!apiQuery.get('__clearBiqQuery')
){
this.mostRecentQuery.set('q', apiQuery.get('q'));
apiQuery = this.mostRecentQuery;
}

this.mostRecentQuery = apiQuery;

if (this.debug) {
Expand Down
2 changes: 0 additions & 2 deletions src/js/widgets/filter_visualizer/templates/item-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ <h5>{{this.display}}</h5>
{{else}}

<span class="filter-{{this.type}}">{{this.display}}</span>
{{#unless this.cantDelete}}
<button class="btn btn-xs filter-{{this.type}}-remove" value="{{this.value}}" title="remove this filter">
<i class="fa fa-remove"></i></button>
{{/unless}}

{{/compare}}

Expand Down
13 changes: 11 additions & 2 deletions src/js/widgets/filter_visualizer/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ define([
type: 'operand',
display: 'custom filter',
value: filter.filter_name + '|control|x',
cantDelete : true
});
guiData.push({elements: oneFilter});
return;
Expand Down Expand Up @@ -523,10 +522,20 @@ define([
* that back views
*/
onFilterEvent: function(node, value) {
var newQuery = this.createModifiedQuery(value);
//remove a bigquery
if (value === 'bigquery|control|x' ){
var newQuery = new ApiQuery({
q : '*',
'__clearBiqQuery' : 'true'
});
} else {
var newQuery = this.createModifiedQuery(value);
}

var ps = this.getBeeHive().getService('PubSub');
if (ps)
ps.publish(ps.START_SEARCH, newQuery);

}

});
Expand Down
3 changes: 2 additions & 1 deletion src/js/widgets/library_individual/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ define([
libController.getLibraryBibcodes(this.model.get("id")).done(function (bibcodes) {
var query = new ApiQuery({
__bigquery : bibcodes,
__bigquerySource : 'Library: ' + that.headerModel.get("name")
__bigquerySource : 'Library: ' + that.headerModel.get("name"),
// sort : 'date desc'
});
pubsub.publish(pubsub.START_SEARCH, query);
});
Expand Down
12 changes: 8 additions & 4 deletions src/js/widgets/sort/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ define(['marionette',
},

handleFeedback: function (feedback) {
if (feedback.code === ApiFeedback.CODES.SEARCH_CYCLE_STARTED) {
this.setCurrentQuery(feedback.query);
this.model.set("sort", this.extractSort(feedback.query.get("sort")[0]))
if (feedback.code === ApiFeedback.CODES.SEARCH_CYCLE_STARTED ) {
try {
this.setCurrentQuery(feedback.query);
this.model.set("sort", this.extractSort(feedback.query.get("sort")[0]))
} catch (e) {
console.warn('sort widget could not detect any query sort')
}
}
}

});

return SortWidget
});
});
1 change: 1 addition & 0 deletions src/styles/sass/ads-sass/filter-visualizer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
h5 {
margin-top: 0 !important;
margin-bottom: 0 !important;
font-weight: bold !important;
}

}
Expand Down
54 changes: 29 additions & 25 deletions test/mocha/js/components/query_mediator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,39 @@ define([

});

it("should be able to take a qid from url-supplied apiQuery and start the search cycle", function(){
it("should simply add q parameters to a bigquery if the bigquery is not overridden", function(){

var qm = new QueryMediator();
qm.activate(beehive, {getPskOfPluginOrWidget: function() {}});
var qm = createTestQM().qm;

var q = new ApiQuery({
__qid : "fakeQID",
q : "foo",
fq : "boo"
});
var publishSpy = sinon.spy(qm.getPubSub(), "publish");

var startSearchCycle = sinon.stub(qm, "startSearchCycle");
qm.getQueryAndStartSearchCycle(q, "fakeKey");
//as if previous query was a bigquery
qm.mostRecentQuery= new ApiQuery({
"q": [
"*:*"
],
"__qid": [
"fakeQueryID"
]
});

expect(startSearchCycle.args[0][0].toJSON()).to.eql({
"__qid": [
"fakeQID"
],
"q": [
"foo"
],
"fq": [
"boo"
]
});
qm.startSearchCycle( new ApiQuery({
q : 'star'
}), {getId : function(){ return 1 }});

qm.startSearchCycle.restore();
expect(publishSpy.args[0][0]).to.eql("[PubSub]-Inviting-Request");

//it has just augmented the query with a new q parameter
expect(publishSpy.args[0][1].toJSON()).to.eql(
{
"q": [
"star"
],
"__qid": [
"fakeQueryID"
]
}
);

});

Expand Down Expand Up @@ -583,7 +589,7 @@ define([
var x = createTestQM();
var qm = x.qm, key1 = x.key1, key2 = x.key2, req1 = x.req1, req2 = x.req2;
qm.activateCache();

var rk = qm._getCacheKey(req1);

qm._executeRequest(req1, key1);
Expand Down Expand Up @@ -761,5 +767,3 @@ define([
"title":["<em>Star</em> Streams"]}}}';

});


63 changes: 58 additions & 5 deletions test/mocha/js/widgets/filter_visualizer_widget.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ define([
var minsub;

beforeEach(function (done) {
var ta = $('#test');
if (ta) {
ta.empty();
}
minsub = new (MinSub.extend({
request: function(apiRequest) {
console.log('called')
Expand All @@ -34,6 +30,10 @@ define([
done();
});

afterEach(function(){
$("#test").empty()
})

it("returns FilterVisualizer object", function () {
expect(new FilterVisualizerWidget()).to.be.instanceof(FilterVisualizerWidget);
});
Expand Down Expand Up @@ -181,7 +181,7 @@ define([
filters = widget.extractFilters(q);
expect(filters[0]).to.eql({
category: 'Author',
filter_name: 'fq_author',
filter_name: 'fq_author',
filter_query: "(author_facet_hier:\"0/Wang, J\")",
filter_key: '__author_facet_hier_fq_author',
filter_value: ["AND","author_facet_hier:\"0/Wang, J\""]
Expand Down Expand Up @@ -545,5 +545,58 @@ define([
expect(widget.beautifyOperand('fq_grant', 'grant:foo')).to.eql('foo');
});

it("handles bigquery as a filter special case", function(){

var widget = new FilterVisualizerWidget();
widget.activate(minsub.beehive.getHardenedInstance());
var $w = $(widget.render());

$('#test').append($w);

widget.processFeedback(minsub.createFeedback({
code: minsub.T.FEEDBACK.CODES.SEARCH_CYCLE_STARTED,
numFound: 100,
query: minsub.createQuery({
"q": [
"*:*"
],
"__qid": [
"bd07de928f0066253af7528336acaf4a"
],
"__bigquerySource": [
"Library: Papers by Alberto Accomazzi"
]
})
})
);

expect($("span.filter-topic-group").length).to.eql(1);
expect($("span.filter-topic-group h5").text()).to.eql('Library: Papers by Alberto Accomazzi');

var publishSpy = sinon.spy();

widget.getBeeHive = function(){
return {
getService : function(){
return {
publish : publishSpy
}
}
}
}

$("button.filter-operand-remove").click();

expect(publishSpy.args[0][1].toJSON()).to.eql({
"q": [
"*"
],
"__clearBiqQuery": [
"true"
]
});

});

});
});

0 comments on commit 4aa77fe

Please sign in to comment.