<commit_before>/*global window,location,ractive*/
(function (root, factory) {
    "use strict";

    if (typeof module === 'object' && module.exports) {
        var Ractive = require("ractive");
        Ractive.events = require("ractive-events-keys");
        var axios = require("axios");
        var Graph = require("./graph");
        var Ontology = require("./ontology");
        var StringUtil = require("./stringutil");
        var _ = require("underscore");
        var $ = require("jquery");
        var ldGraph = require("ld-graph");
        var select2 = require("select2");

        module.exports = factory(Ractive, axios, Graph, Ontology, StringUtil, _, $, ldGraph, select2);

    } else {
        // Browser globals (root is window)
        root.Main = factory(root.Ractive, root.axios, root.Graph, root.Ontology, root.StringUtil, root._, root.$, root.ldGraph, root.select2);
    }
}(this, function (Ractive, axios, Graph, Ontology, StringUtil, _, $, ldGraph, select2) {
    "use strict";

    Ractive.DEBUG = false;
    var ractive;

    // need to leave already parsed JSON from axios
    var ensureJSON = function (res) {
        return (typeof res === "string") ? JSON.parse(res) : res;
    };

    /* Private functions */
    function unPrefix(prefixed) {
        return _.last(prefixed.split(":"));
    }

    var unsetInputsForDomain = function (domainType) {
        _.each(ractive.get("inputs"), function (input, inputIndex) {
            if (domainType === unPrefix(input.domain)) {
                var kp = "inputs." + inputIndex;
                ractive.set(kp + '.values.*.current.value', '');
                ractive.set(kp + '.values.*.old.value', '');
            }
        });
    };

    function i18nLabelValue(label) {
        if (Array.isArray(label)) {
            var value = _.find(label, function (labelValue) {
                return ("no" === labelValue['@language']);
            })['@value'];
            if (value === undefined) {
                value = labelValue[0]['@value'];
            }
            return value;
        }
        else {
            return label['@value'];
        }
    }

    function propertyName(predicate) {
        return _.last(predicate.split("#"));
    }

    function setMultiValues(values, inputs_n) {
        if (values && values.length > 0) {
            var valuesAsArray = values.length == 0 ? [] : _.map(values, function (value) {
                return value.value;
            });
            ractive.set(inputs_n + ".values.0", {
                old: {
                    value: valuesAsArray
                },
                current: {
                    value: valuesAsArray
                }
            });
            console.log("Setting " + inputs_n + ".values.0.current.value -> " + ractive.get(inputs_n + ".values.0.current.value"));
        }
    }

    function setSingleValues(values, inputs_n) {
        if (values.length > 0) {
            var idx;
            for (idx = 0; idx < values.length; idx++) {
                ractive.set(inputs_n + ".values." + idx, {
                    old: {
                        value: values[idx].value,
                        lang: values[idx].lang
                    },
                    current: {
                        value: values[idx].value,
                        lang: values[idx].lang
                    }
                });
                console.log("Setting " + inputs_n + ".values." + idx + ".current.value -> " + ractive.get(inputs_n + ".values." + idx + ".current.value"));
            }
        }
    }

    var loadExistingResource = function (uri) {
            axios.get(uri)
                .then(function (response) {
                    ractive.set("resource_uri", uri);
                    var graphData = ensureJSON(response.data);

                    var type = StringUtil.titelize(/^.*\/(work|person|publication)\/.*$/g.exec(uri)[1]);
                    ractive.set("targetResources." + type + ".uri", uri);
                    var root = ldGraph.parse(graphData).byType(type)[0];

                        _.each(ractive.get("inputs"), function (input, inputIndex) {
                            if (type == unPrefix(input.domain)) {
                                var inputs_n = "inputs." + inputIndex;
                                var predicate = input.predicate;
                                if (input.type === "select-authorized-value") {
                                    setMultiValues(root.outAll(propertyName(predicate)), inputs_n);
                                } else if (input.type === "select-predefined-value") {
                                    setMultiValues(root.getAll(propertyName(predicate)), inputs_n);
                                } else {
                                    setSingleValues(root.getAll(propertyName(predicate)), inputs_n);
                                }
                            }
                        });
                        ractive.update();
                        ractive.set("save_status", "åpnet eksisterende ressurs");
                    $("select").select2();
                    }
                )
                //.catch(function (err) {
                //    console.log("HTTP GET existing resource failed with:");
                //    console.log(err);
                //});
        }
        ;

    var saveNewResourceFromInputs = function (resourceType) {
        // collect inputs related to resource type
        var inputsToSave = _.filter(ractive.get("inputs"), function (input) {<commit_msg>catalinker: uncomment catch block<commit_after>/*global window,location,ractive*/
(function (root, factory) {
    "use strict";

    if (typeof module === 'object' && module.exports) {
        var Ractive = require("ractive");
        Ractive.events = require("ractive-events-keys");
        var axios = require("axios");
        var Graph = require("./graph");
        var Ontology = require("./ontology");
        var StringUtil = require("./stringutil");
        var _ = require("underscore");
        var $ = require("jquery");
        var ldGraph = require("ld-graph");
        var select2 = require("select2");

        module.exports = factory(Ractive, axios, Graph, Ontology, StringUtil, _, $, ldGraph, select2);

    } else {
        // Browser globals (root is window)
        root.Main = factory(root.Ractive, root.axios, root.Graph, root.Ontology, root.StringUtil, root._, root.$, root.ldGraph, root.select2);
    }
}(this, function (Ractive, axios, Graph, Ontology, StringUtil, _, $, ldGraph, select2) {
    "use strict";

    Ractive.DEBUG = false;
    var ractive;

    // need to leave already parsed JSON from axios
    var ensureJSON = function (res) {
        return (typeof res === "string") ? JSON.parse(res) : res;
    };

    /* Private functions */
    function unPrefix(prefixed) {
        return _.last(prefixed.split(":"));
    }

    var unsetInputsForDomain = function (domainType) {
        _.each(ractive.get("inputs"), function (input, inputIndex) {
            if (domainType === unPrefix(input.domain)) {
                var kp = "inputs." + inputIndex;
                ractive.set(kp + '.values.*.current.value', '');
                ractive.set(kp + '.values.*.old.value', '');
            }
        });
    };

    function i18nLabelValue(label) {
        if (Array.isArray(label)) {
            var value = _.find(label, function (labelValue) {
                return ("no" === labelValue['@language']);
            })['@value'];
            if (value === undefined) {
                value = labelValue[0]['@value'];
            }
            return value;
        }
        else {
            return label['@value'];
        }
    }

    function propertyName(predicate) {
        return _.last(predicate.split("#"));
    }

    function setMultiValues(values, inputs_n) {
        if (values && values.length > 0) {
            var valuesAsArray = values.length == 0 ? [] : _.map(values, function (value) {
                return value.value;
            });
            ractive.set(inputs_n + ".values.0", {
                old: {
                    value: valuesAsArray
                },
                current: {
                    value: valuesAsArray
                }
            });
            console.log("Setting " + inputs_n + ".values.0.current.value -> " + ractive.get(inputs_n + ".values.0.current.value"));
        }
    }

    function setSingleValues(values, inputs_n) {
        if (values.length > 0) {
            var idx;
            for (idx = 0; idx < values.length; idx++) {
                ractive.set(inputs_n + ".values." + idx, {
                    old: {
                        value: values[idx].value,
                        lang: values[idx].lang
                    },
                    current: {
                        value: values[idx].value,
                        lang: values[idx].lang
                    }
                });
                console.log("Setting " + inputs_n + ".values." + idx + ".current.value -> " + ractive.get(inputs_n + ".values." + idx + ".current.value"));
            }
        }
    }

    var loadExistingResource = function (uri) {
            axios.get(uri)
                .then(function (response) {
                    ractive.set("resource_uri", uri);
                    var graphData = ensureJSON(response.data);

                    var type = StringUtil.titelize(/^.*\/(work|person|publication)\/.*$/g.exec(uri)[1]);
                    ractive.set("targetResources." + type + ".uri", uri);
                    var root = ldGraph.parse(graphData).byType(type)[0];

                        _.each(ractive.get("inputs"), function (input, inputIndex) {
                            if (type == unPrefix(input.domain)) {
                                var inputs_n = "inputs." + inputIndex;
                                var predicate = input.predicate;
                                if (input.type === "select-authorized-value") {
                                    setMultiValues(root.outAll(propertyName(predicate)), inputs_n);
                                } else if (input.type === "select-predefined-value") {
                                    setMultiValues(root.getAll(propertyName(predicate)), inputs_n);
                                } else {
                                    setSingleValues(root.getAll(propertyName(predicate)), inputs_n);
                                }
                            }
                        });
                        ractive.update();
                        ractive.set("save_status", "åpnet eksisterende ressurs");
                    $("select").select2();
                    }
                )
                .catch(function (err) {
                    console.log("HTTP GET existing resource failed with:");
                    console.log(err);
                });
        }

    var saveNewResourceFromInputs = function (resourceType) {
        // collect inputs related to resource type
        var inputsToSave = _.filter(ractive.get("inputs"), function (input) {
{'fraction_non_alphanumeric': 0.13497483824586629, 'fraction_numerical': 0.003235082674335011, 'mean_word_length': 1.4583609454384803, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1348013', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2811, 'n_words': 589}