<commit_before>/*
 * Copyright 2015-2017 Hewlett Packard Enterprise Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

define([
    'underscore',
    'jquery',
    'backbone',
    'find/app/util/topic-map-view',
    'find/app/model/entity-collection',
    'i18n!find/nls/bundle',
    'find/app/configuration',
    'find/app/util/generate-error-support-message',
    'text!find/templates/app/page/search/results/entity-topic-map-view.html',
    'text!find/templates/app/page/loading-spinner.html',
    'iCheck'
], function(_, $, Backbone, TopicMapView, EntityCollection, i18n, configuration, generateErrorHtml,
            template, loadingTemplate) {
    'use strict';

    const loadingHtml = _.template(loadingTemplate)({i18n: i18n, large: true});

    /**
     * @readonly
     * @enum {String}
     */
    const ViewState = {
        LOADING: 'LOADING',
        ERROR: 'ERROR',
        EMPTY: 'EMPTY',
        MAP: 'MAP'
    };

    const Type = {
        QUERY: 'QUERY',
        COMPARISON: 'COMPARISON'
    };

    const CLUSTER_MODE = 'docsWithPhrase';

    function sum(a, b) {
        return a + b;
    }

    return Backbone.View.extend({
        template: _.template(template),

        events: {
            'change .speed-slider': function(e) {
                const $target = $(e.target);
                const value = $target.val();
                $target.attr('data-original-title', value);
                $target.tooltip('show');
                $target.blur();
                this.model.set('maxResults', value);
            },
            'input .speed-slider': function(e) {
                const $target = $(e.target);
                const value = $target.val();
                this.$('.tooltip-inner').text(value);
            }
        },

        initialize: function(options) {
            this.queryState = options.queryState;

            this.entityCollection = new EntityCollection([], {
                getSelectedRelatedConcepts: function() {
                    // Comparison topic view does not have queryState
                    return this.queryState
                        ? _.flatten(this.queryState.conceptGroups.pluck('concepts'))
                        : [];
                }.bind(this)
            });

            this.throttledFetchRelatedConcepts = _.debounce(this.fetchRelatedConcepts.bind(this), 500);

            this.queryModel = options.queryModel;
            this.type = options.type;
            this.showSlider = _.isUndefined(options.showSlider) || options.showSlider;

            this.topicMap = new TopicMapView({
                clickHandler: options.clickHandler
            });

            this.errorTemplate = generateErrorHtml({messageToUser: i18n['search.topicMap.error']});

            this.viewModel = new Backbone.Model({
                state: ViewState.EMPTY
            });

            this.model = new Backbone.Model({
                maxCount: 10,
                maxResults: options.maxResults || 300
            });

            this.listenTo(this.model, 'change:maxResults', this.throttledFetchRelatedConcepts);
            this.listenTo(this.queryModel, 'change', this.fetchRelatedConcepts);

            this.listenTo(this.entityCollection, 'sync', function() {
                this.viewModel.set('state', this.entityCollection.isEmpty()
                    ? ViewState.EMPTY
                    : ViewState.MAP);
                this.updateTopicMapData();
                this.update();
            });

            this.listenTo(this.entityCollection, 'request', function() {
                this.viewModel.set('state', ViewState.LOADING);
            });

            this.listenTo(this.entityCollection, 'error', function(collection, xhr) {
                this.generateErrorMessage(xhr);
                // Status of zero means the request has been aborted
                this.viewModel.set('state', xhr.status === 0
                    ? ViewState.LOADING
                    : ViewState.ERROR);
            });

            this.listenTo(this.viewModel, 'change', this.updateViewState);

            this.fetchRelatedConcepts();
        },

        render: function() {
            if(this.showSlider) {
                this.$('.speed-slider').tooltip('destroy');
            }

            this.$el.html(this.template({
                cid: this.cid,
                errorTemplate: this.errorTemplate,
                i18n: i18n,
                loadingHtml: loadingHtml,
                showSlider: this.showSlider,
                min: 50,
                max: configuration().topicMapMaxResults,
                step: 1
            }));

            this.$error = this.$('.entity-topic-map-error');
            this.$empty = this.$('.entity-topic-map-empty');
            this.$loading = this.$('.entity-topic-map-loading');

            if(this.showSlider) {
                this.$('.speed-slider')
                    .attr('value', this.model.get('maxResults'))
                    .tooltip({
                        title: this.model.get('maxResults'),
                        placement: 'top'
                    });
            }<commit_msg>[FIND-1084] placing the tooltip on the right so it didn't look out of place on the top.<commit_after>/*
 * Copyright 2015-2017 Hewlett Packard Enterprise Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

define([
    'underscore',
    'jquery',
    'backbone',
    'find/app/util/topic-map-view',
    'find/app/model/entity-collection',
    'i18n!find/nls/bundle',
    'find/app/configuration',
    'find/app/util/generate-error-support-message',
    'text!find/templates/app/page/search/results/entity-topic-map-view.html',
    'text!find/templates/app/page/loading-spinner.html',
    'iCheck'
], function(_, $, Backbone, TopicMapView, EntityCollection, i18n, configuration, generateErrorHtml,
            template, loadingTemplate) {
    'use strict';

    const loadingHtml = _.template(loadingTemplate)({i18n: i18n, large: true});

    /**
     * @readonly
     * @enum {String}
     */
    const ViewState = {
        LOADING: 'LOADING',
        ERROR: 'ERROR',
        EMPTY: 'EMPTY',
        MAP: 'MAP'
    };

    const Type = {
        QUERY: 'QUERY',
        COMPARISON: 'COMPARISON'
    };

    const CLUSTER_MODE = 'docsWithPhrase';

    function sum(a, b) {
        return a + b;
    }

    return Backbone.View.extend({
        template: _.template(template),

        events: {
            'change .speed-slider': function(e) {
                const $target = $(e.target);
                const value = $target.val();
                $target.attr('data-original-title', value);
                $target.tooltip('show');
                $target.blur();
                this.model.set('maxResults', value);
            },
            'input .speed-slider': function(e) {
                const $target = $(e.target);
                const value = $target.val();
                this.$('.tooltip-inner').text(value);
            }
        },

        initialize: function(options) {
            this.queryState = options.queryState;

            this.entityCollection = new EntityCollection([], {
                getSelectedRelatedConcepts: function() {
                    // Comparison topic view does not have queryState
                    return this.queryState
                        ? _.flatten(this.queryState.conceptGroups.pluck('concepts'))
                        : [];
                }.bind(this)
            });

            this.throttledFetchRelatedConcepts = _.debounce(this.fetchRelatedConcepts.bind(this), 500);

            this.queryModel = options.queryModel;
            this.type = options.type;
            this.showSlider = _.isUndefined(options.showSlider) || options.showSlider;

            this.topicMap = new TopicMapView({
                clickHandler: options.clickHandler
            });

            this.errorTemplate = generateErrorHtml({messageToUser: i18n['search.topicMap.error']});

            this.viewModel = new Backbone.Model({
                state: ViewState.EMPTY
            });

            this.model = new Backbone.Model({
                maxCount: 10,
                maxResults: options.maxResults || 300
            });

            this.listenTo(this.model, 'change:maxResults', this.throttledFetchRelatedConcepts);
            this.listenTo(this.queryModel, 'change', this.fetchRelatedConcepts);

            this.listenTo(this.entityCollection, 'sync', function() {
                this.viewModel.set('state', this.entityCollection.isEmpty()
                    ? ViewState.EMPTY
                    : ViewState.MAP);
                this.updateTopicMapData();
                this.update();
            });

            this.listenTo(this.entityCollection, 'request', function() {
                this.viewModel.set('state', ViewState.LOADING);
            });

            this.listenTo(this.entityCollection, 'error', function(collection, xhr) {
                this.generateErrorMessage(xhr);
                // Status of zero means the request has been aborted
                this.viewModel.set('state', xhr.status === 0
                    ? ViewState.LOADING
                    : ViewState.ERROR);
            });

            this.listenTo(this.viewModel, 'change', this.updateViewState);

            this.fetchRelatedConcepts();
        },

        render: function() {
            if(this.showSlider) {
                this.$('.speed-slider').tooltip('destroy');
            }

            this.$el.html(this.template({
                cid: this.cid,
                errorTemplate: this.errorTemplate,
                i18n: i18n,
                loadingHtml: loadingHtml,
                showSlider: this.showSlider,
                min: 50,
                max: configuration().topicMapMaxResults,
                step: 1
            }));

            this.$error = this.$('.entity-topic-map-error');
            this.$empty = this.$('.entity-topic-map-empty');
            this.$loading = this.$('.entity-topic-map-loading');

            if(this.showSlider) {
                this.$('.speed-slider')
                    .attr('value', this.model.get('maxResults'))
                    .tooltip({
                        title: this.model.get('maxResults'),
                        placement: 'right'
                    });
            }
{'fraction_non_alphanumeric': 0.12618326391518364, 'fraction_numerical': 0.006815600151457781, 'mean_word_length': 1.6605389070763032, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4945068', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2724, 'n_words': 506}