<commit_before>

define([
    'flight/lib/component',
    '../withDropdown',
    'detail/properties',
    'tpl!./termForm',
    'tpl!./concept-options',
    'tpl!./entity',
    'service/vertex',
    'service/ontology',
    'util/jquery.removePrefixedClasses'
], function(defineComponent, withDropdown, Properties, dropdownTemplate, conceptsTemplate, entityTemplate, VertexService, OntologyService) {
    'use strict';

    return defineComponent(TermForm, withDropdown);


    function TermForm() {
        this.vertexService = new VertexService();
        this.ontologyService = new OntologyService();

        this.defaultAttrs({
            entityConceptMenuSelector: '.underneath .dropdown-menu a',
            resolveButtonSelector: '.create-term',
            buttonDivSelector: '.buttons',
            objectSignSelector: '.object-sign',
            graphVertexSelector: '.graphVertexId',
            conceptSelector: 'select',
            helpSelector: '.help',
            addNewPropertiesSelector: '.none'
        });

        this.after('teardown', function() {
            if (this.promoted && this.promoted.length) {
                this.demoteSpanToTextVertex(this.promoted);
            }

            var info = $(this.attr.mentionNode).removeClass('focused').data('info');

            if (info) {
                this.updateConceptLabel(info._conceptType);
            }

            // Remove extra textNodes
            if (this.node.parentNode) {
                this.node.parentNode.normalize();
            }
        });

        this.after('initialize', function() {
            this.deferredConcepts = $.Deferred();
            this.setupContent();
            this.registerEvents();
        });

        this.showTypeahead = function() {
            this.select('objectSignSelector').typeahead('lookup');
        };

        this.onKeyPress = function(event) {
            if (!this.lastQuery || this.lastQuery === this.select('objectSignSelector').val()) {
                if (!this.select('resolveButtonSelector').is(":disabled")) {
                    switch (event.which) {
                        case $.ui.keyCode.ENTER:
                            this.onResolveClicked(event);
                    }
                }
                return;
            }

            if (!this.debouncedLookup) {
                this.debouncedLookup = _.debounce(function() {
                    this.select('objectSignSelector').typeahead('lookup');
                }.bind(this), 100);
            }

            this.debouncedLookup();
        };

        this.reset = function() {
            this.currentGraphVertexId = null;
            this.select('helpSelector').show();
            this.select('conceptSelector').attr('disabled', true).hide();
            this.select('resolveButtonSelector').hide();
            this.updateResolveImageIcon();
        };

        this.graphVertexChanged = function(newGraphVertexId, item, initial) {
            var self = this;

            this.currentGraphVertexId = newGraphVertexId;
            if (!initial || newGraphVertexId) {
                this.select('graphVertexSelector').val(newGraphVertexId);
                var info = _.isObject(item) ? item.properties || item : $(this.attr.mentionNode).data('info');

                if (newGraphVertexId) {
                    this.select('conceptSelector').attr('disabled', true);
                } else {
                    this.select('conceptSelector').attr('disabled', false);
                }
                this.updateConceptSelect(info && info._conceptType || '').show();
                if (newGraphVertexId && initial && !this.attr.coords) {
                    this.select('resolveButtonSelector')
                        .hide();
                } else {
                    this.select('resolveButtonSelector')
                        .text(newGraphVertexId ? 'Resolve to Existing' : 'Resolve as New')
                        .show();
                }
                this.select('helpSelector').hide();

                require(['configuration/plugins/visibility/visibilityEditor'], function(Visibility) {
                    Visibility.attachTo(self.$node.find('.visibility'), {
                        value: ''
                    });
                });
            }

            if (newGraphVertexId) {
                this.vertexService.getVertexProperties(newGraphVertexId)
                    .done(this.updateResolveImageIcon.bind(this));
            } else this.updateResolveImageIcon();

        };<commit_msg>Fix resolving to existing entity<commit_after>

define([
    'flight/lib/component',
    '../withDropdown',
    'detail/properties',
    'tpl!./termForm',
    'tpl!./concept-options',
    'tpl!./entity',
    'service/vertex',
    'service/ontology',
    'util/jquery.removePrefixedClasses'
], function(defineComponent, withDropdown, Properties, dropdownTemplate, conceptsTemplate, entityTemplate, VertexService, OntologyService) {
    'use strict';

    return defineComponent(TermForm, withDropdown);


    function TermForm() {
        this.vertexService = new VertexService();
        this.ontologyService = new OntologyService();

        this.defaultAttrs({
            entityConceptMenuSelector: '.underneath .dropdown-menu a',
            resolveButtonSelector: '.create-term',
            buttonDivSelector: '.buttons',
            objectSignSelector: '.object-sign',
            graphVertexSelector: '.graphVertexId',
            conceptSelector: 'select',
            helpSelector: '.help',
            addNewPropertiesSelector: '.none'
        });

        this.after('teardown', function() {
            if (this.promoted && this.promoted.length) {
                this.demoteSpanToTextVertex(this.promoted);
            }

            var info = $(this.attr.mentionNode).removeClass('focused').data('info');

            if (info) {
                this.updateConceptLabel(info._conceptType || '');
            }

            // Remove extra textNodes
            if (this.node.parentNode) {
                this.node.parentNode.normalize();
            }
        });

        this.after('initialize', function() {
            this.deferredConcepts = $.Deferred();
            this.setupContent();
            this.registerEvents();
        });

        this.showTypeahead = function() {
            this.select('objectSignSelector').typeahead('lookup');
        };

        this.onKeyPress = function(event) {
            if (!this.lastQuery || this.lastQuery === this.select('objectSignSelector').val()) {
                if (!this.select('resolveButtonSelector').is(":disabled")) {
                    switch (event.which) {
                        case $.ui.keyCode.ENTER:
                            this.onResolveClicked(event);
                    }
                }
                return;
            }

            if (!this.debouncedLookup) {
                this.debouncedLookup = _.debounce(function() {
                    this.select('objectSignSelector').typeahead('lookup');
                }.bind(this), 100);
            }

            this.debouncedLookup();
        };

        this.reset = function() {
            this.currentGraphVertexId = null;
            this.select('helpSelector').show();
            this.select('conceptSelector').attr('disabled', true).hide();
            this.select('resolveButtonSelector').hide();
            this.updateResolveImageIcon();
        };

        this.graphVertexChanged = function(newGraphVertexId, item, initial) {
            var self = this;

            this.currentGraphVertexId = newGraphVertexId;
            if (!initial || newGraphVertexId) {
                this.select('graphVertexSelector').val(newGraphVertexId);
                var info = _.isObject(item) ? item.properties || item : $(this.attr.mentionNode).data('info');

                if (newGraphVertexId) {
                    this.select('conceptSelector').attr('disabled', true);
                } else {
                    this.select('conceptSelector').attr('disabled', false);
                }
                var conceptType = (info && ((info._conceptType && info._conceptType.value) || info._conceptType)) || '';
                this.updateConceptSelect(conceptType).show();
                if (newGraphVertexId && initial && !this.attr.coords) {
                    this.select('resolveButtonSelector')
                        .hide();
                } else {
                    this.select('resolveButtonSelector')
                        .text(newGraphVertexId ? 'Resolve to Existing' : 'Resolve as New')
                        .show();
                }
                this.select('helpSelector').hide();

                require(['configuration/plugins/visibility/visibilityEditor'], function(Visibility) {
                    Visibility.attachTo(self.$node.find('.visibility'), {
                        value: ''
                    });
                });
            }

            if (newGraphVertexId) {
                this.vertexService.getVertexProperties(newGraphVertexId)
                    .done(this.updateResolveImageIcon.bind(this));
            } else this.updateResolveImageIcon();

        };
{'fraction_non_alphanumeric': 0.12678648765699438, 'fraction_numerical': 0.00064963187527068, 'mean_word_length': 1.7263872491145218, 'pattern_counts': {'":': 2, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836640', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2190, 'n_words': 338}