From 35dbe414ed406645a6f98166989317c4a0f86d8e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 24 Apr 2009 13:44:52 -0400 Subject: [PATCH] Merging with self. --- cake/libs/view/helpers/jquery_engine.php | 24 ++- cake/libs/view/helpers/js.php | 197 ++---------------- cake/libs/view/helpers/mootools_engine.php | 40 +++- cake/libs/view/helpers/prototype_engine.php | 40 +++- .../libs/view/helpers/jquery_engine.test.php | 18 ++ .../view/helpers/mootools_engine.test.php | 29 +++ .../view/helpers/prototype_engine.test.php | 17 ++ 7 files changed, 169 insertions(+), 196 deletions(-) diff --git a/cake/libs/view/helpers/jquery_engine.php b/cake/libs/view/helpers/jquery_engine.php index 57eb588f09b6..a157e90c9aa7 100644 --- a/cake/libs/view/helpers/jquery_engine.php +++ b/cake/libs/view/helpers/jquery_engine.php @@ -47,8 +47,13 @@ class JqueryEngineHelper extends JsBaseEngineHelper { 'drop' => array( 'leave' => 'out', 'hover' => 'over' + ), + 'slider' => array( + 'complete' => 'stop', + 'direction' => 'orientation' ) ); + /** * Helper function to wrap repetitive simple method templating. * @@ -65,6 +70,7 @@ function _methodTemplate($method, $template, $options, $callbacks) { $options = $this->_parseOptions($options, $callbacks); return sprintf($template, $this->selection, $options); } + /** * Create javascript selector for a CSS rule * @@ -79,6 +85,7 @@ function get($selector) { } return $this; } + /** * Add an event to the script cache. Operates on the currently selected elements. * @@ -105,6 +112,7 @@ function event($type, $callback, $options = array()) { } return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback); } + /** * Create a domReady event. This is a special event in many libraries * @@ -114,6 +122,7 @@ function event($type, $callback, $options = array()) { function domReady($functionBody) { return $this->get('document')->event('ready', $functionBody, array('stop' => false)); } + /** * Create an iteration over the current selection result. * @@ -124,6 +133,7 @@ function domReady($functionBody) { function each($callback) { return $this->selection . '.each(function () {' . $callback . '});'; } + /** * Trigger an Effect. * @@ -151,6 +161,7 @@ function effect($name, $options = array()) { } return $this->selection . $effect; } + /** * Create an $.ajax() call. * @@ -175,6 +186,7 @@ function request($url, $options = array()) { $options = $this->_parseOptions($options, $callbacks); return '$.ajax({' . $options .'});'; } + /** * Create a sortable element. * @@ -190,6 +202,7 @@ function sortable($options = array()) { $template = '%s.sortable({%s});'; return $this->_methodTemplate('sortable', $template, $options, $callbacks); } + /** * Create a Draggable element * @@ -204,6 +217,7 @@ function drag($options = array()) { $template = '%s.draggable({%s});'; return $this->_methodTemplate('drag', $template, $options, $callbacks); } + /** * Create a Droppable element * @@ -223,12 +237,14 @@ function drop($options = array()) { * * Requires the Ui.Core and Ui.Slider to be loaded. * - * @param array $options Array of options for the slider. - * @return string Completed slider script. + * @param array $options Array of options for the slider element. + * @return string Completed Slider script. * @see JsHelper::slider() for options list. **/ function slider($options = array()) { - + $callbacks = array('start', 'change', 'slide', 'stop'); + $template = '%s.slider({%s});'; + return $this->_methodTemplate('slider', $template, $options, $callbacks); } } -?> \ No newline at end of file +?> diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index 58202dc8328c..39505d54f43d 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -588,14 +588,22 @@ function sortable() { * * ### Options * - * ### Event Options + * - handle - The id of the element used in sliding. + * - direction - The direction of the slider either 'vertical' or 'horizontal' + * - min - The min value for the slider. + * - max - The max value for the slider. + * - step - The number of steps or ticks the slider will have. + * - value - The initial offset of the slider. + * + * ### Events * + * - change - Fired when the slider's value is updated + * - complete - Fired when the user stops sliding the handle * - * @param array $options Array of options for the slider. See above. - * @return string Completed slider script. + * @return string Completed slider script **/ function slider() { - trigger_error(sprintf(__('%s does not have slider() implemented', true), get_class($this)), E_USER_WARNING); + trigger_error(sprintf(__('%s does not have slider() implemented', true), get_class($this)), E_USER_WARNING); } /** * Parse an options assoc array into an Javascript object literal. @@ -659,183 +667,4 @@ function _toQuerystring($parameters) { return $out; } } - - -class JsHelperObject { - var $__parent = null; - - var $id = null; - - var $pattern = null; - - var $real = null; - - function __construct(&$parent) { - if (is_object($parent)) { - $this->setParent($parent); - } - } - - function toString() { - return $this->__toString(); - } - - function __toString() { - return $this->literal; - } - - function ref($ref = null) { - if ($ref == null) { - foreach (array('id', 'pattern', 'real') as $ref) { - if ($this->{$ref} !== null) { - return $this->{$ref}; - } - } - } else { - return ($this->{$ref} !== null); - } - return null; - } - - function literal($append = null) { - if (!empty($this->id)) { - $data = '$("' . $this->id . '")'; - } - if (!empty($this->pattern)) { - $data = '$$("' . $this->pattern . '")'; - } - if (!empty($this->real)) { - $data = $this->real; - } - if (!empty($append)) { - $data .= '.' . $append; - } - return $data; - } - - function __call($name, $args) { - $data = ''; - - if (isset($this->__parent->effectMap[strtolower($name)])) { - array_unshift($args, $this->__parent->effectMap[strtolower($name)]); - $name = 'effect'; - } - - switch ($name) { - case 'effect': - case 'visualEffect': - - if (strpos($args[0], '_') || $args[0]{0} != strtoupper($args[0]{0})) { - $args[0] = Inflector::camelize($args[0]); - } - - if (strtolower($args[0]) == 'highlight') { - $data .= 'new '; - } - if ($this->pattern == null) { - $data .= 'Effect.' . $args[0] . '(' . $this->literal(); - } else { - $data .= 'Effect.' . $args[0] . '(item'; - } - - if (isset($args[1]) && is_array($args[1])) { - $data .= ', {' . $this->__options($args[1]) . '}'; - } - $data .= ');'; - - if ($this->pattern !== null) { - $data = $this->each($data); - } - break; - case 'remove': - case 'toggle': - case 'show': - case 'hide': - if (empty($args)) { - $obj = 'Element'; - $params = ''; - } else { - $obj = 'Effect'; - $params = ', "' . $args[0] . '"'; - } - - if ($this->pattern != null) { - $data = $this->each($obj . ".{$name}(item);"); - } else { - $data = $obj . ".{$name}(" . $this->literal() . ');'; - } - break; - case 'visible': - $data = $this->literal() . '.visible();'; - break; - case 'update': - $data = $this->literal() . ".update({$args[0]});"; - break; - case 'load': - $data = 'new Ajax.Updater("' . $this->id . '", "' . $args[0] . '"'; - if (isset($args[1]) && is_array($args[1])) { - $data .= ', {' . $this->__options($args[1]) . '}'; - } - $data .= ');'; - break; - case 'each': - case 'all': - case 'any': - case 'detect': - case 'findAll': - if ($this->pattern != null) { - $data = $this->__iterate($name, $args[0]); - } - break; - case 'addClass': - case 'removeClass': - case 'hasClass': - case 'toggleClass': - $data = $this->literal() . ".{$name}Name(\"{$args[0]}\");"; - break; - case 'clone': - case 'inspect': - case 'keys': - case 'values': - $data = "Object.{$name}(" . $this->literal() . ");"; - break; - case 'extend': - $data = "Object.extend(" . $this->literal() . ", {$args[0]});"; - break; - case '...': - // Handle other methods here - // including interfaces to load other files on-the-fly - // that add support for additional methods/replacing existing methods - break; - default: - $data = $this->literal() . '.' . $name . '();'; - break; - } - - if ($this->__parent->output) { - echo $data; - } else { - return $data; - } - } - - function __iterate($method, $data) { - return '$$("' . $this->pattern . '").' . $method . '(function(item) {' . $data . '});'; - } - - function setParent(&$parent) { - $this->__parent =& $parent; - } - - function __options($opts) { - $options = array(); - foreach ($opts as $key => $val) { - if (!is_int($val)) { - $val = '"' . $val . '"'; - } - $options[] = $key . ':' . $val; - } - return join(', ', $options); - } -} -?> \ No newline at end of file +?> diff --git a/cake/libs/view/helpers/mootools_engine.php b/cake/libs/view/helpers/mootools_engine.php index 86a5aef2b351..10b118a6b5a9 100644 --- a/cake/libs/view/helpers/mootools_engine.php +++ b/cake/libs/view/helpers/mootools_engine.php @@ -59,8 +59,15 @@ class MootoolsEngineHelper extends JsBaseEngineHelper { 'drop' => 'onDrop', 'hover' => 'onEnter', 'leave' => 'onLeave', + ), + 'slider' => array( + 'complete' => 'onComplete', + 'change' => 'onChange', + 'direction' => 'mode', + 'step' => 'steps' ) ); + /** * Create javascript selector for a CSS rule * @@ -79,6 +86,7 @@ function get($selector) { $this->selection = '$$("' . $selector . '")'; return $this; } + /** * Add an event to the script cache. Operates on the currently selected elements. * @@ -106,6 +114,7 @@ function event($type, $callback, $options = array()) { $out = $this->selection . ".addEvent(\"{$type}\", $callback);"; return $out; } + /** * Create a domReady event. This is a special event in many libraries * @@ -116,6 +125,7 @@ function domReady($functionBody) { $this->selection = 'window'; return $this->event('domready', $functionBody, array('stop' => false)); } + /** * Create an iteration over the current selection result. * @@ -126,6 +136,7 @@ function domReady($functionBody) { function each($callback) { return $this->selection . '.each(function (item, index) {' . $callback . '});'; } + /** * Trigger an Effect. * @@ -165,6 +176,7 @@ function effect($name, $options = array()) { } return $this->selection . '.' . $effect . ';'; } + /** * Create an new Request. * @@ -201,6 +213,7 @@ function request($url, $options = array()) { $options = $this->_parseOptions($options, $callbacks); return "var jsRequest = new Request$type({{$options}}).send($data);"; } + /** * Create a sortable element. * @@ -216,6 +229,7 @@ function sortable($options = array()) { $options = $this->_parseOptions($options, $callbacks); return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});'; } + /** * Create a Draggable element. * @@ -231,6 +245,7 @@ function drag($options = array()) { $options = $this->_parseOptions($options, $callbacks); return 'var jsDrag = new Drag(' . $this->selection . ', {' . $options . '});'; } + /** * Create a Droppable element. * @@ -270,17 +285,34 @@ function drop($options = array()) { $this->selection = $options['droppables']; return $out; } + /** - * Slider + * Create a slider control * - * Requires the `Slider` plugin from MootoolsMore + * Requires `Slider` from MootoolsMore * * @param array $options Array of options for the slider. * @return string Completed slider script. * @see JsHelper::slider() for options list. **/ function slider($options = array()) { - + $slider = $this->selection; + $this->get($options['handle']); + unset($options['handle']); + + $callbacks = array('onStart', 'onTick', 'onChange', 'onComplete'); + $options = $this->_mapOptions('slider', $options); + if (isset($options['min']) && isset($options['max'])) { + $options['range'] = array($options['min'], $options['max']); + unset($options['min'], $options['max']); + } + $optionString = $this->_parseOptions($options, $callbacks); + if (!empty($optionString)) { + $optionString = ', {' . $optionString . '}'; + } + $out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');'; + $this->selection = $slider; + return $out; } } -?> \ No newline at end of file +?> diff --git a/cake/libs/view/helpers/prototype_engine.php b/cake/libs/view/helpers/prototype_engine.php index e6531a55c973..245c63adfa99 100644 --- a/cake/libs/view/helpers/prototype_engine.php +++ b/cake/libs/view/helpers/prototype_engine.php @@ -59,8 +59,15 @@ class PrototypeEngineHelper extends JsBaseEngineHelper { 'hover' => 'onHover', 'drop' => 'onDrop', 'hoverClass' => 'hoverclass', + ), + 'slider' => array( + 'direction' => 'axis', + 'change' => 'onSlide', + 'complete' => 'onChange', + 'value' => 'sliderValue', ) ); + /** * Create javascript selector for a CSS rule * @@ -79,6 +86,7 @@ function get($selector) { $this->selection = '$$("' . $selector . '")'; return $this; } + /** * Add an event to the script cache. Operates on the currently selected elements. * @@ -106,6 +114,7 @@ function event($type, $callback, $options = array()) { $out = $this->selection . ".observe(\"{$type}\", $callback);"; return $out; } + /** * Create a domReady event. This is a special event in many libraries * @@ -116,6 +125,7 @@ function domReady($functionBody) { $this->selection = 'document'; return $this->event('dom:loaded', $functionBody, array('stop' => false)); } + /** * Create an iteration over the current selection result. * @@ -126,6 +136,7 @@ function domReady($functionBody) { function each($callback) { return $this->selection . '.each(function (item, index) {' . $callback . '});'; } + /** * Trigger an Effect. * @@ -170,6 +181,7 @@ function effect($name, $options = array()) { } return $effect; } + /** * Create an Ajax or Ajax.Updater call. * @@ -197,6 +209,7 @@ function request($url, $options = array()) { } return "var jsRequest = new Ajax$type($url$options);"; } + /** * Create a sortable element. * @@ -215,6 +228,7 @@ function sortable($options = array()) { } return 'var jsSortable = Sortable.create(' . $this->selection . $options . ');'; } + /** * Create a Draggable element. * @@ -233,13 +247,14 @@ function drag($options = array()) { } return 'var jsDrag = new Draggable(' . $this->selection . $options . ');'; } + /** * Create a Droppable element. * * #### Note: Requires scriptaculous to be loaded. * * @param array $options Array of options for the droppable. - * @return string Completed draggable script. + * @return string Completed droppable script. * @see JsHelper::droppable() for options list. **/ function drop($options = array()) { @@ -251,17 +266,34 @@ function drop($options = array()) { } return 'Droppables.add(' . $this->selection . $options . ');'; } + /** - * Slider + * Creates a slider control widget. * - * Requires the Scriptaculous to be loaded. + * ### Note: Requires scriptaculous to be loaded. * * @param array $options Array of options for the slider. * @return string Completed slider script. * @see JsHelper::slider() for options list. **/ function slider($options = array()) { + $slider = $this->selection; + $this->get($options['handle']); + unset($options['handle']); + $callbacks = array('onSlide', 'onChange'); + $options = $this->_mapOptions('slider', $options); + if (isset($options['min']) && isset($options['max'])) { + $options['range'] = array($options['min'], $options['max']); + unset($options['min'], $options['max']); + } + $optionString = $this->_parseOptions($options, $callbacks); + if (!empty($optionString)) { + $optionString = ', {' . $optionString . '}'; + } + $out = 'var jsSlider = new Control.Slider(' . $this->selection . ', ' . $slider . $optionString . ');'; + $this->selection = $slider; + return $out; } } -?> \ No newline at end of file +?> diff --git a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php index 6e50ad356313..e12a6a4ef300 100644 --- a/cake/tests/cases/libs/view/helpers/jquery_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/jquery_engine.test.php @@ -217,5 +217,23 @@ function testDrop() { $expected = '$("#element").droppable({accept:".items", drop:onDrop, out:onExit, over:onHover});'; $this->assertEqual($result, $expected); } +/** + * test slider generation + * + * @return void + **/ + function testSlider() { + $this->Jquery->get('#element'); + $result = $this->Jquery->slider(array( + 'complete' => 'onComplete', + 'change' => 'onChange', + 'min' => 0, + 'max' => 10, + 'value' => 2, + 'direction' => 'vertical' + )); + $expected = '$("#element").slider({change:onChange, max:10, min:0, orientation:"vertical", stop:onComplete, value:2});'; + $this->assertEqual($result, $expected); + } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/view/helpers/mootools_engine.test.php b/cake/tests/cases/libs/view/helpers/mootools_engine.test.php index 8c24d8736457..44b27af7a0ac 100644 --- a/cake/tests/cases/libs/view/helpers/mootools_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/mootools_engine.test.php @@ -250,5 +250,34 @@ function testDrop() { $this->assertEqual($result, $expected); $this->assertEqual($this->Moo->selection, '$("drop-me")'); } +/** + * test slider generation + * + * @return void + **/ + function testSlider() { + $this->Moo->get('#slider'); + $result = $this->Moo->slider(array( + 'handle' => '#my-handle', + 'complete' => 'onComplete', + 'change' => 'onChange', + 'direction' => 'horizontal', + )); + $expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:onChange, onComplete:onComplete});'; + $this->assertEqual($result, $expected); + $this->assertEqual($this->Moo->selection, '$("slider")'); + + $this->Moo->get('#slider'); + $result = $this->Moo->slider(array( + 'handle' => '#my-handle', + 'complete' => 'onComplete', + 'change' => 'onChange', + 'direction' => 'horizontal', + 'min' => 10, + 'max' => 40 + )); + $expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:onChange, onComplete:onComplete, range:[10,40]});'; + $this->assertEqual($result, $expected); + } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/view/helpers/prototype_engine.test.php b/cake/tests/cases/libs/view/helpers/prototype_engine.test.php index ab5ed6746594..b8c838d6557e 100644 --- a/cake/tests/cases/libs/view/helpers/prototype_engine.test.php +++ b/cake/tests/cases/libs/view/helpers/prototype_engine.test.php @@ -256,5 +256,22 @@ function testDrop() { $expected = 'Droppables.add($("element"), {accept:".drag-me", onDrop:onDrop, onHover:onHover});'; $this->assertEqual($result, $expected); } +/** + * ensure that slider() method behaves properly + * + * @return void + **/ + function testSlider() { + $this->Proto->get('#element'); + $result = $this->Proto->slider(array( + 'handle' => '#handle', + 'direction' => 'horizontal', + 'change' => 'onChange', + 'complete' => 'onComplete', + 'value' => 4, + )); + $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {axis:"horizontal", onChange:onComplete, onSlide:onChange, sliderValue:4});'; + $this->assertEqual($result, $expected); + } } ?> \ No newline at end of file