PageRenderTime 1305ms CodeModel.GetById 1301ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/source/Plug-in/xajax/xajax_core/plugin_layer/support/xajaxEvent.inc.php

http://prosporous.googlecode.com/
PHP | 162 lines | 52 code | 19 blank | 91 comment | 4 complexity | bec2ae476c0e41b1eb39be936d79d84d MD5 | raw file
  1<?php
  2
  3/*
  4	File: xajaxEvent.inc.php
  5
  6	Definition of the xajax Event object.
  7
  8	Title: xajaxEvent
  9
 10	Please see <copyright.inc.php> for a detailed description, copyright
 11	and license information.
 12*/
 13
 14/*
 15	@package xajax
 16	@version $Id: xajaxEvent.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
 17	@copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
 18	@license http://www.xajaxproject.org/bsd_license.txt BSD License
 19*/
 20
 21// require_once is necessary here as the function plugin also includes this
 22//SkipAIO
 23require_once dirname(__FILE__) . '/xajaxUserFunction.inc.php';
 24//EndSkipAIO
 25
 26/*
 27	Class: xajaxEvent
 28	
 29	A container class which holds a reference to handler functions and configuration
 30	options associated with a registered event.
 31*/
 32class xajaxEvent
 33{
 34	/*
 35		String: sName
 36		
 37		The name of the event.
 38	*/
 39	var $sName;
 40	
 41	/*
 42		Array: aConfiguration
 43		
 44		Configuration / call options to be used when initiating a xajax request
 45		to trigger this event.
 46	*/
 47	var $aConfiguration;
 48	
 49	/*
 50		Array: aHandlers
 51		
 52		A list of <xajaxUserFunction> objects associated with this registered
 53		event.  Each of these functions will be called when the event is triggered.
 54	*/
 55	var $aHandlers;
 56	
 57	/*
 58		Function: xajaxEvent
 59		
 60		Construct and initialize this <xajaxEvent> object.
 61	*/
 62	function xajaxEvent($sName)
 63	{
 64		$this->sName = $sName;
 65		$this->aConfiguration = array();
 66		$this->aHandlers = array();
 67	}
 68	
 69	/*
 70		Function: getName
 71		
 72		Returns the name of the event.
 73		
 74		Returns:
 75		
 76		string - the name of the event.
 77	*/
 78	function getName()
 79	{
 80		return $this->sName;
 81	}
 82	
 83	/*
 84		Function: configure
 85		
 86		Sets/stores configuration options that will be used when generating
 87		the client script that is sent to the browser.
 88	*/
 89	function configure($sName, $mValue)
 90	{
 91		$this->aConfiguration[$sName] = $mValue;
 92	}
 93	
 94	/*
 95		Function: addHandler
 96		
 97		Adds a <xajaxUserFunction> object to the list of handlers that will
 98		be fired when the event is triggered.
 99	*/
100	function addHandler(&$xuf)
101	{
102		$this->aHandlers[] =& $xuf;
103	}
104	
105	/*
106		Function: generateRequest
107		
108		Generates a <xajaxRequest> object that corresponds to the
109		event so that the client script can easily invoke this event.
110		
111		sXajaxPrefix - (string):  The prefix that will be prepended to
112			the client script stub function associated with this event.
113			
114		sEventPrefix - (string):  The prefix prepended to the client script
115			function stub and <xajaxRequest> script.
116	*/
117	function generateRequest($sXajaxPrefix, $sEventPrefix)
118	{
119		$sEvent = $this->sName;
120		return new xajaxRequest("{$sXajaxPrefix}{$sEventPrefix}{$sEvent}");
121	}
122 	
123 	/*
124 		Function: generateClientScript
125 		
126 		Generates a block of javascript code that declares a stub function
127 		that can be used to easily trigger the event from the browser.
128 	*/
129 	function generateClientScript($sXajaxPrefix, $sEventPrefix)
130	{
131		$sMode = '';
132		$sMethod = '';
133		
134		if (isset($this->aConfiguration['mode']))
135			$sMode = $this->aConfiguration['mode'];
136			
137		if (isset($this->aConfiguration['method']))
138			$sMethod = $this->aConfiguration['method'];
139			
140		if (0 < strlen($sMode))
141			$sMode = ", mode: '{$sMode}'";
142		
143		if (0 < strlen($sMethod))
144			$sMethod = ", method: '{$sMethod}'";
145		
146		$sEvent = $this->sName;
147		echo "{$sXajaxPrefix}{$sEventPrefix}{$sEvent} = function() { return xajax.request( { xjxevt: '{$sEvent}' }, { parameters: arguments{$sMode}{$sMethod} } ); };\n";
148	}
149	
150	/*
151		Function: fire
152		
153		Called by the <xajaxEventPlugin> when the event has been triggered.
154	*/
155	function fire($aArgs)
156	{
157		$objResponseManager =& xajaxResponseManager::getInstance();
158		
159		foreach (array_keys($this->aHandlers) as $sKey)
160			$this->aHandlers[$sKey]->call($aArgs);
161	}
162}