<commit_before>/*
Copyright 2009-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_1_2, cspace*/

cspace = cspace || {};

(function ($, fluid) {

    // operation = one of "create", "delete", "fetch", "update"
    var makeDCErrorHandler = function (that) {
        return function (operation, message) {
            var msgKey = operation + "FailedMessage";
            var msg = that.options.strings[msgKey] + message;
            cspace.util.displayTimestampedMessage(that.dom, msg, "");
            that.events.onError.fire(operation);
            if (operation === "create") {
                // This is only temporary until http://issues.collectionspace.org/browse/CSPACE-263
                // is resolved
                that.applier.requestChange("csid", undefined);
            }
        };
    };

    var makeShowConfirmation = function (that) {
        return function (e) {
            if (that.unsavedChanges) {
                var href;
                if (e.target.nodeName === "IMG") {
                    // this assumes that if the target is an image, it must be wrapped in an <a>
                    href = e.target.parentNode.href;
                } else {
                    href = e.target.href;
                }
                that.confirmation.open(href);
                return false;
            }
        };
    };

	var setupConfirmation = function (that) {
        var confirmationOpts = {
            action: that.save,
            actionSuccessEvents: [that.events.afterCreateObjectDataSuccess, that.events.afterUpdateObjectDataSuccess],
            actionErrorEvents: [that.events.onError]
        };
        // TODO: This should be a subcomponent, with an option block that can be overridden
        that.confirmation = cspace.confirmation(that.container, confirmationOpts);

        var showConf = makeShowConfirmation(that);
        $("a:not([href*=#]):not([class*='" + that.options.selectors.confirmationExclusion.substring(1) + "']):not(.ui-autocomplete a)").live("click", showConf); 
    };

    var validateIdentificationNumber = function (domBinder, container, message) {
        return function () {
            var required = domBinder.locate("identificationNumber");
            if (required === container) {
                return true;
            }
            if ( $.trim(required.val()) === "") {
                cspace.util.displayTimestampedMessage(domBinder, message);
                return false;
            }
            return true;
        };
    };

    var bindEventHandlers = function (that) {

        setupConfirmation(that);

        that.events.onSave.addListener(validateIdentificationNumber(that.dom, that.container, that.options.strings.identificationNumberRequired));

        that.events.onSave.addListener(function () {
            cspace.util.displayTimestampedMessage(that, that.options.strings.savingMessage, "");
        });

        that.options.dataContext.events.afterCreate.addListener(function (data) {
            that.applier.requestChange("csid", data.csid);
            that.locate("deleteButton").removeAttr("disabled").removeClass("deactivate");
            that.events.afterCreateObjectDataSuccess.fire(data, that.options.strings.createSuccessfulMessage);
            that.applier.requestChange("termsUsed", data.termsUsed);
	        cspace.util.displayTimestampedMessage(that, that.options.strings.createSuccessfulMessage, Date());
            that.unsavedChanges = false;
        });

        that.options.dataContext.events.afterUpdate.addListener(function (data) {
            that.events.afterUpdateObjectDataSuccess.fire(data, that.options.strings.updateSuccessfulMessage);
            that.applier.requestChange("termsUsed", data.termsUsed);
	        cspace.util.displayTimestampedMessage(that, that.options.strings.updateSuccessfulMessage, Date());
            that.unsavedChanges = false;
        });

        that.options.dataContext.events.afterRemove.addListener(function() {
            that.events.afterRemove.fire(that.options.strings.removeSuccessfulMessage);
        });

        that.options.dataContext.events.afterFetch.addListener(function (data) {
            that.refreshView();
        });

        that.events.afterRender.addListener(function () {
            that.locate("save").click(that.save);
            that.locate("deleteButton").click(that.remove);
            var setUnchanged = function () {
                that.unsavedChanges = true;
            };
            for (var selector in that.options.uispec) {
                if (that.options.uispec.hasOwnProperty(selector)) {<commit_msg>CSPACE-1515: Committing Yura's fix for this one: "The reason it was duplicating is because the page was rendered twice in IE7,8, when it should've been only once. The reason it was happening is because RecordEditor would listen to afterFetch event when the intention was to listen to modelChanged event that is fired afterFetch anyways. Changing the listener to modelChanged prevents RecordEditor from rendering twice."<commit_after>/*
Copyright 2009-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_1_2, cspace*/

cspace = cspace || {};

(function ($, fluid) {

    // operation = one of "create", "delete", "fetch", "update"
    var makeDCErrorHandler = function (that) {
        return function (operation, message) {
            var msgKey = operation + "FailedMessage";
            var msg = that.options.strings[msgKey] + message;
            cspace.util.displayTimestampedMessage(that.dom, msg, "");
            that.events.onError.fire(operation);
            if (operation === "create") {
                // This is only temporary until http://issues.collectionspace.org/browse/CSPACE-263
                // is resolved
                that.applier.requestChange("csid", undefined);
            }
        };
    };

    var makeShowConfirmation = function (that) {
        return function (e) {
            if (that.unsavedChanges) {
                var href;
                if (e.target.nodeName === "IMG") {
                    // this assumes that if the target is an image, it must be wrapped in an <a>
                    href = e.target.parentNode.href;
                } else {
                    href = e.target.href;
                }
                that.confirmation.open(href);
                return false;
            }
        };
    };

	var setupConfirmation = function (that) {
        var confirmationOpts = {
            action: that.save,
            actionSuccessEvents: [that.events.afterCreateObjectDataSuccess, that.events.afterUpdateObjectDataSuccess],
            actionErrorEvents: [that.events.onError]
        };
        // TODO: This should be a subcomponent, with an option block that can be overridden
        that.confirmation = cspace.confirmation(that.container, confirmationOpts);

        var showConf = makeShowConfirmation(that);
        $("a:not([href*=#]):not([class*='" + that.options.selectors.confirmationExclusion.substring(1) + "']):not(.ui-autocomplete a)").live("click", showConf); 
    };

    var validateIdentificationNumber = function (domBinder, container, message) {
        return function () {
            var required = domBinder.locate("identificationNumber");
            if (required === container) {
                return true;
            }
            if ( $.trim(required.val()) === "") {
                cspace.util.displayTimestampedMessage(domBinder, message);
                return false;
            }
            return true;
        };
    };

    var bindEventHandlers = function (that) {

        setupConfirmation(that);

        that.events.onSave.addListener(validateIdentificationNumber(that.dom, that.container, that.options.strings.identificationNumberRequired));

        that.events.onSave.addListener(function () {
            cspace.util.displayTimestampedMessage(that, that.options.strings.savingMessage, "");
        });

        that.options.dataContext.events.afterCreate.addListener(function (data) {
            that.applier.requestChange("csid", data.csid);
            that.locate("deleteButton").removeAttr("disabled").removeClass("deactivate");
            that.events.afterCreateObjectDataSuccess.fire(data, that.options.strings.createSuccessfulMessage);
            that.applier.requestChange("termsUsed", data.termsUsed);
	        cspace.util.displayTimestampedMessage(that, that.options.strings.createSuccessfulMessage, Date());
            that.unsavedChanges = false;
        });

        that.options.dataContext.events.afterUpdate.addListener(function (data) {
            that.events.afterUpdateObjectDataSuccess.fire(data, that.options.strings.updateSuccessfulMessage);
            that.applier.requestChange("termsUsed", data.termsUsed);
	        cspace.util.displayTimestampedMessage(that, that.options.strings.updateSuccessfulMessage, Date());
            that.unsavedChanges = false;
        });

        that.options.dataContext.events.afterRemove.addListener(function() {
            that.events.afterRemove.fire(that.options.strings.removeSuccessfulMessage);
        });

        that.options.dataContext.events.modelChanged.addListener(function (data) {
            that.refreshView();
        });

        that.events.afterRender.addListener(function () {
            that.locate("save").click(that.save);
            that.locate("deleteButton").click(that.remove);
            var setUnchanged = function () {
                that.unsavedChanges = true;
            };
            for (var selector in that.options.uispec) {
                if (that.options.uispec.hasOwnProperty(selector)) {
{'fraction_non_alphanumeric': 0.11185638192937462, 'fraction_numerical': 0.004142828960347208, 'mean_word_length': 2.4023489932885904, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7229827', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2580, 'n_words': 547}