1- /*
2- * The following codes are moved from ClassExt.js, with modifications
3- */
4-
51/*
62 * Standing for SWT CSS, with all "S" into "$". We love dollors. -zhou renjian
73 *
@@ -14,6 +10,124 @@ $WTC$$ = {};
1410$WTC$$ . cssAlreadyAggregated = false ;
1511$WTC$$ . cssForcedUsingFile = false ;
1612
13+ /* private */
14+ $WTC$$ . timeouts = new Object ( ) ;
15+ $WTC$$ . triedTimes = new Object ( ) ;
16+ $WTC$$ . cssTestEls = [ ] ;
17+ /* private */
18+ $WTC$$ . trackCSS = function ( clazzName ) {
19+ var el = document . createElement ( "DIV" ) ;
20+ el . style . cssText = "position:absolute;left:-1000px;top:-100px;font-size:0;display:block;" ;
21+ var cssID = $WTC$$ . getCSSRuleID ( clazzName ) ;
22+ el . className = cssID ;
23+ if ( document . body != null ) {
24+ document . body . appendChild ( el ) ;
25+ }
26+
27+ var els = $WTC$$ . cssTestEls ;
28+ for ( var i = 0 ; i < els . length ; i ++ ) {
29+ if ( els [ i ] == el ) {
30+ return ;
31+ }
32+ }
33+ els [ els . length ] = el ;
34+ var f = ( function ( e , name ) {
35+ return function ( ) {
36+ $WTC$$ . removeTesting ( name ) ;
37+ var tts = $WTC$$ . triedTimes ;
38+ if ( tts [ e . className ] > 1 ) { // already tried once
39+ alert ( "[Java2Script] Error in loading CSS for " + name + "!" ) ;
40+ } else {
41+ // alert ("Try to load *.css " + name + " again.");
42+ /*
43+ * Remove existed *.css
44+ */
45+ var head = document . getElementsByTagName ( "HEAD" ) [ 0 ] ;
46+ var links = head . getElementsByTagName ( "LINK" ) ;
47+ for ( var i = 0 ; i < links . length ; i ++ ) {
48+ if ( links [ i ] . id == "c$$." + name ) {
49+ head . removeChild ( links [ i ] ) ;
50+ break ;
51+ }
52+ }
53+ $WTC$$ . registerCSS ( name ) ;
54+ }
55+ } ;
56+ } ) ( el , clazzName ) ;
57+ var handle = window . setTimeout ( f , 15000 ) ; // 15 seconds, should be 30s
58+
59+ $WTC$$ . timeouts [ cssID ] = handle ;
60+ var tts = $WTC$$ . triedTimes ;
61+ if ( tts [ cssID ] == null ) {
62+ tts [ cssID ] = 1 ;
63+ } else {
64+ tts [ cssID ] ++ ;
65+ }
66+ if ( els . length == 1 ) { // start global checking
67+ $WTC$$ . globalChecking ( ) ;
68+ }
69+ } ;
70+ /* protected */
71+ $WTC$$ . cssLoaded = function ( clazzName ) { } ;
72+ /* protected */
73+ $WTC$$ . globalLoaded = function ( ) { } ;
74+ /* private */
75+ $WTC$$ . removeTesting = function ( clazzName ) {
76+ var cssClassName = $WTC$$ . getCSSRuleID ( clazzName ) ;
77+ var els = $WTC$$ . cssTestEls ;
78+ for ( var i = 0 ; i < els . length ; i ++ ) {
79+ if ( els [ i ] . className == cssClassName ) {
80+ var el = els [ i ] ;
81+ if ( el . parentNode != null ) {
82+ el . parentNode . removeChild ( el ) ;
83+ }
84+ for ( var j = i ; j < els . length - 1 ; j ++ ) {
85+ els [ j ] = els [ j + 1 ] ;
86+ }
87+ els . length -- ;
88+ return el ;
89+ }
90+ }
91+ return null ;
92+ } ;
93+ /* private */
94+ $WTC$$ . globalChecking = function ( ) {
95+ var els = $WTC$$ . cssTestEls ;
96+ for ( var i = 0 ; i < els . length ; i ++ ) {
97+ var el = els [ i ] ;
98+ var w = Math . max ( el . offsetWidth , Math . max ( el . clientWidth , el . scrollWidth ) ) ;
99+ if ( w == 324 ) { // there are "width:324px;" rules in SWT's *.css
100+ //log ("CSS: " + el.className + " loaded!");
101+ $WTC$$ . cssLoaded ( el . className ) ;
102+ window . clearTimeout ( $WTC$$ . timeouts [ el . className ] ) ;
103+ if ( el . parentNode != null ) {
104+ el . parentNode . removeChild ( el ) ;
105+ }
106+ for ( var j = i ; j < els . length - 1 ; j ++ ) {
107+ els [ j ] = els [ j + 1 ] ;
108+ }
109+ els . length -- ;
110+ }
111+ }
112+ if ( els . length != 0 ) {
113+ //alert ("continue checking ...");
114+ return window . setTimeout ( $WTC$$ . globalChecking , 100 ) ;
115+ } else {
116+ //log ("finished !");
117+ $WTC$$ . globalLoaded ( ) ;
118+ }
119+ } ;
120+
121+ /* private */
122+ $WTC$$ . getCSSRuleID = function ( clazzName ) {
123+ var cssRuleID = null ;
124+ if ( clazzName . indexOf ( "org.eclipse.swt." ) == 0 || clazzName . indexOf ( "$wt." ) ) {
125+ var idx = clazzName . indexOf ( "wt." ) + 3 ;
126+ cssRuleID = "swt." + clazzName . substring ( idx ) ;
127+ }
128+ return cssRuleID . toLowerCase ( ) . replace ( / \. / g, '-' ) ;
129+ } ;
130+
17131/**
18132 * Register css for the given class. If the given css text is null, it will
19133 * try to find relative *.css file instead loading css text directly.
@@ -41,6 +155,10 @@ $WTC$$.registerCSS = function (clazzName, cssText) {
41155 cssLink . id = cssID ;
42156 cssLink . href = cssPath ;
43157 document . getElementsByTagName ( "HEAD" ) [ 0 ] . appendChild ( cssLink ) ;
158+ /*
159+ * register onload event callback
160+ */
161+ $WTC$$ . trackCSS ( clazzName ) ;
44162 } else {
45163 var prefix = "" ;
46164 var idx = cssPath . lastIndexOf ( "/" ) ;
@@ -95,6 +213,9 @@ $WTC$$.registerCSS = function (clazzName, cssText) {
95213 }
96214 }
97215
216+ /*
217+ * This method will generated a lot non existed *.css resouce requests (404) for server.
218+ */
98219 var len = $WTC$$ . themes . length ;
99220 for ( var i = 0 ; i < len ; i ++ ) {
100221 var themeName = $WTC$$ . themes [ i ] ;
@@ -104,49 +225,13 @@ $WTC$$.registerCSS = function (clazzName, cssText) {
104225 cssLink . id = cssID + themeName ;
105226 cssLink . href = themePath ;
106227 document . getElementsByTagName ( "HEAD" ) [ 0 ] . appendChild ( cssLink ) ;
228+ /*
229+ * OK, theme *.css may be not essential. So no onload callbacks here.
230+ */
107231 }
108232 }
109233} ;
110234
111- /*
112- * The above codes are moved from ClassExt.js
113- */
114-
115- /*
116- * The following codes are moved from ClassLoader.js
117- */
118-
119- /*
120- * loadCSS may be considered part of SWT library. Should be packed with
121- * SWT not with Java core.
122- *
123- * Not used in other *.js yet.
124- * - Nov 8, 2006
125- */
126- /* public */
127- $WTC$$ . loadCSS = function ( cssName ) {
128- var cssKey = "" ;
129- var idx = cssName . lastIndexOf ( "." ) ;
130- if ( idx == - 1 ) {
131- cssKey = cssName + ".css" ;
132- } else {
133- cssKey = cssName . substring ( idx + 1 ) + ".css" ;
134- }
135- var resLinks = document . getElementsByTagName ( "LINK" ) ;
136- for ( var i = 0 ; i < resLinks . length ; i ++ ) {
137- var cssPath = resLinks [ i ] . href ;
138- if ( cssPath . lastIndexOf ( cssKey ) == cssPath . length - cssKey . length ) {
139- return ;
140- }
141- }
142-
143- /*-# cssLink -> rel #-*/
144- var cssLink = document . createElement ( "LINK" ) ;
145- cssLink . rel = "stylesheet" ;
146- var path = ClazzLoader . getClasspathFor ( cssName ) ;
147- cssLink . href = path . substring ( 0 , path . lastIndexOf ( ".js" ) ) + ".css" ;
148- document . getElementsByTagName ( "HEAD" ) [ 0 ] . appendChild ( cssLink ) ;
149- } ;
150235/*
151236 * This array will preserves the themes order.
152237 */
@@ -177,10 +262,6 @@ $WTC$$.registerTheme = function(themeName, themePath){
177262 }
178263
179264} ;
180-
181- /*
182- * The above codes are moved from ClassLoader.js
183- */
184265
185266Clazz . declarePackage ( "org.eclipse.swt" ) ;
186267$wt = org . eclipse . swt ;
0 commit comments