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

377 lines
11 KiB
PHP
Raw Permalink Normal View History

2009-03-15 21:32:04 +00:00
<?php
/**
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2009-03-15 21:32:04 +00:00
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
2009-03-15 21:32:04 +00:00
* 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.3
* @license https://opensource.org/licenses/mit-license.php MIT License
2009-03-15 21:32:04 +00:00
*/
2010-12-04 18:10:24 +00:00
App::uses('JsBaseEngineHelper', 'View/Helper');
2009-03-15 21:32:04 +00:00
/**
* MooTools Engine Helper for JsHelper
*
* Provides MooTools specific JavaScript for JsHelper.
* Assumes that you have the following MooTools packages
*
* - Remote, Remote.HTML, Remote.JSON
* - Fx, Fx.Tween, Fx.Morph
* - Selectors, DomReady,
* - Drag, Drag.Move
*
* @package Cake.View.Helper
*/
2009-03-15 21:32:04 +00:00
class MootoolsEngineHelper extends JsBaseEngineHelper {
2012-03-03 22:10:12 +00:00
2009-03-15 21:32:04 +00:00
/**
* Option mappings for MooTools
*
* @var array
*/
protected $_optionMap = array(
2009-03-15 21:32:04 +00:00
'request' => array(
'complete' => 'onComplete',
'success' => 'onSuccess',
'before' => 'onRequest',
2009-03-15 21:32:04 +00:00
'error' => 'onFailure'
2009-03-28 04:15:05 +00:00
),
'sortable' => array(
'distance' => 'snap',
'containment' => 'constrain',
'sort' => 'onSort',
'complete' => 'onComplete',
'start' => 'onStart',
2009-04-12 03:09:58 +00:00
),
'drag' => array(
'snapGrid' => 'snap',
'start' => 'onStart',
'drag' => 'onDrag',
'stop' => 'onComplete',
2009-04-12 03:37:24 +00:00
),
'drop' => array(
'drop' => 'onDrop',
'hover' => 'onEnter',
'leave' => 'onLeave',
2009-04-24 17:44:52 +00:00
),
'slider' => array(
'complete' => 'onComplete',
'change' => 'onChange',
'direction' => 'mode',
'step' => 'steps'
2009-03-15 21:32:04 +00:00
)
);
/**
* Contains a list of callback names -> default arguments.
*
* @var array
*/
protected $_callbackArguments = array(
'slider' => array(
'onTick' => 'position',
'onChange' => 'step',
'onComplete' => 'event'
),
'request' => array(
'onRequest' => '',
'onComplete' => '',
'onCancel' => '',
'onSuccess' => 'responseText, responseXML',
'onFailure' => 'xhr',
'onException' => 'headerName, value',
),
'drag' => array(
'onBeforeStart' => 'element',
'onStart' => 'element',
'onSnap' => 'element',
'onDrag' => 'element, event',
'onComplete' => 'element, event',
'onCancel' => 'element',
),
'drop' => array(
'onBeforeStart' => 'element',
'onStart' => 'element',
'onSnap' => 'element',
'onDrag' => 'element, event',
'onComplete' => 'element, event',
'onCancel' => 'element',
'onDrop' => 'element, droppable, event',
'onLeave' => 'element, droppable',
'onEnter' => 'element, droppable',
),
'sortable' => array(
'onStart' => 'element, clone',
'onSort' => 'element, clone',
'onComplete' => 'element',
)
);
2009-03-15 21:32:04 +00:00
/**
* Create javascript selector for a CSS rule
*
* @param string $selector The selector that is targeted
* @return self
*/
public function get($selector) {
$this->_multipleSelection = false;
if ($selector === 'window' || $selector === 'document') {
2012-03-03 22:10:12 +00:00
$this->selection = "$(" . $selector . ")";
return $this;
}
if (preg_match('/^#[^\s.]+$/', $selector)) {
$this->selection = '$("' . substr($selector, 1) . '")';
return $this;
}
$this->_multipleSelection = true;
$this->selection = '$$("' . $selector . '")';
return $this;
2009-03-15 21:32:04 +00:00
}
2009-03-15 21:32:04 +00:00
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
* ### Options
*
* - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
* - 'stop' - Whether you want the event to stopped. (defaults true)
*
* @param string $type Type of event to bind to the current dom id
* @param string $callback The JavaScript function you wish to trigger or the function literal
2009-03-15 21:32:04 +00:00
* @param array $options Options for the event.
* @return string completed event handler
*/
public function event($type, $callback, $options = array()) {
$defaults = array('wrap' => true, 'stop' => true);
$options += $defaults;
$function = 'function (event) {%s}';
if ($options['wrap'] && $options['stop']) {
2009-03-16 01:51:00 +00:00
$callback = "event.stop();\n" . $callback;
}
if ($options['wrap']) {
$callback = sprintf($function, $callback);
}
$out = $this->selection . ".addEvent(\"{$type}\", $callback);";
return $out;
2009-03-15 21:32:04 +00:00
}
2009-03-15 21:32:04 +00:00
/**
* Create a domReady event. This is a special event in many libraries
*
* @param string $functionBody The code to run on domReady
* @return string completed domReady method
*/
public function domReady($functionBody) {
$this->selection = 'window';
return $this->event('domready', $functionBody, array('stop' => false));
2009-03-15 21:32:04 +00:00
}
2009-03-15 21:32:04 +00:00
/**
* Create an iteration over the current selection result.
*
* @param string $callback The function body you wish to apply during the iteration.
* @return string completed iteration
*/
public function each($callback) {
return $this->selection . '.each(function (item, index) {' . $callback . '});';
2009-03-15 21:32:04 +00:00
}
2009-03-15 21:32:04 +00:00
/**
* Trigger an Effect.
*
* @param string $name The name of the effect to trigger.
* @param array $options Array of options for the effect.
* @return string completed string with effect.
* @see JsBaseEngineHelper::effect()
*/
public function effect($name, $options = array()) {
$speed = null;
if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
if ($options['speed'] === 'fast') {
$speed = '"short"';
} elseif ($options['speed'] === 'slow') {
$speed = '"long"';
}
}
$effect = '';
switch ($name) {
case 'hide':
$effect = 'setStyle("display", "none")';
break;
case 'show':
$effect = 'setStyle("display", "")';
break;
case 'fadeIn':
case 'fadeOut':
case 'slideIn':
case 'slideOut':
list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
$direction = strtolower($direction);
if ($speed) {
$effect .= "set(\"$effectName\", {duration:$speed}).";
}
$effect .= "$effectName(\"$direction\")";
break;
}
return $this->selection . '.' . $effect . ';';
2009-03-15 21:32:04 +00:00
}
2009-03-15 21:32:04 +00:00
/**
2014-07-30 20:11:03 +00:00
* Create a new Request.
*
2012-12-22 22:48:15 +00:00
* Requires `Request`. If you wish to use 'update' key you must have ```Request.HTML```
2009-03-28 04:15:05 +00:00
* if you wish to do Json requests you will need ```JSON``` and ```Request.JSON```.
2009-03-15 21:32:04 +00:00
*
* @param string|array $url URL
* @param array $options Options list.
2009-03-15 21:32:04 +00:00
* @return string The completed ajax call.
*/
public function request($url, $options = array()) {
$url = html_entity_decode($this->url($url), ENT_COMPAT, Configure::read('App.encoding'));
2009-03-16 02:15:22 +00:00
$options = $this->_mapOptions('request', $options);
$type = $data = null;
if (isset($options['type']) || isset($options['update'])) {
if (isset($options['type']) && strtolower($options['type']) === 'json') {
$type = '.JSON';
2009-03-16 02:15:22 +00:00
}
if (isset($options['update'])) {
$options['update'] = str_replace('#', '', $options['update']);
$type = '.HTML';
2009-03-16 02:15:22 +00:00
}
unset($options['type']);
}
if (!empty($options['data'])) {
$data = $options['data'];
unset($options['data']);
}
2009-03-16 02:15:22 +00:00
$options['url'] = $url;
$options = $this->_prepareCallbacks('request', $options);
if (!empty($options['dataExpression'])) {
unset($options['dataExpression']);
} elseif (!empty($data)) {
$data = $this->object($data);
}
$options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
2009-03-16 02:15:22 +00:00
return "var jsRequest = new Request$type({{$options}}).send($data);";
2009-03-15 21:32:04 +00:00
}
2009-03-28 04:15:05 +00:00
/**
* Create a sortable element.
*
* Requires the `Sortables` plugin from MootoolsMore
2009-03-28 04:15:05 +00:00
*
* @param array $options Array of options for the sortable.
* @return string Completed sortable script.
2010-03-06 15:46:26 +00:00
* @see JsBaseEngineHelper::sortable() for options list.
*/
public function sortable($options = array()) {
$options = $this->_processOptions('sortable', $options);
return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
2009-03-28 04:15:05 +00:00
}
2009-04-12 03:09:58 +00:00
/**
* Create a Draggable element.
*
* Requires the `Drag` plugin from MootoolsMore
2009-04-12 03:09:58 +00:00
*
* @param array $options Array of options for the draggable.
* @return string Completed draggable script.
* @see JsHelper::drag() for options list.
*/
public function drag($options = array()) {
$options = $this->_processOptions('drag', $options);
return $this->selection . '.makeDraggable({' . $options . '});';
2009-04-12 03:09:58 +00:00
}
2009-04-12 03:37:24 +00:00
/**
* Create a Droppable element.
*
* Requires the `Drag` and `Drag.Move` plugins from MootoolsMore
2009-04-12 03:37:24 +00:00
*
2012-12-22 22:48:15 +00:00
* Droppables in Mootools function differently from other libraries. Droppables
* are implemented as an extension of Drag. So in addition to making a get() selection for
2009-04-12 03:37:24 +00:00
* the droppable element. You must also provide a selector rule to the draggable element. Furthermore,
* Mootools droppables inherit all options from Drag.
*
* @param array $options Array of options for the droppable.
* @return string Completed droppable script.
2010-03-06 15:46:26 +00:00
* @see JsBaseEngineHelper::drop() for options list.
*/
public function drop($options = array()) {
2009-04-12 03:37:24 +00:00
if (empty($options['drag'])) {
trigger_error(
__d('cake_dev', '%s requires a "drag" option to properly function', 'MootoolsEngine::drop()'), E_USER_WARNING
2009-04-12 03:37:24 +00:00
);
return false;
}
$options['droppables'] = $this->selection;
$this->get($options['drag']);
unset($options['drag']);
$options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
$options = $this->_prepareCallbacks('drop', $options);
$safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
$optionString = $this->_parseOptions($options, $safe);
$out = $this->selection . '.makeDraggable({' . $optionString . '});';
2009-04-12 03:37:24 +00:00
$this->selection = $options['droppables'];
return $out;
}
2009-04-16 03:59:13 +00:00
/**
2009-04-24 17:44:52 +00:00
* Create a slider control
2009-04-16 03:59:13 +00:00
*
2009-04-24 17:44:52 +00:00
* Requires `Slider` from MootoolsMore
2009-04-16 03:59:13 +00:00
*
* @param array $options Array of options for the slider.
* @return string Completed slider script.
2010-03-06 15:46:26 +00:00
* @see JsBaseEngineHelper::slider() for options list.
*/
public function slider($options = array()) {
2009-04-24 17:44:52 +00:00
$slider = $this->selection;
$this->get($options['handle']);
unset($options['handle']);
if (isset($options['min']) && isset($options['max'])) {
$options['range'] = array($options['min'], $options['max']);
unset($options['min'], $options['max']);
}
$optionString = $this->_processOptions('slider', $options);
2009-04-24 17:44:52 +00:00
if (!empty($optionString)) {
$optionString = ', {' . $optionString . '}';
}
$out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
$this->selection = $slider;
return $out;
2009-04-16 03:59:13 +00:00
}
/**
* Serialize the form attached to $selector.
*
* @param array $options Array of options.
* @return string Completed serializeForm() snippet
2010-03-06 15:46:26 +00:00
* @see JsBaseEngineHelper::serializeForm()
*/
public function serializeForm($options = array()) {
$options += array('isForm' => false, 'inline' => false);
$selection = $this->selection;
if (!$options['isForm']) {
$selection = '$(' . $this->selection . '.form)';
}
$method = '.toQueryString()';
if (!$options['inline']) {
$method .= ';';
}
return $selection . $method;
}
2012-03-03 22:10:12 +00:00
2009-03-15 21:32:04 +00:00
}