mubansousuo.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. $(function(){
  2. mui.init({
  3. pullRefresh : {
  4. container:'.mui-scroll-wrapper',
  5. up : {
  6. height:50,//可选.默认50.触发上拉加载拖动距离
  7. auto:false,//可选,默认false.自动上拉加载一次
  8. contentrefresh : "正在加载...",
  9. contentnomore:'没有更多数据了',
  10. callback: function() {
  11. // this.endPullupToRefresh(false)
  12. }
  13. }
  14. }
  15. })
  16. // mui('.mui-scroll-wrapper').pullRefresh().disablePullupToRefresh();
  17. var $searchbarInput = $('#searchPut'),
  18. $searchSuggest = $('#search_suggest_text'),// 搜索框下面悬浮的搜索提示
  19. search_keyword = "",//记录搜索框的内容
  20. $noResultWrap = $('#no_result_wrap'),
  21. $searchResult = $('#search_result');
  22. $searchbarInput.focus();
  23. //监视输入
  24. $searchbarInput.on('input', function() {
  25. var text = $(this).val().trim();
  26. $searchResult.hide();
  27. $noResultWrap.hide();
  28. showSearchSuggest(text);
  29. }).on('keydown',function(e) {
  30. if (e.which === 13) {
  31. searchByPaging()
  32. }
  33. })
  34. //查询
  35. function searchByPaging(){
  36. $searchbarInput.blur();
  37. search_keyword = $searchbarInput.val();
  38. if(!$.trim(search_keyword)) {
  39. return ;
  40. }
  41. ellipsisText($searchResult.find('.mb-li'),search_keyword)
  42. $searchSuggest.hide();
  43. $noResultWrap.hide();
  44. $searchResult.show();
  45. // plus.nativeUI.showWaiting();
  46. // plus.nativeUI.closeWaiting();
  47. }
  48. $searchSuggest.on('click',function() {
  49. searchByPaging()
  50. })
  51. // 控制搜索关键字悬浮提示的显示
  52. function showSearchSuggest(text) {
  53. var suggestText = '搜索“'+text+'”';
  54. // 如果text不为空,则显示;否则隐藏
  55. if(text&&text.trim().length) {
  56. $searchSuggest.text(suggestText);
  57. $searchSuggest.show();
  58. } else {
  59. $searchSuggest.text('');
  60. $searchSuggest.hide();
  61. }
  62. }
  63. //关键字变色
  64. function ellipsisText($elements, searchText) {
  65. _.each($elements,function(el) {
  66. var $textEl = $(el).find(".j-text-ellipsis");
  67. _.each($textEl, function(t){
  68. highlineKeyword($(t),searchText)
  69. })
  70. })
  71. }
  72. function highlineKeyword($el,searchText) {
  73. var props = getRowProps($el),
  74. chartWidth = props.chartWidth,
  75. rowHeight = props.rowHeight,
  76. rowWidth = props.rowWidth,
  77. // 每行显示字符数(以中文字符为标准计算)
  78. chartNum = Math.floor(rowWidth / chartWidth.zh),
  79. // 排除指定数量字符所占宽度
  80. exceptNum = 0,
  81. // 行数
  82. rowNum = 1,
  83. // 预计显示总字符数
  84. expectedNum = chartNum * rowNum - exceptNum,
  85. $target = $el,
  86. // 目标文本
  87. text = $.trim($target.attr('data-text')),
  88. length = text.length,
  89. // 关键字数组
  90. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  91. var fidx = 0,preFidx,diff = 0;
  92. $target.html(replaceAll(text, kws));
  93. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  94. return ;
  95. }
  96. // if(text.length > expectedNum) {
  97. // fidx = text.indexOf(kws[0])+kws[0].length-1;
  98. // diff = fidx - expectedNum + 1;
  99. // preFidx = fidx;
  100. // diff = (diff<0)?0:diff;
  101. // var preChar = (diff>0)?"...":"";
  102. // $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  103. // while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  104. // preFidx++;
  105. // $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  106. // }
  107. // if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  108. // diff = diff>0?(diff - 1):0;
  109. // $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  110. // } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  111. // $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  112. // }
  113. // }
  114. }
  115. function getRowProps($el) {
  116. var $textEllipsis = $el,
  117. $text = $textEllipsis.eq(0),
  118. $chart = $text.text('a'),
  119. enWidth = $chart.width(),
  120. $chart = $text.text('中'),
  121. zhWidth = $chart.width(),
  122. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  123. rowHeight = $chart.height();
  124. $chart.text('');
  125. return {
  126. chartWidth: {
  127. zh: zhWidth,
  128. en: enWidth
  129. },
  130. rowHeight: Math.max(rowHeight, lineHeight),
  131. rowWidth: $el.width()
  132. };
  133. }
  134. function replaceAll(text, arr) {
  135. var html = text;
  136. _.each(arr,function(kw) {
  137. var reg = new RegExp(kw+"(?!>)","gi");
  138. html = html.replace(reg,'<em>'+kw+'</em>');
  139. });
  140. return html;
  141. }
  142. //取消
  143. $('.searchbar-cancel').on('click',function() {
  144. mui.back();
  145. })
  146. //点击显示详情
  147. $searchResult.on('click','.j-li-detail',function(){
  148. var $this = $(this);
  149. mui.openWindow({
  150. id: "mubanxiangqing",
  151. url: "mubanxiangqing.html",
  152. extras: {
  153. }
  154. })
  155. })
  156. //点击选择
  157. $searchResult.on('click','.j-choose-btn',function(event){
  158. var $this = $(this);
  159. event.stopPropagation();
  160. backIndex()
  161. })
  162. function backIndex(){
  163. //删掉搜索页面
  164. var last = plus.webview.getWebviewById("chufangmuban");
  165. if(last){
  166. last.close();
  167. }
  168. var last1 = plus.webview.getWebviewById("mubansousuo");
  169. if(last1){
  170. last1.close();
  171. }
  172. var last2 = plus.webview.getWebviewById("mubanxiangqing");
  173. if(last2){
  174. last2.close();
  175. }
  176. }
  177. })