4 * @version @package_version@
5 * @author Aleksander Machniak <alec@alec.pl>
9 rcmail.addEventListener('init', function() {
10 if (rcmail.gui_objects.acltable) {
11 rcmail.acl_list_init();
12 // enable autocomplete on user input
13 if (rcmail.env.acl_users_source) {
14 rcmail.init_address_input_events($('#acluser'), {action:'settings/plugin.acl-autocomplete'});
16 rcmail.addEventListener('autocomplete_insert', function(e) {
17 if (e.field.id != 'acluser')
21 // get UID from the entry value
22 if (value.match(/\s*\(([^)]+)\)[, ]*$/))
24 e.field.value = value;
29 rcmail.enable_command('acl-create', 'acl-save', 'acl-cancel', 'acl-mode-switch', true);
30 rcmail.enable_command('acl-delete', 'acl-edit', false);
34 // Display new-entry form
35 rcube_webmail.prototype.acl_create = function()
40 // Display ACL edit form
41 rcube_webmail.prototype.acl_edit = function()
43 // @TODO: multi-row edition
44 var id = this.acl_list.get_single_selection();
46 this.acl_init_form(id);
50 rcube_webmail.prototype.acl_delete = function()
52 var users = this.acl_get_usernames();
54 if (users && users.length && confirm(this.get_label('acl.deleteconfirm'))) {
55 this.http_request('settings/plugin.acl', '_act=delete&_user='+urlencode(users.join(','))
56 + '&_mbox='+urlencode(this.env.mailbox),
57 this.set_busy(true, 'acl.deleting'));
62 rcube_webmail.prototype.acl_save = function()
64 var user = $('#acluser').val(), rights = '', type;
66 $(':checkbox', this.env.acl_advanced ? $('#advancedrights') : sim_ul = $('#simplerights')).map(function() {
71 if (type = $('input:checked[name=usertype]').val()) {
77 alert(this.get_label('acl.nouser'));
81 alert(this.get_label('acl.norights'));
85 this.http_request('settings/plugin.acl', '_act=save'
86 + '&_user='+urlencode(user)
88 + '&_mbox='+urlencode(this.env.mailbox)
89 + (this.acl_id ? '&_old='+this.acl_id : ''),
90 this.set_busy(true, 'acl.saving'));
94 rcube_webmail.prototype.acl_cancel = function()
100 // Update data after save (and hide form)
101 rcube_webmail.prototype.acl_update = function(o)
105 this.acl_remove_row(o.old);
106 // make sure the same ID doesn't exist
107 else if (this.env.acl[o.id])
108 this.acl_remove_row(o.id);
111 this.acl_add_row(o, true);
112 // hide autocomplete popup
115 this.acl_form.hide();
118 // Switch table display mode
119 rcube_webmail.prototype.acl_mode_switch = function(elem)
121 this.env.acl_advanced = !this.env.acl_advanced;
122 this.enable_command('acl-delete', 'acl-edit', false);
123 this.http_request('settings/plugin.acl', '_act=list'
124 + '&_mode='+(this.env.acl_advanced ? 'advanced' : 'simple')
125 + '&_mbox='+urlencode(this.env.mailbox),
126 this.set_busy(true, 'loading'));
129 // ACL table initialization
130 rcube_webmail.prototype.acl_list_init = function()
132 this.acl_list = new rcube_list_widget(this.gui_objects.acltable,
133 {multiselect:true, draggable:false, keyboard:true, toggleselect:true});
134 this.acl_list.addEventListener('select', function(o) { rcmail.acl_list_select(o); });
135 this.acl_list.addEventListener('dblclick', function(o) { rcmail.acl_list_dblclick(o); });
136 this.acl_list.addEventListener('keypress', function(o) { rcmail.acl_list_keypress(o); });
137 this.acl_list.init();
140 // ACL table row selection handler
141 rcube_webmail.prototype.acl_list_select = function(list)
143 rcmail.enable_command('acl-delete', list.selection.length > 0);
144 rcmail.enable_command('acl-edit', list.selection.length == 1);
148 // ACL table double-click handler
149 rcube_webmail.prototype.acl_list_dblclick = function(list)
154 // ACL table keypress handler
155 rcube_webmail.prototype.acl_list_keypress = function(list)
157 if (list.key_pressed == list.ENTER_KEY)
158 this.command('acl-edit');
159 else if (list.key_pressed == list.DELETE_KEY || list.key_pressed == list.BACKSPACE_KEY)
160 if (!this.acl_form || !this.acl_form.is(':visible'))
161 this.command('acl-delete');
165 rcube_webmail.prototype.acl_list_update = function(html)
167 $(this.gui_objects.acltable).html(html);
168 this.acl_list_init();
171 // Returns names of users in selected rows
172 rcube_webmail.prototype.acl_get_usernames = function()
174 var users = [], n, len, cell, row,
175 list = this.acl_list,
176 selection = list.get_selection();
178 for (n=0, len=selection.length; n<len; n++) {
179 if (this.env.acl_specials.length && $.inArray(selection[n], this.env.acl_specials) >= 0) {
180 users.push(selection[n]);
182 else if (row = list.rows[selection[n]]) {
183 cell = $('td.user', row.obj);
184 if (cell.length == 1)
185 users.push(cell.text());
192 // Removes ACL table row
193 rcube_webmail.prototype.acl_remove_row = function(id)
195 var list = this.acl_list;
198 list.clear_selection();
200 // we don't need it anymore (remove id conflict)
201 $('#rcmrow'+id).remove();
202 this.env.acl[id] = null;
204 this.enable_command('acl-delete', list.selection.length > 0);
205 this.enable_command('acl-edit', list.selection.length == 1);
208 // Adds ACL table row
209 rcube_webmail.prototype.acl_add_row = function(o, sel)
211 var n, len, ids = [], spec = [], id = o.id, list = this.acl_list,
212 items = this.env.acl_advanced ? [] : this.env.acl_items,
213 table = this.gui_objects.acltable,
214 row = $('thead > tr', table).clone();
217 $('td', row).map(function() {
218 var r, cl = this.className.replace(/^acl/, '');
220 if (items && items[cl])
224 $(this).text(o.username);
226 $(this).addClass(rcmail.acl_class(o.acl, cl)).text('');
229 row.attr('id', 'rcmrow'+id);
232 this.env.acl[id] = o.acl;
234 // sorting... (create an array of user identifiers, then sort it)
235 for (n in this.env.acl) {
236 if (this.env.acl[n]) {
237 if (this.env.acl_specials.length && $.inArray(n, this.env.acl_specials) >= 0)
244 // specials on the top
245 ids = spec.concat(ids);
248 for (n=0, len=ids.length; n<len; n++)
254 $('#rcmrow'+ids[n-1]).after(row);
259 list.insert_row(row);
262 list.select_row(o.id);
265 // Initializes and shows ACL create/edit form
266 rcube_webmail.prototype.acl_init_form = function(id)
268 var ul, row, td, val = '', type = 'user', li_elements, body = $('body'),
269 adv_ul = $('#advancedrights'), sim_ul = $('#simplerights'),
270 name_input = $('#acluser');
272 if (!this.acl_form) {
273 var fn = function () { $('input[value=user]').prop('checked', true); };
274 name_input.click(fn).keypress(fn);
277 this.acl_form = $('#aclform');
280 if (this.env.acl_advanced) {
291 // initialize form fields
292 li_elements = $(':checkbox', ul);
293 li_elements.attr('checked', false);
295 if (id && (row = this.acl_list.rows[id])) {
297 li_elements.map(function() {
299 td = $('td.'+this.id, row);
300 if (td && td.hasClass('enabled'))
304 if (!this.env.acl_specials.length || $.inArray(id, this.env.acl_specials) < 0)
305 val = $('td.user', row).text();
309 // mark read (lrs) rights by default
311 li_elements.filter(function() { return this.id.match(/^acl([lrs]|read)$/); }).prop('checked', true);
314 $('input[value='+type+']').prop('checked', true);
318 // position the form horizontally
319 var bw = body.width(), mw = this.acl_form.width();
322 this.acl_form.css({left: parseInt((bw - mw)/2)+'px'});
325 this.acl_form.show();
329 // unfocus the list, make backspace key in name input field working
330 this.acl_list.blur();
333 // Returns class name according to ACL comparision result
334 rcube_webmail.prototype.acl_class = function(acl1, acl2)
336 var i, len, found = 0;
341 for (i=0, len=acl2.length; i<len; i++)
342 if (acl1.indexOf(acl2[i]) > -1)