cakephp2-php8/lib/Cake/View/Helper/JsHelper.php

437 lines
14 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Javascript Generator class file.
*
2017-06-10 21:33:55 +00:00
* CakePHP : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2017-06-10 21:33:55 +00:00
* @link https://cakephp.org CakePHP(tm) Project
* @package Cake.View.Helper
* @since CakePHP(tm) v 1.2
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
2010-12-04 18:10:24 +00:00
App::uses('AppHelper', 'View/Helper');
App::uses('JsBaseEngineHelper', 'View/Helper');
App::uses('Multibyte', 'I18n');
/**
* Javascript Generator helper class for easy use of JavaScript.
*
* JsHelper provides an abstract interface for authoring JavaScript with a
* given client-side library.
*
* @package Cake.View.Helper
* @property HtmlHelper $Html
* @property FormHelper $Form
*/
class JsHelper extends AppHelper {
2009-03-07 16:08:49 +00:00
/**
* Whether or not you want scripts to be buffered or output.
2009-03-07 16:08:49 +00:00
*
* @var bool
*/
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
*/
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()
*/
protected $_jsVars = array();
2009-10-15 01:26:43 +00:00
2009-03-07 16:08:49 +00:00
/**
* Scripts that are queued for output
2009-03-07 16:08:49 +00:00
*
* @var array
* @see JsHelper::buffer()
*/
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
*/
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
*/
public $setVariable = 'app';
2009-07-27 01:58:58 +00:00
2009-03-07 16:08:49 +00:00
/**
* Constructor - determines engine helper
*
* @param View $View the view object the helper is attached to.
* @param string|array $settings Settings array contains name of engine helper.
*/
public function __construct(View $View, $settings = array()) {
$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;
list(, $className) = pluginSplit($className);
$this->_engineName = $className . 'Engine';
2009-03-07 16:08:49 +00:00
$engineClass = $engineName . 'Engine';
$this->helpers[] = $engineClass;
parent::__construct($View, $settings);
}
2009-07-27 01:58:58 +00:00
2009-03-07 16:08:49 +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
* any engine method to a boolean you can force or disable buffering.
2009-07-27 01:58:58 +00:00
*
* e.g. `$js->get('#foo')->effect('fadeIn', array('speed' => 'slow'), true);`
*
* 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.
*
* e.g. `$js->get('#foo')->event('click', $functionContents, array('buffer' => true));`
*
* 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.
* @return mixed Depends on the return of the dispatched method, or it could be an instance of the EngineHelper
*/
public function __call($method, $params) {
if ($this->{$this->_engineName} && method_exists($this->{$this->_engineName}, $method)) {
$buffer = false;
$engineHelper = $this->{$this->_engineName};
if (in_array(strtolower($method), $engineHelper->bufferedMethods)) {
$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']);
}
}
$out = call_user_func_array(array(&$engineHelper, $method), $params);
if ($this->bufferScripts && $buffer && is_string($out)) {
$this->buffer($out);
return null;
}
2012-08-21 12:47:17 +00:00
if (is_object($out) && $out instanceof JsBaseEngineHelper) {
return $this;
}
return $out;
}
if (method_exists($this, $method . '_')) {
return call_user_func(array(&$this, $method . '_'), $params);
}
trigger_error(__d('cake_dev', 'JsHelper:: Missing Method %s is undefined', $method), E_USER_WARNING);
}
2009-07-27 01:58:58 +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
* @param bool $quoteString If false, leaves string values unquoted
* @param string $key Key name.
* @return string a JavaScript-safe/JSON representation of $val
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::value
2013-01-11 14:06:54 +00:00
*/
public function value($val = array(), $quoteString = null, $key = 'value') {
if ($quoteString === null) {
$quoteString = true;
}
return $this->{$this->_engineName}->value($val, $quoteString);
}
/**
* Writes all Javascript generated so far to a code block or
2012-12-22 22:48:15 +00: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
* onDomReady will be set to false. As the dom is already 'ready'.
*
* ### Options
*
* - `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)
*
* @param array $options options for the code block
* @return mixed Completed javascript tag if there are scripts, if there are no buffered
* scripts null will be returned.
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::writeBuffer
*/
public function writeBuffer($options = array()) {
$domReady = !$this->request->is('ajax');
$defaults = array(
2011-08-16 03:55:08 +00:00
'onDomReady' => $domReady, 'inline' => true,
'cache' => false, 'clear' => true, 'safe' => true
);
$options += $defaults;
$script = implode("\n", $this->getBuffer($options['clear']));
2009-07-27 01:58:58 +00:00
if (empty($script)) {
return null;
}
2009-07-27 01:58:58 +00:00
if ($options['onDomReady']) {
$script = $this->{$this->_engineName}->domReady($script);
}
$opts = $options;
unset($opts['onDomReady'], $opts['cache'], $opts['clear']);
if ($options['cache'] && $options['inline']) {
$filename = md5($script);
$path = WWW_ROOT . Configure::read('App.jsBaseUrl');
if (file_exists($path . $filename . '.js')
|| cache(str_replace(WWW_ROOT, '', $path) . $filename . '.js', $script, '+999 days', 'public')
) {
return $this->Html->script($filename);
}
}
$return = $this->Html->scriptBlock($script, $opts);
if ($options['inline']) {
return $return;
}
return null;
}
2009-07-27 01:58:58 +00:00
/**
* Write a script to the buffered scripts.
*
* @param string $script Script string to add to the buffer.
* @param bool $top If true the script will be added to the top of the
2012-12-22 22:48:15 +00:00
* buffered scripts array. If false the bottom.
* @return void
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::buffer
*/
public function buffer($script, $top = false) {
if ($top) {
array_unshift($this->_bufferedScripts, $script);
} else {
$this->_bufferedScripts[] = $script;
}
}
2009-07-27 01:58:58 +00:00
/**
* Get all the buffered scripts
*
* @param bool $clear Whether or not to clear the script caches (default true)
* @return array Array of scripts added to the request.
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::getBuffer
*/
public function getBuffer($clear = true) {
2009-10-15 01:26:43 +00:00
$this->_createVars();
$scripts = $this->_bufferedScripts;
if ($clear) {
$this->_bufferedScripts = array();
$this->_jsVars = array();
}
return $scripts;
}
2009-07-27 01:58:58 +00:00
2009-10-15 01:26:43 +00:00
/**
2012-11-28 22:30:47 +00:00
* Generates the object string for variables passed to javascript and adds to buffer
2009-10-15 01:26:43 +00:00
*
2012-11-28 22:30:47 +00:00
* @return void
*/
protected function _createVars() {
if (!empty($this->_jsVars)) {
$setVar = (strpos($this->setVariable, '.')) ? $this->setVariable : 'window.' . $this->setVariable;
$this->buffer($setVar . ' = ' . $this->object($this->_jsVars) . ';', true);
2009-10-15 01:26:43 +00:00
}
}
2009-07-12 21:30:41 +00:00
/**
2012-12-22 22:48:15 +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
*
* ### Options
2009-07-27 01:58:58 +00:00
*
* - `confirm` - Generate a confirm() dialog before sending the event.
* - `id` - use a custom id.
2012-12-22 22:48:15 +00:00
* - `htmlAttributes` - additional non-standard htmlAttributes. Standard attributes are class, id,
* rel, title, escape, onblur and onfocus.
* - `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.
2013-04-30 12:11:50 +00:00
* @param string|array $url Mixed either a string URL or a CakePHP 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.
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::link
*/
public function link($title, $url = null, $options = array()) {
2009-07-12 21:30:41 +00:00
if (!isset($options['id'])) {
2014-09-10 14:29:23 +00:00
$options['id'] = 'link-' . (int)mt_rand();
2009-07-12 21:30:41 +00:00
}
list($options, $htmlOptions) = $this->_getHtmlOptions($options);
2009-07-12 21:30:41 +00:00
$out = $this->Html->link($title, $url, $htmlOptions);
$this->get('#' . $htmlOptions['id']);
$requestString = $event = '';
if (isset($options['confirm'])) {
$requestString = $this->confirmReturn($options['confirm']);
unset($options['confirm']);
}
$buffer = isset($options['buffer']) ? $options['buffer'] : null;
$safe = isset($options['safe']) ? $options['safe'] : true;
unset($options['buffer'], $options['safe']);
$requestString .= $this->request($url, $options);
2009-07-12 21:30:41 +00:00
if (!empty($requestString)) {
$event = $this->event('click', $requestString, $options + array('buffer' => $buffer));
}
if (isset($buffer) && !$buffer) {
$opts = array('safe' => $safe);
$out .= $this->Html->scriptBlock($event, $opts);
2009-07-12 21:30:41 +00:00
}
return $out;
}
2009-10-15 01:26:43 +00:00
/**
2012-12-22 22:48:15 +00:00
* Pass variables into Javascript. Allows you to set variables that will be
2009-10-15 01:26:43 +00: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`
*
* @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
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::set
*/
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) {
2009-10-15 01:26:43 +00:00
return false;
}
$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
2012-12-22 22:48:15 +00:00
* element that is enhanced with Javascript. Options can include
2009-07-21 04:24:58 +00:00
* both those for FormHelper::submit() and JsBaseEngine::request(), JsBaseEngine::event();
*
* Forms submitting with this method, cannot send files. Files do not transfer over XmlHttpRequest
* and require an iframe or flash.
2009-07-27 01:58:58 +00:00
*
* ### Options
2011-08-16 03:55:08 +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.
* @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.
2017-06-10 22:15:34 +00:00
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::submit
*/
public function submit($caption = null, $options = array()) {
if (!isset($options['id'])) {
2014-09-10 14:29:23 +00:00
$options['id'] = 'submit-' . (int)mt_rand();
}
$formOptions = array('div');
list($options, $htmlOptions) = $this->_getHtmlOptions($options, $formOptions);
$out = $this->Form->submit($caption, $htmlOptions);
$this->get('#' . $htmlOptions['id']);
$options['data'] = $this->serializeForm(array('isForm' => false, 'inline' => true));
$requestString = $url = '';
if (isset($options['confirm'])) {
$requestString = $this->confirmReturn($options['confirm']);
unset($options['confirm']);
}
if (isset($options['url'])) {
$url = $options['url'];
unset($options['url']);
}
if (!isset($options['method'])) {
$options['method'] = 'post';
}
$options['dataExpression'] = true;
$buffer = isset($options['buffer']) ? $options['buffer'] : null;
$safe = isset($options['safe']) ? $options['safe'] : true;
unset($options['buffer'], $options['safe']);
$requestString .= $this->request($url, $options);
if (!empty($requestString)) {
$event = $this->event('click', $requestString, $options + array('buffer' => $buffer));
}
if (isset($buffer) && !$buffer) {
$opts = array('safe' => $safe);
$out .= $this->Html->scriptBlock($event, $opts);
}
return $out;
2009-07-21 04:24:58 +00:00
}
2009-07-12 21:30:41 +00:00
/**
* Parse a set of Options and extract the Html options.
* Extracted Html Options are removed from the $options param.
2009-07-12 21:30:41 +00:00
*
* @param array $options Options to filter.
* @param array $additional Array of additional keys to extract and include in the return options array.
* @return array Array of js options and Htmloptions
*/
protected function _getHtmlOptions($options, $additional = array()) {
$htmlKeys = array_merge(
2011-08-16 03:55:08 +00:00
array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title', 'style'),
$additional
);
2009-07-12 21:30:41 +00:00
$htmlOptions = array();
foreach ($htmlKeys as $key) {
if (isset($options[$key])) {
$htmlOptions[$key] = $options[$key];
}
unset($options[$key]);
2009-07-12 21:30:41 +00:00
}
if (isset($options['htmlAttributes'])) {
$htmlOptions = array_merge($htmlOptions, $options['htmlAttributes']);
unset($options['htmlAttributes']);
}
return array($options, $htmlOptions);
2009-07-12 21:30:41 +00:00
}
2012-03-03 22:10:12 +00:00
}