<commit_before>/*global jQuery, fluid_1_0*/

var cspace = cspace || {};

(function ($, fluid) {

    var buildFullUISchema = function (that) {
        var fullUISchema = fluid.copy(that.schema);
        
		// This makes the assumption that 'save' exists. This should be configurable.
        fullUISchema.save = {
            "selector": that.options.selectors.save,
            "validators": [],
            "decorators": [
                {type: "jQuery",
                    func: "click", 
                    args: that.save
                }
            ]
        };
        return fullUISchema;
    };
    
    var bindEventHandlers = function (that) {
        that.events.afterFetchSchemaSuccess.addListener(function (schema, textStatus) {
            that.schema = schema;
        });
        
        that.events.afterFetchSchemaError.addListener(function (xhr, msg, error) {
            that.showSchemaErrorMessage(that.options.strings.schemaFetchError + msg + that.options.strings.errorRecoverySuggestion);
        });

        that.events.afterFetchObjectDataSuccess.addListener(function (data, textStatus) {
            that.updateModel(data);
        });

        that.events.afterFetchObjectDataError.addListener(function (xhr, msg, error) {
            // TODO: decide on appropriate response to this situation
        });
    };
    
    var setupObjectEntry = function (that) {
        that.objectDAO = fluid.initSubcomponent(that, "dao");
        bindEventHandlers(that);
        that.objectDAO.fetchObjectSchema(that.events.afterFetchSchemaSuccess.fire, that.events.afterFetchSchemaError.fire);
        that.objectDAO.fetchObjectForId(that.options.objectId, that.events.afterFetchObjectDataSuccess.fire, that.events.afterFetchObjectDataError.fire);
    };
    
    cspace.objectEntry = function (container, options) {
        var that = fluid.initView("cspace.objectEntry", container, options);
        that.model = {};
        
        that.refreshView = function () {
            cspace.renderer.renderPage(that);
        };
        
        that.updateModel = function (newModel, source) {
            that.model = newModel;
            that.refreshView();
        };
        
        that.showSchemaErrorMessage = function (msg) {
            that.locate("errorMessage").text(msg);
            that.locate("errorDialog").dialog({
                modal: true,
                dialogClass: "fl-widget"
            });
        };
        
        that.save = function () {
            that.events.onSave.fire(that.model);
            that.objectDAO.saveObjectForId(that.model,
                that.options.objectId,
                that.events.afterSaveObjectDataSuccess.fire,
                that.events.afterSaveObjectDataError.fire
            );        
            return false;
        };

        setupObjectEntry(that);
        return that;
    };
    
    cspace.saveId = "save";
    
    cspace.renderer = {
        buildCutpoints: function (schema) {
            var cutpoints = [];
            
            var index = 0;
            for (var key in schema) {
                if (schema.hasOwnProperty(key)) {
                    cutpoints[index] = {
                        id: key,
                        selector: schema[key].selector
                    };
                    index += 1;
                }
            }
            return cutpoints;
        },
        buildComponentTree: function (schema, model) {
            var tree = {children: []};
            
            var index = 0;
            for (var key in schema) {
                if (schema.hasOwnProperty(key)) {
                    tree.children[index] = {
                        ID: key,
                        valuebinding: key
                    };
                    if (schema[key].decorators && schema[key].decorators.length > 0) {
                        tree.children[index].decorators = schema[key].decorators;
                    }
                    index += 1;
                }
            }
            return tree;
        },
        renderPage: function (that) {
            var fullUISchema = buildFullUISchema(that);
            var renderOptions = {
                model: that.model,
                autoBind: true,
                debugMode: true
            };
            var resources = {
                objEntry: {
                    href: that.options.templateUrl,
                    nodeId: "csc-object-entry-template",
                    cutpoints: cspace.renderer.buildCutpoints(fullUISchema)
                }
            };
            fluid.fetchResources(resources, function () {
                var template = fluid.parseTemplates(resources, ["objEntry"], {});
                fluid.reRender(template,
                               that.container,
                               cspace.renderer.buildComponentTree(fullUISchema, that.model),
                               renderOptions);
            });
        }    
    };
    
    fluid.defaults("cspace.objectEntry", {
        dao: {
            type: "cspace.collectionObjectDAO"
        },
        events: {<commit_msg>CSPACE-49: Fix a bug in creation of new object (as opposd to editing existing object): Build empty model from schema; Don't load object data unless an objectId is provided. Fix handling of new model to use Infusion's model functions.<commit_after>/*global jQuery, fluid_1_0*/

var cspace = cspace || {};

(function ($, fluid) {

    var buildFullUISchema = function (that) {
        var fullUISchema = fluid.copy(that.schema);
        
		// This makes the assumption that 'save' exists. This should be configurable.
        fullUISchema.save = {
            "selector": that.options.selectors.save,
            "validators": [],
            "decorators": [
                {type: "jQuery",
                    func: "click", 
                    args: that.save
                }
            ]
        };
        return fullUISchema;
    };
    
    var buildEmptyModelFromSchema = function (schema) {
        var model = {};
        for (var key in schema) {
            if (schema.hasOwnProperty(key)) {
                model[key] = "";
            }
        }
        return model;
    };
    
    var bindEventHandlers = function (that) {
        that.events.afterFetchSchemaSuccess.addListener(function (schema, textStatus) {
            that.schema = schema;
            that.model = buildEmptyModelFromSchema(schema);
            if (that.options.objectId) {
                that.objectDAO.fetchObjectForId(that.options.objectId, that.events.afterFetchObjectDataSuccess.fire, that.events.afterFetchObjectDataError.fire);
            } else {
                that.refreshView();
            }
        });
        
        that.events.afterFetchSchemaError.addListener(function (xhr, msg, error) {
            that.showSchemaErrorMessage(that.options.strings.schemaFetchError + msg + that.options.strings.errorRecoverySuggestion);
        });

        that.events.afterFetchObjectDataSuccess.addListener(function (data, textStatus) {
            that.updateModel(data);
        });

        that.events.afterFetchObjectDataError.addListener(function (xhr, msg, error) {
            // TODO: decide on appropriate response to this situation
        });
    };
    
    var setupObjectEntry = function (that) {
        that.objectDAO = fluid.initSubcomponent(that, "dao");
        bindEventHandlers(that);
        that.objectDAO.fetchObjectSchema(that.events.afterFetchSchemaSuccess.fire, that.events.afterFetchSchemaError.fire);
    };
    
    cspace.objectEntry = function (container, options) {
        var that = fluid.initView("cspace.objectEntry", container, options);
        that.model = {};
        
        that.refreshView = function () {
            cspace.renderer.renderPage(that);
        };
        
        that.updateModel = function (newModel, source) {
            that.events.modelChanged.fire(newModel, that.model, source);
            fluid.clear(that.model);
            fluid.model.copyModel(that.model, newModel);
            that.refreshView();
        };
        
        that.showSchemaErrorMessage = function (msg) {
            that.locate("errorMessage").text(msg);
            that.locate("errorDialog").dialog({
                modal: true,
                dialogClass: "fl-widget"
            });
        };
        
        that.save = function () {
            that.events.onSave.fire(that.model);
            that.objectDAO.saveObjectForId(that.model,
                that.options.objectId,
                that.events.afterSaveObjectDataSuccess.fire,
                that.events.afterSaveObjectDataError.fire
            );        
            return false;
        };

        setupObjectEntry(that);
        return that;
    };
    
    cspace.saveId = "save";
    
    cspace.renderer = {
        buildCutpoints: function (schema) {
            var cutpoints = [];
            
            var index = 0;
            for (var key in schema) {
                if (schema.hasOwnProperty(key)) {
                    cutpoints[index] = {
                        id: key,
                        selector: schema[key].selector
                    };
                    index += 1;
                }
            }
            return cutpoints;
        },
        buildComponentTree: function (schema, model) {
            var tree = {children: []};
            
            var index = 0;
            for (var key in schema) {
                if (schema.hasOwnProperty(key)) {
                    tree.children[index] = {
                        ID: key,
                        valuebinding: key
                    };
                    if (schema[key].decorators && schema[key].decorators.length > 0) {
                        tree.children[index].decorators = schema[key].decorators;
                    }
                    index += 1;
                }
            }
            return tree;
        },
        renderPage: function (that) {
            var fullUISchema = buildFullUISchema(that);
            var renderOptions = {
                model: that.model,
                autoBind: true,
                debugMode: true
            };
            var resources = {
                objEntry: {
                    href: that.options.templateUrl,
                    nodeId: "csc-object-entry-template",
                    cutpoints: cspace.renderer.buildCutpoints(fullUISchema)
                }
            };
            fluid.fetchResources(resources, function () {
                var template = fluid.parseTemplates(resources, ["objEntry"], {});
                fluid.reRender(template,
                               that.container,
                               cspace.renderer.buildComponentTree(fullUISchema, that.model),
                               renderOptions);
            });
        }    
    };
    
    fluid.defaults("cspace.objectEntry", {
        dao: {
            type: "cspace.collectionObjectDAO"
        },
        events: {
            modelChanged: null,
{'fraction_non_alphanumeric': 0.10067720090293454, 'fraction_numerical': 0.0014446952595936794, 'mean_word_length': 1.442337375964719, 'pattern_counts': {'":': 6, '<': 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': '908592', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2767, 'n_words': 512}