Skip to content

Commit

Permalink
BUGFIX: make the javascript-producing functions behave in the same wa…
Browse files Browse the repository at this point in the history
…y. Now they will return a javascript snippet and the caller is responsible for adding it to a FormResponse. Removes the duplication in AJAX response which happened when FormResponse::add has been used before the call to JS helper functions (#5359)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.4@103083 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
mateusz authored and Sam Minnee committed Feb 2, 2011
1 parent 4ee4878 commit 0ba1e35
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions code/LeftAndMain.php
Expand Up @@ -618,7 +618,6 @@ public function returnItemToUser($p) {
*/
public function save($urlParams, $form) {
$className = $this->stat('tree_class');
$result = '';

$SQL_id = Convert::raw2sql($_REQUEST['ID']);
if(substr($SQL_id,0,3) != 'new') {
Expand Down Expand Up @@ -690,12 +689,12 @@ public function save($urlParams, $form) {

if($added = DataObjectLog::getAdded('SiteTree')) {
foreach($added as $page) {
if($page->ID != $record->ID) $result .= $this->addTreeNodeJS($page);
if($page->ID != $record->ID) FormResponse::add($this->addTreeNodeJS($page));
}
}
if($deleted = DataObjectLog::getDeleted('SiteTree')) {
foreach($deleted as $page) {
if($page->ID != $record->ID) $result .= $this->deleteTreeNodeJS($page);
if($page->ID != $record->ID) FormResponse::add($this->deleteTreeNodeJS($page));
}
}
if($changed = DataObjectLog::getChanged('SiteTree')) {
Expand Down Expand Up @@ -789,7 +788,7 @@ public function save($urlParams, $form) {
// BUGFIX: Changed icon only shows after Save button is clicked twice http://support.silverstripe.com/gsoc/ticket/76
$title = Convert::raw2js($record->TreeTitle());
FormResponse::add("$('sitetree').setNodeTitle(\"$record->ID\", \"$title\");");
$result .= $this->getActionUpdateJS($record);
FormResponse::add($this->getActionUpdateJS($record));
FormResponse::status_message($message, "good");
FormResponse::update_status($record->Status);

Expand All @@ -801,7 +800,9 @@ public function save($urlParams, $form) {
}

/**
* Return a piece of javascript that will update the actions of the main form
* Returns a javascript snippet that will update the actions of the main form
*
* @return string
*/
public function getActionUpdateJS($record) {
// Get the new action buttons
Expand All @@ -812,13 +813,13 @@ public function getActionUpdateJS($record) {
$actionList .= $action->Field() . ' ';
}

FormResponse::add("$('Form_EditForm').loadActionsFromString('" . Convert::raw2js($actionList) . "');");

return FormResponse::respond();
return "$('Form_EditForm').loadActionsFromString('" . Convert::raw2js($actionList) . "');";
}

/**
* Return JavaScript code to generate a tree node for the given page, if visible
* Returns a javascript snippet to generate a tree node for the given page, if visible
*
* @return string
*/
public function addTreeNodeJS($page, $select = false) {
$parentID = (int)$page->ParentID;
Expand All @@ -829,11 +830,12 @@ public function addTreeNodeJS($page, $select = false) {
if(parentNode) parentNode.appendTreeNode(newNode);
JS;
$response .= ($select ? "newNode.selectTreeNode();\n" : "") ;
FormResponse::add($response);
return FormResponse::respond();
return $response;
}
/**
* Return JavaScript code to remove a tree node for the given page, if it exists.
* Returns a javascript snippet to remove a tree node for the given page, if it exists.
*
* @return string
*/
public function deleteTreeNodeJS($page) {
$id = $page->ID ? $page->ID : $page->OldID;
Expand All @@ -842,14 +844,13 @@ public function deleteTreeNodeJS($page) {
if(node && node.parentTreeNode) node.parentTreeNode.removeTreeNode(node);
$('Form_EditForm').closeIfSetTo($id);
JS;
FormResponse::add($response);

// If we have that page selected currently, then clear that info from the session
if(Session::get("{$this->class}.currentPage") == $id) {
$this->setCurrentPageID(null);
}

return FormResponse::respond();
return $response;
}

/**
Expand Down

0 comments on commit 0ba1e35

Please sign in to comment.