NavItem.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { numbers } from './constants';
  2. export const DEFAULT_TOGGLE_ICON = {
  3. open: 'chevron_up',
  4. closed: 'chevron_down',
  5. };
  6. export default class NavItem {
  7. items: any[];
  8. toggleIcon: any;
  9. constructor(options: any = {}) {
  10. if (options == null || typeof options !== 'object') {
  11. options = {
  12. text: options,
  13. itemKey: options,
  14. maxHeight: numbers.DEFAULT_SUBNAV_MAX_HEIGHT,
  15. // selected: false,
  16. // isOpen: false,
  17. link: null,
  18. items: null,
  19. icon: '',
  20. indent: false,
  21. };
  22. }
  23. for (const key of Object.keys(options)) {
  24. this[key] = options[key];
  25. }
  26. if (options.items && Array.isArray(options.items) && options.items.length) {
  27. this.items = options.items.map((item: any) => new NavItem(item));
  28. if ('toggleIcon' in options) {
  29. this.toggleIcon = NavItem.isValidToggleIcon(options.toggleIcon)
  30. ? { ...options.toggleIcon }
  31. : { ...DEFAULT_TOGGLE_ICON };
  32. } else {
  33. this.toggleIcon = { ...DEFAULT_TOGGLE_ICON };
  34. }
  35. } else {
  36. this.items = null;
  37. }
  38. }
  39. static isValidToggleIcon(toggleIcon: any) {
  40. return Boolean(toggleIcon &&
  41. typeof toggleIcon === 'object' &&
  42. typeof toggleIcon.open === 'string' &&
  43. toggleIcon.open.length &&
  44. typeof toggleIcon.closed === 'string' &&
  45. toggleIcon.closed.length);
  46. }
  47. }