$(function(){ mui.init({ pullRefresh : { container:'.mui-scroll-wrapper', up : { height:50,//可选.默认50.触发上拉加载拖动距离 auto:false,//可选,默认false.自动上拉加载一次 contentrefresh : "正在加载...", contentnomore:'没有更多数据了', callback: function() { // this.endPullupToRefresh(false) } } } }) // mui('.mui-scroll-wrapper').pullRefresh().disablePullupToRefresh(); var $searchbarInput = $('#searchPut'), $searchSuggest = $('#search_suggest_text'),// 搜索框下面悬浮的搜索提示 search_keyword = "",//记录搜索框的内容 $noResultWrap = $('#no_result_wrap'), $searchResult = $('#search_result'); $searchbarInput.focus(); //监视输入 $searchbarInput.on('input', function() { var text = $(this).val().trim(); $searchResult.hide(); $noResultWrap.hide(); showSearchSuggest(text); }).on('keydown',function(e) { if (e.which === 13) { searchByPaging() } }) //查询 function searchByPaging(){ $searchbarInput.blur(); search_keyword = $searchbarInput.val(); if(!$.trim(search_keyword)) { return ; } ellipsisText($searchResult.find('.mb-li'),search_keyword) $searchSuggest.hide(); $noResultWrap.hide(); $searchResult.show(); // plus.nativeUI.showWaiting(); // plus.nativeUI.closeWaiting(); } $searchSuggest.on('click',function() { searchByPaging() }) // 控制搜索关键字悬浮提示的显示 function showSearchSuggest(text) { var suggestText = '搜索“'+text+'”'; // 如果text不为空,则显示;否则隐藏 if(text&&text.trim().length) { $searchSuggest.text(suggestText); $searchSuggest.show(); } else { $searchSuggest.text(''); $searchSuggest.hide(); } } //关键字变色 function ellipsisText($elements, searchText) { _.each($elements,function(el) { var $textEl = $(el).find(".j-text-ellipsis"); _.each($textEl, function(t){ highlineKeyword($(t),searchText) }) }) } function highlineKeyword($el,searchText) { var props = getRowProps($el), chartWidth = props.chartWidth, rowHeight = props.rowHeight, rowWidth = props.rowWidth, // 每行显示字符数(以中文字符为标准计算) chartNum = Math.floor(rowWidth / chartWidth.zh), // 排除指定数量字符所占宽度 exceptNum = 0, // 行数 rowNum = 1, // 预计显示总字符数 expectedNum = chartNum * rowNum - exceptNum, $target = $el, // 目标文本 text = $.trim($target.attr('data-text')), length = text.length, // 关键字数组 kws = $.trim(searchText).replace(/\s+/g," ").split(" "); var fidx = 0,preFidx,diff = 0; $target.html(replaceAll(text, kws)); if(Math.floor($target.height() / rowHeight) <= rowNum) { return ; } // if(text.length > expectedNum) { // fidx = text.indexOf(kws[0])+kws[0].length-1; // diff = fidx - expectedNum + 1; // preFidx = fidx; // diff = (diff<0)?0:diff; // var preChar = (diff>0)?"...":""; // $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"..."); // while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) { // preFidx++; // $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"..."); // } // if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) { // diff = diff>0?(diff - 1):0; // $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)); // } else if((Math.floor($target.height() / rowHeight) > rowNum)) { // $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"..."); // } // } } function getRowProps($el) { var $textEllipsis = $el, $text = $textEllipsis.eq(0), $chart = $text.text('a'), enWidth = $chart.width(), $chart = $text.text('中'), zhWidth = $chart.width(), lineHeight = parseFloat($chart.css("lineHeight"), 10), rowHeight = $chart.height(); $chart.text(''); return { chartWidth: { zh: zhWidth, en: enWidth }, rowHeight: Math.max(rowHeight, lineHeight), rowWidth: $el.width() }; } function replaceAll(text, arr) { var html = text; _.each(arr,function(kw) { var reg = new RegExp(kw+"(?!>)","gi"); html = html.replace(reg,''+kw+''); }); return html; } //取消 $('.searchbar-cancel').on('click',function() { mui.back(); }) //点击显示详情 $searchResult.on('click','.j-li-detail',function(){ var $this = $(this); mui.openWindow({ id: "mubanxiangqing", url: "mubanxiangqing.html", extras: { } }) }) //点击选择 $searchResult.on('click','.j-choose-btn',function(event){ var $this = $(this); event.stopPropagation(); backIndex() }) function backIndex(){ //删掉搜索页面 var last = plus.webview.getWebviewById("chufangmuban"); if(last){ last.close(); } var last1 = plus.webview.getWebviewById("mubansousuo"); if(last1){ last1.close(); } var last2 = plus.webview.getWebviewById("mubanxiangqing"); if(last2){ last2.close(); } } })