From ed1ceea87829093ddd62bf214e56213bf4d91c2f Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Fri, 18 Apr 2014 10:13:51 +0200 Subject: Add possibility to download messages in mbox format (#1486069) Removed "download folder" option, the same can be done by selecting all messages in a folder Code refactoring --- plugins/zipdownload/zipdownload.js | 116 ++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 27 deletions(-) (limited to 'plugins/zipdownload/zipdownload.js') diff --git a/plugins/zipdownload/zipdownload.js b/plugins/zipdownload/zipdownload.js index 080dcd9e3..0e0249dd3 100644 --- a/plugins/zipdownload/zipdownload.js +++ b/plugins/zipdownload/zipdownload.js @@ -2,32 +2,94 @@ * ZipDownload plugin script */ -function rcmail_zipmessages() { - if (rcmail.message_list && rcmail.message_list.get_selection().length > 1) { - rcmail.goto_url('plugin.zipdownload.zip_messages', '_mbox=' + urlencode(rcmail.env.mailbox) + '&_uid=' + rcmail.message_list.get_selection().join(',')); - } +window.rcmail && rcmail.addEventListener('init', function(evt) { + // register additional actions + rcmail.register_command('download-eml', function() { rcmail_zipdownload('eml'); }); + rcmail.register_command('download-mbox', function() { rcmail_zipdownload('mbox'); }); + rcmail.register_command('download-maildir', function() { rcmail_zipdownload('maildir'); }); + + // commands status + rcmail.message_list.addEventListener('select', function(list) { + var selected = list.get_selection().length; + + rcmail.enable_command('download', selected > 0); + rcmail.enable_command('download-eml', selected == 1); + rcmail.enable_command('download-mbox', 'download-maildir', selected > 1); + }); + + // hook before default download action + rcmail.addEventListener('beforedownload', rcmail_zipdownload_menu); + + // find and modify default download link/button + $.each(rcmail.buttons['download'] || [], function() { + var link = $('#' + this.id), + span = $('span', link); + + if (!span.length) { + span = $(''); + link.html('').append(span); + } + + span.addClass('folder-selector-link').text(rcmail.gettext('zipdownload.download')); + + rcmail.env.download_link = link; + }); + + // hide menu on click out of menu element + var fn = function(e) { + var menu = $('#zipdownload-menu'); + if (e.target != menu.get(0)) + menu.hide(); + }; + $(document.body).on('mouseup', fn); + $('iframe').contents().on('mouseup', fn) + .load(function(e) { try { $(this).contents().on('mouseup', fn); } catch(e) {}; }); +}); + + +function rcmail_zipdownload(mode) +{ + // default .eml download of single message + if (mode == 'eml') { + var uid = rcmail.get_single_uid(); + rcmail.goto_url('viewsource', {_uid: uid, _mbox: rcmail.get_message_mailbox(uid), _save: 1}); + return; + } + + // multi-message download, use hidden form to POST selection + if (rcmail.message_list && rcmail.message_list.get_selection().length > 1) { + var inputs = [], form = $('#zipdownload-form'), + post = rcmail.selection_post_data(); + + post._mode = mode; + post._token = rcmail.env.request_token; + + $.each(post, function(k, v) { + inputs.push($('').attr({type: 'hidden', name: k, value: v})); + }); + + if (!form.length) + form = $('
').attr({ + style: 'display: none', + method: 'POST', + action: '?_task=mail&_action=plugin.zipdownload.messages' + }) + .appendTo('body'); + + form.html('').append(inputs).submit(); + } } -$(document).ready(function() { - if (window.rcmail) { - rcmail.addEventListener('init', function(evt) { - // register command (directly enable in message view mode) - rcmail.register_command('plugin.zipdownload.zip_folder', function() { - rcmail.goto_url('plugin.zipdownload.zip_folder', '_mbox=' + urlencode(rcmail.env.mailbox)); - }, rcmail.env.messagecount > 0); - - if (rcmail.message_list && rcmail.env.zipdownload_selection) { - rcmail.message_list.addEventListener('select', function(list) { - rcmail.enable_command('download', list.get_selection().length > 0); - }); - - // check in contextmenu plugin exists and if so allow multiple message download - if (rcmail.contextmenu_disable_multi) - rcmail.contextmenu_disable_multi.splice($.inArray('#download', rcmail.contextmenu_disable_multi), 1); - } - }); - - rcmail.addEventListener('listupdate', function(props) { rcmail.enable_command('plugin.zipdownload.zip_folder', rcmail.env.messagecount > 0); } ); - rcmail.addEventListener('beforedownload', function(props) { rcmail_zipmessages(); } ); - } -}); \ No newline at end of file +// display download options menu +function rcmail_zipdownload_menu() +{ + // fix menu style and display menu + var z_index = rcmail.env.download_link.parents('.popupmenu').css('z-index'), + menu = $('#zipdownload-menu').css({'max-height': 'none', 'z-index': z_index + 1}).show(); + + // position menu on the screen + rcmail.element_position(menu, rcmail.env.download_link); + + // abort default download action + return false; +} -- cgit v1.2.3