@whitesev/pops 1.4.0 → 1.5.0
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/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +50 -48
- package/src/Config.ts +3 -0
- package/src/Core.ts +45 -0
- package/src/Pops.ts +340 -0
- package/src/components/alert/index.css +45 -0
- package/src/components/alert/index.ts +216 -0
- package/src/components/alert/indexType.ts +30 -0
- package/src/components/confirm/index.css +45 -0
- package/src/components/confirm/index.ts +264 -0
- package/src/components/confirm/indexType.ts +17 -0
- package/src/components/drawer/index.css +47 -0
- package/src/components/drawer/index.ts +338 -0
- package/src/components/drawer/indexType.ts +53 -0
- package/src/components/folder/folderIcon.ts +28 -0
- package/src/components/folder/index.css +291 -0
- package/src/components/folder/index.ts +1111 -0
- package/src/components/folder/indexType.ts +87 -0
- package/src/components/iframe/index.css +90 -0
- package/src/components/iframe/index.ts +415 -0
- package/src/components/iframe/indexType.ts +144 -0
- package/src/components/loading/index.css +60 -0
- package/src/components/loading/index.ts +123 -0
- package/src/components/loading/indexType.ts +31 -0
- package/src/components/panel/buttonType.ts +60 -0
- package/src/components/panel/commonType.ts +50 -0
- package/src/components/panel/deepMenuType.ts +59 -0
- package/src/components/panel/formsType.ts +32 -0
- package/src/components/panel/index.css +754 -0
- package/src/components/panel/index.ts +2435 -0
- package/src/components/panel/indexType.ts +107 -0
- package/src/components/panel/inputType.ts +65 -0
- package/src/components/panel/ownType.ts +28 -0
- package/src/components/panel/selectType.ts +80 -0
- package/src/components/panel/sliderType.ts +59 -0
- package/src/components/panel/switchType.ts +43 -0
- package/src/components/panel/textareaType.ts +54 -0
- package/src/components/prompt/index.css +60 -0
- package/src/components/prompt/index.ts +293 -0
- package/src/components/prompt/indexType.ts +47 -0
- package/src/components/rightClickMenu/index.ts +729 -0
- package/src/components/rightClickMenu/indexType.ts +89 -0
- package/src/components/searchSuggestion/index.css +0 -0
- package/src/components/searchSuggestion/index.ts +656 -0
- package/src/components/searchSuggestion/indexType.ts +238 -0
- package/src/components/tooltip/index.css +171 -0
- package/src/components/tooltip/index.ts +358 -0
- package/src/components/tooltip/indexType.ts +95 -0
- package/src/css/animation.css +2240 -0
- package/src/css/button.css +290 -0
- package/src/css/common.css +24 -0
- package/src/css/index.css +135 -0
- package/src/css/ninePalaceGridPosition.css +50 -0
- package/src/css/scrollbar.css +18 -0
- package/src/handler/PopsElementHandler.ts +353 -0
- package/src/handler/PopsHandler.ts +659 -0
- package/src/svg/arrowLeft.svg +4 -0
- package/src/svg/arrowRight.svg +4 -0
- package/src/svg/chromeFilled.svg +14 -0
- package/src/svg/circleClose.svg +8 -0
- package/src/svg/close.svg +5 -0
- package/src/svg/cpu.svg +8 -0
- package/src/svg/delete.svg +5 -0
- package/src/svg/documentCopy.svg +5 -0
- package/src/svg/edit.svg +8 -0
- package/src/svg/eleme.svg +5 -0
- package/src/svg/elemePlus.svg +5 -0
- package/src/svg/headset.svg +5 -0
- package/src/svg/hide.svg +8 -0
- package/src/svg/keyboard.svg +8 -0
- package/src/svg/loading.svg +5 -0
- package/src/svg/max.svg +5 -0
- package/src/svg/min.svg +5 -0
- package/src/svg/mise.svg +5 -0
- package/src/svg/monitor.svg +5 -0
- package/src/svg/next.svg +5 -0
- package/src/svg/picture.svg +8 -0
- package/src/svg/prev.svg +5 -0
- package/src/svg/search.svg +5 -0
- package/src/svg/share.svg +5 -0
- package/src/svg/upload.svg +5 -0
- package/src/svg/videoPause.svg +5 -0
- package/src/svg/videoPlay.svg +5 -0
- package/src/svg/view.svg +5 -0
- package/src/types/PopsDOMUtilsEventType.d.ts +246 -0
- package/src/types/animation.d.ts +19 -0
- package/src/types/button.d.ts +226 -0
- package/src/types/components.d.ts +197 -0
- package/src/types/event.d.ts +62 -0
- package/src/types/global.d.ts +11 -0
- package/src/types/icon.d.ts +32 -0
- package/src/types/layer.d.ts +20 -0
- package/src/types/main.d.ts +136 -0
- package/src/types/mask.d.ts +35 -0
- package/src/types/position.d.ts +60 -0
- package/src/utils/AnyTouch.js +1394 -0
- package/src/utils/PopsDOMUtils.ts +2013 -0
- package/src/utils/PopsInstanceUtils.ts +685 -0
- package/src/utils/PopsMathUtils.ts +77 -0
- package/src/utils/PopsUtils.ts +380 -0
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import { PopsHandler } from "../../handler/PopsHandler";
|
|
2
|
+
import { pops } from "../../Pops";
|
|
3
|
+
import { popsDOMUtils } from "../../utils/PopsDOMUtils";
|
|
4
|
+
import { popsUtils } from "../../utils/PopsUtils";
|
|
5
|
+
import type { PopsSearchSuggestionDetails } from "./indexType";
|
|
6
|
+
|
|
7
|
+
export class PopsSearchSuggestion {
|
|
8
|
+
constructor(details: PopsSearchSuggestionDetails) {
|
|
9
|
+
const { $shadowContainer, $shadowRoot } = PopsHandler.handlerShadow();
|
|
10
|
+
PopsHandler.handleInit($shadowRoot, [
|
|
11
|
+
pops.config.cssText.index,
|
|
12
|
+
pops.config.cssText.anim,
|
|
13
|
+
pops.config.cssText.common,
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
let config: Required<PopsSearchSuggestionDetails> = {
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
target: null,
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
inputTarget: null,
|
|
21
|
+
selfDocument: document,
|
|
22
|
+
data: [
|
|
23
|
+
{
|
|
24
|
+
value: "数据1",
|
|
25
|
+
text: "数据1-html",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
value: "数据2",
|
|
29
|
+
text: "数据2-html",
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
deleteIcon: {
|
|
33
|
+
enable: true,
|
|
34
|
+
callback(event, liElement, data) {
|
|
35
|
+
console.log("删除当前项", [event, liElement, data]);
|
|
36
|
+
liElement.remove();
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
className: "",
|
|
40
|
+
isAbsolute: true,
|
|
41
|
+
isAnimation: true,
|
|
42
|
+
width: "250px",
|
|
43
|
+
maxHeight: "300px",
|
|
44
|
+
followTargetWidth: true,
|
|
45
|
+
topDistance: 0,
|
|
46
|
+
position: "auto",
|
|
47
|
+
positionTopToReverse: true,
|
|
48
|
+
zIndex: 10000,
|
|
49
|
+
searchingTip: "正在搜索中...",
|
|
50
|
+
toSearhNotResultHTML: '<li data-none="true">暂无其它数据</li>',
|
|
51
|
+
toHideWithNotResult: false,
|
|
52
|
+
followPosition: "target",
|
|
53
|
+
getItemHTML(item) {
|
|
54
|
+
return item.text ?? item;
|
|
55
|
+
},
|
|
56
|
+
async getData(value) {
|
|
57
|
+
console.log("当前输入框的值是:", value);
|
|
58
|
+
return [];
|
|
59
|
+
},
|
|
60
|
+
itemClickCallBack(event, liElement, data) {
|
|
61
|
+
console.log("item项的点击回调", [event, liElement, data]);
|
|
62
|
+
this.inputTarget.value = data.value;
|
|
63
|
+
},
|
|
64
|
+
selectCallBack(event, liElement, data) {
|
|
65
|
+
console.log("item项的选中回调", [event, liElement, data]);
|
|
66
|
+
},
|
|
67
|
+
style: "",
|
|
68
|
+
};
|
|
69
|
+
config = popsUtils.assign(config, details);
|
|
70
|
+
if (config.target == null) {
|
|
71
|
+
throw new TypeError("config.target 不能为空");
|
|
72
|
+
}
|
|
73
|
+
/* 做下兼容处理 */
|
|
74
|
+
if (config.inputTarget == null) {
|
|
75
|
+
config.inputTarget = config.target as HTMLInputElement;
|
|
76
|
+
}
|
|
77
|
+
if (details.data) {
|
|
78
|
+
config.data = details.data;
|
|
79
|
+
}
|
|
80
|
+
const guid = popsUtils.getRandomGUID();
|
|
81
|
+
const PopsType = "searchSuggestion";
|
|
82
|
+
|
|
83
|
+
if (config.style != null) {
|
|
84
|
+
let cssNode = document.createElement("style");
|
|
85
|
+
cssNode.setAttribute("type", "text/css");
|
|
86
|
+
cssNode.innerHTML = config.style;
|
|
87
|
+
$shadowRoot.appendChild(cssNode);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const SearchSuggestion = {
|
|
91
|
+
/**
|
|
92
|
+
* 当前的环境,可以是document,可以是shadowroot,默认是document
|
|
93
|
+
*/
|
|
94
|
+
selfDocument: config.selfDocument,
|
|
95
|
+
/**
|
|
96
|
+
* 根元素
|
|
97
|
+
*/
|
|
98
|
+
root: null as any as HTMLElement,
|
|
99
|
+
/**
|
|
100
|
+
* ul元素
|
|
101
|
+
*/
|
|
102
|
+
hintULElement: null as any as HTMLUListElement,
|
|
103
|
+
$data: {
|
|
104
|
+
/** 是否结果为空 */
|
|
105
|
+
isEmpty: true,
|
|
106
|
+
},
|
|
107
|
+
/**
|
|
108
|
+
* 初始化
|
|
109
|
+
*/
|
|
110
|
+
init(parentElement = document.body || document.documentElement) {
|
|
111
|
+
SearchSuggestion.root = SearchSuggestion.getSearchSelectElement();
|
|
112
|
+
SearchSuggestion.hintULElement =
|
|
113
|
+
SearchSuggestion.root.querySelector<HTMLUListElement>("ul")!;
|
|
114
|
+
SearchSuggestion.update(config.data);
|
|
115
|
+
SearchSuggestion.changeHintULElementWidth();
|
|
116
|
+
SearchSuggestion.changeHintULElementPosition();
|
|
117
|
+
|
|
118
|
+
SearchSuggestion.hide();
|
|
119
|
+
if (config.isAnimation) {
|
|
120
|
+
SearchSuggestion.root.classList.add(`pops-${PopsType}-animation`);
|
|
121
|
+
}
|
|
122
|
+
$shadowRoot.appendChild(SearchSuggestion.root);
|
|
123
|
+
parentElement.appendChild($shadowContainer);
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* 获取显示出搜索建议框的html
|
|
127
|
+
*/
|
|
128
|
+
getSearchSelectElement() {
|
|
129
|
+
let element = popsDOMUtils.createElement(
|
|
130
|
+
"div",
|
|
131
|
+
{
|
|
132
|
+
className: `pops pops-${PopsType}-search-suggestion`,
|
|
133
|
+
innerHTML: `
|
|
134
|
+
<style>
|
|
135
|
+
.pops-${PopsType}-animation{
|
|
136
|
+
-moz-animation: searchSelectFalIn 0.5s 1 linear;
|
|
137
|
+
-webkit-animation: searchSelectFalIn 0.5s 1 linear;
|
|
138
|
+
-o-animation: searchSelectFalIn 0.5s 1 linear;
|
|
139
|
+
-ms-animation: searchSelectFalIn 0.5s 1 linear;
|
|
140
|
+
}
|
|
141
|
+
.pops-${PopsType}-search-suggestion{
|
|
142
|
+
border: initial;
|
|
143
|
+
overflow: initial;
|
|
144
|
+
}
|
|
145
|
+
ul.pops-${PopsType}-search-suggestion-hint{
|
|
146
|
+
position: ${config.isAbsolute ? "absolute" : "fixed"};
|
|
147
|
+
z-index: ${config.zIndex};
|
|
148
|
+
width: 0;
|
|
149
|
+
left: 0;
|
|
150
|
+
max-height: ${config.maxHeight};
|
|
151
|
+
overflow-x: hidden;
|
|
152
|
+
overflow-y: auto;
|
|
153
|
+
padding: 5px 0;
|
|
154
|
+
background-color: #fff;
|
|
155
|
+
box-sizing: border-box;
|
|
156
|
+
border-radius: 4px;
|
|
157
|
+
box-shadow: 0 1px 6px rgb(0 0 0 / 20%);
|
|
158
|
+
}
|
|
159
|
+
/* 建议框在上面时 */
|
|
160
|
+
ul.pops-${PopsType}-search-suggestion-hint[data-top-reverse]{
|
|
161
|
+
display: flex;
|
|
162
|
+
flex-direction: column-reverse;
|
|
163
|
+
}
|
|
164
|
+
ul.pops-${PopsType}-search-suggestion-hint[data-top-reverse] li{
|
|
165
|
+
flex-shrink: 0;
|
|
166
|
+
}
|
|
167
|
+
ul.pops-${PopsType}-search-suggestion-hint li{
|
|
168
|
+
padding: 7px;
|
|
169
|
+
margin: 0;
|
|
170
|
+
clear: both;
|
|
171
|
+
color: #515a6e;
|
|
172
|
+
font-size: 14px;
|
|
173
|
+
list-style: none;
|
|
174
|
+
cursor: pointer;
|
|
175
|
+
transition: background .2s ease-in-out;
|
|
176
|
+
overflow: hidden;
|
|
177
|
+
text-overflow: ellipsis;
|
|
178
|
+
width: 100%;
|
|
179
|
+
}
|
|
180
|
+
ul.pops-${PopsType}-search-suggestion-hint li[data-none]{
|
|
181
|
+
text-align: center;
|
|
182
|
+
font-size: 12px;
|
|
183
|
+
color: #8e8e8e;
|
|
184
|
+
}
|
|
185
|
+
ul.pops-${PopsType}-search-suggestion-hint li:hover{
|
|
186
|
+
background-color: rgba(0, 0, 0, .1);
|
|
187
|
+
}
|
|
188
|
+
</style>
|
|
189
|
+
<ul class="pops-${PopsType}-search-suggestion-hint">
|
|
190
|
+
${config.toSearhNotResultHTML}
|
|
191
|
+
</ul>
|
|
192
|
+
`,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
"data-guid": guid,
|
|
196
|
+
"type-value": PopsType,
|
|
197
|
+
}
|
|
198
|
+
);
|
|
199
|
+
if (config.className !== "" && config.className != null) {
|
|
200
|
+
popsDOMUtils.addClassName(element, config.className);
|
|
201
|
+
}
|
|
202
|
+
return element;
|
|
203
|
+
},
|
|
204
|
+
/**
|
|
205
|
+
* 获取显示出搜索建议框的每一项的html
|
|
206
|
+
* @param data 当前项的值
|
|
207
|
+
* @param index 当前项的下标
|
|
208
|
+
*/
|
|
209
|
+
getSearchItemLiElement(data: any, index: number) {
|
|
210
|
+
return popsDOMUtils.createElement("li", {
|
|
211
|
+
className: `pops-${PopsType}-search-suggestion-hint-item pops-flex-items-center pops-flex-y-center`,
|
|
212
|
+
"data-index": index,
|
|
213
|
+
"data-value": SearchSuggestion.getItemDataValue(data),
|
|
214
|
+
innerHTML: `
|
|
215
|
+
${config.getItemHTML(data)}
|
|
216
|
+
${
|
|
217
|
+
config.deleteIcon.enable
|
|
218
|
+
? SearchSuggestion.getDeleteIconHTML()
|
|
219
|
+
: ""
|
|
220
|
+
}
|
|
221
|
+
`,
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
/**
|
|
225
|
+
* 获取data-value值
|
|
226
|
+
* @param data
|
|
227
|
+
*/
|
|
228
|
+
getItemDataValue(data: any) {
|
|
229
|
+
return data;
|
|
230
|
+
},
|
|
231
|
+
/**
|
|
232
|
+
* 设置搜索建议框每一项的点击事件
|
|
233
|
+
* @param liElement
|
|
234
|
+
*/
|
|
235
|
+
setSearchItemClickEvent(liElement: HTMLLIElement) {
|
|
236
|
+
popsDOMUtils.on(
|
|
237
|
+
liElement,
|
|
238
|
+
"click",
|
|
239
|
+
void 0,
|
|
240
|
+
(event) => {
|
|
241
|
+
popsDOMUtils.preventEvent(event);
|
|
242
|
+
let $click = event.target as HTMLLIElement;
|
|
243
|
+
if ($click.closest(`.pops-${PopsType}-delete-icon`)) {
|
|
244
|
+
/* 点击的是删除按钮 */
|
|
245
|
+
if (typeof config.deleteIcon.callback === "function") {
|
|
246
|
+
config.deleteIcon.callback(
|
|
247
|
+
event,
|
|
248
|
+
liElement,
|
|
249
|
+
(liElement as any)["data-value"]
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
if (!this.hintULElement.children.length) {
|
|
253
|
+
/* 全删完了 */
|
|
254
|
+
this.clear();
|
|
255
|
+
}
|
|
256
|
+
} else {
|
|
257
|
+
/* 点击项主体 */
|
|
258
|
+
config.itemClickCallBack(
|
|
259
|
+
event,
|
|
260
|
+
liElement,
|
|
261
|
+
(liElement as any)["data-value"]
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
capture: true,
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
},
|
|
270
|
+
/**
|
|
271
|
+
* 设置搜索建议框每一项的选中事件
|
|
272
|
+
* @param liElement
|
|
273
|
+
*/
|
|
274
|
+
setSearchItemSelectEvent(liElement: HTMLLIElement) {
|
|
275
|
+
// popsDOMUtils.on(
|
|
276
|
+
// liElement,
|
|
277
|
+
// "keyup",
|
|
278
|
+
// void 0,
|
|
279
|
+
// (event) => {
|
|
280
|
+
// let value = liElement["data-value"];
|
|
281
|
+
// config.selectCallBack(event, liElement, value);
|
|
282
|
+
// },
|
|
283
|
+
// {
|
|
284
|
+
// capture: true,
|
|
285
|
+
// }
|
|
286
|
+
// );
|
|
287
|
+
},
|
|
288
|
+
/**
|
|
289
|
+
* 监听输入框内容改变
|
|
290
|
+
*/
|
|
291
|
+
setInputChangeEvent() {
|
|
292
|
+
/* 是input输入框 */
|
|
293
|
+
/* 禁用输入框自动提示 */
|
|
294
|
+
config.inputTarget.setAttribute("autocomplete", "off");
|
|
295
|
+
/* 内容改变事件 */
|
|
296
|
+
popsDOMUtils.on(
|
|
297
|
+
config.inputTarget,
|
|
298
|
+
"input",
|
|
299
|
+
void 0,
|
|
300
|
+
async (event) => {
|
|
301
|
+
let getListResult = await config.getData(
|
|
302
|
+
(event.target as any).value
|
|
303
|
+
);
|
|
304
|
+
SearchSuggestion.update(getListResult);
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
capture: true,
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
},
|
|
311
|
+
/**
|
|
312
|
+
* 移除输入框内容改变的监听
|
|
313
|
+
*/
|
|
314
|
+
removeInputChangeEvent() {
|
|
315
|
+
popsDOMUtils.off(config.inputTarget, "input", void 0, void 0, {
|
|
316
|
+
capture: true,
|
|
317
|
+
});
|
|
318
|
+
},
|
|
319
|
+
/**
|
|
320
|
+
* 显示搜索建议框的事件
|
|
321
|
+
*/
|
|
322
|
+
showEvent() {
|
|
323
|
+
SearchSuggestion.changeHintULElementPosition();
|
|
324
|
+
SearchSuggestion.changeHintULElementWidth();
|
|
325
|
+
if (config.toHideWithNotResult) {
|
|
326
|
+
if (SearchSuggestion.$data.isEmpty) {
|
|
327
|
+
SearchSuggestion.hide();
|
|
328
|
+
} else {
|
|
329
|
+
SearchSuggestion.show();
|
|
330
|
+
}
|
|
331
|
+
} else {
|
|
332
|
+
SearchSuggestion.show();
|
|
333
|
+
}
|
|
334
|
+
},
|
|
335
|
+
/**
|
|
336
|
+
* 设置显示搜索建议框的事件
|
|
337
|
+
*/
|
|
338
|
+
setShowEvent() {
|
|
339
|
+
/* 焦点|点击事件*/
|
|
340
|
+
if (config.followPosition === "target") {
|
|
341
|
+
popsDOMUtils.on(
|
|
342
|
+
[config.target],
|
|
343
|
+
["focus", "click"],
|
|
344
|
+
void 0,
|
|
345
|
+
SearchSuggestion.showEvent,
|
|
346
|
+
{
|
|
347
|
+
capture: true,
|
|
348
|
+
}
|
|
349
|
+
);
|
|
350
|
+
} else if (config.followPosition === "input") {
|
|
351
|
+
popsDOMUtils.on(
|
|
352
|
+
[config.inputTarget],
|
|
353
|
+
["focus", "click"],
|
|
354
|
+
void 0,
|
|
355
|
+
SearchSuggestion.showEvent,
|
|
356
|
+
{
|
|
357
|
+
capture: true,
|
|
358
|
+
}
|
|
359
|
+
);
|
|
360
|
+
} else if (config.followPosition === "inputCursor") {
|
|
361
|
+
popsDOMUtils.on(
|
|
362
|
+
[config.inputTarget],
|
|
363
|
+
["focus", "click", "input"],
|
|
364
|
+
void 0,
|
|
365
|
+
SearchSuggestion.showEvent,
|
|
366
|
+
{
|
|
367
|
+
capture: true,
|
|
368
|
+
}
|
|
369
|
+
);
|
|
370
|
+
} else {
|
|
371
|
+
throw new TypeError("未知followPosition:" + config.followPosition);
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
/**
|
|
375
|
+
* 移除显示搜索建议框的事件
|
|
376
|
+
*/
|
|
377
|
+
removeShowEvent() {
|
|
378
|
+
/* 焦点|点击事件*/
|
|
379
|
+
popsDOMUtils.off(
|
|
380
|
+
[config.target, config.inputTarget],
|
|
381
|
+
["focus", "click"],
|
|
382
|
+
void 0,
|
|
383
|
+
SearchSuggestion.showEvent,
|
|
384
|
+
{
|
|
385
|
+
capture: true,
|
|
386
|
+
}
|
|
387
|
+
);
|
|
388
|
+
/* 内容改变事件 */
|
|
389
|
+
popsDOMUtils.off(
|
|
390
|
+
[config.inputTarget],
|
|
391
|
+
["input"],
|
|
392
|
+
void 0,
|
|
393
|
+
SearchSuggestion.showEvent,
|
|
394
|
+
{
|
|
395
|
+
capture: true,
|
|
396
|
+
}
|
|
397
|
+
);
|
|
398
|
+
},
|
|
399
|
+
/**
|
|
400
|
+
* 隐藏搜索建议框的事件
|
|
401
|
+
* @param event
|
|
402
|
+
*/
|
|
403
|
+
hideEvent(event: PointerEvent | MouseEvent) {
|
|
404
|
+
if (event.target instanceof Node) {
|
|
405
|
+
if ($shadowContainer.contains(event.target)) {
|
|
406
|
+
/* 点击在shadow上的 */
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (config.target.contains(event.target)) {
|
|
410
|
+
/* 点击在目标元素内 */
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
if (config.inputTarget.contains(event.target)) {
|
|
414
|
+
/* 点击在目标input内 */
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
SearchSuggestion.hide();
|
|
418
|
+
}
|
|
419
|
+
},
|
|
420
|
+
/**
|
|
421
|
+
* 设置隐藏搜索建议框的事件
|
|
422
|
+
*/
|
|
423
|
+
setHideEvent() {
|
|
424
|
+
/* 全局点击事件 */
|
|
425
|
+
/* 全局触摸屏点击事件 */
|
|
426
|
+
popsDOMUtils.on(
|
|
427
|
+
SearchSuggestion.selfDocument,
|
|
428
|
+
["click", "touchstart"],
|
|
429
|
+
void 0,
|
|
430
|
+
SearchSuggestion.hideEvent,
|
|
431
|
+
{
|
|
432
|
+
capture: true,
|
|
433
|
+
}
|
|
434
|
+
);
|
|
435
|
+
},
|
|
436
|
+
/**
|
|
437
|
+
* 移除隐藏搜索建议框的事件
|
|
438
|
+
*/
|
|
439
|
+
removeHideEvent() {
|
|
440
|
+
popsDOMUtils.off(
|
|
441
|
+
SearchSuggestion.selfDocument,
|
|
442
|
+
["click", "touchstart"],
|
|
443
|
+
void 0,
|
|
444
|
+
SearchSuggestion.hideEvent,
|
|
445
|
+
{
|
|
446
|
+
capture: true,
|
|
447
|
+
}
|
|
448
|
+
);
|
|
449
|
+
},
|
|
450
|
+
/**
|
|
451
|
+
* 设置所有监听
|
|
452
|
+
*/
|
|
453
|
+
setAllEvent() {
|
|
454
|
+
SearchSuggestion.setInputChangeEvent();
|
|
455
|
+
SearchSuggestion.setHideEvent();
|
|
456
|
+
SearchSuggestion.setShowEvent();
|
|
457
|
+
},
|
|
458
|
+
/**
|
|
459
|
+
* 移除所有监听
|
|
460
|
+
*/
|
|
461
|
+
removeAllEvent() {
|
|
462
|
+
SearchSuggestion.removeInputChangeEvent();
|
|
463
|
+
SearchSuggestion.removeHideEvent();
|
|
464
|
+
SearchSuggestion.removeShowEvent();
|
|
465
|
+
},
|
|
466
|
+
/**
|
|
467
|
+
* 获取删除按钮的html
|
|
468
|
+
*/
|
|
469
|
+
getDeleteIconHTML(size = 16, fill = "#bababa") {
|
|
470
|
+
return `
|
|
471
|
+
<svg class="pops-${PopsType}-delete-icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" fill="${fill}">
|
|
472
|
+
<path d="M512 883.2A371.2 371.2 0 1 0 140.8 512 371.2 371.2 0 0 0 512 883.2z m0 64a435.2 435.2 0 1 1 435.2-435.2 435.2 435.2 0 0 1-435.2 435.2z"></path>
|
|
473
|
+
<path d="M557.056 512l122.368 122.368a31.744 31.744 0 1 1-45.056 45.056L512 557.056l-122.368 122.368a31.744 31.744 0 1 1-45.056-45.056L466.944 512 344.576 389.632a31.744 31.744 0 1 1 45.056-45.056L512 466.944l122.368-122.368a31.744 31.744 0 1 1 45.056 45.056z"></path>
|
|
474
|
+
</svg>
|
|
475
|
+
`;
|
|
476
|
+
},
|
|
477
|
+
/**
|
|
478
|
+
* 设置当前正在搜索中的提示
|
|
479
|
+
*/
|
|
480
|
+
setPromptsInSearch() {
|
|
481
|
+
let isSearchingElement = popsDOMUtils.createElement("li", {
|
|
482
|
+
className: `pops-${PopsType}-search-suggestion-hint-searching-item`,
|
|
483
|
+
innerHTML: config.searchingTip,
|
|
484
|
+
});
|
|
485
|
+
SearchSuggestion.hintULElement.appendChild(isSearchingElement);
|
|
486
|
+
},
|
|
487
|
+
/**
|
|
488
|
+
* 移除正在搜索中的提示
|
|
489
|
+
*/
|
|
490
|
+
removePromptsInSearch() {
|
|
491
|
+
SearchSuggestion.hintULElement
|
|
492
|
+
.querySelector(
|
|
493
|
+
`li.pops-${PopsType}-search-suggestion-hint-searching-item`
|
|
494
|
+
)
|
|
495
|
+
?.remove();
|
|
496
|
+
},
|
|
497
|
+
/**
|
|
498
|
+
* 清空所有的搜索结果
|
|
499
|
+
*/
|
|
500
|
+
clearAllSearchItemLi() {
|
|
501
|
+
SearchSuggestion.hintULElement.innerHTML = "";
|
|
502
|
+
},
|
|
503
|
+
/**
|
|
504
|
+
* 修改搜索建议框的位置(top、left)
|
|
505
|
+
* 因为目标元素可能是动态隐藏的
|
|
506
|
+
*/
|
|
507
|
+
changeHintULElementPosition(
|
|
508
|
+
target = config.target ?? config.inputTarget
|
|
509
|
+
) {
|
|
510
|
+
/** @type {DOMRect|null} */
|
|
511
|
+
let targetRect = null;
|
|
512
|
+
if (config.followPosition === "inputCursor") {
|
|
513
|
+
targetRect = popsDOMUtils.getTextBoundingRect(
|
|
514
|
+
config.inputTarget,
|
|
515
|
+
config.inputTarget.selectionStart || 0,
|
|
516
|
+
config.inputTarget.selectionEnd || 0,
|
|
517
|
+
false
|
|
518
|
+
);
|
|
519
|
+
} else {
|
|
520
|
+
targetRect = config.isAbsolute
|
|
521
|
+
? popsDOMUtils.offset(target)
|
|
522
|
+
: target.getBoundingClientRect();
|
|
523
|
+
}
|
|
524
|
+
if (targetRect == null) {
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
// 文档最大高度
|
|
528
|
+
let documentHeight = document.documentElement.clientHeight;
|
|
529
|
+
if (config.isAbsolute) {
|
|
530
|
+
// 绝对定位
|
|
531
|
+
documentHeight = popsDOMUtils.height(document);
|
|
532
|
+
}
|
|
533
|
+
// 文档最大宽度
|
|
534
|
+
let documentWidth = popsDOMUtils.width(document);
|
|
535
|
+
if (config.position === "top") {
|
|
536
|
+
if (config.positionTopToReverse) {
|
|
537
|
+
SearchSuggestion.hintULElement.setAttribute(
|
|
538
|
+
"data-top-reverse",
|
|
539
|
+
"true"
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
// 在上面
|
|
543
|
+
SearchSuggestion.hintULElement.style.top = "";
|
|
544
|
+
SearchSuggestion.hintULElement.style.bottom =
|
|
545
|
+
documentHeight - targetRect.top + config.topDistance + "px";
|
|
546
|
+
} else if (config.position === "bottom") {
|
|
547
|
+
// 在下面
|
|
548
|
+
SearchSuggestion.hintULElement.removeAttribute("data-top-reverse");
|
|
549
|
+
SearchSuggestion.hintULElement.style.bottom = "";
|
|
550
|
+
SearchSuggestion.hintULElement.style.top =
|
|
551
|
+
targetRect.height + targetRect.top + config.topDistance + "px";
|
|
552
|
+
} else if (config.position === "auto") {
|
|
553
|
+
// 自动判断
|
|
554
|
+
if (
|
|
555
|
+
targetRect.bottom +
|
|
556
|
+
popsDOMUtils.height(SearchSuggestion.hintULElement) >
|
|
557
|
+
documentHeight
|
|
558
|
+
) {
|
|
559
|
+
if (config.positionTopToReverse) {
|
|
560
|
+
SearchSuggestion.hintULElement.setAttribute(
|
|
561
|
+
"data-top-reverse",
|
|
562
|
+
"true"
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
// 超出浏览器高度了,自动转换为上面去
|
|
566
|
+
SearchSuggestion.hintULElement.style.top = "";
|
|
567
|
+
SearchSuggestion.hintULElement.style.bottom =
|
|
568
|
+
documentHeight - targetRect.top + config.topDistance + "px";
|
|
569
|
+
} else {
|
|
570
|
+
SearchSuggestion.hintULElement.removeAttribute("data-top");
|
|
571
|
+
SearchSuggestion.hintULElement.style.bottom = "";
|
|
572
|
+
SearchSuggestion.hintULElement.style.top =
|
|
573
|
+
targetRect.height + targetRect.top + config.topDistance + "px";
|
|
574
|
+
}
|
|
575
|
+
} else {
|
|
576
|
+
throw new TypeError("未知设置的位置" + config.position);
|
|
577
|
+
}
|
|
578
|
+
let hintUIWidth = popsDOMUtils.width(SearchSuggestion.hintULElement);
|
|
579
|
+
SearchSuggestion.hintULElement.style.left =
|
|
580
|
+
(targetRect.left + hintUIWidth > documentWidth
|
|
581
|
+
? documentWidth - hintUIWidth
|
|
582
|
+
: targetRect.left) + "px";
|
|
583
|
+
},
|
|
584
|
+
/**
|
|
585
|
+
* 修改搜索建议框的width
|
|
586
|
+
* 因为目标元素可能是动态隐藏的
|
|
587
|
+
*/
|
|
588
|
+
changeHintULElementWidth(target = config.target ?? config.inputTarget) {
|
|
589
|
+
let targetRect = target.getBoundingClientRect();
|
|
590
|
+
if (config.followTargetWidth) {
|
|
591
|
+
SearchSuggestion.hintULElement.style.width = targetRect.width + "px";
|
|
592
|
+
} else {
|
|
593
|
+
SearchSuggestion.hintULElement.style.width = config.width;
|
|
594
|
+
}
|
|
595
|
+
},
|
|
596
|
+
/**
|
|
597
|
+
* 更新页面显示的搜索结果
|
|
598
|
+
* @param data
|
|
599
|
+
*/
|
|
600
|
+
update(data: any[] = []) {
|
|
601
|
+
if (!Array.isArray(data)) {
|
|
602
|
+
throw new TypeError("传入的数据不是数组");
|
|
603
|
+
}
|
|
604
|
+
config.data = data;
|
|
605
|
+
/* 清空已有的搜索结果 */
|
|
606
|
+
if (config.data.length) {
|
|
607
|
+
SearchSuggestion.$data.isEmpty = false;
|
|
608
|
+
|
|
609
|
+
if (config.toHideWithNotResult) {
|
|
610
|
+
SearchSuggestion.show();
|
|
611
|
+
}
|
|
612
|
+
SearchSuggestion.clearAllSearchItemLi();
|
|
613
|
+
/* 添加进ul中 */
|
|
614
|
+
config.data.forEach((item, index) => {
|
|
615
|
+
let itemElement = SearchSuggestion.getSearchItemLiElement(
|
|
616
|
+
item,
|
|
617
|
+
index
|
|
618
|
+
);
|
|
619
|
+
SearchSuggestion.setSearchItemClickEvent(itemElement);
|
|
620
|
+
SearchSuggestion.setSearchItemSelectEvent(itemElement);
|
|
621
|
+
SearchSuggestion.hintULElement.appendChild(itemElement);
|
|
622
|
+
});
|
|
623
|
+
} else {
|
|
624
|
+
/* 清空 */
|
|
625
|
+
SearchSuggestion.clear();
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
/**
|
|
629
|
+
* 清空当前的搜索结果并显示无结果
|
|
630
|
+
*/
|
|
631
|
+
clear() {
|
|
632
|
+
this.$data.isEmpty = true;
|
|
633
|
+
this.clearAllSearchItemLi();
|
|
634
|
+
this.hintULElement.appendChild(
|
|
635
|
+
popsUtils.parseTextToDOM(config.toSearhNotResultHTML)
|
|
636
|
+
);
|
|
637
|
+
if (config.toHideWithNotResult) {
|
|
638
|
+
this.hide();
|
|
639
|
+
}
|
|
640
|
+
},
|
|
641
|
+
/**
|
|
642
|
+
* 隐藏搜索建议框
|
|
643
|
+
*/
|
|
644
|
+
hide() {
|
|
645
|
+
this.root.style.display = "none";
|
|
646
|
+
},
|
|
647
|
+
/**
|
|
648
|
+
* 显示搜索建议框
|
|
649
|
+
*/
|
|
650
|
+
show() {
|
|
651
|
+
this.root.style.display = "";
|
|
652
|
+
},
|
|
653
|
+
};
|
|
654
|
+
return SearchSuggestion;
|
|
655
|
+
}
|
|
656
|
+
}
|