// TODO 医生列表示例数据 //var doctorData = {"msg":"查询成功","list":[ //{"id":"3502050100","consult":"",patient:"P123",photo:"",title:"",name:"张敏敏",age:23,comment:"激光治疗红血丝治疗部位保持清洁,避免感染和摩擦;部位有痂皮的会自行脱落,不要手动进行抠除。",star:3,type:5,time:"2016-09-12 16:49:17",status:"",sex:0,doctorRead:""}, //{"id":"3502050101","consult":"",patient:"P124",photo:"",title:"",name:"李佳佳",age:23,comment:"激光治疗红血丝治疗部位保持清洁",star:3,type:5,time:"2016-09-13 16:49:17",status:"",sex:0,doctorRead:""}, //{"id":"3502050100","consult":"",patient:"P123",photo:"",title:"",name:"张敏敏",age:23,comment:"激光治疗红血丝治疗部位保持清洁,避免感染和摩擦;部位有痂皮的会自行脱落,不要手动进行抠除。",star:3,type:5,time:"2016-09-12 16:49:17",status:"",sex:0,doctorRead:""}, //{"id":"3502050101","consult":"",patient:"P124",photo:"",title:"",name:"李佳佳",age:23,comment:"激光治疗红血丝治疗部位保持清洁",star:3,type:5,time:"2016-09-13 16:49:17",status:"",sex:0,doctorRead:""}],"status":200}; // //// TODO 临时构造plus对象,使得能够在浏览器中进行调试 //var plus = null; // 基本信息(包括userAgent、上个页面传递的数据) var baseInfo = null, // 基础环境信息(包括当前webview) baseEnv = null; // 搜索框 var $searchbar = $('.searchbar'), // 搜索输入框 $searchbarInput = $('.searchbar input'), // 搜索取消按钮 $searchCancelBtn = $('.searchbar-cancel'), // 搜索框下面悬浮的搜索提示 $searchSuggest = $('#search_suggest_text'), // 搜索结果展示容器 $searchtResult = $('#search_result'), // 搜索无结果时显示 $noResultWrap = $('#no_result_wrap'), $yishengList = $('#yisheng_list'); // 分页查询当前页数 var curPage = 1, // iscroll 滚动条实例 iscroller; // 获取基本信息(包括userAgent、上个页面传递的数据) var getBaseInfoPromise= function() { // 登录的相关信息 var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent")) return { userAgent: userAgent, accessData: baseEnv.webview.accessData } }, // 获取基础环境信息 getBaseEnvPromise = function () { var env = { webview: plus&&plus.webview.currentWebview() }; return Promise.resolve().then(function(res) { return env; }); }, // $el: $('.c-content-warp') getRowProps = function ($el) { var $textEllipsis = $el.find('.j-text-ellipsis'), $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() }; }, replaceAll = function (text, arr) { var html = text; if(!arr || !arr.length) return html; _.each(arr,function(kw) { var reg = new RegExp(kw+"(?!>)","gi"); html = html.replace(reg,''+kw+''); }); return html; }, highlineKeyword = function ($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.find('.j-text-ellipsis'), // 目标文本 text = $target.attr('data-text').trim(), length = text.length, // 关键字数组 kws = searchText&&searchText.trim().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)+"..."); } } }, ellipsisText = function ($elements, searchText) { //$el.ellipsis({ row: 2}); _.each($elements,function(el) { highlineKeyword($(el),searchText) }); }, // 初始化“医生”视图列表 initYiShengViewList = function(data,keyword,isAppend) { var html = template("yisheng_li_tmpl", {list: data.list}) if(isAppend === true) { $yishengList.append(html) } else { $yishengList.html(html); } ellipsisText($yishengList.find('.c-content-warp'),keyword); }, // 分页查询列表 searchByPaging = function () { var kw = $searchbarInput.val(); // TODO 示例示例搜索参数 // id: 上次搜索结果列表最后一条记录id,type:6(进行中),query:搜素关键字,pageSize:每页条数 var url = "/doctor/findDoctorByDept", params = {key: kw,page:curPage,pagesize:15}; getReqPromise(url,params).then(function(res){ var data = res; if(!iscroller) { iscroller = initScroller($searchtResult,url, function() { // 传递分页参数 return $.extend({},params,{page:curPage+1}); },function(data) { if(data.list && data.list.length) { curPage++; } var kw = $searchbarInput.val(); initYiShengViewList(data,kw,true); iscroller.refresh(); }); } // TODO 示例数据data if(!data.list || !data.list.length) { $searchtResult.hide(); $noResultWrap.show(); } else { $noResultWrap.hide(); // TODO 示例数据data initYiShengViewList(data,kw); } iscroller.refresh(); }).catch(function(e) { console && console.error(e) }); }, // 滚动条分页实例初始化 initScroller = function($el,url,getData,pullUpAction) { var scroller = $el.initScroll({pullDown: false,pullUpAction: function() { var data = getData(); getReqPromise(url,data).then(function(data) { if(pullUpAction && $.isFunction(pullUpAction)) { pullUpAction(data); updatePullUpText(scroller,data.list); } }) }}); return scroller; }, // 更新分页上拉加载的提示文本 updatePullUpText= function(scroller,list) { var $wrap = $(scroller.wrapper), $pullupLabel = $wrap.find('.pullUpLabel'); if(!list || !list.length) { $pullupLabel.text('没有更多'); } else { $pullupLabel.text('上拉加载更多'); } scroller.on('refresh',function() { if(!list || !list.length) { $pullupLabel.text('没有更多'); } else { $pullupLabel.text('上拉加载更多'); } }); }, // 绑定页面事件 bindEvents = function () { $searchbarInput.on('tap',function() { mui.fire(baseEnv.webview.opener(),"focusSearchInput"); mui.back(); }); $yishengList.on('tap','li',function() { var code = $(this).attr('data-code'); openWebview("doctor-homepage.html",{accessData: { doctor: code }}); }) }; // 页面业务处理流程开始 new Promise(function(resolve, reject) { // TODO 临时放开 //resolve(true); mui.plusReady(function() { // plus已经准备好,可以往下执行 resolve(true); }); }).then(function() { // 获取基础环境信息 return getBaseEnvPromise().then(function(env) { baseEnv = env; }).then(function() { // 获取登录医生信息 baseInfo = getBaseInfoPromise(); $searchbar.searchBar(); var keyword = baseInfo.accessData && baseInfo.accessData.keyword; if(keyword) { $searchbarInput.val(keyword); searchByPaging(); $searchtResult.show(); } else { $searchtResult.hide(); } // 绑定页面事件 bindEvents(); }) }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); }); template.helper("setPhoto", function(p) { return getImgUrl(p); });