From 8f730177ff1e164a9fc0c5b3101e3a6f672e2cc0 Mon Sep 17 00:00:00 2001 From: Merci Jacob Date: Thu, 10 Oct 2024 13:10:53 +0200 Subject: [PATCH] Final move of the remnants of the code previously handled by hm_list_path(), hm_msg_uid(), and hm_page_name() to the new navigation module --- modules/contacts/site.js | 4 +- modules/core/js_modules/route_handlers.js | 9 +- modules/core/navigation/routes.js | 20 + modules/core/output_modules.php | 8 +- modules/core/site.js | 30 +- modules/feeds/site.js | 10 +- modules/github/site.js | 18 +- .../hello_world/js_modules/route_handlers.js | 8 + modules/hello_world/site.js | 14 +- modules/imap/js_modules/route_handlers.js | 4 + modules/imap/site.js | 68 +- modules/inline_message/site.js | 30 +- .../js_modules/route_handlers.js | 5 + modules/keyboard_shortcuts/site.js | 7 +- modules/nasa/site.js | 2 +- modules/nux/site.js | 12 - modules/pgp/js_modules/route_handlers.js | 13 + modules/pgp/site.js | 31 +- modules/profiles/js_modules/route_handlers.js | 3 + modules/profiles/site.js | 40 +- modules/saved_searches/site.js | 2 +- .../sievefilters/js_modules/route_handlers.js | 7 + modules/sievefilters/site.js | 2089 +++++++++++------ modules/smtp/js_modules/route_handlers.js | 3 + modules/smtp/site.js | 38 +- modules/tags/site.js | 3 - modules/wordpress/site.js | 18 +- 27 files changed, 1584 insertions(+), 912 deletions(-) create mode 100644 modules/hello_world/js_modules/route_handlers.js create mode 100644 modules/keyboard_shortcuts/js_modules/route_handlers.js create mode 100644 modules/pgp/js_modules/route_handlers.js create mode 100644 modules/profiles/js_modules/route_handlers.js create mode 100644 modules/sievefilters/js_modules/route_handlers.js diff --git a/modules/contacts/site.js b/modules/contacts/site.js index f6f14577b1..e4ebfd0f79 100644 --- a/modules/contacts/site.js +++ b/modules/contacts/site.js @@ -58,8 +58,8 @@ var add_contact_from_popup = function(event) { {'name': 'contact_source', 'value': source}], function (res) { $("#contact_popup_body").html(saveContactContent); - sessionStorage.removeItem(`${window.location.pathname}imap_4_${hm_list_path()}`); - sessionStorage.removeItem(`${window.location.pathname}${hm_msg_uid()}_${hm_list_path()}`); + sessionStorage.removeItem(`${window.location.pathname}imap_4_${getListPathParam()}`); + sessionStorage.removeItem(`${window.location.pathname}${getMessageUidParam()}_${getListPathParam()}`); } ); } diff --git a/modules/core/js_modules/route_handlers.js b/modules/core/js_modules/route_handlers.js index 88902cc759..a32b3ffc87 100644 --- a/modules/core/js_modules/route_handlers.js +++ b/modules/core/js_modules/route_handlers.js @@ -15,6 +15,9 @@ function applyServersPageHandlers() { // Optional modules if (window.feedServersPageHandler) feedServersPageHandler(); if (window.githubServersPageHandler) githubServersPageHandler(); + if (window.nasaServersPageHandler) nasaServersPageHandler(); + if (window.smtpServersPageHandler) smtpServersPageHandler(); + if (window.wpServersPageHandler) wpServersPageHandler(); } function applySettingsPageHandlers() { @@ -26,12 +29,16 @@ function applySettingsPageHandlers() { $('.reset_default_timezone').on("click", reset_default_timezone); if (window.expand_feed_settings) expand_feed_settings(); + if (window.smtpSettingsPageHandler) smtpSettingsPageHandler(); } -function applySearchPageHandlers() { +function applySearchPageHandlers(routeParams) { Hm_Message_List.select_combined_view(); sortHandlerForMessageListAndSearchPage(); $('.search_reset').on("click", Hm_Utils.reset_search_form); + + if (window.inlineMessageMessageListAndSearchPageHandler) inlineMessageMessageListAndSearchPageHandler(routeParams); + if (window.savedSearchesSearchPageHandler) savedSearchesSearchPageHandler(); } function applyHomePageHandlers() { diff --git a/modules/core/navigation/routes.js b/modules/core/navigation/routes.js index 702148093f..1ba38779a5 100644 --- a/modules/core/navigation/routes.js +++ b/modules/core/navigation/routes.js @@ -2,6 +2,10 @@ NOTE: Handlers are registered as strings instead of functions because some modules might not be enabled, making their pages' handler functions unaccessible. */ const modulesRoutes = [ + { + page: 'hello_world', + handler: 'applyHelloWorldPageHandlers' + }, { page: 'message_list', handler: 'applyImapMessageListPageHandlers' @@ -57,6 +61,22 @@ const modulesRoutes = [ { page: 'folders_subscription', handler: 'applyFoldersSubscriptionPageHandlers' + }, + { + page: 'pgp', + handler: 'applyPgpPageHandlers' + }, + { + page: 'profiles', + handler: 'applyProfilesPageHandler' + }, + { + page: 'block_list', + handler: 'applyBlockListPageHandlers' + }, + { + page: 'sieve_filters', + handler: 'applySieveFiltersPageHandler' } ] diff --git a/modules/core/output_modules.php b/modules/core/output_modules.php index 1b6442979f..e637f66892 100644 --- a/modules/core/output_modules.php +++ b/modules/core/output_modules.php @@ -552,6 +552,10 @@ protected function output() { } $core = false; $mods = $this->get('router_module_list'); + // Load navigation utilities used by subsequent modules' handlers + if (in_array('core', $mods)) { + $res .= ''; + } foreach (glob(APP_PATH.'modules'.DIRECTORY_SEPARATOR.'**', GLOB_ONLYDIR | GLOB_MARK) as $name) { $rel_name = str_replace(APP_PATH, '', $name); $mod = str_replace(array('modules', DIRECTORY_SEPARATOR), '', $rel_name); @@ -574,11 +578,11 @@ protected function output() { if ($core) { $res = ''.$res; /* Load navigation js modules - * utils.js, routes.js, navigation.js + * routes.js, navigation.js * They have to be loaded after each module's js files, because routes.js depend on the handlers defined in the modules. * Therefore, navigation.js is also loaded after routes.js, because the routes should be loaded beforehand to be able to navigate. */ - foreach (['utils', 'routes', 'navigation'] as $js) { + foreach (['routes', 'navigation'] as $js) { $res .= ''; } } diff --git a/modules/core/site.js b/modules/core/site.js index 8b11825e0b..00ad8d20d0 100644 --- a/modules/core/site.js +++ b/modules/core/site.js @@ -723,7 +723,7 @@ function Message_List() { }); } // apply JS pagination only on aggregate folders; imap ones already have the messages sorted - if (hm_list_path().substring(0, 5) != 'imap_' && element) { + if (getListPathParam().substring(0, 5) != 'imap_' && element) { $(row, msg_rows).insertBefore(element); } else { @@ -752,10 +752,10 @@ function Message_List() { this.update_after_action = function(action_type, selected) { var remove = false; - if (action_type == 'read' && hm_list_path() == 'unread') { + if (action_type == 'read' && getListPathParam() == 'unread') { remove = true; } - if (action_type == 'unflag' && hm_list_path() == 'flagged') { + if (action_type == 'unflag' && getListPathParam() == 'flagged') { remove = true; } else if (action_type == 'delete' || action_type == 'archive') { @@ -777,9 +777,9 @@ function Message_List() { }; this.save_updated_list = function() { - if (this.page_caches.hasOwnProperty(hm_list_path())) { - this.set_message_list_state(this.page_caches[hm_list_path()]); - Hm_Utils.save_to_local_storage('sort_'+hm_list_path(), this.sort_fld); + if (this.page_caches.hasOwnProperty(getListPathParam())) { + this.set_message_list_state(this.page_caches[getListPathParam()]); + Hm_Utils.save_to_local_storage('sort_'+getListPathParam(), this.sort_fld); } }; @@ -852,8 +852,8 @@ function Message_List() { }; this.select_combined_view = function() { - if (self.page_caches.hasOwnProperty(hm_list_path())) { - self.setup_combined_view(self.page_caches[hm_list_path()]); + if (self.page_caches.hasOwnProperty(getListPathParam())) { + self.setup_combined_view(self.page_caches[getListPathParam()]); } else { if (getPageNameParam() == 'search') { @@ -863,7 +863,7 @@ function Message_List() { self.setup_combined_view(false); } } - var sort_type = Hm_Utils.get_from_local_storage('sort_'+hm_list_path()); + var sort_type = Hm_Utils.get_from_local_storage('sort_'+getListPathParam()); if (sort_type != null) { this.sort_fld = sort_type; $('.combined_sort').val(sort_type); @@ -916,23 +916,23 @@ function Message_List() { var count = 0; var rows = Hm_Utils.rows(); var tbody = Hm_Utils.tbody(); - if (hm_list_path() == 'unread') { + if (getListPathParam() == 'unread') { count = rows.length; document.title = count+' '+hm_trans('Unread'); } - else if (hm_list_path() == 'flagged') { + else if (getListPathParam() == 'flagged') { count = rows.length; document.title = count+' '+hm_trans('Flagged'); } - else if (hm_list_path() == 'combined_inbox') { + else if (getListPathParam() == 'combined_inbox') { count = $('tr .unseen', tbody).length; document.title = count+' '+hm_trans('Unread in Everything'); } - else if (hm_list_path() == 'email') { + else if (getListPathParam() == 'email') { count = $('tr .unseen', tbody).length; document.title = count+' '+hm_trans('Unread in Email'); } - else if (hm_list_path() == 'feeds') { + else if (getListPathParam() == 'feeds') { count = $('tr .unseen', tbody).length; document.title = count+' '+hm_trans('Unread in Feeds'); } @@ -2184,7 +2184,7 @@ if(tableBody && !hm_mobile()) { (res) =>{ for (const index in res.move_count) { $('.'+Hm_Utils.clean_selector(res.move_count[index])).remove(); - select_imap_folder(hm_list_path()); + select_imap_folder(getListPathParam()); } } ); diff --git a/modules/feeds/site.js b/modules/feeds/site.js index d39ae8c0ab..0fb93043f6 100644 --- a/modules/feeds/site.js +++ b/modules/feeds/site.js @@ -117,10 +117,10 @@ var display_feeds_combined_inbox = function(res) { var feed_item_view = function(uid, list_path, callback) { if (!uid) { - uid = hm_msg_uid(); + uid = getMessageUidParam(); } if (!list_path) { - list_path = hm_list_path(); + list_path = getListPathParam(); } $('.msg_text_inner').html(''); Hm_Ajax.request( @@ -139,13 +139,13 @@ var display_feed_item_content = function(res) { if (!res.feed_msg_headers) { return; } - var msg_uid = hm_msg_uid(); + var msg_uid = getMessageUidParam(); $('.msg_text').html(''); $('.msg_text').append(res.feed_msg_headers); $('.msg_text').append(res.feed_msg_text); set_message_content(); document.title = $('.header_subject th').text(); - var path = hm_list_path(); + var path = getListPathParam(); if (hm_list_parent() == 'feeds') { Hm_Message_List.prev_next_links('formatted_feed_data', path+'_'+msg_uid); } @@ -170,7 +170,7 @@ var display_feed_item_content = function(res) { }; var load_feed_list = function(id) { - var cached = Hm_Utils.get_from_local_storage(hm_list_path()); + var cached = Hm_Utils.get_from_local_storage(getListPathParam()); if (cached) { $('.message_table tbody').html(cached); } diff --git a/modules/github/site.js b/modules/github/site.js index 604f1a9285..b36d5544ec 100644 --- a/modules/github/site.js +++ b/modules/github/site.js @@ -1,12 +1,12 @@ 'use strict'; var load_github_data = function(id) { - if (hm_list_path() == 'github_all') { + if (getListPathParam() == 'github_all') { Hm_Ajax.request([{'name': 'hm_ajax_hook', 'value': 'ajax_github_data'}, {'name': 'github_repo', 'value': id}], display_github_data, [], false, cache_github_all); } else { - if (hm_list_path().substr(0, 6) == 'github') { - var cached = Hm_Utils.get_from_local_storage(hm_list_path()); + if (getListPathParam().substr(0, 6) == 'github') { + var cached = Hm_Utils.get_from_local_storage(getListPathParam()); if (cached) { $('.message_table tbody').html(cached); } @@ -27,7 +27,7 @@ var display_github_data_background = function(res) { }; var display_github_data = function(res) { - var path = hm_list_path(); + var path = getListPathParam(); Hm_Message_List.update([res.github_server_id], res.formatted_message_list, 'github'); if (path != 'github_all') { var data = Hm_Message_List.filter_list(); @@ -37,17 +37,17 @@ var display_github_data = function(res) { }; var cache_github_all = function() { - if (hm_list_path() == 'github_all') { + if (getListPathParam() == 'github_all') { Hm_Message_List.set_message_list_state('formatted_github_all') } }; var github_item_view = function(list_path, uid, callback) { if (!list_path) { - list_path = hm_list_path(); + list_path = getListPathParam(); } if (!uid) { - uid = hm_msg_uid(); + uid = getMessageUidParam(); } $('.msg_text_inner').html(''); Hm_Ajax.request( @@ -64,7 +64,7 @@ var github_item_view = function(list_path, uid, callback) { var display_github_item_content = function(res) { $('.msg_text').html(res.github_msg_text); - var uid = hm_msg_uid(); + var uid = getMessageUidParam(); if (hm_list_parent() == 'unread') { Hm_Message_List.prev_next_links('formatted_unread_data', uid); } @@ -72,7 +72,7 @@ var display_github_item_content = function(res) { Hm_Message_List.prev_next_links('formatted_github_all', uid); } else { - Hm_Message_List.prev_next_links(hm_list_path(), uid); + Hm_Message_List.prev_next_links(getListPathParam(), uid); } if (Hm_Message_List.track_read_messages(uid)) { if (hm_list_parent() == 'unread') { diff --git a/modules/hello_world/js_modules/route_handlers.js b/modules/hello_world/js_modules/route_handlers.js new file mode 100644 index 0000000000..9f13a45336 --- /dev/null +++ b/modules/hello_world/js_modules/route_handlers.js @@ -0,0 +1,8 @@ +/** + * If we are on the "hello_world" page, activate the click handler + */ +function applyHelloWorldPageHandlers() { + $('.hw_ajax_link').on("click", function() { + hello_world_ajax_update(); + }); +} \ No newline at end of file diff --git a/modules/hello_world/site.js b/modules/hello_world/site.js index 7f5356ac9b..a83a6e8162 100644 --- a/modules/hello_world/site.js +++ b/modules/hello_world/site.js @@ -3,8 +3,7 @@ /** * This JS sets up an AJAX request and assigns it to a link on the hello_world page. * You have access to cash.js functions when this code is loaded, so use the standard - * way to delay actions until page onload if you need to. Built in data sources like - * hm_page_name() are defined before this is run so they are also available. When the + * way to delay actions until page onload if you need to. When the * site build process is run this code will be combined with JS from other module sets, * and optionally minified based on the config/app.php file settings. */ @@ -24,13 +23,4 @@ var hello_world_ajax_update = function() { */ var update_hello_world_display = function(res) { alert(res.hello_world_ajax_result); -}; - -/** - * If we are on the "hello_world" page, activate the click handler - */ -if (hm_page_name() == 'hello_world') { - $('.hw_ajax_link').on("click", function() { - hello_world_ajax_update(); - }); -} +}; \ No newline at end of file diff --git a/modules/imap/js_modules/route_handlers.js b/modules/imap/js_modules/route_handlers.js index c21a3f36df..c65cde3a45 100644 --- a/modules/imap/js_modules/route_handlers.js +++ b/modules/imap/js_modules/route_handlers.js @@ -7,6 +7,8 @@ function applyImapMessageListPageHandlers(routeParams) { imap_setup_tags(); if (window.githubMessageListPageHandler) githubMessageListPageHandler(routeParams); + if (window.inlineMessageMessageListAndSearchPageHandler) inlineMessageMessageListAndSearchPageHandler(routeParams); + if (window.wpMessageListPageHandler) wpMessageListPageHandler(routeParams); return async function() { const refreshIntervalId = await refreshInterval; @@ -21,4 +23,6 @@ function applyImapMessageContentPageHandlers(routeParams) { if (window.feedMessageContentPageHandler) feedMessageContentPageHandler(routeParams); if (window.githubMessageContentPageHandler) githubMessageContentPageHandler(routeParams); + if (window.pgpMessageContentPageHandler) pgpMessageContentPageHandler(); + if (window.wpMessageContentPageHandler) wpMessageContentPageHandler(routeParams); } \ No newline at end of file diff --git a/modules/imap/site.js b/modules/imap/site.js index 8e73ef398f..eded0268e3 100644 --- a/modules/imap/site.js +++ b/modules/imap/site.js @@ -134,10 +134,10 @@ var imap_setup_server_page = function() { var set_message_content = function(path, msg_uid) { if (!path) { - path = hm_list_path(); + path = getListPathParam(); } if (!msg_uid) { - msg_uid = hm_msg_uid(); + msg_uid = getMessageUidParam(); } var key = msg_uid+'_'+path; Hm_Utils.save_to_local_storage(key, $('.msg_text').html()); @@ -147,8 +147,8 @@ var imap_delete_message = function(state, supplied_uid, supplied_detail) { if (!hm_delete_prompt()) { return false; } - var uid = hm_msg_uid(); - var detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); + var uid = getMessageUidParam(); + var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); if (supplied_uid) { uid = supplied_uid; } @@ -166,7 +166,7 @@ var imap_delete_message = function(state, supplied_uid, supplied_detail) { if (Hm_Utils.get_from_global('msg_uid', false)) { return; } - var msg_cache_key = 'imap_'+detail.server_id+'_'+hm_msg_uid()+'_'+detail.folder; + var msg_cache_key = 'imap_'+detail.server_id+'_'+getMessageUidParam()+'_'+detail.folder; remove_from_cached_imap_pages(msg_cache_key); var nlink = $('.nlink'); if (nlink.length && Hm_Utils.get_from_global('auto_advance_email_enabled')) { @@ -174,7 +174,7 @@ var imap_delete_message = function(state, supplied_uid, supplied_detail) { } else { if (!hm_list_parent()) { - Hm_Utils.redirect("?page=message_list&list_path="+hm_list_path()); + Hm_Utils.redirect("?page=message_list&list_path="+getListPathParam()); } else { Hm_Utils.redirect("?page=message_list&list_path="+hm_list_parent()); @@ -188,8 +188,8 @@ var imap_delete_message = function(state, supplied_uid, supplied_detail) { }; var imap_unread_message = function(supplied_uid, supplied_detail) { - var uid = hm_msg_uid(); - var detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); + var uid = getMessageUidParam(); + var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); if (supplied_uid) { uid = supplied_uid; } @@ -212,7 +212,7 @@ var imap_unread_message = function(supplied_uid, supplied_detail) { } else { if (!hm_list_parent()) { - Hm_Utils.redirect("?page=message_list&list_path="+hm_list_path()); + Hm_Utils.redirect("?page=message_list&list_path="+getListPathParam()); } else { Hm_Utils.redirect("?page=message_list&list_path="+hm_list_parent()); @@ -231,8 +231,8 @@ var imap_unread_message = function(supplied_uid, supplied_detail) { } var imap_flag_message = function(state, supplied_uid, supplied_detail) { - var uid = hm_msg_uid(); - var detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); + var uid = getMessageUidParam(); + var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); if (supplied_uid) { uid = supplied_uid; } @@ -325,8 +325,8 @@ var add_auto_folder = function(folder) { }; var cache_folder_data = function() { - if (['sent', 'drafts', 'junk', 'trash','tag'].includes(hm_list_path())) { - Hm_Message_List.set_message_list_state('formatted_'+hm_list_path()+'_data'); + if (['sent', 'drafts', 'junk', 'trash','tag'].includes(getListPathParam())) { + Hm_Message_List.set_message_list_state('formatted_'+getListPathParam()+'_data'); } }; @@ -365,11 +365,11 @@ var update_imap_combined_source = function(path, state, event) { }; var remove_imap_combined_source = function(event) { - return update_imap_combined_source(hm_list_path(), 0, event); + return update_imap_combined_source(getListPathParam(), 0, event); }; var add_imap_combined_source = function(event) { - return update_imap_combined_source(hm_list_path(), 1, event); + return update_imap_combined_source(getListPathParam(), 1, event); }; var imap_combined_unread_content = function(id, folder) { @@ -393,7 +393,7 @@ var imap_tag_content = function(id, folder) { }; var cache_imap_page = function() { - var key = 'imap_'+Hm_Utils.get_url_page_number()+'_'+hm_list_path(); + var key = 'imap_'+Hm_Utils.get_url_page_number()+'_'+getListPathParam(); var data = Hm_Message_List.filter_list(); data.find('input[type=checkbox]').removeAttr('checked'); Hm_Utils.save_to_local_storage(key, data.html()); @@ -407,14 +407,14 @@ var clear_imap_page_combined_inbox = function() { } var fetch_cached_imap_page = function() { - var key = 'imap_'+Hm_Utils.get_url_page_number()+'_'+hm_list_path(); + var key = 'imap_'+Hm_Utils.get_url_page_number()+'_'+getListPathParam(); var page = Hm_Utils.get_from_local_storage(key); var links = Hm_Utils.get_from_local_storage(key+'_page_links'); return [ page, links ]; } var remove_from_cached_imap_pages = function(msg_cache_key) { - var keys = ['imap_'+Hm_Utils.get_url_page_number()+'_'+hm_list_path()]; + var keys = ['imap_'+Hm_Utils.get_url_page_number()+'_'+getListPathParam()]; if (hm_list_parent()) { keys.push('imap_'+Hm_Utils.get_url_page_number()+'_'+hm_list_parent()); if (['combined_inbox', 'unread', 'flagged', 'advanced_search', 'search', 'sent'].includes(hm_list_parent())) { @@ -698,7 +698,7 @@ var imap_mark_as_read = function(uid, detail) { uid = $('.msg_uid').val(); } if (!detail) { - detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); + detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); } if (detail && uid) { Hm_Ajax.request( @@ -791,7 +791,7 @@ var imap_message_view_finished = function(msg_uid, detail, skip_links) { Hm_Message_List.prev_next_links('formatted_tag_data', class_name); } else { - var key = 'imap_'+Hm_Utils.get_url_page_number()+'_'+hm_list_path(); + var key = 'imap_'+Hm_Utils.get_url_page_number()+'_'+getListPathParam(); Hm_Message_List.prev_next_links(key, class_name); } } @@ -829,8 +829,8 @@ var imap_message_view_finished = function(msg_uid, detail, skip_links) { }); $('#show_message_source').on("click", function(e) { e.preventDefault(); - const detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); - window.open(`?page=message_source&imap_msg_uid=${hm_msg_uid()}&imap_server_id=${detail.server_id}&imap_folder=${detail.folder}`); + const detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); + window.open(`?page=message_source&imap_msg_uid=${getMessageUidParam()}&imap_server_id=${detail.server_id}&imap_folder=${detail.folder}`); }); $(document).on('click', '#unblock_sender', function(e) { e.preventDefault(); @@ -1005,8 +1005,8 @@ var imap_perform_move_copy = function(dest_id, context, action = null) { globals['inline_move_uuid'] = false; } else if (page == 'message') { - var uid = hm_msg_uid(); - var path = Hm_Utils.parse_folder_path(hm_list_path()); + var uid = getMessageUidParam(); + var path = Hm_Utils.parse_folder_path(getListPathParam()); ids.push('imap_'+path['server_id']+'_'+uid+'_'+path['folder']); } } @@ -1146,7 +1146,7 @@ var imap_setup_tags = function() { var ids = []; if (getPageNameParam() == 'message') { var list_path = getListPathParam().split('_'); - ids.push(list_path[1]+'_'+hm_msg_uid()+'_'+list_path[2]); + ids.push(list_path[1]+'_'+getMessageUidParam()+'_'+list_path[2]); } else { $('input[type=checkbox]').each(function() { if (this.checked && this.id.search('imap') != -1) { @@ -1167,7 +1167,7 @@ var imap_setup_tags = function() { if (!res.router_user_msgs[0].startsWith('ERR')) { Hm_Utils.search_from_local_storage("/^\d+_imap_.+/")?.forEach((source) => Hm_Utils.save_to_local_storage(source, 1)); Hm_Folders.reload_folders(true); - var path = hm_list_parent()? hm_list_parent(): hm_list_path(); + var path = hm_list_parent()? hm_list_parent(): getListPathParam(); window.location.replace('?page=message_list&list_path='+path); } } @@ -1222,7 +1222,7 @@ var imap_setup_snooze = function() { function(res) { if (res.snoozed_messages > 0) { Hm_Folders.reload_folders(true); - var path = hm_list_parent()? hm_list_parent(): hm_list_path(); + var path = hm_list_parent()? hm_list_parent(): getListPathParam(); window.location.replace('?page=message_list&list_path='+path); } } @@ -1237,7 +1237,7 @@ var imap_unsnooze_messages = function() { ); } -if (hm_list_path() == 'sent') { +if (getListPathParam() == 'sent') { Hm_Message_List.page_caches.sent = 'formatted_sent_data'; } @@ -1283,7 +1283,7 @@ $(function() { $('.imap_move').on("click", function() { return false; }); } - if (hm_list_path() !== 'unread') { + if (getListPathParam() !== 'unread') { if (typeof hm_data_sources_background === 'function') { globals.Hm_Background_Unread = new Message_List(); globals.Hm_Background_Unread.background = true; @@ -1297,8 +1297,8 @@ $(function() { var imap_archive_message = function(state, supplied_uid, supplied_detail) { - var uid = hm_msg_uid(); - var detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); + var uid = getMessageUidParam(); + var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); if (supplied_uid) { uid = supplied_uid; } @@ -1322,7 +1322,7 @@ var imap_archive_message = function(state, supplied_uid, supplied_detail) { } else { if (!hm_list_parent()) { - Hm_Utils.redirect("?page=message_list&list_path="+hm_list_path()); + Hm_Utils.redirect("?page=message_list&list_path="+getListPathParam()); } else { Hm_Utils.redirect("?page=message_list&list_path="+hm_list_parent()); @@ -1354,7 +1354,7 @@ const handleDownloadMsgSource = function() { const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; - const subject = messageSource.textContent.match(/Subject: (.*)/)?.[1] || hm_msg_uid(); // Let's use the message UID if the subject is empty + const subject = messageSource.textContent.match(/Subject: (.*)/)?.[1] || getMessageUidParam(); // Let's use the message UID if the subject is empty a.download = subject + '.eml'; document.body.appendChild(a); a.click(); @@ -1380,7 +1380,7 @@ var imap_screen_email = function() { imap_perform_move_copy("Screen email", "list", 'screen_mail'); } list_msg_uid.forEach(function(msg_uid) { - block_unblock_sender(msg_uid, Hm_Utils.parse_folder_path(hm_list_path()), 'sender', 'blocked'); + block_unblock_sender(msg_uid, Hm_Utils.parse_folder_path(getListPathParam()), 'sender', 'blocked'); }) }; diff --git a/modules/inline_message/site.js b/modules/inline_message/site.js index 3ada138dd5..682034816f 100644 --- a/modules/inline_message/site.js +++ b/modules/inline_message/site.js @@ -158,24 +158,22 @@ var capture_subject_click = function() { }); }; -$(function() { - if (hm_page_name() == 'message_list' || hm_page_name() == 'search') { - if (inline_msg()) { - setTimeout(capture_subject_click, 100); - $('tr').removeClass('hl'); - Hm_Ajax.add_callback_hook('*', capture_subject_click); - Hm_Ajax.add_callback_hook('ajax_imap_delete_message', msg_inline_close); - Hm_Ajax.add_callback_hook('ajax_imap_move_copy_action', msg_inline_close); - Hm_Ajax.add_callback_hook('ajax_imap_archive_message', msg_inline_close); - if (hm_list_path().substr(0, 4) !== 'imap') { - Hm_Ajax.add_callback_hook('ajax_imap_unread', msg_inline_close); - } - if (hm_list_path().substr(0, 4) === 'imap') { - Hm_Ajax.add_callback_hook('ajax_imap_folder_display', capture_subject_click); - } +function inlineMessageMessageListAndSearchPageHandler(routeParams) { + if (window.inline_msg && inline_msg()) { + setTimeout(capture_subject_click, 100); + $('tr').removeClass('hl'); + Hm_Ajax.add_callback_hook('*', capture_subject_click); + Hm_Ajax.add_callback_hook('ajax_imap_delete_message', msg_inline_close); + Hm_Ajax.add_callback_hook('ajax_imap_move_copy_action', msg_inline_close); + Hm_Ajax.add_callback_hook('ajax_imap_archive_message', msg_inline_close); + if (routeParams.list_path?.substr(0, 4) !== 'imap') { + Hm_Ajax.add_callback_hook('ajax_imap_unread', msg_inline_close); + } + if (routeParams.list_path?.substr(0, 4) === 'imap') { + Hm_Ajax.add_callback_hook('ajax_imap_folder_display', capture_subject_click); } } -}); +} const messagesListMutation = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { diff --git a/modules/keyboard_shortcuts/js_modules/route_handlers.js b/modules/keyboard_shortcuts/js_modules/route_handlers.js new file mode 100644 index 0000000000..bd3928aef4 --- /dev/null +++ b/modules/keyboard_shortcuts/js_modules/route_handlers.js @@ -0,0 +1,5 @@ +function applyShortcutsPageHandlers() { + $('.reset_shortcut').on("click", function() { + Hm_Utils.redirect('?page=shortcuts'); + }); +} diff --git a/modules/keyboard_shortcuts/site.js b/modules/keyboard_shortcuts/site.js index 83517ec503..cf1ed432f0 100644 --- a/modules/keyboard_shortcuts/site.js +++ b/modules/keyboard_shortcuts/site.js @@ -87,7 +87,7 @@ var Keyboard_Shortcuts = { var control_keys = {'alt': e.altKey, 'shift': e.shiftKey, 'meta': e.metaKey, 'control': e.ctrlKey}; for (index in shortcuts) { combo = shortcuts[index]; - if (combo['page'] != '*' && combo['page'] != hm_page_name()) { + if (combo['page'] != '*' && combo['page'] != getPageNameParam()) { continue; } if (e.keyCode != combo['char']) { @@ -157,9 +157,4 @@ $(function() { if (typeof shortcuts != 'undefined') { $(document).on('keydown', ':not(input)', function(e) { return Keyboard_Shortcuts.check(e, shortcuts); }); } - if (hm_page_name() == 'shortcuts') { - $('.reset_shortcut').on("click", function() { - Hm_Utils.redirect('?page=shortcuts'); - }); - } }); diff --git a/modules/nasa/site.js b/modules/nasa/site.js index 17aa7cb9ac..e25b8234eb 100644 --- a/modules/nasa/site.js +++ b/modules/nasa/site.js @@ -41,7 +41,7 @@ var nasa_disconnect_result = function(res) { Hm_Folders.reload_folders(true); }; -if (hm_page_name() == 'servers') { +function nasaServersPageHandler() { $('.nasa_api_connect').on("click", nasa_connect); $('.nasa_api_disconnect').on("click", nasa_disconnect); } diff --git a/modules/nux/site.js b/modules/nux/site.js index 5900a4df4c..d7dbdef53b 100644 --- a/modules/nux/site.js +++ b/modules/nux/site.js @@ -120,15 +120,3 @@ var expand_server_settings = function() { var add_extra_fields = function(select, id, label, placeholder) { $(select).parent().after('
'); }; - -$(function() { - if (hm_page_name() === 'message_list') { - var list_path = hm_list_path(); - if (list_path === 'unread' || list_path === 'combined_inbox' || list_path === 'flagged') { - var data_sources = hm_data_sources(); - if (data_sources.length === 0) { - $('.nux_empty_combined_view').show(); - } - } - } -}); diff --git a/modules/pgp/js_modules/route_handlers.js b/modules/pgp/js_modules/route_handlers.js new file mode 100644 index 0000000000..411d64464b --- /dev/null +++ b/modules/pgp/js_modules/route_handlers.js @@ -0,0 +1,13 @@ +function applyPgpPageHandlers() { + $('.priv_title').on("click", function() { $('.priv_keys').toggle(); }); + $('.public_title').on("click", function() { $('.public_keys').toggle(); }); + $('.delete_pgp_key').on("click", function() { return hm_delete_prompt(); }); + $('#priv_key').on("change", function(evt) { Hm_Pgp.read_private_key(evt); }); + Hm_Pgp.list_private_keys(); + if (window.location.hash == '#public_keys') { + $('.public_keys').toggle(); + } + if (window.location.hash == '#private_keys') { + $('.private_keys').toggle(); + } +} \ No newline at end of file diff --git a/modules/pgp/site.js b/modules/pgp/site.js index 96847f5c38..d03688fd67 100644 --- a/modules/pgp/site.js +++ b/modules/pgp/site.js @@ -306,28 +306,13 @@ var Hm_Pgp = { } } -$(function() { - if (hm_page_name() == 'compose') { - if (($('#pgp_encrypt option').length + $('#pgp_sign option').length) == 0) { - $('.pgp_section').hide(); - } - $('.smtp_send_placeholder').on("click", function() { Hm_Pgp.process_settings(); return false; }); - } - else if (hm_page_name() == 'message') { - Hm_Ajax.add_callback_hook('ajax_imap_message_content', Hm_Pgp.check_pgp_msg); +function pgpComposePageHandler() { + if (($('#pgp_encrypt option').length + $('#pgp_sign option').length) == 0) { + $('.pgp_section').hide(); } - else if (hm_page_name() == 'pgp') { - $('.priv_title').on("click", function() { $('.priv_keys').toggle(); }); - $('.public_title').on("click", function() { $('.public_keys').toggle(); }); - $('.delete_pgp_key').on("click", function() { return hm_delete_prompt(); }); - $('#priv_key').on("change", function(evt) { Hm_Pgp.read_private_key(evt); }); - Hm_Pgp.list_private_keys(); - if (window.location.hash == '#public_keys') { - $('.public_keys').toggle(); - } - if (window.location.hash == '#private_keys') { - $('.private_keys').toggle(); - } + $('.smtp_send_placeholder').on("click", function() { Hm_Pgp.process_settings(); return false; }); +} - } -}); +function pgpMessageContentPageHandler() { + Hm_Ajax.add_callback_hook('ajax_imap_message_content', Hm_Pgp.check_pgp_msg); +} diff --git a/modules/profiles/js_modules/route_handlers.js b/modules/profiles/js_modules/route_handlers.js new file mode 100644 index 0000000000..f411e32c2f --- /dev/null +++ b/modules/profiles/js_modules/route_handlers.js @@ -0,0 +1,3 @@ +function applyProfilesPageHandler() { + $('.add_profile').on("click", function() { $('.edit_profile').show(); }); +} \ No newline at end of file diff --git a/modules/profiles/site.js b/modules/profiles/site.js index e60ab17cfd..36b2c6915b 100644 --- a/modules/profiles/site.js +++ b/modules/profiles/site.js @@ -1,23 +1,5 @@ 'use strict'; -if (hm_page_name() == 'compose') { - $('.compose_sign').on("click", function() { - var server_id = $('.compose_server').val(); - if (profile_signatures[server_id]) { - var ta = $('.ke-content', $('iframe').contents()); - if (ta.length) { - ta.html(ta.html() + profile_signatures[server_id].replace(/\n/g, '
')); - } - else { - ta = $('#compose_body'); - insert_sig(ta[0], profile_signatures[server_id]); - } - } else { - Hm_Notices.show(['ERR'+$('#sign_msg').val()]); - } - }); -} - var insert_sig = function(textarea, sig) { var tmpta = document.createElement('textarea'); tmpta.innerHTML = sig; @@ -37,8 +19,20 @@ var insert_sig = function(textarea, sig) { } }; -$(function() { - if (hm_page_name() === 'profiles') { - $('.add_profile').on("click", function() { $('.edit_profile').show(); }); - } -}); +function profilesComposePageHandler() { + $('.compose_sign').on("click", function() { + var server_id = $('.compose_server').val(); + if (profile_signatures[server_id]) { + var ta = $('.ke-content', $('iframe').contents()); + if (ta.length) { + ta.html(ta.html() + profile_signatures[server_id].replace(/\n/g, '
')); + } + else { + ta = $('#compose_body'); + insert_sig(ta[0], profile_signatures[server_id]); + } + } else { + Hm_Notices.show(['ERR'+$('#sign_msg').val()]); + } + }); +} diff --git a/modules/saved_searches/site.js b/modules/saved_searches/site.js index d2a323f295..e7de25d734 100644 --- a/modules/saved_searches/site.js +++ b/modules/saved_searches/site.js @@ -93,7 +93,7 @@ var update_save_search_label_results = function(res) { } } -if (hm_page_name() == 'search') { +function savedSearchesSearchPageHandler() { $('.save_search').on("click", save_search); $('.update_search').on("click", update_search); $('.delete_search').on("click", delete_search); diff --git a/modules/sievefilters/js_modules/route_handlers.js b/modules/sievefilters/js_modules/route_handlers.js new file mode 100644 index 0000000000..970e04dc64 --- /dev/null +++ b/modules/sievefilters/js_modules/route_handlers.js @@ -0,0 +1,7 @@ +function applyBlockListPageHandlers() { + blockListPageHandlers(); +} + +function applySieveFiltersPageHandler() { + sieveFiltersPageHandler(); +} \ No newline at end of file diff --git a/modules/sievefilters/site.js b/modules/sievefilters/site.js index 49e049ffb2..fc3e7b2a8f 100644 --- a/modules/sievefilters/site.js +++ b/modules/sievefilters/site.js @@ -162,805 +162,1462 @@ var hm_sieve_possible_actions = function() { ]; }; -$(function () { - - let is_editing_script = false; - let current_editing_script_name = ''; - let is_editing_filter = false; - let current_editing_filter_name = ''; - let current_account; - - $(document).on('change', '#block_action', function(e) { - if ($(this).val() == 'reject_with_message') { - $('
').insertAfter($(this)); +function blockListPageHandlers() { + $(document).on('change', '.select_default_behaviour', function(e) { + if ($(this).val() != 'Reject') { + $(this).closest('.filter_subblock') + .find('.select_default_reject_message') + .remove(); } else { - $('#reject_message').remove(); + $('').insertAfter($(this)); } }); + $(document).on('click', '.submit_default_behavior', function(e) { + let parent = $(this).closest('.filter_subblock'); + let elem = parent.find('.select_default_behaviour'); + let submit = $(this); + + const payload = [ + {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_change_behaviour'}, + {'name': 'selected_behaviour', 'value': elem.val()}, + {'name': 'imap_server_id', 'value': elem.attr('imap_account')} + ]; + if (elem.val() == 'Reject') { + const reject = parent.find('.select_default_reject_message'); + payload.push({'name': 'reject_message', 'value': reject.val()}); + } - if (hm_page_name() === 'block_list') { - $(document).on('change', '.select_default_behaviour', function(e) { - if ($(this).val() != 'Reject') { - $(this).closest('.filter_subblock') - .find('.select_default_reject_message') - .remove(); - } else { - $('').insertAfter($(this)); + submit.attr('disabled', 1); + Hm_Ajax.request( + payload, + function(res) { + submit.removeAttr('disabled'); } - }); - $(document).on('click', '.submit_default_behavior', function(e) { - let parent = $(this).closest('.filter_subblock'); - let elem = parent.find('.select_default_behaviour'); - let submit = $(this); - - const payload = [ - {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_change_behaviour'}, - {'name': 'selected_behaviour', 'value': elem.val()}, - {'name': 'imap_server_id', 'value': elem.attr('imap_account')} - ]; - if (elem.val() == 'Reject') { - const reject = parent.find('.select_default_reject_message'); - payload.push({'name': 'reject_message', 'value': reject.val()}); - } - - submit.attr('disabled', 1); - Hm_Ajax.request( - payload, - function(res) { - submit.removeAttr('disabled'); - } - ); - }); + ); + }); - $(document).on('click', '.unblock_button', function(e) { - e.preventDefault(); - if (!confirm(hm_trans('Do you want to unblock sender?'))) { - return; + $(document).on('click', '.unblock_button', function(e) { + e.preventDefault(); + if (!confirm(hm_trans('Do you want to unblock sender?'))) { + return; + } + let sender = $(this).parent().parent().children().html(); + let elem = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_unblock_sender'}, + {'name': 'imap_server_id', 'value': $(this).attr('mailbox_id')}, + {'name': 'sender', 'value': sender} + ], + function(res) { + elem.parent().parent().remove(); + var num_filters = $("#filter_num_" + elem.attr('mailbox_id')).html(); + num_filters = parseInt(num_filters) - 1; + $("#filter_num_" + elem.attr('mailbox_id')).html(num_filters); } - let sender = $(this).parent().parent().children().html(); - let elem = $(this); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_unblock_sender'}, - {'name': 'imap_server_id', 'value': $(this).attr('mailbox_id')}, - {'name': 'sender', 'value': sender} - ], - function(res) { - elem.parent().parent().remove(); - var num_filters = $("#filter_num_" + elem.attr('mailbox_id')).html(); - num_filters = parseInt(num_filters) - 1; - $("#filter_num_" + elem.attr('mailbox_id')).html(num_filters); - } - ); - }); + ); + }); - $(document).on('click', '.edit_blocked_behavior', function(e) { - e.preventDefault(); - let parent = $(this).closest('tr'); - let elem = parent.find('.block_action'); - let sender = $(this).closest('tr').children().first().html(); - let scope = sender.startsWith('*@') ? 'domain': 'sender'; - - Hm_Ajax.request( - [ - {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_unblock'}, - {'name': 'imap_server_id', 'value': $(this).attr('mailbox_id')}, - {'name': 'block_action', 'value': elem.val()}, - {'name': 'scope', 'value': scope}, - {'name': 'sender', 'value': sender}, - {'name': 'reject_message', 'value': $('#reject_message_textarea').val() ?? ''}, - {'name': 'change_behavior', 'value': true} - ], - function(res) { - if (/^(Sender|Domain) Behavior Changed$/.test(res.router_user_msgs[0])) { - window.location = window.location; - } + $(document).on('click', '.edit_blocked_behavior', function(e) { + e.preventDefault(); + let parent = $(this).closest('tr'); + let elem = parent.find('.block_action'); + let sender = $(this).closest('tr').children().first().html(); + let scope = sender.startsWith('*@') ? 'domain': 'sender'; + + Hm_Ajax.request( + [ + {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_unblock'}, + {'name': 'imap_server_id', 'value': $(this).attr('mailbox_id')}, + {'name': 'block_action', 'value': elem.val()}, + {'name': 'scope', 'value': scope}, + {'name': 'sender', 'value': sender}, + {'name': 'reject_message', 'value': $('#reject_message_textarea').val() ?? ''}, + {'name': 'change_behavior', 'value': true} + ], + function(res) { + if (/^(Sender|Domain) Behavior Changed$/.test(res.router_user_msgs[0])) { + window.location = window.location; } - ); - }); + } + ); + }); - $(document).on('click', '.toggle-behavior-dropdown', function(e) { - e.preventDefault(); - var default_val = $(this).data('action'); - $('#block_sender_form').trigger('reset'); - $('#reject_message').remove(); - $('#block_action').val(default_val).trigger('change'); - $('#edit_blocked_behavior').attr('data-mailbox-id', $(this).attr('mailbox_id')); - if (default_val == 'reject_with_message') { - $('#reject_message_textarea').val($(this).data('reject-message')); + $(document).on('click', '.toggle-behavior-dropdown', function(e) { + e.preventDefault(); + var default_val = $(this).data('action'); + $('#block_sender_form').trigger('reset'); + $('#reject_message').remove(); + $('#block_action').val(default_val).trigger('change'); + $('#edit_blocked_behavior').attr('data-mailbox-id', $(this).attr('mailbox_id')); + if (default_val == 'reject_with_message') { + $('#reject_message_textarea').val($(this).data('reject-message')); + } + }); + + $(document).on('click', '.block_domain_button', function(e) { + e.preventDefault(); + let sender = $(this).parent().parent().children().html(); + let elem = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_domain'}, + {'name': 'imap_server_id', 'value': $(this).attr('data-mailbox_id')}, + {'name': 'sender', 'value': sender} + ], + function(res) { + window.location = window.location; + } + ); + }); + + $(document).on('click', '.edit_email_behavior_submit', function(e) { + e.preventDefault(); + let sender = $(this).parent().parent().children().html(); + let elem = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_domain'}, + {'name': 'imap_server_id', 'value': $(this).attr('mailbox_id')}, + {'name': 'sender', 'value': sender} + ], + function(res) { + window.location = window.location; + } + ); + }); + + $('.sievefilters_accounts_title').on("click", function () { + $(this).parent().find('.sievefilters_accounts').toggleClass('d-none'); + }); +} + +if (hm_page_name() === 'sieve_filters') { + /************************************************************************************** + * BOOTSTRAP SCRIPT MODAL + **************************************************************************************/ + var edit_script_modal = new Hm_Modal({ + size: 'xl', + modalId: 'myEditScript' + }); + + // set content + edit_script_modal.setContent(document.querySelector('#edit_script_modal').innerHTML); + $('#edit_script_modal').remove(); + + // add a button + edit_script_modal.addFooterBtn('Save', 'btn-primary', async function () { + save_script(current_account); + }); + + + /************************************************************************************** + * BOOTSTRAP SIEVE FILTER MODAL + **************************************************************************************/ + var edit_filter_modal = new Hm_Modal({ + size: 'xl', + modalId: 'myEditFilterModal', + }); + + // set content + edit_filter_modal.setContent(document.querySelector('#edit_filter_modal').innerHTML); + $('#edit_filter_modal').remove(); + + // add a button + edit_filter_modal.addFooterBtn('Save', 'btn-primary ms-auto', async function () { + let result = save_filter(current_account); + if (result) { + edit_filter_modal.hide(); + } + }); + + // add another button + edit_filter_modal.addFooterBtn('Convert to code', 'btn-warning', async function () { + let result = save_filter(current_account, true); + if (result) { + edit_filter_modal.hide(); + } + }); + + + function ordinal_number(n) + { + let ord = 'th'; + + if (n % 10 == 1 && n % 100 != 11) { + ord = 'st'; + } else if (n % 10 == 2 && n % 100 != 12) { + ord = 'nd'; + } else if (n % 10 == 3 && n % 100 != 13) { + ord = 'rd'; + } + + return n + ord; + } + + /************************************************************************************** + * FUNCTIONS + **************************************************************************************/ + function save_filter(imap_account, gen_script = false) { + let validation_failed = false + let conditions_parsed = [] + let actions_parsed = [] + let conditions = $('select[name^=sieve_selected_conditions_field]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let conditions_type = $('select[name^=sieve_selected_conditions_options]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let conditions_value = $('input[name^=sieve_selected_option_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let conditions_extra_value = $('input[name^=sieve_selected_extra_option_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let idx = 0; + if (conditions.length === 0) { + Hm_Utils.add_sys_message(hm_trans('You must provide at least one condition'), 'danger'); + return false; + } + + Hm_Utils.clear_sys_messages(); + 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 ? '
': ''; + Hm_Utils.add_sys_message('The ' + order + ' condition (' + elem + ') must be provided', 'danger'); + validation_failed = true; } + conditions_parsed.push( + { + 'condition': elem, + 'type': conditions_type[idx], + 'extra_option': conditions[idx].extra_option, + 'extra_option_value': conditions_extra_value[idx], + 'value': conditions_value[idx] + } + ) + idx = idx + 1; }); - $(document).on('click', '.block_domain_button', function(e) { - e.preventDefault(); - let sender = $(this).parent().parent().children().html(); - let elem = $(this); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_domain'}, - {'name': 'imap_server_id', 'value': $(this).attr('data-mailbox_id')}, - {'name': 'sender', 'value': sender} - ], - function(res) { - window.location = window.location; + let actions_type = $('select[name^=sieve_selected_actions]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + let actions_value = $('[name^=sieve_selected_action_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + let actions_field_type = $('[name^=sieve_selected_action_value]').map(function(idx, elem) { + return $(elem).attr('type'); + }).get(); + let actions_extra_value = $('input[name^=sieve_selected_extra_action_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + if (actions_type.length === 0) { + Hm_Utils.add_sys_message(hm_trans('You must provide at least one action'), 'danger'); + return false; + } + + idx = 0; + actions_type.forEach(function (elem, key) { + console.log(actions_field_type[idx]) + if (actions_value[idx] === "" && actions_field_type[idx] !== 'hidden') { + let order = ordinal_number(key + 1); + let previous_messages = $('.sys_messages').html(); + previous_messages += previous_messages ? '
': ''; + Hm_Utils.add_sys_message('The ' + order + ' action (' + elem + ') must be provided', 'danger'); + validation_failed = true; + } + actions_parsed.push( + { + 'action': elem, + 'value': actions_value[idx], + 'extra_option': actions_type[idx].extra_option, + 'extra_option_value': actions_extra_value[idx], } - ); + ) + idx = idx + 1; }); - $(document).on('click', '.edit_email_behavior_submit', function(e) { - e.preventDefault(); - let sender = $(this).parent().parent().children().html(); - let elem = $(this); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_block_domain'}, - {'name': 'imap_server_id', 'value': $(this).attr('mailbox_id')}, - {'name': 'sender', 'value': sender} - ], - function(res) { + if ($('#stop_filtering').is(':checked')) { + actions_parsed.push( + { + 'action': "stop", + 'value': "", + 'extra_option': "", + 'extra_option_value': "", + } + ) + } + if ($('.modal_sieve_filter_name').val() == "") { + Hm_Utils.add_sys_message(hm_trans('Filter name is required'), 'danger'); + return false; + } + + if (validation_failed) { + return false; + } + + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_save_filter'}, + {'name': 'imap_account', 'value': imap_account}, + {'name': 'sieve_filter_name', 'value': $('.modal_sieve_filter_name').val()}, + {'name': 'sieve_filter_priority', 'value': $('.modal_sieve_filter_priority').val()}, + {'name': 'is_editing_filter', 'value': is_editing_filter}, + {'name': 'current_editing_filter_name', 'value': current_editing_filter_name}, + {'name': 'conditions_json', 'value': JSON.stringify(conditions_parsed)}, + {'name': 'actions_json', 'value': JSON.stringify(actions_parsed)}, + {'name': 'filter_test_type', 'value': $('.modal_sieve_filter_test').val()}, + {'name': 'gen_script', 'value': gen_script}, + ], + function(res) { + if (Object.keys(res.script_details).length === 0) { window.location = window.location; + } else { + edit_script_modal.open(); + $('.modal_sieve_script_textarea').val(res.script_details.gen_script); + $('.modal_sieve_script_name').val(res.script_details.filter_name); + $('.modal_sieve_script_priority').val(res.script_details.filter_priority); } - ); - }); + } + ); - $('.sievefilters_accounts_title').on("click", function () { - $(this).parent().find('.sievefilters_accounts').toggleClass('d-none'); - }); + return true; } - if (hm_page_name() === 'sieve_filters') { - /************************************************************************************** - * BOOTSTRAP SCRIPT MODAL - **************************************************************************************/ - var edit_script_modal = new Hm_Modal({ - size: 'xl', - modalId: 'myEditScript' - }); + function save_script(imap_account) { + if ($('.modal_sieve_script_name').val() === "") { + Hm_Utils.add_sys_message(hm_trans('You must provide a name for your script'), 'danger'); + return false; + } + if ($('.modal_sieve_script_textarea').val() === "") { + Hm_Utils.add_sys_message(hm_trans('Empty script'), 'danger'); + return false; + } + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_save_script'}, + {'name': 'imap_account', 'value': imap_account}, + {'name': 'sieve_script_name', 'value': $('.modal_sieve_script_name').val()}, + {'name': 'sieve_script_priority', 'value': $('.modal_sieve_script_priority').val()}, + {'name': 'is_editing_script', 'value': is_editing_script}, + {'name': 'current_editing_script', 'value': current_editing_script_name}, + {'name': 'script', 'value': $('.modal_sieve_script_textarea').val()}], + function(res) { + window.location = window.location; + } + ); + } - // set content - edit_script_modal.setContent(document.querySelector('#edit_script_modal').innerHTML); - $('#edit_script_modal').remove(); + /************************************************************************************** + * MODAL EVENTS + **************************************************************************************/ + $('.sievefilters_accounts_title').on("click", function () { + $(this).parent().find('.sievefilters_accounts').toggleClass('d-none'); + }); + $('.add_filter').on('click', function () { + edit_filter_modal.setTitle('Add Filter'); + $('.modal_sieve_filter_priority').val(''); + $('.modal_sieve_filter_test').val('ALLOF'); + $('#stop_filtering').prop('checked', false); + current_account = $(this).attr('account'); + edit_filter_modal.open(); + + // Reset the form fields when opening the modal + $(".modal_sieve_filter_name").val(''); + $(".modal_sieve_script_priority").val(''); + $(".sieve_list_conditions_modal").empty(); + $(".filter_actions_modal_table").empty(); + }); + $('.add_script').on('click', function () { + edit_script_modal.setTitle('Add Script'); + $('.modal_sieve_script_textarea').val(''); + $('.modal_sieve_script_name').val(''); + $('.modal_sieve_script_priority').val(''); + is_editing_script = false; + current_editing_script_name = ''; + current_account = $(this).attr('account'); + edit_script_modal.open(); + }); + $('.edit_filter').on('click', function (e) { + e.preventDefault(); + let script_name = $(this).parent().parent().children().next().html(); + edit_filter_modal.setTitle(script_name); + edit_filter_modal.open(); + }); - // add a button - edit_script_modal.addFooterBtn('Save', 'btn-primary', async function () { - save_script(current_account); - }); + /** + * Delete action Button + */ + $(document).on('click', '.delete_else_action_modal_button', function (e) { + e.preventDefault(); + $(this).parent().parent().remove(); + }); + /** + * Delete action Button + */ + $(document).on('click', '.delete_action_modal_button', function (e) { + e.preventDefault(); + $(this).parent().parent().remove(); + }); - /************************************************************************************** - * BOOTSTRAP SIEVE FILTER MODAL - **************************************************************************************/ - var edit_filter_modal = new Hm_Modal({ - size: 'xl', - modalId: 'myEditFilterModal', - }); + /** + * Delete Condition Button + */ + $(document).on('click', '.delete_condition_modal_button', function (e) { + e.preventDefault(); + $(this).parent().parent().remove(); + }); - // set content - edit_filter_modal.setContent(document.querySelector('#edit_filter_modal').innerHTML); - $('#edit_filter_modal').remove(); + function add_filter_condition() { + let header_fields = ''; + let message_fields = ''; - // add a button - edit_filter_modal.addFooterBtn('Save', 'btn-primary ms-auto', async function () { - let result = save_filter(current_account); - if (result) { - edit_filter_modal.hide(); + hm_sieve_condition_fields().Message.forEach(function (value) { + if (value.selected === true) { + message_fields += ''; + } else { + message_fields += ''; } }); + hm_sieve_condition_fields().Header.forEach(function (value) { + if (value.selected === true) { + header_fields += ''; + } else { + header_fields += ''; + } + }); + let extra_options = ''; + $('.sieve_list_conditions_modal').append( + ' ' + + ' ' + + ' ' + + ' ' + + extra_options + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' Delete' + + ' ' + + ' ' + ); + } + + /** + * Add Condition Button + */ + $(document).on('click', '.sieve_add_condition_modal_button', function () { + add_filter_condition(); + }); + + function add_filter_action(default_value = '') { + let possible_actions_html = ''; - // add another button - edit_filter_modal.addFooterBtn('Convert to code', 'btn-warning', async function () { - let result = save_filter(current_account, true); - if (result) { - edit_filter_modal.hide(); + hm_sieve_possible_actions().forEach(function (value) { + if (value.selected === true) { + possible_actions_html += ''; + return; } + possible_actions_html += ''; }); + let extra_options = ''; + $('.filter_actions_modal_table').append( + '' + + ' ' + + ' ' + + ' ' + + extra_options + + ' ' + + ' ' + + ' ' + + ' ' + + ' Delete' + + ' ' + + '' + ); + } + /** + * Add Action Button + */ + $(document).on('click', '.filter_modal_add_action_btn', function () { + add_filter_action(); + }); - function ordinal_number(n) - { - let ord = 'th'; + /** + * Add Else Action Button + */ + $(document).on('click', '.filter_modal_add_else_action_btn', function () { + let possible_actions_html = ''; - if (n % 10 == 1 && n % 100 != 11) { - ord = 'st'; - } else if (n % 10 == 2 && n % 100 != 12) { - ord = 'nd'; - } else if (n % 10 == 3 && n % 100 != 13) { - ord = 'rd'; + hm_sieve_possible_actions().forEach(function (value) { + if (value.selected === true) { + possible_actions_html += ''; + return; } + possible_actions_html += ''; + }); - return n + ord; - } + $('.filter_else_actions_modal_table').append( + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' Delete' + + ' ' + + '' + ); + }); - /************************************************************************************** - * FUNCTIONS - **************************************************************************************/ - function save_filter(imap_account, gen_script = false) { - let validation_failed = false - let conditions_parsed = [] - let actions_parsed = [] - let conditions = $('select[name^=sieve_selected_conditions_field]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - - let conditions_type = $('select[name^=sieve_selected_conditions_options]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - - let conditions_value = $('input[name^=sieve_selected_option_value]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - - let conditions_extra_value = $('input[name^=sieve_selected_extra_option_value]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - - let idx = 0; - if (conditions.length === 0) { - Hm_Utils.add_sys_message(hm_trans('You must provide at least one condition'), 'danger'); - return false; - } - - Hm_Utils.clear_sys_messages(); - 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 ? '
': ''; - Hm_Utils.add_sys_message('The ' + order + ' condition (' + elem + ') must be provided', 'danger'); - validation_failed = true; - } - conditions_parsed.push( - { - 'condition': elem, - 'type': conditions_type[idx], - 'extra_option': conditions[idx].extra_option, - 'extra_option_value': conditions_extra_value[idx], - 'value': conditions_value[idx] - } - ) - idx = idx + 1; - }); - let actions_type = $('select[name^=sieve_selected_actions]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - let actions_value = $('[name^=sieve_selected_action_value]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - let actions_field_type = $('[name^=sieve_selected_action_value]').map(function(idx, elem) { - return $(elem).attr('type'); - }).get(); - let actions_extra_value = $('input[name^=sieve_selected_extra_action_value]').map(function(idx, elem) { - return $(elem).val(); - }).get(); - - if (actions_type.length === 0) { - Hm_Utils.add_sys_message(hm_trans('You must provide at least one action'), 'danger'); - return false; - } - - idx = 0; - actions_type.forEach(function (elem, key) { - console.log(actions_field_type[idx]) - if (actions_value[idx] === "" && actions_field_type[idx] !== 'hidden') { - let order = ordinal_number(key + 1); - let previous_messages = $('.sys_messages').html(); - previous_messages += previous_messages ? '
': ''; - Hm_Utils.add_sys_message('The ' + order + ' action (' + elem + ') must be provided', 'danger'); - validation_failed = true; - } - actions_parsed.push( - { - 'action': elem, - 'value': actions_value[idx], - 'extra_option': actions_type[idx].extra_option, - 'extra_option_value': actions_extra_value[idx], + /** + * Action change + */ + $(document).on('change', '.sieve_actions_select', function () { + let tr_elem = $(this).parent().parent(); + console.log(tr_elem.attr('default_value')); + let elem = $(this).parent().next().next(); + let elem_extra = $(this).parent().next().find('.condition_extra_action_value'); + let action_name = $(this).val(); + let selected_action; + hm_sieve_possible_actions().forEach(function (action) { + if (action_name === action.name) { + selected_action = action; + } + }); + if (selected_action) { + elem_extra.attr('type', 'hidden'); + if (selected_action.extra_field) { + elem_extra.attr('type', 'text'); + elem_extra.attr('placeholder', selected_action.extra_field_placeholder) + } + if (selected_action.type === 'none') { + elem.html(''); + } + if (selected_action.type === 'string') { + elem.html(''); + } + if (selected_action.type === 'int') { + elem.html(''); + } + if (selected_action.type === 'number') { + elem.html(''); + } + if (selected_action.type === 'text') { + elem.html(''); + } + if (selected_action.type === 'select') { + options = ''; + selected_action.values.forEach(function(val) { + if (tr_elem.attr('default_value') === val) { + options = options + '' + } else { + options = options + '' + } + }); + elem.html(''); + } + if (selected_action.type === 'mailbox') { + let mailboxes = null; + tr_elem.children().eq(2).html(hm_spinner()); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_get_mailboxes'}, + {'name': 'imap_account', 'value': current_account} ], + function(res) { + mailboxes = JSON.parse(res.mailboxes); + options = ''; + mailboxes.forEach(function(val) { + if (tr_elem.attr('default_value') === val) { + options = options + '' + } else { + options = options + '' + } + }); + elem.html(''); + $("[name^=sieve_selected_action_value]").last().val(elem.parent().attr('default_value')); } - ) - idx = idx + 1; + ); + } + } + }) + + /** + * Condition type change + */ + $(document).on('change', '.add_condition_sieve_filters', function () { + let condition_name = $(this).val(); + let elem = $(this).parent().next().next().find('.condition_options'); + let elem_extra = $(this).parent().next().find('.condition_extra_value'); + let elem_type = $(this).parent().next().next().next(); + let condition; + let options_html = ''; + let input_type_html = ''; + hm_sieve_condition_fields().Message.forEach(function (cond) { + if (condition_name === cond.name) { + condition = cond; + } + }); + hm_sieve_condition_fields().Header.forEach(function (cond) { + if (condition_name === cond.name) { + condition = cond; + } + }); + if (condition) { + if (condition.extra_option === true) { + elem_extra.attr('type', 'text'); + elem_extra.attr('placeholder', condition.extra_option_description); + } else { + elem_extra.attr('type', 'hidden'); + } + condition.options.forEach(function (option) { + options_html += ''; + options_html += ''; }); + elem.html(options_html); - if ($('#stop_filtering').is(':checked')) { - actions_parsed.push( - { - 'action': "stop", - 'value': "", - 'extra_option': "", - 'extra_option_value': "", - } - ) - } - if ($('.modal_sieve_filter_name').val() == "") { - Hm_Utils.add_sys_message(hm_trans('Filter name is required'), 'danger'); - return false; - } - - if (validation_failed) { - return false; - } - - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_save_filter'}, - {'name': 'imap_account', 'value': imap_account}, - {'name': 'sieve_filter_name', 'value': $('.modal_sieve_filter_name').val()}, - {'name': 'sieve_filter_priority', 'value': $('.modal_sieve_filter_priority').val()}, - {'name': 'is_editing_filter', 'value': is_editing_filter}, - {'name': 'current_editing_filter_name', 'value': current_editing_filter_name}, - {'name': 'conditions_json', 'value': JSON.stringify(conditions_parsed)}, - {'name': 'actions_json', 'value': JSON.stringify(actions_parsed)}, - {'name': 'filter_test_type', 'value': $('.modal_sieve_filter_test').val()}, - {'name': 'gen_script', 'value': gen_script}, - ], - function(res) { - if (Object.keys(res.script_details).length === 0) { - window.location = window.location; - } else { - edit_script_modal.open(); - $('.modal_sieve_script_textarea').val(res.script_details.gen_script); - $('.modal_sieve_script_name').val(res.script_details.filter_name); - $('.modal_sieve_script_priority').val(res.script_details.filter_priority); - } + if (condition.type === 'string') { + elem_type.html('') + } + if (condition.type === 'int') { + elem_type.html('') + } + if (condition.type === 'none') { + elem_type.html('') + } + } + }); + + /** + * Delete filter event + */ + $(document).on('click', '.delete_filter', function (e) { + e.preventDefault(); + if (!confirm('Do you want to delete filter?')) { + return; + } + let obj = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_delete_filter'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + if (res.script_removed == '1') { + obj.parent().parent().remove(); } - ); + } + ); + }); - return true; + + /** + * Delete script event + */ + $(document).on('click', '.delete_script', function (e) { + e.preventDefault(); + if (!confirm('Do you want to delete script?')) { + return; } + let obj = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_delete_script'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + if (res.script_removed == '1') { + obj.parent().parent().remove(); + } + } + ); + }); - function save_script(imap_account) { - if ($('.modal_sieve_script_name').val() === "") { - Hm_Utils.add_sys_message(hm_trans('You must provide a name for your script'), 'danger'); - return false; + /** + * Edit script event + */ + $(document).on('click', '.edit_script', function (e) { + e.preventDefault(); + let obj = $(this); + edit_script_modal.setTitle('Edit Script'); + current_account = $(this).attr('account'); + is_editing_script = true; + current_editing_script_name = $(this).attr('script_name'); + current_account = $(this).attr('imap_account'); + $('.modal_sieve_script_name').val($(this).attr('script_name_parsed')); + $('.modal_sieve_script_priority').val($(this).attr('priority')); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_edit_script'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + $('.modal_sieve_script_textarea').html(res.script); + edit_script_modal.open(); } - if ($('.modal_sieve_script_textarea').val() === "") { - Hm_Utils.add_sys_message(hm_trans('Empty script'), 'danger'); - return false; + ); + }); + + /** + * Edit filter event + */ + $(document).on('click', '.edit_filter', function (e) { + e.preventDefault(); + let obj = $(this); + current_account = $(this).attr('account'); + is_editing_filter = true; + current_editing_filter_name = $(this).attr('script_name'); + current_account = $(this).attr('imap_account'); + // $('#stop_filtering').prop('checked', false); + $('.modal_sieve_filter_name').val($(this).attr('script_name_parsed')); + $('.modal_sieve_filter_priority').val($(this).attr('priority')); + $('.sieve_list_conditions_modal').html(''); + $('.filter_actions_modal_table').html(''); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_edit_filter'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + conditions = JSON.parse(JSON.parse(res.conditions)); + actions = JSON.parse(JSON.parse(res.actions)); + test_type = res.test_type; + $(".modal_sieve_filter_test").val(test_type); + conditions.forEach(function (condition) { + add_filter_condition(); + $(".add_condition_sieve_filters").last().val(condition.condition); + $(".add_condition_sieve_filters").last().trigger('change'); + $(".condition_options").last().val(condition.type); + $("[name^=sieve_selected_extra_option_value]").last().val(condition.extra_option_value); + if ($("[name^=sieve_selected_option_value]").last().is('input')) { + $("[name^=sieve_selected_option_value]").last().val(condition.value); + } + }); + + actions.forEach(function (action) { + if (action.action === "stop") { + $('#stop_filtering').prop('checked', true); + } else { + add_filter_action(action.value); + $(".sieve_actions_select").last().val(action.action); + $(".sieve_actions_select").last().trigger('change'); + $("[name^=sieve_selected_extra_action_value]").last().val(action.extra_option_value); + if ($("[name^=sieve_selected_action_value]").last().is('input')) { + $("[name^=sieve_selected_action_value]").last().val(action.value); + } else if ($("[name^=sieve_selected_action_value]").last().is('textarea')) { + $("[name^=sieve_selected_action_value]").last().text(action.value); + } + } + }); } - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_save_script'}, - {'name': 'imap_account', 'value': imap_account}, - {'name': 'sieve_script_name', 'value': $('.modal_sieve_script_name').val()}, - {'name': 'sieve_script_priority', 'value': $('.modal_sieve_script_priority').val()}, - {'name': 'is_editing_script', 'value': is_editing_script}, - {'name': 'current_editing_script', 'value': current_editing_script_name}, - {'name': 'script', 'value': $('.modal_sieve_script_textarea').val()}], - function(res) { - window.location = window.location; - } - ); - } + ); + }); +} - /************************************************************************************** - * MODAL EVENTS +function sieveFiltersPageHandler() { + let is_editing_script = false; + let current_editing_script_name = ''; + let is_editing_filter = false; + let current_editing_filter_name = ''; + let current_account; + /************************************************************************************** + * BOOTSTRAP SCRIPT MODAL **************************************************************************************/ - $('.sievefilters_accounts_title').on("click", function () { - $(this).parent().find('.sievefilters_accounts').toggleClass('d-none'); - }); - $('.add_filter').on('click', function () { - edit_filter_modal.setTitle('Add Filter'); - $('.modal_sieve_filter_priority').val(''); - $('.modal_sieve_filter_test').val('ALLOF'); - $('#stop_filtering').prop('checked', false); - current_account = $(this).attr('account'); - edit_filter_modal.open(); - - // Reset the form fields when opening the modal - $(".modal_sieve_filter_name").val(''); - $(".modal_sieve_script_priority").val(''); - $(".sieve_list_conditions_modal").empty(); - $(".filter_actions_modal_table").empty(); - }); - $('.add_script').on('click', function () { - edit_script_modal.setTitle('Add Script'); - $('.modal_sieve_script_textarea').val(''); - $('.modal_sieve_script_name').val(''); - $('.modal_sieve_script_priority').val(''); - is_editing_script = false; - current_editing_script_name = ''; - current_account = $(this).attr('account'); - edit_script_modal.open(); - }); - $('.edit_filter').on('click', function (e) { - e.preventDefault(); - let script_name = $(this).parent().parent().children().next().html(); - edit_filter_modal.setTitle(script_name); - edit_filter_modal.open(); - }); + var edit_script_modal = new Hm_Modal({ + size: 'xl', + modalId: 'myEditScript' + }); - /** - * Delete action Button - */ - $(document).on('click', '.delete_else_action_modal_button', function (e) { - e.preventDefault(); - $(this).parent().parent().remove(); - }); + // set content + edit_script_modal.setContent(document.querySelector('#edit_script_modal').innerHTML); + $('#edit_script_modal').remove(); - /** - * Delete action Button - */ - $(document).on('click', '.delete_action_modal_button', function (e) { - e.preventDefault(); - $(this).parent().parent().remove(); - }); + // add a button + edit_script_modal.addFooterBtn('Save', 'btn-primary', async function () { + save_script(current_account); + }); - /** - * Delete Condition Button - */ - $(document).on('click', '.delete_condition_modal_button', function (e) { - e.preventDefault(); - $(this).parent().parent().remove(); - }); - function add_filter_condition() { - let header_fields = ''; - let message_fields = ''; + /************************************************************************************** + * BOOTSTRAP SIEVE FILTER MODAL + **************************************************************************************/ + var edit_filter_modal = new Hm_Modal({ + size: 'xl', + modalId: 'myEditFilterModal', + }); - hm_sieve_condition_fields().Message.forEach(function (value) { - if (value.selected === true) { - message_fields += ''; - } else { - message_fields += ''; - } - }); - hm_sieve_condition_fields().Header.forEach(function (value) { - if (value.selected === true) { - header_fields += ''; - } else { - header_fields += ''; - } - }); - let extra_options = ''; - $('.sieve_list_conditions_modal').append( - ' ' + - ' ' + - ' ' + - ' ' + - extra_options + - ' ' + - ' ' + - ' ' + - ' ' + - ' ' + - ' ' + - ' ' + - ' Delete' + - ' ' + - ' ' - ); - } - - /** - * Add Condition Button - */ - $(document).on('click', '.sieve_add_condition_modal_button', function () { - add_filter_condition(); - }); + // set content + edit_filter_modal.setContent(document.querySelector('#edit_filter_modal').innerHTML); + $('#edit_filter_modal').remove(); + + // add a button + edit_filter_modal.addFooterBtn('Save', 'btn-primary ms-auto', async function () { + let result = save_filter(current_account); + if (result) { + edit_filter_modal.hide(); + } + }); - function add_filter_action(default_value = '') { - let possible_actions_html = ''; + // add another button + edit_filter_modal.addFooterBtn('Convert to code', 'btn-warning', async function () { + let result = save_filter(current_account, true); + if (result) { + edit_filter_modal.hide(); + } + }); - hm_sieve_possible_actions().forEach(function (value) { - if (value.selected === true) { - possible_actions_html += ''; - return; - } - possible_actions_html += ''; - }); - let extra_options = ''; - $('.filter_actions_modal_table').append( - '' + - ' ' + - ' ' + - ' ' + - extra_options + - ' ' + - ' ' + - ' ' + - ' ' + - ' Delete' + - ' ' + - '' - ); - } - - /** - * Add Action Button - */ - $(document).on('click', '.filter_modal_add_action_btn', function () { - add_filter_action(); - }); - /** - * Add Else Action Button - */ - $(document).on('click', '.filter_modal_add_else_action_btn', function () { - let possible_actions_html = ''; + function ordinal_number(n) + { + let ord = 'th'; - hm_sieve_possible_actions().forEach(function (value) { - if (value.selected === true) { - possible_actions_html += ''; - return; - } - possible_actions_html += ''; - }); + if (n % 10 == 1 && n % 100 != 11) { + ord = 'st'; + } else if (n % 10 == 2 && n % 100 != 12) { + ord = 'nd'; + } else if (n % 10 == 3 && n % 100 != 13) { + ord = 'rd'; + } - $('.filter_else_actions_modal_table').append( - '' + - ' ' + - ' ' + - ' ' + - ' ' + - ' ' + - ' ' + - ' Delete' + - ' ' + - '' - ); + return n + ord; + } + + /************************************************************************************** + * FUNCTIONS + **************************************************************************************/ + function save_filter(imap_account, gen_script = false) { + let validation_failed = false + let conditions_parsed = [] + let actions_parsed = [] + let conditions = $('select[name^=sieve_selected_conditions_field]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let conditions_type = $('select[name^=sieve_selected_conditions_options]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let conditions_value = $('input[name^=sieve_selected_option_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let conditions_extra_value = $('input[name^=sieve_selected_extra_option_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + let idx = 0; + if (conditions.length === 0) { + Hm_Utils.add_sys_message(hm_trans('You must provide at least one condition'), 'danger'); + return false; + } + + Hm_Utils.clear_sys_messages(); + 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 ? '
': ''; + Hm_Utils.add_sys_message('The ' + order + ' condition (' + elem + ') must be provided', 'danger'); + validation_failed = true; + } + conditions_parsed.push( + { + 'condition': elem, + 'type': conditions_type[idx], + 'extra_option': conditions[idx].extra_option, + 'extra_option_value': conditions_extra_value[idx], + 'value': conditions_value[idx] + } + ) + idx = idx + 1; }); + let actions_type = $('select[name^=sieve_selected_actions]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + let actions_value = $('[name^=sieve_selected_action_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + let actions_field_type = $('[name^=sieve_selected_action_value]').map(function(idx, elem) { + return $(elem).attr('type'); + }).get(); + let actions_extra_value = $('input[name^=sieve_selected_extra_action_value]').map(function(idx, elem) { + return $(elem).val(); + }).get(); + + if (actions_type.length === 0) { + Hm_Utils.add_sys_message(hm_trans('You must provide at least one action'), 'danger'); + return false; + } - /** - * Action change - */ - $(document).on('change', '.sieve_actions_select', function () { - let tr_elem = $(this).parent().parent(); - console.log(tr_elem.attr('default_value')); - let elem = $(this).parent().next().next(); - let elem_extra = $(this).parent().next().find('.condition_extra_action_value'); - let action_name = $(this).val(); - let selected_action; - hm_sieve_possible_actions().forEach(function (action) { - if (action_name === action.name) { - selected_action = action; - } - }); - if (selected_action) { - elem_extra.attr('type', 'hidden'); - if (selected_action.extra_field) { - elem_extra.attr('type', 'text'); - elem_extra.attr('placeholder', selected_action.extra_field_placeholder) - } - if (selected_action.type === 'none') { - elem.html(''); - } - if (selected_action.type === 'string') { - elem.html(''); - } - if (selected_action.type === 'int') { - elem.html(''); - } - if (selected_action.type === 'number') { - elem.html(''); - } - if (selected_action.type === 'text') { - elem.html(''); - } - if (selected_action.type === 'select') { - options = ''; - selected_action.values.forEach(function(val) { - if (tr_elem.attr('default_value') === val) { - options = options + '' - } else { - options = options + '' - } - }); - elem.html(''); - } - if (selected_action.type === 'mailbox') { - let mailboxes = null; - tr_elem.children().eq(2).html(hm_spinner()); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_get_mailboxes'}, - {'name': 'imap_account', 'value': current_account} ], - function(res) { - mailboxes = JSON.parse(res.mailboxes); - options = ''; - mailboxes.forEach(function(val) { - if (tr_elem.attr('default_value') === val) { - options = options + '' - } else { - options = options + '' - } - }); - elem.html(''); - $("[name^=sieve_selected_action_value]").last().val(elem.parent().attr('default_value')); - } - ); - } + idx = 0; + actions_type.forEach(function (elem, key) { + console.log(actions_field_type[idx]) + if (actions_value[idx] === "" && actions_field_type[idx] !== 'hidden') { + let order = ordinal_number(key + 1); + let previous_messages = $('.sys_messages').html(); + previous_messages += previous_messages ? '
': ''; + Hm_Utils.add_sys_message('The ' + order + ' action (' + elem + ') must be provided', 'danger'); + validation_failed = true; } - }) - - /** - * Condition type change - */ - $(document).on('change', '.add_condition_sieve_filters', function () { - let condition_name = $(this).val(); - let elem = $(this).parent().next().next().find('.condition_options'); - let elem_extra = $(this).parent().next().find('.condition_extra_value'); - let elem_type = $(this).parent().next().next().next(); - let condition; - let options_html = ''; - let input_type_html = ''; - hm_sieve_condition_fields().Message.forEach(function (cond) { - if (condition_name === cond.name) { - condition = cond; + actions_parsed.push( + { + 'action': elem, + 'value': actions_value[idx], + 'extra_option': actions_type[idx].extra_option, + 'extra_option_value': actions_extra_value[idx], } - }); - hm_sieve_condition_fields().Header.forEach(function (cond) { - if (condition_name === cond.name) { - condition = cond; + ) + idx = idx + 1; + }); + + if ($('#stop_filtering').is(':checked')) { + actions_parsed.push( + { + 'action': "stop", + 'value': "", + 'extra_option': "", + 'extra_option_value': "", } - }); - if (condition) { - if (condition.extra_option === true) { - elem_extra.attr('type', 'text'); - elem_extra.attr('placeholder', condition.extra_option_description); + ) + } + if ($('.modal_sieve_filter_name').val() == "") { + Hm_Utils.add_sys_message(hm_trans('Filter name is required'), 'danger'); + return false; + } + + if (validation_failed) { + return false; + } + + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_save_filter'}, + {'name': 'imap_account', 'value': imap_account}, + {'name': 'sieve_filter_name', 'value': $('.modal_sieve_filter_name').val()}, + {'name': 'sieve_filter_priority', 'value': $('.modal_sieve_filter_priority').val()}, + {'name': 'is_editing_filter', 'value': is_editing_filter}, + {'name': 'current_editing_filter_name', 'value': current_editing_filter_name}, + {'name': 'conditions_json', 'value': JSON.stringify(conditions_parsed)}, + {'name': 'actions_json', 'value': JSON.stringify(actions_parsed)}, + {'name': 'filter_test_type', 'value': $('.modal_sieve_filter_test').val()}, + {'name': 'gen_script', 'value': gen_script}, + ], + function(res) { + if (Object.keys(res.script_details).length === 0) { + window.location = window.location; } else { - elem_extra.attr('type', 'hidden'); + edit_script_modal.open(); + $('.modal_sieve_script_textarea').val(res.script_details.gen_script); + $('.modal_sieve_script_name').val(res.script_details.filter_name); + $('.modal_sieve_script_priority').val(res.script_details.filter_priority); } - condition.options.forEach(function (option) { - options_html += ''; - options_html += ''; - }); - elem.html(options_html); + } + ); - if (condition.type === 'string') { - elem_type.html('') - } - if (condition.type === 'int') { - elem_type.html('') - } - if (condition.type === 'none') { - elem_type.html('') - } + return true; + } + + function save_script(imap_account) { + if ($('.modal_sieve_script_name').val() === "") { + Hm_Utils.add_sys_message(hm_trans('You must provide a name for your script'), 'danger'); + return false; + } + if ($('.modal_sieve_script_textarea').val() === "") { + Hm_Utils.add_sys_message(hm_trans('Empty script'), 'danger'); + return false; + } + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_save_script'}, + {'name': 'imap_account', 'value': imap_account}, + {'name': 'sieve_script_name', 'value': $('.modal_sieve_script_name').val()}, + {'name': 'sieve_script_priority', 'value': $('.modal_sieve_script_priority').val()}, + {'name': 'is_editing_script', 'value': is_editing_script}, + {'name': 'current_editing_script', 'value': current_editing_script_name}, + {'name': 'script', 'value': $('.modal_sieve_script_textarea').val()}], + function(res) { + window.location = window.location; + } + ); + } + + /************************************************************************************** + * MODAL EVENTS + **************************************************************************************/ + $('.sievefilters_accounts_title').on("click", function () { + $(this).parent().find('.sievefilters_accounts').toggleClass('d-none'); + }); + $('.add_filter').on('click', function () { + edit_filter_modal.setTitle('Add Filter'); + $('.modal_sieve_filter_priority').val(''); + $('.modal_sieve_filter_test').val('ALLOF'); + $('#stop_filtering').prop('checked', false); + current_account = $(this).attr('account'); + edit_filter_modal.open(); + + // Reset the form fields when opening the modal + $(".modal_sieve_filter_name").val(''); + $(".modal_sieve_script_priority").val(''); + $(".sieve_list_conditions_modal").empty(); + $(".filter_actions_modal_table").empty(); + }); + $('.add_script').on('click', function () { + edit_script_modal.setTitle('Add Script'); + $('.modal_sieve_script_textarea').val(''); + $('.modal_sieve_script_name').val(''); + $('.modal_sieve_script_priority').val(''); + is_editing_script = false; + current_editing_script_name = ''; + current_account = $(this).attr('account'); + edit_script_modal.open(); + }); + $('.edit_filter').on('click', function (e) { + e.preventDefault(); + let script_name = $(this).parent().parent().children().next().html(); + edit_filter_modal.setTitle(script_name); + edit_filter_modal.open(); + }); + + /** + * Delete action Button + */ + $(document).on('click', '.delete_else_action_modal_button', function (e) { + e.preventDefault(); + $(this).parent().parent().remove(); + }); + + /** + * Delete action Button + */ + $(document).on('click', '.delete_action_modal_button', function (e) { + e.preventDefault(); + $(this).parent().parent().remove(); + }); + + /** + * Delete Condition Button + */ + $(document).on('click', '.delete_condition_modal_button', function (e) { + e.preventDefault(); + $(this).parent().parent().remove(); + }); + + function add_filter_condition() { + let header_fields = ''; + let message_fields = ''; + + hm_sieve_condition_fields().Message.forEach(function (value) { + if (value.selected === true) { + message_fields += ''; + } else { + message_fields += ''; + } + }); + hm_sieve_condition_fields().Header.forEach(function (value) { + if (value.selected === true) { + header_fields += ''; + } else { + header_fields += ''; } }); + let extra_options = ''; + $('.sieve_list_conditions_modal').append( + ' ' + + ' ' + + ' ' + + ' ' + + extra_options + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' Delete' + + ' ' + + ' ' + ); + } - /** - * Delete filter event - */ - $(document).on('click', '.delete_filter', function (e) { - e.preventDefault(); - if (!confirm('Do you want to delete filter?')) { + /** + * Add Condition Button + */ + $(document).on('click', '.sieve_add_condition_modal_button', function () { + add_filter_condition(); + }); + + function add_filter_action(default_value = '') { + let possible_actions_html = ''; + + hm_sieve_possible_actions().forEach(function (value) { + if (value.selected === true) { + possible_actions_html += ''; return; } - let obj = $(this); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_delete_filter'}, - {'name': 'imap_account', 'value': $(this).attr('imap_account')}, - {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], - function(res) { - if (res.script_removed == '1') { - obj.parent().parent().remove(); - } - } - ); + possible_actions_html += ''; }); + let extra_options = ''; + $('.filter_actions_modal_table').append( + '' + + ' ' + + ' ' + + ' ' + + extra_options + + ' ' + + ' ' + + ' ' + + ' ' + + ' Delete' + + ' ' + + '' + ); + } + + /** + * Add Action Button + */ + $(document).on('click', '.filter_modal_add_action_btn', function () { + add_filter_action(); + }); + /** + * Add Else Action Button + */ + $(document).on('click', '.filter_modal_add_else_action_btn', function () { + let possible_actions_html = ''; - /** - * Delete script event - */ - $(document).on('click', '.delete_script', function (e) { - e.preventDefault(); - if (!confirm('Do you want to delete script?')) { + hm_sieve_possible_actions().forEach(function (value) { + if (value.selected === true) { + possible_actions_html += ''; return; } - let obj = $(this); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_delete_script'}, - {'name': 'imap_account', 'value': $(this).attr('imap_account')}, - {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], - function(res) { - if (res.script_removed == '1') { - obj.parent().parent().remove(); + possible_actions_html += ''; + }); + + $('.filter_else_actions_modal_table').append( + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' Delete' + + ' ' + + '' + ); + }); + + + /** + * Action change + */ + $(document).on('change', '.sieve_actions_select', function () { + let tr_elem = $(this).parent().parent(); + console.log(tr_elem.attr('default_value')); + let elem = $(this).parent().next().next(); + let elem_extra = $(this).parent().next().find('.condition_extra_action_value'); + let action_name = $(this).val(); + let selected_action; + hm_sieve_possible_actions().forEach(function (action) { + if (action_name === action.name) { + selected_action = action; + } + }); + if (selected_action) { + elem_extra.attr('type', 'hidden'); + if (selected_action.extra_field) { + elem_extra.attr('type', 'text'); + elem_extra.attr('placeholder', selected_action.extra_field_placeholder) + } + if (selected_action.type === 'none') { + elem.html(''); + } + if (selected_action.type === 'string') { + elem.html(''); + } + if (selected_action.type === 'int') { + elem.html(''); + } + if (selected_action.type === 'number') { + elem.html(''); + } + if (selected_action.type === 'text') { + elem.html(''); + } + if (selected_action.type === 'select') { + options = ''; + selected_action.values.forEach(function(val) { + if (tr_elem.attr('default_value') === val) { + options = options + '' + } else { + options = options + '' } - } - ); + }); + elem.html(''); + } + if (selected_action.type === 'mailbox') { + let mailboxes = null; + tr_elem.children().eq(2).html(hm_spinner()); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_get_mailboxes'}, + {'name': 'imap_account', 'value': current_account} ], + function(res) { + mailboxes = JSON.parse(res.mailboxes); + options = ''; + mailboxes.forEach(function(val) { + if (tr_elem.attr('default_value') === val) { + options = options + '' + } else { + options = options + '' + } + }); + elem.html(''); + $("[name^=sieve_selected_action_value]").last().val(elem.parent().attr('default_value')); + } + ); + } + } + }) + + /** + * Condition type change + */ + $(document).on('change', '.add_condition_sieve_filters', function () { + let condition_name = $(this).val(); + let elem = $(this).parent().next().next().find('.condition_options'); + let elem_extra = $(this).parent().next().find('.condition_extra_value'); + let elem_type = $(this).parent().next().next().next(); + let condition; + let options_html = ''; + let input_type_html = ''; + hm_sieve_condition_fields().Message.forEach(function (cond) { + if (condition_name === cond.name) { + condition = cond; + } + }); + hm_sieve_condition_fields().Header.forEach(function (cond) { + if (condition_name === cond.name) { + condition = cond; + } }); + if (condition) { + if (condition.extra_option === true) { + elem_extra.attr('type', 'text'); + elem_extra.attr('placeholder', condition.extra_option_description); + } else { + elem_extra.attr('type', 'hidden'); + } + condition.options.forEach(function (option) { + options_html += ''; + options_html += ''; + }); + elem.html(options_html); - /** - * Edit script event - */ - $(document).on('click', '.edit_script', function (e) { - e.preventDefault(); - let obj = $(this); - edit_script_modal.setTitle('Edit Script'); - current_account = $(this).attr('account'); - is_editing_script = true; - current_editing_script_name = $(this).attr('script_name'); - current_account = $(this).attr('imap_account'); - $('.modal_sieve_script_name').val($(this).attr('script_name_parsed')); - $('.modal_sieve_script_priority').val($(this).attr('priority')); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_edit_script'}, - {'name': 'imap_account', 'value': $(this).attr('imap_account')}, - {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], - function(res) { - $('.modal_sieve_script_textarea').html(res.script); - edit_script_modal.open(); + if (condition.type === 'string') { + elem_type.html('') + } + if (condition.type === 'int') { + elem_type.html('') + } + if (condition.type === 'none') { + elem_type.html('') + } + } + }); + + /** + * Delete filter event + */ + $(document).on('click', '.delete_filter', function (e) { + e.preventDefault(); + if (!confirm('Do you want to delete filter?')) { + return; + } + let obj = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_delete_filter'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + if (res.script_removed == '1') { + obj.parent().parent().remove(); } - ); - }); + } + ); + }); - /** - * Edit filter event - */ - $(document).on('click', '.edit_filter', function (e) { - e.preventDefault(); - let obj = $(this); - current_account = $(this).attr('account'); - is_editing_filter = true; - current_editing_filter_name = $(this).attr('script_name'); - current_account = $(this).attr('imap_account'); - // $('#stop_filtering').prop('checked', false); - $('.modal_sieve_filter_name').val($(this).attr('script_name_parsed')); - $('.modal_sieve_filter_priority').val($(this).attr('priority')); - $('.sieve_list_conditions_modal').html(''); - $('.filter_actions_modal_table').html(''); - Hm_Ajax.request( - [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_edit_filter'}, - {'name': 'imap_account', 'value': $(this).attr('imap_account')}, - {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], - function(res) { - conditions = JSON.parse(JSON.parse(res.conditions)); - actions = JSON.parse(JSON.parse(res.actions)); - test_type = res.test_type; - $(".modal_sieve_filter_test").val(test_type); - conditions.forEach(function (condition) { - add_filter_condition(); - $(".add_condition_sieve_filters").last().val(condition.condition); - $(".add_condition_sieve_filters").last().trigger('change'); - $(".condition_options").last().val(condition.type); - $("[name^=sieve_selected_extra_option_value]").last().val(condition.extra_option_value); - if ($("[name^=sieve_selected_option_value]").last().is('input')) { - $("[name^=sieve_selected_option_value]").last().val(condition.value); - } - }); - - actions.forEach(function (action) { - if (action.action === "stop") { - $('#stop_filtering').prop('checked', true); - } else { - add_filter_action(action.value); - $(".sieve_actions_select").last().val(action.action); - $(".sieve_actions_select").last().trigger('change'); - $("[name^=sieve_selected_extra_action_value]").last().val(action.extra_option_value); - if ($("[name^=sieve_selected_action_value]").last().is('input')) { - $("[name^=sieve_selected_action_value]").last().val(action.value); - } else if ($("[name^=sieve_selected_action_value]").last().is('textarea')) { - $("[name^=sieve_selected_action_value]").last().text(action.value); - } - } - }); + + /** + * Delete script event + */ + $(document).on('click', '.delete_script', function (e) { + e.preventDefault(); + if (!confirm('Do you want to delete script?')) { + return; + } + let obj = $(this); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_delete_script'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + if (res.script_removed == '1') { + obj.parent().parent().remove(); } - ); - }); - } + } + ); + }); + + /** + * Edit script event + */ + $(document).on('click', '.edit_script', function (e) { + e.preventDefault(); + let obj = $(this); + edit_script_modal.setTitle('Edit Script'); + current_account = $(this).attr('account'); + is_editing_script = true; + current_editing_script_name = $(this).attr('script_name'); + current_account = $(this).attr('imap_account'); + $('.modal_sieve_script_name').val($(this).attr('script_name_parsed')); + $('.modal_sieve_script_priority').val($(this).attr('priority')); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_edit_script'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + $('.modal_sieve_script_textarea').html(res.script); + edit_script_modal.open(); + } + ); + }); + + /** + * Edit filter event + */ + $(document).on('click', '.edit_filter', function (e) { + e.preventDefault(); + let obj = $(this); + current_account = $(this).attr('account'); + is_editing_filter = true; + current_editing_filter_name = $(this).attr('script_name'); + current_account = $(this).attr('imap_account'); + // $('#stop_filtering').prop('checked', false); + $('.modal_sieve_filter_name').val($(this).attr('script_name_parsed')); + $('.modal_sieve_filter_priority').val($(this).attr('priority')); + $('.sieve_list_conditions_modal').html(''); + $('.filter_actions_modal_table').html(''); + Hm_Ajax.request( + [ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_edit_filter'}, + {'name': 'imap_account', 'value': $(this).attr('imap_account')}, + {'name': 'sieve_script_name', 'value': $(this).attr('script_name')}], + function(res) { + conditions = JSON.parse(JSON.parse(res.conditions)); + actions = JSON.parse(JSON.parse(res.actions)); + test_type = res.test_type; + $(".modal_sieve_filter_test").val(test_type); + conditions.forEach(function (condition) { + add_filter_condition(); + $(".add_condition_sieve_filters").last().val(condition.condition); + $(".add_condition_sieve_filters").last().trigger('change'); + $(".condition_options").last().val(condition.type); + $("[name^=sieve_selected_extra_option_value]").last().val(condition.extra_option_value); + if ($("[name^=sieve_selected_option_value]").last().is('input')) { + $("[name^=sieve_selected_option_value]").last().val(condition.value); + } + }); + + actions.forEach(function (action) { + if (action.action === "stop") { + $('#stop_filtering').prop('checked', true); + } else { + add_filter_action(action.value); + $(".sieve_actions_select").last().val(action.action); + $(".sieve_actions_select").last().trigger('change'); + $("[name^=sieve_selected_extra_action_value]").last().val(action.extra_option_value); + if ($("[name^=sieve_selected_action_value]").last().is('input')) { + $("[name^=sieve_selected_action_value]").last().val(action.value); + } else if ($("[name^=sieve_selected_action_value]").last().is('textarea')) { + $("[name^=sieve_selected_action_value]").last().text(action.value); + } + } + }); + } + ); + }); +} + +$(function () { + $(document).on('change', '#block_action', function(e) { + if ($(this).val() == 'reject_with_message') { + $('
').insertAfter($(this)); + } else { + $('#reject_message').remove(); + } + }); }); diff --git a/modules/smtp/js_modules/route_handlers.js b/modules/smtp/js_modules/route_handlers.js index b34b04ff0d..a34309a8b4 100644 --- a/modules/smtp/js_modules/route_handlers.js +++ b/modules/smtp/js_modules/route_handlers.js @@ -245,4 +245,7 @@ function applyComposePageHandlers() { $('.compose_cc').on('keyup', function(e) { autocomplete_contact(e, '.compose_cc', '#cc_contacts'); }); $('.compose_bcc').on('keyup', function(e) { autocomplete_contact(e, '.compose_bcc', '#bcc_contacts'); }); $('.compose_to').focus(); + + if (window.pgpComposePageHandler) pgpComposePageHandler(); + if (window.profilesComposePageHandler) profilesComposePageHandler(); } \ No newline at end of file diff --git a/modules/smtp/site.js b/modules/smtp/site.js index f73f193109..490649e56e 100644 --- a/modules/smtp/site.js +++ b/modules/smtp/site.js @@ -173,7 +173,7 @@ var toggle_recip_flds = function() { return false; } -if (hm_page_name() === 'servers') { +function smtpServersPageHandler() { $('.test_smtp_connect').on('click', smtp_test_action); $('.save_smtp_connection').on('click', smtp_save_action); $('.forget_smtp_connection').on('click', smtp_forget_action); @@ -381,10 +381,10 @@ var is_valid_recipient = function(recipient) { }; var process_compose_form = function(){ - var msg_uid = hm_msg_uid(); - var detail = Hm_Utils.parse_folder_path(hm_list_path(), 'imap'); + var msg_uid = getMessageUidParam(); + var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap'); var class_name = 'imap_' + detail.server_id + '_' + msg_uid + '_' + detail.folder; - var key = 'imap_' + Hm_Utils.get_url_page_number() + '_' + hm_list_path(); + var key = 'imap_' + Hm_Utils.get_url_page_number() + '_' + getListPathParam(); var next_message = Hm_Message_List.prev_next_links(key, class_name)[1]; if (next_message) { @@ -415,23 +415,15 @@ var force_send_message = function() { } } -$(function () { - if (!hm_is_logged()) { - return; - } - if (hm_page_name() === 'settings') { - $('#clear_chunks_button').on('click', function(e) { - e.preventDefault(); - Hm_Ajax.request( - [{'name': 'hm_ajax_hook', 'value': 'ajax_clear_attachment_chunks'}], - function(res) { +function smtpSettingsPageHandler() { + $('#clear_chunks_button').on('click', function(e) { + e.preventDefault(); + Hm_Ajax.request( + [{'name': 'hm_ajax_hook', 'value': 'ajax_clear_attachment_chunks'}], + function(res) { - }, - [] - ); - }); - } - if (hm_page_name() === 'compose') { - applyComposePageHandlers(); - } -}); + }, + [] + ); + }); +} diff --git a/modules/tags/site.js b/modules/tags/site.js index 4f1deb8526..e69de29bb2 100644 --- a/modules/tags/site.js +++ b/modules/tags/site.js @@ -1,3 +0,0 @@ -if (hm_page_name() == 'tags') { - -} \ No newline at end of file diff --git a/modules/wordpress/site.js b/modules/wordpress/site.js index d7375f5a7b..b36912a90e 100644 --- a/modules/wordpress/site.js +++ b/modules/wordpress/site.js @@ -19,7 +19,7 @@ var display_wordpress_notices = function(res) { var wp_notice_view = function(uid, callback) { if (!uid) { - uid = hm_msg_uid(); + uid = getMessageUidParam(); } $('.msg_text_inner').html(''); Hm_Ajax.request( @@ -37,8 +37,8 @@ var display_wp_notice = function(res) { $('.msg_text').html(''); $('.msg_text').append(res.wp_notice_headers); $('.msg_text').append(res.wp_notice_text); - var path = hm_list_path(); - var uid = hm_msg_uid(); + var path = getListPathParam(); + var uid = getMessageUidParam(); if (hm_list_parent() == 'unread') { Hm_Message_List.prev_next_links('formatted_unread_data', uid); } @@ -49,12 +49,13 @@ var display_wp_notice = function(res) { }; -if (hm_page_name() == 'message_list') { - if (hm_list_path() == 'wp_notifications') { +function wpMessageListPageHandler(routeParams) { + if (routeParams.list_path == 'wp_notifications') { Hm_Message_List.page_caches.wp_notifications = 'formatted_wp_notice_data'; } } -else if (hm_page_name() == 'servers') { + +function wpServersPageHandler() { if ($('#wp_disconnect_form').length) { $('#wp_disconnect_form').submit(function(e) { if (!hm_delete_prompt()) { @@ -65,8 +66,9 @@ else if (hm_page_name() == 'servers') { }); } } -else if (hm_page_name() == 'message') { - if (hm_list_path() == 'wp_notifications') { + +function wpMessageContentPageHandler(routeParams) { + if (routeParams.list_path == 'wp_notifications') { wp_notice_view(); } }