1: <?php
2: /*
3: File: xajaxEvent.inc.php
4:
5: Definition of the xajax Event object.
6:
7: Title: xajaxEvent
8:
9: Please see <copyright.inc.php> for a detailed description, copyright
10: and license information.
11: */
12:
13: /*
14: @package xajax
15: @version $Id: xajaxEvent.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
16: @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
17: @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, 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
23: require_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: */
32: final class xajaxEvent
33: {
34: /*
35: String: sName
36:
37: The name of the event.
38: */
39: private $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: private $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: private $aHandlers;
56:
57: /*
58: Function: xajaxEvent
59:
60: Construct and initialize this <xajaxEvent> object.
61: */
62: public function __construct($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: public 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: public 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: public 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: public 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: public 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: public 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: }
163: