Skip to content
Permalink
Browse files

Moving writeCache and getCache to JsHelper.

Adding drag, drop, sortable stubs.
Adding control of caching to JsHelper.
Refactoring tests.
  • Loading branch information...
markstory committed Mar 16, 2009
1 parent 91903a4 commit d787ec3720cc297ef85638a44fe2dd0e3cf75235
@@ -33,59 +33,23 @@
**/
class JsHelper extends AppHelper {
/**
* Base URL
* Whether or not you want scripts to be cached or output.
*
* @var string
**/
var $base = null;
/**
* Webroot path
*
* @var string
**/
var $webroot = null;
/**
* Theme name
*
* @var string
**/
var $themeWeb = null;
/**
* URL to current action.
*
* @var string
**/
var $here = null;
/**
* Parameter array.
*
* @var array
**/
var $params = array();
/**
* Current action.
*
* @var string
**/
var $action = null;
/**
* Plugin path
*
* @var string
* @var boolean
**/
var $plugin = null;
var $cacheScripts = true;
/**
* POST data for models
* helpers
*
* @var array
**/
var $data = null;
var $helpers = array('Html');
/**
* helpers
* Scripts that are queued for output
*
* @var array
**/
var $helpers = array('Html');
var $__cachedScripts = array();
/**
* Current Javascript Engine that is being used
*
@@ -138,7 +102,12 @@ function __construct($settings = array()) {
**/
function call__($method, $params) {
if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) {
return $this->{$this->__engineName}->dispatchMethod($method, $params);
$out = $this->{$this->__engineName}->dispatchMethod($method, $params);
if ($this->cacheScripts) {
$this->writeCache($out);
return null;
}
return $out;
}
if (method_exists($this, $method . '_')) {
return $this->dispatchMethod($method . '_', $params);
@@ -164,7 +133,7 @@ function call__($method, $params) {
function writeScripts($options = array()) {
$defaults = array('onDomReady' => true, 'inline' => true, 'cache' => true, 'clear' => true, 'safe' => true);
$options = array_merge($defaults, $options);
$script = implode("\n", $this->{$this->__engineName}->getCache($options['clear']));
$script = implode("\n", $this->getCache($options['clear']));
if ($options['onDomReady']) {
$script = $this->{$this->__engineName}->domReady($script);
@@ -173,13 +142,33 @@ function writeScripts($options = array()) {
return $this->Html->scriptBlock($script, $options);
}
if ($options['cache'] && $options['inline']) {
//cache to file and return script tag.
//@todo cache to file and return script tag.
}
$view =& ClassRegistry::getObject('view');
$view->addScript($script);
return null;
}
/**
* Write a script to the cached scripts.
*
* @return void
**/
function writeCache($script) {
$this->__cachedScripts[] = $script;
}
/**
* Get all the cached scripts
*
* @param boolean $clear Whether or not to clear the script caches
* @return array Array of scripts added to the request.
**/
function getCache($clear = true) {
$scripts = $this->__cachedScripts;
if ($clear) {
$this->__cachedScripts = array();
}
return $scripts;
}
/**
* Loads a remote URL
*
@@ -277,12 +266,6 @@ class JsBaseEngineHelper extends AppHelper {
* @var array
**/
var $_optionMap = array();
/**
* Scripts that are queued for output
*
* @var array
**/
var $__cachedScripts = array();
/**
* Constructor.
*
@@ -462,27 +445,6 @@ function escape($string) {
$escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
return str_replace(array_keys($escape), array_values($escape), $string);
}
/**
* Write a script to the cached scripts.
*
* @return void
**/
function writeCache($script) {
$this->__cachedScripts[] = $script;
}
/**
* Get all the cached scripts
*
* @param boolean $clear Whether or not to clear the script caches
* @return array Array of scripts added to the request.
**/
function getCache($clear = true) {
$scripts = $this->__cachedScripts;
if ($clear) {
$this->__cachedScripts = array();
}
return $scripts;
}
/**
* Create javascript selector for a CSS rule
*
@@ -575,6 +537,53 @@ function effect($name, $options) {
function request($url, $options = array()) {
trigger_error(sprintf(__('%s does not have request() implemented', true), get_class($this)), E_USER_WARNING);
}
/**
* Create a draggable element. Works on the currently selected element.
* Additional options may be supported by your library.
*
* ### Options
*
* - handle - selector to the handle element.
* - start - Event fired when the drag starts
* - drag - Event fired on every step of the drag
* - stop - Event fired when dragging stops (mouse release)
* - snapGrid - The pixel grid that movement snaps to, an array(x, y)
* - container - The element that acts as a bounding box for the draggable element.
*
* @param array $options Options array see above.
* @return string Completed drag script
**/
function drag($options = array()) {
}
/**
* Create a droppable element. Allows for draggable elements to be dropped on it.
* Additional options may be supported by your library.
*
* ### Options
*
* - drag - Elements that can be dragged into this droppable
* - drop - Event fired when an element is dropped into the drop zone.
* - hover - Event fired when a drag enters a drop zone.
* - leave - Event fired when a drag is removed from a drop zone without being dropped.
*
* @return string Completed drop script
**/
function drop($options = array()) {
}
/**
* Create a sortable element.
*
* ### Options
*
*
* @param array $options Array of options for the sortable. See above.
* @return string Completed sortable script.
**/
function sortable() {
}
/**
* Parse an options assoc array into an Javascript object literal.
* Similar to object() but treats any non-integer value as a string,
@@ -149,7 +149,7 @@ function effect($name, $options = array()) {
return $this->selection . '.' . $effect . ';';
}
/**
* Create an $.ajax() call.
* Create an new Request.
*
* @param mixed $url
* @param array $options
@@ -140,7 +140,8 @@ function testMethodDispatching() {
**/
function testWriteScriptsNoFile() {
$this->Js->JsBaseEngine = new TestJsEngineHelper();
$this->Js->JsBaseEngine->setReturnValue('getCache', array('one = 1;', 'two = 2;'));
$this->Js->writeCache('one = 1;');
$this->Js->writeCache('two = 2;');
$result = $this->Js->writeScripts(array('onDomReady' => false, 'cache' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),

0 comments on commit d787ec3

Please sign in to comment.
You can’t perform that action at this time.