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