forked from nansenat16/Roundcube-Plugin-LKK-AddressBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlkk_abk.js
276 lines (236 loc) · 9.08 KB
/
lkk_abk.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* lkk_abk.js
*
*/
var lkk_global_groups = {};
var lkk_global_addrs = [];
var lkk_local_groups = {};
var lkk_local_addrs = [];
(function () {
var globaladdressbook = false;
var lkk_groups = lkk_global_groups;
var lkk_addrs = lkk_global_addrs;
var Address = {
to: "",
to_array: [],
cc: "",
cc_array: [],
bcc: "",
bcc_array: [],
};
function get_address_html() {
var group_id = $('#lkk_groups').val();
var contact_ids = undefined;
if (lkk_groups[group_id] !== undefined) {
contact_ids = lkk_groups[group_id]["contact_id"];
}
// console.log(contact_ids);
var trs = '<tr><th> </th><th><input type="checkbox" group="all" value="to" id="to_all" /><label for="to_all">To</label><input type="checkbox" group="all" value="cc" id="cc_all" /><label for="cc_all">Cc</label><input type="checkbox" group="all" value="bcc" id="bcc_all" /><label for="bcc_all">Bcc</label></th></tr>';
var group_id = 0;
for (var i in lkk_addrs) {
var contact = lkk_addrs[i];
var contact_id = contact["contact_id"];
var contact_name = contact["name"];
// group member?
if (contact_ids !== undefined && contact_ids.indexOf(contact_id) < 0) {
continue;
}
var emails = "";
for (var email_index in contact["email"]) {
group_id++;
if (emails) {
emails += "<br>";
}
var email = contact["email"][email_index];
var regexp = getMatchEmailRegexp(email);
var status = "";
status = Address["to"].match(regexp) === null ? '' : 'checked="checked"';
emails += '<input type="checkbox" group="' + group_id + '" value="to" id="to_' + group_id + '" ' + status + ' /><label for="to_' + group_id + '">To</label>';
status = Address["cc"].match(regexp) === null ? '' : 'checked="checked"';
emails += '<input type="checkbox" group="' + group_id + '" value="cc" id="cc_' + group_id + '" ' + status + ' /><label for="cc_' + group_id + '">Cc</label>';
status = Address["bcc"].match(regexp) === null ? '' : 'checked="checked"';
emails += '<input type="checkbox" group="' + group_id + '" value="bcc" id="bcc_' + group_id + '" ' + status + ' /><label for="bcc_' + group_id + '">Bcc</label>';
emails += ' <span class="email" group="' + group_id + '" contact_id="' + contact_id + '" email_index="' + email_index + '">' + email + '</span>';
// console.log(emails);
}
trs += '<tr><td class="contact_name">' + contact_name + '</td><td>' + emails + '</td></tr>';
}
var table = '<table>' + trs + '</table>';
return table;
}
function getMatchEmailRegexp(email) {
return new RegExp('([\\s<,;:]+|^)(' + email.replace(".", "\\.") + ')([\\s>,;:]+|$)', 'i');
}
function get_area_html() {
var options = '';
if (globaladdressbook) {
options += '<option value="global">' + rcmail.gettext('global', 'lkk_addressbook') + '</option>';
}
options += '<option value="local">' + rcmail.gettext('local', 'lkk_addressbook') + '</option>';
return '<select id="lkk_area">' + options + '</select>';
}
function get_group_html() {
var options = '<option value="0">' + rcmail.gettext('all', 'lkk_addressbook') + '</option>';
for (var id in lkk_groups) {
// console.log(lkk_groups[id]);
var group = lkk_groups[id];
options += '<option value="' + id + '">' + group["name"] + '</option>';
}
return '<select id="lkk_groups">' + options + '</select>';
}
function get_contact(contact_id, email_index) {
for (var i in lkk_addrs) {
var contact = lkk_addrs[i];
if (contact["contact_id"] != contact_id) {
continue;
}
return { name: contact["name"], email: contact["email"][email_index] };
}
}
function lkk_init() {
// console.log("lkk_init");
if (Object.keys(lkk_global_addrs).length == 0 && Object.keys(lkk_global_groups).length == 0) {
globaladdressbook = false;
} else {
globaladdressbook = true;
}
Address["to"] = $('textarea[name="_to"]').val();
Address["to_array"] = Address["to"].split(/\s*,\s*/);
Address["cc"] = $('textarea[name="_cc"]').val();
Address["cc_array"] = Address["cc"].split(/\s*,\s*/);
Address["bcc"] = $('textarea[name="_bcc"]').val();
Address["bcc_array"] = Address["bcc"].split(/\s*,\s*/);
if ($('.lkk_addressbook')[0]) {
// $('.lkk_addressbook .group').html(get_group_html());
$('.lkk_addressbook .addrs').html(get_address_html());
$('.lkk_addressbook').show();
return;
}
if (globaladdressbook) {
lkk_groups = lkk_global_groups;
lkk_addrs = lkk_global_addrs;
} else {
lkk_groups = lkk_local_groups;
lkk_addrs = lkk_local_addrs;
}
$('body').append('<div class="lkk_addressbook">' +
'<header>' +
'<div class="toolbar">' +
'<button class="close" title="' + rcmail.gettext('close', 'lkk_addressbook') + '">×</button>' +
'<span class="area"></span>' +
'<span class="group"></span>' +
'<button class="done">' + rcmail.gettext('done', 'lkk_addressbook') + '</button>' +
'</div>' +
'</header>' +
'<div class="addrs_wrap">' +
'<div class="addrs"></div>' +
'</div>' +
'</div>');
$('.lkk_addressbook .area').html(get_area_html());
$('.lkk_addressbook .group').html(get_group_html());
$('.lkk_addressbook .addrs').html(get_address_html());
$(document).on('click', '.lkk_addressbook .close', function () {
close_dialog();
});
$(document).on('change', '#lkk_groups', function () {
$('.lkk_addressbook .addrs').html(get_address_html());
});
$(document).on('change', '#lkk_area', function () {
// console.log($('#lkk_area').val());
if ($('#lkk_area').val() == 'global') {
lkk_groups = lkk_global_groups;
lkk_addrs = lkk_global_addrs;
} else {
lkk_groups = lkk_local_groups;
lkk_addrs = lkk_local_addrs;
}
$('.lkk_addressbook .group').html(get_group_html());
$('.lkk_addressbook .addrs').html(get_address_html());
});
$(document).on('click', '.lkk_addressbook .done', function () {
$('textarea[name="_to"]').val(Address["to"]);
$('textarea[name="_cc"]').val(Address["cc"]);
$('textarea[name="_bcc"]').val(Address["bcc"]);
if (rcmail_ui && rcmail_ui.show_header_form) {
if (Address["cc"].length > 0 && $('#compose-cc').css("display") === "none") {
rcmail_ui.show_header_form('cc');
}
if (Address["bcc"].length > 0 && $('#compose-bcc').css("display") === "none") {
rcmail_ui.show_header_form('bcc');
}
} else {
console.log("'rcmail_ui.show_header_form' is not found");
}
close_dialog();
});
// to, cc, bcc
$(document).on('change', '.lkk_addressbook input[type="checkbox"]', function () {
var self = $(this);
var group = self.attr("group");
var toccbcc = self.val();
var status = self.prop('checked');
if (group === "all") {
$('.lkk_addressbook input').prop('checked', false);
$('.lkk_addressbook input[value="' + toccbcc + '"]').prop('checked', status);
$('.lkk_addressbook .email').each(function () {
var e = $(this);
var email_index = e.attr("email_index");
var contact_id = e.attr("contact_id");
// console.log('contact_id: ' + contact_id}');
var contact = get_contact(contact_id, email_index);
// console.log(contact);
set_address(contact["name"], contact["email"], "to", toccbcc == "to" ? status : false);
set_address(contact["name"], contact["email"], "cc", toccbcc == "cc" ? status : false);
set_address(contact["name"], contact["email"], "bcc", toccbcc == "bcc" ? status : false);
});
} else {
$('.lkk_addressbook input[group="' + group + '"]').prop('checked', false);
self.prop('checked', status);
var e = $('.lkk_addressbook .email[group="' + group + '"]');
var email_index = e.attr("email_index");
var contact_id = e.attr("contact_id");
// console.log('contact_id: ' + contact_id + '');
var contact = get_contact(contact_id, email_index);
// console.log(contact);
set_address(contact["name"], contact["email"], "to", toccbcc == "to" ? status : false);
set_address(contact["name"], contact["email"], "cc", toccbcc == "cc" ? status : false);
set_address(contact["name"], contact["email"], "bcc", toccbcc == "bcc" ? status : false);
}
});
}
function close_dialog() {
$('.lkk_addressbook').hide();
}
function set_address(name, email, toccbcc, status) {
var regexp = getMatchEmailRegexp(email)
if (Address[toccbcc].match(regexp) === null) {
// console.log("not found");
if (status) {
// add address
Address[toccbcc + '_array'].push('' + name + ' <' + email + '>');
}
} else {
// console.log("found");
if (!status) {
// console.log("remove");
// remove address
for (var key in Address[toccbcc + '_array']) {
if (Address[toccbcc + '_array'][key].match(regexp) === null) {
continue;
}
// console.log('!!! ' + Address[toccbcc + '_array'][key]);
Address[toccbcc + '_array'].splice(key, 1);
// delete Address[toccbcc + '_array'][key];
}
}
}
// console.log(Address[toccbcc + '_array']);
Address[toccbcc] = (Address[toccbcc + '_array'].join(", ").replace(/,[\s,]*/g, ", ").replace(/(^[\s,]*|[\s,]*$)/g, "") + ", ").replace(/^[\s,]*$/, "");
// console.log(Address[toccbcc]);
}
rcmail.addEventListener('init', function (event) {
$(document).on('click', '.title label', function () {
lkk_init();
});
});
})();