search_article.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. mui.init();
  2. var scroller1 = null,
  3. page = 0,
  4. pagesize = 10;
  5. var type = null; //医生收藏过来为1,全部为0
  6. var isAdmin; //判断是否是新的接口
  7. var patient = null;
  8. var patientName = "";
  9. var isSent,
  10. firstCatId,
  11. pCodes,
  12. referrer;
  13. mui.plusReady(function() {
  14. self = plus.webview.currentWebview();
  15. isAdmin = self.isAdmin;
  16. type = self.type;
  17. patient = self.patient;
  18. patientName = self.patientName;
  19. isSent = self.isSent;
  20. firstCatId = self.firstCatId;
  21. pCodes = self.pCodes,
  22. referrer = self.referrer;
  23. loaded();
  24. initKeyboardAndroid();
  25. openSoftKeyboard();
  26. $('#searchPut').focus();
  27. initLatestSearch();
  28. initHotSearch();
  29. bindEvents();
  30. });
  31. function bindEvents() {
  32. $('#cancel').on('click', function() {
  33. plus.webview.currentWebview().close();
  34. mui.later(function() {
  35. $('#wrapper').hide();
  36. $('#no_result_wrap').hide();
  37. $('#searchValWrap').hide();
  38. $('#searchPut').val("");
  39. }, 500)
  40. });
  41. $('#searchPut').on('input', function() {
  42. var html = $(this).val();
  43. var reg = new RegExp("<" + "(?!>)", "gi");
  44. html = html.replace(reg, '&lt');
  45. var reg = new RegExp(">" + "(?!>)", "gi");
  46. html = html.replace(reg, '&gt');
  47. $('#searchVal').html(html);
  48. $(".searchbar-clear").toggle($(this).val() != "");
  49. $('#searchValWrap').toggle($(this).val() != "");
  50. $("#preSearchPanel").hide();
  51. $('#wrapper').hide();
  52. $("#no_result_wrap").hide();
  53. });
  54. $('#searchValWrap').on('tap', function() {
  55. $(this).hide();
  56. var val = $('#searchPut').val();
  57. searchArticle(true, val);
  58. });
  59. $(".searchbar-clear").on('click', function() {
  60. $('#searchPut').val("");
  61. $('#searchVal').html("");
  62. $('#searchValWrap').hide();
  63. $('#wrapper').hide();
  64. $("#preSearchPanel").show();
  65. return false;
  66. });
  67. $('#articleList').on('tap', 'li', function() {
  68. var code = $(this).attr("data-code");
  69. if(isAdmin){
  70. openWebviewExtras("../../home/html/jianjiaotuisong-xiangqing.html",{articleId:code});
  71. }else{
  72. openWebview("article-info.html", {
  73. articleId:code,
  74. patient: patient,
  75. patientName: patientName,
  76. pCodes: pCodes,
  77. showHandleBar: true,
  78. referrer: referrer
  79. });
  80. }
  81. });
  82. $("#preSearchPanel").on('tap', 'span', function() {
  83. var searchValue = $(this).html();
  84. $('#searchVal').html(searchValue);
  85. $('#searchPut').val(searchValue);
  86. searchArticle(true, searchValue);
  87. });
  88. template.helper("setKeyword", function(str, kw){
  89. var reg = new RegExp(kw + "(?!>)", "gi");
  90. str = str.replace(reg, '<em>' + kw + '</em>');
  91. return str;
  92. });
  93. template.helper("setContent", function(str){
  94. var reg=/<[^<>]+>/g;
  95. str = str.replace(reg, '');
  96. return str;
  97. });
  98. template.helper("formatDate", function(str){
  99. if(str){
  100. return str.substr(0,19)
  101. }else{
  102. return "";
  103. }
  104. })
  105. }
  106. function searchArticle(isInit, filter) {
  107. if(isInit){
  108. page = 0;
  109. }
  110. var docInfo = JSON.parse(plus.storage.getItem('docInfo'));
  111. var url = '/doctor/jkEdu/article/PC/queryArticlePcList'
  112. params = {
  113. articleTitle: filter,
  114. currentUserRole: docInfo.hospital,
  115. currentUserRoleLevel: 4,
  116. iDisplayStart: page * pagesize,
  117. iDisplayLength: pagesize,
  118. isAuthentication: 1, //已认证过的文章
  119. roleType: isAdmin ? 2 : 1 //1、普通医生,2、管理员
  120. };
  121. if(isSent){
  122. url = 'doctor/jkEdu/article/PC/pushArticleList';
  123. params.sendType = isAdmin ? 2 : 1 //1、普通医生,2、管理员
  124. params.currentRoleCode = docInfo.hospital;
  125. }else{
  126. params.firstLevelCategoryId = firstCatId;
  127. }
  128. if(patient){
  129. params.patient = patient;
  130. }
  131. if(isAdmin){
  132. var selectedRole = JSON.parse(plus.storage.getItem("selectedRole"));
  133. params.currentUserRole = selectedRole.code;
  134. params.currentUserRoleLevel = selectedRole.level;
  135. }
  136. sendGet(url, params, queryFails, function(res) {
  137. if(res.status == 200) {
  138. var list = res.data.aaData;
  139. updatePullUpText(scroller1, list);
  140. if(isSent){
  141. var html = template("articleLi2", {list: list, kw: filter});
  142. }else{
  143. var html = template("articleLi", {list: list, kw: filter});
  144. }
  145. var $articleList = $("#articleList");
  146. if(isInit) {
  147. $articleList.empty().append(html);
  148. if(list.length == 0) {
  149. $("#no_result_wrap").show();
  150. }
  151. } else {
  152. $articleList.append(html);
  153. }
  154. // ellipsisText($articleList.find('.c-content-warp'), filter);
  155. page += 1;
  156. $('#wrapper').show();
  157. $("#preSearchPanel").hide();
  158. scroller1.refresh();
  159. } else {
  160. queryFails(res);
  161. }
  162. }, true)
  163. }
  164. function queryFails(res) {
  165. if(res.msg) {
  166. plus.nativeUI.toast(res.msg);
  167. } else {
  168. plus.nativeUI.toast("搜索历史加载失败!");
  169. }
  170. }
  171. function querySuccess(res) {
  172. if(res.status == 200) {
  173. } else {
  174. queryFails(res);
  175. }
  176. }
  177. function loaded() {
  178. scroller1 = $("#wrapper").initScroll({
  179. pullDownAction: function() {
  180. page = 0,
  181. pagesize = 10;
  182. var val = $('#searchPut').val();
  183. searchArticle(true, val);
  184. },
  185. pullUpAction: function() {
  186. var val = $('#searchPut').val();
  187. searchArticle(false, val);
  188. }
  189. });
  190. }
  191. document.addEventListener('touchmove', function(e) { e.preventDefault(); }, false);
  192. function initLatestSearch() {
  193. plus.nativeUI.showWaiting();
  194. var params = {};
  195. params.type = 2;
  196. params.page = 0;
  197. params.pagesize = 10;
  198. sendPost("/doctor/health/edu/listLabel", params, function(res) {
  199. initLableFail(res);
  200. }, function(res) {
  201. plus.nativeUI.closeWaiting();
  202. if(res.status == 200) {
  203. var html = "";
  204. for(var j in res.list) {
  205. var data = res.list[j];
  206. html += "<span>" + data.keyword + "</span>"
  207. }
  208. $("#latestSearch").html(html);
  209. } else {
  210. initLableFail(res);
  211. }
  212. })
  213. }
  214. function initHotSearch() {
  215. plus.nativeUI.showWaiting();
  216. var params = {};
  217. params.type = 1;
  218. params.page = 0;
  219. params.pagesize = 30;
  220. sendPost("/doctor/health/edu/listLabel", params, function(res) {
  221. initLableFail(res);
  222. }, function(res) {
  223. plus.nativeUI.closeWaiting();
  224. if(res.status == 200) {
  225. var html = "";
  226. for(var j in res.list) {
  227. var data = res.list[j];
  228. html += "<span>" + data.keyword + "</span>"
  229. }
  230. $("#hotSearch").html(html);
  231. } else {
  232. initLableFail(res);
  233. }
  234. })
  235. }
  236. function initLableFail(res) {
  237. plus.nativeUI.closeWaiting();
  238. if(res.msg) {
  239. plus.nativeUI.toast(res.msg);
  240. } else {
  241. plus.nativeUI.toast("搜索历史加载失败!");
  242. }
  243. }
  244. var updatePullUpText = function(scroller, list) {
  245. var $wrap = $(scroller.wrapper),
  246. $pullupLabel = $wrap.find('.pullUpLabel');
  247. $(".pullUp").removeClass("loading");
  248. if(!list) {
  249. $pullupLabel.text('没有更多');
  250. } else if(list.length < 10) {
  251. $(".pullUp").hide();
  252. } else {
  253. $pullupLabel.text('上拉加载更多');
  254. }
  255. scroller.on('refresh', function() {
  256. if(!list) {
  257. $pullupLabel.text('没有更多');
  258. } else if(list.length < 10) {
  259. $(".pullUp").hide();
  260. } else {
  261. $pullupLabel.text('上拉加载更多');
  262. }
  263. });
  264. },
  265. getRowProps = function($el) {
  266. var $textEllipsis = $el.find('.j-text-ellipsis'),
  267. $text = $textEllipsis.eq(0),
  268. $chart = $text.text('a'),
  269. enWidth = $chart.width(),
  270. $chart = $text.text('中'),
  271. zhWidth = $chart.width(),
  272. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  273. rowHeight = $chart.height();
  274. $chart.text('');
  275. return {
  276. chartWidth: {
  277. zh: zhWidth,
  278. en: enWidth
  279. },
  280. rowHeight: Math.max(rowHeight, lineHeight),
  281. rowWidth: $el.width()
  282. };
  283. },
  284. replaceAll = function(text, arr) {
  285. var html = text;
  286. _.each(arr, function(kw) {
  287. var reg = new RegExp(kw + "(?!>)", "gi");
  288. html = html.replace(reg, '<em>' + kw + '</em>');
  289. });
  290. return html;
  291. },
  292. highlineKeyword = function($el, searchText) {
  293. var props = getRowProps($el),
  294. chartWidth = props.chartWidth,
  295. rowHeight = props.rowHeight,
  296. rowWidth = props.rowWidth,
  297. // 每行显示字符数(以中文字符为标准计算)
  298. chartNum = Math.floor(rowWidth / chartWidth.zh),
  299. // 排除指定数量字符所占宽度
  300. exceptNum = 0,
  301. // 行数
  302. rowNum = 1,
  303. // 预计显示总字符数
  304. expectedNum = chartNum * rowNum - exceptNum,
  305. $target = $el.find('.j-text-ellipsis'),
  306. // 目标文本
  307. text = $target.attr('data-text').trim(),
  308. length = text.length,
  309. // 关键字数组
  310. kws = searchText.trim().replace(/\s+/g, " ").split(" ");
  311. var fidx = 0,
  312. preFidx, diff = 0;
  313. $target.html(replaceAll(text, kws));
  314. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  315. return;
  316. }
  317. if(text.length > expectedNum) {
  318. fidx = text.indexOf(kws[0]) + kws[0].length - 1;
  319. diff = fidx - expectedNum + 1;
  320. preFidx = fidx;
  321. diff = (diff < 0) ? 0 : diff;
  322. var preChar = (diff > 0) ? "..." : "";
  323. $target.html(preChar + replaceAll(text.slice(diff, preFidx + 1), kws) + "...");
  324. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  325. preFidx++;
  326. $target.html(preChar + replaceAll(text.slice(diff, preFidx + 1), kws) + "...");
  327. }
  328. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  329. diff = diff > 0 ? (diff - 1) : 0;
  330. $target.html(preChar + replaceAll(text.slice(diff, preFidx), kws));
  331. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  332. $target.html(preChar + replaceAll(text.slice(diff, preFidx), kws) + "...");
  333. }
  334. }
  335. },
  336. ellipsisText = function($elements, searchText) {
  337. //$el.ellipsis({ row: 2});
  338. _.each($elements, function(el) {
  339. highlineKeyword($(el), searchText)
  340. });
  341. };