Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | 93x 93x 93x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 7x 4x 3x 3x 2x 1x 1x 19x 12x 12x 12x 1x 23x 27x 2x 2x 1x 4x 21x 23x 11x 12x 7x 6x 6x 5x 5x 3x 2x 5x 1x 5x 4x 3x 3x 3x 6x 6x 5x 6x 6x 6x 2x 1x 1x 1x 9x 12x 4x 4x 4x 2x 2x 2x 2x 1x 1x 1x 3x 3x 1x 3x 3x 3x 2x 4x 4x 4x 10x 4x 6x 4x 10x 8x 2x 6x 3x | import { Accessor, Config, I18n, Loader, Metadata, Utils } from '@zeedhi/core';
import { ComponentRender } from '../zd-component/component-render';
import { IComponentRender } from '../zd-component/interfaces';
import { IMenu, IMenuItem } from './interfaces';
import { MenuGroup } from './menu-group';
/**
* Base class for Menu component.
*/
export class Menu extends ComponentRender implements IMenu {
/**
* Defines if the menu is an application menu
*/
public app: boolean | string = false;
/**
* Defines if the menu must be rendered inside an absolute div
*/
public absolute: boolean | string = false;
/**
* Defines if the menu should rests under the application toolbar
*/
public clipped: boolean | string = false;
/**
* Applies position: fixed to the menu
*/
public fixed: boolean | string = false;
/**
* Defines if the menu should have a visible container
*/
public floating: boolean | string = false;
/**
* Defines if the menu should stay visible regardless of screen size
*/
public permanent: boolean | string = false;
/**
* Create the menu with smaller items.
*/
public dense: boolean | string = true;
/**
* Create the menu with condensed width. Only icons and no label.
*/
public mini: boolean | string = false;
/**
* Defines the width of the menu when in mini state
*/
public miniWidth: number | string = 50;
/**
* Defines if the menu is mini
*/
public miniState = false;
/**
* Defines if the menu is temporary. Temporary menu sits over above the application and uses a overlay div.
*/
public temporary: boolean | string = false;
/**
* Array of menu items
*/
private menuItems: IMenuItem[] = [];
/**
* Defines if the close event will turn menu to mini
*/
public closeToMini: boolean | string = true;
/**
* Defines if the search is shown
*/
public showSearch: boolean | string = false;
/**
* Defines if items are local
*/
public isLocal: boolean | string = false;
/**
* Defines if menu is opened
*/
public drawer: boolean;
/**
* Initial opened prop value
*/
public openedInitialValue?: boolean;
/**
* Defines if the mouse is over the menu
*/
public mouseOver = false;
/**
* Defines the menu width in pixels
*/
public width: number | string = 256;
/**
* Defines the mobile breakpoint in pixels
*/
public mobileBreakpoint: number | string = 600;
/**
* Array of components to be rendered on the top part of the menu
*/
public topSlot: IComponentRender[] = [];
/**
* Object to store all the routes
*/
private routes: any = {};
/**
* Selected path
*/
private selectedPath = '';
/**
* Array of selected items
*/
private selectedItems: IMenuItem[] = [];
/**
* Defines the url to get menu items
*/
private menuItemsUrl = '';
private openingMenuTimeout!: NodeJS.Timeout;
/** Control toolip for overflown items */
public showTooltip = false;
/** Toolip text for overflown items */
public tooltipText = '';
/** Toolip anchor */
public tooltipAnchor?: HTMLElement;
/** Toolip content props */
public tooltipContentProps?: any = {};
/** Filtered menu items */
private filteredMenuItems: IMenuItem[] = [];
/** Search value */
private searchValue = '';
/** Store the current item focused */
public currentItem: IMenuItem | null = null;
public cache?: boolean = false;
public disableRouteWatcher?: boolean = false;
/* istanbul ignore next */
/**
* Creates a new Menu.
* @param props Menu properties
*/
constructor(props: IMenu) {
super(props);
this.app = this.getInitValue('app', props.app, this.app);
this.absolute = this.getInitValue('absolute', props.absolute, this.absolute);
this.clipped = this.getInitValue('clipped', props.clipped, this.clipped);
this.fixed = this.getInitValue('fixed', props.fixed, this.fixed);
this.floating = this.getInitValue('floating', props.floating, this.floating);
this.dense = this.getInitValue('dense', props.dense, this.dense);
this.mini = this.getInitValue('mini', props.mini, this.mini);
this.miniState = this.getInitValue('miniState', props.miniState, this.miniState);
this.miniWidth = this.getInitValue('miniWidth', props.miniWidth, this.miniWidth);
this.temporary = this.getInitValue('temporary', props.temporary, this.temporary);
this.menuItems = props.items || this.menuItems;
if (this.menuItems.length) this.saveLinkRoutes(this.menuItems);
this.menuItemsUrl = this.getInitValue('itemsUrl', props.itemsUrl, this.menuItemsUrl);
this.isLocal = this.getInitValue('isLocal', props.isLocal, this.isLocal);
this.closeToMini = this.getInitValue('closeToMini', props.closeToMini, this.closeToMini);
this.showSearch = this.getInitValue('showSearch', props.showSearch, this.showSearch);
this.width = this.getInitValue('width', props.width, this.width);
this.cache = this.getInitValue('cache', props.cache, this.cache);
this.disableRouteWatcher = this.getInitValue(
'disableRouteWatcher',
props.disableRouteWatcher,
this.disableRouteWatcher,
);
this.permanent = this.getInitValue('permanent', props.permanent, this.permanent);
this.mobileBreakpoint = Config.mobileBreakpoint || 600;
this.drawer = window.innerWidth > Number(this.mobileBreakpoint);
this.openedInitialValue = this.getInitValue('opened', props.opened, this.opened);
if (this.openedInitialValue !== undefined) {
this.opened = this.openedInitialValue;
}
this.topSlot = props.topSlot || this.topSlot;
}
/**
* Defines the url to get menu items
*/
get itemsUrl() {
return this.menuItemsUrl;
}
set itemsUrl(url: string) {
if (this.menuItemsUrl !== url) {
this.menuItemsUrl = url;
if (url) {
this.loadMetadata(url);
} else {
this.items = [];
this.routes = {};
}
}
}
/**
* Menu items
*/
get items() {
return this.searchValue ? this.filteredMenuItems : this.menuItems;
}
set items(items: IMenuItem[]) {
this.menuItems = items;
this.saveLinkRoutes(items);
if (this.selectedPath && this.routes[this.selectedPath]) {
this.selectItem(this.routes[this.selectedPath]);
}
}
public get opened() {
return this.drawer;
}
public set opened(openedValue: boolean) {
this.drawer = openedValue;
}
/**
* Get menu items from a given url
* @param element Element mounted reference
*/
public onMounted(element: any) {
super.onMounted(element);
if (this.itemsUrl) {
this.loadMetadata(this.itemsUrl);
}
}
/**
* Load the menu metadata
* @param url url of the json file
*/
public async loadMetadata(url: string) {
this.items = (await Metadata.get(url, Boolean(this.isLocal), this.cache)) as IMenuItem[];
}
/**
* Save all Link routes parent groups
*/
private saveLinkRoutes(items: IMenuItem[], parents: IMenuItem[] = []) {
items.forEach((item: IMenuItem) => {
if (item.component === 'ZdMenuLink') {
this.routes[item.route] = parents.concat([item]);
} else if (item.component === 'ZdMenuGroup') {
this.saveLinkRoutes(item.items, parents.concat([item]));
}
});
}
/**
* Toggle menu
*/
public toggleMenu() {
const isMobile = window.innerWidth <= Number(this.mobileBreakpoint);
if (this.closeToMini && !isMobile) {
this.miniState = !this.miniState;
if (this.miniState) {
this.closeAllMenus();
} else {
this.openSelectedTree();
}
this.opened = true;
} else {
this.opened = !this.opened;
}
}
/**
* Close all menu
*/
public closeAllMenus() {
this.items.forEach((item) => {
if (item.component === 'ZdMenuGroup') {
item.opened = false;
try {
Metadata.getInstance<MenuGroup>(item.name).close();
} catch (_error) {
// instance not yet created
}
}
});
}
/**
* Saves the selected path
* @param path Path name
*/
public selectPath(path: string) {
this.selectedPath = path;
if (this.routes[path]) {
this.selectItem(this.routes[path]);
}
}
/**
* Select an item
* @param item Menu item
*/
public selectItem(items: IMenuItem[]) {
this.selectedItems = items;
this.openSelectedTree();
}
/**
* @param item Menu item
* @returns Item is selected or not
*/
public isSelected(item: IMenuItem) {
return this.selectedItems.filter((selected) => selected.name === item.name).length > 0;
}
public menuMouseEnter() {
if (this.miniState) {
this.openingMenuTimeout = setTimeout(() => {
this.mouseOver = true;
this.openSelectedTree();
}, 200);
}
}
/**
* Open all selected items
*/
public openSelectedTree() {
this.selectedItems.forEach((item) => {
if (item.component === 'ZdMenuGroup') {
item.opened = true;
try {
Metadata.getInstance<MenuGroup>(item.name).open();
} catch (_error) {
// instance not yet created
}
}
});
}
public menuMouseLeave() {
this.mouseOver = false;
this.showTooltip = false;
clearTimeout(this.openingMenuTimeout);
if (this.miniState) {
this.closeAllMenus();
}
}
/**
* Responds to Url changes
*/
public urlChanged(current: any) {
this.selectPath(current.path);
}
get search() {
return this.searchValue;
}
set search(value: string) {
this.searchValue = value;
this.doSearch();
}
public clearSearch() {
this.search = '';
}
/**
* Search inside menu items
*/
public doSearch() {
const items: IMenuItem[] = this.menuItems;
this.filteredMenuItems = [];
if (this.searchValue) {
this.searchItems(items);
}
}
private getAccessorValue(accessorName: string) {
const [controller, accessor] = Accessor.getAccessor(accessorName);
const instance = Loader.getInstance(controller);
return instance[accessor];
}
private isItemVisible(item: IMenuItem) {
if (typeof item.isVisible === 'string') {
return this.getAccessorValue(item.isVisible);
}
return item.isVisible !== false;
}
private searchItems(items: IMenuItem[]) {
items.forEach((item) => {
if (this.isItemVisible(item)) {
if (item.component === 'ZdMenuGroup') {
this.searchItems(item.items);
} else if (Utils.normalize(I18n.translate(item.label)).indexOf(Utils.normalize(this.searchValue)) !== -1) {
this.filteredMenuItems.push(item);
}
}
});
}
}
|