2021-03-19 13:21:18 +01:00
|
|
|
import {renderGrid} from "@giphy/js-components";
|
|
|
|
import {GiphyFetch} from "@giphy/js-fetch-api";
|
|
|
|
import $ from "jquery";
|
|
|
|
import _ from "lodash";
|
|
|
|
|
|
|
|
import render_giphy_picker from "../templates/giphy_picker.hbs";
|
|
|
|
import render_giphy_picker_mobile from "../templates/giphy_picker_mobile.hbs";
|
|
|
|
|
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-04-11 07:41:57 +02:00
|
|
|
import {media_breakpoints_num} from "./css_variables";
|
2021-03-19 13:21:18 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-03-31 07:48:11 +02:00
|
|
|
import * as popovers from "./popovers";
|
2021-04-20 23:19:08 +02:00
|
|
|
import * as rows from "./rows";
|
2021-03-31 05:43:13 +02:00
|
|
|
import * as ui_util from "./ui_util";
|
2021-03-19 13:21:18 +01:00
|
|
|
|
|
|
|
const giphy_fetch = new GiphyFetch(page_params.giphy_api_key);
|
|
|
|
let search_term = "";
|
2021-04-11 14:26:53 +02:00
|
|
|
let gifs_grid;
|
2021-03-31 07:48:11 +02:00
|
|
|
let active_popover_element;
|
|
|
|
|
|
|
|
// Only used if popover called from edit message, otherwise it is `undefined`.
|
|
|
|
let edit_message_id;
|
|
|
|
|
|
|
|
export function is_popped_from_edit_messsage() {
|
|
|
|
return active_popover_element && edit_message_id !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function focus_current_edit_message() {
|
2021-04-20 19:50:01 +02:00
|
|
|
$(`#edit_form_${CSS.escape(edit_message_id)} .message_edit_content`).trigger("focus");
|
2021-03-31 07:48:11 +02:00
|
|
|
}
|
|
|
|
|
2021-04-25 22:54:23 +02:00
|
|
|
// Approximate width and height of
|
2021-03-31 07:48:11 +02:00
|
|
|
// giphy popover as computed by chrome
|
|
|
|
// + 25px;
|
|
|
|
const APPROX_HEIGHT = 350;
|
|
|
|
const APPROX_WIDTH = 300;
|
2021-03-19 13:21:18 +01:00
|
|
|
|
|
|
|
function fetchGifs(offset) {
|
|
|
|
const config = {
|
|
|
|
offset,
|
|
|
|
limit: 25,
|
|
|
|
// Default rating to 'g' until we can make this configurable.
|
|
|
|
rating: "g",
|
|
|
|
// We don't pass random_id here, for privacy reasons.
|
|
|
|
};
|
|
|
|
if (search_term === "") {
|
|
|
|
// Get the trending gifs by default.
|
|
|
|
return giphy_fetch.trending(config);
|
|
|
|
}
|
|
|
|
return giphy_fetch.search(search_term, config);
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:43:13 +02:00
|
|
|
function renderGIPHYGrid(targetEl) {
|
2021-03-19 13:21:18 +01:00
|
|
|
const render = () =>
|
|
|
|
// See https://github.com/Giphy/giphy-js/blob/master/packages/components/README.md#grid
|
|
|
|
// for detailed documentation.
|
|
|
|
renderGrid(
|
|
|
|
{
|
|
|
|
width: 300,
|
|
|
|
fetchGifs,
|
|
|
|
columns: 3,
|
|
|
|
gutter: 6,
|
|
|
|
noLink: true,
|
|
|
|
// Hide the creator attribution that appears over a
|
|
|
|
// GIF; nice in principle but too distracting.
|
|
|
|
hideAttribution: true,
|
|
|
|
onGifClick: (props) => {
|
2021-03-31 07:48:11 +02:00
|
|
|
let textarea = $("#compose-textarea");
|
|
|
|
if (edit_message_id !== undefined) {
|
2021-04-20 19:50:01 +02:00
|
|
|
textarea = $(
|
|
|
|
`#edit_form_${CSS.escape(edit_message_id)} .message_edit_content`,
|
|
|
|
);
|
2021-03-31 07:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
compose_ui.insert_syntax_and_focus(
|
|
|
|
`[](${props.images.downsized_medium.url})`,
|
|
|
|
textarea,
|
|
|
|
);
|
|
|
|
hide_giphy_popover();
|
2021-03-19 13:21:18 +01:00
|
|
|
},
|
|
|
|
onGifVisible: (gif, e) => {
|
|
|
|
// Set tabindex for all the GIFs that
|
|
|
|
// are visible to the user. This allows
|
|
|
|
// user to navigate the GIFs using tab.
|
|
|
|
// TODO: Remove this after https://github.com/Giphy/giphy-js/issues/174
|
|
|
|
// is closed.
|
|
|
|
e.target.tabIndex = 0;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
targetEl,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Limit the rate at which we do queries to the GIPHY API to
|
|
|
|
// one per 300ms, in line with animation timing, basically to avoid
|
|
|
|
// content appearing while the user is typing.
|
|
|
|
const resizeRender = _.throttle(render, 300);
|
|
|
|
window.addEventListener("resize", resizeRender, false);
|
|
|
|
const remove = render();
|
|
|
|
return {
|
|
|
|
remove: () => {
|
|
|
|
remove();
|
|
|
|
window.removeEventListener("resize", resizeRender, false);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:43:13 +02:00
|
|
|
function update_grid_with_search_term() {
|
2021-04-11 14:26:53 +02:00
|
|
|
if (!gifs_grid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-19 13:21:18 +01:00
|
|
|
const search_elem = $("#giphy-search-query");
|
|
|
|
// GIPHY popover may have been hidden by the
|
|
|
|
// time this function is called.
|
|
|
|
if (search_elem.length) {
|
|
|
|
search_term = search_elem[0].value;
|
2021-04-11 14:26:53 +02:00
|
|
|
gifs_grid.remove();
|
2021-04-11 15:46:56 +02:00
|
|
|
gifs_grid = renderGIPHYGrid($("#giphy_grid_in_popover .giphy-content")[0]);
|
2021-04-11 14:26:53 +02:00
|
|
|
return;
|
2021-03-19 13:21:18 +01:00
|
|
|
}
|
2021-04-11 14:26:53 +02:00
|
|
|
|
|
|
|
// Set to undefined to stop searching.
|
|
|
|
gifs_grid = undefined;
|
2021-03-19 13:21:18 +01:00
|
|
|
}
|
2021-03-31 05:43:13 +02:00
|
|
|
|
2021-03-31 07:48:11 +02:00
|
|
|
export function hide_giphy_popover() {
|
|
|
|
// Returns `true` if the popover was open.
|
|
|
|
if (active_popover_element) {
|
2021-04-11 15:46:56 +02:00
|
|
|
// We need to destroy the popover because when
|
|
|
|
// we hide it, bootstrap popover
|
|
|
|
// library removes `giphy-content` element
|
2021-04-25 22:54:23 +02:00
|
|
|
// as part of cleaning up everything inside
|
2021-04-11 15:46:56 +02:00
|
|
|
// `popover-content`, so we need to reinitialize
|
|
|
|
// the popover by destroying it.
|
|
|
|
active_popover_element.popover("destroy");
|
2021-03-31 07:48:11 +02:00
|
|
|
active_popover_element = undefined;
|
|
|
|
edit_message_id = undefined;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_popover_content() {
|
2021-04-11 07:41:57 +02:00
|
|
|
if (window.innerWidth <= media_breakpoints_num.md) {
|
2021-03-31 07:48:11 +02:00
|
|
|
// Show as modal in the center for small screens.
|
|
|
|
return render_giphy_picker_mobile();
|
|
|
|
}
|
|
|
|
return render_giphy_picker();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_popover_placement() {
|
|
|
|
let placement = popovers.compute_placement(
|
|
|
|
active_popover_element,
|
|
|
|
APPROX_HEIGHT,
|
|
|
|
APPROX_WIDTH,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (placement === "viewport_center") {
|
|
|
|
// For legacy reasons `compute_placement` actually can
|
|
|
|
// return `viewport_center` which used to place popover in
|
|
|
|
// the center of the screen, but bootstrap doesn't actually
|
|
|
|
// support that and we already handle it on small screen sizes
|
|
|
|
// by placing it in center using `popover-flex`.
|
|
|
|
placement = "left";
|
|
|
|
}
|
|
|
|
|
|
|
|
return placement;
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:43:13 +02:00
|
|
|
export function initialize() {
|
|
|
|
$("body").on("keydown", ".giphy-gif", ui_util.convert_enter_to_click);
|
2021-04-06 13:34:33 +02:00
|
|
|
$("body").on("keydown", ".compose_giphy_logo", ui_util.convert_enter_to_click);
|
2021-03-31 05:43:13 +02:00
|
|
|
|
2021-04-11 14:26:53 +02:00
|
|
|
$("body").on("click", "#giphy_search_clear", (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
$("#giphy-search-query").val("");
|
|
|
|
update_grid_with_search_term();
|
|
|
|
});
|
|
|
|
|
2021-04-06 13:34:33 +02:00
|
|
|
$("body").on("click", ".compose_giphy_logo", (e) => {
|
2021-03-31 05:43:13 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-03-31 07:48:11 +02:00
|
|
|
if (active_popover_element && $.contains(active_popover_element.get()[0], e.target)) {
|
|
|
|
// Hide giphy popover if already active.
|
|
|
|
hide_giphy_popover();
|
2021-03-31 05:43:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-03-31 07:48:11 +02:00
|
|
|
popovers.hide_all();
|
|
|
|
const $elt = $(e.target);
|
2021-04-20 23:19:08 +02:00
|
|
|
if ($elt.parents(".message_edit_form").length === 1) {
|
|
|
|
// Store message id in global variable edit_message_id so that
|
|
|
|
// its value can be further used to correctly find the message textarea element.
|
|
|
|
edit_message_id = rows.id($elt.parents(".message_row"));
|
|
|
|
} else {
|
|
|
|
edit_message_id = undefined;
|
|
|
|
}
|
2021-03-31 07:48:11 +02:00
|
|
|
|
|
|
|
active_popover_element = $elt.closest("#compose_box_giphy_grid");
|
|
|
|
active_popover_element.popover({
|
|
|
|
animation: true,
|
|
|
|
placement: get_popover_placement(),
|
|
|
|
html: true,
|
|
|
|
trigger: "manual",
|
|
|
|
template: get_popover_content(),
|
|
|
|
});
|
|
|
|
|
|
|
|
active_popover_element.popover("show");
|
2021-04-11 15:46:56 +02:00
|
|
|
// Allow simplebar to render, then fetch and
|
|
|
|
// render GIFs. otherwise simplebar replaces
|
|
|
|
// the `.gipihy-content` element when trying
|
2021-04-25 22:54:23 +02:00
|
|
|
// to wrap it and hence, our jquery reference
|
2021-04-11 15:46:56 +02:00
|
|
|
// to `.giphy-content` is lost. Thus, no GIF
|
|
|
|
// is rendered.
|
|
|
|
setTimeout(() => {
|
|
|
|
gifs_grid = renderGIPHYGrid($("#giphy_grid_in_popover .giphy-content")[0]);
|
|
|
|
}, 0);
|
2021-03-31 05:43:13 +02:00
|
|
|
|
|
|
|
$("body").on(
|
|
|
|
"keyup",
|
|
|
|
"#giphy-search-query",
|
|
|
|
// Use debounce to create a 300ms interval between
|
|
|
|
// every search. This makes the UX of searching pleasant
|
|
|
|
// by allowing user to finish typing before search
|
|
|
|
// is executed.
|
2021-04-11 14:26:53 +02:00
|
|
|
_.debounce(update_grid_with_search_term, 300),
|
2021-03-31 05:43:13 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$(document).one("compose_canceled.zulip compose_finished.zulip", () => {
|
2021-03-31 07:48:11 +02:00
|
|
|
hide_giphy_popover();
|
2021-03-31 05:43:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Focus on search box by default.
|
|
|
|
// This is specially helpful for users
|
2021-04-25 22:54:23 +02:00
|
|
|
// navigating via keyboard.
|
2021-03-31 05:43:13 +02:00
|
|
|
$("#giphy-search-query").trigger("focus");
|
|
|
|
});
|
|
|
|
}
|