Skip to content
Permalink
Browse files

Adding methods to baseEngine

Removing file inclusion from JsHelper
Adding writeCache to JsHelper
Adding tests.
  • Loading branch information...
markstory committed Mar 13, 2009
1 parent 91e47a6 commit a1a54a152b1cba84359d1fd31112b3aa1492833c
Showing with 151 additions and 202 deletions.
  1. +107 −80 cake/libs/view/helpers/js.php
  2. +44 −122 cake/tests/cases/libs/view/helpers/js.test.php
@@ -85,7 +85,7 @@ class JsHelper extends AppHelper {
*
* @var array
**/
var $helpers = array();
var $helpers = array('Html');
/**
* HTML tags used by this helper.
*
@@ -105,13 +105,6 @@ class JsHelper extends AppHelper {
* @access private
**/
var $__engineName;
/**
* Scripts that have already been included once, prevents duplicate script insertion
*
* @var array
* @access private
**/
var $__includedScripts = array();
/**
* __objects
*
@@ -144,7 +137,7 @@ function __construct($settings = array()) {
}
$this->__engineName = $className . 'Engine';
$engineClass = $engineName . 'Engine';
$this->helpers = array($engineClass);
$this->helpers[] = $engineClass;
parent::__construct();
}
/**
@@ -165,88 +158,48 @@ function call__($method, $params) {
trigger_error(sprintf(__('JsHelper:: Missing Method %s is undefined', true), $method), E_USER_WARNING);
}
/**
* Returns one or many <script> tags depending on the number of scripts given.
* Writes all Javascript generated so far to a code block or
* caches them to a file and returns a linked script.
*
* If the filename is prefixed with "/", the path will be relative to the base path of your
* application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
* Options
*
* Can include one or many Javascript files. If there are .min.js or .pack.js files
* and your debug level == 0 these files will be used instead of the non min/pack files.
* - 'inline' - Set to true to have scripts output as a script block inline
* if 'cache' is also true, a script link tag will be generated. (default true)
* - 'cache' - Set to true to have scripts cached to a file and linked in (default true)
* - 'clear' - Set to false to prevent script cache from being cleared (default true)
* - 'onDomReady' - wrap cached scripts in domready event (default true)
* - 'safe' - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
*
* @param mixed $url String or array of javascript files to include
* @param boolean $inline Whether script should be output inline or into scripts_for_layout.
* @param boolean $once Whether or not the script should be checked for uniqueness. If true scripts will only be
* included once, use false to allow the same script to be included more than once per request.
* @return mixed
* @param array $options options for the code block
* @return string completed javascript tag.
**/
function uses($url, $inline = true, $once = true) {
if (is_array($url)) {
$out = '';
foreach ($url as $i) {
$out .= "\n\t" . $this->uses($i, $inline);
}
if ($inline) {
return $out . "\n";
}
return;
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']));
if ($options['onDomReady']) {
$script = $this->{$this->__engineName}->domReady($script);
}
if ($once && isset($this->__includedScripts[$url])) {
return null;
if (!$options['cache'] && $options['inline']) {
return $this->Html->scriptBlock($script, $options);
}
$this->__includedScripts[$url] = true;
if (strpos($url, '://') === false) {
if ($url[0] !== '/') {
$url = JS_URL . $url;
}
$url = $this->webroot($url);
if (strpos($url, '?') === false) {
if (Configure::read('debug') == 0) {
$suffixes = array('.min.js', '.pack.js');
foreach ($suffixes as $suffix) {
if (file_exists(WWW_ROOT . $url . $suffix)) {
$url .= $suffix;
break;
}
}
}
if (strpos($url, '.js') === false) {
$url .= '.js';
}
}
$timestampEnabled = (
(Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
Configure::read('Asset.timestamp') === 'force'
);
if (strpos($url, '?') === false && $timestampEnabled) {
$url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url));
}
if (Configure::read('Asset.filter.js')) {
$url = str_replace(JS_URL, 'cjs/', $url);
}
}
$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
if ($inline) {
return $out;
} else {
$view =& ClassRegistry::getObject('view');
$view->addScript($out);
if ($options['cache'] && $options['inline']) {
//cache to file and return script tag.
}
$view =& ClassRegistry::getObject('view');
$view->addScript($script);
return null;
}
/**
* Loads a remote URL
*
* @param string $url
* @param array $options
* @return string
**/
function load_($url = null, $options = array()) {
/* function load_($url = null, $options = array()) {
if (isset($options['update'])) {
if (!is_array($options['update'])) {
$func = "new Ajax.Updater('{$options['update']}',";
@@ -285,10 +238,11 @@ function load_($url = null, $options = array()) {
}
/* function get__($name) {
/*
function get__($name) {
return $this->__object($name, 'id');
}
*/
function select($pattern) {
return $this->__object($pattern, 'pattern');
}
@@ -304,7 +258,7 @@ function __object($name, $var) {
}
return $this->__objects[$name];
}
*/
}
/**
@@ -322,6 +276,19 @@ class JsBaseEngineHelper extends AppHelper {
* @access public
**/
var $useNative = false;
/**
* The js snippet for the current selection.
*
* @var string
* @access protected
**/
var $_selection;
/**
* Scripts that are queued for output
*
* @var array
**/
var $__cachedScripts = array();
/**
* Constructor.
*
@@ -495,6 +462,66 @@ 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 cache.s
* @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
*
* @param string $selector The selector that is targeted
* @param boolean $multiple Whether or not the selector could target more than one element.
* @return object instance of $this. Allows chained methods.
**/
function select($selector, $multiple = false) {
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
* @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
* @return string completed event handler
**/
function addEvent($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) {
}
/**
* 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($method, $callback) {
}
/**
* Parse an options assoc array into an Javascript object literal.
* Similar to object() but treats any non-integer value as a string,
@@ -512,7 +539,7 @@ function _parseOptions($options) {
}
$out[] = $key . ':' . $val;
}
return join(', ', $out);;
return join(', ', $out);
}
}
Oops, something went wrong.

0 comments on commit a1a54a1

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