<commit_before>require([
    'configuration/admin/plugin',
    'hbs!io/lumify/web/devTools/templates/vertex-editor',
    'util/messages',
    'util/formatters',
    'd3',
    'service/vertex',
    'less!io/lumify/web/devTools/less/vertex-editor'
], function(
    defineLumifyAdminPlugin,
    template,
    i18n,
    F,
    d3,
    VertexService,
    less) {
    'use strict';

    var vertexService = new VertexService();

    defineLumifyAdminPlugin(VertexEditor, {
        less: less,
        section: i18n('admin.vertex.editor.section'),
        name: i18n('admin.vertex.editor.name'),
        subtitle: i18n('admin.vertex.editor.subtitle')
    });

    function VertexEditor() {
        this.defaultAttrs({
            loadSelector: '.refresh',
            deleteVertexSelector: '.delete-vertex',
            workspaceInputSelector: '.workspaceId',
            vertexInputSelector: '.vertexId',
            collapsibleSelector: '.collapsible > h1',
            editSelector: '.prop-edit',
            deleteSelector: '.prop-delete',
            saveSelector: '.prop-save',
            cancelSelector: '.prop-cancel'
        });

        this.after('initialize', function() {
            var self = this;

            this.on('click', {
                loadSelector: this.onLoad,
                editSelector: this.onEdit,
                deleteVertexSelector: this.onVertexDelete,
                deleteSelector: this.onDelete,
                saveSelector: this.onSave,
                cancelSelector: this.onCancel,
                collapsibleSelector: this.onToggleExpand
            });

            this.on(document, 'objectsSelected', this.onObjectsSelected);
            this.on(document, 'verticesUpdated', this.onVerticesUpdated);

            require(['data'], function(appData) {
                self.$node.html(template({
                    workspaceId: appData.workspaceId,
                    vertexId: appData.selectedVertexIds[0] || ''
                }));
            });
        });

        this.onEdit = function(event) {
            var button = $(event.target),
                li = button.closest('li').addClass('editing');
        };

        this.onDelete = function(event) {
            var self = this,
                button = $(event.target),
                li = button.closest('li').addClass('show-hover-items');

            this.handleSubmitButton(
                button,
                vertexService.deleteProperty(
                    this.$node.find('.vertexId').val(),
                    li.data('property'),
                    this.$node.find('.workspaceId').val()
                )
                    .fail(function() {
                        self.showError();
                    })
                    .done(function() {
                        li.removeClass('show-hover-items');
                    })
                    .done(this.onLoad.bind(this))
            );
        };

        this.onSave = function(event) {
            var self = this,
                button = $(event.target),
                li = button.closest('li'),
                property = li.data('property');

            this.handleSubmitButton(
                button,
                vertexService.setProperty(
                    this.$node.find('.vertexId').val(),
                    li.find('input[name=key]').val(),
                    property.name || li.find('input[name=name]').val(),
                    li.find('input[name=value]').val(),
                    li.find('input[name="http://lumify.io#visibilityJson"]').val(),
                    'admin graph vertex editor',
                    null,
                    JSON.parse(li.find('textarea[name=metadata]').val()),
                    this.$node.find('.workspaceId').val()
                )
                    .fail(function() {
                        self.showError();
                    })
                    .done(function() {
                        if (li.closest('.collapsible').next('.collapsible').length) {
                            li.removeClass('editing');
                        }
                    })
                    .done(this.onLoad.bind(this))
            );
        };

        this.onCancel = function(event) {
            var li = $(event.target).closest('li'),
                section = li.closest('.collapsible');
            if (section.next('.collapsible').length === 0) {
                section.removeClass('expanded');
            } else {
                li.removeClass('editing');
            }
        };

        this.onToggleExpand = function(event) {
            var item = $(event.target).closest('.collapsible');

            if (event.altKey) {
                if (item.hasClass('expanded')) {
                    item.siblings('.collapsible').addBack().removeClass('expanded');
                } else {
                    item.siblings('.collapsible').addBack().addClass('expanded')
                        .last().find('.multivalue').addClass('editing');
                }
            } else {
                item.toggleClass('expanded');
                if (item.next('.collapsible').length === 0) {
                    item.find('.multivalue').addClass('editing');
                } else {
                    item.find('.editing').removeClass('editing');
                }
            }
        };

        this.onVerticesUpdated = function(event, data) {
            if (this.currentVertexId) {
                var vertex = _.findWhere(data && data.vertices, { id: this.currentVertexId })
                if (vertex) {
                    this.update(vertex);
                }
            }
        };

        this.onObjectsSelected = function(event, data) {
            var vertex = _.first(data && data.vertices);

            if (vertex) {
                this.select('vertexInputSelector').val(vertex.id);
                this.update(vertex);
            }
        };

        this.onLoad = function() {
            var self = this;

            vertexService.getVertexProperties(
                this.select('vertexInputSelector').val(),
                this.select('workspaceInputSelector').val()
            ).done(function(vertex) {
                self.update(vertex);
            });
        };

        this.onVertexDelete = function(event) {
            var self = this,
                button = $(event.target),
                graphVertexId = this.select('vertexInputSelector').val(),
                workspaceId = this.select('workspaceInputSelector').val();

            this.handleSubmitButton(
                button,
                vertexService._ajaxPost({
                    url: 'admin/deleteVertex',
                    data: {
                        graphVertexId: graphVertexId,
                        workspaceId: workspaceId
                    }
                }).fail(function() {
                    self.showError();
                }).done(function(vertex) {
                    self.update(vertex);
                })
            );
        };

        this.update = function(vertex) {
            var newVertex = vertex.id !== this.currentVertexId;<commit_msg>Fix vertex delete<commit_after>require([
    'configuration/admin/plugin',
    'hbs!io/lumify/web/devTools/templates/vertex-editor',
    'util/messages',
    'util/formatters',
    'd3',
    'service/vertex',
    'less!io/lumify/web/devTools/less/vertex-editor'
], function(
    defineLumifyAdminPlugin,
    template,
    i18n,
    F,
    d3,
    VertexService,
    less) {
    'use strict';

    var vertexService = new VertexService();

    defineLumifyAdminPlugin(VertexEditor, {
        less: less,
        section: i18n('admin.vertex.editor.section'),
        name: i18n('admin.vertex.editor.name'),
        subtitle: i18n('admin.vertex.editor.subtitle')
    });

    function VertexEditor() {
        this.defaultAttrs({
            loadSelector: '.refresh',
            deleteVertexSelector: '.delete-vertex',
            workspaceInputSelector: '.workspaceId',
            vertexInputSelector: '.vertexId',
            collapsibleSelector: '.collapsible > h1',
            editSelector: '.prop-edit',
            deleteSelector: '.prop-delete',
            saveSelector: '.prop-save',
            cancelSelector: '.prop-cancel'
        });

        this.after('initialize', function() {
            var self = this;

            this.on('click', {
                loadSelector: this.onLoad,
                editSelector: this.onEdit,
                deleteVertexSelector: this.onVertexDelete,
                deleteSelector: this.onDelete,
                saveSelector: this.onSave,
                cancelSelector: this.onCancel,
                collapsibleSelector: this.onToggleExpand
            });

            this.on(document, 'objectsSelected', this.onObjectsSelected);
            this.on(document, 'verticesUpdated', this.onVerticesUpdated);

            require(['data'], function(appData) {
                self.$node.html(template({
                    workspaceId: appData.workspaceId,
                    vertexId: appData.selectedVertexIds[0] || ''
                }));
            });
        });

        this.onEdit = function(event) {
            var button = $(event.target),
                li = button.closest('li').addClass('editing');
        };

        this.onDelete = function(event) {
            var self = this,
                button = $(event.target),
                li = button.closest('li').addClass('show-hover-items');

            this.handleSubmitButton(
                button,
                vertexService.deleteProperty(
                    this.$node.find('.vertexId').val(),
                    li.data('property'),
                    this.$node.find('.workspaceId').val()
                )
                    .fail(function() {
                        self.showError();
                    })
                    .done(function() {
                        li.removeClass('show-hover-items');
                    })
                    .done(this.onLoad.bind(this))
            );
        };

        this.onSave = function(event) {
            var self = this,
                button = $(event.target),
                li = button.closest('li'),
                property = li.data('property');

            this.handleSubmitButton(
                button,
                vertexService.setProperty(
                    this.$node.find('.vertexId').val(),
                    li.find('input[name=key]').val(),
                    property.name || li.find('input[name=name]').val(),
                    li.find('input[name=value]').val(),
                    li.find('input[name="http://lumify.io#visibilityJson"]').val(),
                    'admin graph vertex editor',
                    null,
                    JSON.parse(li.find('textarea[name=metadata]').val()),
                    this.$node.find('.workspaceId').val()
                )
                    .fail(function() {
                        self.showError();
                    })
                    .done(function() {
                        if (li.closest('.collapsible').next('.collapsible').length) {
                            li.removeClass('editing');
                        }
                    })
                    .done(this.onLoad.bind(this))
            );
        };

        this.onCancel = function(event) {
            var li = $(event.target).closest('li'),
                section = li.closest('.collapsible');
            if (section.next('.collapsible').length === 0) {
                section.removeClass('expanded');
            } else {
                li.removeClass('editing');
            }
        };

        this.onToggleExpand = function(event) {
            var item = $(event.target).closest('.collapsible');

            if (event.altKey) {
                if (item.hasClass('expanded')) {
                    item.siblings('.collapsible').addBack().removeClass('expanded');
                } else {
                    item.siblings('.collapsible').addBack().addClass('expanded')
                        .last().find('.multivalue').addClass('editing');
                }
            } else {
                item.toggleClass('expanded');
                if (item.next('.collapsible').length === 0) {
                    item.find('.multivalue').addClass('editing');
                } else {
                    item.find('.editing').removeClass('editing');
                }
            }
        };

        this.onVerticesUpdated = function(event, data) {
            if (this.currentVertexId) {
                var vertex = _.findWhere(data && data.vertices, { id: this.currentVertexId })
                if (vertex) {
                    this.update(vertex);
                }
            }
        };

        this.onObjectsSelected = function(event, data) {
            var vertex = _.first(data && data.vertices);

            if (vertex) {
                this.select('vertexInputSelector').val(vertex.id);
                this.update(vertex);
            }
        };

        this.onLoad = function() {
            var self = this;

            vertexService.getVertexProperties(
                this.select('vertexInputSelector').val(),
                this.select('workspaceInputSelector').val()
            ).done(function(vertex) {
                self.update(vertex);
            });
        };

        this.onVertexDelete = function(event) {
            var self = this,
                button = $(event.target),
                graphVertexId = this.select('vertexInputSelector').val(),
                workspaceId = this.select('workspaceInputSelector').val();

            this.handleSubmitButton(
                button,
                vertexService._ajaxPost({
                    url: 'admin/deleteVertex',
                    dataType: 'html',
                    data: {
                        graphVertexId: graphVertexId,
                        workspaceId: workspaceId
                    }
                }).fail(function() {
                    self.showError();
                }).done(function() {
                    self.$node.find('section').remove();
                    self.$node.find('.vertexId').val('');
                })
            );
        };

        this.update = function(vertex) {
            var newVertex = vertex.id !== this.currentVertexId;
{'fraction_non_alphanumeric': 0.13157164655291997, 'fraction_numerical': 0.0019420169232903315, 'mean_word_length': 1.3331715210355988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4744677', 'n_tokens_mistral': 3701, 'n_tokens_neox': 3471, 'n_words': 498}