hjg
2024-03-18 8d48c59d5d3bcc4148ef97dc6f98e3a8cf9ca436
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
(function () {
    Number.prototype.toPercent = function () {
        return (Math.round(this * 10000) / 100).toFixed(2) + "%"
    };
    var a = function (q) {
        if (!(this instanceof a)) {
            return new a(q)
        }
        var n = this;
        var h = {};
        var r = [];
        var g = {};
        var o;
        var e = [];
        var k = new Date().getTime();
        var j = {
            id: "",
            level: 3,
            levelDesc: ["省份", "城市", "区县"],
            index: "99999999999999999999",
            separator: " / ",
            isInitClick: true,
            isWithMouse: false,
            offsetX: 0,
            offsetY: 0,
            emptyText: "暂无数据",
            color: "#56b4f8",
            fontSize: "14px",
            isAsync: false,
            asyncUrl: "",
            isShowBtn: true,
            btnConfig: [],
            data: ""
        };
        var t = j;
        var l;
        var c = 20;
        var m = true;
        n._init = function () {
            if (!i()) {
                return
            }
            d();
            var x = '<div id="adp-wraper-' + k + '" class="adp-wraper" style="z-index: ' + t.index + '"><p>';
            var v = '<div class="ul-div" style="display: block"><ul></ul></div>';
            x += '<span class="adp-head-active">' + t.levelDesc[0] + "</span><span>" + t.levelDesc[0] + "</span>";
            for (var w = 1; w < t.level; w++) {
                x += "<span>" + t.levelDesc[w] + "</span>";
                v += '<div class="ul-div"><ul><span class="adp-empty-text">' + t.emptyText + "</span></ul></div>"
            }
            x += "</p>";
            var y = b();
           // $("body").append(x + v + y + '</div><div class="adp-wraper-backshadow" style="z-index: ' + (t.index - 1) + '"></div>');
            $("body").append(x + v + y + '</div>');
            if (t.isAsync) {
                o = f(null);
                n._initFirstLevelData()
            } else {
                if (typeof t.data == "string" && !p(t.data)) {
                    $.ajax({
                        url: t.data, async: false, success: function (z) {
                            o = z;
                            n._getEachLevelData(0, t.level, o);
                            n._initFirstLevelData()
                        }
                    })
                } else {
                    o = t.data;
                    if (typeof t.data == "string") {
                        o = JSON.parse(o)
                    }
                    n._getEachLevelData(0, t.level, o);
                    n._initFirstLevelData()
                }
            }
            n._bindEvent()
        };
        n._getEachLevelData = function (w, B, y) {
            var A = [];
            var z = [];
            if (w >= B) {
                return
            }
            for (var x = 0; x < y.length; x++) {
                A.push(y[x]);
                if (w == B - 1) {
                    continue
                }
                if (y[x].children == undefined) {
                    continue
                }
                for (var v = 0; v < y[x].children.length; v++) {
                    z.push(y[x].children[v])
                }
            }
            if (z.length == 0) {
                return
            }
            h["data_level_" + w] = A;
            n._getEachLevelData(w + 1, B, z)
        };
        n._initFirstLevelData = function () {
            for (var w = 0; w < t.level; w++) {
                $("#adp-wraper-" + k).children("div").eq(w).children("ul").empty();
                $("#adp-wraper-" + k).children("div").eq(w).children("ul").append('<span class="adp-empty-text">' + t.emptyText + "</span>")
            }
            if (o.length > 0) {
                $("#adp-wraper-" + k).children("div").eq(0).children("ul").empty()
            }
            for (var w = 0; w < o.length; w++) {
                var v = '<li data-code="' + o[w].value + '" title="' + o[w].text + '">' + o[w].text + "</li>";
                $("#adp-wraper-" + k).children("div").eq(0).children("ul").append(v)
            }
            var y = $("#adp-wraper-" + k).children("p").children("span").eq(1).text();
            var x = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
            x.css("left", "-1px");
            x.text(y);
            $("#adp-wraper-" + k).children("div.ul-div").eq(0).show().siblings("div.ul-div").hide();
            if (t.isShowBtn) {
                s(0)
            }
        };
        n._bindEvent = function () {
            $("#adp-wraper-" + k + " div ul").delegate("li", "click", function () {
                var z = $(this).parent().parent().index();
                g.value = $(this).data("code");
                g.text = $(this).text();
                g.level = z;
                r[z - 1] = $(this).data("code");
                e[z - 1] = $(this).text();
                r.splice(z, r.length - z);
                e.splice(z, e.length - z);
                var G = $("#adp-wraper-" + k).children("div").length;
                G = G -1
                // console.log(z,G,z>G)
                if (z < G) {
                    for (var F = z; F < t.level; F++) {
                        $("#adp-wraper-" + k).children("div.ul-div").eq(F).children("ul").empty().append('<span class="adp-empty-text">' + t.emptyText + "</span>")
                    }
                    $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").empty();
                    var x = t.isAsync ? f(g, z) : o;
                    if (z > 1 && !t.isAsync) {
                        x = h["data_level_" + (z - 1)]
                    }
                    if (x != undefined) {
                        for (var C = 0; C < x.length; C++) {
                            var y = x[C];
                            if (t.isAsync) {
                                var E = '<li data-code="' + y.value + '" title="' + y.text + '">' + y.text + "</li>";
                                $("#adp-wraper-" + k).children("div").eq(z).children("ul").append(E)
                            } else {
                                if (y.text == $(this).text() && y.value == $(this).data("code")) {
                                    if (y.children != null && y.children.length > 0) {
                                        for (var B = 0; B < y.children.length; B++) {
                                            var A = y.children[B];
                                            var E = '<li data-code="' + A.value + '" title="' + A.text + '">' + A.text + "</li>";
                                            $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").append(E)
                                        }
                                    } else {
                                        $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").append('<span class="adp-empty-text">' + t.emptyText + "</span>")
                                    }
                                    break
                                }
                            }
                        }
                        if (t.isShowBtn) {
                            s(z)
                        }
                    } else {
                        $("#adp-wraper-" + k).children("div.ul-div").eq(z).children("ul").append('<span class="adp-empty-text">' + t.emptyText + "</span>")
                    }
                    $(this).addClass("adp-active").siblings().removeClass("adp-active");
                    var D = $("#adp-wraper-" + k).children("p").children("span").eq(z + 1).text();
                    var H = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
                    H.css("left", (0.2 * (z)).toPercent());
                    $(this).parent().parent().next().show().siblings("div.ul-div").hide();
                    setTimeout(function () {
                        H.text(D)
                    }, 200)
                } else {
                    $(this).addClass("adp-active").siblings().removeClass("adp-active");
                    $("#adp-wraper-" + k).fadeOut();
                    $("div.adp-wraper-backshadow").hide()
                }
            });
            $("#adp-wraper-" + k + " p span").click(function () {
                if ($(this).hasClass("adp-head-active")) {
                    return
                }
                var x = $(this).index() - 1;
                var z = $(this).text();
                var y = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
                y.css("left", x == 0 ? -1 : (0.2 * x).toPercent());
                setTimeout(function () {
                    y.text(z)
                }, 200);
                $("#adp-wraper-" + k).children("div").eq(x).show().siblings("div.ul-div").hide()
            });
            $("div.adp-wraper-backshadow").on("click", function () {
                $("div.adp-wraper-backshadow").hide();
                $("#adp-wraper-" + k).fadeOut()
            });
            $("#adp-wraper-" + k).click(function(event) {
                $(document).one("click",
                    function() { //对document绑定一个影藏Div方法
                        $("#adp-wraper-" + k).hide();
                    });
                event.stopPropagation(); //阻止事件向上冒泡
            });
 
            if (t.isInitClick) {
                $("#" + t.id).on("click", function () {
                    var y = u(event);
                    var x = $("#adp-wraper-" + k + " div.ul-div").eq(0);
                    $("#adp-wraper-" + k).css("left", y.x);
                    $("#adp-wraper-" + k).css("top", y.y);
                    $("#adp-wraper-" + k).fadeIn();
                    if ((g.level == undefined || m) && t.isShowBtn) {
                        if (x.children("ul").children("li").height() != 0) {
                            c = x.children("ul").children("li").height()
                        }
                        s(0);
                        m = false
                    }
                    $(".adp-wraper-backshadow").show()
                })
            }
            if (t.isShowBtn) {
                var w = t.btnConfig;
                for (var v = 0; v < w.length; v++) {
                    if (typeof (w[v].click) == "function") {
                        $("#adp_btn_" + v).on("click", w[v].click)
                    }
                }
            }
        };
        n._clearSelectedData = function () {
            r = [];
            e = [];
            g = {};
            var x = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
            x.css("left", "-1px");
            setTimeout(function () {
                x.text(t.levelDesc[0])
            }, 200);
            for (var w = 1; w < t.level; w++) {
                $("#adp-wraper-" + k).children("div.ul-div").eq(w).children("ul").empty().append('<span class="adp-empty-text">' + t.emptyText + "</span>");
                if (t.isShowBtn) {
                    s(w)
                }
            }
            var v;
            if ((v = $("#adp-wraper-" + k).children("div.ul-div").eq(0).children("ul").children("li.adp-active")).length > 0) {
                v.removeClass("adp-active")
            }
            $("#adp-wraper-" + k).children("div").eq(0).show().siblings("div.ul-div").hide()
        };
        n._setSelectedData = function (E) {
            if (typeof (E) != "object" || E == null) {
                return
            }
            if (E.length > t.level || E.length == 0) {
                return
            }
            var v = t.isAsync ? f(null) : o;
            if (typeof (v) != "object" || v == null) {
                return
            }
            var x, G, H, K, I, J = "", D = false, C;
            H = $("#adp-wraper-" + k).children("div.ul-div").eq(0).children("ul");
            for (var z = 0; z < (K = H.children("li")).length; z++) {
                x = v[z];
                if (x.value == E[0]) {
                    g.value = x.value;
                    g.text = x.text;
                    g.level = 1;
                    r[0] = x.value;
                    e[0] = x.text;
                    r.splice(1, r.length - 1);
                    e.splice(1, e.length - 1);
                    G = x;
                    C = 0;
                    D = true;
                    H.children("li").removeClass("adp-active");
                    $(K[z]).addClass("adp-active");
                    continue
                }
            }
            if (!D) {
                return
            }
            for (var B = 1; B < E.length; B++) {
                if (E[B] == null || E[B] == "") {
                    break
                }
                var w = t.isAsync ? f(G, g.level) : G.children;
                if (typeof (w) != "object" || w == null) {
                    break
                }
                J = "";
                D = false;
                G = {};
                for (var A = 0; A < w.length; A++) {
                    if (w[A].value == E[B]) {
                        J += '<li data-code="' + w[A].value + '" title="' + w[A].text + '" class="adp-active">' + w[A].text + "</li>";
                        g.value = w[A].value;
                        g.text = w[A].text;
                        g.level = (B + 1);
                        r[B] = w[A].value;
                        e[B] = w[A].text;
                        r.splice(B + 1, r.length - B - 1);
                        e.splice(B + 1, e.length - B - 1);
                        G = w[A];
                        D = true;
                        C = B;
                        continue
                    }
                    J += '<li data-code="' + w[A].value + '" title="' + w[A].text + '">' + w[A].text + "</li>"
                }
                if (!D) {
                    break
                }
                $("#adp-wraper-" + k).children("div.ul-div").eq(B).children("ul").empty().append(J);
                if (t.isShowBtn) {
                    s(B)
                }
            }
            if (typeof (C) == "number") {
                $("#adp-wraper-" + k).children("div.ul-div").eq(C).show().siblings("div.ul-div").hide();
                var F = $("#adp-wraper-" + k).children("p").children("span").eq(C + 1).text();
                var L = $("#adp-wraper-" + k).children("p").children("span.adp-head-active");
                L.css("left", (0.2 * (C)).toPercent());
                setTimeout(function () {
                    L.text(F)
                }, 200);
                for (var y = C + 1; y < t.level; y++) {
                    $("#adp-wraper-" + k).children("div.ul-div").eq(y).children("ul").empty().append('<span class="adp-empty-text">' + t.emptyText + "</span>");
                    if (t.isShowBtn) {
                        s(y)
                    }
                }
            }
        };
        n._refreshData = function (v) {
            if (typeof v == "string" && p(v)) {
                v = JSON.parse(v)
            }
            o = v;
            if (o.length > 0) {
                n._getEachLevelData(0, t.level, o)
            }
            n._initFirstLevelData()
        };
        n._getTotalValueAsArray = function () {
            return {value: r, text: e}
        };
        n._getTotalValueAsText = function () {
            var w = "";
            for (var v = 0; v < g.level; v++) {
                w += (v < g.level - 1 ? e[v] + t.separator : e[v])
            }
            return w
        };
        n._getCurrentObject = function () {
            return g
        };
        n._show = function () {
            var v = u(event);
            $("#adp-wraper-" + k).css("left", v.x);
            $("#adp-wraper-" + k).css("top", v.y);
            $("#adp-wraper-" + k).fadeIn();
            $(".adp-wraper-backshadow").show()
        };
        n._hide = function () {
            $("#adp-wraper-" + k).fadeOut();
            $("div.adp-wraper-backshadow").hide()
        };
        n._on = function (v, w) {
            $("#adp-wraper-" + k + " div ul").delegate("li", v, w)
        };
 
        function p(x) {
            if (typeof x == "string") {
                try {
                    var w = JSON.parse(x);
                    if (typeof w == "object" && w) {
                        return true
                    } else {
                        return false
                    }
                } catch (v) {
                    return false
                }
            }
        }
 
        function u(w) {
            var v = $("#" + t.id).offset().left - 10;
            var C = $("#" + t.id).offset().top + $("#" + t.id).height() + 5;
            if (t.isWithMouse) {
                var A = w || window.event;
                var B = document.documentElement.scrollLeft || document.body.scrollLeft;
                var z = document.documentElement.scrollTop || document.body.scrollTop;
                v = A.pageX || A.clientX + B;
                C = A.pageY || A.clientY + z
            }
            v += t.offsetX;
            C += t.offsetY;
            return {x: v, y: C}
        }
 
        function d() {
            var w = "";
            var y = $("head script");
            if (y.length > 0) {
                for (var A = 0; A < y.length; A++) {
                    if (y[A].src.indexOf("/js/address-picker.js") != -1) {
                        w = y[A].src.substring(0, y[A].src.indexOf("/js/address-picker.js"));
                        break
                    }
                }
            }
            if (w != "") {
                var v = document.querySelector(":root");
                v.style.setProperty("--theme-color", t.color);
                v.style.setProperty("--font-size", t.fontSize);
                var z = document.getElementsByTagName("head")[0];
                var x = document.createElement("script");
                var B = document.createElement("link");
                B.href = w + "/css/address-picker.css";
                B.rel = "stylesheet";
                B.type = "text/css";
                z.appendChild(B);
                if (t.data == "") {
                    if (t.level == 1 || t.level == 2) {
                        t.data = w + "/data/pc-code.json"
                    } else {
                        if (t.level == 4 || t.level == 5) {
                            t.data = w + "/data/pcas-code.json"
                        } else {
                            t.data = w + "/data/pca-code.json"
                        }
                    }
                }
            }
        }
 
        function i() {
            var v = true;
            if (q instanceof Object) {
                t = $.extend({}, j, q)
            } else {
                if (typeof (q) == "string") {
                    t.id = q
                } else {
                    v = false
                }
            }
            if (t.level > t.levelDesc.length) {
                t.levelDesc = j.levelDesc
            }
            if (t.color == "") {
                t.color = j.color
            }
            if (t.fontSize == "") {
                t.fontSize = j.fontSize
            }
            if (t.id == "" || t.id == undefined) {
                v = false
            } else {
                if ($("#" + t.id).length == 0) {
                    v = false
                }
            }
            if (t.isAsync && (typeof (t.asyncUrl) != "string" || t.asyncUrl == "")) {
                t.isAsync = j.isAsync
            }
            if (typeof (t.btnConfig) != "object" || t.btnConfig == null) {
                t.isShowBtn = false;
                t.btnConfig = j.btnConfig
            }
            return v
        }
 
        function s(y) {
            var w = $("#adp-wraper-" + k).children("div.ul-div").eq(y);
            var x = w.find("ul li");
            var v = Math.ceil(x.length / 5) * (c + 16) + 10;
            w.css("height", v > 65 ? v : 65 + "px")
        }
 
        function b() {
            var x, v = "";
            if (t.isShowBtn && (x = t.btnConfig).length > 0) {
                v = '<div class="adp-btn-area">';
                for (var w = 0; w < x.length; w++) {
                    v += '<div id="adp_btn_' + w + '" class="adp-btn">' + x[w].text + "</div>"
                }
                v += "</div>"
            }
            return v
        }
 
        function f(z, y) {
            var v = [];
            if (t.isAsync) {
                var x = "", w = "", A = 1;
                if (z == null) {
                    if ((typeof (l) != "undefined" && l != null)) {
                        return l
                    }
                } else {
                    x = z.value;
                    w = z.text ? z.text : z.text;
                    A = y
                }
                $.ajax({
                    data: {value: x, text: w, level: A}, url: t.asyncUrl, async: false, success: function (B) {
                        if (typeof (B) == "string" && p(B)) {
                            B = JSON.parse(B)
                        }
                        v = B;
                        if (z == null && typeof (l) == "undefined") {
                            l = B
                        }
                    }
                })
            }
            return v
        }
 
        n._init();
        return {
            on: n._on,
            show: n._show,
            hide: n._hide,
            refreshData: n._refreshData,
            getCurrentObject: n._getCurrentObject,
            getTotalValueAsArray: n._getTotalValueAsArray,
            getTotalValueAsText: n._getTotalValueAsText,
            clearSelectedData: n._clearSelectedData,
            setSelectedData: n._setSelectedData
        }
    };
    window.addressPicker = a
}());