Source: loader/LoadingManager.js

  1. /**
  2. * Handles and keeps track of loaded and pending data. A default global instance of this class is created and used by loaders if not supplied manually - see {@link zen3d.DefaultLoadingManager}.
  3. * In general that should be sufficient, however there are times when it can be useful to have seperate loaders - for example if you want to show seperate loading bars for objects and textures.
  4. * @constructor
  5. * @author mrdoob / http://mrdoob.com/
  6. * @param {Function} onLoad — (optional) this function will be called when all loaders are done.
  7. * @param {Function} onProgress — (optional) this function will be called when an item is complete.
  8. * @param {Function} onError — (optional) this function will be called a loader encounters errors.
  9. */
  10. function LoadingManager(onLoad, onProgress, onError) {
  11. var scope = this;
  12. var isLoading = false;
  13. var itemsLoaded = 0;
  14. var itemsTotal = 0;
  15. var urlModifier = undefined;
  16. // Refer to #5689 for the reason why we don't set .onStart
  17. // in the constructor
  18. this.onStart = undefined;
  19. this.onLoad = onLoad;
  20. this.onProgress = onProgress;
  21. this.onError = onError;
  22. this.itemStart = function (url) {
  23. itemsTotal++;
  24. if (isLoading === false) {
  25. if (scope.onStart !== undefined) {
  26. scope.onStart(url, itemsLoaded, itemsTotal);
  27. }
  28. }
  29. isLoading = true;
  30. };
  31. this.itemEnd = function (url) {
  32. itemsLoaded++;
  33. if (scope.onProgress !== undefined) {
  34. scope.onProgress(url, itemsLoaded, itemsTotal);
  35. }
  36. if (itemsLoaded === itemsTotal) {
  37. isLoading = false;
  38. if (scope.onLoad !== undefined) {
  39. scope.onLoad();
  40. }
  41. }
  42. };
  43. this.itemError = function (url) {
  44. if (scope.onError !== undefined) {
  45. scope.onError(url);
  46. }
  47. };
  48. this.resolveURL = function (url) {
  49. if (urlModifier) {
  50. return urlModifier(url);
  51. }
  52. return url;
  53. };
  54. this.setURLModifier = function (transform) {
  55. urlModifier = transform;
  56. return this;
  57. };
  58. }
  59. var DefaultLoadingManager = new LoadingManager();
  60. export { DefaultLoadingManager, LoadingManager };