index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
  2. import type { App, Events, HTMLAttributes, Plugin } from 'vue';
  3. import { unref, Component } from 'vue';
  4. import { isObject } from '@jeesite/core/utils/is';
  5. export const noop = () => {};
  6. /**
  7. * @description: Set ui mount node
  8. */
  9. export function getPopupContainer(node?: HTMLElement): HTMLElement {
  10. return (node?.parentNode as HTMLElement) ?? document.body;
  11. }
  12. /**
  13. * Resolve a clickable popup host inside modal content.
  14. * Never mount popup overlays on `.ant-modal` because Ant Design sets it to `pointer-events: none`.
  15. */
  16. export function getModalPopupContainer(node?: HTMLElement): HTMLElement {
  17. return (
  18. (node?.closest('.ant-modal-content') as HTMLElement | null) ||
  19. (node?.closest('.ant-modal-body') as HTMLElement | null) ||
  20. (node?.closest('.ant-modal-wrap') as HTMLElement | null) ||
  21. (node?.parentNode as HTMLElement | null) ||
  22. document.body
  23. );
  24. }
  25. /**
  26. * Add the object as a parameter to the URL
  27. * @param baseUrl url
  28. * @param obj
  29. * @returns {string}
  30. * eg:
  31. * let obj = {a: '3', b: '4'}
  32. * setObjToUrlParams('www.baidu.com', obj)
  33. * ==>www.baidu.com?a=3&b=4
  34. */
  35. export function setObjToUrlParams(baseUrl: string, obj: any): string {
  36. let parameters = '';
  37. for (const key in obj) {
  38. parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  39. }
  40. parameters = parameters.replace(/&$/, '');
  41. return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
  42. }
  43. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  44. let key: string;
  45. for (key in target) {
  46. src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  47. }
  48. return src;
  49. }
  50. export function openWindow(
  51. url: string,
  52. opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
  53. ) {
  54. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  55. const feature: string[] = [];
  56. noopener && feature.push('noopener=yes');
  57. noreferrer && feature.push('noreferrer=yes');
  58. // t h i n k g e m / j e e s i t e
  59. if (url && (url.endsWith('?___blank') || url.endsWith('&___blank'))) {
  60. url = url.substring(0, url.length - 9);
  61. }
  62. window.open(url, target, feature.join(','));
  63. }
  64. export function openWindowLayer(url: string, opt?: { width?: number; height?: number }) {
  65. const win = window as any;
  66. let layerWidth = opt?.width || win.$(win).width();
  67. if (layerWidth < 800) {
  68. layerWidth -= 15 * 2;
  69. } else {
  70. layerWidth -= 100 * 2;
  71. }
  72. let layerHeight = opt?.height || win.$(win).height();
  73. if (layerHeight < 500) {
  74. layerHeight -= 15 * 2;
  75. } else {
  76. layerHeight -= 25 * 2;
  77. }
  78. win.layer.open({
  79. type: 2,
  80. maxmin: true,
  81. shadeClose: true, // 点击背景关闭
  82. title: false,
  83. area: [layerWidth + 'px', layerHeight + 'px'],
  84. method: 'get',
  85. content: url,
  86. });
  87. }
  88. // dynamic use hook props
  89. export function getDynamicProps<T, U>(props: T): Partial<U> {
  90. const ret: Recordable = {};
  91. Object.keys(props as any).map((key) => {
  92. ret[key] = unref((props as Recordable)[key]);
  93. });
  94. return ret as Partial<U>;
  95. }
  96. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  97. if (!route) return route;
  98. const { matched, ...opt } = route;
  99. return {
  100. ...opt,
  101. matched: (matched
  102. ? matched.map((item) => ({
  103. meta: item.meta,
  104. name: item.name,
  105. path: item.path,
  106. }))
  107. : undefined) as RouteRecordNormalized[],
  108. };
  109. }
  110. export const withInstall = <T extends Component>(component: T, alias?: string) => {
  111. component['install'] = (app: App) => {
  112. app.component(component.name || component['displayName'], component);
  113. if (alias) {
  114. app.config.globalProperties[alias] = component;
  115. }
  116. };
  117. return component as T & Plugin & HTMLAttributes & Events;
  118. };