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