/** * Sends response to output. * @return void */ public function send() { Nette\Environment::getHttpResponse()->setContentType($this->contentType); Nette\Environment::getHttpResponse()->setExpiration(FALSE); echo Nette\Json::encode($this->payload); }
private function getClientScript(IFormControl $control, $operation, $arg) { $operation = strtolower($operation); $elem = 'form[' . Nette\Json::encode($control->getHtmlName()) . ']'; switch (TRUE) { case $control instanceof HiddenField || $control->isDisabled(): return NULL; case $operation === ':filled' && $control instanceof RadioList: return "res = (val = nette.getValue({$elem})) !== null;"; case $operation === ':submitted' && $control instanceof SubmitButton: return "res = sender && sender.name==" . Nette\Json::encode($control->getHtmlName()) . ";"; case $operation === ':equal' && $control instanceof MultiSelectBox: $tmp = array(); foreach (is_array($arg) ? $arg : array($arg) as $item) { $tmp[] = "options[i].value==" . Nette\Json::encode((string) $item); } $first = $control->isFirstSkipped() ? 1 : 0; return "var options = {$elem}.options; res = false;\n" . "for (var i={$first}, len=options.length; i<len; i++)\n\t" . "if (options[i].selected && (" . implode(' || ', $tmp) . ")) { res = true; break; }"; case $operation === ':filled' && $control instanceof SelectBox: return "res = {$elem}.selectedIndex >= " . ($control->isFirstSkipped() ? 1 : 0) . ";"; case $operation === ':filled' && $control instanceof TextBase: return "val = nette.getValue({$elem}); res = val!='' && val!=" . Nette\Json::encode((string) $control->getEmptyValue()) . ";"; case $operation === ':minlength' && $control instanceof TextBase: return "res = (val = nette.getValue({$elem})).length>=" . (int) $arg . ";"; case $operation === ':maxlength' && $control instanceof TextBase: return "res = (val = nette.getValue({$elem})).length<=" . (int) $arg . ";"; case $operation === ':length' && $control instanceof TextBase: if (!is_array($arg)) { $arg = array($arg, $arg); } return "val = nette.getValue({$elem}); res = " . ($arg[0] === NULL ? "true" : "val.length>=" . (int) $arg[0]) . " && " . ($arg[1] === NULL ? "true" : "val.length<=" . (int) $arg[1]) . ";"; case $operation === ':email' && $control instanceof TextBase: return 'res = /^[^@\\s]+@[^@\\s]+\\.[a-z]{2,10}$/i.test(val = nette.getValue(' . $elem . '));'; case $operation === ':url' && $control instanceof TextBase: return 'res = /^.+\\.[a-z]{2,6}(\\/.*)?$/i.test(val = nette.getValue(' . $elem . '));'; case $operation === ':regexp' && $control instanceof TextBase: if (!preg_match('#^(/.*/)([imu]*)$#', $arg, $matches)) { return NULL; // regular expression must be JavaScript compatible } $arg = $matches[1] . str_replace('u', '', $matches[2]); return "res = {$arg}.test(val = nette.getValue({$elem}));"; case $operation === ':integer' && $control instanceof TextBase: return "res = /^-?[0-9]+\$/.test(val = nette.getValue({$elem}));"; case $operation === ':float' && $control instanceof TextBase: return "res = /^-?[0-9]*[.,]?[0-9]+\$/.test(val = nette.getValue({$elem}));"; case $operation === ':range' && $control instanceof TextBase: return "val = nette.getValue({$elem}); res = " . ($arg[0] === NULL ? "true" : "parseFloat(val)>=" . Nette\Json::encode((double) $arg[0])) . " && " . ($arg[1] === NULL ? "true" : "parseFloat(val)<=" . Nette\Json::encode((double) $arg[1])) . ";"; case $operation === ':filled' && $control instanceof FormControl: return "res = (val = nette.getValue({$elem})) != '';"; case $operation === ':valid' && $control instanceof FormControl: return "res = !this[" . Nette\Json::encode($control->getHtmlName()) . "](sender);"; case $operation === ':equal' && $control instanceof FormControl: if ($control instanceof Checkbox) { $arg = (bool) $arg; } $tmp = array(); foreach (is_array($arg) ? $arg : array($arg) as $item) { if ($item instanceof IFormControl) { // compare with another form control? $tmp[] = "val==nette.getValue(form[" . Nette\Json::encode($item->getHtmlName()) . "])"; } else { $tmp[] = "val==" . Nette\Json::encode($item); } } return "val = nette.getValue({$elem}); res = (" . implode(' || ', $tmp) . ");"; } }
/** * Escapes string for use inside JavaScript template. * @param mixed UTF-8 encoding * @return string */ public static function escapeJs($s) { if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) { $s = $s->__toString(TRUE); } return str_replace(']]>', ']]\\x3E', Nette\Json::encode($s)); }
/** * Sends response to output. * @return void */ public function send(Nette\Web\IHttpRequest $httpRequest, Nette\Web\IHttpResponse $httpResponse) { $httpResponse->setContentType($this->contentType); $httpResponse->setExpiration(FALSE); echo Nette\Json::encode($this->payload); }