home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / CMN / handler.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  3.9 KB  |  105 lines

  1. //
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3. //
  4. //handler.js
  5. //
  6. //Correctly handle the getting, setting, and deleting of function calls
  7. //in event handlers
  8. //
  9. //--------------------------------------------------------------
  10. //
  11. //
  12. //getHandler(obj,eventName,fnName, optStr) 
  13. //setHandler(obj,eventName,fnCall, optStr) {
  14. //delHandler(obj,eventName,fnName, optStr) {
  15.  
  16.  
  17. //Returns a function call if exists in event handler.
  18. //  obj       - DOM object, such as dreamweaver.getDocumentDOM().body
  19. //  eventName - "onLoad", "onClick" etc (not case sensitive)
  20. //  fnName    - "MM_preloadImages" etc.
  21. //  optStr    - (optional) function call must contain this string to be found
  22. //Given <TAG onEvent="aaa();bbb();ccc()">,
  23. //calling getHandler(tagObj,'onEvent','bbb') will
  24. //return "bbb()". Returns empty if event or fn don't exist.
  25.  
  26. function getHandler(obj,eventName,fnName, optStr) {
  27.   var eventStr,fnArray,i,theChunk,retVal = "";
  28.   eventStr = obj.getAttribute(eventName);
  29.   if (eventStr) { //find previous call, or add it
  30.     fnArray = dreamweaver.getTokens(eventStr,";");
  31.     for (i=0; i<fnArray.length; i++) { //look at each code chunk
  32.       if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr ||  //fn call found
  33.           fnArray[i].indexOf(optStr) != -1)) {
  34.         retVal = fnArray[i]; break;
  35.     } }
  36.   }
  37.   return retVal
  38. }
  39.  
  40.  
  41.  
  42. //Replaces or adds a fn call to an event handler
  43. //  obj       - DOM object, such as dreamweaver.getDocumentDOM().body
  44. //  eventName - "onLoad", "onClick" etc (not case sensitive)
  45. //  fnCall    - "myFun('arg1','arg2')" etc.
  46. //  optStr    - (optional) function call must contain this string to be found
  47. //Given <TAG onEvent="aaa();bbb();ccc()">,
  48. //calling setHandler(tagObj,'onEvent','bbb(1,2)') will
  49. //replace "bbb()" with the altered fn call. If the event
  50. //does not exist, adds it. It fn didn't exist, adds it to the
  51. //end of the list.
  52.  
  53. function setHandler(obj,eventName,fnCall, optStr) {
  54.   var eventStr,fnName,fnArray=new Array(),i=0;
  55.   eventStr = obj.getAttribute(eventName);
  56.   if (eventStr) { //if event exists
  57.     fnName = fnCall.substring(0,fnCall.indexOf("("));
  58.     fnArray = dreamweaver.getTokens(eventStr,";");
  59.     for (i; i<fnArray.length; i++) //search for fnName
  60.       if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr ||  //fn call found
  61.           fnArray[i].indexOf(optStr) != -1)) break;
  62.   }
  63.   //if last item is "return value;", insert before that
  64.   if (fnArray.length>0 && i==fnArray.length && fnArray[i-1].toLowerCase().indexOf("return ")==0) {
  65.     fnArray[i] = fnArray[i-1]; //shift last value over one
  66.     i--; //set it to add new value in second-to-last position
  67.   }
  68.   fnArray[i] = fnCall; //adds fn to the end
  69.   obj.setAttribute(eventName,fnArray.join(";"));
  70.   return true
  71. }
  72.  
  73.  
  74.  
  75. //Deletes a fn call from an event handler
  76. //  obj       - DOM object, such as dreamweaver.getDocumentDOM().body
  77. //  eventName - "onLoad", "onClick" etc (not case sensitive)
  78. //  fnName    - "MM_preloadImages" etc.
  79. //  optStr    - (optional) function call must contain this string to be found
  80. //Given <TAG onEvent="aaa();bbb();ccc()">,
  81. //calling delHandler(tagObj,'onEvent','bbb') will
  82. //remove "bbb();". If it is the last fn in the handler,
  83. //removes the event entirely.
  84.  
  85. function delHandler(obj,eventName,fnName, optStr) {
  86.   var eventStr,fnArray=new Array(),i=0,j;
  87.   eventStr = obj.getAttribute(eventName);
  88.   if (eventStr) { //if event exists
  89.     fnArray = dreamweaver.getTokens(eventStr,";");
  90.     for (i; i<fnArray.length; i++) { //look at each code chunk
  91.       if (fnArray[i].indexOf(fnName+'(') != -1 && (!optStr ||  //fn call found
  92.           fnArray[i].indexOf(optStr) != -1)) { //and, if given, optStr exists
  93.         if (fnArray.length == 1) { //if last one, remove attribute
  94.           obj.removeAttribute(eventName);
  95.         } else { //pull out
  96.           for (j=i; j<fnArray.length; j++) fnArray[j] = fnArray[j+1]; //shift array
  97.           fnArray.length--;
  98.           obj.setAttribute(eventName,fnArray.join(';'));
  99.         }
  100.         break;
  101.     } }
  102.   }
  103.   return true
  104. }
  105.