Skip to content

Commit

Permalink
feat(slb-146): remove obsolete restore/reject functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Apr 12, 2024
1 parent 4dcbb47 commit cdeb628
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 150 deletions.
66 changes: 1 addition & 65 deletions packages/drupal/silverback_autosave/js/silverback_autosave.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,71 +124,7 @@
.appendTo('body')
.append(Drupal.autosaveForm.notification.message);

// Show the resume/discard confirmation message only if the form does
// not contain any errors, otherwise continue with normal autosave
// submissions. This is for the use case where a form is submitted but
// returned to the user with validation errors in which case we should
// not show the resume/discard message but continue the autosave
// submissions.
if (Drupal.autosaveForm.message && !Drupal.autosaveForm.formHasErrors) {
var dialogOptions = {
buttons: {
button_confirm: {
text: Drupal.t('Resume editing'),
class: 'autosave-form-resume-button',
click: function () {
// Non ajax buttons are bound to click.
// autosave-form-restore
$('.' + Drupal.autosaveForm.autosave_restore_class).trigger(
'click',
);
},
},
button_reject: {
text: Drupal.t('Discard'),
class: 'autosave-form-reject-button',
click: function () {
triggerAjaxSubmitWithoutProgressIndication(
Drupal.autosaveForm.autosave_reject_class,
true,
);
$(this).dialog('close');
},
primary: true,
},
},
close: function (event, ui) {
$(this).remove();
$(context)
.find('.' + Drupal.autosaveForm.autosave_restore_class)
.remove();
$(context)
.find('.' + Drupal.autosaveForm.autosave_reject_class)
.remove();
$(context).find('.autosave-form-restore-discard').remove();
autosavePeriodic();
},
};

$.extend(
true,
dialogOptions,
Drupal.autosaveForm.defaultDialogOptions,
Drupal.autosaveForm.dialog_options,
);

$('<div></div>')
.appendTo('body')
.html('<div>' + Drupal.autosaveForm.message + '</div>')
.dialog(dialogOptions);

// Temp
const btn = $('.autosave-form-restore');
$('.ui-dialog-buttonset').append(btn[0]);
// ....
} else {
autosavePeriodic();
}
autosavePeriodic();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,4 @@ protected function isAutosaveTriggered(FormStateInterface $form_state) {
return $autosave;
}

/**
* Checks if autosave restore has been triggered.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return bool
* TRUE if autosave restore has been triggered, FALSE otherwise.
*/
protected function isRestoreTriggered($form, FormStateInterface $form_state) {
$input = $form_state->getUserInput();
// The restore submit is a non-ajax element and therefore its name will be
// contained in the user input as a key.
$triggered = isset($input[AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME]);
return $triggered;
}

/**
* Checks if autosave restore has been triggered.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return bool
* TRUE if autosave restore has been triggered, FALSE otherwise.
*/
protected function isRejectTriggered($form, FormStateInterface $form_state) {
$user_input = $form_state->getUserInput();
// The reject submit is an ajax element and therefore its name will not be
// contained in the user input as a key, but will be contained as a value
// under the key "_triggering_element_name".
$triggered = isset($user_input['_triggering_element_name']) && ($user_input['_triggering_element_name'] == AutosaveFormInterface::AUTOSAVE_REJECT_ELEMENT_NAME);
return $triggered;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,6 @@ public function formAlter(array &$form, FormStateInterface $form_state) {
$form['#attached']['drupalSettings']['autosaveForm']['notification'] = $this->configFactory->get('silverback_autosave.settings')->get('notification');
$input = $form_state->getUserInput();

// $show_restore_discard = !$form_state->isRebuilding() ?: !empty($input['autosave_restore_discard']);
$show_restore_discard = FALSE;
if ($show_restore_discard && !$form_state->get('silverback_autosave_state_timestamp') && !$form_state->get('silverback_autosave_rejected') && ($silverback_autosave_state_timestamp = $this->getLastAutosavedTimestamp($form_state, $this->currentUser->id()))) {
$form[AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME] = [
'#type' => 'submit',
'#name' => AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME,
'#value' => $this->t('Autosave restore'),
'#limit_validation_errors' => [],
// '#attributes' => ['class' => ['autosave-form-restore', 'visually-hidden']],
'#attributes' => ['class' => ['autosave-form-restore']],
'#submit' => [[$this, 'autosaveFormRestoreSubmit']],
'#silverback_autosave_state_timestamp' => $silverback_autosave_state_timestamp,
];
$form[AutosaveFormInterface::AUTOSAVE_REJECT_ELEMENT_NAME] = [
'#type' => 'submit',
'#name' => 'silverback_autosave_reject',
'#value' => $this->t('Autosave reject'),
'#limit_validation_errors' => [],
'#attributes' => ['class' => ['autosave-form-reject', 'visually-hidden']],
'#submit' => [[$this, 'autosaveFormRejectSubmit']],
'#ajax' => [
'callback' => [$this, 'autosaveFormRejectAjax'],
],
];
$form['autosave_restore_discard'] = [
'#type' => 'hidden',
'#default_value' => 'autosave_restore_discard',
'#attributes' => ['class' => ['autosave-form-restore-discard']],
];

// Add the message to be shown on the form. Our JS library will check if
// the message exist and only then offer the options for restore and
// reject, otherwise will start the auto save process.
$date = $this->dateFormatter->format($silverback_autosave_state_timestamp, 'custom', 'M d, Y H:i');
$message = $this->t('A version of this page you were editing at @date was saved as a draft. Do you want to resume editing or discard it?', ['@date' => $date]);
$form['#attached']['drupalSettings']['autosaveForm']['message'] = (string) $message;
}

$silverback_autosave_session_id = $this->getAutosaveFormSessionID($form_state);
if (!$silverback_autosave_session_id) {
$silverback_autosave_session_id = !empty($input['silverback_autosave_session_id']) ? $input['silverback_autosave_session_id'] : $form['#build_id'];
Expand Down Expand Up @@ -204,9 +166,6 @@ public function autosaveFormSubmit($form, FormStateInterface $form_state) {
'form_token',
'ajax_page_state',
'silverback_autosave_last_autosave_timestamp',
AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME,
AutosaveFormInterface::AUTOSAVE_REJECT_ELEMENT_NAME,
'autosave_restore_discard',
];
foreach ($skip_from_comparison_keys as $skip_from_comparison_key) {
unset($autosaved_form_state_input[$skip_from_comparison_key]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ public function buildForm($form_id, FormStateInterface &$form_state) {
// Additionally unset the form elements and settings which might have been
// added, but aren't actually needed.
unset($form['#attached']['drupalSettings']['autosaveForm']['message']);
unset($form[AutosaveFormInterface::AUTOSAVE_RESTORE_ELEMENT_NAME]);
unset($form[AutosaveFormInterface::AUTOSAVE_REJECT_ELEMENT_NAME]);
unset($form['autosave_restore_discard']);
}
return $form;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
interface AutosaveFormInterface {

const AUTOSAVE_ELEMENT_NAME = 'silverback_autosave_save';
const AUTOSAVE_RESTORE_ELEMENT_NAME = 'silverback_autosave_restore';
const AUTOSAVE_REJECT_ELEMENT_NAME = 'silverback_autosave_reject';

/**
* Performs the needed alterations to the form.
Expand Down

0 comments on commit cdeb628

Please sign in to comment.