2020 * TODO:
2121 * Make optimization over class dependency tree.
2222 * Give more ways for the feedback of loading.
23+ * Use multiple SCRIPT tags so *.js can be downloaded asynchronously.
24+ * Use Class.for.... to load tabs.
2325 */
2426
2527/*-#
3133/*-#
3234 # parents -> sp
3335 # musts -> sm
34- # optionals -> so
36+ # xxxoptionals -> so
3537 # declaration -> dcl
3638 # optionalsLoaded -> oled
3739 #-*/
@@ -513,6 +515,9 @@ ClazzLoader.scriptCompleted = function (file) {};
513515/* protected */
514516ClazzLoader . globalLoaded = function ( ) { } ;
515517
518+ /* protected */
519+ ClazzLoader . keepOnLoading = true ;
520+
516521/* private */
517522/*-# mapPath2ClassNode -> p2node #-*/
518523ClazzLoader . mapPath2ClassNode = new Object ( ) ;
@@ -525,6 +530,16 @@ ClazzLoader.xhrOnload = function (transport, file) {
525530 ClazzLoader . tryToLoadNext ( file ) ;
526531 } else {
527532 try {
533+ /*
534+ if (transport.responseText.length >= 2048) {
535+ //fileCount++;
536+ }
537+ if (file.indexOf ("examples") != -1) {
538+ } else {
539+ log (transport.responseText.length + "::" + file);
540+ }
541+ */
542+ fileCount += transport . responseText . length ;
528543 eval ( transport . responseText ) ;
529544 } catch ( e ) {
530545 alert ( "[Java2Script] Script error: " + e . message ) ;
@@ -611,9 +626,19 @@ ClazzLoader.loadScript = function (file) {
611626 }
612627 }
613628 } ;
614- transport . send ( null ) ;
629+ try {
630+ transport . send ( null ) ;
631+ } catch ( e ) {
632+ alert ( "[Java2Script] Loading file error: " + e . message ) ;
633+ throw e ;
634+ }
615635 } else {
616- transport . send ( null ) ;
636+ try {
637+ transport . send ( null ) ;
638+ } catch ( e ) {
639+ alert ( "[Java2Script] Loading file error: " + e . message ) ;
640+ throw e ;
641+ }
617642 ClazzLoader . xhrOnload ( transport , file ) ;
618643 }
619644 return ;
@@ -694,6 +719,31 @@ ClazzLoader.loadScript = function (file) {
694719 ClazzLoader . scriptLoading ( file ) ;
695720} ;
696721
722+ /* public */
723+ ClazzLoader . loadCSS = function ( cssName ) {
724+ var cssKey = "" ;
725+ var idx = cssName . lastIndexOf ( "." ) ;
726+ if ( idx == - 1 ) {
727+ cssKey = cssName + ".css" ;
728+ } else {
729+ cssKey = cssName . substring ( idx + 1 ) + ".css" ;
730+ }
731+ var resLinks = document . getElementsByTagName ( "LINK" ) ;
732+ for ( var i = 0 ; i < resLinks . length ; i ++ ) {
733+ var cssPath = resLinks [ i ] . href ;
734+ if ( cssPath . lastIndexOf ( cssKey ) == cssPath . length - cssKey . length ) {
735+ return ;
736+ }
737+ }
738+
739+ /*-# cssLink -> rel #-*/
740+ var cssLink = document . createElement ( "LINK" ) ;
741+ cssLink . rel = "stylesheet" ;
742+ var path = ClazzLoader . getClasspathFor ( cssName ) ;
743+ cssLink . href = path . substring ( 0 , path . lastIndexOf ( ".js" ) ) + ".css" ;
744+ document . getElementsByTagName ( "HEAD" ) [ 0 ] . appendChild ( cssLink ) ;
745+ } ;
746+
697747/**
698748 * After class is loaded, this method will be executed to check whether there
699749 * are classes in the dependency tree that need to be loaded.
@@ -755,6 +805,9 @@ ClazzLoader.tryToLoadNext = function (file) {
755805 ClazzLoader . updateNode ( node ) ;
756806 }
757807 }
808+ if ( ! ClazzLoader . keepOnLoading ) {
809+ return ;
810+ }
758811 var n = ClazzLoader . findNextMustClass ( ClazzLoader . clazzTreeRoot ,
759812 ClazzNode . STATUS_KNOWN ) ;
760813 //alert ("next..." + n) ;
@@ -779,6 +832,8 @@ ClazzLoader.tryToLoadNext = function (file) {
779832 //log ("in optionals..." + n.name);
780833 ClazzLoader . loadClassNode ( n ) ;
781834 } else {
835+ //error ("no more optionals?");
836+ ClazzLoader . updateNode ( node ) ;
782837 ClazzLoader . globalLoaded ( ) ;
783838 //error ("no optionals?");
784839 }
@@ -827,9 +882,11 @@ ClazzLoader.updateNode = function (node) {
827882 if ( nn . status != ClazzNode . STATUS_OPTIONALS_LOADED
828883 && nn != n ) {
829884 nn . status = n . status ;
885+ nn . declaration = null ;
830886 ClazzLoader . updateNode ( nn ) ;
831887 }
832888 }
889+ n . declaration = null ;
833890 }
834891 } else {
835892 isMustsOK = false ;
@@ -887,24 +944,36 @@ ClazzLoader.updateNode = function (node) {
887944 }
888945 var level = ClazzNode . STATUS_DECLARED ;
889946 var isOptionsOK = false ;
890- if ( node . optionals == null || node . optionals . length == 0
947+ if ( ( ( node . optionals == null || node . optionals . length == 0 )
948+ && ( node . musts == null || node . musts . length == 0 ) )
891949 || node . declaration == null ) {
892950 isOptionsOK = true ;
893951 } else {
894952 isOptionsOK = true ;
895- for ( var i = 0 ; i < node . optionals . length ; i ++ ) {
896- var n = node . optionals [ i ] ;
953+ for ( var i = 0 ; i < node . musts . length ; i ++ ) {
954+ var n = node . musts [ i ] ;
897955 if ( n . status < ClazzNode . STATUS_OPTIONALS_LOADED ) {
898956 isOptionsOK = false ;
899957 }
900958 }
959+ if ( isOptionsOK ) {
960+ for ( var i = 0 ; i < node . optionals . length ; i ++ ) {
961+ var n = node . optionals [ i ] ;
962+ if ( n . status < ClazzNode . STATUS_OPTIONALS_LOADED ) {
963+ isOptionsOK = false ;
964+ }
965+ }
966+ }
901967 }
902968 if ( isOptionsOK ) {
903969 level = ClazzNode . STATUS_OPTIONALS_LOADED ;
904970 node . status = level ;
905971 ClazzLoader . scriptCompleted ( node . path ) ;
906972 if ( node . optionalsLoaded != null ) {
907973 node . optionalsLoaded ( ) ;
974+ if ( ! ClazzLoader . keepOnLoading ) {
975+ return false ;
976+ }
908977 }
909978 /*
910979 * For those classes within one *.js file, update
@@ -915,10 +984,15 @@ ClazzLoader.updateNode = function (node) {
915984 var list = node . declaration . clazzList ;
916985 for ( var j = 0 ; j < list . length ; j ++ ) {
917986 var nn = list [ j ] ;
918- if ( nn . status != ClazzNode . level && nn != node ) {
987+ if ( nn . status != level && nn != node ) {
988+ nn . status = level ;
989+ nn . declaration = null ;
919990 ClazzLoader . scriptCompleted ( nn . path ) ;
920991 if ( nn . optionalsLoaded != null ) {
921992 nn . optionalsLoaded ( ) ;
993+ if ( ! ClazzLoader . keepOnLoading ) {
994+ return false ;
995+ }
922996 }
923997 }
924998 }
@@ -1250,6 +1324,7 @@ ClazzLoader.queueBeforeString = new Array ();
12501324 */
12511325/* public */
12521326ClazzLoader . loadClass = function ( name , optionalsLoaded ) {
1327+ ClazzLoader . keepOnLoading = true ;
12531328 if ( ! ClazzLoader . isClassDefined ( "java.lang.String" )
12541329 && name . indexOf ( "java." ) != 0 ) {
12551330 var qbs = ClazzLoader . queueBeforeString ;
@@ -1282,7 +1357,10 @@ ClazzLoader.loadClass = function (name, optionalsLoaded) {
12821357 ClazzLoader . loadScript ( n . path ) ;
12831358 }
12841359 }
1360+ } else if ( optionalsLoaded != null && ClazzLoader . isClassDefined ( name ) ) {
1361+ optionalsLoaded ( ) ;
12851362 }
1363+
12861364} ;
12871365
12881366/**
0 commit comments