;
(function($){
$.fn.fwsSearch = function(opts){
var col_id = $(".col_title").data("id");
var defaults = {
siteId: null, //站点Id
columnId: col_id , //栏目Id
url:'/_wp3services/generalQuery?queryObj=articles', //文章检索接口
lang: 1, //语言类型
level:1, //校级:0,,部门:1
displayType:'filter', //展示类型, 'list':栏目列表, 'select':下拉框, 'filter':条件筛选
category: true,
nav:{
active: true, //是否显示导航
navCon: '.fws_nav' //导航放置位置
},
field: [ //导航数据,格式如下
{title:"字母",children:"A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z",type:"0"}
],
fieldJson: [], //文章的json数据
pageIndex: 1, //默认第一页开始
rows: 999, //每页显示多少篇文章
orderData:[ //排序方式
{field: "letter", type: "asc"}
],
conditFeild:[ //检索需要的字段||检索查询条件
{type:"0", field:"letter", judge:"like"},
{type:"0", field:"f4", judge:"like"}
],
returnInfosdata:[ //检索返回的字段
{field: "title", name: "title"},
{field: "publishTime", name: "publishTime"},
{field: "content", name: "content"},
{field: "summary", name: "summary"},
{field: "mircImgPath", name: "mircImgPath"},
{field: "imgPath", name: "imgPath"},
{field: "link", name: "link"},
{field: "url", name: "url"},
{field: "letter", name: "letter"},
{field: "f4", name: "f4"},
{field: "visitCount", name: "visitCount"}
],
showType: 1, //输出显示方式(以下面tpl参数设置优先) 1:人名+浏览次数 2:上图下文
tpl:function(data){ //输出结构
return '
' +
' ' +
' ';
},
callback: function(){} //回调函数(参数为当前结构,检索结果,配置项)
//paging: true,
//isMobile: false
};
var o = $.extend(true, {}, defaults, opts);
return this.each(function(){
$('html').addClass("fws_searchs");
var c = $(this);
c.wrapInner('');
var $listTable = c.find('.listTable');
var $wp_paging = c.find('.wp_paging');
var pageIndex = o.pageIndex,item = '',type = '';
url = o.url,
rows = o.rows,
showType= o.showType;
/*pageCount = 0,
loadBtn = true;*/
function getSearchData(url,complete,showType,rows,keyword,orgId,pageIndex){
complete.addClass("loading");
$(".listTable .news_list").children().remove();
var orders = JSON.stringify(getOrders()),conditions = JSON.stringify(getConditions(keyword,orgId)),returnInfos = JSON.stringify(getReturnInfos());
$.ajax({
url: url,
type: 'POST',
contentType: "application/x-www-form-urlencoded; charset=utf-8",
async: true,
dataType: 'json',
data: {
siteId: o.siteId,
columnId: o.columnId,
pageIndex: pageIndex,
rows: rows,
conditions: conditions,
orders: orders,
returnInfos: returnInfos,
articleType: 1,
scope:o.scope
},
success: function(result){
//window.console && console.log(result);
complete.children(".news_list").children().remove();
complete.next().remove();
if(result != null){
$listTable.removeClass("loading");
if(result.data.length > 0){
setTempTpl(result,complete,showType,rows);
pageTpl(result,complete,pageIndex,rows);
pageClick(rows,item,type);
}else{
$listTable.children().html('未检索到相关信息!
');
}
}
if(typeof o.callback === "function"){
o.callback.call(this, result, o);
}
}
});
}
/*模板结构*/
function setTempTpl(result,complete,showType,rows){
$(".listTable .news_list").children().remove();
var html = "";
var tecCount = Math.min(rows, result.data.length);
var keyword = 'A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z';
var items = keyword.split("+");
if(o.category){
if(o.showType && o.showType !== null){
var html = "",nameHtml = "";
$.each(items,function(index,name){
nameHtml = '';
$listTable.children().append(nameHtml);
//complete.children(".news_list").append(html);
for (i = 0; i < tecCount; i++){
var art = result.data[i];
var tecUrl = art.cnUrl;
art.cnUrl = checkMobileURL(tecUrl);
if(name == $.trim(art[o.conditFeild[1].field])){
html = '' +
' ' +
' ';
complete.find(".news_item").eq(index).find(".news_con .news_lists").append(html);
}
}
});
}
}else{
for (i = 0; i < tecCount; i++){
var art = result.data[i];
var tecUrl = art.cnUrl;
art.cnUrl = checkMobileURL(tecUrl);
if(o.showType && o.showType !== null){
if(showType == 1){
html += '' +
' ' +
' ';
}else{
html += '' +
' ' +
' ';
}
}else{
if(typeof o.tpl === "function" && o.tpl){
html += o.tpl(art);
}
}
}
//区分不同的输出结构
complete.children(".news_list").append(html);
}
$(document).ready(function(){
$('.news_item.list').each(function(){
$(this).find(".news_con .news_lists").each(function(){
if($(this).find("li").length == 0){
$(this).parents(".news_item").remove();
}
});
});
if($(window).width() < 768){
$('.listzm a').click(function () { $('html, body').animate({ scrollTop: $($.attr(this, 'href')).offset().top - 440 }, 800); return false; });
}else{
$('.listzm a').click(function () { $('html, body').animate({ scrollTop: $($.attr(this, 'href')).offset().top - 65 }, 800); return false; });
}
});
}
/*分页结构*/
function pageTpl(result,complete,pageIndex,rows){
var pageCount = result.pageCount;
var total = result.total;
var pageHtml = ' ';
$(complete).after(pageHtml);
}
//初始化
fwsSearchDate(showType,rows,item,type,pageIndex);
//分页点击事件
function pageClick(rows,item,type){
$(document).off(); //移除所有元素上的 click 事件
initpageClick(rows,item,type); //跳转页面
initFirstPageClick(rows,item,type); //首页
initLastPageClick(rows,item,type); //尾页
initPrevPageClick(rows,item,type); //上一页
initNextPageClick(rows,item,type); //下一页
}
//初始化跳转页面
function initpageClick(rows,item,type){
$(document).on('click','.new_pagingJump',function(event){
var pageNum = c.find(".pageNum").val();
if (pageNum === "") {
alert("请输入页码!");
return;
}
var pageCount = c.find(".all_pages").attr("pageCount");
if (isNaN(pageNum) || pageNum <= 0 || pageNum > pageCount) {
alert("请输入正确页码!");
return;
}
fwsSearchDate(showType,rows,item,type,pageNum);
});
}
//初始化首页
function initFirstPageClick(rows,item,type) {
$(document).on('click', ".new_page_nav .first", function(event) {
fwsSearchDate(showType,rows,item,type,1);
});
}
//初始化尾页
function initLastPageClick(rows,item,type){
$(document).off().on('click', ".new_page_nav .last", function(event) {
var allPages = c.find(".all_pages").attr("pageCount");
fwsSearchDate(showType,rows,item,type,allPages);
});
}
//初始化上一页
function initPrevPageClick(rows,item,type){
$(document).on('click', ".new_page_nav .prev", function(event) {
var pageCount = c.find(".curr_page").attr("curr_page");
var prev = --pageCount;
if (prev <= 0){
alert("当前为第一页");
return;
}
fwsSearchDate(showType,rows,item,type,prev);
});
}
//初始化下一页
function initNextPageClick(rows,item,type) {
$(document).on('click', ".new_page_nav .next", function(event) {
var pageCount = c.find(".curr_page").attr("curr_page");
var next = ++pageCount;
var allPages = c.find(".all_pages").attr("pageCount");
if (allPages < next) {
alert("当前为最后一页");
return;
}
fwsSearchDate(showType,rows,item,type,next);
});
}
/*导航*/
if(o.nav.active && o.displayType === 'list'){
var html= "",subHtml = '',field = o.field;
var $navCon = $(o.nav.navCon);
$navCon.wrapInner('');
$.each(field,function(i,el){
html = ''+
' '+
' ';
$navCon.find(".listnav").append(html);
if(field[i].children !=='' && field[i].children !== undefined){
$navCon.find(".item").eq(i).append('');
var items = (field[i].children).split("+");
$.each(items,function(index,val){
subHtml=''+
' '+val+' '+
' ';
$navCon.find(".item").eq(i).find("ul").append(subHtml);
});
}
});
/*导航点击事件*/
$navCon.find(".listnav").find("li").each(function(){
var _this = $(this);
$(this).find(".itemcon").on("click",function(){
_this.children(".wp_subcolumn").slideToggle();
_this.siblings("li.item").children(".wp_subcolumn").slideUp();
_this.siblings("li").removeClass("hover");
_this.addClass("hover");
item = _this.attr("title");
type = _this.attr("type");
fwsSearchDate(showType,rows,item,type,pageIndex);
});
$(this).find(".wp_column").on("click",function(){
$(this).siblings("li").removeClass("hover");
$(this).addClass("hover");
item = $(this).attr("title");
type = $(this).attr("type");
fwsSearchDate(showType,rows,item,type,pageIndex);
});
});
}
//下拉方式
if(o.displayType === 'select'){
var html= "",sertchHtml='',subHtml = '',field = o.field;
c.prepend('
');
$.each(field,function(i,el){
c.find(".selects_box").append('');
html = '';
c.find('.select_item').eq(i).find(".select_box").append(html);
if(field[i].children !=='' && field[i].children !== undefined){
c.find('.select_item').eq(i).find(".sudy-select").append('');
subHtml=''+this.title+' ';//添加默认标题
c.find(".select_item").eq(i).find(".select-list").append(subHtml);
var items = (field[i].children).split("+");
$.each(items,function(index,val){
subHtml=''+val+' ';
c.find(".select_item").eq(i).find(".select-list").append(subHtml);
});
}
});
$(".sudy-select").sudySelect({
handle: ".select-name",
selects: ".select-list",
trigger: "click",
effect: "slide",
speed: 100,
dir: "down",
autoWidth: !0
});
sertchHtml= '';
c.find(".selects_box").append(sertchHtml);
/*下拉搜索点击事件*/
c.find(".search_btn").on('click',function(){
var subData =[]
c.find(".item").each(function(i,el){
item = $(this).find(".select-name").text();
type = $(this).find(".select-name").attr("type");
if(item == o.field[i].title)item = '';
subData.push({'val':item});
});
item = c.find(".search_keyword").val();
subData.push({'val':item});
fwsSearchDate(showType,rows,subData,type,pageIndex);
});
}
//条件筛选
if(o.displayType === 'filter'){
var subData =[],resultsData = [],field = o.field;
c.prepend('
');
$.each(field,function(i,el){
c.find(".filter_box").append('
');
c.find(".filter_box").before(' ');
subData.push([]);
resultsData.push({});
if(field[i].children !=='' && field[i].children !== undefined){
var items = (field[i].children).split("+");
$.each(items,function(index,val){
subData[i].push({"id":val,"text":val});
});
}
var _this = this;
fwsSearchDate(showType,rows,resultsData,type,pageIndex);
/*c.find(".filter_item").eq(i).comboboxfilter({
scope: '', //显示已选中
idField: 'id', //检索字段
textField: 'text', //显示字段
text: this.title+':',//类型名称
multiple: false, //多选
unlimitText: '全部',
data:subData[i],
onChange:function(newValue){
pageIndex = 1;
loadBtn = true;
c.find(".filter_value").eq(i).val(newValue);
resultsData[i].val = newValue;
fwsSearchDate(showType,rows,resultsData,type,pageIndex);
item = resultsData;
}
});*/
});
}
/*查询条件*/
function getConditions(val,type){
var fieldfaculty = $("a[data-faculty='check']").text();
if(fieldfaculty == "全部"){
$(".mingc").html("姓氏首字母");
}else{
$(".mingc").html(fieldfaculty);
}
$(document).ready(function($){
$(".filter_box").after($(".mingc"));
if($(".mingc").text()== ""){
$(".mingc").html("姓氏首字母");
}
})
var conditdata = [],letterJson = [];
//判断条件是字段串
if(typeof val ==='string'){
if(val !== undefined && val !== ''){
var items = val.split("+");
if (o.conditFeild.length > 0){
$.each(o.conditFeild,function(index,value){
if(index == type){
$.each(items,function(i,el){
if(value.judge !== '=')el = '%'+el+'%';
letterJson.push({field:value.field, value:el, judge: value.judge});
});
}
});
}
if (letterJson != null && letterJson.length > 0) {
conditdata.push({orConditions: letterJson});
}
}
//判断条件是数组
}else if(typeof val ==='object'){
$.each(val,function(index,v){
letterJson.push([]);
if(v.val !== undefined && v.val !== ''){
var items = v.val.split(",");
$.each(items,function(i,el){
if(o.conditFeild[index].judge !== '=')el = '%'+el+'%';
letterJson[index].push({field:o.conditFeild[index].field, value:el, judge: o.conditFeild[index].judge});
});
if (letterJson != null && letterJson.length > 0){
conditdata.push({orConditions: letterJson[index]});
}
}
});
}
//conditdata.push({field:"language", value: o.lang, judge: "="});
//conditdata.push({field:"published", value:1, judge: "="});
return conditdata;
}
/*排序*/
function getOrders(){
var orderData = o.orderData;
return orderData;
}
/*返回属性*/
function getReturnInfos() {
var returnInfosdata = o.returnInfosdata;
return returnInfosdata;
}
//判断是否移动端
function checkMobileURL(teachUrl) {
var localUrl = window.location.href;
if (localUrl.indexOf("mainm.") !== -1 || localUrl.indexOf("listm.") !== -1){
return teachUrl.replace("list.", "listm.");
}
return teachUrl;
}
//对检索方法做精简
function fwsSearchDate(showType,rows,item,type,pageIndex){
return getSearchData(url,$listTable,showType,rows,item,type,pageIndex);
}
});
};
})(jQuery);