File size: 6,701 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<template>
    <li v-if="isTopLevel" class="nav-item">
        <a class="nav-link" @click="runFunction(menuData)">
            {{ menuData.text }}
        </a>
    </li>
    <li v-else>
        <Tooltip :tip="introductionTxt(menuData.pluginId)" placement="right">
            <a
                :class="'dropdown-item pt-0' + (disabled ? ' disabled' : '')"
                style="padding-bottom: 2px; pointer-events: all"
                @click="runFunction(menuData)"
                :id="'menu-plugin-' + idSlug"
            >
                <span v-html='menuData.text?.replace("_", "")'></span>
                <div v-if="showHotkey" style="float: right" class="text-muted">
                    {{ hotkeyPrefix }}{{ hotkeyToShow(menuData) }}
                </div>
            </a>
        </Tooltip>
    </li>
</template>

<script lang="ts">
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { Options, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import { IMenuItem } from "./Menu";
import { dynamicImports } from "@/Core/DynamicImports";
import Tooltip from "@/UI/MessageAlerts/Tooltip.vue";
import { loadedPlugins } from "@/Plugins/LoadedPlugins";
import { slugify } from "@/Core/Utils/StringUtils";

let collapseHamburger: any;
let hamburgerMenu: HTMLElement;

/**
 * MenuActionLink component
 */
@Options({
    components: { Tooltip },
})
export default class MenuActionLink extends Vue {
    @Prop() menuData!: IMenuItem;
    @Prop({ default: false }) isTopLevel!: boolean;

    hotkeyPrefix = "Ctrl+";
    // disabled = false;

    /**
     * Gets the introduction text for the plugin.
     *
     * @param {string | undefined}  pluginId The plugin ID.
     * @returns {string} The introduction text for the plugin.
     */
    introductionTxt(pluginId: string | undefined): string {
        if (pluginId === undefined) {
            return "";
        }

        if (!loadedPlugins[pluginId].intro) {
            return "";
        }

        let txt = loadedPlugins[pluginId].intro as string;

        // Get text, but not any HTML tags.
        txt = txt.replace(/<[^>]*>?/g, "");

        return txt;
    }

    /**
     * Whether the menu item is disabled.
     *
     * @returns {boolean}  Whether the menu item is disabled.
     */
    get disabled(): boolean {
        const checkPluginAllowed = this.menuData.checkPluginAllowed;
        if (checkPluginAllowed) {
            const pluginAllowed = checkPluginAllowed(
                this.$store.state.molecules
            );
            return pluginAllowed !== null;
        }
        return false;
    }

    /**
     * Gets the hot key to show in the menu.
     *
     * @param {IMenuItem} menuData  The menu data.
     * @returns {string}  The hot key to show.
     */
    hotkeyToShow(menuData: IMenuItem): string {
        if (!menuData.hotkey) {
            return "";
        }

        if (menuData.hotkey === "up") {
            return "↑";
        }

        if (menuData.hotkey === "down") {
            return "↓";
        }

        return menuData.hotkey.toUpperCase();
    }

    /**
     * Whether to show the hot key in the menu.
     *
     * @returns {boolean}  Whether to show the hot-key text.
     */
    get showHotkey(): boolean {
        const hotkey = this.menuData.hotkey;
        if (!hotkey) {
            return false;
        }
        if (hotkey === "") {
            return false;
        }

        if (hotkey == "up" || hotkey == "down") {
            return true;
        }

        // Conveniently, hotkey length covers case where hotkey is a long string
        // ("backspace") or an array (multiple hotkeys). In either case, don't
        // want to show it in the menu.
        return hotkey.length === 1;
    }

    /**
     * Gets a slug for the menu text.
     *
     * @returns {string}  The slug.
     */
    get idSlug(): string {
        return `${this.menuData.pluginId}-${slugify(this.menuData.text as string)}`;
    }

    /**
     * Hide all toggles. This is good for regular menu (not hamburger, bigger
     * screens).
     */
    private closeRegularMenu() {
        const dropdownElementList = document.querySelectorAll(
            ".top-level-menu-item"
        );

        dynamicImports.bootstrapDropdown.module
            .then((Dropdown: any) => {
                dropdownElementList.forEach((dropdownToggleEl) =>
                    new Dropdown(dropdownToggleEl).hide()
                );
                return;
            })
            .catch((err) => {
                throw err;
            });
    }

    /**
     * Close the menu. Effective if using hamburger menu (smaller screens).
     */
    private closeHamburgerMenu() {
        if (!hamburgerMenu) {
            hamburgerMenu = document.getElementById(
                "hamburger-button"
            ) as HTMLElement;
        }

        if (hamburgerMenu.offsetWidth > 0 && hamburgerMenu.offsetHeight > 0) {
            dynamicImports.bootstrapCollapse.module
                .then((Collapse: any) => {
                    // Hamburger menu is visible
                    if (!collapseHamburger) {
                        collapseHamburger = new Collapse(
                            document.getElementById(
                                "navbarSupportedContent"
                            ) as HTMLElement
                        );
                    }

                    collapseHamburger.toggle();
                    return;
                })
                .catch((err) => {
                    throw err;
                });
        }
    }

    /**
     * Run the function of the menu item.
     *
     * @param {IMenuItem} item  The menu item.
     */
    runFunction(item: IMenuItem) {
        if (item.function) {
            this.closeRegularMenu();
            this.closeHamburgerMenu();

            // Run the function
            item.function();
        }
    }

    /**
     * Mounted function.
     */
    mounted() {
        // Get the os
        dynamicImports.detectOs.module
            .then((OSDetector) => {
                const os = new OSDetector().detect().os;
                switch (os) {
                    case "macos":
                    case "ios":
                        this.hotkeyPrefix = "⌘ ";
                        break;
                    default:
                        this.hotkeyPrefix = "Ctrl+";
                        break;
                }
                return;
            })
            .catch((err) => {
                throw err;
            });
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss"></style>