amis 1.4.2-beta.12 → 1.4.2-beta.17
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/lib/Schema.d.ts +4 -3
- package/lib/Schema.js.map +1 -1
- package/lib/components/AssociatedSelection.js +2 -2
- package/lib/components/AssociatedSelection.js.map +2 -2
- package/lib/components/Checkbox.d.ts +20 -20
- package/lib/components/Collapse.d.ts +51 -23
- package/lib/components/Collapse.js +69 -11
- package/lib/components/Collapse.js.map +2 -2
- package/lib/components/CollapseGroup.d.ts +88 -0
- package/lib/components/CollapseGroup.js +81 -0
- package/lib/components/CollapseGroup.js.map +13 -0
- package/lib/components/Editor.d.ts +84 -84
- package/lib/components/GridNav.d.ts +52 -0
- package/lib/components/GridNav.js +123 -0
- package/lib/components/GridNav.js.map +13 -0
- package/lib/components/InputBox.d.ts +22 -21
- package/lib/components/InputBox.js +10 -2
- package/lib/components/InputBox.js.map +2 -2
- package/lib/components/ListGroup.d.ts +21 -21
- package/lib/components/ResultBox.d.ts +84 -84
- package/lib/components/ResultBox.js +10 -2
- package/lib/components/ResultBox.js.map +2 -2
- package/lib/components/Tabs.d.ts +20 -20
- package/lib/components/TabsTransfer.d.ts +84 -84
- package/lib/components/Toast.d.ts +86 -85
- package/lib/components/Toast.js +6 -3
- package/lib/components/Toast.js.map +2 -2
- package/lib/components/Transfer.d.ts +84 -84
- package/lib/components/TransferDropDown.d.ts +85 -84
- package/lib/components/TransferDropDown.js +2 -2
- package/lib/components/TransferDropDown.js.map +2 -2
- package/lib/components/TransferPicker.d.ts +4 -0
- package/lib/components/TransferPicker.js +2 -2
- package/lib/components/TransferPicker.js.map +2 -2
- package/lib/components/Tree.d.ts +115 -84
- package/lib/components/Tree.js +183 -30
- package/lib/components/Tree.js.map +2 -2
- package/lib/components/icons.js +2 -0
- package/lib/components/icons.js.map +2 -2
- package/lib/envOverwrite.d.ts +1 -1
- package/lib/envOverwrite.js +24 -9
- package/lib/envOverwrite.js.map +2 -2
- package/lib/factory.d.ts +11 -1
- package/lib/factory.js +31 -4
- package/lib/factory.js.map +2 -2
- package/lib/icons/download.js +7 -0
- package/lib/icons/drag-bar.js +10 -3
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/lib/index.js.map +2 -2
- package/lib/locale/en-US.js +1 -0
- package/lib/locale/en-US.js.map +2 -2
- package/lib/locale/zh-CN.js +1 -0
- package/lib/locale/zh-CN.js.map +2 -2
- package/lib/renderers/Collapse.d.ts +25 -20
- package/lib/renderers/Collapse.js +10 -73
- package/lib/renderers/Collapse.js.map +2 -2
- package/lib/renderers/CollapseGroup.d.ts +42 -0
- package/lib/renderers/CollapseGroup.js +33 -0
- package/lib/renderers/CollapseGroup.js.map +13 -0
- package/lib/renderers/Form/InputCity.d.ts +84 -84
- package/lib/renderers/Form/InputFile.d.ts +6 -0
- package/lib/renderers/Form/InputFile.js +18 -4
- package/lib/renderers/Form/InputFile.js.map +2 -2
- package/lib/renderers/Form/Item.js +2 -1
- package/lib/renderers/Form/Item.js.map +2 -2
- package/lib/renderers/Form/Select.d.ts +1 -0
- package/lib/renderers/Form/Select.js +2 -2
- package/lib/renderers/Form/Select.js.map +2 -2
- package/lib/renderers/Form/TransferPicker.d.ts +4 -0
- package/lib/renderers/Form/TransferPicker.js +2 -2
- package/lib/renderers/Form/TransferPicker.js.map +2 -2
- package/lib/renderers/Form/index.js +1 -1
- package/lib/renderers/Form/index.js.map +2 -2
- package/lib/renderers/GridNav.d.ts +99 -0
- package/lib/renderers/GridNav.js +82 -0
- package/lib/renderers/GridNav.js.map +13 -0
- package/lib/store/formItem.js +60 -4
- package/lib/store/formItem.js.map +2 -2
- package/lib/store/table.js +5 -5
- package/lib/store/table.js.map +2 -2
- package/lib/themes/ang-ie11.css +224 -31
- package/lib/themes/ang.css +224 -31
- package/lib/themes/ang.css.map +1 -1
- package/lib/themes/antd-ie11.css +224 -31
- package/lib/themes/antd.css +224 -31
- package/lib/themes/antd.css.map +1 -1
- package/lib/themes/cxd-ie11.css +231 -38
- package/lib/themes/cxd.css +231 -38
- package/lib/themes/cxd.css.map +1 -1
- package/lib/themes/dark-ie11.css +224 -31
- package/lib/themes/dark.css +224 -31
- package/lib/themes/dark.css.map +1 -1
- package/lib/themes/default.css +231 -38
- package/lib/themes/default.css.map +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/types.js.map +1 -1
- package/lib/utils/api.d.ts +1 -0
- package/lib/utils/api.js +77 -6
- package/lib/utils/api.js.map +2 -2
- package/lib/utils/helper.d.ts +6 -0
- package/lib/utils/helper.js +18 -1
- package/lib/utils/helper.js.map +2 -2
- package/package.json +1 -1
- package/schema.json +278 -48
- package/scss/base/_common.scss +3 -0
- package/scss/components/_anchor-nav.scss +1 -0
- package/scss/components/_collapse-group.scss +11 -0
- package/scss/components/_collapse.scss +33 -22
- package/scss/components/_grid-nav.scss +128 -0
- package/scss/components/_input-box.scss +1 -0
- package/scss/components/_nav.scss +1 -1
- package/scss/components/_picker-columns.scss +1 -0
- package/scss/components/_popover.scss +0 -4
- package/scss/components/_result-box.scss +1 -0
- package/scss/components/_spinner.scss +5 -4
- package/scss/components/_table.scss +6 -0
- package/scss/components/form/_combo.scss +4 -0
- package/scss/components/form/_file.scss +11 -0
- package/scss/components/form/_tree.scss +42 -0
- package/scss/themes/_common.scss +3 -0
- package/scss/themes/_cxd-variables.scss +6 -7
- package/scss/themes/cxd.scss +1 -0
- package/sdk/ang-ie11.css +268 -32
- package/sdk/ang.css +263 -31
- package/sdk/antd-ie11.css +268 -32
- package/sdk/antd.css +263 -31
- package/sdk/charts.js +15 -15
- package/sdk/color-picker.js +65 -65
- package/sdk/cropperjs.js +2 -2
- package/sdk/cxd-ie11.css +274 -36
- package/sdk/cxd.css +270 -38
- package/sdk/dark-ie11.css +268 -32
- package/sdk/dark.css +263 -31
- package/sdk/exceljs.js +1 -1
- package/sdk/markdown.js +69 -69
- package/sdk/papaparse.js +1 -1
- package/sdk/renderers/Form/CityDB.js +1 -1
- package/sdk/rest.js +18 -18
- package/sdk/rich-text.js +62 -62
- package/sdk/sdk-ie11.css +274 -36
- package/sdk/sdk.css +270 -38
- package/sdk/sdk.js +1179 -1143
- package/sdk/thirds/hls.js/hls.js +1 -1
- package/sdk/thirds/mpegts.js/mpegts.js +1 -1
- package/sdk/tinymce.js +57 -57
- package/src/Schema.ts +5 -1
- package/src/components/AssociatedSelection.tsx +3 -1
- package/src/components/Collapse.tsx +144 -20
- package/src/components/CollapseGroup.tsx +130 -0
- package/src/components/GridNav.tsx +233 -0
- package/src/components/InputBox.tsx +10 -9
- package/src/components/ResultBox.tsx +9 -9
- package/src/components/Toast.tsx +23 -16
- package/src/components/TransferDropDown.tsx +4 -1
- package/src/components/TransferPicker.tsx +7 -0
- package/src/components/Tree.tsx +194 -8
- package/src/components/icons.tsx +2 -0
- package/src/envOverwrite.ts +20 -7
- package/src/factory.tsx +52 -6
- package/src/icons/download.svg +4 -0
- package/src/icons/drag-bar.svg +12 -6
- package/src/index.tsx +2 -0
- package/src/locale/en-US.ts +1 -0
- package/src/locale/zh-CN.ts +1 -0
- package/src/renderers/Collapse.tsx +70 -117
- package/src/renderers/CollapseGroup.tsx +80 -0
- package/src/renderers/Form/InputFile.tsx +36 -4
- package/src/renderers/Form/Item.tsx +2 -1
- package/src/renderers/Form/Select.tsx +6 -2
- package/src/renderers/Form/TransferPicker.tsx +7 -1
- package/src/renderers/Form/index.tsx +1 -2
- package/src/renderers/GridNav.tsx +204 -0
- package/src/store/formItem.ts +116 -3
- package/src/store/table.ts +9 -5
- package/src/types.ts +1 -1
- package/src/utils/api.ts +93 -6
- package/src/utils/helper.ts +19 -0
- package/tsconfig-for-declaration.json +1 -1
@@ -0,0 +1,204 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import {Renderer, RendererProps} from '../factory';
|
3
|
+
import {autobind, getPropValue} from '../utils/helper';
|
4
|
+
import {isPureVariable, resolveVariableAndFilter} from '../utils/tpl-builtin';
|
5
|
+
import {
|
6
|
+
BaseSchema,
|
7
|
+
SchemaTokenizeableString,
|
8
|
+
SchemaTpl,
|
9
|
+
SchemaUrlPath
|
10
|
+
} from '../Schema';
|
11
|
+
import {ActionSchema} from './Action';
|
12
|
+
import GridNav, {GridNavDirection, GridNavItem} from '../components/GridNav';
|
13
|
+
import {BadgeSchema} from '../components/Badge';
|
14
|
+
import handleAction from '../utils/handleAction';
|
15
|
+
import {validations} from '../utils/validations';
|
16
|
+
|
17
|
+
export interface ListItemSchema extends Omit<BaseSchema, 'type'> {
|
18
|
+
/**
|
19
|
+
* 单项点击事件
|
20
|
+
*/
|
21
|
+
clickAction?: ActionSchema;
|
22
|
+
|
23
|
+
/**
|
24
|
+
* 跳转地址
|
25
|
+
*/
|
26
|
+
link?: string;
|
27
|
+
|
28
|
+
/**
|
29
|
+
* 打开方式
|
30
|
+
*/
|
31
|
+
blank?: string;
|
32
|
+
|
33
|
+
/**
|
34
|
+
* 图片地址
|
35
|
+
*/
|
36
|
+
icon?: SchemaUrlPath;
|
37
|
+
|
38
|
+
/**
|
39
|
+
* 描述
|
40
|
+
*/
|
41
|
+
text?: SchemaTpl;
|
42
|
+
|
43
|
+
/**
|
44
|
+
* 图标最大宽度比例 0-100
|
45
|
+
*/
|
46
|
+
iconRatio?: number;
|
47
|
+
|
48
|
+
/**
|
49
|
+
* 角标
|
50
|
+
*/
|
51
|
+
badge?: BadgeSchema;
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* List 列表展示控件。
|
56
|
+
* 文档:https://baidu.gitee.io/amis/docs/components/card
|
57
|
+
*/
|
58
|
+
export interface ListSchema extends BaseSchema {
|
59
|
+
/**
|
60
|
+
* 指定为 List 列表展示控件。
|
61
|
+
*/
|
62
|
+
type: 'grid-nav';
|
63
|
+
|
64
|
+
/**
|
65
|
+
* 列表项类名
|
66
|
+
*/
|
67
|
+
itemClassName?: string;
|
68
|
+
|
69
|
+
/**
|
70
|
+
* 静态图片列表配置
|
71
|
+
*/
|
72
|
+
options?: Array<ListItemSchema>;
|
73
|
+
|
74
|
+
/**
|
75
|
+
* 是否将列表项固定为正方形
|
76
|
+
*/
|
77
|
+
square?: boolean;
|
78
|
+
|
79
|
+
/**
|
80
|
+
* 是否将列表项内容居中显示
|
81
|
+
*/
|
82
|
+
center?: boolean;
|
83
|
+
|
84
|
+
/**
|
85
|
+
* 是否显示列表项边框
|
86
|
+
*/
|
87
|
+
border?: boolean;
|
88
|
+
|
89
|
+
/**
|
90
|
+
* 列表项之间的间距,默认单位为px
|
91
|
+
*/
|
92
|
+
gutter?: number;
|
93
|
+
|
94
|
+
/**
|
95
|
+
* 图标宽度占比, 1-100
|
96
|
+
*/
|
97
|
+
iconRatio?: number;
|
98
|
+
|
99
|
+
/**
|
100
|
+
* 列表项内容排列的方向,可选值为 horizontal 、vertical
|
101
|
+
*/
|
102
|
+
direction?: GridNavDirection;
|
103
|
+
|
104
|
+
/**
|
105
|
+
* 列数
|
106
|
+
*/
|
107
|
+
columnNum?: number;
|
108
|
+
|
109
|
+
/**
|
110
|
+
* 数据源: 绑定当前环境变量
|
111
|
+
*
|
112
|
+
* @default ${items}
|
113
|
+
*/
|
114
|
+
source?: SchemaTokenizeableString;
|
115
|
+
}
|
116
|
+
|
117
|
+
export interface Column {
|
118
|
+
type: string;
|
119
|
+
[propName: string]: any;
|
120
|
+
}
|
121
|
+
|
122
|
+
export interface ListProps
|
123
|
+
extends RendererProps,
|
124
|
+
Omit<ListSchema, 'type' | 'className'> {
|
125
|
+
handleClick: (item?: ListItemSchema) => void;
|
126
|
+
}
|
127
|
+
|
128
|
+
@Renderer({
|
129
|
+
type: 'grid-nav'
|
130
|
+
})
|
131
|
+
export default class List extends React.Component<ListProps, object> {
|
132
|
+
@autobind
|
133
|
+
handleClick(item: ListItemSchema) {
|
134
|
+
return (e: React.MouseEvent) => {
|
135
|
+
let action;
|
136
|
+
if (item.link) {
|
137
|
+
action = validations.isUrl({}, item.link)
|
138
|
+
? {
|
139
|
+
type: 'button',
|
140
|
+
actionType: 'url',
|
141
|
+
url: item.link,
|
142
|
+
blank: item.blank
|
143
|
+
}
|
144
|
+
: {
|
145
|
+
type: 'button',
|
146
|
+
actionType: 'link',
|
147
|
+
link: item.link
|
148
|
+
};
|
149
|
+
} else {
|
150
|
+
action = item.clickAction!;
|
151
|
+
}
|
152
|
+
handleAction(e, action as ActionSchema, this.props);
|
153
|
+
};
|
154
|
+
}
|
155
|
+
|
156
|
+
render() {
|
157
|
+
const {itemClassName, source, data, options, classnames} = this.props;
|
158
|
+
|
159
|
+
let value = getPropValue(this.props);
|
160
|
+
let list: any = [];
|
161
|
+
|
162
|
+
if (typeof source === 'string' && isPureVariable(source)) {
|
163
|
+
list = resolveVariableAndFilter(source, data, '| raw') || undefined;
|
164
|
+
} else if (Array.isArray(value)) {
|
165
|
+
list = value;
|
166
|
+
} else if (Array.isArray(options)) {
|
167
|
+
list = options;
|
168
|
+
}
|
169
|
+
|
170
|
+
if (list && !Array.isArray(list)) {
|
171
|
+
list = [list];
|
172
|
+
}
|
173
|
+
|
174
|
+
if (!list?.length) {
|
175
|
+
return null;
|
176
|
+
}
|
177
|
+
|
178
|
+
return (
|
179
|
+
<GridNav {...this.props}>
|
180
|
+
{list.map((item: ListItemSchema, index: number) => (
|
181
|
+
<GridNavItem
|
182
|
+
key={index}
|
183
|
+
onClick={
|
184
|
+
item.clickAction || item.link ? this.handleClick(item) : undefined
|
185
|
+
}
|
186
|
+
className={itemClassName}
|
187
|
+
text={item.text}
|
188
|
+
icon={item.icon}
|
189
|
+
classnames={classnames}
|
190
|
+
badge={
|
191
|
+
item.badge
|
192
|
+
? {
|
193
|
+
badge: item.badge,
|
194
|
+
data: data,
|
195
|
+
classnames
|
196
|
+
}
|
197
|
+
: undefined
|
198
|
+
}
|
199
|
+
/>
|
200
|
+
))}
|
201
|
+
</GridNav>
|
202
|
+
);
|
203
|
+
}
|
204
|
+
}
|
package/src/store/formItem.ts
CHANGED
@@ -25,7 +25,8 @@ import {
|
|
25
25
|
spliceTree,
|
26
26
|
isEmpty,
|
27
27
|
getTreeAncestors,
|
28
|
-
filterTree
|
28
|
+
filterTree,
|
29
|
+
eachTree
|
29
30
|
} from '../utils/helper';
|
30
31
|
import {flattenTree} from '../utils/helper';
|
31
32
|
import {IRendererStore} from '.';
|
@@ -617,12 +618,124 @@ export const FormItemStore = StoreNode.named('FormItemStore')
|
|
617
618
|
return json;
|
618
619
|
});
|
619
620
|
|
621
|
+
const tryDeferLoadLeftOptions: (
|
622
|
+
option: any,
|
623
|
+
api: Api,
|
624
|
+
data?: object,
|
625
|
+
config?: fetchOptions
|
626
|
+
) => Promise<Payload | null> = flow(function* (
|
627
|
+
option: any,
|
628
|
+
api: string,
|
629
|
+
data: object,
|
630
|
+
config?: fetchOptions
|
631
|
+
) {
|
632
|
+
if (
|
633
|
+
self.options.length != 1 ||
|
634
|
+
!Array.isArray(self.options[0].leftOptions)
|
635
|
+
) {
|
636
|
+
return;
|
637
|
+
}
|
638
|
+
|
639
|
+
let leftOptions = self.options[0].leftOptions as any;
|
640
|
+
|
641
|
+
const indexes = findTreeIndex(leftOptions, item => item === option);
|
642
|
+
if (!indexes) {
|
643
|
+
return;
|
644
|
+
}
|
645
|
+
|
646
|
+
setOptions(
|
647
|
+
[
|
648
|
+
{
|
649
|
+
...self.options[0],
|
650
|
+
leftOptions: spliceTree(leftOptions, indexes, 1, {
|
651
|
+
...option,
|
652
|
+
loading: true
|
653
|
+
})
|
654
|
+
}
|
655
|
+
],
|
656
|
+
undefined,
|
657
|
+
data
|
658
|
+
);
|
659
|
+
|
660
|
+
let json = yield fetchOptions(
|
661
|
+
api,
|
662
|
+
data,
|
663
|
+
{
|
664
|
+
...config,
|
665
|
+
silent: true
|
666
|
+
},
|
667
|
+
false
|
668
|
+
);
|
669
|
+
if (!json) {
|
670
|
+
setOptions(
|
671
|
+
[
|
672
|
+
{
|
673
|
+
...self.options[0],
|
674
|
+
leftOptions: spliceTree(leftOptions, indexes, 1, {
|
675
|
+
...option,
|
676
|
+
loading: false,
|
677
|
+
error: true
|
678
|
+
})
|
679
|
+
}
|
680
|
+
],
|
681
|
+
undefined,
|
682
|
+
data
|
683
|
+
);
|
684
|
+
return;
|
685
|
+
}
|
686
|
+
|
687
|
+
let options: Array<IOption> =
|
688
|
+
json.data?.options ||
|
689
|
+
json.data.items ||
|
690
|
+
json.data.rows ||
|
691
|
+
json.data ||
|
692
|
+
[];
|
693
|
+
|
694
|
+
const children = Array.isArray(self.options[0].children)
|
695
|
+
? self.options[0].children.concat()
|
696
|
+
: [];
|
697
|
+
|
698
|
+
eachTree(options as any, item => {
|
699
|
+
if (!item.value) {
|
700
|
+
return;
|
701
|
+
}
|
702
|
+
|
703
|
+
const exits = children.find((a: any) => a.ref === item.value);
|
704
|
+
if (!exits) {
|
705
|
+
children.push({
|
706
|
+
ref: item.value,
|
707
|
+
children: [],
|
708
|
+
defer: true
|
709
|
+
});
|
710
|
+
}
|
711
|
+
});
|
712
|
+
|
713
|
+
setOptions(
|
714
|
+
[
|
715
|
+
{
|
716
|
+
...self.options[0],
|
717
|
+
children,
|
718
|
+
leftOptions: spliceTree(leftOptions, indexes, 1, {
|
719
|
+
...option,
|
720
|
+
loading: false,
|
721
|
+
loaded: true,
|
722
|
+
children: options
|
723
|
+
})
|
724
|
+
}
|
725
|
+
],
|
726
|
+
undefined,
|
727
|
+
data
|
728
|
+
);
|
729
|
+
|
730
|
+
return json;
|
731
|
+
});
|
732
|
+
|
620
733
|
const deferLoadOptions: (
|
621
734
|
option: any,
|
622
735
|
api: Api,
|
623
736
|
data?: object,
|
624
737
|
config?: fetchOptions
|
625
|
-
) => Promise<Payload | null> = flow(function*
|
738
|
+
) => Promise<Payload | null> = flow(function* (
|
626
739
|
option: any,
|
627
740
|
api: string,
|
628
741
|
data: object,
|
@@ -630,7 +743,7 @@ export const FormItemStore = StoreNode.named('FormItemStore')
|
|
630
743
|
) {
|
631
744
|
const indexes = findTreeIndex(self.options, item => item === option);
|
632
745
|
if (!indexes) {
|
633
|
-
return;
|
746
|
+
return yield tryDeferLoadLeftOptions(option, api, data, config);
|
634
747
|
}
|
635
748
|
|
636
749
|
setOptions(
|
package/src/store/table.ts
CHANGED
@@ -429,7 +429,7 @@ export const TableStore = iRendererStore
|
|
429
429
|
}
|
430
430
|
|
431
431
|
function getUnSelectedRows() {
|
432
|
-
return self.rows.filter(item => !item.checked);
|
432
|
+
return flattenTree<IRow>(self.rows).filter((item: IRow) => !item.checked);
|
433
433
|
}
|
434
434
|
|
435
435
|
function getData(superData: any): any {
|
@@ -604,7 +604,9 @@ export const TableStore = iRendererStore
|
|
604
604
|
},
|
605
605
|
|
606
606
|
get checkableRows() {
|
607
|
-
return self.rows.filter(
|
607
|
+
return flattenTree<IRow>(self.rows).filter(
|
608
|
+
(item: IRow) => item.checkable
|
609
|
+
);
|
608
610
|
},
|
609
611
|
|
610
612
|
get expandableRows() {
|
@@ -1023,9 +1025,10 @@ export const TableStore = iRendererStore
|
|
1023
1025
|
|
1024
1026
|
function updateSelected(selected: Array<any>, valueField?: string) {
|
1025
1027
|
self.selectedRows.clear();
|
1026
|
-
|
1028
|
+
|
1029
|
+
eachTree(self.rows, item => {
|
1027
1030
|
if (~selected.indexOf(item.pristine)) {
|
1028
|
-
self.selectedRows.push(item);
|
1031
|
+
self.selectedRows.push(item.id);
|
1029
1032
|
} else if (
|
1030
1033
|
find(
|
1031
1034
|
selected,
|
@@ -1034,9 +1037,10 @@ export const TableStore = iRendererStore
|
|
1034
1037
|
a[valueField || 'value'] == item.pristine[valueField || 'value']
|
1035
1038
|
)
|
1036
1039
|
) {
|
1037
|
-
self.selectedRows.push(item);
|
1040
|
+
self.selectedRows.push(item.id);
|
1038
1041
|
}
|
1039
1042
|
});
|
1043
|
+
|
1040
1044
|
updateCheckDisable();
|
1041
1045
|
}
|
1042
1046
|
|
package/src/types.ts
CHANGED
@@ -31,7 +31,7 @@ export interface fetcherResult {
|
|
31
31
|
}
|
32
32
|
|
33
33
|
export interface fetchOptions {
|
34
|
-
method?: 'get' | 'post' | 'put' | 'patch' | 'delete';
|
34
|
+
method?: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'jsonp';
|
35
35
|
successMessage?: string;
|
36
36
|
errorMessage?: string;
|
37
37
|
autoAppend?: boolean;
|
package/src/utils/api.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import omit from 'lodash/omit';
|
2
2
|
import {Api, ApiObject, EventTrack, fetcherResult, Payload} from '../types';
|
3
3
|
import {fetcherConfig} from '../factory';
|
4
|
-
import {tokenize, dataMapping} from './tpl-builtin';
|
4
|
+
import {tokenize, dataMapping, escapeHtml} from './tpl-builtin';
|
5
5
|
import {evalExpression} from './tpl';
|
6
6
|
import {
|
7
7
|
isObject,
|
@@ -11,11 +11,12 @@ import {
|
|
11
11
|
qsstringify,
|
12
12
|
cloneObject,
|
13
13
|
createObject,
|
14
|
-
qsparse
|
14
|
+
qsparse,
|
15
|
+
uuid
|
15
16
|
} from './helper';
|
16
17
|
import isPlainObject from 'lodash/isPlainObject';
|
17
18
|
|
18
|
-
const rSchema = /(?:^|raw\:)(get|post|put|delete|patch|options|head):/i;
|
19
|
+
const rSchema = /(?:^|raw\:)(get|post|put|delete|patch|options|head|jsonp):/i;
|
19
20
|
|
20
21
|
interface ApiCacheConfig extends ApiObject {
|
21
22
|
cachedPromise: Promise<any>;
|
@@ -125,7 +126,7 @@ export function buildApi(
|
|
125
126
|
}
|
126
127
|
|
127
128
|
// get 类请求,把 data 附带到 url 上。
|
128
|
-
if (api.method === 'get') {
|
129
|
+
if (api.method === 'get' || api.method === 'jsonp') {
|
129
130
|
if (!~raw.indexOf('$') && !api.data && autoAppend) {
|
130
131
|
api.query = api.data = data;
|
131
132
|
} else if (
|
@@ -195,13 +196,28 @@ export function str2AsyncFunction(
|
|
195
196
|
}
|
196
197
|
|
197
198
|
export function responseAdaptor(ret: fetcherResult, api: ApiObject) {
|
198
|
-
|
199
|
+
let data = ret.data;
|
199
200
|
let hasStatusField = true;
|
200
201
|
|
201
202
|
if (!data) {
|
202
203
|
throw new Error('Response is empty');
|
203
204
|
}
|
204
205
|
|
206
|
+
if (typeof data === 'string') {
|
207
|
+
try {
|
208
|
+
data = JSON.parse(data);
|
209
|
+
if (typeof data === 'undefined') {
|
210
|
+
throw new Error('Response should be JSON');
|
211
|
+
}
|
212
|
+
} catch (e) {
|
213
|
+
const responseBrief =
|
214
|
+
typeof data === 'string'
|
215
|
+
? escapeHtml((data as string).substring(0, 100))
|
216
|
+
: '';
|
217
|
+
throw new Error(`Response should be JSON\n ${responseBrief}`);
|
218
|
+
}
|
219
|
+
}
|
220
|
+
|
205
221
|
// 兼容几种常见写法
|
206
222
|
if (data.hasOwnProperty('errorCode')) {
|
207
223
|
// 阿里 Java 规范
|
@@ -247,7 +263,7 @@ export function responseAdaptor(ret: fetcherResult, api: ApiObject) {
|
|
247
263
|
}
|
248
264
|
|
249
265
|
if (payload.ok && api.responseData) {
|
250
|
-
|
266
|
+
const responseData = dataMapping(
|
251
267
|
api.responseData,
|
252
268
|
|
253
269
|
createObject(
|
@@ -261,6 +277,8 @@ export function responseAdaptor(ret: fetcherResult, api: ApiObject) {
|
|
261
277
|
undefined,
|
262
278
|
api.convertKeyToPath
|
263
279
|
);
|
280
|
+
console.debug('responseData', responseData);
|
281
|
+
payload.data = responseData;
|
264
282
|
}
|
265
283
|
|
266
284
|
return payload;
|
@@ -303,6 +321,10 @@ export function wrapFetcher(
|
|
303
321
|
api.data
|
304
322
|
);
|
305
323
|
|
324
|
+
if (api.method?.toLocaleLowerCase() === 'jsonp') {
|
325
|
+
return wrapAdaptor(jsonpFetcher(api), api);
|
326
|
+
}
|
327
|
+
|
306
328
|
if (typeof api.cache === 'number' && api.cache > 0) {
|
307
329
|
const apiCache = getApiCache(api);
|
308
330
|
return wrapAdaptor(
|
@@ -345,6 +367,71 @@ export function wrapAdaptor(promise: Promise<fetcherResult>, api: ApiObject) {
|
|
345
367
|
: promise.then(ret => responseAdaptor(ret, api));
|
346
368
|
}
|
347
369
|
|
370
|
+
export function jsonpFetcher(api: ApiObject): Promise<fetcherResult> {
|
371
|
+
return new Promise((resolve, reject) => {
|
372
|
+
let script: HTMLScriptElement | null = document.createElement('script');
|
373
|
+
let src = api.url;
|
374
|
+
|
375
|
+
script.async = true;
|
376
|
+
|
377
|
+
function remove() {
|
378
|
+
if (script) {
|
379
|
+
// @ts-ignore
|
380
|
+
script.onload = script.onreadystatechange = script.onerror = null;
|
381
|
+
|
382
|
+
if (script.parentNode) {
|
383
|
+
script.parentNode.removeChild(script);
|
384
|
+
}
|
385
|
+
|
386
|
+
script = null;
|
387
|
+
}
|
388
|
+
}
|
389
|
+
|
390
|
+
const jsonp = api.query?.callback || 'axiosJsonpCallback' + uuid();
|
391
|
+
const old = (window as any)[jsonp];
|
392
|
+
|
393
|
+
(window as any)[jsonp] = function (responseData: any) {
|
394
|
+
(window as any)[jsonp] = old;
|
395
|
+
|
396
|
+
const response = {
|
397
|
+
data: responseData,
|
398
|
+
status: 200,
|
399
|
+
headers: {}
|
400
|
+
};
|
401
|
+
|
402
|
+
resolve(response);
|
403
|
+
};
|
404
|
+
|
405
|
+
const additionalParams: any = {
|
406
|
+
_: new Date().getTime(),
|
407
|
+
_callback: jsonp
|
408
|
+
};
|
409
|
+
|
410
|
+
src += (src.indexOf('?') >= 0 ? '&' : '?') + qsstringify(additionalParams);
|
411
|
+
|
412
|
+
// @ts-ignore IE 为script.onreadystatechange
|
413
|
+
script.onload = script.onreadystatechange = function () {
|
414
|
+
// @ts-ignore
|
415
|
+
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
|
416
|
+
remove();
|
417
|
+
}
|
418
|
+
};
|
419
|
+
|
420
|
+
script.onerror = function () {
|
421
|
+
remove();
|
422
|
+
const errResponse = {
|
423
|
+
status: 0,
|
424
|
+
headers: {}
|
425
|
+
};
|
426
|
+
|
427
|
+
reject(errResponse);
|
428
|
+
};
|
429
|
+
|
430
|
+
script.src = src;
|
431
|
+
document.head.appendChild(script);
|
432
|
+
});
|
433
|
+
}
|
434
|
+
|
348
435
|
export function isApiOutdated(
|
349
436
|
prevApi: Api | undefined,
|
350
437
|
nextApi: Api | undefined,
|
package/src/utils/helper.ts
CHANGED
@@ -1706,3 +1706,22 @@ export function isClickOnInput(e: React.MouseEvent<HTMLElement>) {
|
|
1706
1706
|
}
|
1707
1707
|
return false;
|
1708
1708
|
}
|
1709
|
+
|
1710
|
+
/**
|
1711
|
+
* 遍历 schema
|
1712
|
+
* @param json
|
1713
|
+
* @param mapper
|
1714
|
+
*/
|
1715
|
+
export function JSONTraverse(
|
1716
|
+
json: any,
|
1717
|
+
mapper: (value: any, key: string | number, host: Object) => any
|
1718
|
+
) {
|
1719
|
+
Object.keys(json).forEach(key => {
|
1720
|
+
const value: any = json[key];
|
1721
|
+
if (isPlainObject(value) || Array.isArray(value)) {
|
1722
|
+
JSONTraverse(value, mapper);
|
1723
|
+
} else {
|
1724
|
+
mapper(value, key, json);
|
1725
|
+
}
|
1726
|
+
});
|
1727
|
+
}
|