@vxe-ui/core 3.4.42 → 3.5.1
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.
- package/es/index.d.ts +124 -0
- package/es/index.esm.js +135 -122
- package/es/index.js +147 -0
- package/es/src/base-utils.d.ts +2 -0
- package/es/src/base-utils.js +40 -37
- package/es/src/clipboard.d.ts +2 -0
- package/es/src/clipboard.js +48 -45
- package/es/src/commands.d.ts +2 -0
- package/es/src/commands.js +47 -48
- package/es/src/config.d.ts +13 -0
- package/es/src/config.js +21 -23
- package/es/src/configStore.d.ts +2 -0
- package/es/src/configStore.js +8 -5
- package/es/src/core.d.ts +3 -0
- package/es/src/core.js +11 -7
- package/es/src/dataStore.d.ts +2 -0
- package/es/src/dataStore.js +4 -1
- package/es/src/db-utils.d.ts +2 -0
- package/es/src/db-utils.js +234 -0
- package/es/src/dom-utils.d.ts +2 -0
- package/es/src/dom-utils.js +65 -68
- package/es/src/event.d.ts +4 -0
- package/es/src/event.js +133 -147
- package/es/src/formats.d.ts +2 -0
- package/es/src/formats.js +47 -48
- package/es/src/i18n.d.ts +6 -0
- package/es/src/i18n.js +54 -47
- package/es/src/i18nStore.d.ts +5 -0
- package/es/src/i18nStore.js +11 -8
- package/es/src/icon.d.ts +6 -0
- package/es/src/icon.js +27 -21
- package/es/src/iconStore.d.ts +2 -0
- package/es/src/iconStore.js +4 -1
- package/es/src/interceptor.d.ts +2 -0
- package/es/src/interceptor.js +52 -54
- package/es/src/log.d.ts +2 -0
- package/es/src/log.js +14 -11
- package/es/src/menus.d.ts +2 -0
- package/es/src/menus.js +47 -48
- package/es/src/mixins.d.ts +36 -0
- package/es/src/mixins.js +229 -236
- package/es/src/permission.d.ts +3 -0
- package/es/src/permission.js +57 -55
- package/es/src/popper-utils.d.ts +2 -0
- package/es/src/popper-utils.js +159 -179
- package/es/src/renderer.d.ts +5 -0
- package/es/src/renderer.js +37 -42
- package/es/src/resize.d.ts +2 -0
- package/es/src/resize.js +60 -70
- package/es/src/scrollbar-utils.d.ts +2 -0
- package/es/src/scrollbar-utils.js +57 -54
- package/es/src/store.d.ts +13 -0
- package/es/src/store.js +43 -45
- package/es/src/theme.d.ts +3 -0
- package/es/src/theme.js +17 -13
- package/es/src/themeStore.d.ts +4 -0
- package/es/src/themeStore.js +5 -2
- package/es/src/validators.d.ts +2 -0
- package/es/src/validators.js +6 -3
- package/es/src/vm.d.ts +2 -0
- package/es/src/vm.js +9 -6
- package/es/src/z-index.d.ts +2 -0
- package/es/src/z-index.js +29 -26
- package/lib/index.common.js +132 -353
- package/lib/index.js +178 -0
- package/lib/index.umd.js +790 -3303
- package/lib/index.umd.min.js +1 -1
- package/lib/src/base-utils.js +39 -13
- package/lib/src/clipboard.js +50 -23
- package/lib/src/commands.js +46 -27
- package/lib/src/config.js +43 -23
- package/lib/src/configStore.js +24 -7
- package/lib/src/core.js +29 -11
- package/lib/src/dataStore.js +22 -5
- package/lib/src/db-utils.js +263 -0
- package/lib/src/dom-utils.js +40 -20
- package/lib/src/event.js +95 -102
- package/lib/src/formats.js +46 -27
- package/lib/src/i18n.js +54 -41
- package/lib/src/i18nStore.js +35 -9
- package/lib/src/icon.js +45 -21
- package/lib/src/iconStore.js +22 -5
- package/lib/src/interceptor.js +40 -18
- package/lib/src/log.js +29 -12
- package/lib/src/menus.js +46 -27
- package/lib/src/mixins.js +67 -83
- package/lib/src/permission.js +40 -17
- package/lib/src/popper-utils.js +71 -71
- package/lib/src/renderer.js +40 -21
- package/lib/src/resize.js +48 -42
- package/lib/src/scrollbar-utils.js +23 -6
- package/lib/src/store.js +44 -25
- package/lib/src/theme.js +31 -14
- package/lib/src/themeStore.js +24 -7
- package/lib/src/validators.js +34 -10
- package/lib/src/vm.js +34 -8
- package/lib/src/z-index.js +38 -12
- package/package.json +17 -3
- package/packages/index.ts +169 -166
- package/packages/src/base-utils.ts +42 -43
- package/packages/src/clipboard.ts +56 -56
- package/packages/src/commands.ts +56 -56
- package/packages/src/config.ts +30 -30
- package/packages/src/configStore.ts +8 -8
- package/packages/src/core.ts +11 -11
- package/packages/src/dataStore.ts +3 -4
- package/packages/src/db-utils.ts +267 -0
- package/packages/src/dom-utils.ts +86 -87
- package/packages/src/event.ts +161 -161
- package/packages/src/formats.ts +56 -56
- package/packages/src/i18n.ts +66 -66
- package/packages/src/i18nStore.ts +16 -16
- package/packages/src/icon.ts +32 -32
- package/packages/src/iconStore.ts +3 -3
- package/packages/src/interceptor.ts +64 -64
- package/packages/src/log.ts +19 -19
- package/packages/src/menus.ts +56 -56
- package/packages/src/mixins.ts +284 -292
- package/packages/src/permission.ts +61 -61
- package/packages/src/popper-utils.ts +197 -194
- package/packages/src/renderer.ts +50 -50
- package/packages/src/resize.ts +89 -89
- package/packages/src/scrollbar-utils.ts +58 -58
- package/packages/src/store.ts +47 -47
- package/packages/src/theme.ts +20 -20
- package/packages/src/themeStore.ts +7 -7
- package/packages/src/validators.ts +7 -7
- package/packages/src/vm.ts +10 -10
- package/packages/src/z-index.ts +30 -30
- package/types/core/db-utils.d.ts +44 -0
- package/types/core/global-config.d.ts +4 -6
- package/types/core/index.d.ts +7 -0
- package/lib/src/base-utils.min.js +0 -1
- package/lib/src/clipboard.min.js +0 -1
- package/lib/src/commands.min.js +0 -1
- package/lib/src/config.min.js +0 -1
- package/lib/src/configStore.min.js +0 -1
- package/lib/src/core.min.js +0 -1
- package/lib/src/dataStore.min.js +0 -1
- package/lib/src/dom-utils.min.js +0 -1
- package/lib/src/event.min.js +0 -1
- package/lib/src/formats.min.js +0 -1
- package/lib/src/i18n.min.js +0 -1
- package/lib/src/i18nStore.min.js +0 -1
- package/lib/src/icon.min.js +0 -1
- package/lib/src/iconStore.min.js +0 -1
- package/lib/src/interceptor.min.js +0 -1
- package/lib/src/log.min.js +0 -1
- package/lib/src/menus.min.js +0 -1
- package/lib/src/mixins.min.js +0 -1
- package/lib/src/permission.min.js +0 -1
- package/lib/src/popper-utils.min.js +0 -1
- package/lib/src/renderer.min.js +0 -1
- package/lib/src/resize.min.js +0 -1
- package/lib/src/scrollbar-utils.min.js +0 -1
- package/lib/src/store.min.js +0 -1
- package/lib/src/theme.min.js +0 -1
- package/lib/src/themeStore.min.js +0 -1
- package/lib/src/validators.min.js +0 -1
- package/lib/src/vm.min.js +0 -1
- package/lib/src/z-index.min.js +0 -1
package/es/src/clipboard.js
CHANGED
|
@@ -1,52 +1,55 @@
|
|
|
1
|
-
import XEUtils from
|
|
1
|
+
import XEUtils from "xe-utils";
|
|
2
2
|
let copyElem;
|
|
3
3
|
const clipStore = {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
text: "",
|
|
5
|
+
html: ""
|
|
6
6
|
};
|
|
7
7
|
function handleText(text) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
if (!copyElem) {
|
|
9
|
+
copyElem = document.createElement("textarea");
|
|
10
|
+
copyElem.id = "$VxeCopy";
|
|
11
|
+
const styles = copyElem.style;
|
|
12
|
+
styles.width = "48px";
|
|
13
|
+
styles.height = "24px";
|
|
14
|
+
styles.position = "fixed";
|
|
15
|
+
styles.zIndex = "0";
|
|
16
|
+
styles.left = "-500px";
|
|
17
|
+
styles.top = "-500px";
|
|
18
|
+
document.body.appendChild(copyElem);
|
|
19
|
+
}
|
|
20
|
+
copyElem.value = text;
|
|
21
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
|
-
catch (e) { }
|
|
47
|
-
return result;
|
|
48
|
-
},
|
|
49
|
-
getText() {
|
|
50
|
-
return clipStore.text || '';
|
|
22
|
+
const clipboard = {
|
|
23
|
+
getStore() {
|
|
24
|
+
return clipStore;
|
|
25
|
+
},
|
|
26
|
+
setStore(data) {
|
|
27
|
+
Object.assign(clipStore, data || {});
|
|
28
|
+
},
|
|
29
|
+
/**
|
|
30
|
+
* 复制内容到剪贴板
|
|
31
|
+
*
|
|
32
|
+
* @param {String} content Text 内容
|
|
33
|
+
*/
|
|
34
|
+
copy(content) {
|
|
35
|
+
let result = false;
|
|
36
|
+
try {
|
|
37
|
+
const text = XEUtils.toValueString(content);
|
|
38
|
+
handleText(text);
|
|
39
|
+
copyElem.select();
|
|
40
|
+
copyElem.setSelectionRange(0, copyElem.value.length);
|
|
41
|
+
result = document.execCommand("copy");
|
|
42
|
+
copyElem.blur();
|
|
43
|
+
clipStore.text = text;
|
|
44
|
+
clipStore.html = "";
|
|
45
|
+
} catch (e) {
|
|
51
46
|
}
|
|
47
|
+
return result;
|
|
48
|
+
},
|
|
49
|
+
getText() {
|
|
50
|
+
return clipStore.text || "";
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
export {
|
|
54
|
+
clipboard
|
|
52
55
|
};
|
package/es/src/commands.js
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import XEUtils from "xe-utils";
|
|
5
|
+
import { log } from "./log";
|
|
3
6
|
class VXECommandsStore {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// 兼容
|
|
27
|
-
if (XEUtils.isFunction(render)) {
|
|
28
|
-
log.warn('vxe.error.delProp', ['commands -> callback', 'commandMethod']);
|
|
29
|
-
render = {
|
|
30
|
-
commandMethod: render
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
// 检测是否覆盖
|
|
34
|
-
const confKeys = XEUtils.keys(conf);
|
|
35
|
-
XEUtils.each(render, (item, key) => {
|
|
36
|
-
if (confKeys.includes(key)) {
|
|
37
|
-
log.warn('vxe.error.coverProp', [name, key]);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
this.store[name] = conf ? XEUtils.merge(conf, render) : render;
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
delete(name) {
|
|
44
|
-
delete this.store[name];
|
|
45
|
-
}
|
|
46
|
-
forEach(callback) {
|
|
47
|
-
XEUtils.objectEach(this.store, callback);
|
|
7
|
+
constructor() {
|
|
8
|
+
__publicField(this, "store", {});
|
|
9
|
+
}
|
|
10
|
+
mixin(options) {
|
|
11
|
+
XEUtils.each(options, (item, key) => {
|
|
12
|
+
this.add(key, item);
|
|
13
|
+
});
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
has(name) {
|
|
17
|
+
return !!this.get(name);
|
|
18
|
+
}
|
|
19
|
+
get(name) {
|
|
20
|
+
return this.store[name];
|
|
21
|
+
}
|
|
22
|
+
add(name, render) {
|
|
23
|
+
const conf = this.store[name];
|
|
24
|
+
if (XEUtils.isFunction(render)) {
|
|
25
|
+
log.warn("vxe.error.delProp", ["commands -> callback", "commandMethod"]);
|
|
26
|
+
render = {
|
|
27
|
+
commandMethod: render
|
|
28
|
+
};
|
|
48
29
|
}
|
|
30
|
+
const confKeys = XEUtils.keys(conf);
|
|
31
|
+
XEUtils.each(render, (item, key) => {
|
|
32
|
+
if (confKeys.includes(key)) {
|
|
33
|
+
log.warn("vxe.error.coverProp", [name, key]);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
this.store[name] = conf ? XEUtils.merge(conf, render) : render;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
delete(name) {
|
|
40
|
+
delete this.store[name];
|
|
41
|
+
}
|
|
42
|
+
forEach(callback) {
|
|
43
|
+
XEUtils.objectEach(this.store, callback);
|
|
44
|
+
}
|
|
49
45
|
}
|
|
50
|
-
|
|
51
|
-
Object.assign(commands, { _name:
|
|
46
|
+
const commands = new VXECommandsStore();
|
|
47
|
+
Object.assign(commands, { _name: "Commands" });
|
|
48
|
+
export {
|
|
49
|
+
commands
|
|
50
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { VxeGlobalConfig } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* 全局参数设置
|
|
4
|
+
*/
|
|
5
|
+
export declare function setConfig(options?: VxeGlobalConfig): import("../../types").VxeUIExport;
|
|
6
|
+
/**
|
|
7
|
+
* 获取全局参数
|
|
8
|
+
*/
|
|
9
|
+
export declare function getConfig(key: keyof VxeGlobalConfig, defaultValue?: any): string | number | boolean | VxeGlobalConfig | ((key: string, args?: any) => string | number) | ((params: {
|
|
10
|
+
status: boolean;
|
|
11
|
+
code: number;
|
|
12
|
+
msg: string;
|
|
13
|
+
}) => void) | null | undefined;
|
package/es/src/config.js
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
|
-
import XEUtils from
|
|
2
|
-
import DomZIndex from
|
|
3
|
-
import { VxeCore } from
|
|
4
|
-
import { globalConfigStore } from
|
|
5
|
-
import { setTheme } from
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (options) {
|
|
11
|
-
if (options.zIndex) {
|
|
12
|
-
DomZIndex.setCurrent(options.zIndex);
|
|
13
|
-
}
|
|
14
|
-
if (options.theme) {
|
|
15
|
-
setTheme(options.theme);
|
|
16
|
-
}
|
|
17
|
-
XEUtils.merge(globalConfigStore, options);
|
|
1
|
+
import XEUtils from "xe-utils";
|
|
2
|
+
import DomZIndex from "dom-zindex";
|
|
3
|
+
import { VxeCore } from "./core";
|
|
4
|
+
import { globalConfigStore } from "./configStore";
|
|
5
|
+
import { setTheme } from "./theme";
|
|
6
|
+
function setConfig(options) {
|
|
7
|
+
if (options) {
|
|
8
|
+
if (options.zIndex) {
|
|
9
|
+
DomZIndex.setCurrent(options.zIndex);
|
|
18
10
|
}
|
|
19
|
-
|
|
11
|
+
if (options.theme) {
|
|
12
|
+
setTheme(options.theme);
|
|
13
|
+
}
|
|
14
|
+
XEUtils.merge(globalConfigStore, options);
|
|
15
|
+
}
|
|
16
|
+
return VxeCore;
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*/
|
|
24
|
-
export function getConfig(key, defaultValue) {
|
|
25
|
-
return arguments.length ? XEUtils.get(globalConfigStore, key, defaultValue) : globalConfigStore;
|
|
18
|
+
function getConfig(key, defaultValue) {
|
|
19
|
+
return arguments.length ? XEUtils.get(globalConfigStore, key, defaultValue) : globalConfigStore;
|
|
26
20
|
}
|
|
21
|
+
export {
|
|
22
|
+
getConfig,
|
|
23
|
+
setConfig
|
|
24
|
+
};
|
package/es/src/configStore.js
CHANGED
package/es/src/core.d.ts
ADDED
package/es/src/core.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
const coreVersion = "3.5.1";
|
|
2
|
+
const VxeCore = {
|
|
3
|
+
coreVersion,
|
|
4
|
+
uiVersion: "",
|
|
5
|
+
tableVersion: "",
|
|
6
|
+
designVersion: "",
|
|
7
|
+
ganttVersion: ""
|
|
8
|
+
};
|
|
9
|
+
export {
|
|
10
|
+
VxeCore,
|
|
11
|
+
coreVersion
|
|
8
12
|
};
|
package/es/src/dataStore.js
CHANGED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import XEUtils from "xe-utils";
|
|
5
|
+
import { globalConfigStore } from "./configStore";
|
|
6
|
+
class IdbStore {
|
|
7
|
+
constructor(key, version) {
|
|
8
|
+
__publicField(this, "storeName", "Storage");
|
|
9
|
+
__publicField(this, "key");
|
|
10
|
+
__publicField(this, "version");
|
|
11
|
+
__publicField(this, "dbPromise", null);
|
|
12
|
+
__publicField(this, "dbName", "");
|
|
13
|
+
this.key = key;
|
|
14
|
+
this.version = version;
|
|
15
|
+
}
|
|
16
|
+
getDBName() {
|
|
17
|
+
return `${this.key}_${this.version}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 重置连接缓存,下次调用 openDB 会重新建立连接
|
|
21
|
+
*/
|
|
22
|
+
resetDB() {
|
|
23
|
+
this.dbPromise = null;
|
|
24
|
+
this.dbName = "";
|
|
25
|
+
}
|
|
26
|
+
openDB() {
|
|
27
|
+
const name = this.getDBName();
|
|
28
|
+
if (this.dbPromise && this.dbName === name) {
|
|
29
|
+
return this.dbPromise;
|
|
30
|
+
}
|
|
31
|
+
this.dbName = name;
|
|
32
|
+
this.dbPromise = new Promise((resolve, reject) => {
|
|
33
|
+
const request = indexedDB.open(name, 1);
|
|
34
|
+
request.onupgradeneeded = () => {
|
|
35
|
+
const db = request.result;
|
|
36
|
+
if (!db.objectStoreNames.contains(this.storeName)) {
|
|
37
|
+
db.createObjectStore(this.storeName, { keyPath: "id" });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
request.onsuccess = () => {
|
|
41
|
+
const db = request.result;
|
|
42
|
+
db.onversionchange = () => {
|
|
43
|
+
db.close();
|
|
44
|
+
this.resetDB();
|
|
45
|
+
};
|
|
46
|
+
db.onclose = () => {
|
|
47
|
+
this.resetDB();
|
|
48
|
+
};
|
|
49
|
+
resolve(db);
|
|
50
|
+
};
|
|
51
|
+
request.onerror = () => {
|
|
52
|
+
reject(request.error || new Error(`IdbStore open failed: ${name}`));
|
|
53
|
+
};
|
|
54
|
+
request.onblocked = () => {
|
|
55
|
+
console.warn(`[IdbStore] open blocked: ${name}`);
|
|
56
|
+
};
|
|
57
|
+
}).catch((err) => {
|
|
58
|
+
this.resetDB();
|
|
59
|
+
throw err;
|
|
60
|
+
});
|
|
61
|
+
return this.dbPromise;
|
|
62
|
+
}
|
|
63
|
+
getById(id) {
|
|
64
|
+
return this.openDB().then(
|
|
65
|
+
(db) => new Promise((resolve, reject) => {
|
|
66
|
+
const req = db.transaction(this.storeName, "readonly").objectStore(this.storeName).get(id);
|
|
67
|
+
req.onsuccess = () => resolve(req.result);
|
|
68
|
+
req.onerror = () => reject(req.error);
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
getAll() {
|
|
73
|
+
return this.openDB().then(
|
|
74
|
+
(db) => new Promise((resolve, reject) => {
|
|
75
|
+
const store = db.transaction(this.storeName, "readonly").objectStore(this.storeName);
|
|
76
|
+
if (XEUtils.isFunction(store.getAll)) {
|
|
77
|
+
const req2 = store.getAll();
|
|
78
|
+
req2.onsuccess = () => resolve(req2.result || []);
|
|
79
|
+
req2.onerror = () => reject(req2.error);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const list = [];
|
|
83
|
+
const req = store.openCursor();
|
|
84
|
+
req.onsuccess = () => {
|
|
85
|
+
const cursor = req.result;
|
|
86
|
+
if (cursor) {
|
|
87
|
+
list.push(cursor.value);
|
|
88
|
+
cursor.continue();
|
|
89
|
+
} else {
|
|
90
|
+
resolve(list);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
req.onerror = () => reject(req.error);
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
updateById(id, data) {
|
|
98
|
+
if (data == null) {
|
|
99
|
+
return this.deleteById(id);
|
|
100
|
+
}
|
|
101
|
+
return this.openDB().then(
|
|
102
|
+
(db) => new Promise((resolve, reject) => {
|
|
103
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
104
|
+
tx.objectStore(this.storeName).put({ id, data });
|
|
105
|
+
tx.oncomplete = () => resolve();
|
|
106
|
+
tx.onerror = () => reject(tx.error);
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
deleteById(id) {
|
|
111
|
+
return this.openDB().then(
|
|
112
|
+
(db) => new Promise((resolve, reject) => {
|
|
113
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
114
|
+
tx.objectStore(this.storeName).delete(id);
|
|
115
|
+
tx.oncomplete = () => resolve();
|
|
116
|
+
tx.onerror = () => reject(tx.error);
|
|
117
|
+
})
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* 清空当前库的数据
|
|
122
|
+
*/
|
|
123
|
+
clear() {
|
|
124
|
+
return this.openDB().then(
|
|
125
|
+
(db) => new Promise((resolve, reject) => {
|
|
126
|
+
const tx = db.transaction(this.storeName, "readwrite");
|
|
127
|
+
tx.objectStore(this.storeName).clear();
|
|
128
|
+
tx.oncomplete = () => resolve();
|
|
129
|
+
tx.onerror = () => reject(tx.error);
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 清理非当前版本的旧库
|
|
135
|
+
*/
|
|
136
|
+
cleanupOldDBs() {
|
|
137
|
+
if (!indexedDB.databases) {
|
|
138
|
+
return Promise.resolve();
|
|
139
|
+
}
|
|
140
|
+
const current = this.getDBName();
|
|
141
|
+
const prefix = `${this.key}_`;
|
|
142
|
+
return indexedDB.databases().then((dbs) => {
|
|
143
|
+
dbs.forEach((db) => {
|
|
144
|
+
const n = db.name;
|
|
145
|
+
if (!n || n === current || !n.startsWith(prefix)) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const rest = n.slice(prefix.length);
|
|
149
|
+
if (!rest.includes("_")) {
|
|
150
|
+
indexedDB.deleteDatabase(n);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
class LocalStorageStore {
|
|
157
|
+
constructor(key, version) {
|
|
158
|
+
__publicField(this, "key");
|
|
159
|
+
__publicField(this, "version");
|
|
160
|
+
this.key = key;
|
|
161
|
+
this.version = version;
|
|
162
|
+
}
|
|
163
|
+
readAll() {
|
|
164
|
+
const storageData = XEUtils.toStringJSON(localStorage.getItem(this.key) || "");
|
|
165
|
+
return storageData && String(storageData._v) === String(this.version) ? storageData : { _v: this.version };
|
|
166
|
+
}
|
|
167
|
+
writeAll(map) {
|
|
168
|
+
localStorage.setItem(this.key, XEUtils.toJSONString(map));
|
|
169
|
+
}
|
|
170
|
+
getById(id) {
|
|
171
|
+
const map = this.readAll();
|
|
172
|
+
const has = Object.prototype.hasOwnProperty.call(map, id);
|
|
173
|
+
return Promise.resolve(has && map[id] != null ? { id, data: map[id] } : void 0);
|
|
174
|
+
}
|
|
175
|
+
getAll() {
|
|
176
|
+
const map = this.readAll();
|
|
177
|
+
const list = [];
|
|
178
|
+
Object.keys(map).forEach((k) => {
|
|
179
|
+
if (k !== "_v" && map[k] != null) {
|
|
180
|
+
list.push({ id: k, data: map[k] });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return Promise.resolve(list);
|
|
184
|
+
}
|
|
185
|
+
updateById(id, data) {
|
|
186
|
+
if (data == null) {
|
|
187
|
+
return this.deleteById(id);
|
|
188
|
+
}
|
|
189
|
+
const map = this.readAll();
|
|
190
|
+
map[id] = data;
|
|
191
|
+
this.writeAll(map);
|
|
192
|
+
return Promise.resolve();
|
|
193
|
+
}
|
|
194
|
+
deleteById(id) {
|
|
195
|
+
const map = this.readAll();
|
|
196
|
+
delete map[id];
|
|
197
|
+
this.writeAll(map);
|
|
198
|
+
return Promise.resolve();
|
|
199
|
+
}
|
|
200
|
+
clear() {
|
|
201
|
+
this.writeAll({ _v: this.version });
|
|
202
|
+
return Promise.resolve();
|
|
203
|
+
}
|
|
204
|
+
cleanupOldDBs() {
|
|
205
|
+
const raw = localStorage.getItem(this.key);
|
|
206
|
+
if (!raw) {
|
|
207
|
+
return Promise.resolve();
|
|
208
|
+
}
|
|
209
|
+
const storageData = XEUtils.toStringJSON(raw);
|
|
210
|
+
if (!storageData || String(storageData._v) !== String(this.version)) {
|
|
211
|
+
localStorage.removeItem(this.key);
|
|
212
|
+
}
|
|
213
|
+
return Promise.resolve();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const dbUtils = {
|
|
217
|
+
/**
|
|
218
|
+
* 创建存储实例
|
|
219
|
+
* - driver = 'indexedDB' 且环境支持 => IdbStore
|
|
220
|
+
* - 其余情况(未传 / 不支持)=> LocalStorageStore
|
|
221
|
+
*/
|
|
222
|
+
createStore(key, options) {
|
|
223
|
+
const opts = Object.assign({}, options);
|
|
224
|
+
const version = opts.version || globalConfigStore.version || 0;
|
|
225
|
+
const driver = opts.driver;
|
|
226
|
+
if ((!driver || driver === "indexedDB") && typeof indexedDB !== "undefined") {
|
|
227
|
+
return new IdbStore(key, version);
|
|
228
|
+
}
|
|
229
|
+
return new LocalStorageStore(key, version);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
export {
|
|
233
|
+
dbUtils
|
|
234
|
+
};
|