Skip to content

Commit

Permalink
Adding optional param to remove colon on methods. Allows use inside
Browse files Browse the repository at this point in the history
object literals.
  • Loading branch information
markstory committed Apr 27, 2009
1 parent 35dbe41 commit b29e130
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
36 changes: 25 additions & 11 deletions cake/libs/view/helpers/js.php
Expand Up @@ -77,7 +77,7 @@ class JsHelper extends AppHelper {
* @return void
**/
function __construct($settings = array()) {
$className = 'jquery';
$className = 'Jquery';
if (is_array($settings) && isset($settings[0])) {
$className = $settings[0];
} elseif (is_string($settings)) {
Expand Down Expand Up @@ -241,7 +241,7 @@ class JsBaseEngineHelper extends AppHelper {
*
* @var array
**/
var $bufferedMethods = array('event', 'sortable', 'drag', 'drop');
var $bufferedMethods = array('event', 'sortable', 'drag', 'drop', 'slider');
/**
* Constructor.
*
Expand All @@ -254,11 +254,16 @@ function __construct() {
* Create an alert message in Javascript
*
* @param string $message Message you want to alter.
* @param boolean $colon Whether you want a colon or not.
* @access public
* @return void
* @return string Completed alert() call.
**/
function alert($message) {
return 'alert("' . $this->escape($message) . '");';
function alert($message, $colon = true) {
$out = 'alert("' . $this->escape($message) . '")';
if ($colon) {
$out .= ';';
}
return $out;
}
/**
* Redirects to a URL
Expand All @@ -274,22 +279,31 @@ function redirect($url = null) {
* Create a confirm() message
*
* @param string $message Message you want confirmed.
* @param boolean $colon Whether you want a colon or not.
* @access public
* @return void
* @return string Completed confirm() call
**/
function confirm($message) {
return 'confirm("' . $this->escape($message) . '");';
function confirm($message, $colon = true) {
$out = 'confirm("' . $this->escape($message) . '")';
if ($colon) {
$out .= ';';
}
return $out;
}
/**
* Create a prompt() Javascript function
*
* @param string $message Message you want to prompt.
* @param string $default Default message
* @access public
* @return void
* @return string completed prompt() call
**/
function prompt($message, $default = '') {
return 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '");';
function prompt($message, $default = '', $colon = true) {
$out = 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '")';
if ($colon) {
$out .= ';';
}
return $out;
}
/**
* Generates a JavaScript object in JavaScript Object Notation (JSON)
Expand Down
18 changes: 15 additions & 3 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -106,7 +106,7 @@ function endTest() {
**/
function testConstruction() {
$js =& new JsHelper();
$this->assertEqual($js->helpers, array('Html', 'jqueryEngine'));
$this->assertEqual($js->helpers, array('Html', 'JqueryEngine'));

$js =& new JsHelper(array('mootools'));
$this->assertEqual($js->helpers, array('Html', 'mootoolsEngine'));
Expand Down Expand Up @@ -302,6 +302,10 @@ function testPrompt() {
$expected = 'prompt("Hey, hey you", "hi!");';
$this->assertEqual($result, $expected);

$result = $this->JsEngine->prompt('Hey, hey you', 'hi!', false);
$expected = 'prompt("Hey, hey you", "hi!")';
$this->assertEqual($result, $expected);

$result = $this->JsEngine->prompt('"Hey"', '"hi"');
$expected = 'prompt("\"Hey\"", "\"hi\"");';
$this->assertEqual($result, $expected);
Expand All @@ -318,7 +322,11 @@ function testAlert() {

$result = $this->JsEngine->alert('"Hey"');
$expected = 'alert("\"Hey\"");';
$this->assertEqual($result, $expected);
$this->assertEqual($result, $expected);

$result = $this->JsEngine->alert('Hey there', false);
$expected = 'alert("Hey there")';
$this->assertEqual($result, $expected);
}
/**
* test confirm generation
Expand All @@ -332,7 +340,11 @@ function testConfirm() {

$result = $this->JsEngine->confirm('"Are you sure?"');
$expected = 'confirm("\"Are you sure?\"");';
$this->assertEqual($result, $expected);
$this->assertEqual($result, $expected);

$result = $this->JsEngine->confirm('"Are you sure?"', false);
$expected = 'confirm("\"Are you sure?\"")';
$this->assertEqual($result, $expected);
}
/**
* test Redirect
Expand Down

0 comments on commit b29e130

Please sign in to comment.