Administrator
2023-04-17 63fbfddabe08e353ad75e495c2ac8dc5203da88c
提交 | 用户 | 时间
062075 1 (function () {
A 2     Number.prototype.toPercent = function () {
3         return (Math.round(this * 10000) / 100).toFixed(2) + "%"
4     };
5     var a = function (q) {
6         if (!(this instanceof a)) {
7             return new a(q)
8         }
9         var n = this;
10         var h = {};
11         var r = [];
12         var g = {};
13         var o;
14         var e = [];
15         var k = new Date().getTime();
16         var j = {
17             id: "",
18             level: 3,
19             levelDesc: ["省份", "城市", "区县"],
20             index: "99999999999999999999",
21             separator: " / ",
22             isInitClick: true,
23             isWithMouse: false,
24             offsetX: 0,
25             offsetY: 0,
26             emptyText: "暂无数据",
27             color: "#56b4f8",
28             fontSize: "14px",
29             isAsync: false,
30             asyncUrl: "",
31             isShowBtn: true,
32             btnConfig: [],
33             data: ""
34         };
35         var t = j;
36         var l;
37         var c = 20;
38         var m = true;
39         n._init = function () {
40             if (!i()) {
41                 return
42             }
43             d();
44             var x = '<div id="adp-wraper-' + k + '" class="adp-wraper" style="z-index: ' + t.index + '"><p>';
45             var v = '<div class="ul-div" style="display: block"><ul></ul></div>';
46             x += '<span class="adp-head-active">' + t.levelDesc[0] + "</span><span>" + t.levelDesc[0] + "</span>";
47             for (var w = 1; w < t.level; w++) {
48                 x += "<span>" + t.levelDesc[w] + "</span>";
49                 v += '<div class="ul-div"><ul><span class="adp-empty-text">' + t.emptyText + "</span></ul></div>"
50             }
51             x += "</p>";
52             var y = b();
53            // $("body").append(x + v + y + '</div><div class="adp-wraper-backshadow" style="z-index: ' + (t.index - 1) + '"></div>');
54             $("body").append(x + v + y + '</div>');
55             if (t.isAsync) {
56                 o = f(null);
57                 n._initFirstLevelData()
58             } else {
59                 if (typeof t.data == "string" && !p(t.data)) {
60                     $.ajax({
61                         url: t.data, async: false, success: function (z) {
62                             o = z;
63                             n._getEachLevelData(0, t.level, o);
64                             n._initFirstLevelData()
65                         }
66                     })
67                 } else {
68                     o = t.data;
69                     if (typeof t.data == "string") {
70                         o = JSON.parse(o)
71                     }
72                     n._getEachLevelData(0, t.level, o);
73                     n._initFirstLevelData()
74                 }
75             }
76             n._bindEvent()
77         };
78         n._getEachLevelData = function (w, B, y) {
79             var A = [];
80             var z = [];
81             if (w >= B) {
82                 return
83             }
84             for (var x = 0; x < y.length; x++) {
85                 A.push(y[x]);
86                 if (w == B - 1) {
87                     continue
88                 }
89                 if (y[x].children == undefined) {
90                     continue
91                 }
92                 for (var v = 0; v < y[x].children.length; v++) {
93                     z.push(y[x].children[v])
94                 }
95             }
96             if (z.length == 0) {
97                 return
98             }
99             h["data_level_" + w] = A;
100             n._getEachLevelData(w + 1, B, z)
101         };
102         n._initFirstLevelData = function () {
103             for (var w = 0; w < t.level; w++) {
104                 $("#adp-wraper-" + k).children("div").eq(w).children("ul").empty();
105                 $("#adp-wraper-" + k).children("div").eq(w).children("ul").append('<span class="adp-empty-text">' + t.emptyText + "</span>")
106             }
107             if (o.length > 0) {
108                 $("#adp-wraper-" + k).children("div").eq(0).children("ul").empty()
109             }
110             for (var w = 0; w < o.length; w++) {
111                 var v = '<li data-code="' + o[w].value + '" title="' + o[w].text + '">' + o[w].text + "</li>";
112                 $("#adp-wraper-" + k).children("div").eq(0).children("ul").append(v)
113             }
114             var y = $("#adp-wraper-" + k).children("p").children("span").eq(1).text();
115             var x = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
116             x.css("left", "-1px");
117             x.text(y);
118             $("#adp-wraper-" + k).children("div.ul-div").eq(0).show().siblings("div.ul-div").hide();
119             if (t.isShowBtn) {
120                 s(0)
121             }
122         };
123         n._bindEvent = function () {
124             $("#adp-wraper-" + k + " div ul").delegate("li", "click", function () {
125                 var z = $(this).parent().parent().index();
126                 g.value = $(this).data("code");
127                 g.text = $(this).text();
128                 g.level = z;
129                 r[z - 1] = $(this).data("code");
130                 e[z - 1] = $(this).text();
131                 r.splice(z, r.length - z);
132                 e.splice(z, e.length - z);
133                 var G = $("#adp-wraper-" + k).children("div").length;
134                 G = G -1
135                 // console.log(z,G,z>G)
136                 if (z < G) {
137                     for (var F = z; F < t.level; F++) {
138                         $("#adp-wraper-" + k).children("div.ul-div").eq(F).children("ul").empty().append('<span class="adp-empty-text">' + t.emptyText + "</span>")
139                     }
140                     $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").empty();
141                     var x = t.isAsync ? f(g, z) : o;
142                     if (z > 1 && !t.isAsync) {
143                         x = h["data_level_" + (z - 1)]
144                     }
145                     if (x != undefined) {
146                         for (var C = 0; C < x.length; C++) {
147                             var y = x[C];
148                             if (t.isAsync) {
149                                 var E = '<li data-code="' + y.value + '" title="' + y.text + '">' + y.text + "</li>";
150                                 $("#adp-wraper-" + k).children("div").eq(z).children("ul").append(E)
151                             } else {
152                                 if (y.text == $(this).text() && y.value == $(this).data("code")) {
153                                     if (y.children != null && y.children.length > 0) {
154                                         for (var B = 0; B < y.children.length; B++) {
155                                             var A = y.children[B];
156                                             var E = '<li data-code="' + A.value + '" title="' + A.text + '">' + A.text + "</li>";
157                                             $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").append(E)
158                                         }
159                                     } else {
160                                         $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").append('<span class="adp-empty-text">' + t.emptyText + "</span>")
161                                     }
162                                     break
163                                 }
164                             }
165                         }
166                         if (t.isShowBtn) {
167                             s(z)
168                         }
169                     } else {
170                         $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").append('<span class="adp-empty-text">' + t.emptyText + "</span>")
171                     }
172                     $(this).addClass("adp-active").siblings().removeClass("adp-active");
173                     var D = $("#adp-wraper-" + k).children("p").children("span").eq(z + 1).text();
174                     var H = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
175                     H.css("left", (0.2 * (z)).toPercent());
176                     $(this).parent().parent().next().show().siblings("div.ul-div").hide();
177                     setTimeout(function () {
178                         H.text(D)
179                     }, 200)
180                 } else {
181                     $(this).addClass("adp-active").siblings().removeClass("adp-active");
182                     $("#adp-wraper-" + k).fadeOut();
183                     $("div.adp-wraper-backshadow").hide()
184                 }
185             });
186             $("#adp-wraper-" + k + " p span").click(function () {
187                 if ($(this).hasClass("adp-head-active")) {
188                     return
189                 }
190                 var x = $(this).index() - 1;
191                 var z = $(this).text();
192                 var y = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
193                 y.css("left", x == 0 ? -1 : (0.2 * x).toPercent());
194                 setTimeout(function () {
195                     y.text(z)
196                 }, 200);
197                 $("#adp-wraper-" + k).children("div").eq(x).show().siblings("div.ul-div").hide()
198             });
199             $("div.adp-wraper-backshadow").on("click", function () {
200                 $("div.adp-wraper-backshadow").hide();
201                 $("#adp-wraper-" + k).fadeOut()
202             });
203             $("#adp-wraper-" + k).click(function(event) {
204                 $(document).one("click",
205                     function() { //对document绑定一个影藏Div方法
206                         $("#adp-wraper-" + k).hide();
207                     });
208                 event.stopPropagation(); //阻止事件向上冒泡
209             });
210
211             if (t.isInitClick) {
212                 $("#" + t.id).on("click", function () {
213                     var y = u(event);
214                     var x = $("#adp-wraper-" + k + " div.ul-div").eq(0);
215                     $("#adp-wraper-" + k).css("left", y.x);
216                     $("#adp-wraper-" + k).css("top", y.y);
217                     $("#adp-wraper-" + k).fadeIn();
218                     if ((g.level == undefined || m) && t.isShowBtn) {
219                         if (x.children("ul").children("li").height() != 0) {
220                             c = x.children("ul").children("li").height()
221                         }
222                         s(0);
223                         m = false
224                     }
225                     $(".adp-wraper-backshadow").show()
226                 })
227             }
228             if (t.isShowBtn) {
229                 var w = t.btnConfig;
230                 for (var v = 0; v < w.length; v++) {
231                     if (typeof (w[v].click) == "function") {
232                         $("#adp_btn_" + v).on("click", w[v].click)
233                     }
234                 }
235             }
236         };
237         n._clearSelectedData = function () {
238             r = [];
239             e = [];
240             g = {};
241             var x = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
242             x.css("left", "-1px");
243             setTimeout(function () {
244                 x.text(t.levelDesc[0])
245             }, 200);
246             for (var w = 1; w < t.level; w++) {
247                 $("#adp-wraper-" + k).children("div.ul-div").eq(w).children("ul").empty().append('<span class="adp-empty-text">' + t.emptyText + "</span>");
248                 if (t.isShowBtn) {
249                     s(w)
250                 }
251             }
252             var v;
253             if ((v = $("#adp-wraper-" + k).children("div.ul-div").eq(0).children("ul").children("li.adp-active")).length > 0) {
254                 v.removeClass("adp-active")
255             }
256             $("#adp-wraper-" + k).children("div").eq(0).show().siblings("div.ul-div").hide()
257         };
258         n._setSelectedData = function (E) {
259             if (typeof (E) != "object" || E == null) {
260                 return
261             }
262             if (E.length > t.level || E.length == 0) {
263                 return
264             }
265             var v = t.isAsync ? f(null) : o;
266             if (typeof (v) != "object" || v == null) {
267                 return
268             }
269             var x, G, H, K, I, J = "", D = false, C;
270             H = $("#adp-wraper-" + k).children("div.ul-div").eq(0).children("ul");
271             for (var z = 0; z < (K = H.children("li")).length; z++) {
272                 x = v[z];
273                 if (x.value == E[0]) {
274                     g.value = x.value;
275                     g.text = x.text;
276                     g.level = 1;
277                     r[0] = x.value;
278                     e[0] = x.text;
279                     r.splice(1, r.length - 1);
280                     e.splice(1, e.length - 1);
281                     G = x;
282                     C = 0;
283                     D = true;
284                     H.children("li").removeClass("adp-active");
285                     $(K[z]).addClass("adp-active");
286                     continue
287                 }
288             }
289             if (!D) {
290                 return
291             }
292             for (var B = 1; B < E.length; B++) {
293                 if (E[B] == null || E[B] == "") {
294                     break
295                 }
296                 var w = t.isAsync ? f(G, g.level) : G.children;
297                 if (typeof (w) != "object" || w == null) {
298                     break
299                 }
300                 J = "";
301                 D = false;
302                 G = {};
303                 for (var A = 0; A < w.length; A++) {
304                     if (w[A].value == E[B]) {
305                         J += '<li data-code="' + w[A].value + '" title="' + w[A].text + '" class="adp-active">' + w[A].text + "</li>";
306                         g.value = w[A].value;
307                         g.text = w[A].text;
308                         g.level = (B + 1);
309                         r[B] = w[A].value;
310                         e[B] = w[A].text;
311                         r.splice(B + 1, r.length - B - 1);
312                         e.splice(B + 1, e.length - B - 1);
313                         G = w[A];
314                         D = true;
315                         C = B;
316                         continue
317                     }
318                     J += '<li data-code="' + w[A].value + '" title="' + w[A].text + '">' + w[A].text + "</li>"
319                 }
320                 if (!D) {
321                     break
322                 }
323                 $("#adp-wraper-" + k).children("div.ul-div").eq(B).children("ul").empty().append(J);
324                 if (t.isShowBtn) {
325                     s(B)
326                 }
327             }
328             if (typeof (C) == "number") {
329                 $("#adp-wraper-" + k).children("div.ul-div").eq(C).show().siblings("div.ul-div").hide();
330                 var F = $("#adp-wraper-" + k).children("p").children("span").eq(C + 1).text();
331                 var L = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
332                 L.css("left", (0.2 * (C)).toPercent());
333                 setTimeout(function () {
334                     L.text(F)
335                 }, 200);
336                 for (var y = C + 1; y < t.level; y++) {
337                     $("#adp-wraper-" + k).children("div.ul-div").eq(y).children("ul").empty().append('<span class="adp-empty-text">' + t.emptyText + "</span>");
338                     if (t.isShowBtn) {
339                         s(y)
340                     }
341                 }
342             }
343         };
344         n._refreshData = function (v) {
345             if (typeof v == "string" && p(v)) {
346                 v = JSON.parse(v)
347             }
348             o = v;
349             if (o.length > 0) {
350                 n._getEachLevelData(0, t.level, o)
351             }
352             n._initFirstLevelData()
353         };
354         n._getTotalValueAsArray = function () {
355             return {value: r, text: e}
356         };
357         n._getTotalValueAsText = function () {
358             var w = "";
359             for (var v = 0; v < g.level; v++) {
360                 w += (v < g.level - 1 ? e[v] + t.separator : e[v])
361             }
362             return w
363         };
364         n._getCurrentObject = function () {
365             return g
366         };
367         n._show = function () {
368             var v = u(event);
369             $("#adp-wraper-" + k).css("left", v.x);
370             $("#adp-wraper-" + k).css("top", v.y);
371             $("#adp-wraper-" + k).fadeIn();
372             $(".adp-wraper-backshadow").show()
373         };
374         n._hide = function () {
375             $("#adp-wraper-" + k).fadeOut();
376             $("div.adp-wraper-backshadow").hide()
377         };
378         n._on = function (v, w) {
379             $("#adp-wraper-" + k + " div ul").delegate("li", v, w)
380         };
381
382         function p(x) {
383             if (typeof x == "string") {
384                 try {
385                     var w = JSON.parse(x);
386                     if (typeof w == "object" && w) {
387                         return true
388                     } else {
389                         return false
390                     }
391                 } catch (v) {
392                     return false
393                 }
394             }
395         }
396
397         function u(w) {
398             var v = $("#" + t.id).offset().left - 10;
399             var C = $("#" + t.id).offset().top + $("#" + t.id).height() + 5;
400             if (t.isWithMouse) {
401                 var A = w || window.event;
402                 var B = document.documentElement.scrollLeft || document.body.scrollLeft;
403                 var z = document.documentElement.scrollTop || document.body.scrollTop;
404                 v = A.pageX || A.clientX + B;
405                 C = A.pageY || A.clientY + z
406             }
407             v += t.offsetX;
408             C += t.offsetY;
409             return {x: v, y: C}
410         }
411
412         function d() {
413             var w = "";
414             var y = $("head script");
415             if (y.length > 0) {
416                 for (var A = 0; A < y.length; A++) {
417                     if (y[A].src.indexOf("/js/address-picker.js") != -1) {
418                         w = y[A].src.substring(0, y[A].src.indexOf("/js/address-picker.js"));
419                         break
420                     }
421                 }
422             }
423             if (w != "") {
424                 var v = document.querySelector(":root");
425                 v.style.setProperty("--theme-color", t.color);
426                 v.style.setProperty("--font-size", t.fontSize);
427                 var z = document.getElementsByTagName("head")[0];
428                 var x = document.createElement("script");
429                 var B = document.createElement("link");
430                 B.href = w + "/css/address-picker.css";
431                 B.rel = "stylesheet";
432                 B.type = "text/css";
433                 z.appendChild(B);
434                 if (t.data == "") {
435                     if (t.level == 1 || t.level == 2) {
436                         t.data = w + "/data/pc-code.json"
437                     } else {
438                         if (t.level == 4 || t.level == 5) {
439                             t.data = w + "/data/pcas-code.json"
440                         } else {
441                             t.data = w + "/data/pca-code.json"
442                         }
443                     }
444                 }
445             }
446         }
447
448         function i() {
449             var v = true;
450             if (q instanceof Object) {
451                 t = $.extend({}, j, q)
452             } else {
453                 if (typeof (q) == "string") {
454                     t.id = q
455                 } else {
456                     v = false
457                 }
458             }
459             if (t.level > t.levelDesc.length) {
460                 t.levelDesc = j.levelDesc
461             }
462             if (t.color == "") {
463                 t.color = j.color
464             }
465             if (t.fontSize == "") {
466                 t.fontSize = j.fontSize
467             }
468             if (t.id == "" || t.id == undefined) {
469                 v = false
470             } else {
471                 if ($("#" + t.id).length == 0) {
472                     v = false
473                 }
474             }
475             if (t.isAsync && (typeof (t.asyncUrl) != "string" || t.asyncUrl == "")) {
476                 t.isAsync = j.isAsync
477             }
478             if (typeof (t.btnConfig) != "object" || t.btnConfig == null) {
479                 t.isShowBtn = false;
480                 t.btnConfig = j.btnConfig
481             }
482             return v
483         }
484
485         function s(y) {
486             var w = $("#adp-wraper-" + k).children("div.ul-div").eq(y);
487             var x = w.find("ul li");
488             var v = Math.ceil(x.length / 5) * (c + 16) + 10;
489             w.css("height", v > 65 ? v : 65 + "px")
490         }
491
492         function b() {
493             var x, v = "";
494             if (t.isShowBtn && (x = t.btnConfig).length > 0) {
495                 v = '<div class="adp-btn-area">';
496                 for (var w = 0; w < x.length; w++) {
497                     v += '<div id="adp_btn_' + w + '" class="adp-btn">' + x[w].text + "</div>"
498                 }
499                 v += "</div>"
500             }
501             return v
502         }
503
504         function f(z, y) {
505             var v = [];
506             if (t.isAsync) {
507                 var x = "", w = "", A = 1;
508                 if (z == null) {
509                     if ((typeof (l) != "undefined" && l != null)) {
510                         return l
511                     }
512                 } else {
513                     x = z.value;
514                     w = z.text ? z.text : z.text;
515                     A = y
516                 }
517                 $.ajax({
518                     data: {value: x, text: w, level: A}, url: t.asyncUrl, async: false, success: function (B) {
519                         if (typeof (B) == "string" && p(B)) {
520                             B = JSON.parse(B)
521                         }
522                         v = B;
523                         if (z == null && typeof (l) == "undefined") {
524                             l = B
525                         }
526                     }
527                 })
528             }
529             return v
530         }
531
532         n._init();
533         return {
534             on: n._on,
535             show: n._show,
536             hide: n._hide,
537             refreshData: n._refreshData,
538             getCurrentObject: n._getCurrentObject,
539             getTotalValueAsArray: n._getTotalValueAsArray,
540             getTotalValueAsText: n._getTotalValueAsText,
541             clearSelectedData: n._clearSelectedData,
542             setSelectedData: n._setSelectedData
543         }
544     };
545     window.addressPicker = a
546 }());