CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CacheHelper
  • FormHelper
  • HtmlHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • MootoolsEngineHelper
  • NumberHelper
  • PaginatorHelper
  • PrototypeEngineHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
  1: <?php
  2: /**
  3:  * Prototype Engine Helper for JsHelper
  4:  *
  5:  * Provides Prototype specific Javascript for JsHelper. Requires at least
  6:  * Prototype 1.6
  7:  *
  8:  * PHP 5
  9:  *
 10:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 11:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 12:  *
 13:  * Licensed under The MIT License
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP(tm) Project
 18:  * @package       Cake.View.Helper
 19:  * @since         CakePHP(tm) v 1.3
 20:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 21:  */
 22: 
 23: App::uses('JsBaseEngineHelper', 'View/Helper');
 24: 
 25: /**
 26:  * Prototype Engine Helper for JsHelper
 27:  *
 28:  * Provides Prototype specific Javascript for JsHelper. Requires at least
 29:  * Prototype 1.6
 30:  *
 31:  * @package       Cake.View.Helper
 32:  */
 33: class PrototypeEngineHelper extends JsBaseEngineHelper {
 34: 
 35: /**
 36:  * Is the current selection a multiple selection? or is it just a single element.
 37:  *
 38:  * @var boolean
 39:  */
 40:     protected $_multiple = false;
 41: 
 42: /**
 43:  * Option mappings for Prototype
 44:  *
 45:  * @var array
 46:  */
 47:     protected $_optionMap = array(
 48:         'request' => array(
 49:             'async' => 'asynchronous',
 50:             'data' => 'parameters',
 51:             'before' => 'onCreate',
 52:             'success' => 'onSuccess',
 53:             'complete' => 'onComplete',
 54:             'error' => 'onFailure'
 55:         ),
 56:         'sortable' => array(
 57:             'sort' => 'onChange',
 58:             'complete' => 'onUpdate',
 59:         ),
 60:         'drag' => array(
 61:             'snapGrid' => 'snap',
 62:             'container' => 'constraint',
 63:             'stop' => 'onEnd',
 64:             'start' => 'onStart',
 65:             'drag' => 'onDrag',
 66:         ),
 67:         'drop' => array(
 68:             'hover' => 'onHover',
 69:             'drop' => 'onDrop',
 70:             'hoverClass' => 'hoverclass',
 71:         ),
 72:         'slider' => array(
 73:             'direction' => 'axis',
 74:             'change' => 'onSlide',
 75:             'complete' => 'onChange',
 76:             'value' => 'sliderValue',
 77:         )
 78:     );
 79: 
 80: /**
 81:  * Contains a list of callback names -> default arguments.
 82:  *
 83:  * @var array
 84:  */
 85:     protected $_callbackArguments = array(
 86:         'slider' => array(
 87:             'onSlide' => 'value',
 88:             'onChange' => 'value',
 89:         ),
 90:         'drag' => array(
 91:             'onStart' => 'event',
 92:             'onDrag' => 'event',
 93:             'change' => 'draggable',
 94:             'onEnd' => 'event',
 95:         ),
 96:         'drop' => array(
 97:             'onHover' => 'draggable, droppable, event',
 98:             'onDrop' => 'draggable, droppable, event',
 99:         ),
100:         'request' => array(
101:             'onCreate' => 'transport',
102:             'onComplete' => 'transport',
103:             'onFailure' => 'response, jsonHeader',
104:             'onRequest' => 'transport',
105:             'onSuccess' => 'response, jsonHeader'
106:         ),
107:         'sortable' => array(
108:             'onStart' => 'element',
109:             'onChange' => 'element',
110:             'onUpdate' => 'element',
111:         ),
112:     );
113: 
114: /**
115:  * Create javascript selector for a CSS rule
116:  *
117:  * @param string $selector The selector that is targeted
118:  * @return PrototypeEngineHelper instance of $this. Allows chained methods.
119:  */
120:     public function get($selector) {
121:         $this->_multiple = false;
122:         if ($selector == 'window' || $selector == 'document') {
123:             $this->selection = "$(" . $selector . ")";
124:             return $this;
125:         }
126:         if (preg_match('/^#[^\s.]+$/', $selector)) {
127:             $this->selection = '$("' . substr($selector, 1) . '")';
128:             return $this;
129:         }
130:         $this->_multiple = true;
131:         $this->selection = '$$("' . $selector . '")';
132:         return $this;
133:     }
134: 
135: /**
136:  * Add an event to the script cache. Operates on the currently selected elements.
137:  *
138:  * ### Options
139:  *
140:  * - `wrap` - Whether you want the callback wrapped in an anonymous function. (defaults true)
141:  * - `stop` - Whether you want the event to stopped. (defaults true)
142:  *
143:  * @param string $type Type of event to bind to the current 946 id
144:  * @param string $callback The Javascript function you wish to trigger or the function literal
145:  * @param array $options Options for the event.
146:  * @return string completed event handler
147:  */
148:     public function event($type, $callback, $options = array()) {
149:         $defaults = array('wrap' => true, 'stop' => true);
150:         $options = array_merge($defaults, $options);
151: 
152:         $function = 'function (event) {%s}';
153:         if ($options['wrap'] && $options['stop']) {
154:             $callback = "event.stop();\n" . $callback;
155:         }
156:         if ($options['wrap']) {
157:             $callback = sprintf($function, $callback);
158:         }
159:         $out = $this->selection . ".observe(\"{$type}\", $callback);";
160:         return $out;
161:     }
162: 
163: /**
164:  * Create a domReady event. This is a special event in many libraries
165:  *
166:  * @param string $functionBody The code to run on domReady
167:  * @return string completed domReady method
168:  */
169:     public function domReady($functionBody) {
170:         $this->selection = 'document';
171:         return $this->event('dom:loaded', $functionBody, array('stop' => false));
172:     }
173: 
174: /**
175:  * Create an iteration over the current selection result.
176:  *
177:  * @param string $callback The function body you wish to apply during the iteration.
178:  * @return string completed iteration
179:  */
180:     public function each($callback) {
181:         return $this->selection . '.each(function (item, index) {' . $callback . '});';
182:     }
183: 
184: /**
185:  * Trigger an Effect.
186:  *
187:  * ### Note: Effects require Scriptaculous to be loaded.
188:  *
189:  * @param string $name The name of the effect to trigger.
190:  * @param array $options Array of options for the effect.
191:  * @return string completed string with effect.
192:  * @see JsBaseEngineHelper::effect()
193:  */
194:     public function effect($name, $options = array()) {
195:         $effect = '';
196:         $optionString = null;
197:         if (isset($options['speed'])) {
198:             if ($options['speed'] == 'fast') {
199:                 $options['duration'] = 0.5;
200:             } elseif ($options['speed'] == 'slow') {
201:                 $options['duration'] = 2;
202:             } else {
203:                 $options['duration'] = 1;
204:             }
205:             unset($options['speed']);
206:         }
207:         if (!empty($options)) {
208:             $optionString = ', {' . $this->_parseOptions($options) . '}';
209:         }
210:         switch ($name) {
211:             case 'hide':
212:             case 'show':
213:                 $effect = $this->selection . '.' . $name . '();';
214:             break;
215:             case 'slideIn':
216:             case 'slideOut':
217:                 $name = ($name == 'slideIn') ? 'slideDown' : 'slideUp';
218:                 $effect = 'Effect.' . $name . '(' . $this->selection . $optionString . ');';
219:             break;
220:             case 'fadeIn':
221:             case 'fadeOut':
222:                 $name = ($name == 'fadeIn') ? 'appear' : 'fade';
223:                 $effect = $this->selection . '.' . $name . '(' . substr($optionString, 2) . ');';
224:             break;
225:         }
226:         return $effect;
227:     }
228: 
229: /**
230:  * Create an Ajax or Ajax.Updater call.
231:  *
232:  * @param string|array $url
233:  * @param array $options
234:  * @return string The completed ajax call.
235:  */
236:     public function request($url, $options = array()) {
237:         $url = html_entity_decode($this->url($url), ENT_COMPAT, Configure::read('App.encoding'));
238:         $url = '"' . $url . '"';
239:         $options = $this->_mapOptions('request', $options);
240:         $type = '.Request';
241:         if (isset($options['type']) && strtolower($options['type']) == 'json') {
242:             unset($options['type']);
243:         }
244:         if (isset($options['update'])) {
245:             $url = '"' . str_replace('#', '', $options['update']) . '", ' . $url;
246:             $type = '.Updater';
247:             unset($options['update'], $options['type']);
248:         }
249:         $safe = array_keys($this->_callbackArguments['request']);
250:         $options = $this->_prepareCallbacks('request', $options, $safe);
251:         if (!empty($options['dataExpression'])) {
252:             $safe[] = 'parameters';
253:             unset($options['dataExpression']);
254:         }
255:         $options = $this->_parseOptions($options, $safe);
256:         if (!empty($options)) {
257:             $options = ', {' . $options . '}';
258:         }
259:         return "var jsRequest = new Ajax$type($url$options);";
260:     }
261: 
262: /**
263:  * Create a sortable element.
264:  *
265:  * #### Note: Requires scriptaculous to be loaded.
266:  *
267:  * The scriptaculous implementation of sortables does not support the 'start'
268:  * and 'distance' options.
269:  *
270:  * @param array $options Array of options for the sortable.
271:  * @return string Completed sortable script.
272:  * @see JsBaseEngineHelper::sortable() for options list.
273:  */
274:     public function sortable($options = array()) {
275:         $options = $this->_processOptions('sortable', $options);
276:         if (!empty($options)) {
277:             $options = ', {' . $options . '}';
278:         }
279:         return 'var jsSortable = Sortable.create(' . $this->selection . $options . ');';
280:     }
281: 
282: /**
283:  * Create a Draggable element.
284:  *
285:  * #### Note: Requires scriptaculous to be loaded.
286:  *
287:  * @param array $options Array of options for the draggable.
288:  * @return string Completed draggable script.
289:  * @see JsBaseEngineHelper::draggable() for options list.
290:  */
291:     public function drag($options = array()) {
292:         $options = $this->_processOptions('drag', $options);
293:         if (!empty($options)) {
294:             $options = ', {' . $options . '}';
295:         }
296:         if ($this->_multiple) {
297:             return $this->each('new Draggable(item' . $options . ');');
298:         }
299:         return 'var jsDrag = new Draggable(' . $this->selection . $options . ');';
300:     }
301: 
302: /**
303:  * Create a Droppable element.
304:  *
305:  * #### Note: Requires scriptaculous to be loaded.
306:  *
307:  * @param array $options Array of options for the droppable.
308:  * @return string Completed droppable script.
309:  * @see JsBaseEngineHelper::droppable() for options list.
310:  */
311:     public function drop($options = array()) {
312:         $options = $this->_processOptions('drop', $options);
313:         if (!empty($options)) {
314:             $options = ', {' . $options . '}';
315:         }
316:         return 'Droppables.add(' . $this->selection . $options . ');';
317:     }
318: 
319: /**
320:  * Creates a slider control widget.
321:  *
322:  * ### Note: Requires scriptaculous to be loaded.
323:  *
324:  * @param array $options Array of options for the slider.
325:  * @return string Completed slider script.
326:  * @see JsBaseEngineHelper::slider() for options list.
327:  */
328:     public function slider($options = array()) {
329:         $slider = $this->selection;
330:         $this->get($options['handle']);
331:         unset($options['handle']);
332: 
333:         if (isset($options['min']) && isset($options['max'])) {
334:             $options['range'] = sprintf('$R(%s,%s)', $options['min'], $options['max']);
335:             unset($options['min'], $options['max']);
336:         }
337:         $options = $this->_mapOptions('slider', $options);
338:         $options = $this->_prepareCallbacks('slider', $options);
339:         $optionString = $this->_parseOptions(
340:             $options, array_merge(array_keys($this->_callbackArguments['slider']), array('range'))
341:         );
342:         if (!empty($optionString)) {
343:             $optionString = ', {' . $optionString . '}';
344:         }
345:         $out = 'var jsSlider = new Control.Slider(' . $this->selection . ', ' . $slider . $optionString . ');';
346:         $this->selection = $slider;
347:         return $out;
348:     }
349: 
350: /**
351:  * Serialize the form attached to $selector.
352:  *
353:  * @param array $options Array of options.
354:  * @return string Completed serializeForm() snippet
355:  * @see JsBaseEngineHelper::serializeForm()
356:  */
357:     public function serializeForm($options = array()) {
358:         $options = array_merge(array('isForm' => false, 'inline' => false), $options);
359:         $selection = $this->selection;
360:         if (!$options['isForm']) {
361:             $selection = '$(' . $this->selection . '.form)';
362:         }
363:         $method = '.serialize()';
364:         if (!$options['inline']) {
365:             $method .= ';';
366:         }
367:         return $selection . $method;
368:     }
369: 
370: }
371: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs