-
Notifications
You must be signed in to change notification settings - Fork 0
/
harlequin.min.js
1 lines (1 loc) · 4 KB
/
harlequin.min.js
1
Harlequin=function(){var e=null,t=[],n=true,r="stripe",s={};var o={},u={color_class:"color",start_hue:0,end_hue:120,sat:90,lightness:70,colors:null};if(typeof jQuery!="undefined"){$.expr[":"].harlequin=function(){return true}}populateCells=function(n){if(n=="both"){t.push({index:-1,sort:e.data("sort")||"low-to-high"})}else{var r=0,i=0;var s=n=="column"?"thead th":n=="row"?"tbody tr":false;if(s==false)return;$(s,e).each(function(){if($(this).attr("colspan")!=undefined){var e=parseInt($(this).attr("colspan"));if($(this).hasClass(o.color_class)){for(var n=0;n<e;n++){t.push({index:r+n,sort:$(this).data("sort")||"low-to-high"})}}else{r+=e}}else{if($(this).hasClass(o.color_class)){t.push({index:r,sort:$(this).data("sort")||"low-to-high"})}}r++})}};paintCells=function(n){for(i=0,len=t.length;i<len;i++){var r=t[i],s=r.index;seg_max=null,seg_min=null,cells=[];var o=n=="column"?$("tbody tr td:nth-child("+(s+1)+")",e):n=="row"?$("tbody tr:nth-child("+(s+1)+")",e).find("td"):n=="both"?$("tbody td",e):false;if(o==false)return;o.each(function(){var e=$(this);var t={orig:e.text(),value:parseFloat(e.text().replace(/[^0-9\.\-]+/g,"")),el:e};if(t.value.length<1){return true}if(!isNaN(t.value)){if(seg_min==null){seg_min=t.value}else if(t.value<seg_min){seg_min=t.value}if(seg_max==null){seg_max=t.value}else if(t.value>seg_max){seg_max=t.value}}cells.push(t)});for(j=0,jlen=cells.length;j<jlen;j++){paintCell(cells[j],seg_min,seg_max,r.sort)}}};paintCell=function(e,t,n,i){color=getColor(e.value,t,n,i);if(typeof r=="object"){for(var u=0,a=r.length;u<a;u++){s[r[u]](e,t,n,i,color,o)}}else{s[r](e,t,n,i,color,o)}};registerPainter=function(e,t){if(typeof t!="function")return;s[e]=t};getColor=function(e,t,r,i){var s;if(o.colors!=null&&typeof o.colors=="object"){var u=o.colors.length,a=Math.floor((e-t)/(r-t)*u)-1;if(a==-1)a=0;s=o.colors[a]}else{var f=o.end_hue-o.start_hue;if(i=="high-to-low"){var l=Math.ceil(o.end_hue-(e-t)/(r-t)*f)}else{var l=Math.ceil(o.start_hue+(e-t)/(r-t)*f)}if(t==r)l=Math.ceil(f/2);s="hsl("+l+","+o.sat+"%,"+o.lightness+"%)";if(!n){s=hsl2hex(l,o.sat,o.lightness)}}return s};testHsl=function(){var e=document.createElement("harlequin");e.style.cssText="background-color:hsla(120,40%,100%,.5)";return!!~(""+e.style.backgroundColor).indexOf("rgba")||!!~(""+e.style.backgroundColor).indexOf("hsla")};hsl2hex=function(e,t,n){function s(e){e=e<0?e+1:e>1?e-1:e;if(e*6<1)return i+(r-i)*e*6;else if(e*2<1)return r;else if(e*3<2)return i+(r-i)*(2/3-e)*6;else return i}e=e%360/360;t=t/100;n=n/100;if(t==0){n=Math.round(n*255);return{r:n,g:n,b:n}}var r=n>=.5?n+t-n*t:n*(1+t);var i=2*n-r;return"#"+(Math.round(s(e-1/3)*255)|Math.round(s(e)*255)<<8|Math.round(s(e+1/3)*255)<<16).toString(16)};reset=function(){t=[];o={}};paint=function(t,n,i,s){reset();e=$("#"+n);r=t||"stripe";if(s!=undefined){for(k in u){o[k]=s[k]!=undefined?s[k]:u[k]}}else{o=u}populateCells(i);paintCells(i)};n=testHsl();registerPainter("stripe",function(e,t,n,r,i){e.el.css("background-color",i)});registerPainter("bar",function(e,t,n,r){var i=document.createElement("span"),s=document.createElement("span");i.innerHTML=e.orig;i.style.background=color;i.style.width=Math.floor((e.value-t)/(n-t)*100)+"%";i.style.display="inline-block";i.style.textAlign="center";e.el.textAlign="left";e.el[0].innerHTML="";e.el[0].appendChild(i)});registerPainter("dot",function(e,t,n,r){var i=document.createElement("span"),s=document.createElement("span");max_radius=Math.floor(e.el.height()*4),radius=Math.floor((e.value-t)/(n-t)*max_radius)+3;i.innerHTML=e.orig;i.style.background=color;i.style.display="inline-block";i.style.textAlign="center";i.style.width=i.style.height=radius+"px";i.style.borderRadius=i.style.MozBorderRadius=i.style.WebkitBorderRadius="999px";i.style.position="absolute";i.style.zIndex=0;s.style.zIndex=1;s.style.display="inline-block";s.style.lineHeight=e.el.height();e.el[0].style.position="relative";e.el[0].innerHTML="";e.el[0].appendChild(i);e.el[0].appendChild(s)});return{paint:paint,register:registerPainter,stripe:function(e,t,n){paint("stripe",e,t,n)},bar:function(e,t,n){paint("bar",e,t,n)}}}()