@moluoxixi/ajax-package 0.0.36 → 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.
@@ -17,6 +17,7 @@ export default class BaseHttpClient {
17
17
  protected getToken?: () => string | null;
18
18
  protected onLoginRequired?: (messageInstance: MessageInstance) => void;
19
19
  protected appendTo?: HTMLElement | string | null;
20
+ protected appendToFallback: string | null;
20
21
  instance: ReturnType<typeof axios.create>;
21
22
  protected messageInstance: MessageInstance;
22
23
  protected notificationInstance: NotificationInstance;
@@ -33,9 +34,9 @@ export default class BaseHttpClient {
33
34
  protected get hasDocument(): boolean;
34
35
  /**
35
36
  * 解析 appendTo 配置,返回目标 HTMLElement
36
- * @returns 目标元素,默认为 document.body
37
+ * @returns 目标元素,如果找不到且配置为 null 则返回 null,'body' 时返回 document.body,其他字符串作为选择器查找
37
38
  */
38
- protected resolveAppendToTarget(): HTMLElement;
39
+ protected resolveAppendToTarget(): HTMLElement | null;
39
40
  /**
40
41
  * 获取或创建 ajaxPackage-container 容器元素
41
42
  * @returns 容器元素,如果不是浏览器环境则返回 null
@@ -18,6 +18,8 @@ export interface BaseHttpClientConfig {
18
18
  onLoginRequired?: (messageInstance: MessageInstance) => void;
19
19
  /** container 插入的目标元素,可以是 HTMLElement 或选择器字符串,默认为 document.body */
20
20
  appendTo?: HTMLElement | string | null;
21
+ /** 当 appendTo 配置了但找不到元素时的处理方式:'body' - 使用 document.body(默认),string - 作为选择器查找后备元素,null - 返回 null 不降级 */
22
+ appendToFallback?: string | null;
21
23
  /** 允许其他任意配置项,会直接传递给 axios.create */
22
24
  [key: string]: any;
23
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("55abf58f-b1b4-4e82-ab0d-ee119c15bd62")) {
5
+ if (!document.getElementById("d90637ed-57fd-4f94-b0c2-d8acbe328d03")) {
6
6
  var elementStyle = document.createElement("style");
7
- elementStyle.id = "55abf58f-b1b4-4e82-ab0d-ee119c15bd62";
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
  }
@@ -12218,6 +12220,7 @@ const _BaseHttpClient = class _BaseHttpClient {
12218
12220
  __publicField(this, "getToken");
12219
12221
  __publicField(this, "onLoginRequired");
12220
12222
  __publicField(this, "appendTo");
12223
+ __publicField(this, "appendToFallback", "body");
12221
12224
  __publicField(this, "instance");
12222
12225
  __publicField(this, "messageInstance");
12223
12226
  __publicField(this, "notificationInstance");
@@ -12230,16 +12233,16 @@ const _BaseHttpClient = class _BaseHttpClient {
12230
12233
  getToken = defaultGetToken,
12231
12234
  onLoginRequired = defaultOnLoginRequired,
12232
12235
  appendTo,
12236
+ appendToFallback = "body",
12233
12237
  addSign,
12234
12238
  ...axiosConfig
12235
12239
  } = config;
12236
12240
  this.baseURL = baseURL;
12237
12241
  this.timeout = timeout;
12238
12242
  this.appendTo = appendTo;
12239
- this.getContainer();
12240
- const messageContainer = this.getMessageContainer();
12241
- this.messageInstance = createMessageWrapper(this.hasDocument, messageContainer);
12242
- this.notificationInstance = createNotificationWrapper(this.hasDocument, messageContainer);
12243
+ this.appendToFallback = appendToFallback;
12244
+ this.messageInstance = createMessageWrapper(this.hasDocument, () => this.getMessageContainer());
12245
+ this.notificationInstance = createNotificationWrapper(this.hasDocument, () => this.getMessageContainer());
12243
12246
  this.onTimeout = onTimeout;
12244
12247
  this.getToken = getToken;
12245
12248
  this.onLoginRequired = onLoginRequired;
@@ -12261,7 +12264,7 @@ const _BaseHttpClient = class _BaseHttpClient {
12261
12264
  }
12262
12265
  /**
12263
12266
  * 解析 appendTo 配置,返回目标 HTMLElement
12264
- * @returns 目标元素,默认为 document.body
12267
+ * @returns 目标元素,如果找不到且配置为 null 则返回 null,'body' 时返回 document.body,其他字符串作为选择器查找
12265
12268
  */
12266
12269
  resolveAppendToTarget() {
12267
12270
  if (!this.hasDocument) {
@@ -12278,8 +12281,19 @@ const _BaseHttpClient = class _BaseHttpClient {
12278
12281
  if (element) {
12279
12282
  return element;
12280
12283
  }
12281
- console.warn(`appendTo 选择器 "${this.appendTo}" 未找到元素,将使用默认的 document.body`);
12282
- return document.body;
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
+ }
12283
12297
  }
12284
12298
  return document.body;
12285
12299
  }
@@ -12296,12 +12310,19 @@ const _BaseHttpClient = class _BaseHttpClient {
12296
12310
  container = document.createElement("div");
12297
12311
  container.id = "ajaxPackage-container";
12298
12312
  const targetElement = this.resolveAppendToTarget();
12299
- targetElement.appendChild(container);
12313
+ if (targetElement) {
12314
+ targetElement.appendChild(container);
12315
+ } else {
12316
+ return null;
12317
+ }
12300
12318
  const popoverContainer = document.createElement("div");
12301
12319
  popoverContainer.id = "ajaxPackage-popover";
12320
+ popoverContainer.style.position = "relative";
12321
+ popoverContainer.style.zIndex = "999999";
12302
12322
  container.appendChild(popoverContainer);
12303
12323
  const messageContainer = document.createElement("div");
12304
12324
  messageContainer.id = "ajaxPackage-message";
12325
+ messageContainer.style.position = "relative";
12305
12326
  messageContainer.style.zIndex = "99999999";
12306
12327
  container.appendChild(messageContainer);
12307
12328
  }
@@ -12315,8 +12336,16 @@ const _BaseHttpClient = class _BaseHttpClient {
12315
12336
  if (!this.hasDocument) {
12316
12337
  return null;
12317
12338
  }
12318
- this.getContainer();
12319
- 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;
12320
12349
  }
12321
12350
  /**
12322
12351
  * 获取或创建 ajaxPackage-message 容器元素(用于 Message 和 Notification)
@@ -12326,8 +12355,16 @@ const _BaseHttpClient = class _BaseHttpClient {
12326
12355
  if (!this.hasDocument) {
12327
12356
  return null;
12328
12357
  }
12329
- this.getContainer();
12330
- 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;
12331
12368
  }
12332
12369
  /**
12333
12370
  * 处理请求配置,子类可重写此方法自定义请求配置
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moluoxixi/ajax-package",
3
- "version": "0.0.36",
3
+ "version": "0.0.37",
4
4
  "description": "AjaxPackage 组件",
5
5
  "sideEffects": [
6
6
  "*.css",