2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Javascript Generator class file.
|
|
|
|
*
|
2010-10-03 12:38:58 -04:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-26 17:15:15 -05:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 21:28:17 +09:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 20:59:49 +09:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-07-31 19:14:36 -04:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.View.Helper
|
2011-07-31 19:14:36 -04:00
|
|
|
* @since CakePHP(tm) v 1.2
|
2009-11-06 17:51:51 +11:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2010-12-04 13:40:24 -04:30
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2010-12-21 22:01:38 -04:30
|
|
|
App::uses('JsBaseEngineHelper', 'View/Helper');
|
2010-12-04 13:45:07 -04:30
|
|
|
App::uses('Multibyte', 'I18n');
|
2010-12-03 18:37:21 -04:30
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Javascript Generator helper class for easy use of JavaScript.
|
|
|
|
*
|
|
|
|
* JsHelper provides an abstract interface for authoring JavaScript with a
|
|
|
|
* given client-side library.
|
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.View.Helper
|
2011-07-31 20:28:35 -04:00
|
|
|
* @property HtmlHelper $Html
|
|
|
|
* @property FormHelper $Form
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2009-03-07 13:10:50 -05:00
|
|
|
class JsHelper extends AppHelper {
|
2011-11-29 21:23:02 -08:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
2009-03-29 14:23:42 -04:00
|
|
|
* Whether or not you want scripts to be buffered or output.
|
2009-03-07 11:08:49 -05:00
|
|
|
*
|
2009-03-15 23:42:25 -04:00
|
|
|
* @var boolean
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $bufferScripts = true;
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
2011-10-11 00:22:23 +02:00
|
|
|
* Helper dependencies
|
2009-03-07 11:08:49 -05:00
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $helpers = array('Html', 'Form');
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-10-14 21:26:43 -04:00
|
|
|
/**
|
|
|
|
* Variables to pass to Javascript.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see JsHelper::set()
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-08-20 00:43:34 -04:00
|
|
|
protected $_jsVars = array();
|
2009-10-14 21:26:43 -04:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
2009-03-15 23:42:25 -04:00
|
|
|
* Scripts that are queued for output
|
2009-03-07 11:08:49 -05:00
|
|
|
*
|
|
|
|
* @var array
|
2010-01-25 17:59:05 -05:00
|
|
|
* @see JsHelper::buffer()
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-08-20 00:43:34 -04:00
|
|
|
protected $_bufferedScripts = array();
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
|
|
|
* Current Javascript Engine that is being used
|
|
|
|
*
|
|
|
|
* @var string
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-08-20 00:43:34 -04:00
|
|
|
protected $_engineName;
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
2009-10-14 21:26:43 -04:00
|
|
|
* The javascript variable created by set() variables.
|
2009-03-07 11:08:49 -05:00
|
|
|
*
|
|
|
|
* @var string
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-09-18 12:09:04 -04:00
|
|
|
public $setVariable = 'app';
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
|
|
|
* Constructor - determines engine helper
|
|
|
|
*
|
2010-07-03 01:07:50 -04:00
|
|
|
* @param View $View the view object the helper is attached to.
|
2009-03-07 11:08:49 -05:00
|
|
|
* @param array $settings Settings array contains name of engine helper.
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-07-03 01:07:50 -04:00
|
|
|
public function __construct(View $View, $settings = array()) {
|
2009-04-26 22:53:26 -04:00
|
|
|
$className = 'Jquery';
|
2009-03-07 11:08:49 -05:00
|
|
|
if (is_array($settings) && isset($settings[0])) {
|
|
|
|
$className = $settings[0];
|
|
|
|
} elseif (is_string($settings)) {
|
|
|
|
$className = $settings;
|
|
|
|
}
|
|
|
|
$engineName = $className;
|
2012-12-20 13:47:03 +01:00
|
|
|
list(, $className) = pluginSplit($className);
|
2009-11-15 19:55:20 -05:00
|
|
|
|
2011-08-20 00:43:34 -04:00
|
|
|
$this->_engineName = $className . 'Engine';
|
2009-03-07 11:08:49 -05:00
|
|
|
$engineClass = $engineName . 'Engine';
|
2009-03-12 23:48:19 -04:00
|
|
|
$this->helpers[] = $engineClass;
|
2010-07-03 11:36:53 -04:00
|
|
|
parent::__construct($View, $settings);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-07 11:08:49 -05:00
|
|
|
/**
|
2009-03-29 14:38:09 -04:00
|
|
|
* call__ Allows for dispatching of methods to the Engine Helper.
|
|
|
|
* methods in the Engines bufferedMethods list will be automatically buffered.
|
2009-07-26 21:58:58 -04:00
|
|
|
* You can control buffering with the buffer param as well. By setting the last parameter to
|
2009-03-29 14:38:09 -04:00
|
|
|
* any engine method to a boolean you can force or disable buffering.
|
2009-07-26 21:58:58 -04:00
|
|
|
*
|
2009-04-15 23:48:04 -04:00
|
|
|
* e.g. `$js->get('#foo')->effect('fadeIn', array('speed' => 'slow'), true);`
|
2009-03-29 14:38:09 -04:00
|
|
|
*
|
|
|
|
* Will force buffering for the effect method. If the method takes an options array you may also add
|
|
|
|
* a 'buffer' param to the options array and control buffering there as well.
|
|
|
|
*
|
2009-04-15 23:48:04 -04:00
|
|
|
* e.g. `$js->get('#foo')->event('click', $functionContents, array('buffer' => true));`
|
2009-03-29 14:38:09 -04:00
|
|
|
*
|
|
|
|
* The buffer parameter will not be passed onto the EngineHelper.
|
2009-03-07 11:08:49 -05:00
|
|
|
*
|
|
|
|
* @param string $method Method to be called
|
|
|
|
* @param array $params Parameters for the method being called.
|
2009-08-09 20:51:59 -04:00
|
|
|
* @return mixed Depends on the return of the dispatched method, or it could be an instance of the EngineHelper
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-23 21:21:31 -04:00
|
|
|
public function __call($method, $params) {
|
2011-08-20 00:43:34 -04:00
|
|
|
if ($this->{$this->_engineName} && method_exists($this->{$this->_engineName}, $method)) {
|
2009-03-29 16:10:04 -04:00
|
|
|
$buffer = false;
|
2011-08-20 00:43:34 -04:00
|
|
|
$engineHelper = $this->{$this->_engineName};
|
2010-07-03 13:06:11 -04:00
|
|
|
if (in_array(strtolower($method), $engineHelper->bufferedMethods)) {
|
2009-03-29 16:10:04 -04:00
|
|
|
$buffer = true;
|
|
|
|
}
|
|
|
|
if (count($params) > 0) {
|
|
|
|
$lastParam = $params[count($params) - 1];
|
|
|
|
$hasBufferParam = (is_bool($lastParam) || is_array($lastParam) && isset($lastParam['buffer']));
|
|
|
|
if ($hasBufferParam && is_bool($lastParam)) {
|
|
|
|
$buffer = $lastParam;
|
|
|
|
unset($params[count($params) - 1]);
|
|
|
|
} elseif ($hasBufferParam && is_array($lastParam)) {
|
|
|
|
$buffer = $lastParam['buffer'];
|
|
|
|
unset($params['buffer']);
|
|
|
|
}
|
|
|
|
}
|
2010-07-03 13:06:11 -04:00
|
|
|
|
|
|
|
$out = call_user_func_array(array(&$engineHelper, $method), $params);
|
2009-03-29 16:10:04 -04:00
|
|
|
if ($this->bufferScripts && $buffer && is_string($out)) {
|
2009-03-29 14:23:42 -04:00
|
|
|
$this->buffer($out);
|
2009-03-29 16:10:04 -04:00
|
|
|
return null;
|
2009-03-29 14:23:42 -04:00
|
|
|
}
|
2012-08-21 08:47:17 -04:00
|
|
|
if (is_object($out) && $out instanceof JsBaseEngineHelper) {
|
2009-03-29 14:23:42 -04:00
|
|
|
return $this;
|
2009-03-15 23:42:25 -04:00
|
|
|
}
|
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (method_exists($this, $method . '_')) {
|
2010-07-03 13:06:11 -04:00
|
|
|
return call_user_func(array(&$this, $method . '_'), $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-03-20 16:35:43 +01:00
|
|
|
trigger_error(__d('cake_dev', 'JsHelper:: Missing Method %s is undefined', $method), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-12-06 20:45:12 -05:00
|
|
|
/**
|
|
|
|
* Overwrite inherited Helper::value()
|
|
|
|
* See JsBaseEngineHelper::value() for more information on this method.
|
|
|
|
*
|
|
|
|
* @param mixed $val A PHP variable to be converted to JSON
|
2011-07-28 22:45:47 -04:00
|
|
|
* @param boolean $quoteString If false, leaves string values unquoted
|
2009-12-06 20:45:12 -05:00
|
|
|
* @return string a JavaScript-safe/JSON representation of $val
|
2011-11-13 22:08:20 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::value
|
2013-01-11 15:06:54 +01:00
|
|
|
*/
|
2011-11-12 20:44:32 -05:00
|
|
|
public function value($val = array(), $quoteString = null, $key = 'value') {
|
|
|
|
if ($quoteString === null) {
|
|
|
|
$quoteString = true;
|
|
|
|
}
|
2011-08-20 00:43:34 -04:00
|
|
|
return $this->{$this->_engineName}->value($val, $quoteString);
|
2009-12-06 20:45:12 -05:00
|
|
|
}
|
|
|
|
|
2009-03-07 13:10:50 -05:00
|
|
|
/**
|
2009-03-12 23:48:19 -04:00
|
|
|
* Writes all Javascript generated so far to a code block or
|
2012-12-22 23:48:15 +01:00
|
|
|
* caches them to a file and returns a linked script. If no scripts have been
|
|
|
|
* buffered this method will return null. If the request is an XHR(ajax) request
|
2010-03-08 22:51:46 -05:00
|
|
|
* onDomReady will be set to false. As the dom is already 'ready'.
|
2009-03-07 13:10:50 -05:00
|
|
|
*
|
2010-01-25 17:59:05 -05:00
|
|
|
* ### Options
|
2009-03-07 13:10:50 -05:00
|
|
|
*
|
2009-07-24 23:29:44 -04:00
|
|
|
* - `inline` - Set to true to have scripts output as a script block inline
|
|
|
|
* if `cache` is also true, a script link tag will be generated. (default true)
|
|
|
|
* - `cache` - Set to true to have scripts cached to a file and linked in (default false)
|
|
|
|
* - `clear` - Set to false to prevent script cache from being cleared (default true)
|
|
|
|
* - `onDomReady` - wrap cached scripts in domready event (default true)
|
|
|
|
* - `safe` - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
|
2009-03-07 13:10:50 -05:00
|
|
|
*
|
2009-03-12 23:48:19 -04:00
|
|
|
* @param array $options options for the code block
|
2009-12-16 23:48:29 -05:00
|
|
|
* @return mixed Completed javascript tag if there are scripts, if there are no buffered
|
|
|
|
* scripts null will be returned.
|
2011-11-13 22:08:20 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::writeBuffer
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function writeBuffer($options = array()) {
|
2011-12-08 14:03:39 +05:30
|
|
|
$domReady = !$this->request->is('ajax');
|
2010-03-08 22:51:46 -05:00
|
|
|
$defaults = array(
|
2011-08-15 23:55:08 -04:00
|
|
|
'onDomReady' => $domReady, 'inline' => true,
|
2010-03-08 22:51:46 -05:00
|
|
|
'cache' => false, 'clear' => true, 'safe' => true
|
|
|
|
);
|
2009-03-12 23:48:19 -04:00
|
|
|
$options = array_merge($defaults, $options);
|
2009-03-29 14:23:42 -04:00
|
|
|
$script = implode("\n", $this->getBuffer($options['clear']));
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-12-15 01:13:19 +01:00
|
|
|
if (empty($script)) {
|
|
|
|
return null;
|
|
|
|
}
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-12 23:48:19 -04:00
|
|
|
if ($options['onDomReady']) {
|
2011-08-20 00:43:34 -04:00
|
|
|
$script = $this->{$this->_engineName}->domReady($script);
|
2009-03-07 13:10:50 -05:00
|
|
|
}
|
2009-12-21 09:29:28 -05:00
|
|
|
$opts = $options;
|
|
|
|
unset($opts['onDomReady'], $opts['cache'], $opts['clear']);
|
|
|
|
|
2009-03-12 23:48:19 -04:00
|
|
|
if ($options['cache'] && $options['inline']) {
|
2009-03-19 22:02:18 -04:00
|
|
|
$filename = md5($script);
|
2012-08-11 03:31:46 +05:30
|
|
|
if (file_exists(JS . $filename . '.js')
|
|
|
|
|| cache(str_replace(WWW_ROOT, '', JS) . $filename . '.js', $script, '+999 days', 'public')
|
|
|
|
) {
|
|
|
|
return $this->Html->script($filename);
|
2009-03-19 22:02:18 -04:00
|
|
|
}
|
2009-03-07 13:10:50 -05:00
|
|
|
}
|
2012-08-11 03:31:46 +05:30
|
|
|
|
|
|
|
$return = $this->Html->scriptBlock($script, $opts);
|
|
|
|
if ($options['inline']) {
|
|
|
|
return $return;
|
|
|
|
}
|
2009-03-12 23:48:19 -04:00
|
|
|
return null;
|
2009-03-07 13:10:50 -05:00
|
|
|
}
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-15 23:42:25 -04:00
|
|
|
/**
|
2010-01-25 17:59:05 -05:00
|
|
|
* Write a script to the buffered scripts.
|
2009-03-15 23:42:25 -04:00
|
|
|
*
|
2009-12-21 09:41:36 -05:00
|
|
|
* @param string $script Script string to add to the buffer.
|
2011-08-15 23:55:08 -04:00
|
|
|
* @param boolean $top If true the script will be added to the top of the
|
2012-12-22 23:48:15 +01:00
|
|
|
* buffered scripts array. If false the bottom.
|
2009-03-15 23:42:25 -04:00
|
|
|
* @return void
|
2011-11-13 22:08:20 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::buffer
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function buffer($script, $top = false) {
|
2009-12-21 09:41:36 -05:00
|
|
|
if ($top) {
|
2011-08-20 00:43:34 -04:00
|
|
|
array_unshift($this->_bufferedScripts, $script);
|
2009-12-21 09:41:36 -05:00
|
|
|
} else {
|
2011-08-20 00:43:34 -04:00
|
|
|
$this->_bufferedScripts[] = $script;
|
2009-12-21 09:41:36 -05:00
|
|
|
}
|
2009-03-15 23:42:25 -04:00
|
|
|
}
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-03-15 23:42:25 -04:00
|
|
|
/**
|
2010-01-25 17:59:05 -05:00
|
|
|
* Get all the buffered scripts
|
2009-03-15 23:42:25 -04:00
|
|
|
*
|
2009-08-11 23:33:41 -04:00
|
|
|
* @param boolean $clear Whether or not to clear the script caches (default true)
|
2009-03-15 23:42:25 -04:00
|
|
|
* @return array Array of scripts added to the request.
|
2011-11-13 22:08:20 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::getBuffer
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function getBuffer($clear = true) {
|
2009-10-14 21:26:43 -04:00
|
|
|
$this->_createVars();
|
2011-08-20 00:43:34 -04:00
|
|
|
$scripts = $this->_bufferedScripts;
|
2009-03-15 23:42:25 -04:00
|
|
|
if ($clear) {
|
2011-08-20 00:43:34 -04:00
|
|
|
$this->_bufferedScripts = array();
|
|
|
|
$this->_jsVars = array();
|
2009-03-15 23:42:25 -04:00
|
|
|
}
|
|
|
|
return $scripts;
|
|
|
|
}
|
2009-07-26 21:58:58 -04:00
|
|
|
|
2009-10-14 21:26:43 -04:00
|
|
|
/**
|
2012-11-29 04:00:47 +05:30
|
|
|
* Generates the object string for variables passed to javascript and adds to buffer
|
2009-10-14 21:26:43 -04:00
|
|
|
*
|
2012-11-29 04:00:47 +05:30
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:21:28 +10:00
|
|
|
protected function _createVars() {
|
2011-08-20 00:43:34 -04:00
|
|
|
if (!empty($this->_jsVars)) {
|
2009-12-10 23:37:55 -05:00
|
|
|
$setVar = (strpos($this->setVariable, '.')) ? $this->setVariable : 'window.' . $this->setVariable;
|
2011-08-20 00:43:34 -04:00
|
|
|
$this->buffer($setVar . ' = ' . $this->object($this->_jsVars) . ';', true);
|
2009-10-14 21:26:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-12 17:30:41 -04:00
|
|
|
/**
|
2012-12-22 23:48:15 +01:00
|
|
|
* Generate an 'Ajax' link. Uses the selected JS engine to create a link
|
|
|
|
* element that is enhanced with Javascript. Options can include
|
2009-07-26 21:58:58 -04:00
|
|
|
* both those for HtmlHelper::link() and JsBaseEngine::request(), JsBaseEngine::event();
|
2009-07-12 17:30:41 -04:00
|
|
|
*
|
2009-07-24 22:13:54 -04:00
|
|
|
* ### Options
|
2009-07-26 21:58:58 -04:00
|
|
|
*
|
2009-07-24 23:29:44 -04:00
|
|
|
* - `confirm` - Generate a confirm() dialog before sending the event.
|
|
|
|
* - `id` - use a custom id.
|
2012-12-22 23:48:15 +01:00
|
|
|
* - `htmlAttributes` - additional non-standard htmlAttributes. Standard attributes are class, id,
|
2009-07-24 22:13:54 -04:00
|
|
|
* rel, title, escape, onblur and onfocus.
|
2009-07-24 23:29:44 -04:00
|
|
|
* - `buffer` - Disable the buffering and return a script tag in addition to the link.
|
2009-07-26 21:58:58 -04:00
|
|
|
*
|
2009-07-12 17:30:41 -04:00
|
|
|
* @param string $title Title for the link.
|
2013-04-30 14:11:50 +02:00
|
|
|
* @param string|array $url Mixed either a string URL or a CakePHP URL array.
|
2009-07-12 17:30:41 -04:00
|
|
|
* @param array $options Options for both the HTML element and Js::request()
|
|
|
|
* @return string Completed link. If buffering is disabled a script tag will be returned as well.
|
2011-11-13 22:08:20 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::link
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function link($title, $url = null, $options = array()) {
|
2009-07-12 17:30:41 -04:00
|
|
|
if (!isset($options['id'])) {
|
|
|
|
$options['id'] = 'link-' . intval(mt_rand());
|
|
|
|
}
|
2010-01-15 00:02:24 -05:00
|
|
|
list($options, $htmlOptions) = $this->_getHtmlOptions($options);
|
2009-07-12 17:30:41 -04:00
|
|
|
$out = $this->Html->link($title, $url, $htmlOptions);
|
2009-07-12 18:42:36 -04:00
|
|
|
$this->get('#' . $htmlOptions['id']);
|
2010-05-02 21:17:10 -04:00
|
|
|
$requestString = $event = '';
|
2009-07-12 18:42:36 -04:00
|
|
|
if (isset($options['confirm'])) {
|
2009-07-24 22:13:54 -04:00
|
|
|
$requestString = $this->confirmReturn($options['confirm']);
|
2009-07-12 18:42:36 -04:00
|
|
|
unset($options['confirm']);
|
|
|
|
}
|
2010-05-02 21:17:10 -04:00
|
|
|
$buffer = isset($options['buffer']) ? $options['buffer'] : null;
|
|
|
|
$safe = isset($options['safe']) ? $options['safe'] : true;
|
|
|
|
unset($options['buffer'], $options['safe']);
|
|
|
|
|
2009-07-12 18:42:36 -04:00
|
|
|
$requestString .= $this->request($url, $options);
|
2010-05-02 21:17:10 -04:00
|
|
|
|
2009-07-12 17:30:41 -04:00
|
|
|
if (!empty($requestString)) {
|
2010-05-02 21:17:10 -04:00
|
|
|
$event = $this->event('click', $requestString, $options + array('buffer' => $buffer));
|
2009-07-24 22:13:54 -04:00
|
|
|
}
|
2010-05-02 21:17:10 -04:00
|
|
|
if (isset($buffer) && !$buffer) {
|
|
|
|
$opts = array('safe' => $safe);
|
2009-09-16 01:55:15 -04:00
|
|
|
$out .= $this->Html->scriptBlock($event, $opts);
|
2009-07-12 17:30:41 -04:00
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-10-14 21:26:43 -04:00
|
|
|
/**
|
2012-12-22 23:48:15 +01:00
|
|
|
* Pass variables into Javascript. Allows you to set variables that will be
|
2009-10-14 21:26:43 -04:00
|
|
|
* output when the buffer is fetched with `JsHelper::getBuffer()` or `JsHelper::writeBuffer()`
|
|
|
|
* The Javascript variable used to output set variables can be controlled with `JsHelper::$setVariable`
|
|
|
|
*
|
2012-05-13 01:43:31 +01:00
|
|
|
* @param string|array $one Either an array of variables to set, or the name of the variable to set.
|
|
|
|
* @param string|array $two If $one is a string, $two is the value for that key.
|
2009-10-14 21:26:43 -04:00
|
|
|
* @return void
|
2011-11-29 21:23:02 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::set
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function set($one, $two = null) {
|
2009-10-14 21:26:43 -04:00
|
|
|
$data = null;
|
|
|
|
if (is_array($one)) {
|
|
|
|
if (is_array($two)) {
|
|
|
|
$data = array_combine($one, $two);
|
|
|
|
} else {
|
|
|
|
$data = $one;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = array($one => $two);
|
|
|
|
}
|
2012-09-14 18:26:30 +01:00
|
|
|
if (!$data) {
|
2009-10-14 21:26:43 -04:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-20 00:43:34 -04:00
|
|
|
$this->_jsVars = array_merge($this->_jsVars, $data);
|
2009-10-14 21:26:43 -04:00
|
|
|
}
|
|
|
|
|
2009-07-21 00:24:58 -04:00
|
|
|
/**
|
|
|
|
* Uses the selected JS engine to create a submit input
|
2012-12-22 23:48:15 +01:00
|
|
|
* element that is enhanced with Javascript. Options can include
|
2009-07-21 00:24:58 -04:00
|
|
|
* both those for FormHelper::submit() and JsBaseEngine::request(), JsBaseEngine::event();
|
2009-07-25 17:10:22 -04:00
|
|
|
*
|
|
|
|
* Forms submitting with this method, cannot send files. Files do not transfer over XmlHttpRequest
|
2010-01-25 17:59:05 -05:00
|
|
|
* and require an iframe or flash.
|
2009-07-26 21:58:58 -04:00
|
|
|
*
|
2010-05-07 21:26:44 -04:00
|
|
|
* ### Options
|
2011-08-15 23:55:08 -04:00
|
|
|
*
|
2010-05-07 21:26:44 -04:00
|
|
|
* - `url` The url you wish the XHR request to submit to.
|
|
|
|
* - `confirm` A string to use for a confirm() message prior to submitting the request.
|
|
|
|
* - `method` The method you wish the form to send by, defaults to POST
|
|
|
|
* - `buffer` Whether or not you wish the script code to be buffered, defaults to true.
|
|
|
|
* - Also see options for JsHelper::request() and JsHelper::event()
|
|
|
|
*
|
2011-07-28 22:45:47 -04:00
|
|
|
* @param string $caption The display text of the submit button.
|
2010-05-07 21:26:44 -04:00
|
|
|
* @param array $options Array of options to use. See the options for the above mentioned methods.
|
2009-07-21 00:24:58 -04:00
|
|
|
* @return string Completed submit button.
|
2011-11-13 22:08:20 -08:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::submit
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:19:38 +10:00
|
|
|
public function submit($caption = null, $options = array()) {
|
2009-07-24 22:13:54 -04:00
|
|
|
if (!isset($options['id'])) {
|
|
|
|
$options['id'] = 'submit-' . intval(mt_rand());
|
|
|
|
}
|
|
|
|
$formOptions = array('div');
|
2010-01-15 00:02:24 -05:00
|
|
|
list($options, $htmlOptions) = $this->_getHtmlOptions($options, $formOptions);
|
2009-07-24 22:13:54 -04:00
|
|
|
$out = $this->Form->submit($caption, $htmlOptions);
|
|
|
|
|
|
|
|
$this->get('#' . $htmlOptions['id']);
|
2009-07-24 23:29:44 -04:00
|
|
|
|
|
|
|
$options['data'] = $this->serializeForm(array('isForm' => false, 'inline' => true));
|
2009-07-24 22:47:08 -04:00
|
|
|
$requestString = $url = '';
|
2009-07-24 22:13:54 -04:00
|
|
|
if (isset($options['confirm'])) {
|
|
|
|
$requestString = $this->confirmReturn($options['confirm']);
|
|
|
|
unset($options['confirm']);
|
|
|
|
}
|
2009-07-24 22:47:08 -04:00
|
|
|
if (isset($options['url'])) {
|
|
|
|
$url = $options['url'];
|
|
|
|
unset($options['url']);
|
|
|
|
}
|
2009-07-24 23:29:44 -04:00
|
|
|
if (!isset($options['method'])) {
|
2009-07-25 17:10:22 -04:00
|
|
|
$options['method'] = 'post';
|
2009-07-24 23:29:44 -04:00
|
|
|
}
|
|
|
|
$options['dataExpression'] = true;
|
2010-05-02 21:17:10 -04:00
|
|
|
|
|
|
|
$buffer = isset($options['buffer']) ? $options['buffer'] : null;
|
|
|
|
$safe = isset($options['safe']) ? $options['safe'] : true;
|
|
|
|
unset($options['buffer'], $options['safe']);
|
|
|
|
|
2009-07-24 22:47:08 -04:00
|
|
|
$requestString .= $this->request($url, $options);
|
2009-07-24 22:13:54 -04:00
|
|
|
if (!empty($requestString)) {
|
2010-05-02 21:17:10 -04:00
|
|
|
$event = $this->event('click', $requestString, $options + array('buffer' => $buffer));
|
2009-07-24 22:13:54 -04:00
|
|
|
}
|
2010-05-02 21:17:10 -04:00
|
|
|
if (isset($buffer) && !$buffer) {
|
|
|
|
$opts = array('safe' => $safe);
|
2010-04-23 00:04:15 -04:00
|
|
|
$out .= $this->Html->scriptBlock($event, $opts);
|
2009-07-24 22:13:54 -04:00
|
|
|
}
|
|
|
|
return $out;
|
2009-07-21 00:24:58 -04:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-07-12 17:30:41 -04:00
|
|
|
/**
|
|
|
|
* Parse a set of Options and extract the Html options.
|
2009-07-12 18:42:36 -04:00
|
|
|
* Extracted Html Options are removed from the $options param.
|
2009-07-12 17:30:41 -04:00
|
|
|
*
|
2009-07-24 22:13:54 -04:00
|
|
|
* @param array $options Options to filter.
|
|
|
|
* @param array $additional Array of additional keys to extract and include in the return options array.
|
2010-01-15 00:02:24 -05:00
|
|
|
* @return array Array of js options and Htmloptions
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2010-04-05 13:21:28 +10:00
|
|
|
protected function _getHtmlOptions($options, $additional = array()) {
|
2011-04-11 22:48:08 -04:30
|
|
|
$htmlKeys = array_merge(
|
2011-08-15 23:55:08 -04:00
|
|
|
array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title', 'style'),
|
2011-04-11 22:48:08 -04:30
|
|
|
$additional
|
|
|
|
);
|
2009-07-12 17:30:41 -04:00
|
|
|
$htmlOptions = array();
|
|
|
|
foreach ($htmlKeys as $key) {
|
|
|
|
if (isset($options[$key])) {
|
|
|
|
$htmlOptions[$key] = $options[$key];
|
|
|
|
}
|
2009-07-12 18:42:36 -04:00
|
|
|
unset($options[$key]);
|
2009-07-12 17:30:41 -04:00
|
|
|
}
|
2009-07-24 22:13:54 -04:00
|
|
|
if (isset($options['htmlAttributes'])) {
|
|
|
|
$htmlOptions = array_merge($htmlOptions, $options['htmlAttributes']);
|
|
|
|
unset($options['htmlAttributes']);
|
|
|
|
}
|
2010-01-15 00:02:24 -05:00
|
|
|
return array($options, $htmlOptions);
|
2009-07-12 17:30:41 -04:00
|
|
|
}
|
2012-03-03 17:10:12 -05:00
|
|
|
|
2011-09-18 12:09:04 -04:00
|
|
|
}
|