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

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //****************** Globals *******************
  4.  
  5. var helpDoc = MM.HELP_behCheckPlugin;
  6.  
  7. var PLUGIN_NAMES;
  8. var VBScriptCodename;
  9.  
  10. function initGlobals() {
  11.   PLUGIN_NAMES = new Array("Flash","Shockwave","LiveAudio", //nice names
  12.                            "Netscape Media Player","QuickTime");
  13.   PLUGIN_VALUES= new Array("Shockwave Flash","Shockwave for Director","LiveAudio",         //internal names
  14.                            "Netscape Media Player","QuickTime Plug-In");
  15.   VBScriptCodename = "Used by MM_checkPlugin";
  16. }
  17.  
  18.  
  19. //******************* BEHAVIOR FUNCTION **********************
  20.  
  21. //Sends the browser to one URL if the plugin exists, otherwise
  22. //sends the browser to an alternate URL.
  23. //The function accepts the following arguments:
  24. //  plugin  - the *exact* name of the plugin as registered (ex: Shockwave Flash)
  25. //  theURL  - optional URL, often a filename, URL encoded. (ex: file.htm, http://www.x.com/y.htm)
  26. //  altURL  - required URL, often a filename, URL encoded. (ex: file.htm, http://www.x.com/y.htm)
  27. //  autoGo  - boolean. If true, always goes to theURL if plugin detection is not possible
  28. //
  29. //Logic:
  30. //  if not Microsoft browser, plugin is there if it exists in the plugins array
  31. //  otherwise, if not Windows 3.1 (which doesn't support plugins)
  32. //    if seeking flash or director plugins and can detect with VBscript, 
  33. //      then ok iff VB script says ActiveX control exists.
  34. //    otherwise can't detect, so go if the autoGo flag is set.
  35. //  Also, only goes somewhere if the URLs are there.
  36.  
  37. function MM_checkPlugin(plgIn, theURL, altURL, autoGo) { //v3.0
  38.   var ok=false; document.MM_returnValue = false;
  39.   with (navigator) if (appName.indexOf('Microsoft')==-1) ok=(plugins && plugins[plgIn]);
  40.   else if (appVersion.indexOf('3.1')==-1) { //not Netscape or Win3.1
  41.     if (plgIn.indexOf("Flash")!=-1 && window.MM_flash!=null) ok=window.MM_flash;
  42.     else if (plgIn.indexOf("Director")!=-1 && window.MM_dir!=null) ok=window.MM_dir;
  43.     else ok=autoGo; }
  44.   if (!ok) theURL=altURL; if (theURL) window.location=theURL;
  45. }
  46.  
  47. document.VERSION_MM_checkPlugin = 3.0; //define latest version number for behavior inspector
  48.  
  49. //******************* API **********************
  50.  
  51.  
  52. //Can be used with any tag and any event
  53.  
  54. function canAcceptBehavior(){
  55.   return true;
  56. }
  57.  
  58.  
  59.  
  60. //Returns a Javascript function to be inserted in HTML head with script tags.
  61.  
  62. function behaviorFunction() {
  63.   return "MM_checkPlugin";
  64. }
  65.  
  66.  
  67.  
  68. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  69. //Calls escQuotes to find embedded quotes and precede them with \
  70. //Calls dw.doURLEncoding to encode URLs
  71.  
  72. function applyBehavior() {
  73.   var pluginName, theURL, altURL, autoGo, theMenu;
  74.  
  75.   if (document.theForm.theRadio[0].checked) { //get URL from textfield or menu
  76.     pluginName = document.theForm.menu.options[document.theForm.menu.selectedIndex].value;
  77.   } else {
  78.     pluginName = escQuotes(document.theForm.pluginName.value);
  79.   }
  80.  
  81.   theURL = dw.doURLEncoding(document.theForm.theURL.value);  //URL encode
  82.   altURL = dw.doURLEncoding(document.theForm.altURL.value);  //URL encode
  83.   autoGo = document.theForm.IEGoesToURL.checked;
  84.   if (pluginName && altURL) {
  85.     if (!autoGo && (pluginName.indexOf("Flash")!=-1 || pluginName.indexOf("Director"))!=-1) {
  86.       addShockwaveVBscript();
  87.     }
  88.     updateBehaviorFns("MM_checkPlugin");
  89.     return "MM_checkPlugin('"+pluginName+"','"+theURL+"','"+altURL+"',"+autoGo+")";
  90.   } else {
  91.     return MSG_NoPluginOrURL;
  92.   }
  93. }
  94.  
  95.  
  96.  
  97. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  98. //call arguments. This information is used by DW to fixup behavior args when the
  99. //document is moved or changed.
  100. //
  101. //It is passed an actual function call string generated by applyBehavior(), which
  102. //may have a variable list of arguments, and this should return a matching mask.
  103. //
  104. //The return values are:
  105. //  URL     : argument could be a file path, which DW will update during Save As...
  106. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  107. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  108. //  other...: argument is ignored
  109.  
  110. function identifyBehaviorArguments(fnCallStr) {
  111.   var argArray;
  112.  
  113.   argArray = extractArgs(fnCallStr);
  114.   if (argArray.length == 5) {
  115.     return "other,URL,URL,other";
  116.   } else {
  117.     return "";
  118.   }
  119. }
  120.  
  121.  
  122.  
  123. //Passed the function call above, takes prior arguments and reloads the UI.
  124. //Removes any escape characters "\"
  125.  
  126. function inspectBehavior(argStr){
  127.   var i, plugIn, inMenu;
  128.   var argArray = extractArgs(argStr);
  129.  
  130.   if (argArray.length == 5) {
  131.  
  132.     plugIn = unescQuotes(argArray[1]);
  133.     document.theForm.theURL.value = unescape(argArray[2]);  //URL decode
  134.     document.theForm.altURL.value = unescape(argArray[3]);  //URL decode
  135.     document.theForm.IEGoesToURL.checked = eval(argArray[4]);
  136.  
  137.     inMenu = false;
  138.     for (i=0; i<document.theForm.menu.options.length; i++) { //check if exists in menu
  139.       if (document.theForm.menu.options[i].value == plugIn) {
  140.         document.theForm.menu.selectedIndex = i;
  141.         inMenu = true;
  142.         break;
  143.       }
  144.     }
  145.     if (!inMenu) {
  146.       document.theForm.pluginName.value = plugIn;
  147.       setRadio(1); //set radio
  148.     }
  149.   }
  150. }
  151.  
  152.  
  153. // Deletes shockwave VB script if there are no references to "Shockwave " (note the space),
  154. // which occurs in MM_checkPlugin() calls that refer to these plugins.
  155.  
  156. function deleteBehavior(fnCallStr) {
  157.   initGlobals();
  158.   var dom = dw.getDocumentDOM();
  159.   if (dom && dom.documentElement.innerHTML.indexOf("Shockwave ")==-1) {
  160.     delShockwaveVBscript();
  161.   }
  162. }
  163.  
  164.  
  165. //**************** LOCAL FUNCTIONS ****************
  166.  
  167.  
  168. //Loads a preset list of some plugin names.
  169.  
  170. function initializeUI() {
  171.   initGlobals();
  172.   for (i=0; i<PLUGIN_NAMES.length; i++) {
  173.     document.theForm.menu.options[i] = new Option(PLUGIN_NAMES[i]);
  174.     document.theForm.menu.options[i].value = PLUGIN_VALUES[i];
  175.   }
  176.  
  177.   document.theForm.theURL.focus(); //set focus on textbox
  178.   document.theForm.theURL.select(); //set insertion point into textbox
  179. }
  180.  
  181.  
  182.  
  183. //Passed a number, selects that radio.
  184.  
  185. function setRadio(num) {
  186.   document.theForm.theRadio[0].checked = (num==0)?true:false;
  187.   document.theForm.theRadio[1].checked = (num==1)?true:false;
  188. }
  189.  
  190.  
  191.  
  192. //Adds simple VB script for checking shockwave plugins in IE browsers
  193. //Adds below close body tag if not there already.
  194. //Note the unique script name will be used for deletion.
  195.  
  196. function addShockwaveVBscript() {
  197.   var dom = dw.getDocumentDOM();
  198.   if (dom && dom.documentElement.innerHTML.indexOf(VBScriptCodename)==-1) {//if not already on page, add it
  199.     dom.body.outerHTML += "\n"+
  200. "<script name=\"" + VBScriptCodename + "\" language=\"javascript\">\n"+
  201. "<!"+"--\n"+
  202. "with (navigator) if (appName.indexOf('Microsoft')!=-1 && appVersion.indexOf('Mac')==-1) document.write(''+\n"+
  203. "'<scr'+'ipt language=\"VBScript\">\\nOn error resume next\\n'+\n"+
  204. "'MM_dir = (IsObject(CreateObject(\"SWCtl.SWCtl.1\")) Or IsObject(CreateObject(\"Macromedia.ActiveShockwave.1\")))\\n'+\n"+
  205. "'MM_flash = NOT IsNull(CreateObject(\"ShockwaveFlash.ShockwaveFlash\"))\\n</scr'+'ipt>');\n"+
  206. "//--"+">\n"+
  207. "</scr"+"ipt>\n";
  208.   }
  209. }
  210.  
  211.  
  212. //Deletes shockwave VB script (must have code name).
  213.  
  214. function delShockwaveVBscript() {
  215.   var i, allScripts, scriptName, dom = dw.getDocumentDOM();
  216.   if (dom) {
  217.     allScripts = dom.getElementsByTagName("SCRIPT");
  218.     if (allScripts) {
  219.       for (i=0; i<allScripts.length; i++) {
  220.         scriptName = allScripts[i].getAttribute("name");
  221.         if (scriptName && scriptName == VBScriptCodename) {
  222.           allScripts[i].outerHTML = "";
  223.           break
  224.   } } } }
  225. }
  226.  
  227.  
  228. //Called by Attain to silently update behavior calls
  229. //Returns new call if ok, otherwise returns empty string
  230.  
  231. function reapplyBehavior(oldBehaviorCall) {
  232.   var newBehaviorCall = "";
  233.   var behName = "MM_checkPlugin";
  234.  
  235.   document.SILENT_MODE = true;
  236.   initializeUI();
  237.   inspectBehavior(oldBehaviorCall);
  238.   newBehaviorCall = applyBehavior();
  239.   if (newBehaviorCall.indexOf(behName) == -1) newBehaviorCall=""; //if not fn call, return ""
  240.   document.SILENT_MODE = false;
  241.  
  242.   return newBehaviorCall;
  243. }
  244.