articleSearch.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. mui.plusReady(function(){
  2. var page =0,
  3. pagesize = 10;
  4. var self = plus.webview.currentWebview();
  5. //可获得相关的参数
  6. // var firstLevelCategoryId= self.firstLevelCategoryId;
  7. var currentUserRole = self.currentUserRole;
  8. var currentUserRoleLevel = self.currentUserRoleLevel;
  9. var $searchbarInput = $('#searchPut'),
  10. $searchSuggest = $('#search_suggest_text'),
  11. search_keyword = "",//记录搜索框的内容
  12. $noResultWrap = $('#no_result_wrap'),
  13. $searchResult = $('#search_result');
  14. $searchbarInput.focus();
  15. mui.init({
  16. pullRefresh:{
  17. container:'mui-scroll-wrapper',
  18. up:{
  19. height:50,
  20. contentdown:"",
  21. contentrefresh:"正在加载...",
  22. contentnomore:'没有更过数据了',
  23. callback: function(){
  24. var self = this;
  25. page++;
  26. queryListData(function(value){
  27. setTimeout(function(){
  28. self.endPullUpToRefresh(value);
  29. })
  30. },'up')
  31. }
  32. }
  33. }
  34. })
  35. //监视输入
  36. $searchbarInput.on('input',function(){
  37. var text = $(this).val().trim();
  38. $searchResult.hide();
  39. $noResultWrap.hide();
  40. showSearchSuggest(text);
  41. }).on('keydown',function(e){
  42. if(e.which === 13){
  43. searchByPaging()
  44. }
  45. })
  46. //查询
  47. function searchByPaging(){
  48. $searchbarInput.blur();
  49. search_keyword = $searchbarInput.val();
  50. if(!$.trim(search_keyword)){
  51. return;
  52. }
  53. page = 0;
  54. $('#dest_list').empty();
  55. mui('.mui-scroll-wrapper').pullRefresh().refresh(true);
  56. plus.nativeUI.showWaiting();
  57. queryListData();
  58. $searchSuggest.hide();
  59. $noResultWrap.hide();
  60. }
  61. $searchSuggest.on('click',function(){
  62. searchByPaging();
  63. })
  64. //控制搜索关键字悬浮提示的显示
  65. function showSearchSuggest(text){
  66. var suggestText = '搜索“'+text+'”';
  67. //如果text不为空,则显示否则隐藏
  68. if(text&&text.trim().length){
  69. $searchSuggest.text(suggestText);
  70. $searchSuggest.show();
  71. }else{
  72. $searchSuggest.text('');
  73. $searchSuggest.hide();
  74. }
  75. }
  76. //关键字变色
  77. function ellipsisText($elements, searchText) {
  78. _.each($elements,function(el) {
  79. var $textEl = $(el).find(".j-text-ellipsis");
  80. _.each($textEl, function(t){
  81. highlineKeyword($(t),searchText)
  82. })
  83. })
  84. }
  85. function highlineKeyword($el,searchText) {
  86. var props = getRowProps($el),
  87. chartWidth = props.chartWidth,
  88. rowHeight = props.rowHeight,
  89. rowWidth = props.rowWidth,
  90. // 每行显示字符数(以中文字符为标准计算)
  91. chartNum = Math.floor(rowWidth / chartWidth.zh),
  92. // 排除指定数量字符所占宽度
  93. exceptNum = 0,
  94. // 行数
  95. rowNum = 1,
  96. // 预计显示总字符数
  97. expectedNum = chartNum * rowNum - exceptNum,
  98. $target = $el,
  99. // 目标文本
  100. text = $.trim($target.attr('data-text')),
  101. length = text.length,
  102. // 关键字数组
  103. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  104. var fidx = 0,preFidx,diff = 0;
  105. $target.html(replaceAll(text, kws));
  106. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  107. return ;
  108. }
  109. }
  110. function getRowProps($el) {
  111. var $textEllipsis = $el,
  112. $text = $textEllipsis.eq(0),
  113. $chart = $text.text('a'),
  114. enWidth = $chart.width(),
  115. $chart = $text.text('中'),
  116. zhWidth = $chart.width(),
  117. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  118. rowHeight = $chart.height();
  119. $chart.text('');
  120. return {
  121. chartWidth: {
  122. zh: zhWidth,
  123. en: enWidth
  124. },
  125. rowHeight: Math.max(rowHeight, lineHeight),
  126. rowWidth: $el.width()
  127. };
  128. }
  129. function replaceAll(text, arr) {
  130. var html = text;
  131. _.each(arr,function(kw) {
  132. var reg = new RegExp(kw+"(?!>)","gi");
  133. html = html.replace(reg,'<em>'+kw+'</em>');
  134. });
  135. return html;
  136. }
  137. //取消
  138. $('.searchbar-cancel').on('click',function(){
  139. mui.back();
  140. })
  141. function queryListData(fun){
  142. var params = {
  143. firstLevelCategoryId: self.firstLevelCategoryId,
  144. currentUserRole: self.currentUserRole,
  145. currentUserRoleLevel: self.currentUserRoleLevel,
  146. articleTitle: search_keyword,
  147. iDisplayStart: page,
  148. iDisplayLength: pagesize,
  149. currentUserRoleLevel: 4,
  150. roleType:1,
  151. isMyArticle:true
  152. };
  153. var url="/doctor/jkEdu/article/queryArticleAPPList";
  154. sendPost(url,params,function(){
  155. plus.nativeUI.closeWaiting();
  156. fun&&fun.call(this,false)
  157. mui.toast("获取数据失败!");
  158. },function(res){
  159. plus.nativeUI.closeWaiting();
  160. if(res.status==200){
  161. var html = template('li_tmpl',{list:res.data.aaData});
  162. if(page == 0 && res.data.aaData.length==0){
  163. $noResultWrap.show();
  164. $searchResult.hide();
  165. }else{
  166. $noResultWrap.hide();
  167. $searchResult.show();
  168. if(res.data.aaData.length < pagesize){
  169. fun&&fun.call(this,true)
  170. }else{
  171. fun&&fun.call(this,false)
  172. }
  173. $('#dest_list').append(html);
  174. ellipsisText($searchResult.find('.j-li'),search_keyword)//变色
  175. }
  176. }else{
  177. fun&&fun.call(this,false);
  178. mui.toast("获取数据失败!");
  179. }
  180. },'get')
  181. }
  182. $('#dest_list').on('tap','li.article-li',function(){
  183. var articleId = $(this).attr('data-id');
  184. mui.openWindow({
  185. id: "articleDetail",
  186. url: "articleDetail.html",
  187. extras: {
  188. articleId:articleId
  189. }
  190. })
  191. })
  192. //点击跳转
  193. $('#dest_list').on('tap','.tuisong-common',function(e){
  194. e.stopPropagation();
  195. var $this = $(this);
  196. var articleId = $this.attr("data-code");
  197. var articleTitle = $this.attr("data-title");
  198. mui.openWindow({
  199. id: "xuanzejumin_more",
  200. url: "xuanzejumin_more.html",
  201. extras: {
  202. article:articleId,origin:'jiaoyu',articleTitle:articleTitle
  203. }
  204. })
  205. })
  206. $('#dest_list').on('tap','.edit-common',function(e){
  207. e.stopPropagation();
  208. var $this = $(this);
  209. var articleId = $this.attr("data-id");
  210. mui.openWindow({
  211. id: "article-edit",
  212. url: "article-edit.html",
  213. extras: {
  214. articleId:articleId
  215. }
  216. })
  217. })
  218. })
  219. //取消
  220. $('.searchbar-cancel').on('click',function() {
  221. mui.back();
  222. })
  223. template.helper("setPhoto", function(p) {
  224. return getImgUrl(p);
  225. });