Ei kuvausta

city_picker.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 += "<div><li data-code='" + city.regionCode + "'>" + city.region + "</li></div>"
  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. if (document.referrer.indexOf('&name=') === -1) {
  52. window.location.href = './fund_h5_api.html?token=' + token + '&name=' + that.city + '&code=' + that.code;
  53. } else {
  54. window.location.href = document.referrer.replace(/&name=[^&]+&code=[^&]+/, '&name=' + that.city + '&code=' + that.code);
  55. }
  56. }
  57. });
  58. };
  59. p.renderCityNav = function() {
  60. // var str = "热" + Object.keys(this.cities).toString().replace(/,/g, '');
  61. var str = '热ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  62. var arr = str.split("");
  63. var a = "<a href='#top' class='to-top'></a>";
  64. arr.forEach(function(item, i) {
  65. if (item === '热') {
  66. a += '<a href="#hot">' + item + '</a>';
  67. } else {
  68. a += '<a href="#' + item + '">' + item + '</a>';
  69. }
  70. });
  71. var div = '<div class="navbar">' + a + '</div>';
  72. $(".city-list-wrapper").append(div);
  73. };
  74. p.initCityNavEvent = function() {
  75. var that = this;
  76. var navBar = $(".navbar");
  77. var width = navBar.find("a").width();
  78. var height = navBar.find("a").height();
  79. navBar.on("touchstart", function(e) {
  80. $(this).addClass("active");
  81. that.createLetterPrompt($(e.target).html());
  82. });
  83. navBar.on("touchmove", function(e) {
  84. e.preventDefault();
  85. var touch = e.originalEvent.touches[0];
  86. var pos = { "x": touch.pageX, "y": touch.pageY };
  87. var x = pos.x,
  88. y = pos.y;
  89. $(this).find("a").each(function(i, item) {
  90. var offset = $(item).offset();
  91. var left = offset.left,
  92. top = offset.top;
  93. if (x > left && x < (left + width) && y > top && y < (top + height)) {
  94. that.changeLetter($(item).html());
  95. var targetEle = $($(item).attr('href'));
  96. if (targetEle.length > 0) {
  97. targetEle[0].scrollIntoView();
  98. window.scrollBy(0, -56);
  99. }
  100. return e.preventDefault();
  101. }
  102. });
  103. });
  104. navBar.on("touchend", function(e) {
  105. $(this).removeClass("active");
  106. $(".prompt").hide();
  107. });
  108. navBar.click(function(e) {
  109. var currentEle = $(e.target);
  110. that.changeLetter(currentEle.html());
  111. var targetEle = $(currentEle.attr('href'));
  112. if (targetEle.length > 0) {
  113. targetEle[0].scrollIntoView();
  114. window.scrollBy(0, -56);
  115. }
  116. return e.preventDefault();
  117. });
  118. };
  119. p.createLetterPrompt = function(letter) {
  120. var prompt = $(".prompt");
  121. if (prompt[0]) {
  122. prompt.show();
  123. } else {
  124. var span = "<span class='prompt'>" + letter + "</span>";
  125. $(".city-list").append(span);
  126. }
  127. };
  128. p.changeLetter = function(letter) {
  129. var prompt = $(".prompt");
  130. prompt.html(letter);
  131. };
  132. $.fn.CityPicker = function(cities) {
  133. return new CityPicker(this, cities);
  134. }
  135. }(window.jQuery, window, document));