All files / web/bundles/pimui/js pim-formupdatelistener.js

61.9% Statements 26/42
45.45% Branches 5/11
55.56% Functions 5/9
61.9% Lines 26/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76  24x         24x 39x 39x 39x         39x 39x   39x 11x 11x           11x 11x   11x 11x   11x 11x     11x             39x                               39x 39x 39x         39x 39x   39x 30x 30x          
/* global console */
define(
    ['jquery', 'backbone', 'pim/dialog', 'pim/router'],
    function ($, Backbone, Dialog, router) {
        'use strict';
 
        return function ($form) {
            this.updated = false;
            var message = $form.attr('data-updated-message');
            Iif (!message) {
                console.warn('FormUpdateListener: message not provided.');
 
                return;
            }
            var title = $form.attr('data-updated-title');
            var self  = this;
 
            var formUpdated = function (e) {
                var $target = $(e.target);
                Iif ($target.parents('div.filter-box').length ||
                    $target.parents('ul.icons-holder').length ||
                    $target.hasClass('exclude')) {
 
                    return;
                }
                self.updated = true;
                $('#entity-updated').show().css('opacity', 1);
 
                $form.off('change', formUpdated);
                $(document).off('click', '#' + $form.attr('id') + ' ins.jstree-checkbox', formUpdated);
 
                $form.find('button[type="submit"]').on('click', function () {
                    self.updated = false;
                });
 
                $(window).on('beforeunload', function () {
                    if (self.updated) {
                        return message;
                    }
                });
            };
 
            var linkClicked = function (e) {
                e.stopImmediatePropagation();
                e.preventDefault();
                var url      = $(this).attr('href');
                var doAction = function () {
                    router.redirect(url);
                };
                if (!self.updated) {
                    doAction();
                } else {
                    Dialog.confirm(message, title, doAction);
                }
 
                return false;
            };
 
            $form.on('change', formUpdated);
            $(document).on('click', '#' + $form.attr('id') + ' ins.jstree-checkbox', formUpdated);
            $form.on('refresh', function () {
                self.updated = false;
                $('#entity-updated').css('opacity', 0).hide();
            });
 
            $('a[href^="/"]:not(".no-hash")').off('click').on('click', linkClicked);
            $form.on('click', 'a[href^="/"]:not(".no-hash")', linkClicked);
 
            Backbone.Router.prototype.on('route', function () {
                $('a[href^="/"]:not(".no-hash")').off('click', linkClicked);
                $(window).off('beforeunload');
            });
        };
    }
);