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

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //******************* GLOBALS **********************
  4.  
  5. var helpDoc = MM.HELP_behPlaySound;
  6.  
  7. var MM_sndFiles = new Array();
  8. var MM_sndNames = new Array();
  9.  
  10. //******************* BEHAVIOR FUNCTION **********************
  11.  
  12. //Plays a soundfile, or stops one from playing.
  13. //Accepts the following arguments:
  14. //  sndAction - currently, 'stop' or 'play'
  15. //  sndObj    - Javascript object reference for sound EMBED (ex: document.mySound)
  16. //
  17. //Tests if the object exists, then initiates the action on the object.
  18. //The play() method is called for Netscape; for IE, either play() or run() is called depending
  19. //on the plugin: the new Windows Media Player uses the new Netshow 2 API and
  20. //consequently requires play().  The old plugin uses the DirectShow API and
  21. //requires the run() method after ensuring that the FileName property is
  22. //assigned. If all else fails, plays sound as link (may popup a play window).
  23.  
  24. function MM_controlSound(x, _sndObj, sndFile) { //v3.0
  25.   var i, method = "", sndObj = eval(_sndObj);
  26.   if (sndObj != null) {
  27.     if (navigator.appName == 'Netscape') method = "play";
  28.     else {
  29.       if (window.MM_WMP == null) {
  30.         window.MM_WMP = false;
  31.         for(i in sndObj) if (i == "ActiveMovie") {
  32.           window.MM_WMP = true; break;
  33.       } }
  34.       if (window.MM_WMP) method = "play";
  35.       else if (sndObj.FileName) method = "run";
  36.   } }
  37.   if (method) eval(_sndObj+"."+method+"()");
  38.   else window.location = sndFile;
  39. }
  40.  
  41. document.VERSION_MM_controlSound = 3.0; //define latest version number for behavior inspector
  42.  
  43. //******************* API **********************
  44.  
  45.  
  46. //Can be used with any tag and any event
  47.  
  48. function canAcceptBehavior(){
  49.   return true;
  50. }
  51.  
  52.  
  53.  
  54. //Returns a Javascript function to be inserted in HTML head with script tags.
  55.  
  56. function behaviorFunction(){
  57.   return "MM_controlSound";
  58. }
  59.  
  60.  
  61.  
  62. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  63.  
  64. function applyBehavior() {
  65.   var i, sndFile="", sndName="";
  66.  
  67.   sndFile = document.theForm.sndFileObj.value; // get filename from text input
  68.  
  69.  //If snd EMBED already exists, we want to return the old sndName
  70.   for (i=0; i<MM_sndFiles.length; i++) { //scan existing embeds
  71.     if (MM_sndFiles[i] == sndFile) { //if already existing
  72.       sndName = MM_sndNames[i]; break;//get prior uniquename
  73.   } }
  74.  
  75.   if (!sndName) { //sound didn't exist, create it
  76.     sndName = "CS"+((new Date()).getTime()); //create unique name
  77.     createSoundObject(sndName,sndFile);
  78.   }
  79.  
  80.   //return function call or error message
  81.   if (sndFile) {
  82.     updateBehaviorFns("MM_controlSound");
  83.     return "MM_controlSound('play','document."+sndName+"','"+dw.doURLEncoding(sndFile)+"')"
  84.   }
  85.   else return MSG_NoSndFile;
  86. }
  87.  
  88.  
  89. //Passed the function call above, takes prior arguments and reloads the UI.
  90.  
  91. function inspectBehavior(fnCallStr) {
  92.   var sndFile, argArray = new Array;
  93.  
  94.   //get previous args
  95.   argArray = extractArgs(fnCallStr);
  96.   if (argArray.length >3) {
  97.     sndFile = argArray[3];
  98.     document.sndFileObj.value = unescape(sndFile); //show name
  99. } }
  100.  
  101.  
  102.  
  103. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  104. //call arguments. This information is used by DW to fixup behavior args when the
  105. //document is moved or changed.
  106. //
  107. //It is passed an actual function call string generated by applyBehavior(), which
  108. //may have a variable list of arguments, and this should return a matching mask.
  109. //
  110. //The return values are:
  111. //  other...: argument is ignored
  112. //  dep     : argument is a dependent file path, should be included in uploads
  113. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  114.  
  115. function identifyBehaviorArguments(fnCallStr) {
  116.   var argArray;
  117.  
  118.   argArray = extractArgs(fnCallStr);
  119.   if (argArray.length == 4) {
  120.     return "other,NS4.0ref,dep";
  121.   } else {
  122.     return "";
  123.   }
  124. }
  125.  
  126.  
  127.  
  128. //Given the original function call, this parses out the args and maybe
  129. //removes associated sound embed calls. Only if:
  130. //1) there's an embed with the same name
  131. //2) when scanning the entire doc, name only found once
  132.  
  133. function deleteBehavior(fnCallStr) {
  134.   var argArray,sndName,doc,tagArray,i,embedName;
  135.  
  136.   argArray = extractArgs(fnCallStr);
  137.   if (argArray.length > 3) {
  138.     sndName = dreamweaver.getTokens(argArray[2],".")[1]; //remove "document.", use unique name
  139.     //Find all EMBED calls that we created (name starts with "CS"),
  140.     doc = dreamweaver.getDocumentDOM("document"); //get all
  141.     tagArray = doc.getElementsByTagName("EMBED");
  142.     for (i=0; i<tagArray.length; i++) {  //with each EMBED tag
  143.       embedName = tagArray[i].name;
  144.       if (embedName == sndName) { //if same embed
  145.         if (numOccurences(sndName)<2){ // and embed ref'd no where else
  146.            tagArray[i].outerHTML = ""; //delete the embed
  147.            break;
  148.   } } } }
  149. }
  150.  
  151.  
  152.  
  153. //***************** LOCAL FUNCTIONS  ******************
  154.  
  155.  
  156. //Finds any previous sound EMBEDs and adds them to the Stop Sound menu.
  157.  
  158. function initializeUI() {
  159.   var i,j,tagArray,embedName,embedSrc;
  160.  
  161.   //Find all EMBED calls that we created (name starts with "CS"), add to the global list.
  162.   tagArray = dreamweaver.getDocumentDOM("document").getElementsByTagName("EMBED");
  163.   for (i=0; i<tagArray.length; i++) {  //with each EMBED tag
  164.     embedName = tagArray[i].name;
  165.     embedSrc = unescape(tagArray[i].src);
  166.     if (embedName && embedSrc) {
  167.        for (j=0; j<MM_sndFiles.length && MM_sndFiles[j] !=embedSrc; j++); //search
  168.        if (j == MM_sndFiles.length) { //if matc not found
  169.            MM_sndNames.push(embedName);  //save sound name for later
  170.            MM_sndFiles.push(embedSrc); //append to the array
  171.   } } }
  172.   document.theForm.sndFileObj.focus(); //set focus on textbox
  173.   document.theForm.sndFileObj.select(); //set insertion point into textbox
  174. }
  175.  
  176. //Creates a sound embed object, given a name and file. This object is appended to the
  177. //end of the user's document (the end of the BODY tag).
  178.  
  179. function createSoundObject(sndName,sndFile) {
  180.   var objStr;
  181.   objStr = "<EMBED NAME=\'"+sndName+"\' SRC=\'"+dw.doURLEncoding(sndFile)+"\' LOOP=false \n"+
  182.            "AUTOSTART=false MASTERSOUND HIDDEN=true WIDTH=0 HEIGHT=0></EMBED>\n";
  183.   bodyObj = dreamweaver.getDocumentDOM("document").body; //get body
  184.   bodyObj.innerHTML = bodyObj.innerHTML + objStr; //append new embed
  185. }
  186.  
  187.  
  188. //Called by Attain to silently update behavior calls
  189. //Returns new call if ok, otherwise returns empty string
  190.  
  191. function reapplyBehavior(oldBehaviorCall) {
  192.   var newBehaviorCall = "";
  193.   var behName = "MM_controlSound";
  194.  
  195.   document.SILENT_MODE = true;
  196.   initializeUI();
  197.   inspectBehavior(oldBehaviorCall);
  198.   newBehaviorCall = applyBehavior();
  199.   if (newBehaviorCall.indexOf(behName) == -1) newBehaviorCall=""; //if not fn call, return ""
  200.   document.SILENT_MODE = false;
  201.  
  202.   return newBehaviorCall;
  203. }
  204.  
  205. //**************** GENERIC FUNCTIONS ****************
  206.  
  207. function numOccurences(theStr) {
  208.   var src, doc, retVal = 0;
  209.   var pat = new RegExp(theStr, "g");
  210.  
  211.   doc = dreamweaver.getDocumentDOM("document"); //get all
  212.   src = doc.body.outerHTML;
  213.   retVal = src.match(pat).length;
  214.  
  215.   return retVal;
  216. }
  217.