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

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2. //******************* GLOBALS **********************
  3.  
  4. function initGlobals() {
  5.   ACTION_PLAY = "play";
  6.   ACTION_STOP = "stop";
  7. }
  8.  
  9. var ACTION_PLAY;
  10. var ACTION_STOP;
  11.  
  12.  
  13. //******************* BEHAVIOR FUNCTION **********************
  14.  
  15. //Plays a soundfile, or stops one from playing.
  16. //Accepts the following arguments:
  17. //  sndAction - currently, 'stop' or 'play'
  18. //  sndObj    - Javascript object reference for sound EMBED (ex: document.mySound)
  19. //
  20. //Tests if the object exists, then initiates the action on the object.
  21. //For stop, the stop() method is called.  For play(), the play() method
  22. //is called for Netscape; for IE, either play() or run() is called depending
  23. //on the plugin: the new Windows Media Player uses the new Netshow 2 API and
  24. //consequently requires play().  The old plugin uses the DirectShow API and
  25. //requires the run() method after ensuring that the FileName property is 
  26. //assigned.
  27.  
  28. function MM_controlSound(sndAction,_sndObj) { //v2.0
  29.   var sndObj = eval( _sndObj );
  30.   if (sndObj != null) {
  31.     if (sndAction=='stop') {
  32.       sndObj.stop();
  33.     } else {
  34.       if (navigator.appName == 'Netscape' ) {
  35.          sndObj.play();
  36.       } else {
  37.          if (document.MM_WMP_DETECTED == null) {
  38.             document.MM_WMP_DETECTED = false;
  39.             var i;
  40.             for( i in sndObj )
  41.                if ( i == "ActiveMovie" ) {
  42.                   document.MM_WMP_DETECTED = true;
  43.                   break; }
  44.          }
  45.          if (document.MM_WMP_DETECTED)
  46.             sndObj.play();
  47.          else if ( sndObj.FileName )
  48.             sndObj.run();
  49. }}}}
  50.  
  51. //******************* API **********************
  52.  
  53.  
  54. //Can be used with any tag and any event
  55.  
  56. function canAcceptBehavior(){
  57.   return true;
  58. }
  59.  
  60.  
  61.  
  62. //Returns a Javascript function to be inserted in HTML head with script tags.
  63.  
  64. function behaviorFunction(){
  65.   return "MM_controlSound";
  66. }
  67.  
  68.  
  69.  
  70. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  71.  
  72. function applyBehavior() {
  73.   var i, sndAction, menuIndex, sndFile="", sndName="";
  74.  
  75.   //Based on action (Play or Stop) get the sound filename from the INPUT or SELECT
  76.   sndAction = (document.theForm.theRadio[0].checked)?ACTION_PLAY:ACTION_STOP;
  77.   if (sndAction == ACTION_PLAY) {
  78.     sndFile = document.theForm.sndFileObj.value; //if play, get filename from text input
  79.   } else { //ACTION_STOP
  80.     menuIndex = document.theForm.menu.selectedIndex; //get index selected
  81.     if (menuIndex != -1 && document.theForm.menu.options[menuIndex].text.indexOf("***")!=0)
  82.       sndFile = document.theForm.menu.options[menuIndex].text; //if stop get from menu
  83.   }
  84.  
  85.   //If snd EMBED already exists, we want to return the old sndName
  86.   for (i=0; i<document.theForm.menu.options.length; i++) { //scan menu for name
  87.     if (document.theForm.menu.options[i].text == sndFile) { //if already existing
  88.       sndName = document.MM_sndNames[i]; break;//get prior uniquename
  89.   } }
  90.   
  91.   if (!sndName || document.NEW_SOUND) { //sound didn't exist, create it
  92.     if (!sndName) sndName = "CS"+((new Date()).getTime()); //create unique name
  93.     createSoundObject(sndName,sndFile);
  94.   }
  95.  
  96.   //return function call or error message
  97.   if (sndFile) return "MM_controlSound('"+sndAction+"','document."+sndName+"','"+sndFile+"')";
  98.   else return MSG_NoSndFile;
  99. }
  100.  
  101.  
  102.  
  103. //Passed the function call above, takes prior arguments and reloads the UI.
  104.  
  105. function inspectBehavior(fnCallStr) {
  106.   var i, sndAction, sndName, menuLength;
  107.   var tempArray, argArray = new Array;
  108.  
  109.   //get previous args
  110.   argArray = extractArgs(fnCallStr);
  111.   if (argArray.length == 4) {
  112.     sndAction = argArray[1];
  113.     sndName = dreamweaver.getTokens(argArray[2],".")[1]; //remove "document.", use unique name
  114.     sndFile = argArray[3];
  115.  
  116.     with (document.theForm) {
  117.       theRadio[sndAction==ACTION_PLAY?0:1].checked = true; //select radio
  118.       //try and locate the sound object name in the list of known sound objects
  119.       for (i=0; i<document.MM_sndNames.length && document.MM_sndNames[i]!=sndName; i++);
  120.       if (i < document.MM_sndNames.length) { //snd found, select it
  121.         if (sndAction == ACTION_PLAY) sndFileObj.value = menu.options[i].text; //PLAY: show name
  122.         else menu.selectedIndex = i;  //STOP: select sound
  123.       } else {
  124.         if (sndAction == ACTION_PLAY) sndFileObj.value = sndFile; //PLAY: show name
  125.         else { //didn't exists, but add to menu and applyBehavior will create it
  126.           menuLength = document.MM_sndNames.length;
  127.           document.MM_sndNames[menuLength] = sndName;  //add to list
  128.           menu.options[menuLength]= new Option(sndFile); //add to menu
  129.           menu.selectedIndex = Math.max(0,menuLength - 1);  //select new menu item
  130.           document.NEW_SOUND = true;
  131.   } } } }
  132. }
  133.  
  134.  
  135.  
  136. //Given the original function call, this parses out the args and maybe
  137. //removes associated sound embed calls. Only if:
  138. //1) there's an embed with the same name
  139. //2) when scanning the entire doc, name only found once
  140.  
  141. function deleteBehavior(fnCallStr) {
  142.   var argArray,sndName,doc,tagArray,i,embedName;
  143.  
  144.   argArray = extractArgs(fnCallStr);
  145.   if (argArray.length > 2) {
  146.     sndName = dreamweaver.getTokens(argArray[2],".")[1]; //remove "document.", use unique name
  147.     //Find all EMBED calls that we created (name starts with "CS"), add to menu
  148.     doc = dreamweaver.getDocumentDOM("document"); //get all
  149.     tagArray = doc.getElementsByTagName("EMBED");
  150.     for (i=0; i<tagArray.length; i++) {  //with each EMBED tag
  151.       embedName = tagArray[i].name;
  152.       if (embedName == sndName) { //if same embed
  153.         if ( -1 == doc.body.outerHTML.indexOf( argArray[2] ) ) // and embed ref'd no where else
  154.            tagArray[i].outerHTML = "";
  155.         break;
  156.   } } }
  157. }
  158.  
  159.  
  160.  
  161. //***************** LOCAL FUNCTIONS  ******************
  162.  
  163.  
  164. //Finds any previous sound EMBEDs and adds them to the Stop Sound menu.
  165.  
  166. function initializeUI() {
  167.   initGlobals();
  168.   var i,j,tagArray,embedName,embedSrc;
  169.   document.MM_sndNames = new Array;
  170.  
  171.   //Find all EMBED calls that we created (name starts with "CS"), add to menu
  172.   tagArray = dreamweaver.getDocumentDOM("document").getElementsByTagName("EMBED");
  173.   for (i=0; i<tagArray.length; i++) {  //with each EMBED tag
  174.     embedName = tagArray[i].name;
  175.     embedSrc = unescape(tagArray[i].src);
  176.     if (embedName && embedName.indexOf("CS") == 0 && embedSrc) with (document) { //if one of ours
  177.       for (j=0; j<MM_sndNames.length && embedSrc != theForm.menu.options[j].text; j++); //search
  178.       if (j == MM_sndNames.length) { //if not there
  179.         MM_sndNames[j] = embedName;  //save sound name for later
  180.         theForm.menu.options[j]= new Option(embedSrc); //add to menu
  181.   } } }
  182.  
  183.   document.theForm.sndFileObj.focus(); //set focus on textbox
  184.   document.theForm.sndFileObj.select(); //set insertion point into textbox
  185. }
  186.  
  187.  
  188.  
  189. //Creates a sound embed object, given a name and file. This object is appended to the
  190. //end of the user's document (the end of the BODY tag).
  191.  
  192. function createSoundObject(sndName,sndFile) {
  193.   var objStr;
  194.   objStr = "<EMBED NAME=\'"+sndName+"\' SRC=\'"+escape(sndFile)+"\' LOOP=false \n"+
  195.            "AUTOSTART=false MASTERSOUND HIDDEN=true WIDTH=0 HEIGHT=0></EMBED>\n";
  196.   bodyObj = dreamweaver.getDocumentDOM("document").body; //get body
  197.   bodyObj.innerHTML = bodyObj.innerHTML + objStr; //append new embed
  198. }
  199.  
  200.  
  201.  
  202. //**************** GENERIC FUNCTIONS ****************
  203.  
  204. //function browseFile(fieldToStoreURL){
  205.