home *** CD-ROM | disk | FTP | other *** search
/ internet.au CDrom 42 / NETCD42.iso / web / w95 / dream2.exe / data1.cab / Program_Files / Configuration / Behaviors / Actions / Preload Images.js < prev    next >
Encoding:
JavaScript  |  1998-11-30  |  10.9 KB  |  310 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBALS VARS *****************
  4.  
  5. //******************* BEHAVIOR FUNCTION **********************
  6.  
  7. //Preloads multiple images files in order. Accepts a variable number of args
  8. //(each should be quoted):
  9. //  imgURL   - an image filename, URL encoded. (ex: file.gif, http://www.x.com/y.gif)
  10. //
  11. //Creates a new array of Image objects. With each one, it assigns an image source
  12. //from the argument list. These are downloaded essentially simultaneously into the
  13. //client cache. When the user needs a new image file (for example: they go to the
  14. //next web page), the browser should quickly find this image in the cache.
  15. //!!! IMPORTANT !!! This function is also defined in file "Preload Images.htm".
  16. //Any edits must be made there as well as here.
  17.  
  18. function MM_preloadImages() { //v2.0
  19.   if (document.images) {
  20.     var imgFiles = MM_preloadImages.arguments;
  21.     if (document.preloadArray==null) document.preloadArray = new Array();
  22.     var i = document.preloadArray.length;
  23.     with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){
  24.       preloadArray[i] = new Image;
  25.       preloadArray[i++].src = imgFiles[j];
  26.   } }
  27. }
  28.  
  29.  
  30. //******************* API **********************
  31.  
  32.  
  33. //Always okay to use.
  34.  
  35. function canAcceptBehavior(){
  36.   return true;
  37. }
  38.  
  39.  
  40.  
  41. //Returns a Javascript function to be inserted in HTML head with script tags.
  42.  
  43. function behaviorFunction(){
  44.   return "MM_preloadImages";
  45. }
  46.  
  47.  
  48.  
  49. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  50. //Gets list of img filenames from the select list. Strips spaces off each name,
  51. //URL encodes them, wraps them in quotes, and returns them as a list of args.
  52.  
  53. function applyBehavior(uniqueName) {
  54.   var i,argList="",imgFileStr, imgFileArray;
  55.  
  56.   imgFileArray = document.imgList.get('all');
  57.   for (i=0;i<imgFileArray.length;i++) {  //step thru menu
  58.     imgFileStr = listControlStripSpaces(imgFileArray[i]);  //strip leading and following spaces
  59.     if (imgFileStr) {  //if any string exists
  60.       if (argList) argList += ",";  //if not the first one, add a comma
  61.       argList += "'"+escape(imgFileStr)+"'";  //concatenate string in quotes
  62.     }
  63.   }
  64.   if (document.preloadID) argList += ",'"+document.preloadID+"'";  //concatenate id in quotes
  65.  
  66.   if (argList) return "MM_preloadImages("+argList+")";  //return fn call with args
  67.   else         return MSG_NoImgNames;
  68. }
  69.  
  70.  
  71.  
  72. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  73. //call arguments. This information is used by DW to fixup behavior args when the
  74. //document is moved or changed.
  75. //
  76. //It is passed an actual function call string generated by applyBehavior(), which
  77. //may have a variable list of arguments, and this should return a matching mask.
  78. //
  79. //The return values are:
  80. //  URL     : argument could be a file path, which DW will update during Save As...
  81. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  82. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  83. //  other...: argument is ignored
  84.  
  85. function identifyBehaviorArguments(fnCallStr) {
  86.   var argList, argArray, i;
  87.  
  88.   argList = "";
  89.   argArray = extractArgs(fnCallStr);
  90.   for (i=1; i<argArray.length; i++) { //with each img URL
  91.     if (argArray[i].charAt(0)=="#")
  92.       argList += ((argList)?",":"")+"other";
  93.     else argList += ((argList)?",":"")+"DEP";
  94.   }
  95.   return argList;
  96. }
  97.  
  98.  
  99.  
  100. //Given the original function call, this parses out the args and updates
  101. //the UI. With each image filename, URL decodes it, and adds it to the list.
  102. //Loads the first item for editing.
  103.  
  104. function inspectBehavior(behFnCallStr){
  105.   var argArray,imgFileStr,i;
  106.  
  107.   argArray = extractArgs(behFnCallStr);//gets list of args (first is fn name, ignore)
  108.   if (argArray.length > 1) {
  109.     for (i=1; i<argArray.length; i++){ //with each img
  110.       imgFileStr = unescape(argArray[i]); //undo URL encoding
  111.       if (imgFileStr.charAt(0) != "#") //don't display special id code
  112.         document.imgList.append(imgFileStr); //add item
  113.       else document.preloadID = imgFileStr; //save ID
  114.     }
  115.     document.imgList.append(); //add new, blank item
  116.   }
  117. }
  118.  
  119.  
  120.  
  121. //***************** LOCAL FUNCTIONS  ******************
  122.  
  123.  
  124. //Clears the select menu "itemList" and selects the first item
  125.  
  126. function initializeUI(){
  127.   document.preloadID = "";
  128.   document.imgList = new listControl(document.theForm.itemList, document.theForm.itemEntry);
  129.   document.imgList.append(); //add new, blank item
  130. }
  131.  
  132.  
  133.  
  134. //Invokes dialog to allow user to select filename. Puts value in text input.
  135.  
  136. function browseFileAndSet(){
  137.   var fileName,imgSrcList="";
  138.   fileName = browseForFileURL("select", "", true);  //returns a local filename
  139.   if (fileName) {
  140.     document.theForm.itemEntry.value = fileName;
  141.     document.imgList.set(fileName);
  142.   }
  143. }
  144.  
  145.  
  146.  
  147. //***************** LIST CLASS FUNCTIONS  ******************
  148.  
  149. //classListControl.js
  150. //
  151. //This control consists of a SELECT control and an optional INPUT/TEXT.
  152. //If you provide an INPUT/TEXT, it will always keep it in sync w/ the selection.
  153. //
  154. //To define a new listControl, create a global variable and define it after onLoad:
  155. //  MYLIST = new listControl(document.theForm.theSelect);
  156. //
  157. //If you have a matching INPUT/TEXT, provide it as the 2nd argument:
  158. //  MYLIST = new listControl(document.theForm.theSelect, document.theForm.theText);
  159. //
  160. //Thereafter, you can call methods and get properties, for example:
  161. //  MYLIST.append('default');    MYLIST.get();   length = MYLIST.len;
  162. //
  163. //See properties and methods below:
  164.  
  165. function listControl(listObj,inputTextObj) {
  166.   // PROPERTIES
  167.   this.object     = listObj;           //SELECT object
  168.   this.input      = inputTextObj;      //INPUT/TEXT object (optional)
  169.   this.len        = 0;                 //current length of the list
  170.   this.index      = 0;                 //current selectedIndex
  171.  
  172.   // METHODS
  173.   this.append     = listControlAppend; // append()    //append a new blank line to end of list
  174.                                        // append('default')      //append default text
  175.                                        // append('default',true) //append default text and enumerate
  176.   this.del        = listControlDel;    // del()       //delete selected line
  177.   this.set        = listControlSet;    // set('text') //set current line to text
  178.   this.refresh    = listControlRefresh;// refresh()   //refresh text input (for SELECT's onChange)
  179.   this.get        = listControlGet;    // get()       //return current selection text
  180.                                        // get(n)      //return text item n (starts at zero)
  181.                                        // get('all')  //return array of all text items
  182. }
  183.  
  184.  
  185.  
  186. //Append a new, blank item to the end of the list.
  187. //If there is no selection, it replaces the first item.
  188.  
  189. function listControlAppend(defaultItem){
  190.   var i, retVal = false;
  191.   with (this.object) {
  192.     this.index = this.len; //should be after last line
  193.     while (this.index > 0 && (options[this.index-1].text == "")) this.index--; //find earliest blank line
  194.     if (!defaultItem) defaultItem = "";  //if no defaultItem, make it blank
  195.     options[this.index] = new Option(listControlStripSpaces(defaultItem));  //append new item
  196.     selectedIndex = this.index;  //select new item
  197.     this.len = options.length;
  198.     while (this.len > 0 && (options[this.len-1].text == "")) this.len--; //find earliest blank line
  199.     retVal = true;
  200.   }
  201.   if (this.input) with (this.input) {  //if there's a text input, update it
  202.     value = this.get();
  203.     focus();
  204.     select();
  205.   }
  206.   return retVal;
  207. }
  208.  
  209.  
  210.  
  211. //Deletes the currently selected item, and selects the one that followed it.
  212.  
  213. function listControlDel() {
  214.   var i, retVal = false;
  215.   with (this.object) {
  216.     this.index = selectedIndex; //get insertion point
  217.     if (this.index >= 0) {  //if there is a selection
  218.       lastIndex = this.len - 1;
  219.       for (i=this.index; i<lastIndex; i++)
  220.         options[i] = new Option(options[i+1].text); //shift items after deleted line
  221.       options[i].text = "";  //clear out extra line at end
  222.       selectedIndex = (this.index < lastIndex)? this.index : --this.index; //if deleted last item, move selection up one
  223.       this.len = options.length;
  224.       while (this.len > 0 && (options[this.len-1].text == "")) this.len--; //find last non-blank line
  225.       retVal = true;
  226.     }
  227.   }
  228.   if (this.input) with (this.input) {  //if there's a text input, update it
  229.     value = this.get();
  230.     focus();
  231.     select();
  232.   }
  233.   return retVal;
  234. }
  235.  
  236.  
  237.  
  238. //Replaces the list selection with the value of the text entry.
  239.  
  240. function listControlSet(itemStr) {
  241.   var retVal = false;
  242.   with (this.object) {
  243.     this.index = selectedIndex;
  244.     if (this.index > -1 && itemStr) {  // if something selected & non-empty
  245.       if (options[this.index].text != itemStr) {  //if text has been changed
  246.         if (this.index > this.len) this.index = this.len; //don't exceed list len
  247.         options[this.index].text = listControlStripSpaces(itemStr);  //replace text
  248.         selectedIndex = this.index; //reselect
  249.         this.len = options.length;  //recompute length by counting backward thru blank lines
  250.         while (this.len > 0 && (options[this.len-1].text == "")) this.len--; //find last non-blank line
  251.       }
  252.       retVal = true;
  253.   } }
  254.   return retVal;
  255. }
  256.  
  257.  
  258.  
  259. //Called when there's a new selection, loads and selects the text entry.
  260.  
  261. function listControlGet(index) {
  262.   var i, retVal = "";  //return blank if all else fails
  263.   with (this.object) {
  264.     if (!index) index = selectedIndex;  //if they don't pass a num, use index
  265.     if (index == "all") {
  266.       var retVal = new Array();
  267.       for (i=0; i<this.len; i++) retVal[i] = options[i].text;  //if 'all', return array
  268.     } else if (index > -1) {
  269.       retVal = options[index].text;
  270.   } }
  271.   return retVal;
  272. }
  273.  
  274.  
  275.  
  276. //Called when there's a new selection, loads and selects the text entry.
  277.  
  278. function listControlRefresh() {
  279.   var retVal = false;
  280.   with (this.object) {
  281.     this.index = selectedIndex;
  282.     if (this.index > -1) {  //if something selected
  283.       if (this.input) with (this.input) {  //if there's a text input, update it
  284.         value = this.get();
  285.         focus();
  286.         select();
  287.       }
  288.       retVal = true;
  289.   } }
  290.   return retVal;
  291. }
  292.  
  293.  
  294.  
  295. //Given a string, strips all leading and ending spaces and returns the result.
  296.  
  297. function listControlStripSpaces(theStr) {
  298.   if (!theStr) theStr = "";  //ensure its not null
  299.   var firstNonSpace = 0;
  300.   var lastNonSpace = theStr.length - 1;
  301.   while (theStr.charAt(firstNonSpace) == " " && firstNonSpace < theStr.length) firstNonSpace++;
  302.   while (theStr.charAt(lastNonSpace) == " " && lastNonSpace > 0) lastNonSpace--;
  303.   return theStr.substring(firstNonSpace, lastNonSpace+1);
  304. }
  305.  
  306. //**************** GENERIC FUNCTIONS ****************
  307.  
  308. //function extractArgs(behFnCallStr){
  309. //function unescQuotes(theStr){
  310.