Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic PJAX type change when building a HTTPResponse #488

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions admin/javascript/LeftAndMain.Content.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,10 @@
// as automatic browser ajax response redirects seem to discard the hash/fragment.
formData.push({name: 'BackURL', value:History.getPageUrl()});

// Some action buttons are redirecting to content areas as oposed to reloading the form.
// They will have pjax-content class on them.
var pjaxType = 'CurrentForm', pjaxSelector = '.cms-edit-form';
if ($(button).hasClass('pjax-content')) {
pjaxType = 'Content';
pjaxSelector = '.cms-content';
}

jQuery.ajax(jQuery.extend({
headers: {
"X-Pjax" : pjaxType,
'X-Pjax-Selector': pjaxSelector
"X-Pjax" : "CurrentForm",
'X-Pjax-Selector': '.cms-edit-form'
},
url: form.attr('action'),
data: formData,
Expand Down
20 changes: 19 additions & 1 deletion control/PjaxResponseNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class PjaxResponseNegotiator {

/**
* Holds the overriden type.
*/
protected $pjaxTypeOverride = null;

/**
* @var Array See {@link respond()}
*/
Expand Down Expand Up @@ -44,8 +49,11 @@ public function respond(SS_HTTPRequest $request, $extraCallbacks = array()) {
array_change_key_case($this->callbacks, CASE_LOWER),
array_change_key_case($extraCallbacks, CASE_LOWER)
);

// Get the PJAX type for this request (might have been overriden).
$fragment = $this->pjaxTypeOverride ? $this->pjaxTypeOverride : $request->getHeader('X-Pjax');

if($fragment = $request->getHeader('X-Pjax')) {
if($fragment) {
$fragment = strtolower($fragment);
if(isset($callbacks[$fragment])) {
return call_user_func($callbacks[$fragment]);
Expand All @@ -59,6 +67,16 @@ public function respond(SS_HTTPRequest $request, $extraCallbacks = array()) {

}

/**
* Overrides the Pjax request type.
*
* @param $type string Overriding type.
*/
public function forcePjaxType($type) {
$this->pjaxTypeOverride = $type;
return $this;
}

/**
* @param String $fragment
* @param Callable $callback
Expand Down
10 changes: 3 additions & 7 deletions forms/gridfield/GridFieldDetailForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,8 @@ function ItemEditForm() {
if($this->record->ID !== 0) {
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Save', 'Save'))
->setUseButtonTag(true)->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept'));
// The delete action will redirect, hence pjax-content class.
$actions->push(FormAction::create('doDelete', _t('GridFieldDetailForm.Delete', 'Delete'))
->addExtraClass('ss-ui-action-destructive')->addExtraClass('pjax-content'));
->addExtraClass('ss-ui-action-destructive'));
}else{ // adding new record
//Change the Save label to 'Create'
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Create', 'Create'))
Expand Down Expand Up @@ -402,11 +401,8 @@ function doDelete($data, $form) {

$form->sessionMessage($message, 'good');

//when an item is deleted, redirect to the revelant admin section without the action parameter
$controller = Controller::curr();
$noActionURL = $controller->removeAction($data['url']);

return Director::redirect($noActionURL, 302); //redirect back to admin section
$controller = $this->getToplevelController();
return $controller->getResponseNegotiator()->forcePjaxType('Content')->respond($controller->request);
}

/**
Expand Down