81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
/*jshint multistr:true */
|
|
|
|
this.recline = this.recline || {};
|
|
this.recline.View = this.recline.View || {};
|
|
|
|
(function($, my) {
|
|
|
|
my.FacetViewer = Backbone.View.extend({
|
|
className: 'recline-facet-viewer well',
|
|
template: ' \
|
|
<a class="close js-hide" href="#">×</a> \
|
|
<div class="facets row"> \
|
|
<div class="span1"> \
|
|
<h3>Facets</h3> \
|
|
</div> \
|
|
{{#facets}} \
|
|
<div class="facet-summary span2 dropdown" data-facet="{{id}}"> \
|
|
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><i class="icon-chevron-down"></i> {{id}} {{label}}</a> \
|
|
<ul class="facet-items dropdown-menu"> \
|
|
{{#terms}} \
|
|
<li><a class="facet-choice js-facet-filter" data-value="{{term}}">{{term}} ({{count}})</a></li> \
|
|
{{/terms}} \
|
|
{{#entries}} \
|
|
<li><a class="facet-choice js-facet-filter" data-value="{{time}}">{{term}} ({{count}})</a></li> \
|
|
{{/entries}} \
|
|
</ul> \
|
|
</div> \
|
|
{{/facets}} \
|
|
</div> \
|
|
',
|
|
|
|
events: {
|
|
'click .js-hide': 'onHide',
|
|
'click .js-facet-filter': 'onFacetFilter'
|
|
},
|
|
initialize: function(model) {
|
|
_.bindAll(this, 'render');
|
|
this.el = $(this.el);
|
|
this.model.facets.bind('all', this.render);
|
|
this.model.fields.bind('all', this.render);
|
|
this.render();
|
|
},
|
|
render: function() {
|
|
var tmplData = {
|
|
facets: this.model.facets.toJSON(),
|
|
fields: this.model.fields.toJSON()
|
|
};
|
|
tmplData.facets = _.map(tmplData.facets, function(facet) {
|
|
if (facet._type === 'date_histogram') {
|
|
facet.entries = _.map(facet.entries, function(entry) {
|
|
entry.term = new Date(entry.time).toDateString();
|
|
return entry;
|
|
});
|
|
}
|
|
return facet;
|
|
});
|
|
var templated = Mustache.render(this.template, tmplData);
|
|
this.el.html(templated);
|
|
// are there actually any facets to show?
|
|
if (this.model.facets.length > 0) {
|
|
this.el.show();
|
|
} else {
|
|
this.el.hide();
|
|
}
|
|
},
|
|
onHide: function(e) {
|
|
e.preventDefault();
|
|
this.el.hide();
|
|
},
|
|
onFacetFilter: function(e) {
|
|
var $target= $(e.target);
|
|
var fieldId = $target.closest('.facet-summary').attr('data-facet');
|
|
var value = $target.attr('data-value');
|
|
this.model.queryState.addTermFilter(fieldId, value);
|
|
}
|
|
});
|
|
|
|
|
|
})(jQuery, recline.View);
|
|
|