Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 171 lines (169 sloc) 5.324 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
<?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
*/
App::import('Helper', 'Js');

class JqueryEngineHelper extends JsBaseEngineHelper {
/**
* Option mappings for jQuery
*
* @var array
**/
var $_optionMap = array(
'request' => array(
'type' => 'dataType',
'before' => 'beforeSend',
),
'sortable' => array(
'complete' => 'stop',
)
);
/**
* Create javascript selector for a CSS rule
*
* @param string $selector The selector that is targeted
* @return object instance of $this. Allows chained methods.
**/
function get($selector) {
if ($selector == 'window' || $selector == 'document') {
$this->selection = '$(' . $selector .')';
} else {
$this->selection = '$("' . $selector . '")';
}
return $this;
}
/**
* 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
* @param array $options Options for the event.
* @return string completed event handler
**/
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);
}
return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);
}
/**
* 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) {
return $this->get('document')->event('ready', $functionBody, array('stop' => false));
}
/**
* 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 . '});';
}
/**
* 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) {
case 'slideIn':
case 'slideOut':
$name = ($name == 'slideIn') ? 'slideDown' : 'slideUp';
case 'hide':
case 'show':
  case 'fadeIn':
case 'fadeOut':
$effect = ".$name($speed);";
break;
}
return $this->selection . $effect;
}
/**
* Create an $.ajax() call.
*
* If the 'update' key is set, success callback will be overridden.
*
* @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);
if (isset($options['data']) && is_array($options['data'])) {
$options['data'] = $this->_toQuerystring($options['data']);
}
$options['url'] = $url;
if (isset($options['update'])) {
$options['success'] = 'function (msg, status) {$("' . $options['update'] . '").html(msg);}';
unset($options['update']);
}
$callbacks = array('success', 'error', 'beforeSend', 'complete');
$options = $this->_parseOptions($options, $callbacks);
return '$.ajax({' . $options .'});';
}
/**
* 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 . '});';
}
}
?>
Something went wrong with that request. Please try again.