eventManager.htc


<!-- ---------------------------------------------------------------------
//
//  Copyright 2001 Chordiant Software.  All Rights Reserved.
//
//  File:         eventmanager.htc
//
//	Auther:				Ryan Carroll
//	Created:			13/11/2001
//
//  Description:  Implements a manager registering and dispatching events
//
//	Changes log:
//-------------------------------------------------------------------- -->

<PUBLIC:COMPONENT tagname="eventmanager" lightWeight="true">



<!-- Public Properties -->    

<!-- Public Methods -->    
<METHOD   NAME="registerEvents"   	 	/>
<METHOD   NAME="dispatch"    		  	/>
<METHOD   NAME="unregisterWindow"		/>
<METHOD   NAME="unregisterEvent"    	/>


<!-- Public Events -->    
<EVENT    NAME="onError"        ID="error"		  />

    
<!-- Private Event Listeners -->
<ATTACH   EVENT="ondocumentready" HANDLER="setDefaults" />
// { start - performance fix
<ATTACH   EVENT="ondetach"            HANDLER="cleanUp"       />
// } end - performance fix

</PUBLIC:COMPONENT>


<SCRIPT LANGUAGE="jscript">

//+----------------------------------------------------------------------------
//
//  Global Variables
//
//-----------------------------------------------------------------------------
var registeredEvents = new Array();

function registeredEventObject(sEventName)
{
	this.eventName = sEventName;
	this.eventHandlers = new Array();
}

function registeredEventHandlerObject(sEventHandlerFunction, oWindow){
	this.eventHandlerFunction = sEventHandlerFunction;
	this.window = oWindow;
}


////////////////////////////////////////////////////////////////////////////////
//	Methods Section																										        // 
////////////////////////////////////////////////////////////////////////////////

//+----------------------------------------------------------------------------
//
//  Public Methods
//
//-----------------------------------------------------------------------------

//+----------------------------------------------------------------------------
//
//  Function:       registerEvents
//
//  Description:    
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function registerEvents(oEventRegistration, oWindow) {
	var oEvents;
	var oRegisterEventNodeList;
	var sEventName;
	var sEventHandler;
	var sEventHandlerScriptFile;
	var oSelectedRegisterEventNode;
	var oRegisterEventNodeList;
	var oNewEventScriptFile;

	try {
		oEvents = new ActiveXObject("Msxml.DOMDocument");
    	if((typeof oEventRegistration) == "string") {
    		oEvents.loadXML(oEventRegistration);
    	} else {
    		oEvents.loadXML(oEventRegistration.xml);
    	}
    
    	oRegisterEventNodeList = oEvents.selectNodes(".//registerevent");
    	for (var i=0; i < oRegisterEventNodeList.length; i++) {
    		oSelectedRegisterEventNode = oRegisterEventNodeList.item(i);
    		if(oSelectedRegisterEventNode.selectSingleNode("eventname") != null){
    			sEventName = oSelectedRegisterEventNode.selectSingleNode("eventname").text;
    		}
    		if(oSelectedRegisterEventNode.selectSingleNode("eventhandlerfunction") != null){
    			sEventHandler = oSelectedRegisterEventNode.selectSingleNode("eventhandlerfunction").text;
    		}
    		if(oSelectedRegisterEventNode.selectSingleNode("eventhandlerscriptfilename") != null){
    			sEventHandlerScriptFile = oSelectedRegisterEventNode.selectSingleNode("eventhandlerscriptfilename").text;
    		}
    		
    		if( sEventHandlerScriptFile != null && sEventHandlerScriptFile != "") {
    			oNewEventScriptFile = oWindow.document.createElement("<script language=javascript src='"+sEventHandlerScriptFile+"'><\/script>")
    			oWindow.document.body.appendChild(oNewEventScriptFile);
    		}
    		
    		addRegisteredEvent(sEventName, sEventHandler, oWindow);
        }
	} catch(e) {
	  	returnError(e.toString());
	} finally {
	  	oEventRegistration = null;
		oWindow = null;
		oEvents = null;
		oRegisterEventNodeList = null;
		sEventName = null;
		sEventHandler = null;
		sEventHandlerScriptFile = null;
		oSelectedRegisterEventNode = null;
		oRegisterEventNodeList = null;
		oNewEventScriptFile = null;
	}			
}


