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