2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-10 16:47:06 +01:00
|
|
|
import render_feedback_container from "../templates/feedback_container.hbs";
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
|
|
|
|
2018-12-21 15:58:28 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
This code lets you show something like this:
|
|
|
|
|
|
|
|
+-----
|
|
|
|
| TOPIC MUTES [undo] [x]
|
|
|
|
|
|
|
|
|
| You muted stream Foo, topic Bar.
|
|
|
|
+-----
|
|
|
|
|
|
|
|
And then you configure the undo behavior, and
|
|
|
|
everything else is controlled by the widget.
|
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
Code-wise it's a singleton widget that controls the DOM inside
|
2018-12-21 15:58:28 +01:00
|
|
|
#feedback_container, which gets served up by server.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2018-12-21 15:58:28 +01:00
|
|
|
hide_me_time: null,
|
|
|
|
alert_hover_state: false,
|
|
|
|
$container: null,
|
|
|
|
opened: false,
|
|
|
|
};
|
|
|
|
|
2019-10-26 00:26:37 +02:00
|
|
|
const animate = {
|
2020-07-20 22:18:43 +02:00
|
|
|
maybe_close() {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (!meta.opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-22 11:54:49 +01:00
|
|
|
if (meta.hide_me_time < Date.now() && !meta.alert_hover_state) {
|
2018-12-21 15:58:28 +01:00
|
|
|
animate.fadeOut();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(animate.maybe_close, 100);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
fadeOut() {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (!meta.opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meta.$container) {
|
|
|
|
meta.$container.fadeOut(500).removeClass("show");
|
|
|
|
meta.opened = false;
|
|
|
|
meta.alert_hover_state = false;
|
|
|
|
}
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
fadeIn() {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (meta.opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meta.$container) {
|
|
|
|
meta.$container.fadeIn(500).addClass("show");
|
|
|
|
meta.opened = true;
|
|
|
|
setTimeout(animate.maybe_close, 100);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function set_up_handlers() {
|
|
|
|
if (meta.handlers_set) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.handlers_set = true;
|
|
|
|
|
|
|
|
// if the user mouses over the notification, don't hide it.
|
2020-07-20 21:26:58 +02:00
|
|
|
meta.$container.on("mouseenter", () => {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (!meta.opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.alert_hover_state = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// once the user's mouse leaves the notification, restart the countdown.
|
2020-07-20 21:26:58 +02:00
|
|
|
meta.$container.on("mouseleave", () => {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (!meta.opened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.alert_hover_state = false;
|
|
|
|
// add at least 2000ms but if more than that exists just keep the
|
|
|
|
// current amount.
|
2020-12-22 11:54:49 +01:00
|
|
|
meta.hide_me_time = Math.max(meta.hide_me_time, Date.now() + 2000);
|
2018-12-21 15:58:28 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.$container.on("click", ".exit-me", () => {
|
2018-12-21 15:58:28 +01:00
|
|
|
animate.fadeOut();
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.$container.on("click", ".feedback_undo", () => {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (meta.undo) {
|
|
|
|
meta.undo();
|
|
|
|
}
|
|
|
|
animate.fadeOut();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:47:06 +01:00
|
|
|
export function is_open() {
|
2018-12-21 16:18:08 +01:00
|
|
|
return meta.opened;
|
2021-02-10 16:47:06 +01:00
|
|
|
}
|
2018-12-21 16:18:08 +01:00
|
|
|
|
2021-02-10 16:47:06 +01:00
|
|
|
export function dismiss() {
|
2018-12-21 15:58:28 +01:00
|
|
|
animate.fadeOut();
|
2021-02-10 16:47:06 +01:00
|
|
|
}
|
2018-12-21 15:58:28 +01:00
|
|
|
|
2021-02-10 16:47:06 +01:00
|
|
|
export function show(opts) {
|
2018-12-21 15:58:28 +01:00
|
|
|
if (!opts.populate) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("programmer needs to supply populate callback.");
|
2018-12-21 15:58:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.$container = $("#feedback_container");
|
2018-12-21 15:58:28 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_feedback_container();
|
2018-12-21 18:21:37 +01:00
|
|
|
meta.$container.html(html);
|
|
|
|
|
2018-12-21 15:58:28 +01:00
|
|
|
set_up_handlers();
|
|
|
|
|
|
|
|
meta.undo = opts.on_undo;
|
|
|
|
|
|
|
|
// add a four second delay before closing up.
|
2020-12-22 11:54:49 +01:00
|
|
|
meta.hide_me_time = Date.now() + 4000;
|
2018-12-21 15:58:28 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
meta.$container.find(".feedback_title").text(opts.title_text);
|
|
|
|
meta.$container.find(".feedback_undo").text(opts.undo_button_text);
|
|
|
|
opts.populate(meta.$container.find(".feedback_content"));
|
2018-12-21 15:58:28 +01:00
|
|
|
|
|
|
|
animate.fadeIn();
|
2021-02-10 16:47:06 +01:00
|
|
|
}
|