2005-06-21 23:44:49 +00:00
|
|
|
<?php
|
2005-08-21 06:49:02 +00:00
|
|
|
/* SVN FILE: $Id$ */
|
2005-06-21 23:44:49 +00:00
|
|
|
|
2005-07-04 04:16:20 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* Helper for AJAX operations.
|
2005-08-22 04:15:57 +00:00
|
|
|
*
|
2005-09-07 01:52:45 +00:00
|
|
|
* Helps doing AJAX using the Prototype library.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-02-02 10:39:45 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2006-05-26 05:29:17 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-08-22 04:15:57 +00:00
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-02-02 10:39:45 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2007-02-02 10:39:45 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2006-05-26 05:29:17 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-07-04 04:16:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AjaxHelper helper library.
|
2005-08-22 04:15:57 +00:00
|
|
|
*
|
2005-09-07 01:52:45 +00:00
|
|
|
* Helps doing AJAX using the Prototype library.
|
2005-07-04 04:16:20 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2005-07-04 04:16:20 +00:00
|
|
|
*/
|
2006-10-27 21:40:34 +00:00
|
|
|
class AjaxHelper extends AppHelper {
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Included helpers.
|
|
|
|
*
|
|
|
|
* @var array
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-12-30 14:31:35 +00:00
|
|
|
var $helpers = array('Html', 'Javascript', 'Form');
|
2007-12-23 21:08:54 +00:00
|
|
|
/**
|
|
|
|
* HtmlHelper instance
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-11-25 08:57:27 +00:00
|
|
|
var $Html = null;
|
2007-12-23 21:08:54 +00:00
|
|
|
/**
|
|
|
|
* JavaScriptHelper instance
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-11-25 08:57:27 +00:00
|
|
|
var $Javascript = null;
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Names of Javascript callback functions.
|
|
|
|
*
|
|
|
|
* @var array
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2007-12-23 21:08:54 +00:00
|
|
|
var $callbacks = array('complete', 'create', 'exception', 'failure', 'interactive', 'loading', 'loaded', 'success', 'uninitialized');
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Names of AJAX options.
|
|
|
|
*
|
|
|
|
* @var array
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2008-01-22 07:47:56 +00:00
|
|
|
var $ajaxOptions = array('after', 'asynchronous', 'before', 'confirm', 'condition', 'contentType', 'encoding', 'evalScripts', 'failure', 'fallback', 'form', 'indicator', 'insertion', 'interactive', 'loaded', 'loading', 'method', 'onCreate', 'onComplete', 'onException', 'onFailure', 'onInteractive', 'onLoaded', 'onLoading', 'onSuccess', 'onUninitialized', 'parameters', 'position', 'postBody', 'requestHeaders', 'success', 'type', 'update', 'with');
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Options for draggable.
|
|
|
|
*
|
|
|
|
* @var array
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-06-16 19:02:30 +00:00
|
|
|
var $dragOptions = array('handle', 'revert', 'snap', 'zindex', 'constraint', 'change', 'ghosting', 'starteffect', 'reverteffect', 'endeffect');
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Options for droppable.
|
|
|
|
*
|
|
|
|
* @var array
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2007-12-24 01:45:39 +00:00
|
|
|
var $dropOptions = array('accept', 'containment', 'greedy', 'hoverclass', 'onHover', 'onDrop', 'overlap');
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Options for sortable.
|
|
|
|
*
|
|
|
|
* @var array
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2007-12-23 21:08:54 +00:00
|
|
|
var $sortOptions = array('constraint', 'containment', 'dropOnEmpty', 'ghosting', 'handle', 'hoverclass', 'onUpdate', 'onChange', 'only', 'overlap', 'scroll', 'scrollSensitivity', 'scrollSpeed', 'tag', 'tree', 'treeTag', 'update');
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Options for slider.
|
|
|
|
*
|
|
|
|
* @var array
|
2006-02-07 02:19:53 +00:00
|
|
|
*/
|
2007-12-24 01:45:39 +00:00
|
|
|
var $sliderOptions = array('alignX', 'alignY', 'axis', 'disabled', 'handleDisabled', 'handleImage', 'increment', 'maximum', 'minimum', 'onChange', 'onSlide', 'range', 'sliderValue', 'values');
|
2006-02-08 15:25:34 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Options for in-place editor.
|
|
|
|
*
|
|
|
|
* @var array
|
2006-02-08 15:25:34 +00:00
|
|
|
*/
|
2006-06-16 19:02:30 +00:00
|
|
|
var $editorOptions = array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'rows', 'cols', 'size', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'callback', 'ajaxOptions', 'clickToEditText', 'collection');
|
2006-06-18 19:21:09 +00:00
|
|
|
/**
|
|
|
|
* Options for auto-complete editor.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2007-12-24 01:45:39 +00:00
|
|
|
var $autoCompleteOptions = array('afterUpdateElement', 'callback', 'frequency', 'indicator', 'minChars', 'onShow', 'onHide', 'parameters', 'paramName', 'tokens', 'updateElement');
|
2006-05-26 05:29:17 +00:00
|
|
|
/**
|
|
|
|
* Output buffer for Ajax update content
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $__ajaxBuffer = array();
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2005-12-27 03:33:44 +00:00
|
|
|
* Returns link to remote action
|
|
|
|
*
|
|
|
|
* Returns a link to a remote action defined by <i>options[url]</i>
|
2006-06-17 13:57:44 +00:00
|
|
|
* (using the url() format) that's called in the background using
|
2005-12-27 03:33:44 +00:00
|
|
|
* XMLHttpRequest. The result of that request can then be inserted into a
|
|
|
|
* DOM object whose id can be specified with <i>options[update]</i>.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* <code>
|
2006-01-12 04:08:19 +00:00
|
|
|
* link("Delete this post",
|
2006-05-26 05:29:17 +00:00
|
|
|
* array("update" => "posts", "url" => "delete/{$postid->id}"));
|
2006-01-12 04:08:19 +00:00
|
|
|
* link(imageTag("refresh"),
|
2006-05-26 05:29:17 +00:00
|
|
|
* array("update" => "emails", "url" => "list_emails" ));
|
2005-12-27 03:33:44 +00:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* By default, these remote requests are processed asynchronous during
|
|
|
|
* which various callbacks can be triggered (for progress indicators and
|
|
|
|
* the likes).
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
2006-05-26 05:29:17 +00:00
|
|
|
* link (word,
|
|
|
|
* array("url" => "undo", "n" => word_counter),
|
|
|
|
* array("complete" => "undoRequestCompleted(request)"));
|
2005-12-27 03:33:44 +00:00
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* The callbacks that may be specified are:
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* - <i>loading</i>:: Called when the remote document is being
|
|
|
|
* loaded with data by the browser.
|
|
|
|
* - <i>loaded</i>:: Called when the browser has finished loading
|
|
|
|
* the remote document.
|
|
|
|
* - <i>interactive</i>:: Called when the user can interact with the
|
|
|
|
* remote document, even though it has not
|
|
|
|
* finished loading.
|
|
|
|
* - <i>complete</i>:: Called when the XMLHttpRequest is complete.
|
2005-12-27 03:33:44 +00:00
|
|
|
*
|
|
|
|
* If you for some reason or another need synchronous processing (that'll
|
|
|
|
* block the browser while the request is happening), you can specify
|
|
|
|
* <i>options[type] = synchronous</i>.
|
|
|
|
*
|
|
|
|
* You can customize further browser side call logic by passing
|
|
|
|
* in Javascript code snippets via some optional parameters. In
|
|
|
|
* their order of use these are:
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* - <i>confirm</i>:: Adds confirmation dialog.
|
|
|
|
* -<i>condition</i>:: Perform remote request conditionally
|
|
|
|
* by this expression. Use this to
|
|
|
|
* describe browser-side conditions when
|
|
|
|
* request should not be initiated.
|
|
|
|
* - <i>before</i>:: Called before request is initiated.
|
|
|
|
* - <i>after</i>:: Called immediately after request was
|
|
|
|
* initiated and before <i>loading</i>.
|
|
|
|
*
|
|
|
|
* @param string $title Title of link
|
|
|
|
* @param string $href Href string "/products/view/12"
|
|
|
|
* @param array $options Options for JavaScript function
|
|
|
|
* @param string $confirm Confirmation message. Calls up a JavaScript confirm() message.
|
2007-10-22 16:54:36 +00:00
|
|
|
* @param boolean $escapeTitle Escaping the title string to HTML entities
|
2006-02-07 02:19:53 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @return string HTML code for link to remote action
|
|
|
|
*/
|
|
|
|
function link($title, $href = null, $options = array(), $confirm = null, $escapeTitle = true) {
|
|
|
|
if (!isset($href)) {
|
|
|
|
$href = $title;
|
|
|
|
}
|
|
|
|
if (!isset($options['url'])) {
|
|
|
|
$options['url'] = $href;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($confirm)) {
|
|
|
|
$options['confirm'] = $confirm;
|
|
|
|
unset($confirm);
|
|
|
|
}
|
2008-04-16 22:17:11 +00:00
|
|
|
$htmlOptions = $this->__getHtmlOptions($options, array('url'));
|
2006-05-26 05:29:17 +00:00
|
|
|
|
|
|
|
if (empty($options['fallback']) || !isset($options['fallback'])) {
|
|
|
|
$options['fallback'] = $href;
|
|
|
|
}
|
2008-03-12 03:57:54 +00:00
|
|
|
$htmlOptions = array_merge(array('id' => 'link' . intval(rand()), 'onclick' => ''), $htmlOptions);
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-10-19 07:41:42 +00:00
|
|
|
$htmlOptions['onclick'] .= ' event.returnValue = false; return false;';
|
2006-05-26 05:29:17 +00:00
|
|
|
$return = $this->Html->link($title, $href, $htmlOptions, null, $escapeTitle);
|
|
|
|
$script = $this->Javascript->event("'{$htmlOptions['id']}'", "click", $this->remoteFunction($options));
|
|
|
|
|
|
|
|
if (is_string($script)) {
|
|
|
|
$return .= $script;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Creates JavaScript function for remote AJAX call
|
|
|
|
*
|
|
|
|
* This function creates the javascript needed to make a remote call
|
2006-11-25 08:57:27 +00:00
|
|
|
* it is primarily used as a helper for AjaxHelper::link.
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
|
|
|
* @param array $options options for javascript
|
|
|
|
* @return string html code for link to remote action
|
2008-05-18 19:51:30 +00:00
|
|
|
* @see AjaxHelper::link() for docs on options parameter.
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2008-05-18 19:51:30 +00:00
|
|
|
function remoteFunction($options) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (isset($options['update'])) {
|
|
|
|
if (!is_array($options['update'])) {
|
|
|
|
$func = "new Ajax.Updater('{$options['update']}',";
|
|
|
|
} else {
|
|
|
|
$func = "new Ajax.Updater(document.createElement('div'),";
|
|
|
|
}
|
|
|
|
if (!isset($options['requestHeaders'])) {
|
|
|
|
$options['requestHeaders'] = array();
|
|
|
|
}
|
|
|
|
if (is_array($options['update'])) {
|
|
|
|
$options['update'] = join(' ', $options['update']);
|
|
|
|
}
|
|
|
|
$options['requestHeaders']['X-Update'] = $options['update'];
|
|
|
|
} else {
|
|
|
|
$func = "new Ajax.Request(";
|
|
|
|
}
|
|
|
|
|
2006-08-30 16:23:24 +00:00
|
|
|
$func .= "'" . $this->url(isset($options['url']) ? $options['url'] : "") . "'";
|
2006-05-26 05:29:17 +00:00
|
|
|
$func .= ", " . $this->__optionsForAjax($options) . ")";
|
|
|
|
|
|
|
|
if (isset($options['before'])) {
|
|
|
|
$func = "{$options['before']}; $func";
|
|
|
|
}
|
|
|
|
if (isset($options['after'])) {
|
|
|
|
$func = "$func; {$options['after']};";
|
|
|
|
}
|
|
|
|
if (isset($options['condition'])) {
|
|
|
|
$func = "if ({$options['condition']}) { $func; }";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['confirm'])) {
|
|
|
|
$func = "if (confirm('" . $this->Javascript->escapeString($options['confirm'])
|
2007-10-19 07:41:42 +00:00
|
|
|
. "')) { $func; } else { event.returnValue = false; return false; }";
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
return $func;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Periodically call remote url via AJAX.
|
|
|
|
*
|
|
|
|
* Periodically calls the specified url (<i>options[url]</i>) every <i>options[frequency]</i> seconds (default is 10).
|
|
|
|
* Usually used to update a specified div (<i>options[update]</i>) with the results of the remote call.
|
|
|
|
* The options for specifying the target with url and defining callbacks is the same as linkToRemote.
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $options Callback options
|
|
|
|
* @return string Javascript code
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function remoteTimer($options = null) {
|
2006-06-14 23:24:54 +00:00
|
|
|
$frequency = (isset($options['frequency'])) ? $options['frequency'] : 10;
|
|
|
|
$code = "new PeriodicalExecuter(function() {" . $this->remoteFunction($options) . "}, $frequency)";
|
2006-05-26 05:29:17 +00:00
|
|
|
return $this->Javascript->codeBlock($code);
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns form tag that will submit using Ajax.
|
|
|
|
*
|
|
|
|
* Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular
|
|
|
|
* reloading POST arrangement. Even though it's using Javascript to serialize the form elements, the form submission
|
|
|
|
* will work just like a regular submission as viewed by the receiving side (all elements available in params).
|
2006-03-28 02:44:55 +00:00
|
|
|
* The options for defining callbacks is the same as link().
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $params Form target
|
|
|
|
* @param array $type How form data is posted: 'get' or 'post'
|
|
|
|
* @param array $options Callback/HTML options
|
|
|
|
* @return string JavaScript/HTML code
|
|
|
|
*/
|
|
|
|
function form($params = null, $type = 'post', $options = array()) {
|
2008-01-22 07:47:56 +00:00
|
|
|
$model = false;
|
2006-05-26 05:29:17 +00:00
|
|
|
if (is_array($params)) {
|
|
|
|
extract($params, EXTR_OVERWRITE);
|
2008-01-22 07:47:56 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2008-01-22 07:47:56 +00:00
|
|
|
if (empty($options['url'])) {
|
|
|
|
$options['url'] = array('action' => $params);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2008-01-22 07:47:56 +00:00
|
|
|
|
2008-05-18 19:51:30 +00:00
|
|
|
$htmlOptions = array_merge(array(
|
|
|
|
'id' => 'form' . intval(rand()), 'onsubmit' => "event.returnValue = false; return false;",
|
|
|
|
'type' => $type),
|
2008-01-22 07:47:56 +00:00
|
|
|
$this->__getHtmlOptions($options, array('model', 'with'))
|
2007-02-03 18:38:22 +00:00
|
|
|
);
|
2008-01-22 07:47:56 +00:00
|
|
|
|
2008-05-18 19:51:30 +00:00
|
|
|
$options = array_merge(array('model' => $model,'with' => "Form.serialize('{$htmlOptions['id']}')"), $options);
|
2007-02-03 18:38:22 +00:00
|
|
|
return $this->Form->create($options['model'], $htmlOptions)
|
2007-01-05 19:21:46 +00:00
|
|
|
. $this->Javascript->event("'" . $htmlOptions['id']. "'", 'submit', $this->remoteFunction($options));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Returns a button input tag that will submit using Ajax
|
|
|
|
*
|
|
|
|
* Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular
|
|
|
|
* reloading POST arrangement. <i>options</i> argument is the same as in <i>form_remote_tag</i>
|
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param string $title Input button title
|
|
|
|
* @param array $options Callback options
|
|
|
|
* @return string Ajaxed input button
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function submit($title = 'Submit', $options = array()) {
|
2007-02-26 18:58:18 +00:00
|
|
|
$htmlOptions = $this->__getHtmlOptions($options);
|
|
|
|
$htmlOptions['value'] = $title;
|
2006-01-12 04:08:19 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!isset($options['with'])) {
|
2007-02-26 18:58:18 +00:00
|
|
|
$options['with'] = 'Form.serialize(Event.element(event).form)';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
if (!isset($htmlOptions['id'])) {
|
2007-02-26 18:58:18 +00:00
|
|
|
$htmlOptions['id'] = 'submit' . intval(rand());
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 04:08:19 +00:00
|
|
|
|
2007-10-19 07:41:42 +00:00
|
|
|
$htmlOptions['onclick'] = "event.returnValue = false; return false;";
|
2007-03-05 17:53:43 +00:00
|
|
|
return $this->Form->submit($title, $htmlOptions)
|
2007-02-26 18:58:18 +00:00
|
|
|
. $this->Javascript->event('"' . $htmlOptions['id'] . '"', 'click', $this->remoteFunction($options));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Observe field and call ajax on change.
|
|
|
|
*
|
2007-12-24 01:45:39 +00:00
|
|
|
* Observes the field with the DOM ID specified by <i>field</i> and makes
|
2006-02-18 23:42:21 +00:00
|
|
|
* an Ajax when its contents have changed.
|
|
|
|
*
|
|
|
|
* Required +options+ are:
|
|
|
|
* - <i>frequency</i>:: The frequency (in seconds) at which changes to
|
2006-05-26 05:29:17 +00:00
|
|
|
* this field will be detected.
|
2006-06-17 13:57:44 +00:00
|
|
|
* - <i>url</i>:: @see url() -style options for the action to call
|
2006-05-26 05:29:17 +00:00
|
|
|
* when the field has changed.
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
|
|
|
* Additional options are:
|
2006-05-26 05:29:17 +00:00
|
|
|
* - <i>update</i>:: Specifies the DOM ID of the element whose
|
|
|
|
* innerHTML should be updated with the
|
|
|
|
* XMLHttpRequest response text.
|
|
|
|
* - <i>with</i>:: A Javascript expression specifying the
|
|
|
|
* parameters for the XMLHttpRequest. This defaults
|
2007-12-24 01:45:39 +00:00
|
|
|
* to Form.Element.serialize('$field'), which can be
|
2006-05-26 05:29:17 +00:00
|
|
|
* accessed from params['form']['field_id'].
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
|
|
|
* Additionally, you may specify any of the options documented in
|
|
|
|
* @see linkToRemote().
|
|
|
|
*
|
2007-12-24 01:45:39 +00:00
|
|
|
* @param string $field DOM ID of field to observe
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $options ajax options
|
|
|
|
* @return string ajax script
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-12-24 01:45:39 +00:00
|
|
|
function observeField($field, $options = array()) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!isset($options['with'])) {
|
2007-12-24 01:45:39 +00:00
|
|
|
$options['with'] = 'Form.Element.serialize(\'' . $field . '\')';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-12-24 01:45:39 +00:00
|
|
|
$observer = 'Observer';
|
2006-07-23 16:10:35 +00:00
|
|
|
if (!isset($options['frequency']) || intval($options['frequency']) == 0) {
|
2007-12-24 01:45:39 +00:00
|
|
|
$observer = 'EventObserver';
|
2006-07-23 16:10:35 +00:00
|
|
|
}
|
2007-12-24 01:45:39 +00:00
|
|
|
return $this->Javascript->codeBlock($this->_buildObserver('Form.Element.' . $observer, $field, $options));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Observe entire form and call ajax on change.
|
|
|
|
*
|
|
|
|
* Like @see observeField(), but operates on an entire form identified by the
|
2007-12-24 01:45:39 +00:00
|
|
|
* DOM ID <b>form</b>. <b>options</b> are the same as <b>observeField</b>, except
|
2006-02-18 23:42:21 +00:00
|
|
|
* the default value of the <i>with</i> option evaluates to the
|
|
|
|
* serialized (request string) value of the form.
|
|
|
|
*
|
2007-12-24 01:45:39 +00:00
|
|
|
* @param string $form DOM ID of form to observe
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $options ajax options
|
|
|
|
* @return string ajax script
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2007-12-24 01:45:39 +00:00
|
|
|
function observeForm($form, $options = array()) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!isset($options['with'])) {
|
2007-12-24 01:45:39 +00:00
|
|
|
$options['with'] = 'Form.serialize(\'' . $form . '\')';
|
|
|
|
}
|
|
|
|
$observer = 'Observer';
|
|
|
|
if (!isset($options['frequency']) || intval($options['frequency']) == 0) {
|
|
|
|
$observer = 'EventObserver';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-12-24 01:45:39 +00:00
|
|
|
return $this->Javascript->codeBlock($this->_buildObserver('Form.' . $observer, $form, $options));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Create a text field with Autocomplete.
|
|
|
|
*
|
|
|
|
* Creates an autocomplete field with the given ID and options.
|
|
|
|
*
|
2007-12-24 01:45:39 +00:00
|
|
|
* options['with'] defaults to "Form.Element.serialize('$field')",
|
2006-02-18 23:42:21 +00:00
|
|
|
* but can be any valid javascript expression defining the
|
|
|
|
*
|
2007-12-24 01:45:39 +00:00
|
|
|
* @param string $field DOM ID of field to observe
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param string $url URL for the autocomplete action
|
|
|
|
* @param array $options Ajax options
|
|
|
|
* @return string Ajax script
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function autoComplete($field, $url = "", $options = array()) {
|
2006-06-18 19:21:09 +00:00
|
|
|
$var = '';
|
|
|
|
if (isset($options['var'])) {
|
|
|
|
$var = 'var ' . $options['var'] . ' = ';
|
|
|
|
unset($options['var']);
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (!isset($options['id'])) {
|
2007-12-24 01:45:39 +00:00
|
|
|
$options['id'] = Inflector::camelize(str_replace(".", "_", $field));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2005-09-19 17:20:24 +00:00
|
|
|
|
2006-06-18 19:21:09 +00:00
|
|
|
$divOptions = array('id' => $options['id'] . "_autoComplete", 'class' => isset($options['class']) ? $options['class'] : 'auto_complete');
|
|
|
|
if (isset($options['div_id'])) {
|
|
|
|
$divOptions['id'] = $options['div_id'];
|
|
|
|
unset($options['div_id']);
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$htmlOptions = $this->__getHtmlOptions($options);
|
|
|
|
$htmlOptions['autocomplete'] = "off";
|
2005-09-19 17:20:24 +00:00
|
|
|
|
2006-06-18 19:21:09 +00:00
|
|
|
foreach ($this->autoCompleteOptions as $opt) {
|
|
|
|
unset($htmlOptions[$opt]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['tokens'])) {
|
|
|
|
if (is_array($options['tokens'])) {
|
|
|
|
$options['tokens'] = $this->Javascript->object($options['tokens']);
|
|
|
|
} else {
|
|
|
|
$options['tokens'] = '"' . $options['tokens'] . '"';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2005-10-03 04:48:00 +00:00
|
|
|
|
2006-06-18 19:21:09 +00:00
|
|
|
$options = $this->_optionsToString($options, array('paramName', 'indicator'));
|
|
|
|
$options = $this->_buildOptions($options, $this->autoCompleteOptions);
|
2006-01-12 04:08:19 +00:00
|
|
|
|
2007-03-14 20:47:39 +00:00
|
|
|
return $this->Form->text($field, $htmlOptions) . "\n" .
|
2006-07-11 14:34:35 +00:00
|
|
|
$this->Html->div(null, '', $divOptions) . "\n" .
|
2006-06-18 19:21:09 +00:00
|
|
|
$this->Javascript->codeBlock("{$var}new Ajax.Autocompleter('" . $htmlOptions['id']
|
|
|
|
. "', '" . $divOptions['id'] . "', '" . $this->Html->url($url) . "', " .
|
|
|
|
$options . ");");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Creates an Ajax-updateable DIV element
|
|
|
|
*
|
|
|
|
* @param string $id options for javascript
|
|
|
|
* @return string HTML code
|
|
|
|
*/
|
|
|
|
function div($id, $options = array()) {
|
|
|
|
if (env('HTTP_X_UPDATE') != null) {
|
2007-02-21 22:13:16 +00:00
|
|
|
$this->Javascript->enabled = false;
|
2006-05-26 05:29:17 +00:00
|
|
|
$divs = explode(' ', env('HTTP_X_UPDATE'));
|
2007-02-21 22:13:16 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
if (in_array($id, $divs)) {
|
|
|
|
@ob_end_clean();
|
|
|
|
ob_start();
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2007-12-08 06:08:03 +00:00
|
|
|
$attr = $this->_parseAttributes(array_merge($options, array('id' => $id)));
|
2006-11-22 03:51:21 +00:00
|
|
|
return $this->output(sprintf($this->Html->tags['blockstart'], $attr));
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Closes an Ajax-updateable DIV element
|
|
|
|
*
|
|
|
|
* @param string $id The DOM ID of the element
|
|
|
|
* @return string HTML code
|
|
|
|
*/
|
|
|
|
function divEnd($id) {
|
|
|
|
if (env('HTTP_X_UPDATE') != null) {
|
|
|
|
$divs = explode(' ', env('HTTP_X_UPDATE'));
|
|
|
|
if (in_array($id, $divs)) {
|
|
|
|
$this->__ajaxBuffer[$id] = ob_get_contents();
|
|
|
|
ob_end_clean();
|
2006-09-28 21:15:27 +00:00
|
|
|
ob_start();
|
2006-05-26 05:29:17 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2006-11-22 03:51:21 +00:00
|
|
|
return $this->output($this->Html->tags['blockend']);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-06-16 16:10:28 +00:00
|
|
|
/**
|
|
|
|
* Detects Ajax requests
|
|
|
|
*
|
2007-10-22 16:09:35 +00:00
|
|
|
* @return boolean True if the current request is a Prototype Ajax update call
|
2006-06-16 16:10:28 +00:00
|
|
|
*/
|
|
|
|
function isAjax() {
|
|
|
|
return (isset($this->params['isAjax']) && $this->params['isAjax'] === true);
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-06-16 19:02:30 +00:00
|
|
|
* Creates a draggable element. For a reference on the options for this function,
|
|
|
|
* check out http://wiki.script.aculo.us/scriptaculous/show/Draggable
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2006-06-16 19:02:30 +00:00
|
|
|
* @param unknown_type $id
|
2006-02-18 23:42:21 +00:00
|
|
|
* @param array $options
|
2006-06-16 19:02:30 +00:00
|
|
|
* @return unknown
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-06-16 19:02:30 +00:00
|
|
|
function drag($id, $options = array()) {
|
|
|
|
$var = '';
|
|
|
|
if (isset($options['var'])) {
|
|
|
|
$var = 'var ' . $options['var'] . ' = ';
|
|
|
|
unset($options['var']);
|
|
|
|
}
|
2007-01-31 23:48:51 +00:00
|
|
|
$options = $this->_buildOptions($this->_optionsToString($options, array('handle', 'constraint')), $this->dragOptions);
|
|
|
|
return $this->Javascript->codeBlock("{$var}new Draggable('$id', " .$options . ");");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* For a reference on the options for this function, check out
|
|
|
|
* http://wiki.script.aculo.us/scriptaculous/show/Droppables.add
|
|
|
|
*
|
2006-02-07 02:19:53 +00:00
|
|
|
* @param unknown_type $id
|
2006-02-18 23:42:21 +00:00
|
|
|
* @param array $options
|
2007-03-16 23:54:24 +00:00
|
|
|
* @return string
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function drop($id, $options = array()) {
|
2007-12-24 01:45:39 +00:00
|
|
|
$optionsString = array('overlap', 'hoverclass');
|
|
|
|
if (!isset($options['accept']) || !is_array($options['accept'])) {
|
|
|
|
$optionsString[] = 'accept';
|
|
|
|
} else if (isset($options['accept'])) {
|
|
|
|
$options['accept'] = $this->Javascript->object($options['accept']);
|
|
|
|
}
|
|
|
|
$options = $this->_buildOptions($this->_optionsToString($options, $optionsString), $this->dropOptions);
|
2006-07-06 16:44:03 +00:00
|
|
|
return $this->Javascript->codeBlock("Droppables.add('{$id}', {$options});");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2007-03-16 23:54:24 +00:00
|
|
|
* Make an element with the given $id droppable, and trigger an Ajax call when a draggable is
|
|
|
|
* dropped on it.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2007-03-16 23:54:24 +00:00
|
|
|
* For a reference on the options for this function, check out
|
|
|
|
* http://wiki.script.aculo.us/scriptaculous/show/Droppables.add
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param array $options
|
|
|
|
* @param array $ajaxOptions
|
|
|
|
* @return string JavaScript block to create a droppable element
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function dropRemote($id, $options = array(), $ajaxOptions = array()) {
|
2007-10-28 05:28:45 +00:00
|
|
|
$options['onDrop'] = "function(element, droppable, event) {" . $this->remoteFunction($ajaxOptions) . "}";
|
2007-12-24 01:45:39 +00:00
|
|
|
$optionsString = array('overlap', 'hoverclass');
|
|
|
|
if (!isset($options['accept']) || !is_array($options['accept'])) {
|
|
|
|
$optionsString[] = 'accept';
|
|
|
|
} else if (isset($options['accept'])) {
|
|
|
|
$options['accept'] = $this->Javascript->object($options['accept']);
|
|
|
|
}
|
|
|
|
$options = $this->_buildOptions($this->_optionsToString($options, $optionsString), $this->dropOptions);
|
2006-07-06 16:44:03 +00:00
|
|
|
return $this->Javascript->codeBlock("Droppables.add('{$id}', {$options});");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Makes a slider control.
|
|
|
|
*
|
|
|
|
* @param string $id DOM ID of slider handle
|
2007-12-24 01:45:39 +00:00
|
|
|
* @param string $trackId DOM ID of slider track
|
2006-02-18 23:42:21 +00:00
|
|
|
* @param array $options Array of options to control the slider
|
|
|
|
* @link http://wiki.script.aculo.us/scriptaculous/show/Slider
|
|
|
|
*/
|
2007-12-24 01:45:39 +00:00
|
|
|
function slider($id, $trackId, $options = array()) {
|
2006-06-16 19:02:30 +00:00
|
|
|
if (isset($options['var'])) {
|
|
|
|
$var = 'var ' . $options['var'] . ' = ';
|
|
|
|
unset($options['var']);
|
|
|
|
} else {
|
|
|
|
$var = 'var ' . $id . ' = ';
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$options = $this->_optionsToString($options, array('axis', 'handleImage', 'handleDisabled'));
|
2007-12-24 01:45:39 +00:00
|
|
|
$callbacks = array('change', 'slide');
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-12-24 01:45:39 +00:00
|
|
|
foreach($callbacks as $callback) {
|
|
|
|
if (isset($options[$callback])) {
|
|
|
|
$options['on' . ucfirst($callback)] = 'function(value) {' . $options[$callback] . '}';
|
|
|
|
unset($options[$callback]);
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
|
2006-06-16 19:02:30 +00:00
|
|
|
if (isset($options['values']) && is_array($options['values'])) {
|
|
|
|
$options['values'] = $this->Javascript->object($options['values']);
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$options = $this->_buildOptions($options, $this->sliderOptions);
|
2007-12-24 01:45:39 +00:00
|
|
|
return $this->Javascript->codeBlock("{$var}new Control.Slider('$id', '$trackId', $options);");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-08 15:25:34 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Makes an Ajax In Place editor control.
|
|
|
|
*
|
|
|
|
* @param string $id DOM ID of input element
|
|
|
|
* @param string $url Postback URL of saved data
|
|
|
|
* @param array $options Array of options to control the editor, including ajaxOptions (see link).
|
|
|
|
* @link http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function editor($id, $url, $options = array()) {
|
2006-08-30 16:23:24 +00:00
|
|
|
$url = $this->url($url);
|
2006-05-26 05:29:17 +00:00
|
|
|
$options['ajaxOptions'] = $this->__optionsForAjax($options);
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->ajaxOptions as $opt) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (isset($options[$opt])) {
|
|
|
|
unset($options[$opt]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['callback'])) {
|
|
|
|
$options['callback'] = 'function(form, value) {' . $options['callback'] . '}';
|
|
|
|
}
|
|
|
|
|
2006-06-16 19:02:30 +00:00
|
|
|
$type = 'InPlaceEditor';
|
|
|
|
if (isset($options['collection']) && is_array($options['collection'])) {
|
|
|
|
$options['collection'] = $this->Javascript->object($options['collection']);
|
|
|
|
$type = 'InPlaceCollectionEditor';
|
|
|
|
}
|
|
|
|
|
|
|
|
$var = '';
|
|
|
|
if (isset($options['var'])) {
|
|
|
|
$var = 'var ' . $options['var'] . ' = ';
|
|
|
|
unset($options['var']);
|
|
|
|
}
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$options = $this->_optionsToString($options, array('okText', 'cancelText', 'savingText', 'formId', 'externalControl', 'highlightcolor', 'highlightendcolor', 'savingClassName', 'formClassName', 'loadTextURL', 'loadingText', 'clickToEditText'));
|
|
|
|
$options = $this->_buildOptions($options, $this->editorOptions);
|
2006-06-16 19:02:30 +00:00
|
|
|
return $this->Javascript->codeBlock("{$var}new Ajax.{$type}('{$id}', '{$url}', {$options});");
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-02-07 02:19:53 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Makes a list or group of floated objects sortable.
|
|
|
|
*
|
|
|
|
* @param string $id DOM ID of parent
|
|
|
|
* @param array $options Array of options to control sort.http://wiki.script.aculo.us/scriptaculous/show/Sortable.create
|
|
|
|
* @link http://wiki.script.aculo.us/scriptaculous/show/Sortable.create
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function sortable($id, $options = array()) {
|
|
|
|
if (!empty($options['url'])) {
|
2007-02-14 07:24:54 +00:00
|
|
|
if (empty($options['with'])) {
|
|
|
|
$options['with'] = "Sortable.serialize('$id')";
|
|
|
|
}
|
2007-06-20 07:51:52 +00:00
|
|
|
$options['onUpdate'] = 'function(sortable) {' . $this->remoteFunction($options) . '}';
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2008-03-12 03:57:54 +00:00
|
|
|
$block = true;
|
|
|
|
|
|
|
|
if (isset($options['block'])) {
|
|
|
|
$block = $options['block'];
|
|
|
|
unset($options['block']);
|
|
|
|
}
|
2008-03-15 22:48:46 +00:00
|
|
|
$strings = array('tag', 'constraint', 'only', 'handle', 'hoverclass', 'tree', 'treeTag', 'update', 'overlap');
|
|
|
|
if (isset($options['scroll']) && $options['scroll'] != 'window' && strpos($options['scroll'], '$(') !== 0) {
|
|
|
|
$strings[] = 'scroll';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2008-03-15 22:48:46 +00:00
|
|
|
$options = $this->_optionsToString($options, $strings);
|
2007-12-23 21:08:54 +00:00
|
|
|
$options = array_merge($options, $this->_buildCallbacks($options));
|
2006-06-16 19:02:30 +00:00
|
|
|
$options = $this->_buildOptions($options, $this->sortOptions);
|
2008-03-12 03:57:54 +00:00
|
|
|
$result = "Sortable.create('$id', $options);";
|
|
|
|
if (!$block) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
return $this->Javascript->codeBlock($result);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Private helper function for Javascript.
|
|
|
|
*
|
2008-05-18 19:51:30 +00:00
|
|
|
* @param array $options Set of options
|
|
|
|
* @access private
|
2006-02-18 23:42:21 +00:00
|
|
|
*/
|
2008-05-18 19:51:30 +00:00
|
|
|
function __optionsForAjax($options) {
|
2007-01-06 22:09:27 +00:00
|
|
|
if (isset($options['indicator'])) {
|
|
|
|
if (isset($options['loading'])) {
|
2007-03-29 02:16:00 +00:00
|
|
|
if (!empty($options['loading']) && substr(trim($options['loading']), -1, 1) != ';') {
|
|
|
|
$options['loading'] .= '; ';
|
|
|
|
}
|
2007-12-23 21:08:54 +00:00
|
|
|
$options['loading'] .= "Element.show('{$options['indicator']}');";
|
2007-01-06 22:09:27 +00:00
|
|
|
} else {
|
2007-12-23 21:08:54 +00:00
|
|
|
$options['loading'] = "Element.show('{$options['indicator']}');";
|
2007-01-06 22:09:27 +00:00
|
|
|
}
|
|
|
|
if (isset($options['complete'])) {
|
2007-03-29 02:16:00 +00:00
|
|
|
if (!empty($options['complete']) && substr(trim($options['complete']), -1, 1) != ';') {
|
|
|
|
$options['complete'] .= '; ';
|
|
|
|
}
|
2007-01-06 22:09:27 +00:00
|
|
|
$options['complete'] .= "Element.hide('{$options['indicator']}');";
|
|
|
|
} else {
|
2007-12-23 21:08:54 +00:00
|
|
|
$options['complete'] = "Element.hide('{$options['indicator']}');";
|
2007-01-06 22:09:27 +00:00
|
|
|
}
|
|
|
|
unset($options['indicator']);
|
|
|
|
}
|
|
|
|
|
2007-12-08 06:08:03 +00:00
|
|
|
$jsOptions = array_merge(
|
2007-02-04 19:28:22 +00:00
|
|
|
array('asynchronous' => 'true', 'evalScripts' => 'true'),
|
|
|
|
$this->_buildCallbacks($options)
|
|
|
|
);
|
2007-12-23 21:08:54 +00:00
|
|
|
$options = $this->_optionsToString($options, array('contentType', 'encoding', 'fallback', 'method', 'postBody', 'update', 'url'));
|
|
|
|
$jsOptions = array_merge($jsOptions, array_intersect_key($options, array_flip(array('contentType', 'encoding', 'method', 'postBody'))));
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($options as $key => $value) {
|
2006-05-26 05:29:17 +00:00
|
|
|
switch($key) {
|
|
|
|
case 'type':
|
2007-02-04 19:28:22 +00:00
|
|
|
$jsOptions['asynchronous'] = ife(($value == 'synchronous'), 'false', 'true');
|
|
|
|
break;
|
|
|
|
case 'evalScripts':
|
|
|
|
$jsOptions['evalScripts'] = ife($value, 'true', 'false');
|
2006-05-26 05:29:17 +00:00
|
|
|
break;
|
|
|
|
case 'position':
|
2007-02-04 19:28:22 +00:00
|
|
|
$jsOptions['insertion'] = "Insertion." . Inflector::camelize($options['position']);
|
2006-05-26 05:29:17 +00:00
|
|
|
break;
|
|
|
|
case 'with':
|
2007-02-04 19:28:22 +00:00
|
|
|
$jsOptions['parameters'] = $options['with'];
|
2006-05-26 05:29:17 +00:00
|
|
|
break;
|
|
|
|
case 'form':
|
2007-02-04 19:28:22 +00:00
|
|
|
$jsOptions['parameters'] = 'Form.serialize(this)';
|
2006-05-26 05:29:17 +00:00
|
|
|
break;
|
|
|
|
case 'requestHeaders':
|
|
|
|
$keys = array();
|
|
|
|
foreach ($value as $key => $val) {
|
2006-06-14 18:02:37 +00:00
|
|
|
$keys[] = "'" . $key . "'";
|
|
|
|
$keys[] = "'" . $val . "'";
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-02-04 19:28:22 +00:00
|
|
|
$jsOptions['requestHeaders'] = '[' . join(', ', $keys) . ']';
|
2006-05-26 05:29:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-02-04 19:28:22 +00:00
|
|
|
return $this->_buildOptions($jsOptions, $this->ajaxOptions);
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
2007-02-04 19:28:22 +00:00
|
|
|
/**
|
|
|
|
* Private Method to return a string of html options
|
|
|
|
* option data as a JavaScript options hash.
|
|
|
|
*
|
|
|
|
* @param array $options Options in the shape of keys and values
|
|
|
|
* @param array $extra Array of legal keys in this options context
|
|
|
|
* @return array Array of html options
|
|
|
|
* @access private
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function __getHtmlOptions($options, $extra = array()) {
|
2008-03-15 22:48:46 +00:00
|
|
|
foreach (array_merge($this->ajaxOptions, $this->callbacks, $extra) as $key) {
|
2006-07-11 14:34:35 +00:00
|
|
|
if (isset($options[$key])) {
|
2006-05-26 05:29:17 +00:00
|
|
|
unset($options[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-07 02:19:53 +00:00
|
|
|
* Returns a string of JavaScript with the given option data as a JavaScript options hash.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $options Options in the shape of keys and values
|
|
|
|
* @param array $acceptable Array of legal keys in this options context
|
|
|
|
* @return string String of Javascript array definition
|
|
|
|
*/
|
|
|
|
function _buildOptions($options, $acceptable) {
|
|
|
|
if (is_array($options)) {
|
|
|
|
$out = array();
|
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($options as $k => $v) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (in_array($k, $acceptable)) {
|
2007-06-21 16:24:13 +00:00
|
|
|
if ($v === true) {
|
|
|
|
$v = 'true';
|
|
|
|
} elseif ($v === false) {
|
|
|
|
$v = 'false';
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
$out[] = "$k:$v";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = join(', ', $out);
|
|
|
|
$out = '{' . $out . '}';
|
|
|
|
return $out;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-18 23:42:21 +00:00
|
|
|
* Return JavaScript text for an observer...
|
|
|
|
*
|
|
|
|
* @param string $klass Name of JavaScript class
|
|
|
|
* @param string $name
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $options Ajax options
|
2006-02-18 23:42:21 +00:00
|
|
|
* @return string Formatted JavaScript
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function _buildObserver($klass, $name, $options = null) {
|
|
|
|
if (!isset($options['with']) && isset($options['update'])) {
|
|
|
|
$options['with'] = 'value';
|
|
|
|
}
|
2005-09-19 17:20:24 +00:00
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$callback = $this->remoteFunction($options);
|
|
|
|
$javascript = "new $klass('$name', ";
|
2007-04-04 07:25:48 +00:00
|
|
|
$javascript .= (!isset($options['frequency']) || intval($options['frequency']) == 0 ? '' : $options['frequency'] . ', ' ) . "function(element, value) {";
|
2006-05-26 05:29:17 +00:00
|
|
|
$javascript .= "$callback})";
|
|
|
|
return $javascript;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2007-12-24 01:45:39 +00:00
|
|
|
* Return Javascript text for callbacks.
|
2006-02-18 23:42:21 +00:00
|
|
|
*
|
2007-12-24 01:45:39 +00:00
|
|
|
* @param array $options Option array where a callback is specified
|
|
|
|
* @return array Options with their callbacks properly set
|
|
|
|
* @access protected
|
2005-12-27 03:33:44 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function _buildCallbacks($options) {
|
|
|
|
$callbacks = array();
|
2005-09-19 17:20:24 +00:00
|
|
|
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($this->callbacks as $callback) {
|
2006-05-26 05:29:17 +00:00
|
|
|
if (isset($options[$callback])) {
|
|
|
|
$name = 'on' . ucfirst($callback);
|
|
|
|
$code = $options[$callback];
|
2007-12-23 21:08:54 +00:00
|
|
|
switch($name) {
|
|
|
|
case 'onComplete':
|
|
|
|
$callbacks[$name] = "function(request, json) {" . $code . "}";
|
|
|
|
break;
|
|
|
|
case 'onCreate':
|
|
|
|
$callbacks[$name] = "function(request, xhr) {" . $code . "}";
|
|
|
|
break;
|
|
|
|
case 'onException':
|
|
|
|
$callbacks[$name] = "function(request, exception) {" . $code . "}";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$callbacks[$name] = "function(request) {" . $code . "}";
|
|
|
|
break;
|
2007-01-01 18:04:40 +00:00
|
|
|
}
|
2006-06-14 23:24:54 +00:00
|
|
|
if (isset($options['bind'])) {
|
|
|
|
if ((is_array($options['bind']) && in_array($callback, $options['bind'])) || (is_string($options['bind']) && strpos($options['bind'], $callback) !== false)) {
|
|
|
|
$callbacks[$name] .= ".bind(this)";
|
|
|
|
}
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $callbacks;
|
|
|
|
}
|
2006-01-12 02:10:47 +00:00
|
|
|
/**
|
2006-02-07 02:19:53 +00:00
|
|
|
* Returns a string of JavaScript with a string representation of given options array.
|
2006-01-12 02:10:47 +00:00
|
|
|
*
|
2006-05-26 05:29:17 +00:00
|
|
|
* @param array $options Ajax options array
|
|
|
|
* @param array $stringOpts Options as strings in an array
|
2006-02-07 02:19:53 +00:00
|
|
|
* @access private
|
|
|
|
* @return array
|
2006-01-12 02:10:47 +00:00
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function _optionsToString($options, $stringOpts = array()) {
|
2007-06-20 06:15:35 +00:00
|
|
|
foreach ($stringOpts as $option) {
|
2007-12-23 21:08:54 +00:00
|
|
|
if (isset($options[$option]) && !empty($options[$option]) && is_string($options[$option]) && $options[$option][0] != "'") {
|
2006-09-19 05:18:47 +00:00
|
|
|
if ($options[$option] === true || $options[$option] === 'true') {
|
|
|
|
$options[$option] = 'true';
|
|
|
|
} elseif ($options[$option] === false || $options[$option] === 'false') {
|
|
|
|
$options[$option] = 'false';
|
|
|
|
} else {
|
|
|
|
$options[$option] = "'{$options[$option]}'";
|
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
2007-12-23 21:08:54 +00:00
|
|
|
/**
|
|
|
|
* Executed after a view has rendered, used to include bufferred code
|
|
|
|
* blocks.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2006-05-26 05:29:17 +00:00
|
|
|
function afterRender() {
|
2007-12-23 21:08:54 +00:00
|
|
|
if (env('HTTP_X_UPDATE') != null && !empty($this->__ajaxBuffer)) {
|
2006-09-28 21:15:27 +00:00
|
|
|
@ob_end_clean();
|
|
|
|
|
2006-05-26 05:29:17 +00:00
|
|
|
$data = array();
|
2006-06-14 18:02:37 +00:00
|
|
|
$divs = explode(' ', env('HTTP_X_UPDATE'));
|
2006-06-27 10:29:35 +00:00
|
|
|
$keys = array_keys($this->__ajaxBuffer);
|
2006-06-14 18:02:37 +00:00
|
|
|
|
2006-06-27 10:29:35 +00:00
|
|
|
if (count($divs) == 1 && in_array($divs[0], $keys)) {
|
|
|
|
e($this->__ajaxBuffer[$divs[0]]);
|
|
|
|
} else {
|
|
|
|
foreach ($this->__ajaxBuffer as $key => $val) {
|
|
|
|
if (in_array($key, $divs)) {
|
|
|
|
$data[] = $key . ':"' . rawurlencode($val) . '"';
|
|
|
|
}
|
2006-06-14 18:02:37 +00:00
|
|
|
}
|
2006-07-04 01:29:48 +00:00
|
|
|
$out = 'var __ajaxUpdater__ = {' . join(", \n", $data) . '};' . "\n";
|
2007-07-08 18:06:44 +00:00
|
|
|
$out .= 'for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string" && $(n)) Element.update($(n), unescape(decodeURIComponent(__ajaxUpdater__[n]))); }';
|
2007-02-21 20:46:34 +00:00
|
|
|
e($this->Javascript->codeBlock($out, false));
|
2006-06-27 10:29:35 +00:00
|
|
|
}
|
2007-02-21 22:13:16 +00:00
|
|
|
$scripts = $this->Javascript->getCache();
|
|
|
|
|
|
|
|
if (!empty($scripts)) {
|
|
|
|
e($this->Javascript->codeBlock($scripts, false));
|
|
|
|
}
|
2008-05-18 19:51:30 +00:00
|
|
|
|
|
|
|
$this->stop();
|
2006-05-26 05:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-21 23:44:49 +00:00
|
|
|
}
|
2006-05-26 05:29:17 +00:00
|
|
|
|
2005-09-19 17:20:24 +00:00
|
|
|
?>
|