Facets.js 3.99 KB
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var Backbone = require('backbone');
    var NestedModels = require('backbone-nested-models');
    var Facet = require('./Facet');
    var Sort = require('./Sort');
    var Util = require('./Util');

    var Facets =  NestedModels.mixin(Backbone.Model.extend({
        initialize: function(data, options) {
            this.url = function() {
                var url = options.search.url;
                return _.isFunction(url) ? url.call() : url;
            };
            _.each(this.keys(), _.bind(function(facetName) {
                var facet = this.get(facetName);
                _.each({
                    'item-change': 'item-change',
                    'change:query': 'child-query',
                    'change:queryMin': 'item-change',
                    'change:queryMax': 'item-change',
                }, function(eventValue, eventKey) {
                    facet.on(eventKey, function(model, value, options) {
                        console.log('trigger', facet.get('formName'), eventValue);
                        this.trigger(eventValue, facet, null, options);
                    }, this);
                }, this);
            }, this));

            this._doSuggestions = _.debounce(_.bind(function(childFacet) {
                if (childFacet.get('query')) {
                    this.fetch({
                        data: this.toJSON({childFacet: childFacet, facetSuggestions: true}),
                        childFacet: childFacet,
                        facetSuggestions: true,
                        merge: true,
                        traditional: true,
                    });
                } else {
                    childFacet.set('suggestions', []);
                }
            }, this), 250);
            this.on('child-query', this._doSuggestions, this);

            return Facets.__super__.initialize.apply(this, arguments);
        },
        resetSearch: function(options) {
            _.each(this.values(), function(facet) {
                facet.get('items').reset(null, options);
            });
        },
        applyFacetResults: function(data, options) {
            options = options || {};
            _.each(this.keys(), _.bind(function(facetName) {
                var facet = this.get(facetName);
                facet.applyFacetResults(facetName, data, options);
            }, this));
        },
        getFacetFormData: function(options) {
            options = options || {};
            var result = {
                facet: true,
                stats: true,
                'facet.missing': true,
                'facet.field': [],
                'facet.range': [],
                'facet.interval': [],
                'facet.query': [],
                'stats.field': [],
                fq: [],
            };
            _.each(this.keys(), _.bind(function(facetName) {
                var facet = this.get(facetName);
                var facetResult = facet.getFacetFormData(facetName, options);
                _.each([
                    'facet.field',
                    'facet.range',
                    'facet.interval',
                    'facet.query',
                    'stats.field',
                    'fq',
                ], function(key) {
                    result[key] = result[key].concat(facetResult[key]);
                });
            }, this));
            return result;
        },



        toJSON: function toJSON(options) {
            if (!!options.facetSuggestions) {
                return _.extend({
                    rows: 0,
                    facet: true,
                    wt: 'json',
                    q: '*:*',
                }, this.getFacetFormData(options));
            }
        },
        parse: function parse(data, options) {
            if (!!options.facetSuggestions) {
                this.applyFacetResults(data, options);
            }
            return null;
        },
    }));
    Facets.Sort = Sort;
    Facets.Facet = Facet;

    return Facets;
});