<commit_before>'use strict';

const EDITOR_MODE_CREATE = 1;
const EDITOR_MODE_EDIT   = 2;

const ADD_NEW_FIELD_BUTTON_ROW = 'add_new_field_button';

function EntityEditor(entity_event_subscriber) {
    this.__init__(entity_event_subscriber);
}

EntityEditor.prototype = {

    __init__: function(entity_event_subscriber) {
        this.entity_event_subscriber = entity_event_subscriber;
        this.current_mode = null;
    },

    create_or_save_changes_button_pressed: function() {
        if (this.current_mode === EDITOR_MODE_CREATE) {
            this._entity_created();
        } else {
            this._modify_entity();
        }
    },

    cancel_or_delete_button_pressed: function() {
        if (this.current_mode === EDITOR_MODE_CREATE) {
            this.wall_entity_editor.force_hide_self_and_all_child_attachments_recursively();
        } else {
            MANAGER_ENTITY.delete_entity_by_id(this.entity_id_being_edited);
        }
    },

    delete_entity_field: function(field_name) {
        delete this.entity_field_rows[field_name];
        this.wall_entity_editor.delete_row_by_name(field_name);

        if (this.current_mode === EDITOR_MODE_EDIT) {
            var entity_modify = MANAGER_ENTITY.get_entity_by_id(this.entity_id_being_edited);
            entity_modify.delete_property(field_name, true);
        }
    },

    create: function(edit_mode) {
        if (!is_defined(this.wall_entity_editor)) {
            this.wall_entity_editor = new FloatingWall(600, 400, new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, 0), this.entity_event_subscriber.world, false, COLOR_FLOATING_WALL_SUCCESS);
            this.wall_entity_editor.set_auto_adjust_height(true);
            this.wall_entity_editor.manual_visibility = true;
            this.wall_entity_editor.set_attachment_depth_offset(10);
            this.wall_entity_editor.add_close_button();

            this.wall_title = this.wall_entity_editor.add_row(-1).add_3D_element('HODL IOTA', TYPE_TITLE);

            this.entity_field_rows = {};

            // Default field : entity type selector.
            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE] = new FieldRowEntityType(this);
            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE].create(null);

            // Default field : entity name.
            this.entity_field_rows[ENTITY_PROPERTY_NAME] = new FieldRowName(this);
            this.entity_field_rows[ENTITY_PROPERTY_NAME].create(null);

            // Both creating a new entity and editing an existing entity will contain an add a new field button.
            this.add_new_field_button = this.wall_entity_editor.add_row(null, ADD_NEW_FIELD_BUTTON_ROW).add_2D_button([0, 1], 'add new field', COLOR_BLUE, null);

            // Both creating a new entity and editing an existing entity will contain an create/save-changes button.
            this.create_or_save_changes_button = this.wall_entity_editor.add_row(null).add_2D_button([0, 1], 'SET MY TEXT', COLOR_GREEN, this.create_or_save_changes_button_pressed.bind(this));

            // Both creating a new entity and editing an existing entity will contain an delete/cancel button.
            this.cancel_or_delete_button = this.wall_entity_editor.add_row(null).add_2D_button([0, 1], 'SET MY TEXT', COLOR_RED, this.cancel_or_delete_button_pressed.bind(this));

            // Create the entity field creator.
            this.wall_entity_field_creater = new EntityFieldCreator(this);
            this.wall_entity_field_creater.set_display_button(this.add_new_field_button);
        } else {
            this.wall_entity_editor.detach_from_parent();
        }

        // Delete all previously made non-default entity field rows.
        var field_rows_to_delete = [];
        for (var key in this.entity_field_rows) {
            if (this.entity_field_rows.hasOwnProperty(key)) {
                if (key !== ENTITY_DEFAULT_PROPERTY_TYPE && key !== ENTITY_PROPERTY_NAME) {
                    field_rows_to_delete.push(key);
                }
            }
        }
        for (var r = 0; r < field_rows_to_delete.length; r++) {
            this.wall_entity_editor.delete_row_by_name(field_rows_to_delete[r]);
            delete this.entity_field_rows[field_rows_to_delete[r]];
        }

        if (edit_mode === EDITOR_MODE_CREATE) {
            this.current_mode = EDITOR_MODE_CREATE;

            this.wall_entity_editor.attach_to(this.create_new_entity_button);
            this.wall_title.update_text('Create New Entity');

            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE].set_value_to_default();
            this.entity_field_rows[ENTITY_PROPERTY_NAME].set_value_to_default();

            this.create_or_save_changes_button.update_text('create entity');
            this.cancel_or_delete_button.update_text('cancel');
        } else {
            this.current_mode = EDITOR_MODE_EDIT;

            this.wall_entity_editor.attach_to(this.entity_being_edited_button);

            var entity_being_edited = MANAGER_ENTITY.get_entity_by_id(this.entity_id_being_edited);

            this.wall_title.update_text('Editing : ' + entity_being_edited.get_value(ENTITY_PROPERTY_NAME));

            this.create_or_save_changes_button.update_text('save changes');
            this.cancel_or_delete_button.update_text('delete entity');

            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE].set_value_from_entity();
            this.entity_field_rows[ENTITY_PROPERTY_NAME].set_value_from_entity();

            // TODO : Populate all the required entity field rows.
            var all_non_default_editable_fields = entity_being_edited.get_all_non_default_editable_fields();
            // Sort the fields alphabetically.
            all_non_default_editable_fields = all_non_default_editable_fields.sort(function(a, b) {
                return a[0] > b[0];
            });

            for (var f = 0; f < all_non_default_editable_fields.length; f++) {
                this.add_entity_field(all_non_default_editable_fields[f][0], all_non_default_editable_fields[f][1]);
            }
        }

        this.wall_entity_editor.refresh_position_and_look_at();
        this.wall_entity_editor.force_display_self_and_all_child_attachments_recursively();
    },

    _add_entity_field: function(field_name) {
        if (field_name === ENTITY_PROPERTY_START_DATE_TIME || field_name === ENTITY_PROPERTY_END_DATE_TIME) {
            this.entity_field_rows[field_name] = new FieldRowDateTime(this, field_name);
        } else if (field_name === ENTITY_PROPERTY_COMPLETED) {
            this.entity_field_rows[field_name] = new FieldRowCompleted(this);
        } else {
            this.entity_field_rows[field_name] = new FieldRowRegular(this, field_name);
        }
        return this.entity_field_rows[field_name];
    },

    add_entity_field: function(field_name, field_value) {
        // Check if the field was already added.
        if (!this.wall_entity_editor.has_row_with_name(field_name)) {

            // Get the row index of the 'add new field' button.
            var insert_index_for_row = this.wall_entity_editor.get_row_with_name(ADD_NEW_FIELD_BUTTON_ROW).row_number;

            var new_entity_field = this._add_entity_field(field_name);
            new_entity_field.create(insert_index_for_row, field_value);<commit_msg>client side dynamic content management work 06<commit_after>'use strict';

const EDITOR_MODE_CREATE = 1;
const EDITOR_MODE_EDIT   = 2;

const ADD_NEW_FIELD_BUTTON_ROW = 'add_new_field_button';

function EntityEditor(entity_event_subscriber) {
    this.__init__(entity_event_subscriber);
}

EntityEditor.prototype = {

    __init__: function(entity_event_subscriber) {
        this.entity_event_subscriber = entity_event_subscriber;
        this.current_mode = null;
    },

    create_or_save_changes_button_pressed: function() {
        if (this.current_mode === EDITOR_MODE_CREATE) {
            this._entity_created();
        } else {
            this._modify_entity();
        }
    },

    cancel_or_delete_button_pressed: function() {
        if (this.current_mode === EDITOR_MODE_CREATE) {
            this.wall_entity_editor.force_hide_self_and_all_child_attachments_recursively();
        } else {
            MANAGER_ENTITY.delete_entity_by_id(this.entity_id_being_edited);
        }
    },

    delete_entity_field: function(field_name) {
        delete this.entity_field_rows[field_name];
        this.wall_entity_editor.delete_row_by_name(field_name);

        if (this.current_mode === EDITOR_MODE_EDIT) {
            var entity_modify = MANAGER_ENTITY.get_entity_by_id(this.entity_id_being_edited);
            entity_modify.delete_property(field_name, true);
        }
    },

    create: function(edit_mode) {
        if (!is_defined(this.wall_entity_editor)) {
            this.wall_entity_editor = new FloatingWall(600, 400, null, null, this.entity_event_subscriber.world, false, COLOR_FLOATING_WALL_SUCCESS);
            this.wall_entity_editor.manual_visibility = true;
            this.wall_entity_editor.set_attachment_depth_offset(10);
            this.wall_entity_editor.add_close_button();

            this.wall_title = this.wall_entity_editor.add_row(-1).add_3D_element('HODL IOTA', TYPE_TITLE);

            this.entity_field_rows = {};

            // Default field : entity type selector.
            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE] = new FieldRowEntityType(this);
            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE].create(null);

            // Default field : entity name.
            this.entity_field_rows[ENTITY_PROPERTY_NAME] = new FieldRowName(this);
            this.entity_field_rows[ENTITY_PROPERTY_NAME].create(null);

            // Both creating a new entity and editing an existing entity will contain an add a new field button.
            this.add_new_field_button = this.wall_entity_editor.add_row(null, ADD_NEW_FIELD_BUTTON_ROW).add_2D_button([0, 1], 'add new field', COLOR_BLUE, null);

            // Both creating a new entity and editing an existing entity will contain an create/save-changes button.
            this.create_or_save_changes_button = this.wall_entity_editor.add_row(null).add_2D_button([0, 1], 'SET MY TEXT', COLOR_GREEN, this.create_or_save_changes_button_pressed.bind(this));

            // Both creating a new entity and editing an existing entity will contain an delete/cancel button.
            this.cancel_or_delete_button = this.wall_entity_editor.add_row(null).add_2D_button([0, 1], 'SET MY TEXT', COLOR_RED, this.cancel_or_delete_button_pressed.bind(this));

            // Create the entity field creator.
            this.wall_entity_field_creater = new EntityFieldCreator(this);
            this.wall_entity_field_creater.set_display_button(this.add_new_field_button);
        } else {
            this.wall_entity_editor.detach_from_parent();
        }

        // Delete all previously made non-default entity field rows.
        var field_rows_to_delete = [];
        for (var key in this.entity_field_rows) {
            if (this.entity_field_rows.hasOwnProperty(key)) {
                if (key !== ENTITY_DEFAULT_PROPERTY_TYPE && key !== ENTITY_PROPERTY_NAME) {
                    field_rows_to_delete.push(key);
                }
            }
        }
        for (var r = 0; r < field_rows_to_delete.length; r++) {
            this.wall_entity_editor.delete_row_by_name(field_rows_to_delete[r]);
            delete this.entity_field_rows[field_rows_to_delete[r]];
        }

        if (edit_mode === EDITOR_MODE_CREATE) {
            this.current_mode = EDITOR_MODE_CREATE;

            this.wall_entity_editor.attach_to(this.create_new_entity_button);
            this.wall_title.update_text('Create New Entity');

            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE].set_value_to_default();
            this.entity_field_rows[ENTITY_PROPERTY_NAME].set_value_to_default();

            this.create_or_save_changes_button.update_text('create entity');
            this.cancel_or_delete_button.update_text('cancel');
        } else {
            this.current_mode = EDITOR_MODE_EDIT;

            this.wall_entity_editor.attach_to(this.entity_being_edited_button);

            var entity_being_edited = MANAGER_ENTITY.get_entity_by_id(this.entity_id_being_edited);

            this.wall_title.update_text('Editing : ' + entity_being_edited.get_value(ENTITY_PROPERTY_NAME));

            this.create_or_save_changes_button.update_text('save changes');
            this.cancel_or_delete_button.update_text('delete entity');

            this.entity_field_rows[ENTITY_DEFAULT_PROPERTY_TYPE].set_value_from_entity();
            this.entity_field_rows[ENTITY_PROPERTY_NAME].set_value_from_entity();

            // TODO : Populate all the required entity field rows.
            var all_non_default_editable_fields = entity_being_edited.get_all_non_default_editable_fields();
            // Sort the fields alphabetically.
            all_non_default_editable_fields = all_non_default_editable_fields.sort(function(a, b) {
                return a[0] > b[0];
            });

            for (var f = 0; f < all_non_default_editable_fields.length; f++) {
                this.add_entity_field(all_non_default_editable_fields[f][0], all_non_default_editable_fields[f][1]);
            }
        }

        this.wall_entity_editor.set_auto_adjust_height(true);
        this.wall_entity_editor.auto_adjust_height_if_needed();
        this.wall_entity_editor.refresh_position_and_look_at();
        this.wall_entity_editor.force_display_self_and_all_child_attachments_recursively();
    },

    _add_entity_field: function(field_name) {
        if (field_name === ENTITY_PROPERTY_START_DATE_TIME || field_name === ENTITY_PROPERTY_END_DATE_TIME) {
            this.entity_field_rows[field_name] = new FieldRowDateTime(this, field_name);
        } else if (field_name === ENTITY_PROPERTY_COMPLETED) {
            this.entity_field_rows[field_name] = new FieldRowCompleted(this);
        } else {
            this.entity_field_rows[field_name] = new FieldRowRegular(this, field_name);
        }
        return this.entity_field_rows[field_name];
    },

    add_entity_field: function(field_name, field_value) {
        // Check if the field was already added.
        if (!this.wall_entity_editor.has_row_with_name(field_name)) {

            // Get the row index of the 'add new field' button.
            var insert_index_for_row = this.wall_entity_editor.get_row_with_name(ADD_NEW_FIELD_BUTTON_ROW).row_number;

            var new_entity_field = this._add_entity_field(field_name);
            new_entity_field.create(insert_index_for_row, field_value);
{'fraction_non_alphanumeric': 0.088045618084312, 'fraction_numerical': 0.004344579458285249, 'mean_word_length': 3.1059085841694536, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1374301', 'n_tokens_mistral': 4578, 'n_tokens_neox': 4472, 'n_words': 658}