protected function getJavascriptCallback() { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $config = $this->getConfig(); $arguments = array($this->owner->getJavascriptValue()); foreach ($config['arguments'] as $arg) { $arguments[] = HTML_QuickForm2_JavascriptBuilder::encode($arg); } return "function() { return !" . $this->findJavascriptName() . "(" . implode(', ', $arguments) . "); }"; }
protected function renderClientRules(HTML_QuickForm2_JavascriptBuilder $builder) { $id = Am_Controller::escape($this->editor->getId()); $vars = ""; foreach ($this->tagsOptions as $k => $v) { $vars .= sprintf("[%s, %s],\n", Am_Controller::getJson($v), Am_Controller::getJson($k)); } $vars = trim($vars, "\n\r,"); $builder->addElementJavascript(<<<CUT \$(function(){ \$('select#insert-tags').change(function(){ var val = \$(this).val(); if (!val) return; \$("#txt-0").insertAtCaret(val); \$(this).prop("selectedIndex", -1); }); if (CKEDITOR.instances["{$id}"]) { delete CKEDITOR.instances["{$id}"]; } var editor = null; \$("input[name='format']").change(function() { if (window.configDisable_rte) return; if (!this.checked) return; if (this.value == 'html') { if (!editor) { editor = initCkeditor("{$id}", { placeholder_items: [ {$vars} ], entities_greek: false}); } \$('#insert-tags-wrapper').hide(); } else { if (editor) { editor.destroy(); editor = null; } \$('#insert-tags-wrapper').show(); } }).change(); }); CUT ); }
protected function renderClientRules(HTML_QuickForm2_JavascriptBuilder $builder) { $id = Am_Controller::escape($this->editor->getId()); $vars = ""; foreach ($this->tagsOptions as $k => $v) { $vars .= "['{$k}'],\n"; } $vars = trim($vars, "\n\r,"); $builder->addElementJavascript(<<<CUT \$(function(){ // modified version of http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript \$.fn.insertAtCaret = function (myValue) { return this.each(function(){ //IE support if (document.selection) { this.focus(); sel = document.selection.createRange(); sel.text = myValue; this.focus(); } //MOZILLA/NETSCAPE support else if (this.selectionStart || this.selectionStart == '0') { var startPos = this.selectionStart; var endPos = this.selectionEnd; var scrollTop = this.scrollTop; this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length); this.focus(); this.selectionStart = startPos + myValue.length; this.selectionEnd = startPos + myValue.length; this.scrollTop = scrollTop; } else { this.value += myValue; this.focus(); } }); }; \$('select#insert-tags').change(function(){ var val = \$(this).val(); if (!val) return; \$("#txt-0").insertAtCaret(val); \$(this).prop("selectedIndex", -1); }); if (CKEDITOR.instances["{$id}"]) { delete CKEDITOR.instances["{$id}"]; } var editor = null; \$("input[name='format']").change(function() { if (!this.checked) return; if (this.value == 'html') { if (!editor) { editor = initCkeditor("{$id}", { placeholder_items: [ {$vars} ]}); } \$('select#insert-tags').hide(); } else { if (editor) { editor.destroy(); editor = null; } \$('select#insert-tags').show(); } }).change(); }); CUT ); }
/** * Generates a javascript function call to initialize hierselect behaviour * * @return string */ private function _generateInitScript() { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $ids = array(); /* @var $element HTML_QuickForm2_Element */ foreach ($this as $element) { $ids[] = $element->getId(); } return 'qf.elements.hierselect.init(' . HTML_QuickForm2_JavascriptBuilder::encode($ids) . (empty($this->jsCallback) ? '' : ", {$this->jsCallback}") . ');'; }
/** * Adds element's client-side validation rules to a builder object * * @param HTML_QuickForm2_JavascriptBuilder $builder */ protected function renderClientRules(HTML_QuickForm2_JavascriptBuilder $builder) { if ($this->toggleFrozen()) { return; } $onblur = HTML_QuickForm2_Rule::ONBLUR_CLIENT ^ HTML_QuickForm2_Rule::CLIENT; foreach ($this->rules as $rule) { if ($rule[1] & HTML_QuickForm2_Rule::CLIENT) { $builder->addRule($rule[0], $rule[1] & $onblur); } } }
/** * Returns the client-side representation of the Rule * * The Javascript object returned contains the following fields: * - callback: {@see getJavascriptCallback()} * - elementId: element ID to set error for if validation fails * - errorMessage: error message to set if validation fails * - chained: chained rules, array of arrays like in $chainedRules property * * @return string * @throws HTML_QuickForm2_Exception if Rule or its chained Rules can only * be run server-side */ public function getJavascript($outputTriggers = true) { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $js = $this->getJavascriptCallback() . ",\n\t'" . $this->owner->getId() . "', " . HTML_QuickForm2_JavascriptBuilder::encode($this->getMessage()); $js = $outputTriggers && count($triggers = $this->getJavascriptTriggers()) ? 'new qf.LiveRule(' . $js . ', ' . HTML_QuickForm2_JavascriptBuilder::encode($triggers) : 'new qf.Rule(' . $js; if (count($this->chainedRules) > 1 || count($this->chainedRules[0]) > 0) { $chained = array(); foreach ($this->chainedRules as $item) { $multipliers = array(); foreach ($item as $multiplier) { $multipliers[] = $multiplier->getJavascript(false); } $chained[] = '[' . implode(",\n", $multipliers) . ']'; } $js .= ",\n\t [" . implode(",\n", $chained) . "]"; } return $js . ')'; }
/** * Returns the client-side representation of the Rule * * The Javascript object returned contains the following fields: * - callback: {@see getJavascriptCallback()} * - elementId: element ID to set error for if validation fails * - errorMessage: error message to set if validation fails * - chained: chained rules, array of arrays like in $chainedRules property * * @return string * @throws HTML_QuickForm2_Exception if Rule or its chained Rules can only * be run server-side */ public function getJavascript() { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $js = "{\n\tcallback: " . $this->getJavascriptCallback() . ",\n" . "\towner: '" . $this->owner->getId() . "',\n" . "\tmessage: " . HTML_QuickForm2_JavascriptBuilder::encode($this->getMessage()); if (count($this->chainedRules) > 1 || count($this->chainedRules[0]) > 0) { $chained = array(); foreach ($this->chainedRules as $item) { $multipliers = array(); foreach ($item as $multiplier) { $multipliers[] = $multiplier->getJavascript(); } $chained[] = '[' . implode(",\n", $multipliers) . ']'; } $js .= ",\n\tchained: [" . implode(",\n", $chained) . "]"; } return $js . "\n}"; }
public function testFormJavascript() { $builder = new HTML_QuickForm2_JavascriptBuilder(); $element = new HTML_QuickForm2_Element_InputText(); $mockRuleOne = $this->getMock('HTML_QuickForm2_Rule', array('validateOwner', 'getJavascriptCallback'), array($element)); $mockRuleOne->expects($this->once())->method('getJavascriptCallback')->will($this->returnValue('jsRuleOne')); $mockRuleTwo = $this->getMock('HTML_QuickForm2_Rule', array('validateOwner', 'getJavascriptCallback'), array($element)); $mockRuleTwo->expects($this->once())->method('getJavascriptCallback')->will($this->returnValue('jsRuleTwo')); $this->assertEquals('', $builder->getFormJavascript()); $builder->setFormId('formOne'); $builder->addRule($mockRuleOne); $builder->addElementJavascript('setupCodeOne'); $builder->setFormId('formTwo'); $builder->addRule($mockRuleTwo); $builder->addElementJavascript('setupCodeTwo'); $scriptOne = $builder->getFormJavascript('formOne', false); $this->assertContains('jsRuleOne', $scriptOne); $this->assertContains('setupCodeOne', $scriptOne); $this->assertNotContains('jsRuleTwo', $scriptOne); $this->assertNotContains('setupCodeTwo', $scriptOne); $this->assertNotContains('<script', $scriptOne); $scriptTwo = $builder->getFormJavascript('formTwo', true); $this->assertNotContains('jsRuleOne', $scriptTwo); $this->assertNotContains('setupCodeOne', $scriptTwo); $this->assertContains('jsRuleTwo', $scriptTwo); $this->assertContains('setupCodeTwo', $scriptTwo); $this->assertContains('<script', $scriptTwo); $scriptBoth = $builder->getFormJavascript(); $this->assertContains('jsRuleOne', $scriptBoth); $this->assertContains('setupCodeTwo', $scriptBoth); }
protected function renderClientRules(HTML_QuickForm2_JavascriptBuilder $builder) { $generate = ___("generate"); $builder->addElementJavascript(<<<CUT \$(document).ready(function(){ var pass0 = \$("input#_pass-0").after(" <a href='javascript:;' class='local' id='generate-pass'>{$generate}</a>"); \$("a#generate-pass").click(function(){ if (pass0.attr("type")!="text") { pass0.replaceWith("<input type='text' name='"+pass0.attr("name") +"' id='"+pass0.attr("id") +"' size='"+pass0.attr("size") +"' />"); pass0 = \$("input#_pass-0"); } var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"; var pass = ""; var len = 9; for(i=0;i<len;i++) { x = Math.floor(Math.random() * 62); pass += chars.charAt(x); } pass0.val(pass); }); }); CUT ); }
protected function getJavascriptCallback() { HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); $config = $this->getConfig(); $operand1 = $this->owner->getJavascriptValue(); $operand2 = $config['operand'] instanceof HTML_QuickForm2_Node ? $config['operand']->getJavascriptValue() : HTML_QuickForm2_JavascriptBuilder::encode($config['operand']); if (!in_array($config['operator'], array('===', '!=='))) { $check = "Number({$operand1}) {$config['operator']} Number({$operand2})"; } else { $check = "String({$operand1}) {$config['operator']} String({$operand2})"; } return "function () { return {$check}; }"; }
/** * Adds element's client-side validation rules to a builder object * * This will also call forceValidator() if the repeat does not contain * any (visible) items but some of the child elements define client-side rules * * @param HTML_QuickForm2_JavascriptBuilder $builder */ protected function renderClientRules(HTML_QuickForm2_JavascriptBuilder $builder) { if ($this->toggleFrozen()) { return; } if (!$this->getIndexes()) { $fakeBuilder = new HTML_QuickForm2_JavascriptBuilder(); /* @var $child HTML_QuickForm2_Node */ foreach ($this->getRecursiveIterator() as $child) { $child->renderClientRules($fakeBuilder); } if ($fakeBuilder->getValidator()) { $builder->forceValidator(); } } parent::renderClientRules($builder); }