source: trunk/spip/esqueleto-redcta/themes/En-rondeur/javascript/niftycube.js @ 30

Last change on this file since 30 was 30, checked in by sebas, 17 years ago

nueva importacion del codigo del esqueleto de redcta con los plugins

File size: 8.4 KB
Line 
1/* Nifty Corners Cube - rounded corners with CSS and Javascript
2Copyright 2006 Alessandro Fulciniti (a.fulciniti@html.it)
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*/
18
19var niftyOk=(document.getElementById && document.createElement && Array.prototype.push);
20var niftyCss=true;
21
22String.prototype.find=function(what){
23return(this.indexOf(what)>=0 ? true : false);
24}
25
26var oldonload=window.onload;
27if(typeof(NiftyLoad)!='function') NiftyLoad=function(){};
28if(typeof(oldonload)=='function')
29    window.onload=function(){oldonload();AddCss();NiftyLoad()};
30else window.onload=function(){AddCss();NiftyLoad()};
31
32/*
33function AddCss(){
34niftyCss=true;
35var l=CreateEl("link");
36l.setAttribute("type","text/css");
37l.setAttribute("rel","stylesheet");
38l.setAttribute("href","niftyCorners.css");
39l.setAttribute("media","screen");
40document.getElementsByTagName("head")[0].appendChild(l);
41}
42*/
43
44function Nifty(selector,options){
45if(niftyOk==false) return;
46if(niftyCss==false) AddCss();
47var i,v=selector.split(","),h=0;
48if(options==null) options="";
49if(options.find("fixed-height"))
50    h=getElementsBySelector(v[0])[0].offsetHeight;
51for(i=0;i<v.length;i++)
52    Rounded(v[i],options);
53if(options.find("height")) SameHeight(selector,h);
54}
55
56function Rounded(selector,options){
57var i,top="",bottom="",v=new Array();
58if(options!=""){
59    options=options.replace("left","tl bl");
60    options=options.replace("right","tr br");
61    options=options.replace("top","tr tl");
62    options=options.replace("bottom","br bl");
63    options=options.replace("transparent","alias");
64    if(options.find("tl")){
65        top="both";
66        if(!options.find("tr")) top="left";
67        }
68    else if(options.find("tr")) top="right";
69    if(options.find("bl")){
70        bottom="both";
71        if(!options.find("br")) bottom="left";
72        }
73    else if(options.find("br")) bottom="right";
74    }
75if(top=="" && bottom=="" && !options.find("none")){top="both";bottom="both";}
76v=getElementsBySelector(selector);
77for(i=0;i<v.length;i++){
78    FixIE(v[i]);
79    if(top!="") AddTop(v[i],top,options);
80    if(bottom!="") AddBottom(v[i],bottom,options);
81    }
82}
83
84function AddTop(el,side,options){
85var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
86d.style.marginLeft="-"+getPadding(el,"Left")+"px";
87d.style.marginRight="-"+getPadding(el,"Right")+"px";
88if(options.find("alias") || (color=getBk(el))=="transparent"){
89    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
90    }
91else{
92    bk=getParentBk(el); border=Mix(color,bk);
93    }
94d.style.background=bk;
95d.className="niftycorners";
96p=getPadding(el,"Top");
97if(options.find("small")){
98    d.style.marginBottom=(p-2)+"px";
99    btype+="s"; lim=2;
100    }
101else if(options.find("big")){
102    d.style.marginBottom=(p-10)+"px";
103    btype+="b"; lim=8;
104    }
105else d.style.marginBottom=(p-5)+"px";
106for(i=1;i<=lim;i++)
107    d.appendChild(CreateStrip(i,side,color,border,btype));
108el.style.paddingTop="0";
109el.insertBefore(d,el.firstChild);
110}
111
112function AddBottom(el,side,options){
113var d=CreateEl("b"),lim=4,border="",p,i,btype="r",bk,color;
114d.style.marginLeft="-"+getPadding(el,"Left")+"px";
115d.style.marginRight="-"+getPadding(el,"Right")+"px";
116if(options.find("alias") || (color=getBk(el))=="transparent"){
117    color="transparent";bk="transparent"; border=getParentBk(el);btype="t";
118    }
119else{
120    bk=getParentBk(el); border=Mix(color,bk);
121    }
122d.style.background=bk;
123d.className="niftycorners";
124p=getPadding(el,"Bottom");
125if(options.find("small")){
126    d.style.marginTop=(p-2)+"px";
127    btype+="s"; lim=2;
128    }
129else if(options.find("big")){
130    d.style.marginTop=(p-10)+"px";
131    btype+="b"; lim=8;
132    }
133else d.style.marginTop=(p-5)+"px";
134for(i=lim;i>0;i--)
135    d.appendChild(CreateStrip(i,side,color,border,btype));
136el.style.paddingBottom=0;
137el.appendChild(d);
138}
139
140function CreateStrip(index,side,color,border,btype){
141var x=CreateEl("b");
142x.className=btype+index;
143x.style.backgroundColor=color;
144x.style.borderColor=border;
145if(side=="left"){
146    x.style.borderRightWidth="0";
147    x.style.marginRight="0";
148    }
149else if(side=="right"){
150    x.style.borderLeftWidth="0";
151    x.style.marginLeft="0";
152    }
153return(x);
154}
155
156function CreateEl(x){
157return(document.createElement(x));
158}
159
160function FixIE(el){
161if(el.currentStyle!=null && el.currentStyle.hasLayout!=null && el.currentStyle.hasLayout==false)
162    el.style.display="inline-block";
163}
164
165function SameHeight(selector,maxh){
166var i,v=selector.split(","),t,j,els=[],gap;
167for(i=0;i<v.length;i++){
168    t=getElementsBySelector(v[i]);
169    els=els.concat(t);
170    }
171for(i=0;i<els.length;i++){
172    if(els[i].offsetHeight>maxh) maxh=els[i].offsetHeight;
173    els[i].style.height="auto";
174    }
175for(i=0;i<els.length;i++){
176    gap=maxh-els[i].offsetHeight;
177    if(gap>0){
178        t=CreateEl("b");t.className="niftyfill";t.style.height=gap+"px";
179        nc=els[i].lastChild;
180        if(nc.className=="niftycorners")
181            els[i].insertBefore(t,nc);
182        else els[i].appendChild(t);
183        }
184    }
185}
186
187function getElementsBySelector(selector){
188var i,j,selid="",selclass="",tag=selector,tag2="",v2,k,f,a,s=[],objlist=[],c;
189if(selector.find("#")){ //id selector like "tag#id"
190    if(selector.find(" ")){  //descendant selector like "tag#id tag"
191        s=selector.split(" ");
192        var fs=s[0].split("#");
193        if(fs.length==1) return(objlist);
194        f=document.getElementById(fs[1]);
195        if(f){
196            v=f.getElementsByTagName(s[1]);
197            for(i=0;i<v.length;i++) objlist.push(v[i]);
198            }
199        return(objlist);
200        }
201    else{
202        s=selector.split("#");
203        tag=s[0];
204        selid=s[1];
205        if(selid!=""){
206            f=document.getElementById(selid);
207            if(f) objlist.push(f);
208            return(objlist);
209            }
210        }
211    }
212if(selector.find(".")){      //class selector like "tag.class"
213    s=selector.split(".");
214    tag=s[0];
215    selclass=s[1];
216    if(selclass.find(" ")){   //descendant selector like tag1.classname tag2
217        s=selclass.split(" ");
218        selclass=s[0];
219        tag2=s[1];
220        }
221    }
222var v=document.getElementsByTagName(tag);  // tag selector like "tag"
223if(selclass==""){
224    for(i=0;i<v.length;i++) objlist.push(v[i]);
225    return(objlist);
226    }
227for(i=0;i<v.length;i++){
228    c=v[i].className.split(" ");
229    for(j=0;j<c.length;j++){
230        if(c[j]==selclass){
231            if(tag2=="") objlist.push(v[i]);
232            else{
233                v2=v[i].getElementsByTagName(tag2);
234                for(k=0;k<v2.length;k++) objlist.push(v2[k]);
235                }
236            }
237        }
238    }
239return(objlist);
240}
241
242function getParentBk(x){
243var el=x.parentNode,c;
244while(el.tagName.toUpperCase()!="HTML" && (c=getBk(el))=="transparent")
245    el=el.parentNode;
246if(c=="transparent") c="#FFFFFF";
247return(c);
248}
249
250function getBk(x){
251var c=getStyleProp(x,"backgroundColor");
252if(c==null || c=="transparent" || c.find("rgba(0, 0, 0, 0)"))
253    return("transparent");
254if(c.find("rgb")) c=rgb2hex(c);
255return(c);
256}
257
258function getPadding(x,side){
259var p=getStyleProp(x,"padding"+side);
260if(p==null || !p.find("px")) return(0);
261return(parseInt(p));
262}
263
264function getStyleProp(x,prop){
265if(x.currentStyle)
266    return(x.currentStyle[prop]);
267if(document.defaultView.getComputedStyle)
268    return(document.defaultView.getComputedStyle(x,'')[prop]);
269return(null);
270}
271
272function rgb2hex(value){
273var hex="",v,h,i;
274var regexp=/([0-9]+)[, ]+([0-9]+)[, ]+([0-9]+)/;
275var h=regexp.exec(value);
276for(i=1;i<4;i++){
277    v=parseInt(h[i]).toString(16);
278    if(v.length==1) hex+="0"+v;
279    else hex+=v;
280    }
281return("#"+hex);
282}
283
284function Mix(c1,c2){
285var i,step1,step2,x,y,r=new Array(3);
286if(c1.length==4)step1=1;
287else step1=2;
288if(c2.length==4) step2=1;
289else step2=2;
290for(i=0;i<3;i++){
291    x=parseInt(c1.substr(1+step1*i,step1),16);
292    if(step1==1) x=16*x+x;
293    y=parseInt(c2.substr(1+step2*i,step2),16);
294    if(step2==1) y=16*y+y;
295    r[i]=Math.floor((x*50+y*50)/100);
296    r[i]=r[i].toString(16);
297    if(r[i].length==1) r[i]="0"+r[i];
298    }
299return("#"+r[0]+r[1]+r[2]);
300}
Note: See TracBrowser for help on using the repository browser.