Skip to content

Commit

Permalink
Reorganize system alert messages based on bootstrap alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinBM authored and josaphatim committed Mar 12, 2024
1 parent 2471d56 commit 52a978b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 62 deletions.
6 changes: 3 additions & 3 deletions modules/core/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,14 @@ protected function output() {
if (!$this->get('router_login_state') && !empty($msgs)) {
$logged_out_class = ' logged_out';
}
$res .= '<div class="z-3 position-fixed top-0 end-0 mt-3 me-3 sys_messages'.$logged_out_class.'">';
$res .= '<div class="d-none z-3 position-fixed top-0 end-0 mt-3 me-3 sys_messages'.$logged_out_class.'">';
if (!empty($msgs)) {
$res .= implode(',', array_map(function($v) {
if (preg_match("/ERR/", $v)) {
return sprintf('<div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="bi bi-exclamation-triangle me-2"></i><span class="err">%s</span>', $this->trans(substr((string) $v, 3)));
return sprintf('<div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="bi bi-exclamation-triangle me-2"></i><span class="danger">%s</span>', $this->trans(substr((string) $v, 3)));
}
else {
return sprintf('<div class="alert alert-info alert-dismissible fade show" role="alert"><i class="bi bi-info-circle me-2"></i><span>%s</span>', $this->trans($v));
return sprintf('<div class="alert alert-success alert-dismissible fade show" role="alert"><i class="bi bi-check-circle me-2"></i><span>%s</span class="info">', $this->trans($v));
}
}, $msgs));
$res .= '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
Expand Down
1 change: 0 additions & 1 deletion modules/core/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ td { vertical-align: top; }
.date { display: none; position: absolute; top: 15px; right: 20px; color: #888; padding-right: 0px; padding-top: 6px;}

/* .sys_messages { position: fixed; right: 20px; top: 15px; min-height: 30px; min-width: 300px; cursor: pointer; display: none; margin-top: 0px; padding: 15px; padding-bottom: 5px; white-space: nowrap; border: solid 1px #999; border-radius: 5px; filter: drop-shadow(4px 4px 4px #ccc); z-index: 101; } */
.logged_out { display: block !important; }
#theme_setting, #timezone_setting { max-width: 200px; }
.s_pre { color: #666}
.message_table > thead > tr > th { display: none; }
Expand Down
55 changes: 27 additions & 28 deletions modules/core/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,33 +387,21 @@ function Hm_Modal(options) {
var Hm_Notices = {
hide_id: false,

show: function(msgs, keep) {
var msg_list = [];
show: function(msgs) {
var message = '';
var type = '';
for (var i in msgs) {
if (msgs[i].match(/^ERR/)) {
msg_list.push(msgs[i].substring(3));
message = msgs[i].substring(3);
type = 'danger';
}
else {
msg_list.push(msgs[i]);
}
}
if (!keep) {
$('.err').html(msg_list.join(', '));
}
else {
var existing = $('.err').html();
if (existing) {
$('.err').append('<br />'+msg_list.join(', '));
}
else {
$('.err').html(msg_list.join(', '));
type = 'info';
message = msgs[i];
}

Hm_Utils.add_sys_message(message, type);
}
$('.sys_messages').show();
$('.sys_messages').on('click', function() {
$('.sys_messages').hide();
$('.sys_messages').html('');
});
},

hide: function(now) {
Expand Down Expand Up @@ -1605,14 +1593,25 @@ var Hm_Utils = {
$('#unsaved_changes').val(state);
},

/**
* Shows pending messages added with the add_sys_message method
*/
show_sys_messages: function() {
if ($('.sys_messages').text().length) {
$('.sys_messages').show();
$('.sys_messages').on('click', function() {
$('.sys_messages').hide();
$('.sys_messages').html('');
});
}
$('.sys_messages').removeClass('d-none');
},

/**
*
* @param {*} msg : The alert message to display
* @param {*} type : The type of message to display, depending on the type of boostrap5 alert (primary, secondary, success, danger, warning, info, light, dark )
* @param {*} pending : Defined if the added message must be displayed directly, or be pending until the show_sys_messages method is called
*/
add_sys_message: function(msg, type = 'info', pending = false) {
if (pending) $('.sys_messages').addClass('d-none');
else Hm_Utils.show_sys_messages();
if (!msg || msg == '') return $('.sys_messages').html('');
const icon = type == 'success' ? 'bi-check-circle' : 'bi-exclamation-circle';
return $('.sys_messages').append('<div class="alert alert-'+type+' alert-dismissible fade show" role="alert"><i class="bi '+icon+' me-2"></i><span class="' + type + '">'+msg+'</span><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
},

cancel_logout_event: function() {
Expand Down
20 changes: 7 additions & 13 deletions modules/pgp/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,26 @@ var Hm_Pgp = {
Hm_Pgp.show_error();
}
else {
$('.sys_messages').hide();
Hm_Notices.hide(true);
}
},

show_error: function() {
$('.sys_messages').html('<span class="err">'+Hm_Pgp.error_msg+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(Hm_Pgp.error_msg, 'danger');
$('.smtp_send').removeClass('disabled_input');
Hm_Pgp.error_msg = '';
},

update_private_keys: function(key) {
if (!Hm_Pgp.validate_private_key(key)) {
$('.sys_messages').html('<span class="err">'+hm_trans('Unable to import private key')+'</span>'));
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('Unable to import private key'), 'danger');
return;
}
var keys = Hm_Pgp.get_private_keys();
keys.push(key);
Hm_Utils.save_to_local_storage('pgp_keys', JSON.stringify(keys));
Hm_Pgp.list_private_keys();
$('.sys_messages').html('Private key saved');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('Private key saved'), 'success');
},

get_private_keys: function() {
Expand All @@ -244,8 +241,7 @@ var Hm_Pgp = {

read_private_key: function(evt) {
if (!evt.target.files.length) {
$('.sys_messages').html('<span class="err">'+hm_trans('Unable to import private key')+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('Unable to import private key'), 'danger');
return;
}
var reader = new FileReader();
Expand Down Expand Up @@ -281,8 +277,7 @@ var Hm_Pgp = {
}
}
Hm_Utils.save_to_local_storage('pgp_keys', JSON.stringify(newkeys));
$('.sys_messages').html(hm_trans('Private key removed'));
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('Private key removed'), 'info');
Hm_Pgp.list_private_keys();
}
},
Expand All @@ -307,8 +302,7 @@ var Hm_Pgp = {
else {
msg = 'Encrypting message...';
}
$('.sys_messages').html(hm_trans(msg));
$('.sys_messages').show();
Hm_Utils.add_sys_message(hm_trans(msg), 'info');
}
}

Expand Down
23 changes: 8 additions & 15 deletions modules/sievefilters/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,19 +394,17 @@ $(function () {

let idx = 0;
if (conditions.length === 0) {
$('.sys_messages').html('<span class="err">'+hm_trans('You must provide at least one condition')+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('You must provide at least one condition'), 'danger');
return false;
}

$('.sys_messages').html('');
Hm_Utils.add_sys_message();
conditions.forEach(function (elem, key) {
if (conditions_value[idx] === "" && conditions_value[idx] !== 'none') {
let order = ordinal_number(key + 1);
let previous_messages = $('.sys_messages').html();
previous_messages += previous_messages ? '<br>': '';
$('.sys_messages').html(previous_messages + '<span class="err">The ' + order + ' condition (' + elem + ') must be provided</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message('The ' + order + ' condition (' + elem + ') must be provided', 'danger');
validation_failed = true;
}
conditions_parsed.push(
Expand Down Expand Up @@ -435,8 +433,7 @@ $(function () {
}).get();

if (actions_type.length === 0) {
$('.sys_messages').html('<span class="err">'+hm_trans('You must provide at least one action')+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('You must provide at least one action'), 'danger');
return false;
}

Expand All @@ -447,8 +444,7 @@ $(function () {
let order = ordinal_number(key + 1);
let previous_messages = $('.sys_messages').html();
previous_messages += previous_messages ? '<br>': '';
$('.sys_messages').html(previous_messages + '<span class="err">The ' + order + ' action (' + elem + ') must be provided</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message('The ' + order + ' action (' + elem + ') must be provided', 'danger');
validation_failed = true;
}
actions_parsed.push(
Expand All @@ -463,8 +459,7 @@ $(function () {
});

if ($('.modal_sieve_filter_name').val() == "") {
$('.sys_messages').html('<span class="err">'+hm_trans('Filter name is required')+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('Filter name is required'), 'danger');
return false;
}

Expand Down Expand Up @@ -501,13 +496,11 @@ $(function () {

function save_script(imap_account) {
if ($('.modal_sieve_script_name').val() === "") {
$('.sys_messages').html('<span class="err">'+hm_trans('You must provide a name for your script')+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('You must provide a name for your script'), 'danger');
return false;
}
if ($('.modal_sieve_script_textarea').val() === "") {
$('.sys_messages').html('<span class="err">'+hm_trans('Empty script')+'</span>');
Hm_Utils.show_sys_messages();
Hm_Utils.add_sys_message(hm_trans('Empty script'), 'danger');
return false;
}
Hm_Ajax.request(
Expand Down
2 changes: 1 addition & 1 deletion modules/smtp/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var get_smtp_profile = function(profile_value) {
if (typeof profile_value === "undefined" || profile_value == "0" || profile_value == "") {
Hm_Notices.show([err_msg('Please create a profile for saving sent messages option')], true);
Hm_Notices.show([err_msg('Please create a profile for saving sent messages option')]);
}
else {
Hm_Ajax.request(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/modules/core/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ public function test_msgs() {
$test = new Output_Test('msgs', 'core');
$test->handler_response = array('router_login_state' => false);
$res = $test->run();
$this->assertEquals(array('<div class="z-3 position-fixed top-0 end-0 mt-3 me-3 sys_messages logged_out"><div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="bi bi-exclamation-triangle me-2"></i><span class="err">foo</span>,<div class="alert alert-info alert-dismissible fade show" role="alert"><i class="bi bi-info-circle me-2"></i><span>foo</span><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div></div>'), $res->output_response);
$this->assertEquals(array('<div class="d-none z-3 position-fixed top-0 end-0 mt-3 me-3 sys_messages logged_out"><div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="bi bi-exclamation-triangle me-2"></i><span class="danger">foo</span>,<div class="alert alert-info alert-dismissible fade show" role="alert"><i class="bi bi-info-circle me-2"></i><span>foo</span><button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div></div>'), $res->output_response);
}
/**
* @preserveGlobalState disabled
Expand Down

0 comments on commit 52a978b

Please sign in to comment.