2009-03-09 13:25:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* jQuery Engine Helper for JsHelper
|
|
|
|
*
|
|
|
|
* Provides jQuery specific Javascript for JsHelper.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
|
|
|
* Copyright 2006-2008, Cake Software Foundation, Inc.
|
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
|
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.
|
|
|
|
* @version
|
|
|
|
* @modifiedby
|
|
|
|
* @lastmodified
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2009-03-14 05:57:24 +00:00
|
|
|
App::import('Helper', 'Js');
|
|
|
|
|
2009-03-14 17:48:31 +00:00
|
|
|
class JqueryEngineHelper extends JsBaseEngineHelper {
|
2009-03-15 17:08:29 +00:00
|
|
|
/**
|
|
|
|
* Option mappings for jQuery
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
**/
|
|
|
|
var $_optionMap = array(
|
|
|
|
'request' => array(
|
|
|
|
'type' => 'dataType',
|
|
|
|
'complete' => 'success',
|
|
|
|
'request' => 'beforeSend',
|
2009-03-28 03:44:57 +00:00
|
|
|
),
|
|
|
|
'sortable' => array(
|
|
|
|
'complete' => 'stop',
|
2009-03-15 17:08:29 +00:00
|
|
|
)
|
|
|
|
);
|
2009-03-14 00:50:31 +00:00
|
|
|
/**
|
|
|
|
* Create javascript selector for a CSS rule
|
|
|
|
*
|
|
|
|
* @param string $selector The selector that is targeted
|
|
|
|
* @return object instance of $this. Allows chained methods.
|
|
|
|
**/
|
2009-03-15 21:48:27 +00:00
|
|
|
function get($selector) {
|
2009-03-14 00:50:31 +00:00
|
|
|
if ($selector == 'window' || $selector == 'document') {
|
2009-03-16 01:44:40 +00:00
|
|
|
$this->selection = '$(' . $selector .')';
|
2009-03-14 00:50:31 +00:00
|
|
|
} else {
|
2009-03-16 01:44:40 +00:00
|
|
|
$this->selection = '$("' . $selector . '")';
|
2009-03-14 00:50:31 +00:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Add an event to the script cache. Operates on the currently selected elements.
|
|
|
|
*
|
2009-03-15 16:10:46 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
|
|
|
|
* - 'stop' - Whether you want the event to stopped. (defaults true)
|
|
|
|
*
|
2009-03-14 00:50:31 +00:00
|
|
|
* @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 16:10:46 +00:00
|
|
|
* @param array $options Options for the event.
|
2009-03-14 00:50:31 +00:00
|
|
|
* @return string completed event handler
|
|
|
|
**/
|
2009-03-15 16:10:46 +00:00
|
|
|
function event($type, $callback, $options = array()) {
|
|
|
|
$defaults = array('wrap' => true, 'stop' => true);
|
|
|
|
$options = array_merge($defaults, $options);
|
|
|
|
|
|
|
|
$function = 'function (event) {%s}';
|
|
|
|
if ($options['wrap'] && $options['stop']) {
|
|
|
|
$callback .= "\nreturn false;";
|
|
|
|
}
|
|
|
|
if ($options['wrap']) {
|
|
|
|
$callback = sprintf($function, $callback);
|
2009-03-14 00:50:31 +00:00
|
|
|
}
|
2009-03-16 01:44:40 +00:00
|
|
|
return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);
|
2009-03-14 00:50:31 +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
|
|
|
|
**/
|
|
|
|
function domReady($functionBody) {
|
2009-03-15 16:10:46 +00:00
|
|
|
return $this->get('document')->event('ready', $functionBody, array('stop' => false));
|
2009-03-14 00:50:31 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create an iteration over the current selection result.
|
|
|
|
*
|
|
|
|
* @param string $method The method you want to apply to the selection
|
|
|
|
* @param string $callback The function body you wish to apply during the iteration.
|
|
|
|
* @return string completed iteration
|
|
|
|
**/
|
|
|
|
function each($callback) {
|
|
|
|
return $this->selection . '.each(function () {' . $callback . '});';
|
|
|
|
}
|
2009-03-14 05:57:24 +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()
|
|
|
|
**/
|
|
|
|
function effect($name, $options = array()) {
|
|
|
|
$speed = null;
|
|
|
|
if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
|
|
|
|
$speed = $this->value($options['speed']);
|
|
|
|
}
|
|
|
|
$effect = '';
|
|
|
|
switch ($name) {
|
2009-03-29 04:28:15 +00:00
|
|
|
case 'slideIn':
|
|
|
|
case 'slideOut':
|
|
|
|
$name = ($name == 'slideIn') ? 'slideDown' : 'slideUp';
|
2009-03-14 05:57:24 +00:00
|
|
|
case 'hide':
|
|
|
|
case 'show':
|
2009-03-29 04:28:15 +00:00
|
|
|
case 'fadeIn':
|
2009-03-14 05:57:24 +00:00
|
|
|
case 'fadeOut':
|
|
|
|
$effect = ".$name($speed);";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $this->selection . $effect;
|
|
|
|
}
|
2009-03-14 21:01:49 +00:00
|
|
|
/**
|
|
|
|
* Create an $.ajax() call.
|
|
|
|
*
|
|
|
|
* @param mixed $url
|
|
|
|
* @param array $options
|
|
|
|
* @return string The completed ajax call.
|
|
|
|
**/
|
|
|
|
function request($url, $options = array()) {
|
|
|
|
$url = $this->url($url);
|
|
|
|
$options = $this->_mapOptions('request', $options);
|
2009-03-15 16:26:12 +00:00
|
|
|
if (isset($options['data']) && is_array($options['data'])) {
|
2009-03-15 17:08:29 +00:00
|
|
|
$options['data'] = $this->_toQuerystring($options['data']);
|
2009-03-15 16:26:12 +00:00
|
|
|
}
|
|
|
|
$options['url'] = $url;
|
2009-03-15 17:08:29 +00:00
|
|
|
$callbacks = array('success', 'error', 'beforeSend', 'complete');
|
|
|
|
$options = $this->_parseOptions($options, $callbacks);
|
2009-03-15 16:26:12 +00:00
|
|
|
return '$.ajax({' . $options .'});';
|
2009-03-14 21:01:49 +00:00
|
|
|
}
|
2009-03-28 03:44:57 +00:00
|
|
|
/**
|
|
|
|
* Create a sortable element.
|
|
|
|
*
|
|
|
|
* Requires both Ui.Core and Ui.Sortables to be loaded.
|
|
|
|
*
|
|
|
|
* @param array $options Array of options for the sortable.
|
|
|
|
* @return string Completed sortable script.
|
|
|
|
* @see JsHelper::sortable() for options list.
|
|
|
|
**/
|
|
|
|
function sortable($options = array()) {
|
|
|
|
$options = $this->_mapOptions('sortable', $options);
|
|
|
|
$callbacks = array('start', 'sort', 'change', 'beforeStop', 'stop', 'update', 'receive', 'remove',
|
|
|
|
'over', 'out', 'activate', 'deactivate');
|
|
|
|
$options = $this->_parseOptions($options, $callbacks);
|
|
|
|
return $this->selection . '.sortable({' . $options . '});';
|
|
|
|
}
|
2009-03-09 13:25:39 +00:00
|
|
|
}
|
|
|
|
?>
|