Skip to content

Commit

Permalink
[feat]add confirm on grid row form action
Browse files Browse the repository at this point in the history
  • Loading branch information
z-song committed Apr 20, 2020
1 parent 3b51d45 commit 700be46
Showing 1 changed file with 104 additions and 30 deletions.
134 changes: 104 additions & 30 deletions src/Actions/Interactor/Form.php
Original file line number Diff line number Diff line change
@@ -24,6 +24,11 @@ class Form extends Interactor
*/
protected $modalId;

/**
* @var string
*/
protected $confirm = '';

/**
* @param string $label
*
@@ -331,6 +336,17 @@ public function hidden($column, $label = '')
return $field;
}

/**
* @param $message
* @return $this
*/
public function confirm($message)
{
$this->confirm = $message;

return $this;
}

/**
* @param string $content
* @param string $selector
@@ -498,6 +514,90 @@ public function addScript()
Admin::script($script);
}

/**
* @return string
*/
protected function buildConfirmActionPromise()
{
$trans = [
'cancel' => trans('admin.cancel'),
'submit' => trans('admin.submit'),
];

$settings = [
'type' => 'question',
'showCancelButton' => true,
'showLoaderOnConfirm' => true,
'confirmButtonText' => $trans['submit'],
'cancelButtonText' => $trans['cancel'],
'title' => $this->confirm,
'text' => '',
];

$settings = trim(substr(json_encode($settings, JSON_PRETTY_PRINT), 1, -1));

return <<<PROMISE
var process = $.admin.swal({
{$settings},
preConfirm: function() {
{$this->buildGeneralActionPromise()}
return process;
}
}).then(function(result) {
if (typeof result.dismiss !== 'undefined') {
return Promise.reject();
}
var result = result.value[0];
if (typeof result.status === "boolean") {
var response = result;
} else {
var response = result.value;
}
return [response, target];
});
PROMISE;
}

protected function buildGeneralActionPromise()
{
return <<<SCRIPT
var process = new Promise(function (resolve,reject) {
Object.assign(data, {
_token: $.admin.token,
_action: '{$this->action->getCalledClass()}',
});
var formData = new FormData(form);
for (var key in data) {
formData.append(key, data[key]);
}
$.ajax({
method: '{$this->action->getMethod()}',
url: '{$this->action->getHandleRoute()}',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
resolve([data, target]);
if (data.status === true) {
$('#'+modalId).modal('hide');
}
},
error:function(request){
reject(request);
}
});
});
SCRIPT;
}

/**
* @throws \Exception
*
@@ -513,36 +613,10 @@ protected function buildActionPromise()

$this->addModalHtml();

return <<<SCRIPT
var process = new Promise(function (resolve,reject) {
Object.assign(data, {
_token: $.admin.token,
_action: '{$this->action->getCalledClass()}',
});
var formData = new FormData(form);
for (var key in data) {
formData.append(key, data[key]);
}
if (!empty($this->confirm)) {
return $this->buildConfirmActionPromise();
}

$.ajax({
method: '{$this->action->getMethod()}',
url: '{$this->action->getHandleRoute()}',
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
resolve([data, target]);
if (data.status === true) {
$('#'+modalId).modal('hide');
}
},
error:function(request){
reject(request);
}
});
});
SCRIPT;
return $this->buildGeneralActionPromise();
}
}

0 comments on commit 700be46

Please sign in to comment.