Aleksander Machniak
2016-03-06 9d4e5f5e493e76f51ba3f6ea824b6deb574c1494
commit | author | age
70bbab 1 /**
TB 2  * ZipDownload plugin script
b34d67 3  *
TB 4  * @licstart  The following is the entire license notice for the
5  * JavaScript code in this file.
6  *
7  * Copyright (c) 2013-2014, The Roundcube Dev Team
8  *
9  * The JavaScript code in this page is free software: you can redistribute it
10  * and/or modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation, either version 3 of
12  * the License, or (at your option) any later version.
13  *
14  * @licend  The above is the entire license notice
15  * for the JavaScript code in this file.
70bbab 16  */
TB 17
ed1cee 18 window.rcmail && rcmail.addEventListener('init', function(evt) {
AM 19     // register additional actions
20     rcmail.register_command('download-eml', function() { rcmail_zipdownload('eml'); });
21     rcmail.register_command('download-mbox', function() { rcmail_zipdownload('mbox'); });
22     rcmail.register_command('download-maildir', function() { rcmail_zipdownload('maildir'); });
23
24     // commands status
5a7b7c 25     rcmail.message_list && rcmail.message_list.addEventListener('select', function(list) {
ed1cee 26         var selected = list.get_selection().length;
AM 27
28         rcmail.enable_command('download', selected > 0);
29         rcmail.enable_command('download-eml', selected == 1);
30         rcmail.enable_command('download-mbox', 'download-maildir', selected > 1);
31     });
32
33     // hook before default download action
34     rcmail.addEventListener('beforedownload', rcmail_zipdownload_menu);
35
36     // find and modify default download link/button
37     $.each(rcmail.buttons['download'] || [], function() {
38         var link = $('#' + this.id),
39             span = $('span', link);
40
41         if (!span.length) {
42             span = $('<span>');
43             link.html('').append(span);
44         }
45
8f8bea 46         span.text(rcmail.get_label('zipdownload.download'));
ed1cee 47         rcmail.env.download_link = link;
AM 48     });
9d4e5f 49 });
ed1cee 50
AM 51
52 function rcmail_zipdownload(mode)
53 {
54     // default .eml download of single message
55     if (mode == 'eml') {
56         var uid = rcmail.get_single_uid();
4a4088 57         rcmail.goto_url('viewsource', rcmail.params_from_uid(uid, {_save: 1}), false, true);
ed1cee 58         return;
AM 59     }
60
61     // multi-message download, use hidden form to POST selection
62     if (rcmail.message_list && rcmail.message_list.get_selection().length > 1) {
63         var inputs = [], form = $('#zipdownload-form'),
64             post = rcmail.selection_post_data();
65
66         post._mode = mode;
67         post._token = rcmail.env.request_token;
68
69         $.each(post, function(k, v) {
ca33f3 70             if (typeof v == 'object' && v.length > 1) {
TB 71               for (var j=0; j < v.length; j++)
72                   inputs.push($('<input>').attr({type: 'hidden', name: k+'[]', value: v[j]}));
73             }
74             else {
75                 inputs.push($('<input>').attr({type: 'hidden', name: k, value: v}));
76             }
ed1cee 77         });
AM 78
79         if (!form.length)
80             form = $('<form>').attr({
81                     style: 'display: none',
82                     method: 'POST',
83                     action: '?_task=mail&_action=plugin.zipdownload.messages'
84                 })
85                 .appendTo('body');
86
87         form.html('').append(inputs).submit();
88     }
70bbab 89 }
TB 90
ed1cee 91 // display download options menu
6789bf 92 function rcmail_zipdownload_menu(e)
ed1cee 93 {
6789bf 94     // show (sub)menu for download selection
TB 95     rcmail.command('menu-open', 'zipdownload-menu', e && e.target ? e.target : rcmail.env.download_link, e);
70bbab 96
ed1cee 97     // abort default download action
AM 98     return false;
99 }