feedback widget: Avoid duplicate handlers.

We only need these once, not during every show()
call.  We actually were only setting up the
click handlers one time, but we had redundant
mouse handlers.

More importantly, we stop a runaway timer
that tries to fade out our feedback widget
every 100ms or ten times per second!
This commit is contained in:
Steve Howell 2018-12-21 00:37:10 +00:00 committed by Tim Abbott
parent 39cdc926a5
commit 4468311620
1 changed files with 69 additions and 31 deletions

View File

@ -30,24 +30,85 @@ exports.build_feedback_widget = function () {
hide_me_time: null,
alert_hover_state: false,
$container: null,
opened: false,
};
var animate = {
maybe_close: function () {
if (!meta.opened) {
return;
}
if (meta.hide_me_time < new Date().getTime() && !meta.alert_hover_state) {
animate.fadeOut();
return;
}
setTimeout(animate.maybe_close, 100);
},
fadeOut: function () {
if (!meta.opened) {
return;
}
if (meta.$container) {
meta.$container.fadeOut(500).removeClass("show");
meta.opened = false;
meta.alert_hover_state = false;
}
},
fadeIn: function () {
if (meta.opened) {
return;
}
if (meta.$container) {
meta.$container.fadeIn(500).addClass("show");
meta.opened = true;
setTimeout(animate.maybe_close, 100);
}
},
};
setInterval(function () {
if (meta.hide_me_time < new Date().getTime() && !meta.alert_hover_state) {
animate.fadeOut();
function set_up_handlers() {
if (meta.handlers_set) {
return;
}
}, 100);
meta.handlers_set = true;
// if the user mouses over the notification, don't hide it.
meta.$container.mouseenter(function () {
if (!meta.opened) {
return;
}
meta.alert_hover_state = true;
});
// once the user's mouse leaves the notification, restart the countdown.
meta.$container.mouseleave(function () {
if (!meta.opened) {
return;
}
meta.alert_hover_state = false;
// add at least 2000ms but if more than that exists just keep the
// current amount.
meta.hide_me_time = Math.max(meta.hide_me_time, new Date().getTime() + 2000);
});
meta.$container.find('.exit-me').click(function () {
animate.fadeOut();
});
meta.$container.find(".feedback_undo").click(function () {
if (meta.undo) {
meta.undo();
}
animate.fadeOut();
});
}
self.dismiss = function () {
animate.fadeOut();
@ -59,23 +120,12 @@ exports.build_feedback_widget = function () {
return;
}
meta.$container = $('#feedback_container');
set_up_handlers();
meta.undo = opts.on_undo;
if (!meta.$container) {
meta.$container = $("#feedback_container");
meta.$container.find('.exit-me').click(function () {
animate.fadeOut();
});
meta.$container.find(".feedback_undo").click(function () {
if (meta.undo) {
meta.undo();
}
animate.fadeOut();
});
}
// add a four second delay before closing up.
meta.hide_me_time = new Date().getTime() + 4000;
@ -85,18 +135,6 @@ exports.build_feedback_widget = function () {
animate.fadeIn();
// if the user mouses over the notification, don't hide it.
meta.$container.mouseenter(function () {
meta.alert_hover_state = true;
});
// once the user's mouse leaves the notification, restart the countdown.
meta.$container.mouseleave(function () {
meta.alert_hover_state = false;
// add at least 2000ms but if more than that exists just keep the
// current amount.
meta.hide_me_time = Math.max(meta.hide_me_time, new Date().getTime() + 2000);
});
};
return self;