//+----------------------------------------------------------------------------
//
//  Function:       unregisterWindow
//
//  Description:    
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function unregisterWindow( oWindow ) {
	var oRegisteredEvent;

	try {
    	for(var i=0; i < registeredEvents.length; i++){
    		oRegisteredEvent = registeredEvents[i];
    		for(var j=oRegisteredEvent.eventHandlers.length - 1; j >= 0; j--){
    			if(oRegisteredEvent.eventHandlers[j].window == oWindow){
    				oRegisteredEvent.eventHandlers[j]=null;
    				oRegisteredEvent.eventHandlers.splice(j,1);
    			}
    		}
    	}
	} catch(e) {
	  	returnError(e.toString());
	} finally {
	  	oRegisteredEvent = null;
		oWindow = null;
	}			
}


//+----------------------------------------------------------------------------
//
//  Function:       unregisterEvent
//
//  Description:    
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function unregisterEvent( sEventName, oWindow ) {
	var oRegisteredEvent;
	
	try {
    	for(var i=0; i < registeredEvents.length; i++){
    		if(registeredEvents[i].eventName == sEventName){
        		oRegisteredEvent = registeredEvents[i];
        		for(var j=oRegisteredEvent.eventHandlers.length - 1; j >= 0; j--){
        			if(oRegisteredEvent.eventHandlers[j].window == oWindow){
        				oRegisteredEvent.eventHandlers.splice(j,1);
        			}
        		}
    			break;
    		}
    	}
	} catch(e) {
	  	returnError(e.toString());
	} finally {
	  	oRegisteredEvent = null;
		sEventName = null;
		oWindow = null;
	}		
}


//+----------------------------------------------------------------------------
//
//  Function:       dispatch
//
//  Description:    
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function dispatch( sEventName, oEventData ) {
	var oRegisteredEvent;
	var sEventHandlerFunction;
	var oEventHandlerWindow;
	
	try {	
    	for(var i=0; i < registeredEvents.length; i++){
    		if(registeredEvents[i].eventName == sEventName){
    			oRegisteredEvent = registeredEvents[i];
    			break;
    		}
    	}
    
    	if(oRegisteredEvent != null){
    		for(var i=0; i < oRegisteredEvent.eventHandlers.length; i++){
    			oEventHandlerWindow = oRegisteredEvent.eventHandlers[i].window;
    			sEventHandlerFunction = oRegisteredEvent.eventHandlers[i].eventHandlerFunction;
    			if(oEventHandlerWindow != null && sEventHandlerFunction != null){
    				try {
		    			oEventHandlerWindow.eval(sEventHandlerFunction)(oEventData);
				} catch(ex)
				{
					//don't do anything, handling windows that no longer exist but were not properly deregistered
				}
			}
    		}
    	}
	} catch(e) {
	  	returnError(e.toString());
	} finally {
	  	oRegisteredEvent = null;
		sEventHandlerFunction = null;
		oEventHandlerWindow = null;
		sEventName = null;
		oEventData = null;
	}		
}


//+----------------------------------------------------------------------------
//
//  Private Methods
//
//-----------------------------------------------------------------------------

//+----------------------------------------------------------------------------
//
//  Function:       addRegisteredEvent
//
//  Description:    
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function addRegisteredEvent(sEventName, sEventHandlerFunction, oWindow){
	var oRegisteredEvent;
	var oEventHandler;
	
	try {
    	//create new event handler object
    	oEventHandler = new registeredEventHandlerObject(sEventHandlerFunction, oWindow);
    	
    	//search the registeredEvents collection to see if the event has already been registered
    	for(var i=0; i < registeredEvents.length; i++){
    		if(registeredEvents[i].eventName == sEventName){
    			oRegisteredEvent = registeredEvents[i];
    			break;
    		}
    	}
    	
    	if(oRegisteredEvent == null){
    		oRegisteredEvent = new registeredEventObject(sEventName);
    		registeredEvents[registeredEvents.length] = oRegisteredEvent;
    	} 
    
    	for(var i=0; i < oRegisteredEvent.eventHandlers.length; i++){
    		oSelectedEventHanlder = oRegisteredEvent.eventHandlers[i];
    		if(oSelectedEventHanlder.eventHandlerFunction == oEventHandler.eventHandlerFunction && oSelectedEventHanlder.window == oEventHandler.window ){
    			return;
    		}
    	}
    
    	oRegisteredEvent.eventHandlers[oRegisteredEvent.eventHandlers.length] = oEventHandler;
	} catch(e) {
	  	returnError(e.toString());
	} finally {
	  	oRegisteredEvent = null;
		oEventHandler = null;
		sEventName = null;
		sEventHandlerFunction = null;
		oWindow = null;
	}		
}


