<commit_before>(function(root, factory) {
    'use strict';
    // Set up Backbone appropriately for the environment. Start with AMD.
    if (typeof define === 'function' && define.amd) {
        define(['jquery', 'toastr', 'sweetalert', 'crud-utils',
               'semantic-ui'], function($, toastr, swal, crudUtils) {
            // Export global even in AMD case in case this script is loaded with
            // others that may still expect a global Backbone.
            factory(root, $, toastr, swal, crudUtils);
        });

        // Next for Node.js or CommonJS. jQuery may not be needed as a module.
    } else if (typeof exports !== 'undefined') {
        var $ = require('jquery');
        factory(root, $);
    } else {
        factory(root, (root.jQuery || root.Zepto || root.ender || root.$));
    }
}(this, function(root, $, toastr, swal, crudUtils) {
    'use strict';

    var objId = $('.ui.form :hidden[name="id"]').val(); 

    $('.ui.form').submit(function () {
        return !objId; // prevent submit the form when edit the object
    });

    $('.remove.button').click(function () {
        swal({
            type: 'warning',
            title: 'Warning',
            text: 'Are you sure to remove this <%= modelName %>?',
            showCancelButton: true,
            closeOnConfirm: false,
            cancelButtonText: 'Don\'t remove!',
        }, function () {
            $('.ui.form').addClass('loading');
            $.ajax({
                url: '/<%= packageName %>/object/' + objId + '.json',
                type: 'DELETE'
            }).done(function () {
                swal({
                    type: 'success',
                    title: 'Success!',
                    text: '<%= modelName %> "' + $('[name="name"]').val() + '" has been removed!'
                }, function () {
                    root.location.href = '/<%= packageName %>/list';
                });
            }).fail(function () {
                swal({
                    type: 'error',
                    title: 'Error!',
                    text: 'Can\'t remove <%= modelName %> "' + $('[name="name"]').val() + '"',
                });
            }).always(function () {
                $('.ui.form').removeClass('loading');
            });
        });
    });

    $('.ui.form').form({
        // you should place all the fields here, including the optional one 
        // this is due to a bug in semantic-ui, the fields placed here will
        // invoke "onValid" with the wrong "this". 
        // but NOT includeing the sophiscated field you created which by pass 
        // semantic-ui's form validation mechanism entirely.
        foo: {
            identifier: 'foo',
            rules: [{
                type: 'empty',
                prompt: 'please input name'
            }, {
                type: 'foo-test',
                prompt: 'name already exists'
            }],
        },
    }, {
        keyboardShortcuts: !objId, // only when create, enter will submit the form 
        inline: true,
        on: 'blur',
        rules: {
            'foo-test': function () {
                return false;
            },
        },
        onValid: function () {
            if (!objId || (this.val() === this.attr('data-committed-value'))) {
                return; // only commit the change when EDIT the object, and this field is changed
            }
            $('.ui.form').addClass('loading');

            var data = {};
            var $input = this;
            data[this.attr('name')] = this.val();
            $.ajax({
                url: objId + '.json',
                type: 'PUT',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
            }).done(function () {
                toastr.success('updated!', '', {
                    positionClass: 'toast-bottom-center',
                    timeOut: 1000,
                });
                $input.attr('data-committed-value', $input.val());
            }).fail(function (data) {
                data.responseJSON && crudUtils.showFormErrors(data.responseJSON.errors, $('.ui.form'));
                toastr.error('update failed!', '', {
                    positionClass: 'toast-bottom-center',
                    timeOut: 1000,
                });
            }).always(function () {
                $('.ui.form').removeClass('loading');
            });

        },
    });
    $('.ui.form input').keypress(function (e) {
        if (e.keyCode === 13) {
            $(this).blur();
            return false;
        }
    });
    function hasDirtyContent() {
        // add real implementation
        return true;
    }
    window.beforeunload = function (e) {
        if (!objId && hasDirtyContent()) {
            var confirmationMessage = 'Unsaved changes will be discarded if you leave this page';

            (e || window.event).returnValue = confirmationMessage;     //Gecko + IE
            return confirmationMessage;                                //Gecko + Webkit, Safari, Chrome etc.
        }
    };
}));<commit_msg>trivial<commit_after>(function(root, factory) {
    'use strict';
    // Set up Backbone appropriately for the environment. Start with AMD.
    if (typeof define === 'function' && define.amd) {
        define(['jquery', 'toastr', 'sweetalert', 'crud-utils',
               'semantic-ui'], function($, toastr, swal, crudUtils) {
            // Export global even in AMD case in case this script is loaded with
            // others that may still expect a global Backbone.
            factory(root, $, toastr, swal, crudUtils);
        });

        // Next for Node.js or CommonJS. jQuery may not be needed as a module.
    } else if (typeof exports !== 'undefined') {
        var $ = require('jquery');
        factory(root, $);
    } else {
        factory(root, (root.jQuery || root.Zepto || root.ender || root.$));
    }
}(this, function(root, $, toastr, swal, crudUtils) {
    'use strict';

    var objId = $('.ui.form :hidden[name="id"]').val(); 

    $('.ui.form').submit(function () {
        return !objId; // prevent submit the form when edit the object
    });

    $('.remove.button').click(function () {
        swal({
            type: 'warning',
            title: 'Warning',
            text: 'Are you sure to remove this <%= modelName %>?',
            showCancelButton: true,
            closeOnConfirm: false,
            cancelButtonText: 'Don\'t remove!',
        }, function () {
            $('.ui.form').addClass('loading');
            $.ajax({
                url: '/<%= packageName %>/object/' + objId + '.json',
                type: 'DELETE'
            }).done(function () {
                swal({
                    type: 'success',
                    title: 'Success!',
                    text: '<%= modelName %> "' + $('[name="name"]').val() + '" has been removed!'
                }, function () {
                    root.location.href = '/<%= packageName %>/list';
                });
            }).fail(function () {
                swal({
                    type: 'error',
                    title: 'Error!',
                    text: 'Can\'t remove <%= modelName %> "' + $('[name="name"]').val() + '"',
                });
            }).always(function () {
                $('.ui.form').removeClass('loading');
            });
        });
    });

    $('.ui.form').form({
        // you should place all the fields here, including the optional one 
        // this is due to a bug in semantic-ui, the fields placed here will
        // invoke "onValid" with the wrong "this". 
        // but NOT includeing the sophiscated field you created which by pass 
        // semantic-ui's form validation mechanism entirely.
        foo: {
            identifier: 'foo',
            rules: [{
                type: 'empty',
                prompt: 'please input name'
            }, {
                type: 'foo-test',
                prompt: 'name already exists'
            }],
        },
    }, {
        keyboardShortcuts: !objId, // only when create, enter will submit the form 
        inline: true,
        on: 'blur',
        rules: {
            'foo-test': function () {
                return false;
            },
            'optional': function () {
                return true;
            }
        },
        onValid: function () {
            if (!objId || (this.val() === this.attr('data-committed-value'))) {
                return; // only commit the change when EDIT the object, and this field is changed
            }
            $('.ui.form').addClass('loading');

            var data = {};
            var $input = this;
            data[this.attr('name')] = this.val();
            $.ajax({
                url: objId + '.json',
                type: 'PUT',
                data: JSON.stringify(data),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
            }).done(function () {
                toastr.success('updated!', '', {
                    positionClass: 'toast-bottom-center',
                    timeOut: 1000,
                });
                $input.attr('data-committed-value', $input.val());
            }).fail(function (data) {
                data.responseJSON && crudUtils.showFormErrors(data.responseJSON.errors, $('.ui.form'));
                toastr.error('update failed!', '', {
                    positionClass: 'toast-bottom-center',
                    timeOut: 1000,
                });
            }).always(function () {
                $('.ui.form').removeClass('loading');
            });

        },
    });
    $('.ui.form input').keypress(function (e) {
        if (e.keyCode === 13) {
            $(this).blur();
            return false;
        }
    });
    function dirty() {
        // add real implementation
        return true;
    }
    window.onbeforeunload = function (e) {
        if (!objId && dirty()) {
            var confirmationMessage = 'Unsaved changes will be discarded if you leave this page';

            (e || window.event).returnValue = confirmationMessage;     //Gecko + IE
            return confirmationMessage;                                //Gecko + Webkit, Safari, Chrome etc.
        }
    };
}));
{'fraction_non_alphanumeric': 0.15147699757869248, 'fraction_numerical': 0.0021307506053268766, 'mean_word_length': 1.4538973384030418, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4429222', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2611, 'n_words': 708}