mlellipsis.js 5.62 KB
window.lineHeightDatabase={};

var $=require("jquery");

exports.init=function(){
	/*获取一段文本共计多少行*/
	$.fn.getTextLineNumber=function(keyword){
		//省略后加上title
		var $this=$(this);
		
		
		var fontSize ;
		if(!window.lineHeightDatabase[keyword]){
			window.lineHeightDatabase[keyword]={fontSize:0,lineHeight:0};
		}
		if(window.lineHeightDatabase[keyword].fontSize!=0)
		{
			fontSize=window.lineHeightDatabase[keyword].fontSize;
		}
		else
		{
			fontSize= getFinalStyle(this[0],"fontSize");
			window.lineHeightDatabase[keyword].fontSize=fontSize;
		}
		//获取计算后的样式
		if(window.lineHeightDatabase[keyword].lineHeight!=0)
		{
			lineHeight=window.lineHeightDatabase[keyword].lineHeight;
			window.lineHeightDatabase[keyword].lineHeight=lineHeight;
		}
		else
		{
			if(/msie/i.test(navigator.userAgent)){
				var lineHeight = getFinalStyle(this[0],"lineHeight",fontSize);
			}
			else{
				var lineHeight = getFinalStyle(this[0],"lineHeight");
			}
		}
		
		if(this[0]==null)
			return;
		
		var height  = this[0].clientHeight;
		if(lineHeight == "norm"){
			lineHeight = Number(fontSize*1.5);
			var nowStyle = $this.attr("style")||"";
			this[0].setAttribute("style",nowStyle+";line-height:"+lineHeight+"px");
		}
		else{lineHeight = Number(lineHeight);}
	
		return parseInt(height/lineHeight); 
	}
	$.fn.mlellipsis=function(row,keyword){
		//省略后加上title
		var $this=$(this);
		var title = $this.attr("_title");
		if(!title){
			$this.attr("_title",$this.text());
			
		}
		else{
			$this.text(title);
		}
		//获取计算后的样式
		var fontSize ;
		if(!window.lineHeightDatabase[keyword]){
			window.lineHeightDatabase[keyword]={fontSize:0,lineHeight:0};
		}
		if(window.lineHeightDatabase[keyword].fontSize!=0)
		{
			fontSize=window.lineHeightDatabase[keyword].fontSize;
		}
		else
		{
			var fontSize= getFinalStyle(this[0],"fontSize");
			window.lineHeightDatabase[keyword].fontSize=fontSize;
		}
		//获取计算后的样式
		if(window.lineHeightDatabase[keyword].lineHeight!=0)
		{
			lineHeight=window.lineHeightDatabase[keyword].lineHeight;
		}
		else
		{
			if(/msie/i.test(navigator.userAgent)){
				var lineHeight = getFinalStyle(this[0],"lineHeight",fontSize);
			}
			else{
				var lineHeight = getFinalStyle(this[0],"lineHeight");
				//window.lineHeightDatabase[keyword].lineHeight=lineHeight;
			}
		}
		if(!this[0])
			return;
		var height  = this[0].clientHeight;
		if(lineHeight == "norm"){
			lineHeight = Number(fontSize*1.5);
			var nowStyle = $this.attr("style")||"";
			this[0].setAttribute("style",nowStyle+";line-height:"+lineHeight+"px");
		}
		else{lineHeight = Number(lineHeight);}
	
		
		//若高度足够,则不用省略
		var dheight = Math.floor(row*lineHeight);
		if(height >= dheight)
		{
			var str = this.text();
			
			while(dheight*3<this[0].clientHeight){
				$this.text(str.substring(0,str.length/2));
				str = $this.text();
			}

			//减去末尾文字
			while(dheight<this[0].clientHeight){
				str = $this.text();
				$this.text(str.replace(/(\s)*([a-zA-Z0-9]?|\W)(\.\.\.)?$/,"..."));
			}
			return row;
		}
		else
		{
			return parseInt(height/lineHeight);
		}
		
	}
	$.fn.fillText=function(keyword){
		//省略后加上title
		var $this=$(this);
		var title = $this.attr("_title");
		if(!title){
			$this.attr("_title",$this.text());
			
		}
		else{
			$this.text(title);
		}
		//获取计算后的样式
		var fontSize ;
		if(!window.lineHeightDatabase[keyword]){
			window.lineHeightDatabase[keyword]={fontSize:0,lineHeight:0};
		}
		if(window.lineHeightDatabase[keyword].fontSize!=0)
		{
			fontSize=window.lineHeightDatabase[keyword].fontSize;
		}
		else
		{
			var fontSize= getFinalStyle(this[0],"fontSize");
			window.lineHeightDatabase[keyword].fontSize=fontSize;
		}
		//获取计算后的样式
		if(window.lineHeightDatabase[keyword].lineHeight!=0)
		{
			lineHeight=window.lineHeightDatabase[keyword].lineHeight;
		}
		else
		{
			if(/msie/i.test(navigator.userAgent)){
				var lineHeight = getFinalStyle(this[0],"lineHeight",fontSize);
			}
			else{
				var lineHeight = getFinalStyle(this[0],"lineHeight");
				//window.lineHeightDatabase[keyword].lineHeight=lineHeight;
			}
		}
		if(!this[0])
			return;
		var height  = this[0].clientHeight;
		if(lineHeight == "norm"){
			lineHeight = Number(fontSize*1.5);
			var nowStyle = $this.attr("style")||"";
			this[0].setAttribute("style",nowStyle+";line-height:"+lineHeight+"px");
		}
		else{lineHeight = Number(lineHeight);}
	
		
		//若高度足够,则不用省略
		if(height>5*lineHeight)
		{
			row=5;
		}
		else
		{
			row=(Math.floor(height/lineHeight)-1);
		}
		var titleHeight=$this.parent().find(".a-title").height()/30;
		var subTitleHeight=$this.parent().find(".a-subtitle").height()/30;
		if(titleHeight+subTitleHeight>=4)
		
		{
			row=4;
		}
		var dheight = Math.floor(row*lineHeight);
		if(height >= dheight)
		{
			var str = this.text();
			
			while(dheight*3<this[0].clientHeight){
				$this.text(str.substring(0,str.length/2));
				str = $this.text();
			}

			//减去末尾文字
			while(dheight<this[0].clientHeight){
				str = $this.text();
				$this.text(str.replace(/(\s)*([a-zA-Z0-9]?|\W)(\.\.\.)?$/,"..."));
			}
			return row;
		}
	}

}
		
function getFinalStyle( obj, property,fontSize)
{
	var s;
	if(obj!=null&&obj.currentStyle){
		s = parseInt(obj.currentStyle[property].replace("px",""));
	}
	else
	if(window.getComputedStyle&&obj!=null){
		s = window.getComputedStyle(obj, null)[property].replace("px","");
	}
	
	
	//兼容IEbug:IE解析getComputedStyle或currentStyle,然而若line-height=1.5,它会获取计算后是1.5,而其他浏览器获得1.5*line-height
	/*if(fontSize!=undefined){
		s = s*fontSize ;
	}*/
		return s;
	
		
}