Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

11.2 KB
Raw Blame
import {$} from "jquery";
import assert from "minimalistic-assert";
import type * as tippy from "tippy.js";

import render_left_sidebar_folder_popover from "../templates/popovers/left_sidebar/left_sidebar_folder_popover.hbs";
import render_left_sidebar_menu_popover from "../templates/popovers/left_sidebar_menu_popover.hbs";

import * as channel from "./channel.ts";
import * as channel_folders_ui from "./channel_folders_ui.ts";
import * as keydown_util from "./keydown_util.ts";
import * as left_sidebar_navigation_area from "./left_sidebar_navigation_area.ts";
import * as pm_list from "./pm_list.ts";
import * as popover_menus from "./popover_menus.ts";
import * as settings_config from "./settings_config.ts";
import * as settings_data from "./settings_data.ts";
import {current_user} from "./state_data.ts";
import * as stream_list from "./stream_list.ts";
import * as stream_settings_ui from "./stream_settings_ui.ts";
import * as ui_util from "./ui_util.ts";
import {user_settings} from "./user_settings.ts";
import * as util from "./util.ts";

function do_change_show_channel_folders_left_sidebar(instance: tippy.Instance): void {
    const show_channel_folders = user_settings.web_left_sidebar_show_channel_folders;
    const data = {
        web_left_sidebar_show_channel_folders: JSON.stringify(!show_channel_folders),
    };
    void channel.patch({
        url: "/json/settings",
        data,
    });
    popover_menus.hide_current_popover_if_visible(instance);
}

function do_change_web_stream_unreads_count_display_policy(
    instance: tippy.Instance,
    value: number,
): void {
    if (user_settings.web_stream_unreads_count_display_policy === value) {
        popover_menus.hide_current_popover_if_visible(instance);
        return;
    }

    const data = {
        web_stream_unreads_count_display_policy: value,
    };

    void channel.patch({
        url: "/json/settings",
        data,
    });

    popover_menus.hide_current_popover_if_visible(instance);
}

function do_change_web_channel_default_view(instance: tippy.Instance, value: number): void {
    if (user_settings.web_channel_default_view === value) {
        popover_menus.hide_current_popover_if_visible(instance);
        return;
    }

    const data = {
        web_channel_default_view: value,
    };

    void channel.patch({
        url: "/json/settings",
        data,
    });

    popover_menus.hide_current_popover_if_visible(instance);
}

function do_change_show_channel_folders_inbox(instance: tippy.Instance): void {
    const show_channel_folders = user_settings.web_inbox_show_channel_folders;
    const data = {
        web_inbox_show_channel_folders: JSON.stringify(!show_channel_folders),
    };
    void channel.patch({
        url: "/json/settings",
        data,
    });
    popover_menus.hide_current_popover_if_visible(instance);
}

function expand_all_sections(instance: tippy.Instance): void {
    // Expand Views section
    left_sidebar_navigation_area.force_expand_views();

    // Expand Direct Messages section
    if (pm_list.is_private_messages_collapsed()) {
        pm_list.expand();
    }

    // Expand all channel/stream sections
    stream_list.expand_all_stream_sections();

    popover_menus.hide_current_popover_if_visible(instance);
}

function collapse_all_sections(instance: tippy.Instance): void {
    // Collapse Views section
    left_sidebar_navigation_area.force_collapse_views();

    // Collapse Direct Messages section
    if (!pm_list.is_private_messages_collapsed()) {
        pm_list.close();
    }

    // Collapse all channel/stream sections
    stream_list.collapse_all_stream_sections();
    popover_menus.hide_current_popover_if_visible(instance);
}

