-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedSearch.js
586 lines (558 loc) · 24 KB
/
AdvancedSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
;(function (window, document) {//$,window, document
/**
* 用于参数合并
* @param defaultOptions
* @param options
* @returns {*}
*/
function merge(defaultOptions, options) {
for (var i in defaultOptions) {
if (options.hasOwnProperty(i)) {
defaultOptions[i] = options[i];
}
}
return defaultOptions;
}
/**
* 窗体移动事件绑定及处理代码
* @constructor
*/
function FormMove(d) {
var isMove = false, x, y;
var advancedsearch = d.view.advancedsearch;
var getstyle = window.getComputedStyle(advancedsearch, null);
//标题栏,监听鼠标移入
d.view.advancedsearch_title.addEventListener("mousedown", function (e) {
x = e.clientX;
y = e.clientY;
isMove = true;
}, false);
window.addEventListener("mouseup", function () {
isMove = false;
}, false);
window.addEventListener("mousemove", function (e) {
if (isMove) {
//移动窗体
var mx = e.clientX;
var my = e.clientY;
var left = parseInt(getstyle.left.split("px")[0]);
var top = parseInt(getstyle.top.split("px")[0]);
var width = parseInt(getstyle.width.split("px")[0]);
var height = parseInt(getstyle.height.split("px")[0]);
var documentHeight = document.documentElement.clientHeight - height;
var documentWidth = document.documentElement.clientWidth - width;
if (top >= documentHeight) advancedsearch.style.top = documentHeight;
if (left >= documentWidth) advancedsearch.style.left = documentWidth;
if (left <= 0) advancedsearch.style.left = 0 + "px";
if (top <= 0) advancedsearch.style.top = 0 + "px";
if (mx > x && left < documentWidth) {
advancedsearch.style.left = (left + (mx - x)) + "px";
} else if (mx < x && left > 0) {
advancedsearch.style.left = left - (x - mx) + "px";
}
if (my > y && top < documentHeight) {
advancedsearch.style.top = top + (my - y) + "px";
} else if (my < y && top > 0) {
advancedsearch.style.top = top - (y - my) + "px";
}
x = e.clientX;
y = e.clientY;
}
});
}
/**
* 下拉dd鼠标按下事件绑定
*/
function ddMousedown() {
var _this = this;
this.parentNode.previousSibling.value = _this.innerHTML;
}
/**
* 用于创建下拉框,根据explanation创建option
* @param explanation
* @returns {Element}
* @constructor
*/
function CreateSelectVal(explanation, code, type, explanation, d) {
var advancedsearch_content_row_right = document.createElement("div");
advancedsearch_content_row_right.className = "advancedsearch_content_row_right";
advancedsearch_content_row_right.dataset.align = "right";
var advancedsearch_select = document.createElement("input");
advancedsearch_select.dataset.code = code;
advancedsearch_select.dataset.type = type;
advancedsearch_select.dataset.explanation = explanation;
advancedsearch_select.className = "advancedsearch_select advancedsearch_ordinary";
//检索框事件绑定
advancedsearch_select.addEventListener("focus", function (event) {
selectInput(this, d, event);
});
advancedsearch_select.addEventListener("blur", function (event) {
selectInput(this, d, event);
});
advancedsearch_select.addEventListener("input", function (event) {
selectInput(this, d, event);
});
explanation = JSON.parse(explanation);
var dl = document.createElement("dl");
for (var i in explanation) {
var dd = document.createElement("dd");
dd.innerHTML = i;
dd.dataset.value = explanation[i];
dd.addEventListener("mousedown", ddMousedown);
dl.appendChild(dd);
}
//组装
advancedsearch_content_row_right.appendChild(advancedsearch_select);
advancedsearch_content_row_right.appendChild(dl);
return advancedsearch_content_row_right;
}
/**
* 用于创建默认input
* @param code
* @returns {[null,null]}
* @constructor
*/
function CreateInput(code, type) {
var advancedsearch_content_row_right = document.createElement("div");
advancedsearch_content_row_right.className = "advancedsearch_content_row_right";
advancedsearch_content_row_right.dataset.align = "right";
var advancedsearch_val = document.createElement("input");
advancedsearch_val.className = "advancedsearch_val advancedsearch_ordinary";
advancedsearch_val.dataset.code = code;
advancedsearch_val.dataset.type = type;
advancedsearch_val.dataset.value = "";
advancedsearch_val.type = "text";
advancedsearch_content_row_right.appendChild(advancedsearch_val);
return [advancedsearch_content_row_right, advancedsearch_val];
}
/**
* 创建时间区间
* @param code
* @returns element
* @constructor
*/
function CreateDateInputs(code, type, dateformat) {
var advancedsearch_content_row_right = document.createElement("div");
advancedsearch_content_row_right.className = "advancedsearch_content_row_right";
advancedsearch_content_row_right.dataset.align = "right";
var dateBox = document.createElement("div");
dateBox.className = "advancedsearch_content_row_right_box";
var span = document.createElement("span");
span.innerHTML = " - ";
for (var i = 0; i < 2; i++) {
var advancedsearch_val = document.createElement("input");
advancedsearch_val.className = "advancedsearch_val advancedsearch_interval";
advancedsearch_val.dataset.code = code;
advancedsearch_val.dataset.type = type;
advancedsearch_val.dataset.dateformat = dateformat;
advancedsearch_val.dataset.value = "";
advancedsearch_val.type = "text";
if (i == 1) dateBox.appendChild(span);
dateBox.appendChild(advancedsearch_val);
}
advancedsearch_content_row_right.appendChild(dateBox);
return advancedsearch_content_row_right;
}
/**
* 根据type动态更改值界面
* @param dd
* @constructor
*/
function DynamicVal(dd, d) {
var dd_code = dd.dataset.code;
var dd_type = dd.dataset.type;
//得到row
var row = dd.parentNode.parentNode.parentNode;
dd_type = dd_type.toLowerCase();
if (dd_type == "date" || dd_type == "timestamp" || dd_type == "datetime" || dd_type == "time") {
//时间类型
var right_element = CreateDateInputs(dd_code, dd_type, dd.dataset.dateformat);
row.replaceChild(right_element, row.childNodes[1]);
//添加至页面后回调
d.options.dateInput(row.querySelectorAll("input"), dd.dataset.dateformat);
} else {
//下拉框或者普通文本类型
if ((typeof dd.dataset.explanation) != "undefined" && dd.dataset.explanation != "") {
//下拉框
var explanation = dd.dataset.explanation;
//替换节点
row.replaceChild(CreateSelectVal(explanation, dd_code, dd_type, explanation, d), row.childNodes[1]);
} else {
//普通文本
var input = CreateInput(dd_code, dd_type);
row.replaceChild(input[0], row.childNodes[1]);
}
}
}
/**
* 下拉检索框事件
* @param k
*/
function selectInput(u, d, event) {
var _this = u;
var type = event.type;
var dl = _this.nextSibling;//取得下一兄弟节点,也就是dl
switch (type) {
case "focus":
dl.style.display = "block";
break;
case "blur":
var val = _this.value;
//获取所有子节点nodes
var dds = dl.childNodes;
var isexist = false;
for (var i = 0; i < dds.length; i++) {
var dd = dds.item(i);
if (dd.innerHTML == val) {
isexist = true;
var ddVal = dd.dataset.value;
if ((typeof ddVal) != "undefined" && ddVal != null && ddVal != "") {
_this.dataset.value = ddVal;
} else {
_this.dataset.value = "";
}
//判断是否为右边select
if (dl.parentNode.dataset.align != "right") {
//初始化对应input结果
var valInput = _this.parentNode.nextSibling.childNodes.item(0);
valInput.dataset.code = "";
DynamicVal(dd, d);//动态修改值的input
}
break;
}
}
if (!isexist) {
_this.value = "";
for (var i = 0; i < dds.length; i++) {
dds.item(i).style.display = "block";
}
}
//隐藏dl
dl.style.display = "none";
break;
case "input":
var dds = dl.childNodes;
if (_this.value != "") {
for (var i = 0; i < dds.length; i++) {
var dd = dds.item(i);
if (dd.innerHTML.indexOf(_this.value) != -1) {
dd.style.display = "block";
} else {
dd.style.display = "none";
}
}
} else {
for (var i = 0; i < dds.length; i++) {
dds.item(i).style.display = "block";
}
}
break;
}
}
/**
* 用于删除row
* @constructor
*/
function DeleteRow() {
var row = this.parentNode;
this.parentNode.parentNode.removeChild(row);
}
function isEmpty(e) {
for (var i in e) {
return false;
}
return true;
}
/**
* 动态创建默认select
* @constructor
*/
function CreateSelectDom(d) {
var advancedsearch_content_row = document.createElement("div");
advancedsearch_content_row.className = "advancedsearch_content_row";
var advancedsearch_content_row_left = document.createElement("div");
advancedsearch_content_row_left.className = "advancedsearch_content_row_left";
advancedsearch_content_row_left.dataset.align = "left";
var advancedsearch_content_row_right = document.createElement("div");
advancedsearch_content_row_right.className = "advancedsearch_content_row_right";
advancedsearch_content_row_right.dataset.align = "right";
var advancedsearch_content_row_close = document.createElement("div");
advancedsearch_content_row_close.className = "advancedsearch_content_row_close";
advancedsearch_content_row_close.innerHTML = "—";
advancedsearch_content_row_close.addEventListener("click", DeleteRow);
var advancedsearch_val = document.createElement("input");
advancedsearch_val.type = "text";
advancedsearch_val.className = "advancedsearch_val advancedsearch_ordinary";
var advancedsearch_select = document.createElement("input");
advancedsearch_select.type = "text";
advancedsearch_select.className = "advancedsearch_select advancedsearch_ordinary";
//检索框事件绑定
advancedsearch_select.addEventListener("focus", function (event) {
selectInput(this, d, event)
});
advancedsearch_select.addEventListener("blur", function (event) {
selectInput(this, d, event)
});
advancedsearch_select.addEventListener("input", function (event) {
selectInput(this, d, event)
});
var dl = document.createElement("dl");
var ddArray = d.options["columns"];
for (var i = 0; i < ddArray.length; i++) {
var obj = ddArray[i];
var dd = document.createElement("dd");
dd.innerHTML = obj["title"];
dd.dataset.type = obj["type"];
dd.dataset.code = obj["code"];
dd.dataset.value = "";
if (obj.hasOwnProperty("explanation")) dd.dataset.explanation = JSON.stringify(obj["explanation"]);
if (obj.hasOwnProperty("dateformat")) dd.dataset.dateformat = obj["dateformat"];
//dd事件绑定
dd.addEventListener("mousedown", ddMousedown);
dl.appendChild(dd);
}
//组合
//left
advancedsearch_content_row_left.appendChild(advancedsearch_select);
advancedsearch_content_row_left.appendChild(dl);
//right
advancedsearch_content_row_right.appendChild(advancedsearch_val);
//row
advancedsearch_content_row.appendChild(advancedsearch_content_row_left);
advancedsearch_content_row.appendChild(advancedsearch_content_row_right);
advancedsearch_content_row.appendChild(advancedsearch_content_row_close);
return advancedsearch_content_row;
}
/**
* 底部按钮事件绑定
* @constructor
*/
function ControlEvent(d) {
d.view.advancedsearch_add_btn.addEventListener("click", function () {
d.view.advancedsearch_content.appendChild(CreateSelectDom(d));
});
d.view.advancedsearch_add_search.addEventListener("click", function () {
//整理所有结果(普通结果)
var inputs = d.view.advancedsearch.querySelectorAll(".advancedsearch_content_row_right input.advancedsearch_ordinary");
var result = [];
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
var obj = {};
if (input.value != "" && input.parentNode.previousSibling.childNodes.item(0).value != "") {
if (input.dataset.value != "") {//选择类判断
//select
obj["value"] = input.dataset.value;
obj["code"] = input.dataset.code;
obj["type"] = input.dataset.type;
obj["explanation"] = input.dataset.explanation;
} else {
obj["value"] = input.value;
obj["code"] = input.dataset.code;
obj["type"] = input.dataset.type;
}
if (!isEmpty(obj)) result.push(obj);
}
}
//区间
var dateInputBox = d.view.advancedsearch.querySelectorAll(".advancedsearch_content_row_right .advancedsearch_content_row_right_box");
for (var j = 0; j < dateInputBox.length; j++) {
var dates = dateInputBox[j].childNodes;//获取子元素集合
var interval = "", obj = {};
for (var k = 0; k < dates.length; k++) {
if (dates[k].classList.contains('advancedsearch_interval')) {
if (dates[k].value != "" && dates[k].parentNode.parentNode.previousSibling.childNodes.item(0).value != "") {
obj["code"] = dates[k].dataset.code;
obj["type"] = dates[k].dataset.type;
interval += dates[k].value + (k == 0 ? "#" : "");
}
}
}
if (interval != "") obj["value"] = interval;
if (!isEmpty(obj)) result.push(obj);
}
d.options.search(result);
});
}
/**
* 退出事件
* @constructor
*/
function CloseEvent(d) {
d.view.advancedsearch_close.addEventListener("click", function () {
hide(d);
d.options.close();//退出回调
});
}
/**
* 隐藏窗体
*/
function hide(d) {
if (d.options.shadeEnable) {
d.view.advancedsearch_cover.style.display = "none";
}
d.view.advancedsearch.style.display = "none";
}
/**
* 显示窗体
*/
function show(d) {
if (d.options.shadeEnable) {
d.view.advancedsearch_cover.style.display = "block";
}
d.view.advancedsearch.style.display = "block";
}
/**
* 用于创建默认框架
* @constructor
*/
function DefaultBox(d) {
var advancedsearch_cover = document.createElement("div");
advancedsearch_cover.className = "advancedsearch_cover";
var advancedsearch = document.createElement("div");
advancedsearch.className = "advancedsearch";
var advancedsearch_title = document.createElement("div");
advancedsearch_title.className = "advancedsearch_title";
var advancedsearch_title_txt = document.createElement("div");
advancedsearch_title_txt.className = "advancedsearch_title_txt";
var advancedsearch_title_txt_span = document.createElement("span");
advancedsearch_title_txt_span.className = "advancedsearch_title_txt_span";
var advancedsearch_close = document.createElement("div");
advancedsearch_close.className = "advancedsearch_close";
var span = document.createElement("span");
span.innerHTML = "X";
var advancedsearch_content = document.createElement("div");
advancedsearch_content.className = "advancedsearch_content";
var advancedsearch_control = document.createElement("div");
advancedsearch_control.className = "advancedsearch_control";
var advancedsearch_add_btn = document.createElement("button");
advancedsearch_add_btn.className = "advancedsearch_add_btn";
advancedsearch_add_btn.innerHTML = "Add";
var advancedsearch_add_search = document.createElement("button");
advancedsearch_add_search.className = "advancedsearch_add_search";
advancedsearch_add_search.innerHTML = "Search";
//组装
advancedsearch.appendChild(advancedsearch_title);
advancedsearch.appendChild(advancedsearch_content);
advancedsearch.appendChild(advancedsearch_control);
advancedsearch_title.appendChild(advancedsearch_title_txt);
advancedsearch_close.appendChild(span);
advancedsearch_title.appendChild(advancedsearch_close);
advancedsearch_title_txt.appendChild(advancedsearch_title_txt_span);
advancedsearch_control.appendChild(advancedsearch_add_btn);
advancedsearch_control.appendChild(advancedsearch_add_search);
document.querySelector("body").appendChild(advancedsearch_cover);
document.querySelector("body").appendChild(advancedsearch);
d.view.advancedsearch = advancedsearch;
d.view.advancedsearch_title_txt_span = advancedsearch_title_txt_span;
d.view.advancedsearch_content = advancedsearch_content;
d.view.advancedsearch_close = advancedsearch_close;
d.view.advancedsearch_cover = advancedsearch_cover;
d.view.advancedsearch_title = advancedsearch_title;
d.view.advancedsearch_add_btn = advancedsearch_add_btn;
d.view.advancedsearch_add_search = advancedsearch_add_search;
}
/*----------------------------------------------------------------------------------------------------------------*/
//入口
var AsSearch = function (targetDom, options) {
// 判断是用函数创建的还是用new创建的。这样我们就可以通过AsSearch("dom") 或 new AsSearch("dom")来使用这个插件了
if (!(this instanceof AsSearch)) return new AsSearch(targetDom, options);
//判断触发目标是字符串还是dom
if ((typeof targetDom) == "string") {
this.targetDom = document.querySelector(targetDom);
} else {
this.targetDom = targetDom;
}
//默认界面
this.init(options);
}
AsSearch.prototype = {
init: function (options) {
var defaultOptions = {
size: [700, 400],//初始弹窗宽、高
shadeEnable: true,//遮罩层开关
shade: ["#000", 0.3],//遮罩层,黑色0.3透明度
shadeClose: false,//点击遮罩层是否关闭
shadeZindex: 1000000,//遮罩层z-index,弹窗默认+1
title: "Advanced Search",//弹窗标题
columns: [],//下拉条件
success: function () {
},//弹出成功后回调
search: function (result) {
},//搜索按钮回调
close: function () {
},//关闭回调
dateInput: function (inputs, dateformat) {
//inputs -> input element array
//dateformat -> date format string
}//input -> date回调
};
this.view = {}
options = this.options = merge(defaultOptions, options);
//检查框架是否存在
if (!this.hasOwnProperty("view") || !this["view"].hasOwnProperty("advancedsearch")) {
DefaultBox(this);
}
//配置标题
this.view.advancedsearch_title_txt_span.innerHTML = options.title;
//z-index配置
this.view.advancedsearch.style.zIndex = options.shadeZindex + 1;
this.view.advancedsearch_cover.style.zIndex = options.shadeZindex;
//遮罩层点击事件
if (options.shadeClose) {
var _this = this;
this.view.advancedsearch_cover.addEventListener("click",function(){
hide(_this);
})
}
//窗体大小及位置
var advancedsearch = this.view.advancedsearch;
advancedsearch.style.width = options.size[0] + "px";
advancedsearch.style.height = options.size[1] + "px";
advancedsearch.style.left = "calc(50% - " + (this.options.size[0] / 2) + "px)";
advancedsearch.style.top = "calc(50% - " + (this.options.size[1] / 2) + "px)";
//遮罩层颜色及透明度
this.view.advancedsearch_cover.style.backgroundColor = options.shade[0];
this.view.advancedsearch_cover.style.opacity = options.shade[1];
//添加默认条件
this.view.advancedsearch_content.appendChild(CreateSelectDom(this));
//初始化事件监听
this.event();
},
event: function () {
var _this = this;
//用户触发按钮
FormMove(this);//窗体移动
ControlEvent(this);//底部按钮事件绑定
CloseEvent(this);//退出事件
/**
* 用户绑定元素
*/
this.targetDom.addEventListener("click", function () {
show(_this);
});
this.options.success();//成功回调
},
remove: function () {
//清理痕迹
document.querySelector("body").removeChild(this.view.advancedsearch_cover);
document.querySelector("body").removeChild(this.view.advancedsearch);
},
/**
* 重新渲染
*/
render: function () {
this.remove();
this.init(this.options);
},
/**
* 隐藏窗体
*/
hide: function () {
hide(this);
},
show: function () {
show(this);
}
}
window.AsSearch = AsSearch;//暴露方法
})(window, document);////$,window, document