<commit_before>/*
Copyright 2010 University of Toronto

Licensed under the Educational Community License (ECL), Version 2.0. 
ou may not use this file except in compliance with this License.

You may obtain a copy of the ECL 2.0 License at
https://source.collectionspace.org/collection-space/LICENSE.txt
*/

/*global jQuery, fluid, cspace*/

cspace = cspace || {};

(function ($, fluid) {

    var injectElementsOfType = function (container, elementType, elements) {
        if (!elements || elements.length < 1) {
            return;
        }

        var elementsOfType = $(elementType, container);
        var repeat = elementsOfType.length === 0 ? function (idx, element) {
            container.append(element);
        } : function (idx, element) {
            var lastEl = $(elementType + ":last", container);
            lastEl.after(element);
        };

        $.each(elements, repeat);
    };
    
    var inject = function (docString, selector, container) {
        if (!docString || docString === "") {
            return;
        }

//        var headTag = docString.match(/<head(.|\s)*?\/head>/gi)[0];
        var bodyTag = docString.match(/<body(.|\s)*?\/body>/gi);
        if (bodyTag) {
            bodyTag = bodyTag[0];
        } else {
            bodyTag = docString;
        }
// Currently, parsing the link and script tags is not working quite properly, so
// for now, forego that process: assume the target HTML has everything you'd need
//        var linkTags = [].concat(headTag.match(/<link(.|\s)*?\/>/gi)).concat(headTag.match(/<link(.|\s)*?\/link>/gi)),
//            scriptTags = headTag.match(/<script(.|\s)*?\/script>/gi);

//        var head = $("head");
//        injectElementsOfType(head, "link", linkTags);
//        injectElementsOfType(head, "script", scriptTags);

        var templateContainer = $("<div></div>").html(bodyTag);
        var templateContent = $(selector, templateContainer);
        container.append($(selector, templateContainer)); 
    };    
    
    var expandOptions = function (pageBuilder, args) {        
        $.each(args, function (index, arg) {
            if (arg) {
                var type = typeof(arg);
                if (type === "object") {
                    expandOptions(pageBuilder, arg);
                }
                else if (type === "string" && arg.indexOf("{pageBuilder}") === 0) {
                    args[index] = fluid.model.getBeanValue(pageBuilder, arg.substring(14, arg.length));
                }
            }
        });
    };

    var invokeDependencies = function (that) {
        that.events.onDependencySetup.fire(that.uispec);
        
        that.components = [];
        for (var region in that.dependencies) {
            if (that.dependencies.hasOwnProperty(region)) {
                var dep = that.dependencies[region];
                expandOptions(that, dep.args);
                that.components[region] = fluid.invokeGlobalFunction(dep.funcName, dep.args);
            }
        }
    };

    var setupModelAndDependencies = function (that) {
        that.applier = fluid.makeChangeApplier(that.model);    
        invokeDependencies(that);
        that.events.pageReady.fire();
    };
    
    var createEmptyModel = function () {
        return {
            csid: null,
            fields: {},
            termsUsed: [],
            relations: []
        };
    };
    
    var setUpPageBuilder = function (that) {
        that.model = createEmptyModel();        
        that.dataContext = fluid.initSubcomponent(that, "dataContext", [that.model, fluid.COMPONENT_OPTIONS]);
        that.dataContext.events.afterFetch.addListener(function () {
            setupModelAndDependencies(that);
        });
        
        if (that.options.csid) {
            that.dataContext.fetch(that.options.csid);
        } else {
            setupModelAndDependencies(that);
        }
    };

    var assembleHTML = function (that) {
        fluid.fetchResources(that.options.pageSpec, function (resourceSpecs) {
            for (var regionName in resourceSpecs) {<commit_msg>CSPACE-2060: Committing Michelle and Yura's patch to update the UI layer to correctly treat relationships as a hash of named values instead of an unsorted array.<commit_after>/*
Copyright 2010 University of Toronto

Licensed under the Educational Community License (ECL), Version 2.0. 
ou may not use this file except in compliance with this License.

You may obtain a copy of the ECL 2.0 License at
https://source.collectionspace.org/collection-space/LICENSE.txt
*/

/*global jQuery, fluid, cspace*/

cspace = cspace || {};

(function ($, fluid) {

    var injectElementsOfType = function (container, elementType, elements) {
        if (!elements || elements.length < 1) {
            return;
        }

        var elementsOfType = $(elementType, container);
        var repeat = elementsOfType.length === 0 ? function (idx, element) {
            container.append(element);
        } : function (idx, element) {
            var lastEl = $(elementType + ":last", container);
            lastEl.after(element);
        };

        $.each(elements, repeat);
    };
    
    var inject = function (docString, selector, container) {
        if (!docString || docString === "") {
            return;
        }

//        var headTag = docString.match(/<head(.|\s)*?\/head>/gi)[0];
        var bodyTag = docString.match(/<body(.|\s)*?\/body>/gi);
        if (bodyTag) {
            bodyTag = bodyTag[0];
        } else {
            bodyTag = docString;
        }
// Currently, parsing the link and script tags is not working quite properly, so
// for now, forego that process: assume the target HTML has everything you'd need
//        var linkTags = [].concat(headTag.match(/<link(.|\s)*?\/>/gi)).concat(headTag.match(/<link(.|\s)*?\/link>/gi)),
//            scriptTags = headTag.match(/<script(.|\s)*?\/script>/gi);

//        var head = $("head");
//        injectElementsOfType(head, "link", linkTags);
//        injectElementsOfType(head, "script", scriptTags);

        var templateContainer = $("<div></div>").html(bodyTag);
        var templateContent = $(selector, templateContainer);
        container.append($(selector, templateContainer)); 
    };    
    
    var expandOptions = function (pageBuilder, args) {        
        $.each(args, function (index, arg) {
            if (arg) {
                var type = typeof(arg);
                if (type === "object") {
                    expandOptions(pageBuilder, arg);
                }
                else if (type === "string" && arg.indexOf("{pageBuilder}") === 0) {
                    args[index] = fluid.model.getBeanValue(pageBuilder, arg.substring(14, arg.length));
                }
            }
        });
    };

    var invokeDependencies = function (that) {
        that.events.onDependencySetup.fire(that.uispec);
        
        that.components = [];
        for (var region in that.dependencies) {
            if (that.dependencies.hasOwnProperty(region)) {
                var dep = that.dependencies[region];
                expandOptions(that, dep.args);
                that.components[region] = fluid.invokeGlobalFunction(dep.funcName, dep.args);
            }
        }
    };

    var setupModelAndDependencies = function (that) {
        that.applier = fluid.makeChangeApplier(that.model);    
        invokeDependencies(that);
        that.events.pageReady.fire();
    };
    
    var createEmptyModel = function () {
        return {
            csid: null,
            fields: {},
            termsUsed: [],
            relations: {}
        };
    };
    
    var setUpPageBuilder = function (that) {
        that.model = createEmptyModel();        
        that.dataContext = fluid.initSubcomponent(that, "dataContext", [that.model, fluid.COMPONENT_OPTIONS]);
        that.dataContext.events.afterFetch.addListener(function () {
            setupModelAndDependencies(that);
        });
        
        if (that.options.csid) {
            that.dataContext.fetch(that.options.csid);
        } else {
            setupModelAndDependencies(that);
        }
    };

    var assembleHTML = function (that) {
        fluid.fetchResources(that.options.pageSpec, function (resourceSpecs) {
            for (var regionName in resourceSpecs) {
{'fraction_non_alphanumeric': 0.1332691845080587, 'fraction_numerical': 0.004089487611258119, 'mean_word_length': 2.0705317577548006, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '7299929', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2219, 'n_words': 525}