[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: ajax.php 4450 2007-02-05 05:18:05Z phpnut $ */ 3 /** 4 * Helper for AJAX operations. 5 * 6 * Helps doing AJAX using the Prototype library. 7 * 8 * PHP versions 4 and 5 9 * 10 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 11 * Copyright 2005-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs.view.helpers 23 * @since CakePHP(tm) v 0.10.0.1076 24 * @version $Revision: 4450 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-04 23:18:05 -0600 (Sun, 04 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * AjaxHelper library. 31 * 32 * Helps doing AJAX using the Prototype library. 33 * 34 * @package cake 35 * @subpackage cake.cake.libs.view.helpers 36 */ 37 class AjaxHelper extends Helper { 38 /** 39 * Included helpers. 40 * 41 * @var array 42 * @access public 43 */ 44 var $helpers = array('Html', 'Javascript'); 45 /** 46 * Names of Javascript callback functions. 47 * 48 * @var array 49 * @access public 50 */ 51 var $callbacks = array('uninitialized', 'loading', 'loaded', 'interactive', 'complete', 'success', 'failure'); 52 /** 53 * Names of AJAX options. 54 * 55 * @var array 56 * @access public 57 */ 58 var $ajaxOptions = array('type', 'confirm', 'condition', 'before', 'after', 'fallback', 'update', 'loading', 'loaded', 'interactive', 'complete', 'with', 'url', 'method', 'position', 'form', 'parameters', 'evalScripts', 'asynchronous', 'onComplete', 'onUninitialized', 'onLoading', 'onLoaded', 'onInteractive', 'success', 'failure', 'onSuccess', 'onFailure', 'insertion', 'requestHeaders'); 59 /** 60 * Options for draggable. 61 * 62 * @var array 63 * @access public 64 */ 65 var $dragOptions = array('handle', 'revert', 'constraint', 'change', 'ghosting'); 66 /** 67 * Options for droppable. 68 * 69 * @var array 70 * @access public 71 */ 72 var $dropOptions = array('accept', 'containment', 'overlap', 'greedy', 'hoverclass', 'onHover', 'onDrop'); 73 /** 74 * Options for sortable. 75 * 76 * @var array 77 * @access public 78 */ 79 var $sortOptions = array('tag', 'only', 'overlap', 'constraint', 'containment', 'handle', 'hoverclass', 'ghosting', 'dropOnEmpty', 'onUpdate', 'onChange'); 80 /** 81 * Options for slider. 82 * 83 * @var array 84 * @access public 85 */ 86 var $sliderOptions = array('axis', 'increment', 'maximum', 'minimum', 'alignX', 'alignY', 'sliderValue', 'disabled', 'handleImage', 'handleDisabled', 'values', 'onSlide', 'onChange'); 87 /** 88 * Options for in-place editor. 89 * 90 * @var array 91 * @access public 92 */ 93 var $editorOptions = array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'rows', 'cols', 'size', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'callback', 'ajaxOptions', 'clickToEditText'); 94 /** 95 * Options for auto-complete editor. 96 * 97 * @var array 98 * @access public 99 */ 100 var $autoCompleteOptions = array('paramName', 'tokens', 'frequency', 'minChars', 'indicator', 'updateElement', 'afterUpdateElement', 'onShow', 'onHide'); 101 /** 102 * Output buffer for Ajax update content 103 * 104 * @var array 105 * @access private 106 */ 107 var $__ajaxBuffer = array(); 108 /** 109 * Returns link to remote action 110 * 111 * Returns a link to a remote action defined by <i>options[url]</i> 112 * (using the urlFor format) that's called in the background using 113 * XMLHttpRequest. The result of that request can then be inserted into a 114 * DOM object whose id can be specified with <i>options[update]</i>. 115 * 116 * Examples: 117 * <code> 118 * $ajax->link("Delete this post", "/posts/delete/{$post['Post']['id']}" 119 * array("update" => "posts", "loading"=>"Element.show('loading');", "complete"=>"Element.hide('loading');"), 120 * "Are you sure you want to delte this post?"); 121 * $ajax->link($html->img("refresh"), '/emails/refresh', 122 * array("update" => "posts", "loading"=>"Element.show('loading');", "complete"=>"Element.hide('loading');"), 123 * null, false); 124 * </code> 125 * 126 * By default, these remote requests are processed asynchronous during 127 * which various callbacks can be triggered (for progress indicators and 128 * the likes). 129 * 130 * The callbacks that may be specified are: 131 * 132 * - <i>loading</i>:: Called when the remote document is being 133 * loaded with data by the browser. 134 * - <i>loaded</i>:: Called when the browser has finished loading 135 * the remote document. 136 * - <i>interactive</i>:: Called when the user can interact with the 137 * remote document, even though it has not 138 * finished loading. 139 * - <i>complete</i>:: Called when the request is complete. 140 * 141 * If you for some reason or another need synchronous processing (that'll 142 * block the browser while the request is happening), you can specify 143 * <i>$options['type'] = synchronous</i>. 144 * 145 * You can customize further browser side call logic by passing 146 * in Javascript code snippets via some optional parameters. In 147 * their order of use these are: 148 * 149 * - <i>confirm</i> :: Adds confirmation dialog. 150 * - <i>condition</i> :: Perform remote request conditionally 151 * by this expression. Use this to 152 * describe browser-side conditions when 153 * request should not be initiated. 154 * - <i>before</i> :: Called before request is initiated. 155 * - <i>after</i> :: Called immediately after request was 156 * initiated and before <i>loading</i>. 157 * 158 * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.Updater 159 * @param string $title Title of link 160 * @param string $href href string "/products/view/12" 161 * @param array $options Options for JavaScript function 162 * @param string $confirm Confirmation message. Calls up a JavaScript confirm() message. 163 * @param boolean $escapeTitle Escaping the title string to HTML entities 164 * @return HTML code for link to remote action 165 * @access public 166 */ 167 function link($title, $href = null, $options = array(), $confirm = null, $escapeTitle = true) { 168 if (!isset($href)) { 169 $href = $title; 170 } 171 172 if (!isset($options['url'])) { 173 $options['url'] = $href; 174 } 175 176 if (isset($confirm)) { 177 $options['confirm'] = $confirm; 178 unset($confirm); 179 } 180 $htmlOptions = $this->__getHtmlOptions($options); 181 182 if (empty($options['fallback']) || !isset($options['fallback'])) { 183 $options['fallback'] = $href; 184 } 185 186 if (!isset($htmlOptions['id'])) { 187 $htmlOptions['id'] = 'link' . intval(rand()); 188 } 189 190 if (!isset($htmlOptions['onclick'])) { 191 $htmlOptions['onclick'] = ''; 192 } 193 $htmlOptions['onclick'] .= ' return false;'; 194 $return = $this->Html->link($title, $href, $htmlOptions, null, $escapeTitle); 195 $script = $this->Javascript->event("'{$htmlOptions['id']}'", "click", $this->remoteFunction($options)); 196 197 if (is_string($script)) { 198 $return .= $script; 199 } 200 return $return; 201 } 202 /** 203 * Creates JavaScript function for remote AJAX call 204 * 205 * This function creates the javascript needed to make a remote call 206 * it is primarily used as a helper for link. 207 * 208 * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.Updater 209 * @see link() for docs on options parameter. 210 * @param array $options options for javascript 211 * @return string html code for link to remote action 212 * @access public 213 */ 214 function remoteFunction($options = null) { 215 if (isset($options['update'])) { 216 if (!is_array($options['update'])) { 217 $func = "new Ajax.Updater('{$options['update']}',"; 218 } else { 219 $func = "new Ajax.Updater(document.createElement('div'),"; 220 } 221 if (!isset($options['requestHeaders'])) { 222 $options['requestHeaders'] = array(); 223 } 224 if (is_array($options['update'])) { 225 $options['update'] = join(' ', $options['update']); 226 } 227 $options['requestHeaders']['X-Update'] = $options['update']; 228 } else { 229 $func = "new Ajax.Request("; 230 } 231 232 $func .= "'" . $this->Html->url(isset($options['url']) ? $options['url'] : "") . "'"; 233 $func .= ", " . $this->__optionsForAjax($options) . ")"; 234 235 if (isset($options['before'])) { 236 $func = "{$options['before']}; $func"; 237 } 238 239 if (isset($options['after'])) { 240 $func = "$func; {$options['after']};"; 241 } 242 243 if (isset($options['condition'])) { 244 $func = "if ({$options['condition']}) { $func; }"; 245 } 246 247 if (isset($options['confirm'])) { 248 $func = "if (confirm('" . $this->Javascript->escapeString($options['confirm']) 249 . "')) { $func; } else { return false; }"; 250 } 251 return $func; 252 } 253 /** 254 * Periodically call remote url via AJAX. 255 * 256 * Periodically calls the specified url (<i>options['url']</i>) every <i>options['frequency']</i> seconds (default is 10). 257 * Usually used to update a specified div (<i>options['update']</i>) with the results of the remote call. 258 * The options for specifying the target with url and defining callbacks is the same as link. 259 * 260 * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.Updater 261 * @param array $options Callback options 262 * @return string Javascript codeblock 263 * @access public 264 */ 265 function remoteTimer($options = null) { 266 $frequency=(isset($options['frequency'])) ? $options['frequency'] : 10; 267 $code="new PeriodicalExecuter(function() {" . $this->remoteFunction($options) . "}, $frequency)"; 268 return $this->Javascript->codeBlock($code); 269 } 270 /** 271 * Returns form tag that will submit using Ajax. 272 * 273 * Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular 274 * reloading POST arrangement. Even though it's using Javascript to serialize the form elements, the form submission 275 * will work just like a regular submission as viewed by the receiving side (all elements available in params). 276 * The options for defining callbacks is the same as link(). 277 * 278 * @param array $params Form target 279 * @param array $type How form data is posted: 'get' or 'post' 280 * @param array $options Callback/HTML options 281 * @return string JavaScript/HTML code 282 * @access public 283 */ 284 function form($params = null, $type = 'post', $options = array()) { 285 if (is_array($params)) { 286 extract($params, EXTR_OVERWRITE); 287 288 if (!isset($action)) { 289 $action = null; 290 } 291 292 if (!isset($type)) { 293 $type = 'post'; 294 } 295 296 if (!isset($options)) { 297 $options = array(); 298 } 299 } else { 300 $action = $params; 301 } 302 $htmlOptions = $this->__getHtmlOptions($options); 303 $htmlOptions['action'] = $action; 304 305 if (!isset($htmlOptions['id'])) { 306 $htmlOptions['id'] = 'form' . intval(rand()); 307 } 308 $htmlOptions['onsubmit']="return false;"; 309 310 if (!isset($options['with'])) { 311 $options['with'] = "Form.serialize('{$htmlOptions['id']}')"; 312 } 313 $options['url']=$action; 314 315 return $this->Html->formTag($htmlOptions['action'], $type, $htmlOptions) 316 . $this->Javascript->event("'" . $htmlOptions['id']. "'", "submit", $this->remoteFunction($options)); 317 } 318 /** 319 * Returns a button input tag that will submit using Ajax 320 * 321 * Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular 322 * reloading POST arrangement. <i>options</i> argument is the same as in <i>form_remote_tag</i> 323 * 324 * @param string $title Input button title 325 * @param array $options Callback options 326 * @return string Ajaxed input button 327 * @access public 328 */ 329 function submit($title = 'Submit', $options = array()) { 330 $htmlOptions =$this->__getHtmlOptions($options); 331 $htmlOptions['value']=$title; 332 333 if (!isset($options['with'])) { 334 $options['with'] = 'Form.serialize(Event.element(event).form)'; 335 } 336 337 if (!isset($htmlOptions['id'])) { 338 $htmlOptions['id'] = 'submit' . intval(rand()); 339 } 340 $htmlOptions['onclick']="return false;"; 341 return $this->Html->submit($title, $htmlOptions) 342 . $this->Javascript->event('"' . $htmlOptions['id'] . '"', 'click', $this->remoteFunction($options)); 343 } 344 /** 345 * Observe field and call ajax on change. 346 * 347 * Observes the field with the DOM ID specified by <i>field_id</i> and makes 348 * an Ajax when its contents have changed. 349 * 350 * Required +options+ are: 351 * - <i>frequency</i>:: The frequency (in seconds) at which changes to 352 * this field will be detected. 353 * - <i>url</i>:: @see urlFor() -style options for the action to call 354 * when the field has changed. 355 * 356 * Additional options are: 357 * - <i>update</i>:: Specifies the DOM ID of the element whose 358 * innerHTML should be updated with the 359 * XMLHttpRequest response text. 360 * - <i>with</i>:: A Javascript expression specifying the 361 * parameters for the XMLHttpRequest. This defaults 362 * to Form.Element.serialize('$field_id'), which can be 363 * accessed from params['form']['field_id']. 364 * 365 * @see link(). 366 * @param string $field_id DOM ID of field to observe 367 * @param array $options ajax options 368 * @return string ajax script 369 * @access public 370 */ 371 function observeField($field_id, $options = array()) { 372 if (!isset($options['with'])) { 373 $options['with'] = "Form.Element.serialize('$field_id')"; 374 } 375 return $this->Javascript->codeBlock($this->_buildObserver('Form.Element.Observer', $field_id, $options)); 376 } 377 /** 378 * Observe entire form and call ajax on change. 379 * 380 * Like @see observeField(), but operates on an entire form identified by the 381 * DOM ID <b>form_id</b>. <b>options</b> are the same as <b>observe_field</b>, except 382 * the default value of the <i>with</i> option evaluates to the 383 * serialized (request string) value of the form. 384 * 385 * @param string $field_id DOM ID of field to observe 386 * @param array $options ajax options 387 * @return string ajax script 388 * @access public 389 */ 390 function observeForm($field_id, $options = array()) { 391 if (!isset($options['with'])) { 392 $options['with'] = 'Form.serialize("' . $field_id . '")'; 393 } 394 return $this->Javascript->codeBlock($this->_buildObserver('Form.Observer', $field_id, $options)); 395 } 396 /** 397 * Create a text field with Autocomplete. 398 * 399 * Creates an autocomplete field with the given ID and options. 400 * 401 * options['with'] defaults to "Form.Element.serialize('$field_id')", 402 * but can be any valid javascript expression defining the 403 * 404 * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.Autocompleter 405 * @param string $field_id DOM ID of field to observe 406 * @param string $url URL for the autocomplete action 407 * @param array $options Ajax options 408 * @return string Ajax script 409 * @access public 410 */ 411 function autoComplete($field, $url = "", $options = array()) { 412 $var = ''; 413 if (isset($options['var'])) { 414 $var = 'var ' . $options['var'] . ' = '; 415 unset($options['var']); 416 } 417 418 if (!isset($options['id'])) { 419 $options['id'] = Inflector::camelize(r("/", "_", $field)); 420 } 421 $divOptions = array('id' => $options['id'] . "_autoComplete", 'class' => isset($options['class']) ? $options['class'] : 'auto_complete'); 422 423 if (isset($options['div_id'])) { 424 $divOptions['id'] = $options['div_id']; 425 unset($options['div_id']); 426 } 427 $htmlOptions = $this->__getHtmlOptions($options); 428 $htmlOptions['autocomplete'] = "off"; 429 430 foreach ($this->autoCompleteOptions as $opt) { 431 unset($htmlOptions[$opt]); 432 } 433 434 if (isset($options['tokens'])) { 435 if (is_array($options['tokens'])) { 436 $options['tokens'] = $this->Javascript->object($options['tokens']); 437 } else { 438 $options['tokens'] = '"' . $options['tokens'] . '"'; 439 } 440 } 441 $options = $this->_optionsToString($options, array('paramName', 'indicator')); 442 $options = $this->_buildOptions($options, $this->autoCompleteOptions); 443 return $this->Html->input($field, $htmlOptions) . "\n" . 444 $this->Html->tag('div', $divOptions, true) . "</div>\n" . 445 $this->Javascript->codeBlock("{$var}new Ajax.Autocompleter('" . $htmlOptions['id'] 446 . "', '" . $divOptions['id'] . "', '" . $this->Html->url($url) . "', " . 447 $options . ");"); 448 } 449 /** 450 * Setup a Draggable Element. 451 * For a reference on the options for this function, check out 452 * 453 * @link http://wiki.script.aculo.us/scriptaculous/show/Draggable 454 * @param sting $id the DOM id to enable 455 * @param array $options a set of options 456 * @return string Javascript::codeBlock(); 457 * @access public 458 */ 459 function drag($id, $options = array()) { 460 return $this->Javascript->codeBlock("new Draggable('$id', " . $this->_optionsForDraggable($options) . ");"); 461 } 462 /** 463 * Creates an Ajax-updateable DIV element 464 * 465 * @param string $id options for javascript 466 * @param array $options a set of options 467 * @return string HTML code 468 * @access public 469 */ 470 function div($id, $options = array()) { 471 if (env('HTTP_X_UPDATE') != null) { 472 $divs = explode(' ', env('HTTP_X_UPDATE')); 473 if (in_array($id, $divs)) { 474 @ob_end_clean(); 475 ob_start(); 476 return ''; 477 } 478 } 479 $attr = $this->Html->_parseAttributes(am($options, array('id' => $id))); 480 return $this->output(sprintf($this->tags['blockstart'], $attr)); 481 } 482 /** 483 * Closes an Ajax-updateable DIV element 484 * 485 * @param string $id The DOM ID of the element 486 * @return string HTML code 487 * @access public 488 */ 489 function divEnd($id) { 490 if (env('HTTP_X_UPDATE') != null) { 491 $divs = explode(' ', env('HTTP_X_UPDATE')); 492 if (in_array($id, $divs)) { 493 $this->__ajaxBuffer[$id] = ob_get_contents(); 494 ob_end_clean(); 495 return ''; 496 } 497 } 498 return $this->output($this->tags['blockend']); 499 } 500 /** 501 * Protectd helper method to return an array of options for draggable. 502 * 503 * @param array $options 504 * @return array 505 * @access protected 506 */ 507 function _optionsForDraggable($options) { 508 $options = $this->_optionsToString($options, array('handle', 'constraint')); 509 return $this->_buildOptions($options, $this->dragOptions); 510 } 511 /** 512 * Setup a droppable element 513 * For a reference on the options for this function, check out 514 * 515 * @link http://wiki.script.aculo.us/scriptaculous/show/Droppables.add 516 * @param string $id 517 * @param array $options 518 * @return array 519 * @access public 520 */ 521 function drop($id, $options = array()) { 522 $options = $this->_optionsForDroppable($options); 523 return $this->Javascript->codeBlock("Droppables.add('$id', $options);"); 524 } 525 /** 526 * Protected helper method to return an array of options for droppable. 527 * 528 * @param string $options 529 * @return string String of Javascript array definition 530 * @access protected 531 */ 532 function _optionsForDroppable($options) { 533 $options = $this->_optionsToString($options, array('accept', 'overlap', 'hoverclass')); 534 return $this->_buildOptions($options, $this->dropOptions); 535 } 536 /** 537 * Setup a remote droppable element. 538 * 539 * @link http://wiki.script.aculo.us/scriptaculous/show/Droppables.add 540 * @see link() and remoteFunction() 541 * @param string $id DOM id of droppable 542 * @param array $options ame as drop() 543 * @param array $ajaxOptions same as remoteFunction() 544 * @return string Javascript::codeBlock() 545 * @access public 546 */ 547 function dropRemote($id, $options = array(), $ajaxOptions = array()) { 548 $options['onDrop'] = "function(element){" . $this->remoteFunction($ajaxOptions) . "}"; 549 $options = $this->_optionsForDroppable($options); 550 return $this->Javascript->codeBlock("Droppables.add('$id', $options);"); 551 } 552 /** 553 * Makes a slider control. 554 * 555 * @link http://wiki.script.aculo.us/scriptaculous/show/Slider 556 * @param string $id DOM ID of slider handle 557 * @param string $track_id DOM ID of slider track 558 * @param array $options Array of options to control the slider 559 * @return string Javascript::codeBlock() 560 * @access public 561 */ 562 function slider($id, $track_id, $options = array()) { 563 $options = $this->_optionsToString($options, array('axis', 'handleImage', 'handleDisabled')); 564 565 if (isset($options['change'])) { 566 $options['onChange'] = $options['change']; 567 unset($options['change']); 568 } 569 570 if (isset($options['slide'])) { 571 $options['onSlide'] = $options['slide']; 572 unset($options['slide']); 573 } 574 575 $options = $this->_buildOptions($options, $this->sliderOptions); 576 return $this->Javascript->codeBlock("var $id = new Control.Slider('$id', '$track_id', $options);"); 577 } 578 /** 579 * Makes an Ajax In Place editor control. 580 * 581 * @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor 582 * @param string $id DOM ID of input element 583 * @param string $url Postback URL of saved data 584 * @param array $options Array of options to control the editor, including ajaxOptions (see link). 585 * @return string Javascript::codeBlock() 586 * @access public 587 */ 588 function editor($id, $url, $options = array()) { 589 $url = $this->Html->url($url); 590 $options['ajaxOptions'] = $this->__optionsForAjax($options); 591 592 foreach($this->ajaxOptions as $opt) { 593 if (isset($options[$opt])) { 594 unset($options[$opt]); 595 } 596 } 597 598 if (isset($options['callback'])) { 599 $options['callback'] = 'function(form, value) {' . $options['callback'] . '}'; 600 } 601 602 $options = $this->_optionsToString($options, array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'clickToEditText')); 603 $options = $this->_buildOptions($options, $this->editorOptions); 604 return $this->Javascript->codeBlock("new Ajax.InPlaceEditor('{$id}', '{$url}', {$options});"); 605 } 606 /** 607 * Makes a list or group of floated objects sortable. 608 * 609 * @link http://wiki.script.aculo.us/scriptaculous/show/Sortable.create 610 * @param string $id DOM ID of parent 611 * @param array $options Array of options to control sort 612 * @return string Javascript::codeBlock() 613 * @access public 614 */ 615 function sortable($id, $options = array()) { 616 if (!empty($options['url'])) { 617 $options['with'] = "Sortable.serialize('$id')"; 618 $options['onUpdate'] = 'function(sortable){' . $this->remoteFunction($options) . '}'; 619 } 620 621 $options = $this->__optionsForSortable($options); 622 return $this->Javascript->codeBlock("Sortable.create('$id', $options);"); 623 } 624 /** 625 * Private method; generates sortables code from array options 626 * 627 * @param array $options 628 * @return string String of Javascript array definition 629 * @access private 630 */ 631 function __optionsForSortable($options) { 632 $options = $this->_optionsToString($options, array('handle', 'tag', 'constraint', 'ghosting', 'only', 'hoverclass')); 633 return $this->_buildOptions($options, $this->sortOptions); 634 } 635 /** 636 * Private helper function for Ajax. 637 * 638 * @param array $options 639 * @return string String of Javascript array definition 640 * @access private 641 */ 642 function __optionsForAjax($options = array()) { 643 644 if (isset($options['indicator'])) { 645 if (isset($options['loading'])) { 646 $options['loading'] .= "Element.show('{$options['indicator']}');"; 647 } else { 648 $options['loading'] = "Element.show('{$options['indicator']}');"; 649 } 650 if (isset($options['complete'])) { 651 $options['complete'] .= "Element.hide('{$options['indicator']}');"; 652 } else { 653 $options['complete'] = "Element.hide('{$options['indicator']}');"; 654 } 655 unset($options['indicator']); 656 } 657 658 $jsOptions = am( 659 array('asynchronous' => 'true', 'evalScripts' => 'true'), 660 $this->_buildCallbacks($options) 661 ); 662 $options = $this->_optionsToString($options, array('method')); 663 664 foreach($options as $key => $value) { 665 switch($key) { 666 case 'type': 667 $jsOptions['asynchronous'] = ife(($value == 'synchronous'), 'false', 'true'); 668 break; 669 case 'evalScripts': 670 $jsOptions['evalScripts'] = ife($value, 'true', 'false'); 671 break; 672 case 'position': 673 $jsOptions['insertion'] = "Insertion." . Inflector::camelize($options['position']); 674 break; 675 case 'with': 676 $jsOptions['parameters'] = $options['with']; 677 break; 678 case 'form': 679 $jsOptions['parameters'] = 'Form.serialize(this)'; 680 break; 681 case 'requestHeaders': 682 $keys = array(); 683 foreach ($value as $key => $val) { 684 $keys[] = "'" . $key . "'"; 685 $keys[] = "'" . $val . "'"; 686 } 687 $jsOptions['requestHeaders'] = '[' . join(', ', $keys) . ']'; 688 break; 689 } 690 } 691 return $this->_buildOptions($jsOptions, $this->ajaxOptions); 692 } 693 /** 694 * Private Method to return a string of html options 695 * option data as a JavaScript options hash. 696 * 697 * @param array $options Options in the shape of keys and values 698 * @param array $extra Array of legal keys in this options context 699 * @return array Array of html options 700 * @access private 701 */ 702 function __getHtmlOptions($options, $extra = array()) { 703 foreach($this->ajaxOptions as $key) { 704 if (isset($options[$key])) { 705 unset($options[$key]); 706 } 707 } 708 709 foreach($extra as $key) { 710 if (isset($options[$key])) { 711 unset($options[$key]); 712 } 713 } 714 return $options; 715 } 716 /** 717 * Protected Method to return a string of JavaScript with the given 718 * option data as a JavaScript options hash. 719 * 720 * @param array $options Options in the shape of keys and values 721 * @param array $acceptable Array of legal keys in this options context 722 * @return string String of Javascript array definition 723 * @access protected 724 */ 725 function _buildOptions($options, $acceptable) { 726 if (is_array($options)) { 727 $out = array(); 728 729 foreach($options as $k => $v) { 730 if (in_array($k, $acceptable)) { 731 $out[] = "$k:$v"; 732 } 733 } 734 735 $out = join(', ', $out); 736 $out = '{' . $out . '}'; 737 return $out; 738 } else { 739 return false; 740 } 741 } 742 /** 743 * Protected Method to return JavaScript text for an observer... 744 * 745 * @param string $klass Name of JavaScript class 746 * @param string $name 747 * @param array $options Ajax options 748 * @return string Formatted JavaScript 749 * @access protected 750 */ 751 function _buildObserver($klass, $name, $options = null) { 752 if (!isset($options['with']) && isset($options['update'])) { 753 $options['with'] = 'value'; 754 } 755 756 $callback = $this->remoteFunction($options); 757 $javascript = "new $klass('$name', "; 758 $javascript .= (isset($options['frequency']) ? $options['frequency'] : 2) . ", function(element, value) {"; 759 $javascript .= "$callback})"; 760 return $javascript; 761 } 762 /** 763 * Protected Method to return JavaScript text for all callbacks... 764 * 765 * @param array $options 766 * @return array 767 * @access protected 768 */ 769 function _buildCallbacks($options) { 770 $callbacks = array(); 771 772 foreach($this->callbacks as $callback) { 773 if (isset($options[$callback])) { 774 $name = 'on' . ucfirst($callback); 775 $code = $options[$callback]; 776 $callbacks[$name] = "function(request){" . $code . "}"; 777 } 778 } 779 return $callbacks; 780 } 781 /** 782 * Protected Method to return a string of JavaScript with a string representation 783 * of given options array. 784 * 785 * @param array $options Ajax options array 786 * @param array $stringOpts Options as strings in an array 787 * @access private 788 * @return array 789 * @access protected 790 */ 791 function _optionsToString($options, $stringOpts = array()) { 792 foreach($stringOpts as $option) { 793 if (isset($options[$option]) && !$options[$option][0] != "'") { 794 if ($options[$option] === true || $options[$option] === 'true') { 795 $options[$option] = 'true'; 796 } elseif ($options[$option] === false || $options[$option] === 'false') { 797 $options[$option] = 'false'; 798 } else { 799 $options[$option] = "'{$options[$option]}'"; 800 } 801 } 802 } 803 return $options; 804 } 805 /** 806 * afterRender callback 807 * 808 * @return array 809 * @access public 810 */ 811 function afterRender() { 812 if (env('HTTP_X_UPDATE') != null && count($this->__ajaxBuffer) > 0) { 813 $data = array(); 814 $divs = explode(' ', env('HTTP_X_UPDATE')); 815 816 foreach ($this->__ajaxBuffer as $key => $val) { 817 if (in_array($key, $divs)) { 818 $data[] = $key . ':"' . rawurlencode($val) . '"'; 819 } 820 } 821 822 $out = 'var __ajaxUpdater__ = {' . join(', ', $data) . '};' . "\n"; 823 $out .= 'for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string" && $(n)) Element.update($(n), unescape(__ajaxUpdater__[n])); }'; 824 825 @ob_end_clean(); 826 e($this->Javascript->codeBlock($out)); 827 exit(); 828 } 829 } 830 } 831 /** 832 * Replaced by AjaxHelper::link() 833 * 834 * @deprecated will not be avialable after 1.1.x.x 835 */ 836 function linkToRemote($title, $options = array(), $html_options = array()) { 837 //trigger_error('Deprecated function: use AjaxHelper::link', E_USER_WARNING); 838 $href = '#'; 839 840 if (!empty($options['fallback']) && isset($options['fallback'])) { 841 $href = $options['fallback']; 842 } 843 844 if (isset($html_options['id'])) { 845 return $this->Html->link($title, $href, $html_options) . 846 $this->Javascript->event("$('{$html_options['id']}')", "click", $this->remoteFunction($options)); 847 } else { 848 $html_options['onclick'] = $this->remoteFunction($options) . "; return false;"; 849 return $this->Html->link($title, $href, $html_options); 850 } 851 } 852 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |