initial commit, for roundcube v0.7.2-9
[roundcube/plugins/junk_keyword.git] / junk_keyword.js
1 function junk_keyword_message_set(junk_uids, nonjunk_uids) {
2 var i;
3 for (i=0; i<junk_uids.length; i++) {
4 var uid = junk_uids[i];
5 var message = rcmail.env.messages[uid];
6 var row = rcmail.message_list.rows[uid];
7 var rowobj = $(row.junk_icon);
8 if (!rowobj.hasClass('junk')) {
9 rowobj.addClass('junk');
10 message.flags.junk = true;
11 }
12 rcmail.set_message(uid, 'unread', false);
13 rcmail.update_thread_root(uid, 'read');
14 if (rcmail.env.unread_counts[rcmail.env.mailbox]) {
15 rcmail.env.unread_counts[rcmail.env.mailbox] -= 1;
16 rcmail.set_unread_count(rcmail.env.mailbox
17 , rcmail.env.unread_counts[rcmail.env.mailbox]
18 , rcmail.env.mailbox == 'INBOX');
19 }
20 if (rcmail.env.preview_pane_mark_read > 0)
21 rcmail.http_post('mark', '_uid='+id+'&_flag=read&_quiet=1');
22 }
23 for (i=0; i<nonjunk_uids.length; i++) {
24 var uid = nonjunk_uids[i];
25 var message = rcmail.env.messages[uid];
26 var row = rcmail.message_list.rows[uid];
27 var rowobj = $(row.junk_icon);
28 if (rowobj.hasClass('junk')) {
29 rowobj.removeClass('junk');
30 message.flags.junk = false;
31 }
32 }
33 var lock = rcmail.set_busy(true, 'loading');
34 var str_junk_uids = junk_uids.join(',');
35 var str_nonjunk_uids = nonjunk_uids.join(',');
36 rcmail.http_request('plugin.junk_keyword.set_keywords'
37 , '&_junk_uids=' + str_junk_uids
38 + '&_nonjunk_uids=' + str_nonjunk_uids
39 + '&_mbox=' + urlencode(rcmail.env.mailbox)
40 //+ '&_search='+ (this.env.search_request ? this.env.search_request : '')
41 , lock);
42 };
43 function junk_keyword_message_toggle(uid) {
44 var uids = [], junk_uids = [], nonjunk_uids = []
45 , i, id
46 , list = rcmail.message_list;
47
48 if (uid)
49 uids[0] = uid;
50 else if (rcmail.env.uid)
51 uids[0] = rcmail.env.uid;
52 else if (list)
53 uids = list.get_selection();
54
55 list.focus();
56 for (i=0; i<uids.length; i++) {
57 id = uids[i];
58 if (list.rows[id].flags.junk)
59 nonjunk_uids.push(id);
60 else
61 junk_uids.push(id);
62 }
63 junk_keyword_message_set(junk_uids, nonjunk_uids);
64 };
65 function junk_keyword_icon_toggle(props) {
66 if (props && !props._row)
67 return false;
68 junk_keyword_message_toggle(props._row.uid);
69 return true;
70 };
71
72 function junk_keyword_menu() {
73 $('#rcmjunk_keyword').html(rcmail.env.junk_keywordicon);
74 };
75 if (window.rcmail) {
76 rcmail.addEventListener('init', function(evt) {
77 rcmail.register_command('plugin.junk_keyword.icon_toggle', junk_keyword_icon_toggle, true);
78 });
79
80 rcmail.addEventListener('listupdate','junk_keyword_menu');
81 rcmail.addEventListener('insertrow', function(evt) {
82 var row = evt.row, uid = evt.uid;
83 var message = rcmail.env.messages[uid];
84 //if (message.flags) {
85 // var rowobj = $(row.obj);
86 // if (message.flags.junk)
87 // rowobj.addClass('junk');
88 // }
89 if ((found = $.inArray('junk_keyword', rcmail.env.coltypes)) >= 0) {
90 rcmail.set_env('junk_keyword_col', found);
91 }
92 if (rcmail.env.junk_keyword_col != null) {
93 var junk_icon = 'rcmjunk_keyword' + row.uid;
94 if (row.junk_icon = document.getElementById(junk_icon)) {
95 row.junk_icon._row = row.obj;
96 row.junk_icon.onmousedown = function(e) {
97 rcmail.command('plugin.junk_keyword.icon_toggle', this);
98 rcube_event.cancel(e);
99 };
100 }
101 }
102 });
103 };
104 $(document).ready(function() {
105 junk_keyword_menu();
106 var li = '<li><input type="checkbox" name="list_col[]" value="junk_keyword" id="cols_junk_keyword" /><label for="cols_junk_keyword">'+rcmail.get_label('junk_keyword.junk_keyword')+'</label></li>';
107 $("#listmenu fieldset ul input#cols_threads").parent().after(li);
108 });