简体   繁体   English

如何覆盖event.stopPropagation(),preventDefault()。stopImmediatePropagation()

[英]How to override event.stopPropagation(),preventDefault().stopImmediatePropagation()

I am trying to modify Jira Tempo plugin's plan work form. 我正在尝试修改Jira Tempo插件的计划工作表单。 So in the tempo time sheet page I have a button who has 2 actions bind-ed, one is a click action and one is a focus out. 因此,在速度时间表页面中,我有一个按钮,该按钮绑定了2个动作,一个是单击动作,一个是焦点移出。 the click action opens a popup. 单击动作将打开一个弹出窗口。 and the focus out verifies the form in the popup. 并且焦点移出将验证弹出窗口中的表单。 I need the clik action to be executed first and then the focus out. 我需要先执行clik动作,然后再进行聚焦。

The problem is when the popup opens there is a call to all of the three functions. 问题是,当弹出窗口打开时,将调用所有三个功能。 event.stopPropagation().preventDefault().stopImmediatePropagation(); so the focus out does not fire in chrome. 因此对焦不会在Chrome中启动。

In IE10 and Firefox works great. 在IE10和Firefox中效果很好。

Can i override jQuery basic function of event.stopPropagation().preventDefault().stopImmediatePropagation() to do not apply for that button? 我可以覆盖event.stopPropagation().preventDefault().stopImmediatePropagation() jQuery基本功能以不申请该按钮吗? all three, maybe? 全部三个,也许吗?

I tried this: 我尝试了这个:

(function () {
jQuery.Event.prototype = {
    isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse,
    stopPropagation: function () {
        if (jQuery('#tempo-plan-button').hasClass(this.currentTarget.className)) {

        } else {
            var e = this.originalEvent;

            this.isPropagationStopped = returnTrue;

            if (e && e.stopPropagation) {
                e.stopPropagation();
            }
        }
    },
    preventDefault: function () {
        if (jQuery('#tempo-plan-button').hasClass(this.currentTarget.className)) {

        } else {
            var e = this.originalEvent;

            this.isDefaultPrevented = returnTrue;

            if (e && e.preventDefault) {
                e.preventDefault();
            }
        }
    },
    stopImmediatePropagation: function () {
        if (jQuery('#tempo-plan-button').hasClass(this.currentTarget.className)) {

        } else {
            var e = this.originalEvent;

            this.isImmediatePropagationStopped = returnTrue;

            if (e && e.stopImmediatePropagation) {
                e.stopImmediatePropagation();
            }

            this.stopPropagation();
        }
    }

};
function returnFalse() {
    return false;
}
function returnTrue() {
    return true;
}
})();

UPDATE: 更新:

I don't have control on what the click function does, but i have control on the focus out, I have made some test and i looks like Chrome does not see the focus out event, because the element is hidden before the focus out occurs ( it is a popup button). 我无法控制点击功能的作用,但可以控制聚焦,我做了一些测试,但我似乎Chrome没有看到聚焦事件,因为该元素在聚焦发生之前就被隐藏了(这是一个弹出按钮)。 I managed to call my functions with mouse out event but this does not work on IE so I have to bind separate event listener for IE and Chrome,Firefox,Safari. 我设法用鼠标移出事件来调用函数,但这在IE上不起作用,因此我必须为IE和Chrome,Firefox,Safari绑定单独的事件侦听器。 Can you help with some browser detection examples. 您能否提供一些浏览器检测示例的帮助。

UPDATE 2: 更新2:

My problem solved with a different listener to the popup element. 我的问题通过对popup元素使用不同的侦听器解决。 ex: 例如:

jQuery(document).ready(function ($) {
var currentUser = null;
var selected = null;
var opened = null;
var formOpened = null;
var activityField = null;
var activitySelected = null;
var popupBody = null;
var formOpened = null;
var periodCheckbox = null;
var dateField = null;
var endDateField = null;
var plannedHours = null;
var periodselected = null;
var days = 1;
var startdate = 0;
var enddate = 0;
var planButton = $('#tempo-plan-button');
//this is the default LM-Holiday activity ID set this
value to corespond with the value on jira tempo form.
var holidayid = "10100";
var holidaysLeft = 21; /*
 $('#tempo-plan-button').click(function () {
 alert("click event");
 });
 $('#tempo-plan-button').focusin(function () {
 alert("focus event");
 });
 $('#tempo-plan-button').hover(function () {
 alert("hover event");

 });
 $('#tempo-plan-button').mouseleave(function () {
 alert("mouse leave event");

 });
 $('#tempo-plan-button').mouseout(function () {
 alert("mouse out event");

 }); */
$('body').one('focus', 'div[class="aui-popup aui-dialog"]', function (event) {
    $('div[class="aui-popup aui-dialog"]').each(function () {
        popupBody = $(this).find(".dialog-components");
        if ($(this).find(".dialog-title").hasClass("tempo-add-button") === false) {
            i = 0;
            j = 0;
            $(popupBody).find(".dialog-page-menu li").each(function () {
                if ($(this).attr("class") === "page-menu-item selected") {
                    button = $(this).find('.item-button');
                    if ((button).text() === "Internal") {
                        selected = i;
                    }
                }
                i++;
            });
            $(popupBody).find(".dialog-panel-body").each(function () {
                if ($(this).is(":visible")) {
                    opened = j;
                    formOpened = $(this).find('form');
                }
                j++;
            });
            if (selected === null) {
                i = 0;
                j = 0;
                $(popupBody).find(".dialog-page-menu li").click(function () {
                    $(popupBody).find(".dialog-page-menu li").each(function () {
                        if ($(this).attr("class") === "page-menu-item selected") {
                            button = $(this).find('.item-button');
                            if ((button).text() === "Internal") {
                                selected = i;
                            }
                        }
                        i++;
                    });
                    $(popupBody).find(".dialog-panel-body").each(function () {
                        if ($(this).is(":visible")) {
                            opened = j;
                            formOpened = $(this).find('form');
                        }
                        j++;
                    });
                    if (selected === opened) {
                        activityField = $(formOpened).find('.tempo-activity-picker.select');
                        periodCheckbox = $(formOpened).find('.showperiod.tempo-show-period');
                        dateField = $(formOpened).find(' input[name="date"]');
                        endDateField = $(formOpened).find('input[name="enddate"]');
                        plannedHours = $(formOpened).find('input[name="time"]');
                        days = 1;
                        $(activityField).change(function () {
                            activitySelected = $(this).val();
                        });

                        $(periodCheckbox).change(function () {
                            if ($(this).prop("checked")) {
                                periodselected = true;
                            }
                            else
                            {
                                periodselected = false;

                            }
                        });
                        $(dateField).change(function () {
                            if (periodselected) {
                                startdate = parseDate($(this).val());
                                enddate = parseDate($(endDateField).val());
                            } else {
                                days = 1;
                            }
                            ;
                        });
                        $(endDateField).change(function () {
                            startdatestring = $(dateField).val();
                            enddatestring = $(this).val();
                            startdate = parseDate(startdatestring);
                            enddate = parseDate(enddatestring);
                        });
                        $(plannedHours).off("focusin");
                        $(plannedHours).focusin(function () {
                            if (activitySelected === holidayid) {
                                if (periodselected) {
                                    days = calcBusinessDays(startdate, enddate);

                                    if (holidaysLeft >= days) {
                                        holidaysLeft = holidaysLeft - days;
                       alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
                      $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                    }
                                    else {
         $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
         alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                    }
                                } else
                                {
                                    if (holidaysLeft >= days) {
                                        holidaysLeft = holidaysLeft - days;
                                 alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
                        $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                    }
                                    else {
                 $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
               alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                    }
                                }
                            }
                        });
                    }
                });
            } else {
                j = 0;
                $(popupBody).find(".dialog-panel-body").each(function () {
                    if ($(this).is(":visible")) {
                        opened = j;
                        formOpened = $(this).find('form');
                    }
                    j++;
                });
                if (selected === opened) {
                    activityField = $(formOpened).find('.tempo-activity-picker.select');
                    periodCheckbox = $(formOpened).find('.showperiod.tempo-show-period');
                    dateField = $(formOpened).find(' input[name="date"]');
                    endDateField = $(formOpened).find('input[name="enddate"]');
                    plannedHours = $(formOpened).find('input[name="time"]');
                    days = 1;
                    $(activityField).change(function () {
                        activitySelected = $(this).val();
                    });

                    $(periodCheckbox).change(function () {
                        if ($(this).prop("checked")) {
                            periodselected = true;
                        }
                        else
                        {
                            periodselected = false;

                        }
                    });
                    $(dateField).change(function () {
                        if (periodselected) {
                            startdate = parseDate($(this).val());
                            enddate = parseDate($(endDateField).val());
                        } else {
                            days = 1;
                        }
                        ;
                    });
                    $(endDateField).change(function () {
                        startdatestring = $(dateField).val();
                        enddatestring = $(this).val();
                        startdate = parseDate(startdatestring);
                        enddate = parseDate(enddatestring);
                    });
                    $(plannedHours).off("focusin");
                    $(plannedHours).focusin(function () {
                        if (activitySelected === holidayid) {
                            if (periodselected) {
                                days = calcBusinessDays(startdate, enddate);

                                if (holidaysLeft >= days) {
                                    holidaysLeft = holidaysLeft - days;
               alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
               $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                }
                                else {
                       $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
                alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                }
                            } else
                            {
                                if (holidaysLeft >= days) {
                                    holidaysLeft = holidaysLeft - days;
                              alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
                         $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                }
                                else {
                    $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
          alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                }
                            }
                        }
                    });
                }
            }
            ;
        }
        ;
        return false;
    });

    $.ajax({
        type: "GET",
        url: location.protocol + '//' + location.host + "/rest/api/2/myself",
        success: function (response) {
            currentUser = $.parseJSON(response);
        },
        error: function (response) {
            alert("Eroare" + response.result);
        }
    });
    return false;
});
});
 function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1)
    return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 === 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 === 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5))
    iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000);

