Aleksander Machniak
2015-11-22 9f1f754daf4b57a0d0d3aea95d2321716d218cf5
commit | author | age
b34d67 1 /**
TB 2  * Attachment Reminder plugin script
3  *
4  * @licstart  The following is the entire license notice for the
5  * JavaScript code in this file.
6  *
7  * Copyright (c) 2013, 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.
16  */
864745 17
AM 18 function rcmail_get_compose_message()
19 {
20   var msg;
21
22   if (window.tinyMCE && (ed = tinyMCE.get(rcmail.env.composebody))) {
23     msg = ed.getContent();
24     msg = msg.replace(/<blockquote[^>]*>(.|[\r\n])*<\/blockquote>/gmi, '');
25   }
26   else {
27     msg = $('#' + rcmail.env.composebody).val();
28     msg = msg.replace(/^>.*$/gmi, '');
29   }
30
31   return msg;
c8bc8c 32 };
864745 33
AM 34 function rcmail_check_message(msg)
35 {
8f8bea 36   var i, rx, keywords = rcmail.get_label('keywords', 'attachment_reminder').split(",").concat([".doc", ".pdf"]);
864745 37
055c26 38   keywords = $.map(keywords, function(n) { return RegExp.escape(n); });
dffdd1 39   rx = new RegExp('(' + keywords.join('|') + ')', 'i');
864745 40
dffdd1 41   return msg.search(rx) != -1;
c8bc8c 42 };
864745 43
AM 44 function rcmail_have_attachments()
45 {
46   return rcmail.env.attachments && $('li', rcmail.gui_objects.attachmentlist).length;
c8bc8c 47 };
AM 48
49 function rcmail_attachment_reminder_dialog()
50 {
51   var buttons = {};
52
8f8bea 53   buttons[rcmail.get_label('addattachment')] = function() {
c8bc8c 54     $(this).remove();
AM 55     if (window.UI && UI.show_uploadform) // Larry skin
56       UI.show_uploadform();
57     else if (window.rcmail_ui && rcmail_ui.show_popup) // classic skin
58       rcmail_ui.show_popup('uploadmenu', true);
59   };
8f8bea 60   buttons[rcmail.get_label('send')] = function(e) {
c8bc8c 61     $(this).remove();
AM 62     rcmail.env.attachment_reminder = true;
63     rcmail.command('send', '', e);
64   };
65
66   rcmail.env.attachment_reminder = false;
8f8bea 67   rcmail.show_popup_dialog(rcmail.get_label('attachment_reminder.forgotattachment'), '', buttons);
c8bc8c 68 };
864745 69
AM 70
71 if (window.rcmail) {
72   rcmail.addEventListener('beforesend', function(evt) {
73     var msg = rcmail_get_compose_message(),
74       subject = $('#compose-subject').val();
75
c8bc8c 76     if (!rcmail.env.attachment_reminder && !rcmail_have_attachments()
AM 77       && (rcmail_check_message(msg) || rcmail_check_message(subject))
78     ) {
79       rcmail_attachment_reminder_dialog();
80       return false;
864745 81     }
AM 82   });
83 }