search.js 3.66 KB

	var $=require("jquery");
	require("jquery.autocomplete");
	var box=require('yoho-box');

		if ($("#search-content").find(".content-item").length==0) {
			var clientHeight=document.body.clientHeight;
			$("#search-content").height(clientHeight-704);
		}
		
		$("#search-button-search").click(function(){
			var input=$("#input-search-search");
			if($.trim(input.val())=="")
			{
				box.alert("请输入搜索内容后继续");
			}
			else if (input.val().length>150) {
				box.alert("搜索内容超长,最多150个字");
			}
			else
			{
				window.location.href=YohoConfig.mainUrl+"/search?q="+input.val();
			}
		});
		$("#input-search-search").autocomplete('/search/default/words', {
		        	resultsClass:  "search-suggest helveticaneue", 
		        	loadingClass:"loading",
		            width:249, 
		            scrollHeight:360, 
		            max:10, 
		            selectFirst:false, 
		            autoFill:false,
		            formatResult: function(row, data)
		            {
		                return data.replace(/<\/?[^>]*>/g,'');
		            }
		        }).result(function(event, data, formatted) 
		        {
				
				if (data.length>150) {
					box.alert("搜索内容超长,最多150个字");
				}
				else
				{
					window.location.href = YohoConfig.mainUrl+'./search?q=' + encodeURIComponent(formatted.replace(/<\/?[^>]*>/g,''));
				}
		           // window.location.href =YohoConfig.mainUrl+"/search?q="+encodeURIComponent(formatted.replace(/<\/?[^>]*>/g,''));
		        })
		.keydown(function(event){
			if(event.keyCode==13)
			{
				$("#search-button-search").trigger("click");
			}
		});
//首页碎片交互=================================================================================================



function dealItems() {
    var citems = $(".content-item,.content-item-without-border");
    if (!citems.length) {
        return;
    }
    var totalLineNum = 7;
    // 7 行
    var maxTitleStrNum = 45;
    // 15个字
    var maxDetailStrNum = 24;
    // 22个字

    if (document.body.clientWidth < 1190) {
        totalLineNum = 6;
        // 6 行
        maxTitleStrNum = 39;
        // 13个字
        maxDetailStrNum = 54;
        // 18个字
    }

    var titleLineNum = 0;
    var subTitleLineNum = 0;
    var detailLineNum = 0;
    var detailStr = "";
    var self = null;

    citems.each(function() {
        self = $(this);

        var itemTitleObj= self.find(".content-item-title");
        // 主标题行数
        if(typeof(itemTitleObj.attr("_title"))==="undefined"||itemTitleObj.attr("_title")===null)
            return;
        titleLineNum = 1;
        if (maxTitleStrNum < itemTitleObj.attr("_title").length) {
            titleLineNum = 2;
        }

        // 副标题行数
        subTitleLineNum = 1;
        if (maxTitleStrNum < $(".content-item-subtitle").attr("_title").length) {
            subTitleLineNum = 2;
        }

        // 摘要内容行数
        
        detailLineNum = totalLineNum - titleLineNum - subTitleLineNum;
        if(itemTitleObj.text()=="sports"){
            console.log((maxDetailStrNum * detailLineNum) );
        }
        if ((maxDetailStrNum * detailLineNum) < self.find(".content-item-detail").attr("_title").length) {
            detailStr = self.find(".content-item-detail").attr("_title").substring(0, maxDetailStrNum * detailLineNum ) + " ...";
            
            self.find(".content-item-detail").text(detailStr.replace(/[a-zA-Z]+?\s{1}\.\.\.\$/, " ..."));
        }
        else
        {
            self.find(".content-item-detail").text(self.find(".content-item-detail").attr("_title"));
        }
    });
    citems = null;
}
dealItems();
//=============================================================