This repository has been archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
markasjunk2.js
156 lines (131 loc) · 6.11 KB
/
markasjunk2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* MarkAsJunk2 plugin script
*
* @licstart The following is the entire license notice for the
* JavaScript code in this file.
*
* Copyright (C) 2009-2018 Philip Weir
*
* The JavaScript code in this page is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* @licend The above is the entire license notice
* for the JavaScript code in this file.
*/
rcube_webmail.prototype.markasjunk2_mark = function(is_spam) {
var uids = this.env.uid ? [this.env.uid] : this.message_list.get_selection();
if (!uids)
return;
var lock = this.set_busy(true, 'loading');
this.http_post('plugin.markasjunk2.' + (is_spam ? 'junk' : 'not_junk'), this.selection_post_data({_uid: uids}), lock);
}
rcube_webmail.prototype.rcmail_markasjunk2_move = function(mbox, uids) {
var prev_uid = this.env.uid, a_uids = $.isArray(uids) ? uids : uids.split(",");
if (this.message_list && a_uids.length == 1 && !this.message_list.in_selection([a_uids[0]]))
this.env.uid = a_uids[0];
if (mbox)
this.move_messages(mbox);
else if (this.env.markasjunk2_permanently_remove == true)
this.permanently_remove_messages();
else
this.delete_messages();
this.env.uid = prev_uid;
}
rcube_webmail.prototype.markasjunk2_toggle_button = function() {
var spamobj = $('a.markasjunk2');
var hamobj = $('a.markasnotjunk2');
var disp = {'spam': true, 'ham': true};
if (this.env.markasjunk2_spam_only) {
disp.ham = false;
}
else if (!this.is_multifolder_listing() && this.env.markasjunk2_spam_mailbox) {
if (this.env.mailbox != this.env.markasjunk2_spam_mailbox)
disp.ham = false;
else
disp.spam = false;
}
// if only 1 button is visible make sure its the last one (for styling)
// allow for multiple instances of the buttons, eg toolbar and contextmenu
$.each(spamobj, function(i) {
var cur_spamobj = spamobj.eq(i),
cur_hamobj = hamobj.eq(i),
cur_index = spamobj.eq(i).index();
if (cur_spamobj.parent('li').length > 0) {
cur_spamobj = cur_spamobj.parent();
cur_hamobj = cur_hamobj.parent();
}
var evt_rtn = rcmail.triggerEvent('markasjunk2-update', {'objs': {'spamobj': cur_spamobj, 'hamobj': cur_hamobj}, 'disp': disp});
if (evt_rtn && evt_rtn.abort)
return;
disp = evt_rtn ? evt_rtn.disp : disp;
disp.spam ? cur_spamobj.show() : cur_spamobj.hide();
disp.ham ? cur_hamobj.show() : cur_hamobj.hide();
if (disp.spam && !disp.ham) {
if (cur_index < cur_hamobj.index()) {
cur_spamobj.insertAfter(cur_hamobj);
}
}
else if (cur_index > cur_hamobj.index()) {
cur_hamobj.insertAfter(cur_spamobj);
}
});
}
rcube_webmail.prototype.markasjunk2_is_spam_mbox = function() {
return !this.is_multifolder_listing() && this.env.mailbox == this.env.markasjunk2_spam_mailbox;
}
$(document).ready(function() {
if (window.rcmail) {
rcmail.addEventListener('init', function() {
// register command (directly enable in message view mode)
rcmail.register_command('plugin.markasjunk2.junk', function() { rcmail.markasjunk2_mark(true); }, !rcmail.markasjunk2_is_spam_mbox() && rcmail.env.uid);
rcmail.register_command('plugin.markasjunk2.not_junk', function() { rcmail.markasjunk2_mark(false); }, rcmail.env.uid);
if (rcmail.message_list) {
rcmail.message_list.addEventListener('select', function(list) {
rcmail.enable_command('plugin.markasjunk2.junk', !rcmail.markasjunk2_is_spam_mbox() && list.get_selection(false).length > 0);
rcmail.enable_command('plugin.markasjunk2.not_junk', list.get_selection(false).length > 0);
});
}
// make sure the correct icon is displayed even when there is no listupdate event
rcmail.markasjunk2_toggle_button();
});
rcmail.addEventListener('listupdate', function() { rcmail.markasjunk2_toggle_button(); });
rcmail.addEventListener('beforemoveto', function(mbox) {
if (mbox && typeof mbox === 'object')
mbox = mbox.id;
var is_spam = null;
// check if destination mbox equals junk box (and we're not already in the junk box)
if (rcmail.env.markasjunk2_move_spam && mbox && mbox == rcmail.env.markasjunk2_spam_mailbox && mbox != rcmail.env.mailbox)
is_spam = true;
// or if destination mbox equals ham box and we are in the junk box
else if (rcmail.env.markasjunk2_move_ham && mbox && mbox == rcmail.env.markasjunk2_ham_mailbox && rcmail.env.mailbox == rcmail.env.markasjunk2_spam_mailbox)
is_spam = false;
if (is_spam !== null) {
rcmail.markasjunk2_mark(is_spam);
return false;
}
});
// integration with Swipe plugin
rcmail.addEventListener('swipe-action', function(p) {
if (rcmail.env.swipe_actions[p.direction] == 'markasjunk2' && rcmail.env.markasjunk2_spam_mailbox) {
var action = {};
if (!rcmail.env.markasjunk2_spam_only && rcmail.env.mailbox == rcmail.env.markasjunk2_spam_mailbox) {
action = {
'class': 'notjunk',
'text': 'markasjunk2.markasnotjunk',
'callback': function(p) { rcmail.swipe_action_callback('plugin.markasjunk2.not_junk', null, p); }
};
}
else {
action = {
'class': 'junk',
'text': 'markasjunk2.markasjunk',
'callback': function(p) { rcmail.swipe_action_callback('plugin.markasjunk2.junk', null, p); }
};
}
return action;
}
});
}
});