shorten many invocations of Ajax.Request in inline form methods

This commit is contained in:
Andrew Dolgov 2021-02-18 12:27:26 +03:00
parent ee0b66b6bd
commit 39c0fe3697
5 changed files with 78 additions and 112 deletions

View File

@ -291,18 +291,14 @@ class Pref_Prefs extends Handler_Protected {
<?= \Controls\hidden_tag("op", "pref-prefs") ?> <?= \Controls\hidden_tag("op", "pref-prefs") ?>
<?= \Controls\hidden_tag("method", "changeemail") ?> <?= \Controls\hidden_tag("method", "changeemail") ?>
<script type='dojo/method' event='onSubmit' args='evt'> <script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
Notify.progress('Saving data...', true); Notify.progress('Saving data...', true);
xhrPost("backend.php", this.getValues(), (transport) => {
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
Notify.info(transport.responseText); Notify.info(transport.responseText);
} })
}); }
}
</script> </script>
<fieldset> <fieldset>
@ -350,32 +346,28 @@ class Pref_Prefs extends Handler_Protected {
<?= \Controls\hidden_tag("op", "pref-prefs") ?> <?= \Controls\hidden_tag("op", "pref-prefs") ?>
<?= \Controls\hidden_tag("method", "changepassword") ?> <?= \Controls\hidden_tag("method", "changepassword") ?>
<script type='dojo/method' event='onSubmit' args='evt'> <!-- TODO: return JSON the backend call -->
<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
Notify.progress('Changing password...', true); Notify.progress('Saving data...', true);
xhrPost("backend.php", this.getValues(), (transport) => {
Notify.close();
if (transport.responseText.indexOf('ERROR: ') == 0) {
new Ajax.Request('backend.php', { $('pwd_change_infobox').innerHTML =
parameters: dojo.objectToQuery(this.getValues()), transport.responseText.replace('ERROR: ', '');
onComplete: function(transport) {
Notify.close();
if (transport.responseText.indexOf('ERROR: ') == 0) {
$('pwd_change_infobox').innerHTML = } else {
transport.responseText.replace('ERROR: ', ''); $('pwd_change_infobox').innerHTML =
transport.responseText.replace('ERROR: ', '');
} else { const warn = $('default_pass_warning');
$('pwd_change_infobox').innerHTML = if (warn) Element.hide(warn);
transport.responseText.replace('ERROR: ', '');
const warn = $('default_pass_warning');
if (warn) Element.hide(warn);
}
new Effect.Appear('pwd_change_infobox');
} }
});
this.reset(); new Effect.Appear('pwd_change_infobox');
})
} }
</script> </script>
@ -459,23 +451,20 @@ class Pref_Prefs extends Handler_Protected {
<?= \Controls\hidden_tag("op", "pref-prefs") ?> <?= \Controls\hidden_tag("op", "pref-prefs") ?>
<?= \Controls\hidden_tag("method", "otpdisable") ?> <?= \Controls\hidden_tag("method", "otpdisable") ?>
<script type='dojo/method' event='onSubmit' args='evt'> <!-- TODO: return JSON from the backend call -->
<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
Notify.progress('Disabling OTP', true); Notify.progress('Saving data...', true);
xhrPost("backend.php", this.getValues(), (transport) => {
Notify.close();
new Ajax.Request('backend.php', { if (transport.responseText.indexOf('ERROR: ') == 0) {
parameters: dojo.objectToQuery(this.getValues()), Notify.error(transport.responseText.replace('ERROR: ', ''));
onComplete: function(transport) { } else {
Notify.close(); window.location.reload();
if (transport.responseText.indexOf('ERROR: ') == 0) {
Notify.error(transport.responseText.replace('ERROR: ', ''));
} else {
window.location.reload();
}
} }
}); })
this.reset();
} }
</script> </script>
@ -521,22 +510,20 @@ class Pref_Prefs extends Handler_Protected {
<input dojoType='dijit.form.ValidationTextBox' disabled='disabled' value="<?= $otp_secret ?>" size='32'> <input dojoType='dijit.form.ValidationTextBox' disabled='disabled' value="<?= $otp_secret ?>" size='32'>
</fieldset> </fieldset>
<script type='dojo/method' event='onSubmit' args='evt'> <!-- TODO: return JSON from the backend call -->
<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
Notify.progress('Saving data...', true); Notify.progress('Saving data...', true);
xhrPost("backend.php", this.getValues(), (transport) => {
Notify.close();
new Ajax.Request('backend.php', { if (transport.responseText.indexOf('ERROR:') == 0) {
parameters: dojo.objectToQuery(this.getValues()), Notify.error(transport.responseText.replace('ERROR:', ''));
onComplete: function(transport) { } else {
Notify.close(); window.location.reload();
if (transport.responseText.indexOf('ERROR:') == 0) {
Notify.error(transport.responseText.replace('ERROR:', ''));
} else {
window.location.reload();
}
} }
}); })
} }
</script> </script>
@ -806,26 +793,23 @@ class Pref_Prefs extends Handler_Protected {
<form dojoType='dijit.form.Form' id='changeSettingsForm'> <form dojoType='dijit.form.Form' id='changeSettingsForm'>
<?= \Controls\hidden_tag("op", "pref-prefs") ?> <?= \Controls\hidden_tag("op", "pref-prefs") ?>
<?= \Controls\hidden_tag("method", "saveconfig") ?> <?= \Controls\hidden_tag("method", "saveconfig") ?>
<script type='dojo/method' event='onSubmit' args='evt, quit'>
<script type="dojo/method" event="onSubmit" args="evt, quit">
if (evt) evt.preventDefault(); if (evt) evt.preventDefault();
if (this.validate()) { if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues())); xhrPost("backend.php", this.getValues(), (transport) => {
if (quit) {
document.location.href = 'index.php';
} else {
const msg = transport.responseText;
new Ajax.Request('backend.php', { if (msg == 'PREFS_NEED_RELOAD') {
parameters: dojo.objectToQuery(this.getValues()), window.location.reload();
onComplete: function(transport) {
var msg = transport.responseText;
if (quit) {
document.location.href = 'index.php';
} else { } else {
if (msg == 'PREFS_NEED_RELOAD') { Notify.info(msg);
window.location.reload();
} else {
Notify.info(msg);
}
} }
} }
}); })
} }
</script> </script>
@ -956,20 +940,15 @@ class Pref_Prefs extends Handler_Protected {
?> ?>
<form dojoType="dijit.form.Form" id="changePluginsForm"> <form dojoType="dijit.form.Form" id="changePluginsForm">
<script type="dojo/method" event="onSubmit" args="evt"> <script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
Notify.progress('Saving data...', true); xhrPost("backend.php", this.getValues(), (transport) => {
new Ajax.Request('backend.php', {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
Notify.close(); Notify.close();
if (confirm(__('Selected plugins have been enabled. Reload?'))) { if (confirm(__('Selected plugins have been enabled. Reload?'))) {
window.location.reload(); window.location.reload();
} }
} })
}); }
}
</script> </script>
<?= \Controls\hidden_tag("op", "pref-prefs") ?> <?= \Controls\hidden_tag("op", "pref-prefs") ?>

View File

@ -69,19 +69,15 @@ class Af_Readability extends Plugin {
<?= \Controls\pluginhandler_tags($this, "save") ?> <?= \Controls\pluginhandler_tags($this, "save") ?>
<script type='dojo/method' event='onSubmit' args='evt'> <script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues())); Notify.progress('Saving data...', true);
new Ajax.Request('backend.php', { xhrPost("backend.php", this.getValues(), (transport) => {
parameters: dojo.objectToQuery(this.getValues()), Notify.info(transport.responseText);
onComplete: function(transport) { })
Notify.info(transport.responseText);
}
});
//this.reset();
} }
</script> </script>
<fieldset> <fieldset>
<label class='checkbox'> <label class='checkbox'>

View File

@ -43,16 +43,13 @@ class Af_RedditImgur extends Plugin {
<?= \Controls\pluginhandler_tags($this, "save") ?> <?= \Controls\pluginhandler_tags($this, "save") ?>
<script type='dojo/method' event='onSubmit' args='evt'> <script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues())); Notify.progress('Saving data...', true);
new Ajax.Request('backend.php', { xhrPost("backend.php", this.getValues(), (transport) => {
parameters: dojo.objectToQuery(this.getValues()), Notify.info(transport.responseText);
onComplete: function(transport) { })
Notify.info(transport.responseText);
}
});
} }
</script> </script>

View File

@ -50,15 +50,12 @@ class Mail extends Plugin {
<script type="dojo/method" event="onSubmit" args="evt"> <script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
console.log(dojo.objectToQuery(this.getValues())); Notify.progress('Saving data...', true);
new Ajax.Request('backend.php', { xhrPost("backend.php", this.getValues(), (transport) => {
parameters: dojo.objectToQuery(this.getValues()), Notify.info(transport.responseText);
onComplete: function(transport) { })
Notify.info(transport.responseText);
}
});
} }
</script> </script>
<header><?= __("You can set predefined email addressed here (comma-separated list):") ?></header> <header><?= __("You can set predefined email addressed here (comma-separated list):") ?></header>
@ -70,7 +67,6 @@ class Mail extends Plugin {
<?= \Controls\submit_tag(__("Save")) ?> <?= \Controls\submit_tag(__("Save")) ?>
</form> </form>
</div> </div>
<?php <?php
} }

View File

@ -55,12 +55,10 @@ class NSFW extends Plugin {
<script type="dojo/method" event="onSubmit" args="evt"> <script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault(); evt.preventDefault();
if (this.validate()) { if (this.validate()) {
new Ajax.Request('backend.php', { Notify.progress('Saving data...', true);
parameters: dojo.objectToQuery(this.getValues()), xhrPost("backend.php", this.getValues(), (transport) => {
onComplete: function(transport) { Notify.info(transport.responseText);
Notify.info(transport.responseText); })
}
});
} }
</script> </script>