| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 | 
							- // TODO 临时构造plus对象,使得能够在浏览器中进行调试
 
- //var plus = null;
 
- 	// 登录者相关信息(包括userAgent)
 
- var loginerInfo = null,
 
- 	// 基础环境信息(包括当前webview)
 
- 	baseEnv = null,
 
- 	self = null,
 
- 	activeType = null;
 
- 	
 
- 	// 搜索框
 
- var $searchbar = $('.searchbar'),
 
- 	// 搜索输入框
 
- 	$searchbarInput = $('.searchbar input'),
 
- 	// 搜索取消按钮
 
- 	$searchCancelBtn = $('.searchbar-cancel'),
 
- 	// 搜索框下面悬浮的搜索提示
 
- 	$searchSuggest = $('#search_suggest_text'),
 
- 	// 搜索结果展示容器
 
- 	$searchtResult = $('#search_result'),
 
- 	// 搜索无结果时显示
 
- 	$noResultWrap = $('#no_result_wrap'),
 
- 	$doingWrapper = $('#doing_wrapper'),
 
- 	$endWrapper = $('#end_wrapper'),
 
- 	$doingList = $('#doing_list'),
 
- 	$endList = $('#end_list'),
 
- 	$moreResult = $('.more-result');
 
- 	
 
- 	mui.plusReady(function() {
 
- 	 self = plus.webview.currentWebview();
 
-      activeType = self.type;//1为咨询我的 2为我咨询的
 
- });
 
- // 获取登录相关信息
 
- var getLoginerInfo = 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,'<em>'+kw+'</em>');
 
- 	});
 
- 	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 = 7,
 
- 		// 行数
 
- 		rowNum = 2,
 
- 		// 预计显示总字符数
 
- 		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();
 
- 	}
 
- },
 
- // 初始化“进行中”视图列表
 
- initDoingViewList = function(list) {
 
- 	var html = "";
 
- 	var searchText = $searchbarInput.val().trim();
 
- 	if(activeType=="1"){//咨询我的
 
- 		 html = template("zixun_li_tmpl", {list: list.slice(0,3)});
 
- 	}else{//我咨询的
 
- 		html = template("wzixun_li_tmpl", {list: list.slice(0,3)});
 
- 	}
 
- 	
 
- 	// 搜索结果小于等于3个时,隐藏“查看更多”
 
- 	if(list.length<=3) {
 
- 		$doingWrapper.find('.more-result').hide();
 
- 	} else {
 
- 		$doingWrapper.find('.more-result').show();
 
- 	}	
 
- 	$doingList.html(html);
 
- 	ellipsisText($doingList.find('.c-content-warp'),searchText);
 
- },
 
- // 初始化“已结束”视图列表
 
- initEndViewList = function(list) {
 
- 	var searchText = $searchbarInput.val().trim();
 
- 	if(activeType=="1"){//咨询我的
 
- 		 html = template("zixun_li_tmpl", {list: list.slice(0,3)});
 
- 	}else{//我咨询的
 
- 		html = template("wzixun_li_tmpl", {list: list.slice(0,3)});
 
- 	}
 
- 	// 搜索结果小于等于3个时,隐藏“查看更多”
 
- 	if(list.length<=3) {
 
- 		$endWrapper.find('.more-result').hide();
 
- 	} else {
 
- 		$endWrapper.find('.more-result').show();
 
- 	}		
 
- 	$endList.html(html);
 
- 	ellipsisText($endList.find('.c-content-warp'),searchText);
 
- },
 
- // 搜索框搜索执行方法
 
- search = function () {
 
- 	var kw = $searchbarInput.val().trim(),
 
- 		// "进行中"搜索结果列表数据
 
- 		doingList = [],
 
- 		// "已结束"搜索结果列表数据
 
- 		endList = [],
 
- 		urls = [];
 
- 		// TODO promise请求测试示例
 
- //		promise = Promise.resolve();
 
- 		if(activeType=="1"){//咨询我的
 
- 			urls = [{url:"doctor/consult/list",data:{id:0,title: kw,type:6,pagesize:4}},{url:"doctor/consult/list",data:{id:0,title: kw,type:7,pagesize:4}}]
 
- 		}else{//我咨询的
 
- 			urls = [{url:"doctor/consult/list",data:{id:0,title: kw,type:11,pagesize:4}},{url:"doctor/consult/list",data:{id:0,title: kw,type:12,pagesize:4}}]
 
- 		}
 
- 	    promise = getReqPromises(urls)
 
- 		// 隐藏搜索提示
 
- 		showSearchSuggest(false);
 
- 		return kw && promise.then(function(datas){
 
- 			var doingList = datas[0].list,
 
- 				endList = datas[1].list;
 
- 			if(!doingList.length && !endList.length) {
 
- 				$noResultWrap.show();
 
- 				$searchtResult.hide();
 
- 			} else {
 
- 				$noResultWrap.hide();
 
- 				$searchtResult.show();
 
- 				if(doingList.length && !endList.length){
 
- 					$doingWrapper.show();
 
- 					$endWrapper.hide();
 
- 					initDoingViewList(doingList);
 
- 				}
 
- 				if(!doingList.length && endList.length){
 
- 					$doingWrapper.hide();
 
- 					$endWrapper.show();
 
- 					initEndViewList(endList);
 
- 				}
 
- 				if(doingList.length && endList.length){
 
- 					$doingWrapper.show();
 
- 					$endWrapper.show();
 
- 					initDoingViewList(doingList);
 
- 					initEndViewList(endList);
 
- 				}
 
- 				
 
- 			}
 
- 		})
 
- },
 
