Source for file UserClientScript.php

Documentation is available at UserClientScript.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Forms
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * User validation JavaScript generator.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Forms
  33. 33:  */
  34. 34: class UserClientScript extends Object
  35. 35: {
  36. 36:     /** @var Form */
  37. 37:     private $form;
  38. 38:  
  39. 39:  
  40. 40:  
  41. 41:     public function __construct(Form $form)
  42. 42:     {
  43. 43:         $this->form $form;
  44. 44:     }
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * Generates the client side validation script.
  50. 50:      * @return void 
  51. 51:      */
  52. 52:     public function renderClientScript()
  53. 53:     {
  54. 54:         $this->form->getElementPrototype()->attrs['data-nette-rules'json_encode($this->exportContainer($this->form));
  55. 55:     }
  56. 56:  
  57. 57:  
  58. 58:  
  59. 59:     /**
  60. 60:      * Exports description for JavaScript.
  61. 61:      * @return array 
  62. 62:      */
  63. 63:     public function exportContainer(FormContainer $container)
  64. 64:     {
  65. 65:         $data array();
  66. 66:         foreach ($container->getComponents(as $name => $control{
  67. 67:             if ($control instanceof FormContainer{
  68. 68:                 $data[$name$this->exportContainer($control);
  69. 69:  
  70. 70:             elseif ($control instanceof IFormControl{
  71. 71:                 $data[$name$this->exportControl($control);
  72. 72:             }
  73. 73:         }
  74. 74:         return array(
  75. 75:             'class' => $container->getClass(),
  76. 76:             'controls' => $data,
  77. 77:         );
  78. 78:     }
  79. 79:  
  80. 80:  
  81. 81:  
  82. 82:     /**
  83. 83:      * Exports description for JavaScript.
  84. 84:      * @return array 
  85. 85:      */
  86. 86:     private function exportControl(IFormControl $control)
  87. 87:     {
  88. 88:         return $control->isDisabled(NULL array(
  89. 89:             'class' => $control->getClass(),
  90. 90:             'rules' => $this->exportRules($control->getRules()),
  91. 91:             'opt' => $control instanceof FormControl $control->getOptions(NULL,
  92. 92:             'scope' => $control instanceof ISubmitterControl ? (bool) $control->getValidationScope(NULL,
  93. 93:         );
  94. 94:     }
  95. 95:  
  96. 96:  
  97. 97:  
  98. 98:     /**
  99. 99:      * Exports rules for JavaScript.
  100. 100:      * @return array 
  101. 101:      */
  102. 102:     private function exportRules(Rules $rules)
  103. 103:     {
  104. 104:         $data array();
  105. 105:         foreach ($rules as $rule{
  106. 106:             if (!is_string($rule->operation)) continue;
  107. 107:             $data[array(
  108. 108:                 'class' => $rule->control->getClass(),
  109. 109:                 'op' => $rule->operation,
  110. 110:                 'neg' => $rule->isNegative,
  111. 111:                 'type' => $rule->type,
  112. 112:                 'msg' => $rule->message,
  113. 113:                 'id' => $rule->control->getHtmlId(),
  114. 114:                 'arg' => $rule->arg instanceof FormControl $rule->arg->getHtmlId($rule->arg,
  115. 115:                 'sub' => $rule->subRules $this->exportRules($rule->subRulesNULL,
  116. 116:             );
  117. 117:         }
  118. 118:         return $data;
  119. 119:         /*return array(
  120. 120:             'rules' => $data,
  121. 121:             'toggles' => $this->toggles,
  122. 122:         );*/
  123. 123:     }
  124. 124: