home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Preload Images.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  9.6 KB  |  289 lines

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