sousuojigouyisheng.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. var plus;
  2. // 基本信息(包括userAgent、上个页面传递的数据)
  3. var baseInfo = null,
  4. // 基础环境信息(包括当前webview)
  5. baseEnv = null;
  6. // 搜索框
  7. var $searchbar = $('.searchbar'),
  8. // 搜索输入框
  9. $searchbarInput = $('.searchbar input'),
  10. // 搜索取消按钮
  11. $searchCancelBtn = $('.searchbar-cancel'),
  12. // 搜索框下面悬浮的搜索提示
  13. $searchSuggest = $('#search_suggest_text'),
  14. // 搜索结果展示容器
  15. $searchtResult = $('#search_result'),
  16. // 搜索无结果时显示
  17. $noResultWrap = $('#no_result_wrap'),
  18. $yiyuanWrapper = $('#yiyuan_wrapper'),
  19. $shequWrapper = $('#shequ_wrapper'),
  20. $yishengWrapper = $('#yisheng_wrapper'),
  21. $yiyuanList = $('#yiyuan_list'),
  22. $shequList = $('#shequ_list'),
  23. $yishengList = $('#yisheng_list'),
  24. $moreResult = $('.more-result');
  25. // 获取基本信息(包括userAgent、上个页面传递的数据)
  26. var getBaseInfoPromise = function() {
  27. // 登录的相关信息
  28. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  29. return {
  30. userAgent: userAgent
  31. }
  32. },
  33. // 获取基础环境信息
  34. getBaseEnvPromise = function () {
  35. var env = {
  36. webview: plus&&plus.webview.currentWebview()
  37. };
  38. return Promise.resolve().then(function(res) {
  39. return env;
  40. });
  41. },
  42. // $el: $('.c-content-warp')
  43. getRowProps = function ($el) {
  44. var $textEllipsis = $el.find('.j-text-ellipsis'),
  45. $text = $textEllipsis.eq(0),
  46. $chart = $text.text('a'),
  47. enWidth = $chart.width(),
  48. $chart = $text.text('中'),
  49. zhWidth = $chart.width(),
  50. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  51. rowHeight = $chart.height();
  52. $chart.text('');
  53. return {
  54. chartWidth: {
  55. zh: zhWidth,
  56. en: enWidth
  57. },
  58. rowHeight: Math.max(rowHeight, lineHeight),
  59. rowWidth: $el.width()
  60. };
  61. },
  62. replaceAll = function (text, arr) {
  63. var html = text;
  64. _.each(arr,function(kw) {
  65. var reg = new RegExp(kw+"(?!>)","gi");
  66. html = html.replace(reg,'<em>'+kw+'</em>');
  67. });
  68. return html;
  69. },
  70. highlineKeyword = function ($el,searchText) {
  71. var props = getRowProps($el),
  72. chartWidth = props.chartWidth,
  73. rowHeight = props.rowHeight,
  74. rowWidth = props.rowWidth,
  75. // 每行显示字符数(以中文字符为标准计算)
  76. chartNum = Math.floor(rowWidth / chartWidth.zh),
  77. // 排除指定字符数所占宽度
  78. exceptNum = 0,
  79. // 行数
  80. rowNum = 1,
  81. // 预计显示总字符数
  82. expectedNum = chartNum * rowNum - exceptNum,
  83. $target = $el.find('.j-text-ellipsis'),
  84. // 目标文本
  85. text = $target.attr('data-text').trim(),
  86. length = text.length,
  87. // 关键字数组
  88. kws = searchText.trim().replace(/\s+/g," ").split(" ");
  89. var fidx = 0,preFidx,diff = 0;
  90. $target.html(replaceAll(text, kws));
  91. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  92. return ;
  93. }
  94. if(text.length > expectedNum) {
  95. fidx = text.indexOf(kws[0])+kws[0].length-1;
  96. diff = fidx - expectedNum + 1;
  97. preFidx = fidx;
  98. diff = (diff<0)?0:diff;
  99. var preChar = (diff>0)?"...":"";
  100. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  101. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  102. preFidx++;
  103. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  104. }
  105. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  106. diff = diff>0?(diff - 1):0;
  107. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  108. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  109. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  110. }
  111. }
  112. },
  113. ellipsisText = function ($elements, searchText) {
  114. //$el.ellipsis({ row: 2});
  115. _.each($elements,function(el) {
  116. highlineKeyword($(el),searchText)
  117. });
  118. },
  119. // 控制搜索关键字悬浮提示的显示
  120. showSearchSuggest = function(text) {
  121. var suggestText = '搜索“'+text+'”';
  122. // 如果text不为空,则显示;否则隐藏
  123. if(text&&text.trim().length) {
  124. $searchSuggest.text(suggestText);
  125. $searchSuggest.show();
  126. } else {
  127. $searchSuggest.text('');
  128. $searchSuggest.hide();
  129. }
  130. },
  131. // 初始化“医院”视图列表
  132. initYiYuanViewList = function(list) {
  133. var html = template("org_li_tmpl", {list: list.slice(0,3)}),
  134. searchText = $searchbarInput.val().trim();
  135. // 搜索结果小于等于3个时,隐藏“查看更多”
  136. if(list.length<=3) {
  137. $yiyuanWrapper.find('.more-result').hide();
  138. } else {
  139. $yiyuanWrapper.find('.more-result').show();
  140. }
  141. $yiyuanList.html(html);
  142. ellipsisText($yiyuanList.find('.c-content-warp'),searchText);
  143. },
  144. // 初始化“社区”视图列表
  145. initSheQuViewList = function(list) {
  146. var html = template("org_li_tmpl", {list: list.slice(0,3)}),
  147. searchText = $searchbarInput.val().trim();
  148. // 搜索结果小于等于3个时,隐藏“查看更多”
  149. if(list.length<=3) {
  150. $shequWrapper.find('.more-result').hide();
  151. } else {
  152. $shequWrapper.find('.more-result').show();
  153. }
  154. $shequList.html(html);
  155. ellipsisText($shequList.find('.c-content-warp'),searchText);
  156. },
  157. // 初始化“医生”视图列表
  158. initYiShengViewList = function(list) {
  159. var html = template("yisheng_li_tmpl", {list: list.slice(0,3)}),
  160. searchText = $searchbarInput.val().trim();
  161. // 搜索结果小于等于3个时,隐藏“查看更多”
  162. if(list.length<=3) {
  163. $yishengWrapper.find('.more-result').hide();
  164. } else {
  165. $yishengWrapper.find('.more-result').show();
  166. }
  167. $yishengList.html(html);
  168. ellipsisText($yishengList.find('.c-content-warp'),searchText);
  169. },
  170. // 搜索框搜索执行方法
  171. search = function () {
  172. var kw = $searchbarInput.val().trim(),
  173. // "进行中"搜索结果列表数据
  174. doingList = [],
  175. // "已结束"搜索结果列表数据
  176. endList = [],
  177. // TODO promise请求测试示例
  178. //promise = Promise.resolve();
  179. promise = getReqPromises([{url:"doctor/hospitals",data:{id:0,key: kw, type:1, pagesize:4}},
  180. {url:"doctor/hospitals",data:{id:0,key: kw,type:2,pagesize:4}},
  181. {url:"/doctor/findDoctorByDept",data:{id:0,key: kw,page:1,pagesize:4}}])
  182. // 隐藏搜索提示
  183. showSearchSuggest(false);
  184. return kw && promise.then(function(datas){
  185. // TODO 测试数据示例
  186. // var yiyuanList = orgData.list,
  187. // shequList = orgData.list,
  188. // yishengList = doctorData.list;
  189. var yiyuanList = datas[0].list,
  190. shequList = datas[1].list,
  191. yishengList = datas[2].list;
  192. if(!yiyuanList.length && !shequList.length && !yishengList.length) {
  193. $noResultWrap.show();
  194. } else {
  195. $noResultWrap.hide();
  196. $searchtResult.show();
  197. initYiYuanViewList(yiyuanList);
  198. initSheQuViewList(shequList);
  199. initYiShengViewList(yishengList);
  200. }
  201. })
  202. },
  203. // 绑定页面事件
  204. bindEvents = function () {
  205. $searchbarInput.on('input', function() {
  206. var text = $(this).val().trim();
  207. $searchtResult.hide();
  208. showSearchSuggest(text);
  209. }).on('keydown',function(e) {
  210. if (e.which === 13) {
  211. search();
  212. }
  213. });
  214. $searchCancelBtn.on('click',function() {
  215. mui.back();
  216. });
  217. $searchSuggest.on('click',function() {
  218. $searchSuggest.hide();
  219. search();
  220. });
  221. $moreResult.on('click',function() {
  222. var kw = $searchbarInput.val().trim(),
  223. href = $(this).attr('data-href');
  224. openWebview(href,{accessData: {
  225. keyword: kw
  226. }});
  227. });
  228. $searchtResult.on('tap','.list-item',function() {
  229. var code = $(this).attr('data-code');
  230. var oldWebview = plus.webview.getWebviewById('xuanzekeshi');
  231. if(oldWebview) {
  232. oldWebview.close();
  233. }
  234. openWebview("xuanzekeshi.html",{accessData: {
  235. hospital: code,
  236. createNew: true
  237. }});
  238. }).on('tap','.yisheng-list-item',function() {
  239. var code = $(this).attr('data-code');
  240. openWebview("doctor-homepage.html",{accessData: {
  241. doctor: code
  242. }});
  243. });
  244. /*聚焦搜索框事件*/
  245. window.addEventListener("focusSearchInput", function group(e) {
  246. setTimeout(function() {
  247. $searchbarInput.focus();
  248. },0)
  249. });
  250. };
  251. // 页面业务处理流程开始
  252. new Promise(function(resolve, reject) {
  253. // TODO 临时放开
  254. // resolve(true);
  255. mui.plusReady(function() {
  256. var xzksWebview = plus.webview.getWebviewById('xuanzekeshi');
  257. if(xzksWebview) {
  258. xzksWebview.close();
  259. }
  260. mui.back = backToWebviewById('xinzengmingyi');
  261. // plus已经准备好,可以往下执行
  262. resolve(true);
  263. });
  264. }).then(function() {
  265. // 获取基础环境信息
  266. return getBaseEnvPromise().then(function(env) {
  267. baseEnv = env;
  268. }).then(function() {
  269. // 获取登录医生信息
  270. baseInfo = getBaseInfoPromise();
  271. $searchbar.searchBar();
  272. $searchbarInput.focus();
  273. // 绑定页面事件
  274. bindEvents();
  275. })
  276. }).catch(function(e) {
  277. plus.nativeUI.closeWaiting();
  278. console && console.error(e);
  279. });
  280. template.helper("setPhoto", function(p) {
  281. return getImgUrl(p);
  282. });
  283. template.helper("setHosPhoto", function(p) {
  284. if(!p || p == ""){
  285. return '../images/hospital_default.png';
  286. }
  287. return getImgUrl(p);
  288. });