Sha256: aba1a3dcadb069d4a6ce8325f44e7cf68f80a97e12347a5dd2d7f1274ffeb281

Contents?: true

Size: 1.92 KB

Versions: 118

Compression:

Stored size: 1.92 KB

Contents

/*global window, $ */
window.warnBeforeUnload = {

    className: 'warn-before-unload',

    ignored: {
        classes: ['do-not-warn-before-unload', 'filter'],
        types: ['search']
    },

    message: 'You have entered new data on this page. If you navigate away from this page without first saving your data, the changes will be lost.',

    init: function () {
        'use strict';
        this.watchUnload();
        this.watchInputs();
        this.watchSubmits();
    },

    watchUnload: function () {
        'use strict';
        var that = this;
        window.addEventListener('beforeunload', function (event) {
            if ($('body').hasClass(that.className)) {
                event.returnValue = that.message;
                return that.message;
            }
        });
    },

    watchInputs: function () {
        'use strict';
        var that = this;
        $(':input, textarea').bind('change', function () {
            if (that.shouldLock(this)) {
                that.lock();
            }
        });
    },

    watchSubmits: function () {
        'use strict';
        var that = this;
        $('form').on('submit', function () {
            that.unlock();
        });
    },

    shouldLock: function (object) {
        'use strict';
        var that = this;
        var shouldLock = true;
        that.ignored.classes.forEach(function (item) {
            if ($(object).hasClass(item)) {
                shouldLock = false;
            }
        });
        that.ignored.types.forEach(function (item) {
            if ($(object).attr('type') === item) {
                shouldLock = false;
            }
        });
        return shouldLock;
    },

    lock: function () {
        'use strict';
        $('body').addClass(this.className);
    },

    unlock: function () {
        'use strict';
        $('body').removeClass(this.className);
    }

};

$(function () {
    'use strict';
    window.warnBeforeUnload.init();
});

Version data entries

118 entries across 118 versions & 1 rubygems

Version Path
promethee-5.1.1 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-5.1.0 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-5.0.2 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-5.0.1 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-5.0.0 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.28 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.27 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.26 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.25 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.24 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.23 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.22 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.21 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.20 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.19 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.18 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.17 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.16 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.15 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js
promethee-4.1.14 node_modules/jquery-warn-before-unload/jquery.warnBeforeUnload.js