home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / browser / ns405lyc / netcast.z / ncjs10.jar / layermgr.js < prev    next >
Encoding:
JavaScript  |  1998-03-06  |  8.1 KB  |  309 lines

  1. /* layermgr.js
  2.  
  3.    Copyright (c) 1997 Netscape Comunications Corporation,
  4.    All Rights Reserved
  5.  
  6.    Handles layer management in an efficient way
  7.    
  8. */
  9.  
  10. // An object which encapsulates the layer loading.
  11. //
  12. //  layerObj specifies a layer object in which to load the content;
  13. //     null specifies that the layer should be created when it's the layer's
  14. //     turn to load.
  15. //
  16. //  layerURL gives the URL of source content to load into the layer.
  17. //     null indicates that no special loading should occur (usually only useful
  18. //     if layerObj is also null). 
  19. //  
  20. //  container is used only for new layers, to specify the parent layer
  21. //
  22. //  notifier specifes an object whose layerLoaded function will be called,
  23. //     with the parameter specified in parameter when the layer loads.
  24. //
  25. //
  26.  
  27. function LayerLoadObject(layerObj, url, container, notifier, parameter, external)
  28. {
  29.     this.layerObj = layerObj;
  30.     this.layerURL = url;
  31.     this.container = container;
  32.     this.notifier = notifier;
  33.     this.parameter = parameter
  34.     this.nextLoad = null;
  35.     this.external = external;
  36. }
  37.  
  38.  
  39. // A "stock" notifier to load content into newly created layers.
  40. //  Takes a layerLoadObject.
  41.  
  42. function LayerMgr_NewLayerAndLoad(layerObj, layerLoadObject) {
  43.     // just re-queue the request.
  44.     
  45.     layerLoadObject.layerObj = layerObj;
  46.     
  47.     window.layerManager.layerLoad(layerLoadObject);
  48.     
  49.     return;
  50. }
  51.  
  52. // LayerLoad takes a layer load object and adds it to the pending queue
  53. //   of loading operations.
  54.  
  55. function LayerMgr_LayerLoad(layerLoadObject)
  56. {
  57.  
  58.     // Add this entry to the end of the queue (atomically) and then
  59.     // schedule it for loading.
  60.     
  61.     if (this.mgrBusy == true) {
  62. //        java.lang.System.out.println("Deferring load");
  63.         setTimeout(this.layerLoad, 100, layerLoadObject);
  64.     }
  65.     
  66.     this.mgrBusy = true;        // used to semaphore the adding to the queue
  67.     
  68.     if ((!this.lastLoad) || (this.lastLoad == null)) {
  69.         // if there's no tail, we assume there's also no head
  70.         
  71.         this.loading = layerLoadObject;
  72.         this.lastLoad = layerLoadObject;
  73.  
  74. //        java.lang.System.out.println("Scheduled a load");
  75.     
  76.         this.mgrBusy = false;
  77.  
  78.         this.startLoad();
  79.     } else {
  80.         // got a tail
  81.  
  82. //        java.lang.System.out.println("Added a load (previous last: " + this.lastLoad.layerObj.name);
  83.  
  84.         this.lastLoad.nextLoad = layerLoadObject;
  85. /*        if (this.lastLoad == null) {
  86.             java.lang.System.out.println("lastLoad was null");
  87.         } else {
  88.             java.lang.System.out.println("lastLoad item was " + this.lastLoad.layerObj);
  89.         }
  90. */
  91.         this.lastLoad = layerLoadObject;
  92.  
  93. /*        if (this.lastLoad == null) {
  94.             java.lang.System.out.println("lastLoad is null");
  95.         } else {
  96.             java.lang.System.out.println("lastLoad item is " + this.lastLoad.layerObj);
  97.         }
  98. */
  99.         this.mgrBusy = false;
  100.     }
  101.     
  102.     return; 
  103. }
  104.  
  105. // StartLoad gets called in order to begin loading of the page
  106. // currently at the head of the queue.
  107.  
  108. function LayerMgr_StartNextLoad()
  109. {
  110.     var loadObject = this.loading;
  111.     
  112.     if (loadObject && (loadObject != null)) {
  113.         // ok, so load this thing.
  114.         
  115.         // for sanity checking
  116.         if (this.isLoading == true) {
  117.             java.lang.System.out.println("Warning: another layer is already loading");
  118.         }
  119.         
  120.         this.isLoading = true;
  121.         
  122.         if (loadObject.layerObj == null) {
  123.             // a request to create a new layer
  124.             var newLayer = this.layerNew(200, loadObject.container);
  125.             
  126.             // if we also need to load a URL
  127.  
  128.             if (loadObject.layerURL != null) {
  129.                 window.layerManager.loading.layerObj = newLayer;;
  130.                 newLayer.visibility = "show";
  131.                 newLayer.src = loadObject.layerURL;
  132.                 if(loadObject.external == true) {
  133.                     newLayer.__parent__ = null;
  134.                     newLayer.initStandardObjects();
  135.                     newLayer.Event = Event;
  136.                 }
  137.             }
  138.                         
  139. /*            if (loadObject.layerURL != null) {
  140.                 // it's not null, so we'll queue a new request for
  141.                 // this layer.
  142.  
  143.                 var newLoadObj = new LayerLoadObject(newLayer,
  144.                     loadObject.layerURL, null, loadObject.notifier,
  145.                     loadObject.parameter, loadObject.external);
  146.                 
  147.                 loadObject.layerObj = newLayer;
  148.                 loadObject.notifier = LayerMgr_NewLayerAndLoad;
  149.                 loadObject.parameter = newLoadObj; */
  150.                 
  151. //                java.lang.System.out.println("Created a new layer named " + newLayer.name);
  152.  
  153. /*                this.layerLoad(newLoadObj);
  154.             }*/
  155.         } else {
  156.             // only do the load
  157.  
  158. //            java.lang.System.out.println("Loading " + loadObject.layerURL + " into an existing layer named " + loadObject.layerObj.name);
  159.  
  160.             loadObject.visibility = "show";
  161.             loadObject.layerObj.src = loadObject.layerURL;
  162.             if(loadObject.external == true) {
  163.                 loadObject.layerObj.__parent__ = null;
  164.                 loadObject.layerObj.initStandardObjects();
  165.                 loadObject.layerObj.Event = Event;
  166.             }
  167.         }
  168.  
  169.         this.isLoading = false;
  170.     }
  171.     
  172. }
  173.  
  174.  
  175. // CaptureLoad handles the task of synchronizing the load operations,
  176. // firing off notifications, and generally keeping things in order in
  177. // the layer manager.
  178.  
  179. function LayerMgr_CaptureLoad(e)
  180. {
  181.     // Ok, we've gotten something.  Let's figure out if it's what
  182.     // we're currently thinking we're loading
  183.     
  184.     var currentLoad = window.layerManager.loading;
  185.     
  186. // alert("currentLoad " + currentLoad);
  187.             
  188.     if (currentLoad && (currentLoad != null)) {
  189.         if (currentLoad.layerObj != e.target) {
  190.             // not the load we were waiting for; it's probably one of
  191.             // it's children.  In any event, abort and get out.
  192. //java.lang.System.out.println("Returning, wrong target; wanted: " + currentLoad.layerObj);            
  193. //java.lang.System.out.println("got: " + e.target);            
  194.  
  195.             if (e.target != window) {
  196. //java.lang.System.out.println("Returning, wrong target layer (expecting " + currentLoad.layerObj.name + "; got " + e.target.name);
  197.                 // in other words, we're loading a layer
  198.                 window.routeEvent(e);
  199.             } else {
  200.                 if (window.onLoadHandler && (window.onLoadHandler != null)) {
  201.                     window.onLoadHandler(e);
  202.                 }
  203.             }
  204.             return;
  205.         }
  206.         
  207. //java.lang.System.out.println("Got a load capture for " + e.target.name);
  208.  
  209.         // Ok, we've got the call, so do the notification
  210.         
  211.         if (currentLoad.notifier != null && currentLoad.notifier.layerLoaded) {
  212.             currentLoad.notifier.layerLoaded(e.target, currentLoad.parameter);
  213.         }    
  214.  
  215.         // And remove from the queue
  216.         
  217.         window.layerManager.loading = window.layerManager.loading.nextLoad;
  218.         if (window.layerManager.loading == null) {
  219. //            java.lang.System.out.println("No more items to process");
  220.             window.layerManager.lastLoad = null;
  221.         }
  222.         
  223.     } else {
  224.         // This is irregular.  We don't think we're loading something,
  225.         // but we apparently are...
  226.     }        
  227.  
  228.     if (e.target != window) {
  229.         // in other words, we're loading a layer
  230.         window.routeEvent(e);
  231.  
  232.     } else {
  233.         if (window.onLoadHandler && (window.onLoadHandler != null)) {
  234.             window.onLoadHandler(e);
  235.         }
  236.     }
  237.     
  238.     window.layerManager.startLoad();
  239.         
  240.  
  241. }
  242.  
  243. // Called to create a new layer or recycle one from a previously used space.
  244.  
  245. function LayerMgr_NewLayer(width, parent)
  246. {
  247.     var        returnLayer;
  248.     var     count;
  249.     
  250.     count = this.layerCount--;        // this must be an atomic operation
  251.     
  252.     if (count > 0) {
  253.         returnLayer = this.layerArray[count];
  254.     } else {
  255.         this.layerCount++;
  256.         returnLayer = new Layer(width, parent);
  257.     }
  258.  
  259.     return returnLayer;
  260. }
  261.  
  262. // Return a layer to the free store
  263.  
  264. function LayerMgr_DeleteLayer(thisLayer)
  265. {
  266.     if (thisLayer && thisLayer != null) {
  267.         thisLayer.src="about:blank";        // clear out any references
  268.  
  269.         var count = this.layerCount++;
  270.  
  271.         this.layerArray[count] = thisLayer;
  272.     }
  273.  
  274.     return;
  275. }
  276.  
  277. function LayerManager() {
  278.     this.layerCount = 0;
  279.     this.layerArray = new Array(5);
  280.     
  281.     this.layerDelete = LayerMgr_DeleteLayer;
  282.     this.layerNew = LayerMgr_NewLayer;
  283.  
  284.     this.loading = null;
  285.     this.lastLoad = null;
  286.     
  287.     this.layerLoad = LayerMgr_LayerLoad;
  288.     this.startLoad = LayerMgr_StartNextLoad;
  289.  
  290.     this.mgrBusy = false;
  291.     this.isLoading = false;
  292.  
  293.     //window.captureEvents(Event.LOAD);
  294.     //window.onload = LayerMgr_CaptureLoad;
  295.  
  296.     netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite"); 
  297.     window.enableExternalCapture();
  298.  
  299.     document.captureEvents(Event.LOAD);
  300.     document.onload = LayerMgr_CaptureLoad;
  301.  
  302.     window.layerManager = this;    
  303.     
  304. //    java.lang.System.out.println("Layer Manager Created");
  305. }
  306.  
  307. void(0);
  308.  
  309.