@moluoxixi/ajax-package 0.0.35 → 0.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,8 @@ export default class BaseHttpClient {
16
16
  protected onTimeout: (messageInstance: MessageInstance) => void;
17
17
  protected getToken?: () => string | null;
18
18
  protected onLoginRequired?: (messageInstance: MessageInstance) => void;
19
+ protected appendTo?: HTMLElement | string | null;
20
+ protected appendToFallback: string | null;
19
21
  instance: ReturnType<typeof axios.create>;
20
22
  protected messageInstance: MessageInstance;
21
23
  protected notificationInstance: NotificationInstance;
@@ -30,6 +32,11 @@ export default class BaseHttpClient {
30
32
  * @returns 是否在浏览器环境
31
33
  */
32
34
  protected get hasDocument(): boolean;
35
+ /**
36
+ * 解析 appendTo 配置,返回目标 HTMLElement
37
+ * @returns 目标元素,如果找不到且配置为 null 则返回 null,'body' 时返回 document.body,其他字符串作为选择器查找
38
+ */
39
+ protected resolveAppendToTarget(): HTMLElement | null;
33
40
  /**
34
41
  * 获取或创建 ajaxPackage-container 容器元素
35
42
  * @returns 容器元素,如果不是浏览器环境则返回 null
@@ -16,6 +16,10 @@ export interface BaseHttpClientConfig {
16
16
  getToken?: () => string | null;
17
17
  /** 登录失效回调函数,当检测到 401 错误时调用,接收 messageInstance 用于显示消息提示 */
18
18
  onLoginRequired?: (messageInstance: MessageInstance) => void;
19
+ /** container 插入的目标元素,可以是 HTMLElement 或选择器字符串,默认为 document.body */
20
+ appendTo?: HTMLElement | string | null;
21
+ /** 当 appendTo 配置了但找不到元素时的处理方式:'body' - 使用 document.body(默认),string - 作为选择器查找后备元素,null - 返回 null 不降级 */
22
+ appendToFallback?: string | null;
19
23
  /** 允许其他任意配置项,会直接传递给 axios.create */
20
24
  [key: string]: any;
21
25
  }
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * 创建消息实例的包装函数
3
3
  * @param hasDocument - 是否在浏览器环境
4
- * @param container - 容器元素,如果提供则消息将挂载到该容器中
4
+ * @param container - 容器元素或获取容器的函数,如果提供则消息将挂载到该容器中
5
5
  * @returns 消息函数实例,直接调用即可,支持传入 type: 'success' | 'error' | 'warning' | 'info'
6
6
  */
7
- export declare function createMessageWrapper(hasDocument: boolean, container?: HTMLElement | null): (options: string | {
7
+ export declare function createMessageWrapper(hasDocument: boolean, container?: HTMLElement | null | (() => HTMLElement | null)): (options: string | {
8
8
  message?: string;
9
9
  type?: "success" | "error" | "warning" | "info";
10
10
  [key: string]: any;
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * 创建通知实例的包装函数(用于 errors / tips 展示)
3
3
  * @param hasDocument - 是否在浏览器环境
4
- * @param container - 容器元素,如果提供则通知将挂载到该容器中
4
+ * @param container - 容器元素或获取容器的函数,如果提供则通知将挂载到该容器中
5
5
  * @returns 通知函数实例,直接调用即可,支持传入 type: 'success' | 'error' | 'warning' | 'info'
6
6
  */
7
- export declare function createNotificationWrapper(hasDocument: boolean, container?: HTMLElement | null): ((options: string | {
7
+ export declare function createNotificationWrapper(hasDocument: boolean, container?: HTMLElement | null | (() => HTMLElement | null)): ((options: string | {
8
8
  message?: string;
9
9
  title?: string;
10
10
  type?: "success" | "error" | "warning" | "info";
package/es/index.mjs CHANGED
@@ -2,9 +2,9 @@
2
2
  "use strict";
3
3
  try {
4
4
  if (typeof document !== "undefined") {
5
- if (!document.getElementById("59b794d1-a3ef-4b37-9b28-cc9399203b57")) {
5
+ if (!document.getElementById("d90637ed-57fd-4f94-b0c2-d8acbe328d03")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "59b794d1-a3ef-4b37-9b28-cc9399203b57";
7
+ elementStyle.id = "d90637ed-57fd-4f94-b0c2-d8acbe328d03";
8
8
  elementStyle.appendChild(document.createTextNode("._root_11p33_1 .el-dialog__header {\n padding: 0 12px 12px;\n}\n\n._root_11p33_1 .el-dialog__body {\n border-top: 1px solid #e5e7eb;\n border-bottom: 1px solid #e5e7eb;\n padding: 0 12px;\n}\n\n._root_11p33_1 .el-dialog__footer {\n padding: 0 12px;\n}"));
9
9
  document.head.appendChild(elementStyle);
10
10
  }
@@ -12053,7 +12053,8 @@ function createMessageWrapper(hasDocument, container) {
12053
12053
  if (hasDocument) {
12054
12054
  return (options) => {
12055
12055
  const opts = typeof options === "string" ? { message: options } : options;
12056
- const finalOptions = container ? { ...opts, appendTo: container } : opts;
12056
+ const actualContainer = typeof container === "function" ? container() : container;
12057
+ const finalOptions = actualContainer ? { ...opts, appendTo: actualContainer } : opts;
12057
12058
  return ElMessage(finalOptions);
12058
12059
  };
12059
12060
  }
@@ -12078,7 +12079,8 @@ function createNotificationWrapper(hasDocument, container) {
12078
12079
  if (hasDocument) {
12079
12080
  return (options) => {
12080
12081
  const opts = typeof options === "string" ? { message: options } : options;
12081
- const finalOptions = container ? { ...opts, appendTo: container } : opts;
12082
+ const actualContainer = typeof container === "function" ? container() : container;
12083
+ const finalOptions = actualContainer ? { ...opts, appendTo: actualContainer } : opts;
12082
12084
  return ElNotification(finalOptions);
12083
12085
  };
12084
12086
  }
@@ -12217,6 +12219,8 @@ const _BaseHttpClient = class _BaseHttpClient {
12217
12219
  __publicField(this, "onTimeout");
12218
12220
  __publicField(this, "getToken");
12219
12221
  __publicField(this, "onLoginRequired");
12222
+ __publicField(this, "appendTo");
12223
+ __publicField(this, "appendToFallback", "body");
12220
12224
  __publicField(this, "instance");
12221
12225
  __publicField(this, "messageInstance");
12222
12226
  __publicField(this, "notificationInstance");
@@ -12228,15 +12232,17 @@ const _BaseHttpClient = class _BaseHttpClient {
12228
12232
  },
12229
12233
  getToken = defaultGetToken,
12230
12234
  onLoginRequired = defaultOnLoginRequired,
12235
+ appendTo,
12236
+ appendToFallback = "body",
12231
12237
  addSign,
12232
12238
  ...axiosConfig
12233
12239
  } = config;
12234
12240
  this.baseURL = baseURL;
12235
12241
  this.timeout = timeout;
12236
- this.getContainer();
12237
- const messageContainer = this.getMessageContainer();
12238
- this.messageInstance = createMessageWrapper(this.hasDocument, messageContainer);
12239
- this.notificationInstance = createNotificationWrapper(this.hasDocument, messageContainer);
12242
+ this.appendTo = appendTo;
12243
+ this.appendToFallback = appendToFallback;
12244
+ this.messageInstance = createMessageWrapper(this.hasDocument, () => this.getMessageContainer());
12245
+ this.notificationInstance = createNotificationWrapper(this.hasDocument, () => this.getMessageContainer());
12240
12246
  this.onTimeout = onTimeout;
12241
12247
  this.getToken = getToken;
12242
12248
  this.onLoginRequired = onLoginRequired;
@@ -12256,6 +12262,41 @@ const _BaseHttpClient = class _BaseHttpClient {
12256
12262
  get hasDocument() {
12257
12263
  return _BaseHttpClient.hasDocument;
12258
12264
  }
12265
+ /**
12266
+ * 解析 appendTo 配置,返回目标 HTMLElement
12267
+ * @returns 目标元素,如果找不到且配置为 null 则返回 null,'body' 时返回 document.body,其他字符串作为选择器查找
12268
+ */
12269
+ resolveAppendToTarget() {
12270
+ if (!this.hasDocument) {
12271
+ return document.body;
12272
+ }
12273
+ if (!this.appendTo) {
12274
+ return document.body;
12275
+ }
12276
+ if (this.appendTo instanceof HTMLElement) {
12277
+ return this.appendTo;
12278
+ }
12279
+ if (typeof this.appendTo === "string") {
12280
+ const element = document.querySelector(this.appendTo);
12281
+ if (element) {
12282
+ return element;
12283
+ }
12284
+ if (this.appendToFallback === null) {
12285
+ console.warn(`appendTo 选择器 "${this.appendTo}" 未找到元素,appendToFallback 为 null,返回 null`);
12286
+ return null;
12287
+ } else if (this.appendToFallback === "body") {
12288
+ return document.body;
12289
+ } else if (typeof this.appendToFallback === "string") {
12290
+ const fallbackElement = document.querySelector(this.appendToFallback);
12291
+ if (fallbackElement) {
12292
+ return fallbackElement;
12293
+ }
12294
+ console.warn(`appendTo 选择器 "${this.appendTo}" 和 appendToFallback 选择器 "${this.appendToFallback}" 都未找到元素,返回 null`);
12295
+ return null;
12296
+ }
12297
+ }
12298
+ return document.body;
12299
+ }
12259
12300
  /**
12260
12301
  * 获取或创建 ajaxPackage-container 容器元素
12261
12302
  * @returns 容器元素,如果不是浏览器环境则返回 null
@@ -12268,12 +12309,20 @@ const _BaseHttpClient = class _BaseHttpClient {
12268
12309
  if (!container) {
12269
12310
  container = document.createElement("div");
12270
12311
  container.id = "ajaxPackage-container";
12271
- document.body.appendChild(container);
12312
+ const targetElement = this.resolveAppendToTarget();
12313
+ if (targetElement) {
12314
+ targetElement.appendChild(container);
12315
+ } else {
12316
+ return null;
12317
+ }
12272
12318
  const popoverContainer = document.createElement("div");
12273
12319
  popoverContainer.id = "ajaxPackage-popover";
12320
+ popoverContainer.style.position = "relative";
12321
+ popoverContainer.style.zIndex = "999999";
12274
12322
  container.appendChild(popoverContainer);
12275
12323
  const messageContainer = document.createElement("div");
12276
12324
  messageContainer.id = "ajaxPackage-message";
12325
+ messageContainer.style.position = "relative";
12277
12326
  messageContainer.style.zIndex = "99999999";
12278
12327
  container.appendChild(messageContainer);
12279
12328
  }
@@ -12287,8 +12336,16 @@ const _BaseHttpClient = class _BaseHttpClient {
12287
12336
  if (!this.hasDocument) {
12288
12337
  return null;
12289
12338
  }
12290
- this.getContainer();
12291
- return document.getElementById("ajaxPackage-popover");
12339
+ const container = this.getContainer();
12340
+ if (!container) {
12341
+ return null;
12342
+ }
12343
+ const popoverContainer = document.getElementById("ajaxPackage-popover");
12344
+ if (popoverContainer && !popoverContainer.style.position) {
12345
+ popoverContainer.style.position = "relative";
12346
+ popoverContainer.style.zIndex = "999999";
12347
+ }
12348
+ return popoverContainer;
12292
12349
  }
12293
12350
  /**
12294
12351
  * 获取或创建 ajaxPackage-message 容器元素(用于 Message 和 Notification)
@@ -12298,8 +12355,16 @@ const _BaseHttpClient = class _BaseHttpClient {
12298
12355
  if (!this.hasDocument) {
12299
12356
  return null;
12300
12357
  }
12301
- this.getContainer();
12302
- return document.getElementById("ajaxPackage-message");
12358
+ const container = this.getContainer();
12359
+ if (!container) {
12360
+ return null;
12361
+ }
12362
+ const messageContainer = document.getElementById("ajaxPackage-message");
12363
+ if (messageContainer && !messageContainer.style.position) {
12364
+ messageContainer.style.position = "relative";
12365
+ messageContainer.style.zIndex = "99999999";
12366
+ }
12367
+ return messageContainer;
12303
12368
  }
12304
12369
  /**
12305
12370
  * 处理请求配置,子类可重写此方法自定义请求配置
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",