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]);
183 row = list.rows[selection[n]].obj;
184 cell = $('td.user', row);
185 if (cell.length == 1)
186 users.push(cell.text());
193 // Removes ACL table row
194 rcube_webmail.prototype.acl_remove_row = function(id)
196 this.acl_list.remove_row(id);
197 // we don't need it anymore (remove id conflict)
198 $('#rcmrow'+id).remove();
199 this.env.acl[id] = null;
202 // Adds ACL table row
203 rcube_webmail.prototype.acl_add_row = function(o, sel)
205 var n, len, ids = [], spec = [], id = o.id, list = this.acl_list,
206 items = this.env.acl_advanced ? [] : this.env.acl_items,
207 table = this.gui_objects.acltable,
208 row = $('thead > tr', table).clone();
211 $('td', row).map(function() {
212 var r, cl = this.className.replace(/^acl/, '');
214 if (items && items[cl])
218 $(this).text(o.username);
220 $(this).addClass(rcmail.acl_class(o.acl, cl)).text('');
223 row.attr('id', 'rcmrow'+id);
226 this.env.acl[id] = o.acl;
228 // sorting... (create an array of user identifiers, then sort it)
229 for (n in this.env.acl) {
230 if (this.env.acl[n]) {
231 if (this.env.acl_specials.length && $.inArray(n, this.env.acl_specials) >= 0)
238 // specials on the top
239 ids = spec.concat(ids);
242 for (n=0, len=ids.length; n<len; n++)
248 $('#rcmrow'+ids[n-1]).after(row);
253 list.insert_row(row);
256 list.select_row(o.id);
259 // Initializes and shows ACL create/edit form
260 rcube_webmail.prototype.acl_init_form = function(id)
262 var ul, row, val = '', type = 'user', li_elements, body = $('body'),
263 adv_ul = $('#advancedrights'), sim_ul = $('#simplerights'),
264 name_input = $('#acluser');
266 if (!this.acl_form) {
267 var fn = function () { $('input[value=user]').prop('checked', true); };
268 name_input.click(fn).keypress(fn);
271 this.acl_form = $('#aclform');
274 if (this.env.acl_advanced) {
285 // initialize form fields
286 li_elements = $(':checkbox', ul);
287 li_elements.attr('checked', false);
290 row = this.acl_list.rows[id].obj;
291 li_elements.map(function() {
292 var val = this.value, td = $('td.'+this.id, row);
293 if (td && td.hasClass('enabled'))
297 if (!this.env.acl_specials.length || $.inArray(id, this.env.acl_specials) < 0)
298 val = $('td.user', row).text();
304 $('input[value='+type+']').prop('checked', true);
308 // position the form horizontally
309 var bw = body.width(), mw = this.acl_form.width();
312 this.acl_form.css({left: parseInt((bw - mw)/2)+'px'});
315 this.acl_form.show();
320 // Returns class name according to ACL comparision result
321 rcube_webmail.prototype.acl_class = function(acl1, acl2)
323 var i, len, found = 0;
328 for (i=0, len=acl2.length; i<len; i++)
329 if (acl1.indexOf(acl2[i]) > -1)