//// TODO 医院、社区列表示例数据 //var orgData = {"msg":"查询成功","list":[{"code":"3502050100","name":"海沧区嵩屿街道社区卫生服务中心",address:"升平路40号"},{"code":"3502050101","name":"海沧社区卫生服务站",address:"升平路40号"},{"code":"3502050200","name":"石塘社区卫生服务中心",address:"升平路40号"},{"code":"3502050300","name":"东孚卫生院",address:"升平路40号"},{"code":"3502050301","name":"天竺社区卫生服务站",address:"升平路40号"},{"code":"3502050302","name":"国营厦门第一农场社区卫生服务站",address:"升平路40号"},{"code":"3502050400","name":"新阳社区卫生服务中心",address:"升平路40号"},{"code":"0a11148d-5b04-11e6-8344-fa163e8aee56","name":"厦门市海沧医院","photo":"",address:"升平路40号"}],"status":200}; //// 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; var plus; // 基本信息(包括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'), $yiyuanWrapper = $('#yiyuan_wrapper'), $shequWrapper = $('#shequ_wrapper'), $yishengWrapper = $('#yisheng_wrapper'), $yiyuanList = $('#yiyuan_list'), $shequList = $('#shequ_list'), $yishengList = $('#yisheng_list'), $moreResult = $('.more-result'); // 获取基本信息(包括userAgent、上个页面传递的数据) var getBaseInfoPromise = function() { // 登录的相关信息 var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent")) return { userAgent: userAgent } }, // 获取基础环境信息 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; _.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.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) }); }, // 控制搜索关键字悬浮提示的显示 showSearchSuggest = function(text) { var suggestText = '搜索“'+text+'”'; // 如果text不为空,则显示;否则隐藏 if(text&&text.trim().length) { $searchSuggest.text(suggestText); $searchSuggest.show(); } else { $searchSuggest.text(''); $searchSuggest.hide(); } }, // 初始化“医院”视图列表 initYiYuanViewList = function(list) { var html = template("org_li_tmpl", {list: list.slice(0,3)}), searchText = $searchbarInput.val().trim(); // 搜索结果小于等于3个时,隐藏“查看更多” if(list.length<=3) { $yiyuanWrapper.find('.more-result').hide(); } else { $yiyuanWrapper.find('.more-result').show(); } $yiyuanList.html(html); ellipsisText($yiyuanList.find('.c-content-warp'),searchText); }, // 初始化“社区”视图列表 initSheQuViewList = function(list) { var html = template("org_li_tmpl", {list: list.slice(0,3)}), searchText = $searchbarInput.val().trim(); // 搜索结果小于等于3个时,隐藏“查看更多” if(list.length<=3) { $shequWrapper.find('.more-result').hide(); } else { $shequWrapper.find('.more-result').show(); } $shequList.html(html); ellipsisText($shequList.find('.c-content-warp'),searchText); }, // 初始化“医生”视图列表 initYiShengViewList = function(list) { var html = template("yisheng_li_tmpl", {list: list.slice(0,3)}), searchText = $searchbarInput.val().trim(); // 搜索结果小于等于3个时,隐藏“查看更多” if(list.length<=3) { $yishengWrapper.find('.more-result').hide(); } else { $yishengWrapper.find('.more-result').show(); } $yishengList.html(html); ellipsisText($yishengList.find('.c-content-warp'),searchText); }, // 搜索框搜索执行方法 search = function () { var kw = $searchbarInput.val().trim(), // "进行中"搜索结果列表数据 doingList = [], // "已结束"搜索结果列表数据 endList = [], // TODO promise请求测试示例 //promise = Promise.resolve(); promise = getReqPromises([{url:"doctor/hospitals",data:{id:0,key: kw, type:1, pagesize:4}}, {url:"doctor/hospitals",data:{id:0,key: kw,type:2,pagesize:4}}, {url:"/doctor/findDoctorByDept",data:{id:0,key: kw,page:1,pagesize:4}}]) // 隐藏搜索提示 showSearchSuggest(false); return kw && promise.then(function(datas){ // TODO 测试数据示例 // var yiyuanList = orgData.list, // shequList = orgData.list, // yishengList = doctorData.list; var yiyuanList = datas[0].list, shequList = datas[1].list, yishengList = datas[2].list; if(!yiyuanList.length && !shequList.length && !yishengList.length) { $noResultWrap.show(); } else { $noResultWrap.hide(); $searchtResult.show(); initYiYuanViewList(yiyuanList); initSheQuViewList(shequList); initYiShengViewList(yishengList); } }) }, // 绑定页面事件 bindEvents = function () { $searchbarInput.on('input', function() { var text = $(this).val().trim(); $searchtResult.hide(); showSearchSuggest(text); }).on('keydown',function(e) { if (e.which === 13) { search(); } }); $searchCancelBtn.on('click',function() { mui.back(); }); $searchSuggest.on('click',function() { $searchSuggest.hide(); search(); }); $moreResult.on('click',function() { var kw = $searchbarInput.val().trim(), href = $(this).attr('data-href'); openWebview(href,{accessData: { keyword: kw }}); }); $searchtResult.on('tap','.list-item',function() { var code = $(this).attr('data-code'); var oldWebview = plus.webview.getWebviewById('xuanzekeshi'); if(oldWebview) { oldWebview.close(); } openWebview("xuanzekeshi.html",{accessData: { hospital: code, createNew: true }}); }).on('tap','.yisheng-list-item',function() { var code = $(this).attr('data-code'); openWebview("doctor-homepage.html",{accessData: { doctor: code }}); }); /*聚焦搜索框事件*/ window.addEventListener("focusSearchInput", function group(e) { setTimeout(function() { $searchbarInput.focus(); },0) }); }; // 页面业务处理流程开始 new Promise(function(resolve, reject) { // TODO 临时放开 // resolve(true); mui.plusReady(function() { var xzksWebview = plus.webview.getWebviewById('xuanzekeshi'); if(xzksWebview) { xzksWebview.close(); } mui.back = backToWebviewById('xinzengmingyi'); // plus已经准备好,可以往下执行 resolve(true); }); }).then(function() { // 获取基础环境信息 return getBaseEnvPromise().then(function(env) { baseEnv = env; }).then(function() { // 获取登录医生信息 baseInfo = getBaseInfoPromise(); $searchbar.searchBar(); $searchbarInput.focus(); // 绑定页面事件 bindEvents(); }) }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); }); template.helper("setPhoto", function(p) { return getImgUrl(p); }); template.helper("setHosPhoto", function(p) { if(!p || p == ""){ return '../images/hospital_default.png'; } return getImgUrl(p); });