Source for file HTML.class.php

Documentation is available at HTML.class.php

  1. <?php
  2. /**
  3. * NAJAX HTML Class file.
  4. *
  5. * <p>This file defines the {@link NAJAX_HTML} Class.</p>
  6. * <p>Example:</p>
  7. * <code>
  8. * <?php
  9. *
  10. * class Update
  11. * {
  12. * function ui()
  13. * {
  14. * sleep(1);
  15. *
  16. * $content =& NAJAX_HTML::getElementById('content');
  17. *
  18. * $content->innerHTML = 'Hello World! How are you?';
  19. * }
  20. * }
  21. *
  22. * define('NAJAX_AUTOHANDLE', true);
  23. *
  24. * require_once('najax.php');
  25. *
  26. * ?>
  27. * <?= NAJAX_Utilities::header('.') ?>
  28. *
  29. * <div id="content">Hello!</div>
  30. *
  31. * <script type="text/javascript">
  32. *
  33. * var obj = <?= NAJAX_Client::register(new Update()) ?>;
  34. *
  35. * obj.ui(najax.asyncCall);
  36. *
  37. * </script>
  38. * </code>
  39. *
  40. * @author Stanimir Angeloff
  41. *
  42. * @package NAJAX
  43. *
  44. * @subpackage NAJAX_HTML
  45. *
  46. * @version 0.4.1.0
  47. *
  48. */
  49.  
  50. /**
  51. * Defines the string that is replaced with the current value
  52. * of the field.
  53. *
  54. * <p>Example:</p>
  55. * <code>
  56. * <?php
  57. *
  58. * class Update
  59. * {
  60. * function ui()
  61. * {
  62. * sleep(1);
  63. *
  64. * $content =& NAJAX_HTML::getElementById('content');
  65. *
  66. * $content->innerHTML = NAJAX_HTML_CURRENT_VALUE . ' How are you?';
  67. * }
  68. * }
  69. *
  70. * define('NAJAX_AUTOHANDLE', true);
  71. *
  72. * require_once('najax.php');
  73. *
  74. * ?>
  75. * <?= NAJAX_Utilities::header('.') ?>
  76. * <div id="content">Hello!</div>
  77. * <script type="text/javascript">
  78. *
  79. * var obj = <?= NAJAX_Client::register(new Update()) ?>;
  80. *
  81. * obj.ui(najax.asyncCall);
  82. *
  83. * </script>
  84. * </code>
  85. */
  86. define('NAJAX_HTML_CURRENT_VALUE', '<![najaxHtml:currentValue[');
  87.  
  88. /**
  89. * @global Contains all NAJAX_HTML DOM objects.
  90. */
  91. $GLOBALS['_NAJAX_HTML_DATA'] = array();
  92.  
  93. /**
  94. * NAJAX HTML Class.
  95. *
  96. * <p>This class is used to update the content and the
  97. * style of a page.</p>
  98. * <p>Example:</p>
  99. * <code>
  100. * <?php
  101. *
  102. * class Update
  103. * {
  104. * function ui()
  105. * {
  106. * sleep(1);
  107. *
  108. * $content =& NAJAX_HTML::getElementById('content');
  109. *
  110. * $content->innerHTML = 'Hello World! How are you?';
  111. * }
  112. * }
  113. *
  114. * define('NAJAX_AUTOHANDLE', true);
  115. *
  116. * require_once('najax.php');
  117. *
  118. * ?>
  119. * <?= NAJAX_Utilities::header('.') ?>
  120. *
  121. * <div id="content">Hello!</div>
  122. *
  123. * <script type="text/javascript">
  124. *
  125. * var obj = <?= NAJAX_Client::register(new Update()) ?>;
  126. *
  127. * obj.ui(najax.asyncCall);
  128. *
  129. * </script>
  130. * </code>
  131. *
  132. * @author Stanimir Angeloff
  133. *
  134. * @package NAJAX
  135. *
  136. * @subpackage NAJAX_HTML
  137. *
  138. * @version 0.4.1.0
  139. *
  140. */
  141. class NAJAX_HTML
  142. {
  143. /**
  144. * Returns the element whose ID is specified.
  145. *
  146. * <p>Note that you must use the '&' operator in order
  147. * to get a reference to the element.</p>
  148. * <p>Example:</p>
  149. * <code>
  150. * $content =& NAJAX_HTML::getElementById('content');
  151. * </code>
  152. *
  153. * @access public
  154. *
  155. * @param string $id String representing the unique id of
  156. * the element being sought.
  157. *
  158. * @return object The DOM element with the specified ID.
  159. * Note that the server has no idea whether
  160. * the element exists on the client or not.
  161. *
  162. * @static
  163. *
  164. */
  165. function &getElementById($id)
  166. {
  167. /**
  168. * Loads the class that defines the DOM element by ID class.
  169. */
  170. require_once(NAJAX_HTML_BASE . '/classes/DOM/ElementById.class.php');
  171.  
  172. $element = new NAJAX_HTML_DOM_ElementById($id);
  173.  
  174. $GLOBALS['_NAJAX_HTML_DATA'][] =& $element;
  175.  
  176. return $element;
  177. }
  178.  
  179. /**
  180. * Returns an object representing a list of elements
  181. * of a given name in the document.
  182. *
  183. * <p>Note that you must use the '&' operator in order
  184. * to get a reference to the list.</p>
  185. * <p>Example:</p>
  186. * <code>
  187. * $messages =& NAJAX_HTML::getElementsByName('message');
  188. * </code>
  189. *
  190. * @access public
  191. *
  192. * @param string $name String representing the value of
  193. * the name attribute on the element.
  194. *
  195. * @return object The DOM elements list with the specified name.
  196. * Note that the server has no idea whether
  197. * any elements exists on the client or not.
  198. *
  199. * @static
  200. *
  201. */
  202. function &getElementsByName($name)
  203. {
  204. /**
  205. * Loads the class that defines the DOM elements by name class.
  206. */
  207. require_once(NAJAX_HTML_BASE . '/classes/DOM/ElementsByName.class.php');
  208.  
  209. $elements = new NAJAX_HTML_DOM_ElementsByName($name);
  210.  
  211. $GLOBALS['_NAJAX_HTML_DATA'][] =& $elements;
  212.  
  213. return $elements;
  214. }
  215.  
  216. /**
  217. * Returns an object representing a list of elements
  218. * of a given tag name in the document.
  219. *
  220. * <p>Note that you must use the '&' operator in order
  221. * to get a reference to the list.</p>
  222. * <p>Example:</p>
  223. * <code>
  224. * $inputs =& NAJAX_HTML::getElementsByTagName('input');
  225. * </code>
  226. *
  227. * @access public
  228. *
  229. * @param string $tagName String representing the name of
  230. * the tag on the element.
  231. *
  232. * @return object The DOM elements list with the specified tag name.
  233. * Note that the server has no idea whether
  234. * any elements exists on the client or not.
  235. *
  236. * @static
  237. *
  238. */
  239. function &getElementsByTagName($tagName)
  240. {
  241. /**
  242. * Loads the class that defines the DOM elements by tag name class.
  243. */
  244. require_once(NAJAX_HTML_BASE . '/classes/DOM/ElementsByTagName.class.php');
  245.  
  246. $elements = new NAJAX_HTML_DOM_ElementsByTagName($tagName);
  247.  
  248. $GLOBALS['_NAJAX_HTML_DATA'][] =& $elements;
  249.  
  250. return $elements;
  251. }
  252.  
  253. /**
  254. * Adds a given JavaScript code to the output.
  255. *
  256. * <p>Note that you must use the '&' operator in order
  257. * to get a reference to the script block.</p>
  258. * <p>You should add ';' at the end of the script.</p>
  259. * <p>Example:</p>
  260. * <code>
  261. * NAJAX_HTML::addScriptBlock('alert("Hello World!");');
  262. * </code>
  263. *
  264. * @access public
  265. *
  266. * @param string $script JavaScript source code.
  267. *
  268. * @return object The DOM element that represents the script block.
  269. *
  270. * @static
  271. *
  272. */
  273. function &addScriptBlock($script)
  274. {
  275. /**
  276. * Loads the class that defines the DOM script block class.
  277. */
  278. require_once(NAJAX_HTML_BASE . '/classes/DOM/ScriptBlock.class.php');
  279.  
  280. $scriptBlock = new NAJAX_HTML_DOM_ScriptBlock($script);
  281.  
  282. $GLOBALS['_NAJAX_HTML_DATA'][] =& $scriptBlock;
  283.  
  284. return $scriptBlock;
  285. }
  286.  
  287. /**
  288. * Imports an associative array to the corresponding form elements.
  289. *
  290. * <p>Example:</p>
  291. * <code>
  292. * NAJAX_HTML::importForm('mainForm', array('firstName' => 'First', 'lastName' => 'Last'));
  293. * </code>
  294. *
  295. * @access public
  296. *
  297. * @param string $id The client ID of the form.
  298. *
  299. * @param string $formData Associative array that contains the
  300. * values to import.
  301. *
  302. * @return void
  303. *
  304. * @static
  305. *
  306. */
  307. function importForm($id, $formData)
  308. {
  309. $script = 'najax.html.importForm(';
  310. $script .= NAJAX_Client::register($id);
  311. $script .= ', ';
  312. $script .= NAJAX_Client::register($formData);
  313. $script .= ');';
  314.  
  315. NAJAX_HTML::addScriptBlock($script);
  316. }
  317.  
  318. /**
  319. * Returns the JavaScript code of all DOM elements.
  320. *
  321. * <p>You should not call this method directly.</p>
  322. *
  323. * @access public
  324. *
  325. * @return string JavaScript source code for each DOM element
  326. * or null if no DOM elements were created.
  327. *
  328. * @static
  329. *
  330. */
  331. function process()
  332. {
  333. if ( ! empty($GLOBALS['_NAJAX_HTML_DATA'])) {
  334.  
  335. $returnValue = '';
  336.  
  337. foreach ($GLOBALS['_NAJAX_HTML_DATA'] as $domObject) {
  338.  
  339. $returnValue .= $domObject->process();
  340. }
  341.  
  342. return $returnValue;
  343. }
  344.  
  345. return null;
  346. }
  347. }
  348. ?>

Documentation generated on Tue, 20 Sep 2005 21:40:03 +0300 by phpDocumentor 1.3.0RC3