//+----------------------------------------------------------------------------
//
//  Function:       setDefaults
//
//  Description:    Called during the initialization of the behavior.  Sets
//                  the required default settings, and attaches the 
//                  onpropertychange event (not done in the header to prevent 
//                  firing the event during initialization).
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function setDefaults()
{ 
	if(element.document.media != "print") {
		//  Attach the onpropertychange event
		attachEvent("onpropertychange", doPropChange);
	}

	// { start - performance fix
		window.attachEvent("onbeforeunload", cleanUp);
	// } end - performance fix
}

// { start - performance fix
function cleanUp() {
  try {
        if(element.document.media != "print") {
		detachEvent("onpropertychange", doPropChange);
	}
	
	if (window)
		window.detachEvent("onbeforeunload", cleanUp);

  } catch(e) {
      returnError(e.toString());
  } finally {
    // Make sure all module variables are freed
    registeredEvents = null;
  }
}
// } end - performance fix

//+----------------------------------------------------------------------------
//
//  Function:       doPropChange
//
//  Description:    Runs on the onpropertychange event and calls the necessary
//                  functions to correctly handle the property that was just
//                  changed.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function doPropChange()
{
    var propertyName;
	
	try {
		propertyName = window.event.propertyName;
        //  Handle CSS property changes by calling necessary functions, setting
        //  variables, and/or setting styles
        //
        if (propertyName.substring(0,5) == "style")
        {
    	   // Handle Style changes
           // switch(propertyName)
           // {
           //     case "style.zIndex"             :
           //         break;                
           // }
        }
        else
        {
            //
            //  Detach the onpropertychange event to prevent it from firing while
            //  the changes are handled
            //
            detachEvent("onpropertychange", doPropChange);
            
            // Handle Property changes
            switch(propertyName)
            {
                //case "leftBoundary" :
                //		leftBoundary = parseInt(leftBoundary);
                //		setLeftBoundary(leftBoundary);
                //    break;
                  						    
                default :
                    returnError(propertyName + " not a valid property");
                    break;
            }
            
            //  Re-attach the onpropertychange event
            attachEvent("onpropertychange", doPropChange);
        }
	} catch(e) {
	  	returnError(e.toString());
	} finally {
	  	propertyName = null;
	}		
}


//+----------------------------------------------------------------------------
//
//  Function:       returnError
//
//  Description:    Fires the error event, along with a descriptive text
//                  message.
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function returnError(sMsg)
{
	var oEvent;
 	try {
        oEvent = createEventObject();
        oEvent.setAttribute("error", sMsg);
        error.fire(oEvent);
    	//alert(sMsg);
	} finally {
	  	oEvent = null;
		sMsg = null;
	}
}


</SCRIPT>

페이지 참조 및 등록:

<IMPORT namespace="chordiant" implementation="/.../eventManager.htc" />

<xml id="CaseSearchResultListEventHandlers">
	<events>
		<registerevent>
			<eventname>StopDisplayMessageReactivateHandler</eventname>
			<eventhandlerfunction>stopDisplayMessageHandlerReactivate</eventhandlerfunction>
		</registerevent>		
	</events>
</xml>	
function stopDisplayMessageHandlerReactivate(){
     do something here.
}
  :
desktop.eventManager.registerEvents(CaseSearchResultListEventHandlers, window );

top.advisorDesktop.eventManager.dispatch("StopDisplayMessageReactivateHandler", obj);

좋은 웹페이지 즐겨찾기