@@ -85,7 +85,7 @@ class JsHelper extends AppHelper {
85
85
*
86
86
* @var array
87
87
**/
88
- var $ helpers = array ();
88
+ var $ helpers = array (' Html ' );
89
89
/**
90
90
* HTML tags used by this helper.
91
91
*
@@ -105,13 +105,6 @@ class JsHelper extends AppHelper {
105
105
* @access private
106
106
**/
107
107
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 ();
115
108
/**
116
109
* __objects
117
110
*
@@ -144,7 +137,7 @@ function __construct($settings = array()) {
144
137
}
145
138
$ this ->__engineName = $ className . 'Engine ' ;
146
139
$ engineClass = $ engineName . 'Engine ' ;
147
- $ this ->helpers = array ( $ engineClass) ;
140
+ $ this ->helpers [] = $ engineClass ;
148
141
parent ::__construct ();
149
142
}
150
143
/**
@@ -165,88 +158,48 @@ function call__($method, $params) {
165
158
trigger_error (sprintf (__ ('JsHelper:: Missing Method %s is undefined ' , true ), $ method ), E_USER_WARNING );
166
159
}
167
160
/**
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.
169
163
*
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
172
165
*
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)
175
172
*
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.
181
175
**/
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 );
192
183
}
193
-
194
- if ($ once && isset ($ this ->__includedScripts [$ url ])) {
195
- return null ;
184
+ if (!$ options ['cache ' ] && $ options ['inline ' ]) {
185
+ return $ this ->Html ->scriptBlock ($ script , $ options );
196
186
}
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.
240
189
}
190
+ $ view =& ClassRegistry::getObject ('view ' );
191
+ $ view ->addScript ($ script );
192
+ return null ;
241
193
}
194
+
242
195
/**
243
196
* Loads a remote URL
244
197
*
245
198
* @param string $url
246
199
* @param array $options
247
200
* @return string
248
201
**/
249
- function load_ ($ url = null , $ options = array ()) {
202
+ /* function load_($url = null, $options = array()) {
250
203
if (isset($options['update'])) {
251
204
if (!is_array($options['update'])) {
252
205
$func = "new Ajax.Updater('{$options['update']}',";
@@ -285,10 +238,11 @@ function load_($url = null, $options = array()) {
285
238
}
286
239
287
240
288
- /* function get__($name) {
241
+ /*
242
+ function get__($name) {
289
243
return $this->__object($name, 'id');
290
244
}
291
- */
245
+
292
246
function select($pattern) {
293
247
return $this->__object($pattern, 'pattern');
294
248
}
@@ -304,7 +258,7 @@ function __object($name, $var) {
304
258
}
305
259
return $this->__objects[$name];
306
260
}
307
-
261
+ */
308
262
}
309
263
310
264
/**
@@ -322,6 +276,19 @@ class JsBaseEngineHelper extends AppHelper {
322
276
* @access public
323
277
**/
324
278
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 ();
325
292
/**
326
293
* Constructor.
327
294
*
@@ -495,6 +462,66 @@ function escape($string) {
495
462
$ escape = array ("\r\n" => '\n ' , "\r" => '\n ' , "\n" => '\n ' , '" ' => '\" ' , "' " => "\\' " );
496
463
return str_replace (array_keys ($ escape ), array_values ($ escape ), $ string );
497
464
}
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
+ }
498
525
/**
499
526
* Parse an options assoc array into an Javascript object literal.
500
527
* Similar to object() but treats any non-integer value as a string,
@@ -512,7 +539,7 @@ function _parseOptions($options) {
512
539
}
513
540
$ out [] = $ key . ': ' . $ val ;
514
541
}
515
- return join (', ' , $ out );;
542
+ return join (', ' , $ out );
516
543
}
517
544
}
518
545
0 commit comments