export function initialize(): void {
    popover_menus.register_popover_menu(
        "#left-sidebar-search .channel-folders-sidebar-menu-icon",
        {
            ...popover_menus.left_sidebar_tippy_options,
            theme: "popover-menu",
            onMount(instance) {
                const $popper = $(instance.popper);
                assert(instance.reference instanceof HTMLElement);
                $popper.one("click", "#left_sidebar_channel_folders", () => {
                    do_change_show_channel_folders_left_sidebar(instance);
                });
                $popper.one("click", "#left_sidebar_expand_all", () => {
                    expand_all_sections(instance);
                });
                $popper.one("click", "#left_sidebar_collapse_all", () => {
                    collapse_all_sections(instance);
                });
                $popper.one("click", ".web_stream_unreads_count_display_policy_selector", (e) => {
                    const value = Number($(e.currentTarget).attr("value"));
                    do_change_web_stream_unreads_count_display_policy(instance, value);
                });

                $popper.one("click", ".web_channel_default_view_selector", (e) => {
                    const value = Number($(e.currentTarget).attr("value"));
                    do_change_web_channel_default_view(instance, value);
                });
                popover_menus.focus_popover(instance);
            },
            onShow(instance) {
                const show_channel_folders = user_settings.web_left_sidebar_show_channel_folders;
                const show_collapse_expand_all_options = true;
                const show_channel_display_options = true;
                // Assuming that the instance can be shown, track and
                // prep the instance for showing
                popover_menus.popover_instances.show_folders_sidebar = instance;
                instance.setContent(
                    ui_util.parse_html(
                        render_left_sidebar_menu_popover({
                            show_channel_folders,
                            channel_folders_id: "left_sidebar_channel_folders",
                            show_collapse_expand_all_options,
                            show_channel_display_options,
                            web_stream_unreads_count_display_policy_values:
                                settings_config.web_stream_unreads_count_display_policy_values,
                            web_stream_unreads_count_display_policy:
                                user_settings.web_stream_unreads_count_display_policy,
                            web_channel_default_view_values:
                                settings_config.web_channel_default_view_values,
                            web_channel_default_view: user_settings.web_channel_default_view,
                        }),
                    ),
                );
                popover_menus.on_show_prep(instance);

                return undefined;
            },
            onHidden(instance) {
                instance.destroy();
                popover_menus.popover_instances.show_folders_sidebar = null;
            },
        },
        {
            also_trigger_on_enter: true,
        },
    );

    popover_menus.register_popover_menu("#inbox-view .channel-folders-inbox-menu-icon", {
        ...popover_menus.left_sidebar_tippy_options,
        theme: "popover-menu",
        onMount(instance) {
            const $popper = $(instance.popper);
            assert(instance.reference instanceof HTMLElement);
            $popper.one("click", "#inbox_channel_folders", () => {
                do_change_show_channel_folders_inbox(instance);
            });
        },
        onShow(instance) {
            const show_channel_folders = user_settings.web_inbox_show_channel_folders;
            const show_collapse_expand_all_options = false;
            const show_channel_display_options = false;
            // Assuming that the instance can be shown, track and
            // prep the instance for showing
            popover_menus.popover_instances.show_folders_inbox = instance;
            instance.setContent(
                ui_util.parse_html(
                    render_left_sidebar_menu_popover({
                        show_channel_folders,
                        channel_folders_id: "inbox_channel_folders",
                        show_collapse_expand_all_options,
                        show_channel_display_options,
                    }),
                ),
            );
            popover_menus.on_show_prep(instance);

            return undefined;
        },
        onHidden(instance) {
            instance.destroy();
            popover_menus.popover_instances.show_folders_inbox = null;
        },
    });

    function on_folder_sidebar_menu_icon_press(
        element: HTMLElement,
        e: JQuery.ClickEvent | JQuery.KeyDownEvent,
    ): void {
        e.preventDefault();
        e.stopPropagation();

        const folder_id = Number.parseInt(
            $(element).closest(".stream-list-section-container").attr("data-section-id")!,
            10,
        );
        popover_menus.toggle_popover_menu(
            element,
            {
                ...popover_menus.left_sidebar_tippy_options,
                theme: "popover-menu",
                onMount(instance) {
                    popover_menus.popover_instances.folder_actions = instance;
                    const $popper = $(instance.popper);
                    assert(instance.reference instanceof HTMLElement);
                    ui_util.show_left_sidebar_menu_icon(instance.reference);
                    $popper.one("click", "#folder_popover_view_channels", () => {
                        let left_side_tab = "all";
                        if (current_user.is_guest) {
                            left_side_tab = "subscribed";
                        }
                        stream_settings_ui.launch(undefined, left_side_tab, undefined, folder_id);
                    });
                    $popper.one("click", "#folder_popover_manage_folder", () => {
                        channel_folders_ui.handle_editing_channel_folder(folder_id);
                    });
                    popover_menus.focus_popover(instance);
                },
                onShow(instance) {
                    instance.setContent(
                        ui_util.parse_html(
                            render_left_sidebar_folder_popover({
                                can_manage_folder: settings_data.can_user_manage_folder(),
                            }),
                        ),
                    );
                    popover_menus.on_show_prep(instance);

                    return undefined;
                },
                onHidden(instance) {
                    ui_util.hide_left_sidebar_menu_icon();
                    instance.destroy();
                    popover_menus.popover_instances.folder_actions = null;
                },
            },
            {
                get_focus_return_element: (reference) =>
                    util.the($(reference).siblings(".stream-list-section-toggle")),
            },
        );
    }

    $("#streams_list").on(
        "click",
        ".stream-list-section-container .folder-section-sidebar-menu-icon",
        function (this: HTMLElement, e) {
            on_folder_sidebar_menu_icon_press(this, e);
        },
    );

    $("#streams_list").on(
        "keydown",
        ".stream-list-section-container .folder-section-sidebar-menu-icon",
        function (this: HTMLElement, e) {
            if (keydown_util.is_enter_event(e)) {
                on_folder_sidebar_menu_icon_press(this, e);
            }
        },
    );
}