jianjiaotuisong_search.js 8.9 KB

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