/[drupal]/contributions/themes/ranch/niftycube.js
ViewVC logotype

Contents of /contributions/themes/ranch/niftycube.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2