mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-03 10:02:42 +00:00
Adding min/pack detection and auto use.
This commit is contained in:
parent
9022784628
commit
0c77cc98b5
2 changed files with 159 additions and 3 deletions
|
@ -31,7 +31,7 @@
|
||||||
* @package cake
|
* @package cake
|
||||||
* @subpackage cake.cake.libs.view.helpers
|
* @subpackage cake.cake.libs.view.helpers
|
||||||
*/
|
*/
|
||||||
class JsHelper extends Overloadable {
|
class JsHelper extends AppHelper {
|
||||||
/**
|
/**
|
||||||
* Base URL
|
* Base URL
|
||||||
*
|
*
|
||||||
|
@ -85,7 +85,19 @@ class JsHelper extends Overloadable {
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
**/
|
**/
|
||||||
public $helpers = array();
|
var $helpers = array();
|
||||||
|
/**
|
||||||
|
* HTML tags used by this helper.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $tags = array(
|
||||||
|
'javascriptblock' => '<script type="text/javascript">%s</script>',
|
||||||
|
'javascriptstart' => '<script type="text/javascript">',
|
||||||
|
'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
|
||||||
|
'javascriptend' => '</script>'
|
||||||
|
);
|
||||||
/**
|
/**
|
||||||
* Current Javascript Engine that is being used
|
* Current Javascript Engine that is being used
|
||||||
*
|
*
|
||||||
|
@ -93,6 +105,13 @@ class JsHelper extends Overloadable {
|
||||||
* @access private
|
* @access private
|
||||||
**/
|
**/
|
||||||
var $__engineName;
|
var $__engineName;
|
||||||
|
/**
|
||||||
|
* Scripts that have already been included once, prevents duplicate script insertion
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access private
|
||||||
|
**/
|
||||||
|
var $__includedScriptNames = array();
|
||||||
/**
|
/**
|
||||||
* __objects
|
* __objects
|
||||||
*
|
*
|
||||||
|
@ -160,6 +179,74 @@ class JsHelper extends Overloadable {
|
||||||
function alert_($message) {
|
function alert_($message) {
|
||||||
return 'alert("' . $this->escape($message) . '");';
|
return 'alert("' . $this->escape($message) . '");';
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Returns one or many <script> tags depending on the number of scripts given.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
* @return mixed
|
||||||
|
**/
|
||||||
|
function uses($url, $inline = true) {
|
||||||
|
if (is_array($url)) {
|
||||||
|
$out = '';
|
||||||
|
foreach ($url as $i) {
|
||||||
|
$out .= "\n\t" . $this->uses($i, $inline);
|
||||||
|
}
|
||||||
|
if ($inline) {
|
||||||
|
return $out . "\n";
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function if_($if, $then, $else = null, $elseIf = array()) {
|
function if_($if, $then, $else = null, $elseIf = array()) {
|
||||||
$len = strlen($if) - 1;
|
$len = strlen($if) - 1;
|
||||||
|
|
|
@ -79,8 +79,12 @@ class JsHelperTestCase extends CakeTestCase {
|
||||||
$js = new JsHelper(array('TestJs'));
|
$js = new JsHelper(array('TestJs'));
|
||||||
$js->TestJsEngine = new TestJsEngineHelper();
|
$js->TestJsEngine = new TestJsEngineHelper();
|
||||||
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
|
$js->TestJsEngine->expectOnce('dispatchMethod', array('methodOne', array()));
|
||||||
|
|
||||||
$js->methodOne();
|
$js->methodOne();
|
||||||
|
|
||||||
|
$js->TestEngine = new StdClass();
|
||||||
|
$this->expectError();
|
||||||
|
$js->someMethodThatSurelyDoesntExist();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* test escape string skills
|
* test escape string skills
|
||||||
|
@ -140,6 +144,71 @@ class JsHelperTestCase extends CakeTestCase {
|
||||||
$expected = 'alert("\"Hey\"");';
|
$expected = 'alert("\"Hey\"");';
|
||||||
$this->assertEqual($result, $expected);
|
$this->assertEqual($result, $expected);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* test script tag generation
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testUses() {
|
||||||
|
$result = $this->Js->uses('foo');
|
||||||
|
$expected = array(
|
||||||
|
'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')
|
||||||
|
);
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Js->uses('jquery-1.3');
|
||||||
|
$expected = array(
|
||||||
|
'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.js')
|
||||||
|
);
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Js->uses('/plugin/js/jquery-1.3');
|
||||||
|
$expected = array(
|
||||||
|
'script' => array('type' => 'text/javascript', 'src' => '/plugin/js/jquery-1.3.js')
|
||||||
|
);
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Js->uses('scriptaculous.js?load=effects');
|
||||||
|
$expected = array(
|
||||||
|
'script' => array('type' => 'text/javascript', 'src' => 'js/scriptaculous.js?load=effects')
|
||||||
|
);
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
|
||||||
|
$result = $this->Js->uses(array('foo', 'bar'));
|
||||||
|
$expected = array(
|
||||||
|
array('script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')),
|
||||||
|
'/script',
|
||||||
|
array('script' => array('type' => 'text/javascript', 'src' => 'js/bar.js')),
|
||||||
|
'/script',
|
||||||
|
);
|
||||||
|
$this->assertTags($result, $expected);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* test Min/pack version autofinding
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testMinPackAutoUse() {
|
||||||
|
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, min/pack js testing is skipped')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Configure::write('debug', 0);
|
||||||
|
touch(WWW_ROOT . 'js' . DS. '__cake_js_min_test.min.js');
|
||||||
|
touch(WWW_ROOT . 'js' . DS. '__cake_js_pack_test.pack.js');
|
||||||
|
|
||||||
|
$result = $this->Js->uses('__cake_js_min_test');
|
||||||
|
$this->assertPattern('/__cake_js_min_test\.min\.js/', $result);
|
||||||
|
|
||||||
|
$result = $this->Js->uses('__cake_js_pack_test');
|
||||||
|
$this->assertPattern('/__cake_js_pack_test\.pack\.js/', $result);
|
||||||
|
|
||||||
|
Configure::write('debug', 2);
|
||||||
|
$result = $this->Js->uses('__cake_js_pack_test');
|
||||||
|
$this->assertNoPattern('/pack\.js/', $result);
|
||||||
|
|
||||||
|
unlink(WWW_ROOT . 'js' . DS. '__cake_js_min_test.min.js');
|
||||||
|
unlink(WWW_ROOT . 'js' . DS. '__cake_js_pack_test.pack.js');
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* test confirm generation
|
* test confirm generation
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue