Skip to content

Commit

Permalink
Fixes Settings not getting saved when rewrites were turned off (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
namithj authored Nov 3, 2024
1 parent 8c201e8 commit 1629ad6
Showing 1 changed file with 112 additions and 83 deletions.
195 changes: 112 additions & 83 deletions assets/js/aspire-update.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,84 @@
jQuery(document).ready(function () {
new FieldRules();
new ApiHost();
new ApiRewrites();
new ApiDebug();
});


class ApiHost {
class ApiRewrites {
constructor() {
ApiHost.host_selector.init();
ApiHost.other_hosts.init();
ApiHost.api_key.init();
ApiRewrites.host_selector.init();
ApiRewrites.other_hosts.init();
ApiRewrites.api_key.init();
ApiRewrites.enabled_rewrites.init();
}

static enabled_rewrites = {
field: jQuery('#aspireupdate-settings-field-enable'),
sub_fields: [],
init() {
ApiRewrites.enabled_rewrites.sub_fields = [
ApiRewrites.host_selector,
ApiRewrites.api_key
];

ApiRewrites.enabled_rewrites.field.change(function () {
if (jQuery(this).is(':checked')) {
ApiRewrites.enabled_rewrites.show_options();
} else {
ApiRewrites.enabled_rewrites.hide_options();
}
}).change();
},
show_options() {
Fields.show(ApiRewrites.enabled_rewrites.sub_fields);
},
hide_options() {
Fields.hide(ApiRewrites.enabled_rewrites.sub_fields);
}
}

static host_selector = {
field: jQuery('#aspireupdate-settings-field-api_host'),
init() {
ApiHost.host_selector.field.change(function () {
let selected_option = ApiHost.host_selector.field.find(":selected");
ApiRewrites.host_selector.field.change(function () {
let selected_option = ApiRewrites.host_selector.field.find(":selected");
if ('other' === selected_option.val()) {
ApiHost.other_hosts.show();
ApiRewrites.other_hosts.show();
} else {
ApiHost.other_hosts.hide();
ApiRewrites.other_hosts.hide();
}

if (ApiHost.host_selector.is_api_key_required()) {
ApiHost.api_key.make_required();
if (ApiRewrites.host_selector.is_api_key_required()) {
ApiRewrites.api_key.make_required();
} else {
ApiHost.api_key.remove_required();
ApiRewrites.api_key.remove_required();
}

if (ApiHost.host_selector.has_api_key_url()) {
ApiHost.api_key.show_action_button();
if (ApiRewrites.host_selector.has_api_key_url()) {
ApiRewrites.api_key.show_action_button();
} else {
ApiHost.api_key.hide_action_button();
ApiRewrites.api_key.hide_action_button();
}
}).change();
},
is_api_key_required() {
let selected_option = ApiHost.host_selector.field.find(":selected");
let is_api_rewrites_enabled = jQuery('#aspireupdate-settings-field-enable').is(':checked');
let selected_option = ApiRewrites.host_selector.field.find(":selected");
let require_api_key = selected_option.attr('data-require-api-key');
if ('true' === require_api_key) {
if (is_api_rewrites_enabled && 'true' === require_api_key) {
return true;
}
return false;
},
has_api_key_url() {
let selected_option = ApiHost.host_selector.field.find(":selected");
let selected_option = ApiRewrites.host_selector.field.find(":selected");
let api_url = selected_option.attr('data-api-key-url');
if ('' !== api_url) {
return true;
}
return false;
},
get_api_key_url() {
let selected_option = ApiHost.host_selector.field.find(":selected");
let selected_option = ApiRewrites.host_selector.field.find(":selected");
let api_url = selected_option.attr('data-api-key-url');
if ('' !== api_url) {
return api_url;
Expand All @@ -64,27 +90,27 @@ class ApiHost {
static other_hosts = {
field: jQuery('#aspireupdate-settings-field-api_host_other'),
init() {
ApiHost.other_hosts.field.on("blur", function () {
let value = ApiHost.other_hosts.field.val();
value = ApiHost.other_hosts.strip_protocol(value);
value = ApiHost.other_hosts.strip_dangerous_characters(value);
ApiHost.other_hosts.field.val(value);
ApiRewrites.other_hosts.field.on("blur", function () {
let value = ApiRewrites.other_hosts.field.val();
value = ApiRewrites.other_hosts.strip_protocol(value);
value = ApiRewrites.other_hosts.strip_dangerous_characters(value);
ApiRewrites.other_hosts.field.val(value);
});
},
show() {
ApiHost.other_hosts.field.parent().show();
ApiHost.other_hosts.field.focus();
ApiHost.other_hosts.make_required();
ApiRewrites.other_hosts.field.parent().show();
ApiRewrites.other_hosts.field.focus();
ApiRewrites.other_hosts.make_required();
},
hide() {
ApiHost.other_hosts.field.parent().hide();
ApiHost.other_hosts.remove_required();
ApiRewrites.other_hosts.field.parent().hide();
ApiRewrites.other_hosts.remove_required();
},
make_required() {
ApiHost.other_hosts.field.prop('required', true);
ApiRewrites.other_hosts.field.prop('required', true);
},
remove_required() {
ApiHost.other_hosts.field.prop('required', false);
ApiRewrites.other_hosts.field.prop('required', false);
},
strip_protocol(value) {
const protocol_regex = /^(https?|ftp|sftp|smtp|ftps|file):\/\/|^www\./i;
Expand All @@ -100,15 +126,15 @@ class ApiHost {
field: jQuery('#aspireupdate-settings-field-api_key'),
action_button: jQuery('#aspireupdate-generate-api-key'),
init() {
ApiHost.api_key.action_button.click(function () {
ApiHost.api_key.hide_error();
ApiHost.api_key.get_api_key();
ApiRewrites.api_key.action_button.click(function () {
ApiRewrites.api_key.hide_error();
ApiRewrites.api_key.get_api_key();
});
ApiHost.api_key.hide_error();
ApiRewrites.api_key.hide_error();
},
get_api_key() {
let parameters = {
"url": ApiHost.host_selector.get_api_key_url(),
"url": ApiRewrites.host_selector.get_api_key_url(),
"type": "POST",
"contentType": 'application/json',
"data": JSON.stringify({
Expand All @@ -117,94 +143,97 @@ class ApiHost {
};
jQuery.ajax(parameters)
.done(function (response) {
ApiHost.api_key.field.val(response.apikey);
ApiRewrites.api_key.field.val(response.apikey);
})
.fail(function (response) {
if ((response.status === 400) || (response.status === 401)) {
ApiHost.api_key.show_error(response.responseJSON?.error);
ApiRewrites.api_key.show_error(response.responseJSON?.error);
} else {
ApiHost.api_key.show_error('Unexpected Error: ' + response.status);
ApiRewrites.api_key.show_error('Unexpected Error: ' + response.status);
}
});
},
show() {
ApiHost.api_key.field.parent().parent().parent().show();
ApiRewrites.api_key.field.parent().parent().parent().show();
},
hide() {
ApiHost.api_key.field.parent().parent().parent().hide();
ApiRewrites.api_key.field.parent().parent().parent().hide();
},
show_action_button() {
ApiHost.api_key.action_button.show();
ApiRewrites.api_key.action_button.show();
},
hide_action_button() {
ApiHost.api_key.action_button.hide();
ApiRewrites.api_key.action_button.hide();
},
make_required() {
ApiHost.api_key.field.prop('required', true);
ApiRewrites.api_key.field.prop('required', true);
},
remove_required() {
ApiHost.api_key.field.prop('required', false);
ApiRewrites.api_key.field.prop('required', false);
},
show_error(message) {
ApiHost.api_key.field.parent().find('.error').html(message).show();
ApiRewrites.api_key.field.parent().find('.error').html(message).show();
},
hide_error() {
ApiHost.api_key.field.parent().find('.error').html('').hide();
ApiRewrites.api_key.field.parent().find('.error').html('').hide();
}
}
}

class FieldRules {
class ApiDebug {
constructor() {
FieldRules.check_enabled_rewrites.init();
FieldRules.check_enabled_debug_mode.init();
ApiDebug.enabled_debug.init();
}

static check_enabled_rewrites = {
static enabled_debug = {
field: jQuery('#aspireupdate-settings-field-enable_debug'),
sub_fields: [],
init() {
jQuery('#aspireupdate-settings-field-enable').change(function () {
if (jQuery(this).is(':checked')) {
FieldRules.show_field('api_host');
FieldRules.show_field('api_key');
} else {
FieldRules.hide_field('api_host');
FieldRules.hide_field('api_key');
}
}).change();
}
}
ApiDebug.enabled_debug.sub_fields = [
ApiDebug.debug_type,
ApiDebug.disable_ssl_verification
];

static check_enabled_debug_mode = {
init() {
jQuery('#aspireupdate-settings-field-enable_debug').change(function () {
ApiDebug.enabled_debug.field.change(function () {
if (jQuery(this).is(':checked')) {
FieldRules.show_field('enable_debug_type');
FieldRules.show_field('disable_ssl_verification');
ApiDebug.enabled_debug.show_options();
} else {
FieldRules.hide_field('enable_debug_type');
FieldRules.hide_field('disable_ssl_verification');
ApiDebug.enabled_debug.hide_options();
}
}).change();
},
show_options() {
Fields.show(ApiDebug.enabled_debug.sub_fields);
},
hide_options() {
Fields.hide(ApiDebug.enabled_debug.sub_fields);
}
}

static hide_field(id) {
jQuery('.aspireupdate-settings-field-wrapper-' + id).parent().parent().hide();
static debug_type = {
field: jQuery('.aspireupdate-settings-field-wrapper-enable_debug_type'),
}

static show_field(id) {
let field_row = jQuery('.aspireupdate-settings-field-wrapper-' + id).parent().parent();
field_row.show().addClass('glow-reveal');
setTimeout(function () {
field_row.removeClass('glow-reveal');
}, 500);
static disable_ssl_verification = {
field: jQuery('#aspireupdate-settings-field-disable_ssl_verification'),
}
}

static remove_required(id) {
jQuery('#aspireupdate-settings-field-' + id).prop('required', false);
class Fields {
static show(sub_fields) {
jQuery.each(sub_fields, function (index, sub_field) {
sub_field.field.closest('tr').show().addClass('glow-reveal');
sub_field.field.change();
setTimeout(function () {
sub_field.field.closest('tr').removeClass('glow-reveal');
}, 500);
});
}

static make_required(id) {
jQuery('#aspireupdate-settings-field-' + id).prop('required', true);
static hide(sub_fields) {
jQuery.each(sub_fields, function (index, sub_field) {
sub_field.field.closest('tr').hide();
sub_field.field.change();
});
}
}

0 comments on commit 1629ad6

Please sign in to comment.