if (iWeekday1 <= iWeekday2) {
    iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1);
} else {
    iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2);
}

iDateDiff -= iAdjust // take into account both days on weekend

return (iDateDiff + 1); // add 1 because dates are inclusive
}
function parseDate(s) {
var lastSlash = s.lastIndexOf("/");
var years = s.substring(lastSlash + 1);
years = "20" + years;
s = s.replaceAt(lastSlash + 1, years);
var pattern = /(.*?)\/(.*?)\/(.*?)$/;
var result = s.replace(pattern, function (match, p1, p2, p3) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    return (months.indexOf(p2) + 1) + "/" + (p1) + "/" + p3;
});
return new Date(result);
}
String.prototype.replaceAt = function (index, character) {
 return this.substr(0, index) + character + this.substr(index + character.length);
};
function propStopped(event) {
var msg = "";
if (event.isPropagationStopped()) {
    msg = "called";
} else {
    msg = "not called";
}
alert(msg);
}

Thanks. 谢谢。

Did you mean this? 你是这个意思吗

 Event.prototype.stopPropagation = function(){ alert('123') } $('input').on('click',function(e){ e.stopPropagation(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='button' value='click' /> 

From the question, it is unclear as to which code you control. 从这个问题来看,不清楚要控制哪个代码。 I am reading between the lines that perhaps you control the function bound to the click event for the button, but you do not control the function bound to the focusout event, which is why you need to work around those calls. 我在两行之间读到的内容可能是您控制了与按钮的click事件绑定的功能,但没有控制与focusout事件绑定的功能,这就是为什么您需要解决这些调用的原因。

If you want to control of execution of the events and make sure that yours happen first, it's possible to do this with something like jQuery.bindUp , just so long as the events are of the same type. 如果您想控制事件的执行并确保您的事件首先发生,则可以使用jQuery.bindUp之类的东西来做到这一点,只要事件的类型相同即可。 In that way, you don't care if the other handler tries to call stopPropagation etc. because your handler will get to execute first anyway. 这样,您不必在乎其他处理程序是否尝试调用stopPropagation等,因为您的处理程序无论如何都会首先执行。

Given that, is it possible to restructure your code so that the logic you control (the part which needs to happen first!) is of the same event type as the existing event handler, and then use jQuery.bindUp to ensure that it gets executed before the event handler that you do not control? 鉴于此,是否可以重组代码,使您控制的逻辑(首先需要发生的部分!)具有与现有事件处理程序相同的事件类型,然后使用jQuery.bindUp确保执行该逻辑。您无法控制的事件处理程序之前? You can still bind to the click event if you need it, just so long as the dependent business logic is moved to the focusout event. 只要需要,您仍可以绑定到click事件,只要将相关业务逻辑移至focusout事件即可。

(If my assumptions are not correct, it would be helpful if you could update the question to describe the problem constraints in more detail.) (如果我的假设不正确,则可以更新问题以更详细地描述问题约束,这将很有帮助。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM