search_doctor.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview)
  4. baseEnv = null;
  5. var self;
  6. var isConsulted = false;
  7. var $searchbarInput = $('#search-input'),
  8. //搜索清除按钮
  9. $searchClearBtn = $('.searchbar-clear'),
  10. // 搜索取消按钮
  11. $searchCancelBtn = $('#cancel'),
  12. // 搜索框下面悬浮的搜索提示
  13. $searchSuggest = $('#searchValWrap'),
  14. // 搜索结果展示容器
  15. $searchtResult = $('#search_result'),
  16. // 搜索无结果时显示
  17. $noResultWrap = $('#no_result_wrap'),
  18. $docList = $('#doctor_list');
  19. // 分页查询最后一页
  20. var lastId = 1,
  21. // iscroll 滚动条实例
  22. iscroller,
  23. // 从缓存取出所选团队
  24. pagesize = 15,
  25. hasMoreResults = false;
  26. // 获取登录相关信息
  27. var getBaseInfo = function() {
  28. self = plus.webview.currentWebview();
  29. isConsulted = self.isConsulted;
  30. // 登录的相关信息
  31. var userAgent = JSON.parse(plus.storage.getItem("userAgent"))
  32. return {
  33. userAgent: userAgent
  34. }
  35. },
  36. // 获取基础环境信息
  37. getBaseEnvPromise = function () {
  38. var env = {
  39. webview: plus&&plus.webview.currentWebview()
  40. };
  41. return Promise.resolve().then(function(res) {
  42. return env;
  43. });
  44. },
  45. // 初始化“患者”视图列表
  46. initDoctorViewList = function(data,keyword,isAppend) {
  47. var html = template("doctor_list_tmpl", {list: _.map(data,function(o) {
  48. o.jsonStr = JSON.stringify(o);
  49. o.labels = _.chain(o.labels).filter(function(v) {
  50. return v.labelType != "1";
  51. }).pluck("labelName").value().join(",");
  52. return o;
  53. })}),
  54. searchText = $searchbarInput.val().trim();
  55. if(isAppend === true) {
  56. $docList.append(html)
  57. } else {
  58. $docList.html(html);
  59. }
  60. ellipsisText($docList.find('li'),searchText);
  61. },
  62. // $el: $('li')
  63. getRowProps = function ($el) {
  64. var $textEllipsis = $el.find('.j-text-ellipsis'),
  65. $text = $textEllipsis.eq(0),
  66. $chart = $text.text('a'),
  67. enWidth = $chart.width(),
  68. $chart = $text.text('中'),
  69. zhWidth = $chart.width(),
  70. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  71. rowHeight = $chart.height();
  72. $chart.text('');
  73. return {
  74. chartWidth: {
  75. zh: zhWidth,
  76. en: enWidth
  77. },
  78. rowHeight: Math.max(rowHeight, lineHeight),
  79. rowWidth: $el.width()
  80. };
  81. },
  82. replaceAll = function (text, arr) {
  83. var html = text;
  84. _.each(arr,function(kw) {
  85. var reg = new RegExp(kw+"(?!>)","gi");
  86. html = html.replace(reg,'<em>'+kw+'</em>');
  87. });
  88. return html;
  89. },
  90. highlineKeyword = function ($el,searchText) {
  91. var props = getRowProps($el),
  92. chartWidth = props.chartWidth,
  93. rowHeight = props.rowHeight,
  94. rowWidth = props.rowWidth,
  95. // 每行显示字符数(以中文字符为标准计算)
  96. chartNum = Math.floor(rowWidth / chartWidth.zh),
  97. // 排除指定数量字符所占宽度
  98. exceptNum = 0,
  99. // 行数
  100. rowNum = 1,
  101. // 预计显示总字符数
  102. expectedNum = chartNum * rowNum - exceptNum,
  103. $target = $el.find('.j-text-ellipsis'),
  104. // 目标文本
  105. text = $.trim($target.attr('data-text')),
  106. length = text.length,
  107. // 关键字数组
  108. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  109. var fidx = 0,preFidx,diff = 0;
  110. $target.html(replaceAll(text, kws));
  111. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  112. return ;
  113. }
  114. if(text.length > expectedNum) {
  115. fidx = text.indexOf(kws[0])+kws[0].length-1;
  116. diff = fidx - expectedNum + 1;
  117. preFidx = fidx;
  118. diff = (diff<0)?0:diff;
  119. var preChar = (diff>0)?"...":"";
  120. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  121. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  122. preFidx++;
  123. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  124. }
  125. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  126. diff = diff>0?(diff - 1):0;
  127. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  128. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  129. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  130. }
  131. }
  132. },
  133. ellipsisText = function ($elements, searchText) {
  134. //$el.ellipsis({ row: 2});
  135. _.each($elements,function(el) {
  136. highlineKeyword($(el),searchText)
  137. });
  138. },
  139. // 分页查询列表
  140. searchByPaging = function () {
  141. var kw = $searchbarInput.val();
  142. if(!$.trim(kw)) {
  143. return ;
  144. }
  145. plus.nativeUI.showWaiting();
  146. // page: 分页索引,filter:搜素关键字,pagesize:每页条数
  147. var url = "/doctor/doctors_list",
  148. params = {
  149. key: kw,
  150. page:1,
  151. pagesize:pagesize
  152. };
  153. getReqPromise(url,params).then(function(res){
  154. if(res.status == 200) {
  155. lastId = lastId+1;
  156. if(!res.data || !res.data.length) {
  157. $searchtResult.hide();
  158. $noResultWrap.show();
  159. } else {
  160. if(res.data.length < pagesize) {
  161. hasMoreResults = false;
  162. } else {
  163. hasMoreResults = true;
  164. }
  165. $noResultWrap.hide();
  166. $searchtResult.show();
  167. initDoctorViewList(res.data,kw);
  168. }
  169. if(!iscroller) {
  170. iscroller = initScroller($searchtResult,url,
  171. function() { // 传递分页参数
  172. return $.extend({},params,{page:lastId});
  173. },function(res) {
  174. lastId = lastId+1;
  175. var kw = $searchbarInput.val();
  176. initDoctorViewList(res.data,kw,true);
  177. iscroller.refresh();
  178. });
  179. }
  180. iscroller.refresh();
  181. } else {
  182. plus.nativeUI.toast("搜索失败!");
  183. }
  184. plus.nativeUI.closeWaiting();
  185. }).catch(function(e) {
  186. console && console.error(e);
  187. });
  188. },
  189. // 滚动条分页实例初始化
  190. initScroller = function($el,url,getData,pullUpAction) {
  191. var scroller;
  192. if(hasMoreResults) {
  193. scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  194. var data = getData();
  195. getReqPromise(url,data).then(function(data) {
  196. if(pullUpAction && $.isFunction(pullUpAction)) {
  197. pullUpAction(data);
  198. updatePullUpText(scroller,data.list);
  199. }
  200. })
  201. }});
  202. } else {
  203. scroller = $el.initScroll({pullDown: false,pullUp: false});
  204. }
  205. return scroller;
  206. },
  207. // 更新分页上拉加载的提示文本
  208. updatePullUpText= function(scroller,list) {
  209. var $wrap = $(scroller.wrapper),
  210. $pullupLabel = $wrap.find('.pullUpLabel');
  211. if(!list || !list.length) {
  212. $pullupLabel.text('没有更多');
  213. } else {
  214. $pullupLabel.text('上拉加载更多');
  215. }
  216. scroller.on('refresh',function() {
  217. if(!list || !list.length) {
  218. $pullupLabel.text('没有更多');
  219. } else {
  220. $pullupLabel.text('上拉加载更多');
  221. }
  222. });
  223. },
  224. bindEvents = function(){
  225. $('#cancel').on('click', function(){
  226. mui.back();
  227. });
  228. $searchbarInput.on('input', function(){
  229. var html = $(this).val();
  230. var reg = new RegExp("<"+"(?!>)","gi");
  231. html = html.replace(reg,'&lt');
  232. var reg = new RegExp(">"+"(?!>)","gi");
  233. html = html.replace(reg,'&gt');
  234. $('#searchVal').html(html);
  235. $searchClearBtn.toggle($(this).val() != "");
  236. $searchSuggest.toggle($(this).val()!="");
  237. $searchtResult.hide();
  238. $noResultWrap.hide();
  239. $("#sendCount").text(0);
  240. sendCount=0;
  241. }).on('keydown',function(e) {
  242. if (e.which === 13) {
  243. $searchSuggest.hide();
  244. searchByPaging();
  245. }
  246. });
  247. $searchClearBtn.on('click', function(){
  248. $(this).hide();
  249. $searchbarInput.val("");
  250. $('#searchVal').html("");
  251. $searchSuggest.hide();
  252. });
  253. $searchSuggest.on('click',function() {
  254. $searchSuggest.hide();
  255. $("#sendCount").text(0);
  256. sendCount=0;
  257. searchByPaging();
  258. });
  259. $("#cy_btn").on('click',function(){
  260. mui.openWindow({
  261. url:"changyong_doctor.html",
  262. id:"changyong_doctor"
  263. });
  264. });
  265. };
  266. // 页面业务处理流程开始
  267. new Promise(function(resolve, reject) {
  268. // TODO 临时放开
  269. //resolve(true);
  270. mui.plusReady(function() {
  271. // hrefhrefplus已经准备好,可以往下执行
  272. resolve(true);
  273. initKeyboardAndroid();
  274. });
  275. }).then(function() {
  276. // 获取基础环境信息
  277. return getBaseEnvPromise().then(function(env) {
  278. baseEnv = env;
  279. }).then(function() {
  280. // 获取登录基本信息
  281. baseInfo = getBaseInfo();
  282. openSoftKeyboard();
  283. $searchbarInput.focus();
  284. // 绑定页面事件
  285. bindEvents();
  286. var self = plus.webview.currentWebview();
  287. })
  288. }).catch(function(e) {
  289. plus.nativeUI.closeWaiting();
  290. console && console.error(e);
  291. });
  292. function closeOpener(wv){
  293. if(wv){
  294. if(wv.id.indexOf('qiuzhuqunliao')!=0 && wv.id.indexOf('p2dzixun')!=0 ){
  295. closeOpener(wv.opener());
  296. wv.close();
  297. } else {
  298. mui.fire(wv, "getMember");
  299. }
  300. }
  301. }