Brak opisu

city_picker2.js 5.3KB

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