Source for file InstantClientScript.php

Documentation is available at InstantClientScript.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:  * Instant 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: final class InstantClientScript extends Object
  34. 34: {
  35. 35:     /** @var string  JavaScript event handler name */
  36. 36:     public $validateFunction;
  37. 37:  
  38. 38:     /** @var string  JavaScript event handler name */
  39. 39:     public $toggleFunction;
  40. 40:  
  41. 41:     /** @var string  JavaScript code */
  42. 42:     public $doAlert = 'if (element) element.focus(); alert(message);';
  43. 43:  
  44. 44:     /** @var string  JavaScript code */
  45. 45:     public $doToggle = 'if (element) element.style.display = visible ? "" : "none";';
  46. 46:  
  47. 47:     /** @var string */
  48. 48:     public $validateScript;
  49. 49:  
  50. 50:     /** @var string */
  51. 51:     public $toggleScript;
  52. 52:  
  53. 53:     /** @var bool */
  54. 54:     private $central;
  55. 55:  
  56. 56:     /** @var Form */
  57. 57:     private $form;
  58. 58:  
  59. 59:  
  60. 60:  
  61. 61:     public function __construct(Form $form)
  62. 62:     {
  63. 63:         $this->form $form;
  64. 64:         $name ucfirst($form->getName())//ucfirst(strtr($form->getUniqueId(), Form::NAME_SEPARATOR, '_'));
  65. 65:         $this->validateFunction = 'validate' $name;
  66. 66:         $this->toggleFunction = 'toggle' $name;
  67. 67:     }
  68. 68:  
  69. 69:  
  70. 70:  
  71. 71:     public function enable()
  72. 72:     {
  73. 73:         $this->validateScript = '';
  74. 74:         $this->toggleScript = '';
  75. 75:         $this->central TRUE;
  76. 76:  
  77. 77:         foreach ($this->form->getControls(as $control{
  78. 78:             $script $this->getValidateScript($control->getRules());
  79. 79:             if ($script{
  80. 80:                 $this->validateScript .= "do {\n\t$script} while(0);\n\n\t";
  81. 81:             }
  82. 82:             $this->toggleScript .= $this->getToggleScript($control->getRules());
  83. 83:  
  84. 84:             if ($control instanceof ISubmitterControl && $control->getValidationScope(!== TRUE{
  85. 85:                 $this->central FALSE;
  86. 86:             }
  87. 87:         }
  88. 88:  
  89. 89:         if ($this->validateScript || $this->toggleScript{
  90. 90:             if ($this->central{
  91. 91:                 $this->form->getElementPrototype()->onsubmit("return $this->validateFunction(this)"TRUE);
  92. 92:  
  93. 93:             else {
  94. 94:                 foreach ($this->form->getComponents(TRUE'Nette\Forms\ISubmitterControl'as $control{
  95. 95:                     if ($control->getValidationScope()) {
  96. 96:                         $control->getControlPrototype()->onclick("return $this->validateFunction(this)"TRUE);
  97. 97:                     }
  98. 98:                 }
  99. 99:             }
  100. 100:         }
  101. 101:     }
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Generates the client side validation script.
  107. 107:      * @return string 
  108. 108:      */
  109. 109:     public function renderClientScript()
  110. 110:     {
  111. 111:         $s '';
  112. 112:  
  113. 113:         if ($this->validateScript{
  114. 114:             $s .= "function $this->validateFunction(sender) {\n\t"
  115. 115:             . "var element, message, res;\n\t"
  116. 116:             . $this->validateScript
  117. 117:             . "return true;\n"
  118. 118:             . "}\n\n";
  119. 119:         }
  120. 120:  
  121. 121:         if ($this->toggleScript{
  122. 122:             $s .= "function $this->toggleFunction(sender) {\n\t"
  123. 123:             . "var element, visible, res;\n\t"
  124. 124:             . $this->toggleScript
  125. 125:             . "\n}\n\n"
  126. 126:             . "$this->toggleFunction(null);\n";
  127. 127:         }
  128. 128:  
  129. 129:         if ($s{
  130. 130:             return "<script type=\"text/javascript\">\n"
  131. 131:             . "/* <![CDATA[ */\n"
  132. 132:             . $s
  133. 133:             . "/* ]]> */\n"
  134. 134:             . "</script>";
  135. 135:         }
  136. 136:     }
  137. 137:  
  138. 138:  
  139. 139:  
  140. 140:     private function getValidateScript(Rules $rules$onlyCheck FALSE)
  141. 141:     {
  142. 142:         $res '';
  143. 143:         foreach ($rules as $rule{
  144. 144:             if (!is_string($rule->operation)) continue;
  145. 145:  
  146. 146:             if (strcasecmp($rule->operation'Nette\Forms\InstantClientScript::javascript'=== 0{
  147. 147:                 $res .= "$rule->arg\n\t";
  148. 148:                 continue;
  149. 149:             }
  150. 150:  
  151. 151:             $script $this->getClientScript($rule->control$rule->operation$rule->arg);
  152. 152:             if (!$scriptcontinue;
  153. 153:  
  154. 154:             if (!empty($rule->message)) // this is rule
  155. 155:                 if ($onlyCheck{
  156. 156:                     $res .= "$script\n\tif (($rule->isNegative '' '!'"res) { return false; }\n\t";
  157. 157:  
  158. 158:                 else {
  159. 159:                     $res .= "$script\n\t"
  160. 160:                         . "if (" ($rule->isNegative '' '!'"res) { "
  161. 161:                         . "message = " json_encode((string) vsprintf($rule->control->translate($rule->message)(array) $rule->arg)) "; "
  162. 162:                         . $this->doAlert
  163. 163:                         . " return false; }\n\t";
  164. 164:                 }
  165. 165:             }
  166. 166:  
  167. 167:             if ($rule->type === Rule::CONDITION// this is condition
  168. 168:                 $innerScript $this->getValidateScript($rule->subRules$onlyCheck);
  169. 169:                 if ($innerScript{
  170. 170:                     $res .= "$script\n\tif (($rule->isNegative '!' ''"res) {\n\t\t" str_replace("\n\t""\n\t\t"rtrim($innerScript)) "\n\t}\n\t";
  171. 171:                     if (!$onlyCheck && $rule->control instanceof ISubmitterControl{
  172. 172:                         $this->central FALSE;
  173. 173:                     }
  174. 174:                 }
  175. 175:             }
  176. 176:         }
  177. 177:         return $res;
  178. 178:     }
  179. 179:  
  180. 180:  
  181. 181:  
  182. 182:     private function getToggleScript(Rules $rules$cond NULL)
  183. 183:     {
  184. 184:         $s '';
  185. 185:         foreach ($rules->getToggles(as $id => $visible{
  186. 186:             $s .= "visible = true; {$cond}element = document.getElementById('$id "');\n\t"
  187. 187:                 . ($visible '' 'visible = !visible; ')
  188. 188:                 . $this->doToggle
  189. 189:                 . "\n\t";
  190. 190:         }
  191. 191:         foreach ($rules as $rule{
  192. 192:             if ($rule->type === Rule::CONDITION && is_string($rule->operation)) {
  193. 193:                 $script $this->getClientScript($rule->control$rule->operation$rule->arg);
  194. 194:                 if ($script{
  195. 195:                     $res $this->getToggleScript($rule->subRules$cond "$script visible = visible && ($rule->isNegative '!' ''"res;\n\t");
  196. 196:                     if ($res{
  197. 197:                         $el $rule->control->getControlPrototype();
  198. 198:                         if ($el->getName(=== 'select'{
  199. 199:                             $el->onchange("$this->toggleFunction(this)"TRUE);
  200. 200:                         else {
  201. 201:                             $el->onclick("$this->toggleFunction(this)"TRUE);
  202. 202:                             //$el->onkeyup("$this->toggleFunction(this)", TRUE);
  203. 203:                         }
  204. 204:                         $s .= $res;
  205. 205:                     }
  206. 206:                 }
  207. 207:             }
  208. 208:         }
  209. 209:         return $s;
  210. 210:     }
  211. 211:  
  212. 212:  
  213. 213:  
  214. 214:     private function getValueScript(IFormControl $control)
  215. 215:     {
  216. 216:         $tmp "element = document.getElementById(" json_encode($control->getHtmlId()) ");\n\t";
  217. 217:         switch (TRUE{
  218. 218:         case $control instanceof Checkbox:
  219. 219:             return $tmp "var val = element.checked;\n\t";
  220. 220:  
  221. 221:         case $control instanceof RadioList:
  222. 222:             return "for (var val=null, i=0; i<" count($control->getItems()) "; i++) {\n\t\t"
  223. 223:             . "element = document.getElementById(" json_encode($control->getHtmlId('-'"+i);\n\t\t"
  224. 224:             . "if (element.checked) { val = element.value; break; }\n\t"
  225. 225:             . "}\n\t";
  226. 226:  
  227. 227:         default:
  228. 228:             return $tmp "var val = element.value.replace(/^\\s+|\\s+\$/g, '');\n\t";
  229. 229:         }
  230. 230:     }
  231. 231:  
  232. 232:  
  233. 233:  
  234. 234:     private function getClientScript(IFormControl $control$operation$arg)
  235. 235:     {
  236. 236:         $operation strtolower($operation);
  237. 237:         switch (TRUE{
  238. 238:         case $control instanceof HiddenField || $control->isDisabled():
  239. 239:             return NULL;
  240. 240:  
  241. 241:         case $operation === ':filled' && $control instanceof RadioList:
  242. 242:             return $this->getValueScript($control"res = val !== null;";
  243. 243:  
  244. 244:         case $operation === ':submitted' && $control instanceof SubmitButton:
  245. 245:             return "element=null; res=sender && sender.name==" json_encode($control->getHtmlName()) ";";
  246. 246:  
  247. 247:         case $operation === ':equal' && $control instanceof MultiSelectBox:
  248. 248:             $tmp array();
  249. 249:             foreach ((is_array($arg$arg array($arg)) as $item{
  250. 250:                 $tmp["element.options[i].value==" json_encode((string) $item);
  251. 251:             }
  252. 252:             $first $control->isFirstSkipped(0;
  253. 253:             return "element = document.getElementById(" json_encode($control->getHtmlId()) ");\n\tres = false;\n\t"
  254. 254:                 . "for (var i=$first;i<element.options.length;i++)\n\t\t"
  255. 255:                 . "if (element.options[i].selected && (" implode(' || '$tmp")) { res = true; break; }";
  256. 256:  
  257. 257:         case $operation === ':filled' && $control instanceof SelectBox:
  258. 258:             return "element = document.getElementById(" json_encode($control->getHtmlId()) ");\n\t"
  259. 259:                 . "res = element.selectedIndex >= " ($control->isFirstSkipped(0";";
  260. 260:  
  261. 261:         case $operation === ':filled' && $control instanceof TextBase:
  262. 262:             return $this->getValueScript($control"res = val!='' && val!=" json_encode((string) $control->getEmptyValue()) ";";
  263. 263:  
  264. 264:         case $operation === ':minlength' && $control instanceof TextBase:
  265. 265:             return $this->getValueScript($control"res = val.length>=" . (int) $arg ";";
  266. 266:  
  267. 267:         case $operation === ':maxlength' && $control instanceof TextBase:
  268. 268:             return $this->getValueScript($control"res = val.length<=" . (int) $arg ";";
  269. 269:  
  270. 270:         case $operation === ':length' && $control instanceof TextBase:
  271. 271:             if (!is_array($arg)) {
  272. 272:                 $arg array($arg$arg);
  273. 273:             }
  274. 274:             return $this->getValueScript($control"res = " ($arg[0=== NULL "true" "val.length>=" . (int) $arg[0]" && "
  275. 275:                 . ($arg[1=== NULL "true" "val.length<=" . (int) $arg[1]";";
  276. 276:  
  277. 277:         case $operation === ':email' && $control instanceof TextBase:
  278. 278:             return $this->getValueScript($control'res = /^[^@\s]+@[^@\s]+\.[a-z]{2,10}$/i.test(val);';
  279. 279:  
  280. 280:         case $operation === ':url' && $control instanceof TextBase:
  281. 281:             return $this->getValueScript($control'res = /^.+\.[a-z]{2,6}(\\/.*)?$/i.test(val);';
  282. 282:  
  283. 283:         case $operation === ':regexp' && $control instanceof TextBase:
  284. 284:             if (strncmp($arg'/'1)) {
  285. 285:                 throw new InvalidStateException("Regular expression '$arg' must be JavaScript compatible.");
  286. 286:             }
  287. 287:             return $this->getValueScript($control"res = $arg.test(val);";
  288. 288:  
  289. 289:         case $operation === ':integer' && $control instanceof TextBase:
  290. 290:             return $this->getValueScript($control"res = /^-?[0-9]+$/.test(val);";
  291. 291:  
  292. 292:         case $operation === ':float' && $control instanceof TextBase:
  293. 293:             return $this->getValueScript($control"res = /^-?[0-9]*[.,]?[0-9]+$/.test(val);";
  294. 294:  
  295. 295:         case $operation === ':range' && $control instanceof TextBase:
  296. 296:             return $this->getValueScript($control"res = " ($arg[0=== NULL "true" "parseFloat(val)>=" json_encode((float) $arg[0])) " && "
  297. 297:                 . ($arg[1=== NULL "true" "parseFloat(val)<=" json_encode((float) $arg[1])) ";";
  298. 298:  
  299. 299:         case $operation === ':filled' && $control instanceof FormControl:
  300. 300:             return $this->getValueScript($control"res = val!='';";
  301. 301:  
  302. 302:         case $operation === ':valid' && $control instanceof FormControl:
  303. 303:             return $this->getValueScript($control"res = function(){\n\t" $this->getValidateScript($control->getRules()TRUE"return true; }();";
  304. 304:  
  305. 305:         case $operation === ':equal' && $control instanceof FormControl:
  306. 306:             if ($control instanceof Checkbox$arg = (bool) $arg;
  307. 307:             $tmp array();
  308. 308:             foreach ((is_array($arg$arg array($arg)) as $item{
  309. 309:                 if ($item instanceof IFormControl// compare with another form control?
  310. 310:                     $tmp["val==function(){var element;" $this->getValueScript($item)"return val;}()";
  311. 311:                 else {
  312. 312:                     $tmp["val==" json_encode($item);
  313. 313:                 }
  314. 314:             }
  315. 315:             return $this->getValueScript($control"res = (" implode(' || '$tmp");";
  316. 316:         }
  317. 317:     }
  318. 318:  
  319. 319:  
  320. 320:  
  321. 321:     public static function javascript()
  322. 322:     {
  323. 323:         return TRUE;
  324. 324:     }
  325. 325: