Skip to content

Commit a1a54a1

Browse files
committed
Adding methods to baseEngine
Removing file inclusion from JsHelper Adding writeCache to JsHelper Adding tests.
1 parent 91e47a6 commit a1a54a1

File tree

2 files changed

+151
-202
lines changed

2 files changed

+151
-202
lines changed

cake/libs/view/helpers/js.php

Lines changed: 107 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class JsHelper extends AppHelper {
8585
*
8686
* @var array
8787
**/
88-
var $helpers = array();
88+
var $helpers = array('Html');
8989
/**
9090
* HTML tags used by this helper.
9191
*
@@ -105,13 +105,6 @@ class JsHelper extends AppHelper {
105105
* @access private
106106
**/
107107
var $__engineName;
108-
/**
109-
* Scripts that have already been included once, prevents duplicate script insertion
110-
*
111-
* @var array
112-
* @access private
113-
**/
114-
var $__includedScripts = array();
115108
/**
116109
* __objects
117110
*
@@ -144,7 +137,7 @@ function __construct($settings = array()) {
144137
}
145138
$this->__engineName = $className . 'Engine';
146139
$engineClass = $engineName . 'Engine';
147-
$this->helpers = array($engineClass);
140+
$this->helpers[] = $engineClass;
148141
parent::__construct();
149142
}
150143
/**
@@ -165,88 +158,48 @@ function call__($method, $params) {
165158
trigger_error(sprintf(__('JsHelper:: Missing Method %s is undefined', true), $method), E_USER_WARNING);
166159
}
167160
/**
168-
* Returns one or many <script> tags depending on the number of scripts given.
161+
* Writes all Javascript generated so far to a code block or
162+
* caches them to a file and returns a linked script.
169163
*
170-
* If the filename is prefixed with "/", the path will be relative to the base path of your
171-
* application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
164+
* Options
172165
*
173-
* Can include one or many Javascript files. If there are .min.js or .pack.js files
174-
* and your debug level == 0 these files will be used instead of the non min/pack files.
166+
* - 'inline' - Set to true to have scripts output as a script block inline
167+
* if 'cache' is also true, a script link tag will be generated. (default true)
168+
* - 'cache' - Set to true to have scripts cached to a file and linked in (default true)
169+
* - 'clear' - Set to false to prevent script cache from being cleared (default true)
170+
* - 'onDomReady' - wrap cached scripts in domready event (default true)
171+
* - 'safe' - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
175172
*
176-
* @param mixed $url String or array of javascript files to include
177-
* @param boolean $inline Whether script should be output inline or into scripts_for_layout.
178-
* @param boolean $once Whether or not the script should be checked for uniqueness. If true scripts will only be
179-
* included once, use false to allow the same script to be included more than once per request.
180-
* @return mixed
173+
* @param array $options options for the code block
174+
* @return string completed javascript tag.
181175
**/
182-
function uses($url, $inline = true, $once = true) {
183-
if (is_array($url)) {
184-
$out = '';
185-
foreach ($url as $i) {
186-
$out .= "\n\t" . $this->uses($i, $inline);
187-
}
188-
if ($inline) {
189-
return $out . "\n";
190-
}
191-
return;
176+
function writeScripts($options = array()) {
177+
$defaults = array('onDomReady' => true, 'inline' => true, 'cache' => true, 'clear' => true, 'safe' => true);
178+
$options = array_merge($defaults, $options);
179+
$script = implode("\n", $this->{$this->__engineName}->getCache($options['clear']));
180+
181+
if ($options['onDomReady']) {
182+
$script = $this->{$this->__engineName}->domReady($script);
192183
}
193-
194-
if ($once && isset($this->__includedScripts[$url])) {
195-
return null;
184+
if (!$options['cache'] && $options['inline']) {
185+
return $this->Html->scriptBlock($script, $options);
196186
}
197-
$this->__includedScripts[$url] = true;
198-
199-
if (strpos($url, '://') === false) {
200-
if ($url[0] !== '/') {
201-
$url = JS_URL . $url;
202-
}
203-
$url = $this->webroot($url);
204-
205-
if (strpos($url, '?') === false) {
206-
if (Configure::read('debug') == 0) {
207-
$suffixes = array('.min.js', '.pack.js');
208-
foreach ($suffixes as $suffix) {
209-
if (file_exists(WWW_ROOT . $url . $suffix)) {
210-
$url .= $suffix;
211-
break;
212-
}
213-
}
214-
}
215-
if (strpos($url, '.js') === false) {
216-
$url .= '.js';
217-
}
218-
}
219-
220-
$timestampEnabled = (
221-
(Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
222-
Configure::read('Asset.timestamp') === 'force'
223-
);
224-
225-
if (strpos($url, '?') === false && $timestampEnabled) {
226-
$url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url));
227-
}
228-
229-
if (Configure::read('Asset.filter.js')) {
230-
$url = str_replace(JS_URL, 'cjs/', $url);
231-
}
232-
}
233-
$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
234-
235-
if ($inline) {
236-
return $out;
237-
} else {
238-
$view =& ClassRegistry::getObject('view');
239-
$view->addScript($out);
187+
if ($options['cache'] && $options['inline']) {
188+
//cache to file and return script tag.
240189
}
190+
$view =& ClassRegistry::getObject('view');
191+
$view->addScript($script);
192+
return null;
241193
}
194+
242195
/**
243196
* Loads a remote URL
244197
*
245198
* @param string $url
246199
* @param array $options
247200
* @return string
248201
**/
249-
function load_($url = null, $options = array()) {
202+
/* function load_($url = null, $options = array()) {
250203
if (isset($options['update'])) {
251204
if (!is_array($options['update'])) {
252205
$func = "new Ajax.Updater('{$options['update']}',";
@@ -285,10 +238,11 @@ function load_($url = null, $options = array()) {
285238
}
286239
287240
288-
/* function get__($name) {
241+
/*
242+
function get__($name) {
289243
return $this->__object($name, 'id');
290244
}
291-
*/
245+
292246
function select($pattern) {
293247
return $this->__object($pattern, 'pattern');
294248
}
@@ -304,7 +258,7 @@ function __object($name, $var) {
304258
}
305259
return $this->__objects[$name];
306260
}
307-
261+
*/
308262
}
309263

310264
/**
@@ -322,6 +276,19 @@ class JsBaseEngineHelper extends AppHelper {
322276
* @access public
323277
**/
324278
var $useNative = false;
279+
/**
280+
* The js snippet for the current selection.
281+
*
282+
* @var string
283+
* @access protected
284+
**/
285+
var $_selection;
286+
/**
287+
* Scripts that are queued for output
288+
*
289+
* @var array
290+
**/
291+
var $__cachedScripts = array();
325292
/**
326293
* Constructor.
327294
*
@@ -495,6 +462,66 @@ function escape($string) {
495462
$escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
496463
return str_replace(array_keys($escape), array_values($escape), $string);
497464
}
465+
/**
466+
* Write a script to the cached scripts.
467+
*
468+
* @return void
469+
**/
470+
function writeCache($script) {
471+
$this->__cachedScripts[] = $script;
472+
}
473+
/**
474+
* Get all the cached scripts
475+
*
476+
* @param boolean $clear Whether or not to clear the script cache.s
477+
* @return array Array of scripts added to the request.
478+
**/
479+
function getCache($clear = true) {
480+
$scripts = $this->__cachedScripts;
481+
if ($clear) {
482+
$this->__cachedScripts = array();
483+
}
484+
return $scripts;
485+
}
486+
/**
487+
* Create javascript selector for a CSS rule
488+
*
489+
* @param string $selector The selector that is targeted
490+
* @param boolean $multiple Whether or not the selector could target more than one element.
491+
* @return object instance of $this. Allows chained methods.
492+
**/
493+
function select($selector, $multiple = false) {
494+
return $this;
495+
}
496+
/**
497+
* Add an event to the script cache. Operates on the currently selected elements.
498+
*
499+
* @param string $type Type of event to bind to the current dom id
500+
* @param string $callback The Javascript function you wish to trigger or the function literal
501+
* @return string completed event handler
502+
**/
503+
function addEvent($type, $callback) {
504+
505+
}
506+
/**
507+
* Create a domReady event. This is a special event in many libraries
508+
*
509+
* @param string $functionBody The code to run on domReady
510+
* @return string completed domReady method
511+
**/
512+
function domReady($functionBody) {
513+
514+
}
515+
/**
516+
* Create an iteration over the current selection result.
517+
*
518+
* @param string $method The method you want to apply to the selection
519+
* @param string $callback The function body you wish to apply during the iteration.
520+
* @return string completed iteration
521+
**/
522+
function each($method, $callback) {
523+
524+
}
498525
/**
499526
* Parse an options assoc array into an Javascript object literal.
500527
* Similar to object() but treats any non-integer value as a string,
@@ -512,7 +539,7 @@ function _parseOptions($options) {
512539
}
513540
$out[] = $key . ':' . $val;
514541
}
515-
return join(', ', $out);;
542+
return join(', ', $out);
516543
}
517544
}
518545

0 commit comments

Comments
 (0)