- // 绑定页面事件
 
- bindEvents = function () {
 
- 	$searchbarInput.on('input', function() {
 
- 		var text = $(this).val().trim();
 
- 		$searchtResult.hide();
 
- 		showSearchSuggest(text);	
 
- 	}).on('keydown',function(e) {
 
- 		if (e.which === 13) {
 
- 			search();
 
- 		}
 
- 	});
 
- 	
 
- 	$searchSuggest.on('click',function() {
 
- 		$searchSuggest.hide();
 
- 		search();
 
- 		$searchtResult.show();
 
- 	});
 
- 	
 
- 	$moreResult.on('click',function() {
 
- 			var kw = $searchbarInput.val().trim(),
 
- 			href = $(this).attr('data-href');
 
- 			var params = {};
 
- 			params.kw = kw;
 
- 			params.activeType = activeType;
 
- 			console.log(href);
 
- 			openWebviewExtras(href,params);
 
- 	});
 
- 	
 
- 	$searchtResult.on('click','li',function() {
 
- 		var $this = $(this),
 
- 			consult = $this.attr('data-consult'),
 
- 			code = $this.attr('data-code'),
 
- 			name = $this.attr('data-name'),
 
- 			photo = $this.attr('data-photo'),
 
- 			sex = $this.attr('data-sex'),
 
- 			patient = $this.attr('data-patient'),
 
- 			// (咨询状态(0进行中,1已完成,-1患者取消)
 
- 			status = $this.attr('data-status');
 
- //		if(status == "1") {
 
- //			// flagZx 是为了跳转至咨询详情页面控制聊天对话框的显示
 
- //			plus.storage.setItem("flagZx","4");
 
- //		} else {
 
- //			plus.storage.setItem("flagZx","");
 
- //		}
 
- 		mui.openWindow({
 
- 			id: "p2p",
 
- 			url: "../../message/html/p2p.html",
 
- 			extras: {
 
- 				otherCode: patient,
 
- 				otherName: name,
 
- 				otherPhoto: photo,
 
- 				otherSex: sex
 
- 			}
 
- 		})
 
- 		//openWebview("mingyizixunxiangqing.html",{consult: consult, patient: patient,name: name});
 
- 	});
 
- 	window.onpageshow = function() {
 
- 		$searchbarInput.val('').focus();
 
- 		$searchtResult.hide();
 
- 	}
 
- 	
 
- 	window.addEventListener("focusAction", function() {
 
- 		$searchbarInput.focus();
 
- 	});
 
- 	
 
- 	$searchCancelBtn.on("click",function(){
 
- 		plus.webview.currentWebview().close();
 
- 		openWebview("mingyizixunliebiao.html");		
 
- 	})
 
- };
 
- $(function(){
 
- 	$searchbarInput.val('').focus();
 
- 	$searchtResult.hide();
 
- })
 
- // 页面业务处理流程开始
 
- new Promise(function(resolve, reject) {
 
- 	// TODO 临时放开
 
- //	resolve(true);
 
- 	mui.plusReady(function() {
 
- 		// hrefhrefplus已经准备好,可以往下执行
 
- 		resolve(true);
 
- 	});
 
- }).then(function() {
 
- 	
 
- 	// 获取基础环境信息
 
- 	return getBaseEnvPromise().then(function(env) {
 
- 		baseEnv = env;
 
- 	}).then(function() {
 
- 		// 获取登录医生信息
 
- 		loginerInfo = getLoginerInfo();
 
- 		$searchbar.searchBar();
 
- 		// 绑定页面事件
 
- 		bindEvents();
 
- 	})
 
- }).catch(function(e) {
 
- 	plus.nativeUI.closeWaiting();
 
- 	console && console.error(e);
 
- });
 
- template.helper("setPhoto", function(p) {		
 
- 		return getImgUrl(p);
 
- });
 
 
  |