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

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBAL VARS  *****************
  4.  
  5. function initGlobals() {
  6.   var x,y;
  7.  
  8.   TAGTYPES = new Array("LAYER","DIV","SPAN","IMG","FORM","INPUT/CHECKBOX","INPUT/RADIO",
  9.                          "INPUT/TEXT","TEXTAREA","INPUT/PASSWORD","SELECT");
  10.   MENUMAXLEN = 0;
  11.  
  12.   PROP = new Array();
  13.   //Create array for each tagname, which we can access with PROP[browser][tag]
  14.   for (x=0; x<BROWSERS.length; x++) {
  15.     PROP[x] = new Array();
  16.     for (y=0; y<TAGNAMES.length; y++) PROP[x][y] = new Array();
  17.   }
  18.  
  19.   //This table provides a dynamic way add properties to the picklist. There are are 4
  20.   //groups, one for each browser/version (see BROWSERS array above).
  21.   //You can add writeable properties, comma-separated, between the first pair of quotes.
  22.   //To add another tag, extend the TAGNAMES and TAGTYPES arrays above, then add another
  23.   //array element to each group below.
  24.  
  25.   //Netscape 3 Writable Properties
  26.   PROP[0][ 0] = "".split(",");//layer
  27.   PROP[0][ 1] = "".split(",");//div
  28.   PROP[0][ 2] = "".split(",");//span
  29.   PROP[0][ 3] = "src".split(",");//image
  30.   PROP[0][ 4] = "action".split(",");//form
  31.   PROP[0][ 5] = "checked".split(",");//checkbox
  32.   PROP[0][ 6] = "checked".split(",");//radio
  33.   PROP[0][ 7] = "value".split(",");//text
  34.   PROP[0][ 8] = "value".split(",");//textarea
  35.   PROP[0][ 9] = "value".split(",");//password
  36.   PROP[0][10] = "selectedIndex".split(",");//select
  37.  
  38.   //IE 3 Writable Properties
  39.   PROP[1][ 0] = "".split(",");//layer
  40.   PROP[1][ 1] = "".split(",");//div
  41.   PROP[1][ 2] = "".split(",");//span
  42.   PROP[1][ 3] = "".split(",");//image
  43.   PROP[1][ 4] = "action".split(",");//form
  44.   PROP[1][ 5] = "checked".split(",");//checkbox
  45.   PROP[1][ 6] = "checked".split(",");//radio
  46.   PROP[1][ 7] = "value".split(",");//text
  47.   PROP[1][ 8] = "value".split(",");//textarea
  48.   PROP[1][ 9] = "value".split(",");//password
  49.   PROP[1][10] = "selectedIndex".split(",");//select
  50.  
  51.   //Netscape 4 Writable Properties
  52.   PROP[2][ 0] = "top,left,zIndex,clip,visibility,document.bgColor,document.background".split(",");//layer
  53.   PROP[2][ 1] = "".split(",");//div
  54.   PROP[2][ 2] = "".split(",");//span
  55.   PROP[2][ 3] = "src".split(",");//image
  56.   PROP[2][ 4] = "action".split(",");//form
  57.   PROP[2][ 5] = "checked".split(",");//checkbox
  58.   PROP[2][ 6] = "checked".split(",");//radio
  59.   PROP[2][ 7] = "value".split(",");//text
  60.   PROP[2][ 8] = "value".split(",");//textarea
  61.   PROP[2][ 9] = "value".split(",");//password
  62.   PROP[2][10] = "selectedIndex".split(",");//select
  63.  
  64.   //IE 4 Writable Properties
  65.   PROP[3][ 0] = ("style.top,style.left,style.width,style.height,style.zIndex,style.clip,style.visibility,"+
  66.                  "style.backgroundColor,style.backgroundImage,style.filter").split(",");//layer
  67.   PROP[3][ 1] = ("style.fontFamily,style.fontStyle,style.fontWeight,style.fontSize,"+
  68.                 "style.borderStyle,style.borderWidth,style.borderColor,style.backgroundColor,"+
  69.                 "style.backgroundImage,style.filter,innerHTML,innerText").split(",");//div
  70.   PROP[3][ 2] = ("style.fontFamily,style.fontStyle,style.fontWeight,style.fontSize,"+
  71.                 "style.borderStyle,style.borderWidth,style.borderColor,style.backgroundColor,"+
  72.                 "style.backgroundImage,style.filter,innerHTML,innerText").split(",");//span
  73.   PROP[3][ 3] = "src".split(",");//image
  74.   PROP[3][ 4] = "action".split(",");//form
  75.   PROP[3][ 5] = "checked".split(",");//checkbox
  76.   PROP[3][ 6] = "checked".split(",");//radio
  77.   PROP[3][ 7] = "value".split(",");//text
  78.   PROP[3][ 8] = "value".split(",");//textarea
  79.   PROP[3][ 9] = "value".split(",");//password
  80.   PROP[3][10] = "selectedIndex".split(",");//select
  81. }
  82.  
  83. var TAGTYPES;
  84. var MENUMAXLEN;
  85. var PROP;
  86.  
  87.  
  88. //******************* BEHAVIOR FUNCTION **********************
  89.  
  90. //Sets a property of an object to a new value.
  91. //Accepts the following arguments:
  92. //  objStrNS - Javascript object ref for Netscape (ex: document.layers['foo'].document.myImage)
  93. //  objStrIE - JScript object reference for Internet Explorer (ex: document.all['myImage'])
  94. //  theProp  - the property to change (ex: value, style.fontFace)
  95. //  theValue - the new value (ex: sans-serif)
  96. //
  97. //Tests for browser, and uses the first object string for NS, the second for IE
  98. //The next test is to prevent errors in < 4.0 browsers if the object's in a layer.
  99. //The last test is to prevent errors if the browser doesn't support the object or the property.
  100. //If the object still exists, and either it's not a style property, or the style prop is okay:
  101. //  evals the whole equation: objStr.theProp = theValue
  102. //I would've used theObj[theProp] = theValue, but it doesn't work for style.property.
  103.  
  104. function MM_changeProp(objStrNS,objStrIE,theProp,theValue) { //v2.0
  105.   var NS = (navigator.appName == 'Netscape');
  106.   var objStr = (NS)?objStrNS:objStrIE;
  107.   if (( NS && (objStr.indexOf('document.layers[')!=0 || document.layers!=null)) ||
  108.       (!NS && (objStr.indexOf('document.all[')   !=0 || document.all   !=null))) {
  109.     var obj = eval(objStr);
  110.     if ((obj != null) && (theProp.indexOf("style.") != 0 || obj.style != null)) {
  111.       eval(objStr+'.'+theProp + '="'+theValue+'"');
  112.   } }
  113. }
  114.  
  115.  
  116. //******************* API **********************
  117.  
  118.  
  119. //Can be used with any tag and any event
  120.  
  121. function canAcceptBehavior(){
  122.   return true;
  123. }
  124.  
  125.  
  126.  
  127. //Returns a Javascript function to be inserted in HTML head with script tags.
  128.  
  129. function behaviorFunction(){
  130.   return "MM_changeProp";
  131. }
  132.  
  133.  
  134.  
  135. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  136. //Gets values from the UI and adds them as params to the function call.
  137.  
  138. function applyBehavior() {
  139.   var menuIndex,theObjNS,theProp="",theValue,theTag;
  140.  
  141.   //read all values from UI
  142.   menuIndex = document.theForm.menu.selectedIndex; //get index selected
  143.   if (menuIndex != -1 && document.theForm.menu.options[menuIndex].text.indexOf("*")!=0) {
  144.     theObjNS   = escQuotes(document.MM_NS_REFS[menuIndex]);
  145.     theObjIE   = escQuotes(document.MM_IE_REFS[menuIndex]);
  146.     theValue = escQuotes(document.theForm.theValue.value);
  147.     menuIndex = document.theForm.typeOfObj.selectedIndex; //get index selected
  148.     theTag   = document.theForm.typeOfObj.options[menuIndex].text;
  149.  
  150.     if (document.theForm.theRadio[0].checked) { //get property from menu
  151.       theProp = document.theForm.propMenu.options[document.theForm.propMenu.selectedIndex].text;
  152.     } else { //get from textfield
  153.       theProp  = document.theForm.theProp.value;
  154.     }
  155.  
  156.     if (theObjNS.indexOf(REF_UNNAMED) == 0)  //if unnamed reference
  157.       return MSG_UnnamedObj;
  158.   }
  159.  
  160.   if (!theProp) {
  161.     return MSG_NoSelection;
  162.   } else {
  163.     return "MM_changeProp('"+theObjNS+"','"+theObjIE+"','"+theProp+"','"+theValue+"','"+theTag+"')"; //fn call w/args
  164.   }
  165. }
  166.  
  167.  
  168.  
  169. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  170. //call arguments. This information is used by DW to fixup behavior args when the
  171. //document is moved or changed.
  172. //
  173. //It is passed an actual function call string generated by applyBehavior(), which
  174. //may have a variable list of arguments, and this should return a matching mask.
  175. //
  176. //The return values are:
  177. //  URL     : argument could be a file path, which DW will update during Save As...
  178. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  179. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  180. //  other...: argument is ignored (I add a descriptive word for future generations)
  181.  
  182. function identifyBehaviorArguments(fnCallStr) {
  183.   var argArray;
  184.  
  185.   argArray = extractArgs(fnCallStr);
  186.   if (argArray.length == 6) {
  187.     return "NS4.0ref,IE4.0ref,other,other,other";
  188.   } else {
  189.     return "";
  190.   }
  191. }
  192.  
  193.  
  194.  
  195. //Given the original function call, this parses out the args and updates
  196. //the UI.
  197.  
  198. function inspectBehavior(fnCallStr){
  199.   var i,found,numTags,numObjs;
  200.   var theObjNS, theProp, theValue, theTag;
  201.   var argArray = new Array;
  202.  
  203.   //get previous args
  204.   argArray = extractArgs(fnCallStr);
  205.   if (argArray.length > 5) {
  206.     theObjNS = unescQuotes(argArray[1]);
  207.     theProp = argArray[3];
  208.     theValue = unescQuotes(argArray[4]);
  209.     theTag = argArray[5];
  210.  
  211.     //select tag in tag list
  212.     found = false;
  213.     numTags = document.theForm.typeOfObj.options.length;
  214.     for (i=0; i<numTags; i++)  //check if theTag is in menu
  215.       if (document.theForm.typeOfObj.options[i].text == theTag) { //if theTag there
  216.         document.theForm.typeOfObj.selectedIndex = i;
  217.         found = true;
  218.         break;
  219.       }
  220.     if (!found) alert(errMsg(MSG_TagNotFound,theTag));
  221.     else {
  222.       loadAllMenus();  //simulate tag being selected
  223.  
  224.       //select obj in menu
  225.       found = false;
  226.       numObjs = document.MM_NS_REFS.length;
  227.       for (i=0; i<numObjs; i++)  //check if theObjNS is in menu
  228.         if (document.MM_NS_REFS[i] == theObjNS) { //if theObjNS there
  229.           document.theForm.menu.selectedIndex = i;
  230.           found = true;
  231.           break;
  232.         }
  233.       if (!found) alert(errMsg(MSG_ObjNotFound,theObjNS));
  234.       else {
  235.         document.theForm.theProp.value = theProp;
  236.         selectRadio(1);
  237.         document.theForm.theValue.value = theValue;
  238.       }
  239.     }
  240.  
  241.   }
  242. }
  243.  
  244.  
  245.  
  246. //***************** LOCAL FUNCTIONS  ******************
  247.  
  248.  
  249. //Load the typeOfObj menu with tag names, the browser menu,
  250. //and initialize the object menu.
  251.  
  252. function initializeUI(){
  253.   initGlobals();
  254.   var listLen=eval(TAGTYPES.length); //get array length
  255.  
  256.   //load TAGS picklist
  257.   for (var i=0;i<listLen;i++)
  258.     document.theForm.typeOfObj.options[i]=new Option(TAGTYPES[i]); //load menu
  259.  
  260.   //load browser picklist
  261.   for (i=0;i<BROWSERS.length;i++)
  262.     document.theForm.browserMenu.options[i]=new Option(BROWSERS[i]); //load menu
  263.   document.theForm.browserMenu.selectedIndex = DEFAULT_BROWSER;
  264.  
  265.   document.theForm.propMenu.options[0]=new Option("");
  266.  
  267.   document.theForm.menu.options[0]=new Option("*** "+MENUITEM_NoTypeSelected+" **");
  268.   document.theForm.menu.mylength = 1;  //WORKAROUND! create the length property for main menu
  269.   document.theForm.menu.selectedIndex = 0;
  270.  
  271.   document.theForm.theValue.focus(); //set focus on textbox
  272.   document.theForm.theValue.select(); //set insertion point into textbox
  273. }
  274.  
  275.  
  276.  
  277. //Loads the new objects, and a list of usefule properties.
  278.  
  279. function loadAllMenus() {
  280.   loadObjectMenu();
  281.   loadPropMenu();
  282. }
  283.  
  284.  
  285.  
  286. //Loads a list of useful properties from the data array PROP.
  287.  
  288. function loadPropMenu() {
  289.   var tagIndex, brIndex, i;
  290.  
  291.   //get browser selection from browserMenu
  292.   brIndex = document.theForm.browserMenu.selectedIndex;
  293.  
  294.   //get tag selection from typeOfObj
  295.   tagIndex = document.theForm.typeOfObj.selectedIndex;
  296.  
  297.   if (brIndex > -1 && tagIndex > -1) {
  298.  
  299.     //Can't set menu length, so must erase old values
  300.     for (i=0; i<MENUMAXLEN; i++)
  301.       document.theForm.propMenu.options[i]=new Option("");
  302.  
  303.     //add each property to list
  304.     for (i=0; i<PROP[brIndex][tagIndex].length; i++)
  305.       document.theForm.propMenu.options[i]=new Option(PROP[brIndex][tagIndex][i]);
  306.  
  307.     document.theForm.propMenu.selectedIndex = 0;
  308.     MENUMAXLEN = Math.max(MENUMAXLEN,i);
  309.   }
  310. }
  311.  
  312.  
  313.  
  314. //Load the select menu with object references.
  315.  
  316. function loadObjectMenu() {
  317.   var nameArray = new Array;
  318.   var i,menuLen,tagIndex,tagStr,listLen,niceNameSrcArray;
  319.  
  320.   //put up a temporary msg while their waiting  (not currently seen)
  321.   document.theForm.menu.options[0]=new Option("*** "+MENUITEM_Searching+" ***"); //temporary msg
  322.   document.theForm.menu.selectedIndex = 0; //reselect the menu item
  323.  
  324.   //Clear out old menu (WORKAROUND! Must maintain length field)
  325.   menuLen = document.theForm.menu.mylength;  //get previous length
  326.   for (i=1; i<menuLen; i++) document.theForm.menu.options[i]=new Option(""); //clear each item
  327.   menuLen = 0; //menu now "zero" length
  328.  
  329.   //get list of objects
  330.   tagIndex = document.theForm.typeOfObj.selectedIndex;
  331.   tagStr = document.theForm.typeOfObj.options[tagIndex].text;
  332.   document.MM_NS_REFS = getAllObjectRefs("NS 4.0",tagStr); //store parallel NS refs
  333.   document.MM_IE_REFS = getAllObjectRefs("IE 4.0",tagStr); //store parallel IE refs
  334.   niceNameSrcArray = document.MM_NS_REFS;
  335.  
  336.   //Search for unreferenceable objects. <DIV id="foo"> is IE only, <LAYER> is NS only.
  337.   //if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
  338.   for (i=0; i<document.MM_NS_REFS.length; i++) {
  339.     if (document.MM_IE_REFS[i].indexOf(REF_CANNOT) == 0) {
  340.       document.MM_IE_REFS[i] = ""; //blank it out
  341.     }
  342.     if (document.MM_NS_REFS[i].indexOf(REF_CANNOT) == 0) {
  343.       document.MM_NS_REFS[i] = ""; //blank it out
  344.       niceNameSrcArray = document.MM_IE_REFS; //use the IE list
  345.     }
  346.   }
  347.   nameArray = niceNames(niceNameSrcArray,TAGNAMES[tagIndex]);
  348.  
  349.   //load menu with object names
  350.   if (nameArray.length == 0) {  //if nothing to display...
  351.     document.theForm.menu.options[0]=new Option("*** "+errMsg(MENUITEM_ItemsNotFnd,tagStr)+" ***"); //clear menu
  352.   } else { //something there, load the menu
  353.     listLen = nameArray.length; //get array length
  354.     for (menuLen=0; menuLen<listLen; menuLen++)
  355.       document.theForm.menu.options[menuLen]=new Option(nameArray[menuLen]); //load menu
  356.   }
  357.   document.theForm.menu.mylength = menuLen;  //WORKAROUND! re-store the length property
  358.  
  359.   document.theForm.typeOfObj.selectedIndex = tagIndex; //reselect the menu item
  360. }
  361.  
  362.  
  363.  
  364. //Passed a number, selects that radio.
  365.  
  366. function selectRadio(num) {
  367.   document.theForm.theRadio[0].checked = (num==0)?true:false;
  368.   document.theForm.theRadio[1].checked = (num==1)?true:false;
  369. }
  370.  
  371.  
  372.  
  373. //**************** GENERIC FUNCTIONS ****************
  374.  
  375. //function extractArgs(upStr){
  376. //function escQuotes(theStr){
  377. //function unescQuotes(theStr){
  378. //function niceNames(objRefArray,objTypeStr) {
  379. //function nameReduce (objName) {
  380. //function errMsg() {
  381.