|
@@ -0,0 +1,154 @@
|
|
1
|
+(function($, win, doc) {
|
|
2
|
+ var CityPicker = function(ele, cities) {
|
|
3
|
+ this.ele = $(ele);
|
|
4
|
+ this.cities = cities || [];
|
|
5
|
+ this.hotCities = cities.hot || [];
|
|
6
|
+ this.city = null;
|
|
7
|
+ this.code = null;
|
|
8
|
+ this.init();
|
|
9
|
+ };
|
|
10
|
+
|
|
11
|
+ var p = CityPicker.prototype;
|
|
12
|
+
|
|
13
|
+ p.init = function() {
|
|
14
|
+ this.renderHotCityList();
|
|
15
|
+ this.renderCityList();
|
|
16
|
+ this.initCityListEvent();
|
|
17
|
+ this.renderCityNav();
|
|
18
|
+ this.initCityNavEvent();
|
|
19
|
+ };
|
|
20
|
+
|
|
21
|
+ p.renderCityList = function() {
|
|
22
|
+ var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split("");
|
|
23
|
+ var cities = this.cities;
|
|
24
|
+ var cityListStr = "";
|
|
25
|
+ for (var letter of letters) {
|
|
26
|
+ var val = cities[letter];
|
|
27
|
+ if (val && val.length > 0) {
|
|
28
|
+ var dt = "<dt id='" + letter + "'>" + letter + "</dt>";
|
|
29
|
+ var dd = "";
|
|
30
|
+ for (var city of val) {
|
|
31
|
+ dd += "<dd data-letter='" + letter + "'' data-code='" + city.regionCode + "'>" + city.region + "</dd>";
|
|
32
|
+ }
|
|
33
|
+ cityListStr += "<dl>" + dt + dd + "</dl>";
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ $(".city-list").append(cityListStr);
|
|
37
|
+ };
|
|
38
|
+
|
|
39
|
+ p.renderHotCityList = function() {
|
|
40
|
+ var hotCitiesStr = '';
|
|
41
|
+ for (var city of this.hotCities) {
|
|
42
|
+ hotCitiesStr += "<li data-code='" + city.regionCode + "'>" + city.region + "</li>"
|
|
43
|
+ }
|
|
44
|
+ $('.city-list-hot').html(hotCitiesStr);
|
|
45
|
+ };
|
|
46
|
+
|
|
47
|
+ p.initCityListEvent = function() {
|
|
48
|
+ var that = this;
|
|
49
|
+ $(".city-list-wrapper").on("click", function(e) {
|
|
50
|
+ var target = e.target;
|
|
51
|
+ if ($(target).is("dd") || $(target).is("li")) {
|
|
52
|
+ that.city = $(target).text().trim();
|
|
53
|
+ that.code = $(target).attr('data-code');
|
|
54
|
+ var letter = $(target).data("letter");
|
|
55
|
+ $('.current-city span').text(that.city);
|
|
56
|
+
|
|
57
|
+ var data = {
|
|
58
|
+ name: that.city,
|
|
59
|
+ code: that.code
|
|
60
|
+ }
|
|
61
|
+ CityLocationMessageHandler(data)
|
|
62
|
+ window.history.go(-1)
|
|
63
|
+// if (document.referrer.indexOf('&name=') === -1) {
|
|
64
|
+// window.location.href = './fund_h5_api.html?token=' + token + '&name=' + that.city + '&code=' + that.code;
|
|
65
|
+// } else {
|
|
66
|
+// window.location.href = document.referrer.replace(/&name=[^&]+&code=[^&]+/, '&name=' + that.city + '&code=' + that.code);
|
|
67
|
+// }
|
|
68
|
+ }
|
|
69
|
+ });
|
|
70
|
+ };
|
|
71
|
+
|
|
72
|
+ p.renderCityNav = function() {
|
|
73
|
+ // var str = "热" + Object.keys(this.cities).toString().replace(/,/g, '');
|
|
74
|
+ var str = '热ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
75
|
+ var arr = str.split("");
|
|
76
|
+ var a = "<a href='#top' class='to-top'></a>";
|
|
77
|
+ arr.forEach(function(item, i) {
|
|
78
|
+ if (item === '热') {
|
|
79
|
+ a += '<a href="#hot">' + item + '</a>';
|
|
80
|
+ } else {
|
|
81
|
+ a += '<a href="#' + item + '">' + item + '</a>';
|
|
82
|
+ }
|
|
83
|
+ });
|
|
84
|
+ var div = '<div class="navbar">' + a + '</div>';
|
|
85
|
+ $(".city-list-wrapper").append(div);
|
|
86
|
+ };
|
|
87
|
+
|
|
88
|
+ p.initCityNavEvent = function() {
|
|
89
|
+ var that = this;
|
|
90
|
+ var navBar = $(".navbar");
|
|
91
|
+ var width = navBar.find("a").width();
|
|
92
|
+ var height = navBar.find("a").height();
|
|
93
|
+ navBar.on("touchstart", function(e) {
|
|
94
|
+ $(this).addClass("active");
|
|
95
|
+ that.createLetterPrompt($(e.target).html());
|
|
96
|
+ });
|
|
97
|
+ navBar.on("touchmove", function(e) {
|
|
98
|
+ e.preventDefault();
|
|
99
|
+ var touch = e.originalEvent.touches[0];
|
|
100
|
+ var pos = { "x": touch.pageX, "y": touch.pageY };
|
|
101
|
+ var x = pos.x,
|
|
102
|
+ y = pos.y;
|
|
103
|
+ $(this).find("a").each(function(i, item) {
|
|
104
|
+ var offset = $(item).offset();
|
|
105
|
+ var left = offset.left,
|
|
106
|
+ top = offset.top;
|
|
107
|
+ if (x > left && x < (left + width) && y > top && y < (top + height)) {
|
|
108
|
+ that.changeLetter($(item).html());
|
|
109
|
+ var targetEle = $($(item).attr('href'));
|
|
110
|
+ if (targetEle.length > 0) {
|
|
111
|
+ targetEle[0].scrollIntoView();
|
|
112
|
+ window.scrollBy(0, -56);
|
|
113
|
+ }
|
|
114
|
+ return e.preventDefault();
|
|
115
|
+ }
|
|
116
|
+ });
|
|
117
|
+ });
|
|
118
|
+
|
|
119
|
+ navBar.on("touchend", function(e) {
|
|
120
|
+ $(this).removeClass("active");
|
|
121
|
+ $(".prompt").hide();
|
|
122
|
+ });
|
|
123
|
+
|
|
124
|
+ navBar.click(function(e) {
|
|
125
|
+ var currentEle = $(e.target);
|
|
126
|
+ that.changeLetter(currentEle.html());
|
|
127
|
+ var targetEle = $(currentEle.attr('href'));
|
|
128
|
+ if (targetEle.length > 0) {
|
|
129
|
+ targetEle[0].scrollIntoView();
|
|
130
|
+ window.scrollBy(0, -56);
|
|
131
|
+ }
|
|
132
|
+ return e.preventDefault();
|
|
133
|
+ });
|
|
134
|
+ };
|
|
135
|
+
|
|
136
|
+ p.createLetterPrompt = function(letter) {
|
|
137
|
+ var prompt = $(".prompt");
|
|
138
|
+ if (prompt[0]) {
|
|
139
|
+ prompt.show();
|
|
140
|
+ } else {
|
|
141
|
+ var span = "<span class='prompt'>" + letter + "</span>";
|
|
142
|
+ $(".city-list").append(span);
|
|
143
|
+ }
|
|
144
|
+ };
|
|
145
|
+
|
|
146
|
+ p.changeLetter = function(letter) {
|
|
147
|
+ var prompt = $(".prompt");
|
|
148
|
+ prompt.html(letter);
|
|
149
|
+ };
|
|
150
|
+
|
|
151
|
+ $.fn.CityPicker = function(cities) {
|
|
152
|
+ return new CityPicker(this, cities);
|
|
153
|
+ }
|
|
154
|
+}(window.jQuery, window, document));
|