LoadingManager.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. function LoadingManager( onLoad, onProgress, onError ) {
  5. var scope = this;
  6. var isLoading = false;
  7. var itemsLoaded = 0;
  8. var itemsTotal = 0;
  9. var urlModifier = undefined;
  10. var handlers = [];
  11. // Refer to #5689 for the reason why we don't set .onStart
  12. // in the constructor
  13. this.onStart = undefined;
  14. this.onLoad = onLoad;
  15. this.onProgress = onProgress;
  16. this.onError = onError;
  17. this.itemStart = function ( url ) {
  18. itemsTotal ++;
  19. if ( isLoading === false ) {
  20. if ( scope.onStart !== undefined ) {
  21. scope.onStart( url, itemsLoaded, itemsTotal );
  22. }
  23. }
  24. isLoading = true;
  25. };
  26. this.itemEnd = function ( url ) {
  27. itemsLoaded ++;
  28. if ( scope.onProgress !== undefined ) {
  29. scope.onProgress( url, itemsLoaded, itemsTotal );
  30. }
  31. if ( itemsLoaded === itemsTotal ) {
  32. isLoading = false;
  33. if ( scope.onLoad !== undefined ) {
  34. scope.onLoad();
  35. }
  36. }
  37. };
  38. this.itemError = function ( url ) {
  39. if ( scope.onError !== undefined ) {
  40. scope.onError( url );
  41. }
  42. };
  43. this.resolveURL = function ( url ) {
  44. if ( urlModifier ) {
  45. return urlModifier( url );
  46. }
  47. return url;
  48. };
  49. this.setURLModifier = function ( transform ) {
  50. urlModifier = transform;
  51. return this;
  52. };
  53. this.addHandler = function ( regex, loader ) {
  54. handlers.push( regex, loader );
  55. return this;
  56. };
  57. this.removeHandler = function ( regex ) {
  58. var index = handlers.indexOf( regex );
  59. if ( index !== - 1 ) {
  60. handlers.splice( index, 2 );
  61. }
  62. return this;
  63. };
  64. this.getHandler = function ( file ) {
  65. for ( var i = 0, l = handlers.length; i < l; i += 2 ) {
  66. var regex = handlers[ i ];
  67. var loader = handlers[ i + 1 ];
  68. if ( regex.test( file ) ) {
  69. return loader;
  70. }
  71. }
  72. return null;
  73. };
  74. }
  75. var DefaultLoadingManager = new LoadingManager();
  76. export { DefaultLoadingManager, LoadingManager };