2 +-----------------------------------------------------------------------+
3 | RoundCube Webmail Client Script |
5 | This file is part of the RoundCube Webmail client |
6 | Copyright (C) 2005-2009, RoundCube Dev, - Switzerland |
7 | Licensed under the GNU GPL |
9 +-----------------------------------------------------------------------+
10 | Authors: Thomas Bruederli <roundcube@gmail.com> |
11 | Charles McNulty <charles@charlesmcnulty.com> |
12 +-----------------------------------------------------------------------+
13 | Requires: jquery.js, common.js, list.js |
14 +-----------------------------------------------------------------------+
16 $Id: app.js 2889 2009-08-29 18:41:17Z alec $
20 function rcube_webmail()
22 this.env = new Object();
23 this.labels = new Object();
24 this.buttons = new Object();
25 this.buttons_sel = new Object();
26 this.gui_objects = new Object();
27 this.gui_containers = new Object();
28 this.commands = new Object();
29 this.command_handlers = new Object();
30 this.onloads = new Array();
32 // create protected reference to myself
36 // webmail client settings
37 this.dblclick_time = 500;
38 this.message_time = 3000;
40 this.identifier_expr = new RegExp('[^0-9a-z\-_]', 'gi');
42 // mimetypes supported by the browser (default settings)
43 this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
44 'image/jpeg', 'image/gif', 'image/png',
45 'application/x-javascript', 'application/pdf',
46 'application/x-shockwave-flash');
48 // default environment vars
49 this.env.keep_alive = 60; // seconds
50 this.env.request_timeout = 180; // seconds
51 this.env.draft_autosave = 0; // seconds
52 this.env.comm_path = './';
53 this.env.bin_path = './bin/';
54 this.env.blankpage = 'program/blank.gif';
56 // set jQuery ajax options
57 jQuery.ajaxSetup({ cache:false,
58 error:function(request, status, err){ ref.http_error(request, status, err); },
59 beforeSend:function(xmlhttp){ xmlhttp.setRequestHeader('X-RoundCube-Request', ref.env.request_token); }
62 // set environment variable(s)
63 this.set_env = function(p, value)
65 if (p != null && typeof(p) == 'object' && !value)
72 // add a localized label to the client environment
73 this.add_label = function(key, value)
75 this.labels[key] = value;
78 // add a button to the button list
79 this.register_button = function(command, id, type, act, sel, over)
81 if (!this.buttons[command])
82 this.buttons[command] = new Array();
84 var button_prop = {id:id, type:type};
85 if (act) button_prop.act = act;
86 if (sel) button_prop.sel = sel;
87 if (over) button_prop.over = over;
89 this.buttons[command][this.buttons[command].length] = button_prop;
92 // register a specific gui object
93 this.gui_object = function(name, id)
95 this.gui_objects[name] = id;
98 // register a container object
99 this.gui_container = function(name, id)
101 this.gui_containers[name] = id;
104 // add a GUI element (html node) to a specified container
105 this.add_element = function(elm, container)
107 if (this.gui_containers[container] && this.gui_containers[container].jquery)
108 this.gui_containers[container].append(elm);
111 // register an external handler for a certain command
112 this.register_command = function(command, callback, enable)
114 this.command_handlers[command] = callback;
117 this.enable_command(command, true);
120 // execute the given script on load
121 this.add_onload = function(f)
123 this.onloads[this.onloads.length] = f;
126 // initialize webmail client
127 this.init = function()
130 this.task = this.env.task;
133 if (!bw.dom || !bw.xmlhttp_test())
135 this.goto_url('error', '_code=0x199');
139 // find all registered gui containers
140 for (var n in this.gui_containers)
141 this.gui_containers[n] = $('#'+this.gui_containers[n]);
143 // find all registered gui objects
144 for (var n in this.gui_objects)
145 this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
147 // init registered buttons
150 // tell parent window that this frame is loaded
151 if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
152 parent.rcmail.set_busy(false);
154 // enable general commands
155 this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
157 if (this.env.permaurl)
158 this.enable_command('permaurl', true);
163 if (this.gui_objects.messagelist)
165 this.message_list = new rcube_list_widget(this.gui_objects.messagelist, {multiselect:true, draggable:true, keyboard:true, dblclick_time:this.dblclick_time});
166 this.message_list.row_init = function(o){ p.init_message_row(o); };
167 this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
168 this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
169 this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
170 this.message_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
171 this.message_list.addEventListener('dragmove', function(e){ p.drag_move(e); });
172 this.message_list.addEventListener('dragend', function(e){ p.drag_end(e); });
173 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
175 this.message_list.init();
176 this.enable_command('toggle_status', 'toggle_flag', true);
178 if (this.gui_objects.mailcontframe)
179 this.gui_objects.mailcontframe.onmousedown = function(e){ return p.click_on_list(e); };
181 this.message_list.focus();
184 if (this.env.coltypes)
185 this.set_message_coltypes(this.env.coltypes);
187 // enable mail commands
188 this.enable_command('list', 'checkmail', 'compose', 'add-contact', 'search', 'reset-search', 'collapse-folder', true);
190 if (this.env.search_text != null && document.getElementById('quicksearchbox') != null)
191 document.getElementById('quicksearchbox').value = this.env.search_text;
193 if (this.env.action=='show' || this.env.action=='preview')
195 this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete',
196 'open', 'mark', 'edit', 'viewsource', 'download', 'print', 'load-attachment', 'load-headers', true);
198 if (this.env.next_uid)
200 this.enable_command('nextmessage', true);
201 this.enable_command('lastmessage', true);
203 if (this.env.prev_uid)
205 this.enable_command('previousmessage', true);
206 this.enable_command('firstmessage', true);
209 if (this.env.blockedobjects)
211 if (this.gui_objects.remoteobjectsmsg)
212 this.gui_objects.remoteobjectsmsg.style.display = 'block';
213 this.enable_command('load-images', 'always-load', true);
217 if (this.env.trash_mailbox && this.env.mailbox != this.env.trash_mailbox)
218 this.set_alttext('delete', 'movemessagetotrash');
220 // make preview/message frame visible
221 if (this.env.action == 'preview' && this.env.framed && parent.rcmail)
223 this.enable_command('compose', 'add-contact', false);
224 parent.rcmail.show_contentframe(true);
227 if (this.env.action=='compose')
229 this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
230 if (this.env.spellcheck)
232 this.env.spellcheck.spelling_state_observer = function(s){ ref.set_spellcheck_state(s); };
233 this.set_spellcheck_state('ready');
234 if ($("input[name='_is_html']").val() == '1')
235 this.display_spellcheck_controls(false);
237 if (this.env.drafts_mailbox)
238 this.enable_command('savedraft', true);
240 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
242 // init message compose form
243 this.init_messageform();
246 if (this.env.messagecount)
247 this.enable_command('select-all', 'select-none', 'expunge', true);
249 if (this.purge_mailbox_test())
250 this.enable_command('purge', true);
252 this.set_page_buttons();
254 // show printing dialog
255 if (this.env.action=='print')
258 // get unread count for each mailbox
259 if (this.gui_objects.mailboxlist)
261 this.env.unread_counts = {};
262 this.gui_objects.folderlist = this.gui_objects.mailboxlist;
263 this.http_request('getunread', '');
266 // ask user to send MDN
267 if (this.env.mdn_request && this.env.uid)
269 var mdnurl = '_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox);
270 if (confirm(this.get_label('mdnrequest')))
271 this.http_post('sendmdn', mdnurl);
273 this.http_post('mark', mdnurl+'&_flag=mdnsent');
280 if (this.gui_objects.contactslist)
282 this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
283 this.contact_list.row_init = function(row){ p.triggerEvent('insertrow', { cid:row.uid, row:row }); };
284 this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
285 this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
286 this.contact_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
287 this.contact_list.addEventListener('dragmove', function(e){ p.drag_move(e); });
288 this.contact_list.addEventListener('dragend', function(e){ p.drag_end(e); });
289 this.contact_list.init();
292 this.contact_list.highlight_row(this.env.cid);
294 if (this.gui_objects.contactslist.parentNode)
296 this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
297 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
300 this.contact_list.focus();
302 this.gui_objects.folderlist = this.gui_objects.contactslist;
305 this.set_page_buttons();
307 if (this.env.address_sources && this.env.address_sources[this.env.source] && !this.env.address_sources[this.env.source].readonly)
308 this.enable_command('add', true);
311 this.enable_command('show', 'edit', true);
313 if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
314 this.enable_command('save', true);
316 this.enable_command('search', 'reset-search', 'moveto', 'import', true);
318 if (this.contact_list && this.contact_list.rowcount > 0)
319 this.enable_command('export', true);
321 this.enable_command('list', true);
326 this.enable_command('preferences', 'identities', 'save', 'folders', true);
328 if (this.env.action=='identities') {
329 this.enable_command('add', this.env.identities_level < 2);
331 else if (this.env.action=='edit-identity' || this.env.action=='add-identity') {
332 this.enable_command('add', this.env.identities_level < 2);
333 this.enable_command('save', 'delete', 'edit', true);
335 else if (this.env.action=='folders')
336 this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', true);
338 if (this.gui_objects.identitieslist)
340 this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
341 this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
342 this.identity_list.init();
343 this.identity_list.focus();
346 this.identity_list.highlight_row(this.env.iid);
348 else if (this.gui_objects.sectionslist)
350 this.sections_list = new rcube_list_widget(this.gui_objects.sectionslist, {multiselect:false, draggable:false, keyboard:false});
351 this.sections_list.addEventListener('select', function(o){ p.section_select(o); });
352 this.sections_list.init();
353 this.sections_list.focus();
354 this.sections_list.select('general'); // open first section by default
356 else if (this.gui_objects.subscriptionlist)
357 this.init_subscription_list();
362 var input_user = $('#rcmloginuser');
363 input_user.bind('keyup', function(e){ return rcmail.login_user_keyup(e); });
365 if (input_user.val() == '')
368 $('#rcmloginpwd').focus();
370 // detect client timezone
371 $('#rcmlogintz').val(new Date().getTimezoneOffset() / -60);
373 this.enable_command('login', true);
380 // flag object as complete
384 if (this.pending_message)
385 this.display_message(this.pending_message[0], this.pending_message[1]);
387 // map implicit containers
388 if (this.gui_objects.folderlist)
389 this.gui_containers.foldertray = $(this.gui_objects.folderlist);
391 // trigger init event hook
392 this.triggerEvent('init', { task:this.task, action:this.env.action });
394 // execute all foreign onload scripts
396 for (var i=0; i<this.onloads.length; i++)
398 if (typeof(this.onloads[i]) == 'string')
399 eval(this.onloads[i]);
400 else if (typeof(this.onloads[i]) == 'function')
404 // start keep-alive interval
405 this.start_keepalive();
408 // start interval for keep-alive/recent_check signal
409 this.start_keepalive = function()
411 if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.mailboxlist)
412 this._int = setInterval(function(){ ref.check_for_recent(false); }, this.env.keep_alive * 1000);
413 else if (this.env.keep_alive && !this.env.framed && this.task!='login')
414 this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
417 this.init_message_row = function(row)
420 if (uid && this.env.messages[uid])
422 row.deleted = this.env.messages[uid].deleted ? true : false;
423 row.unread = this.env.messages[uid].unread ? true : false;
424 row.replied = this.env.messages[uid].replied ? true : false;
425 row.flagged = this.env.messages[uid].flagged ? true : false;
426 row.forwarded = this.env.messages[uid].forwarded ? true : false;
429 // set eventhandler to message icon
430 if (row.icon = row.obj.getElementsByTagName('td')[0].getElementsByTagName('img')[0])
433 row.icon.id = 'msgicn_'+row.uid;
434 row.icon._row = row.obj;
435 row.icon.onmousedown = function(e) { p.command('toggle_status', this); };
438 // global variable 'flagged_col' may be not defined yet
439 if (!this.env.flagged_col && this.env.coltypes)
442 if((found = find_in_array('flag', this.env.coltypes)) >= 0)
443 this.set_env('flagged_col', found+1);
446 // set eventhandler to flag icon, if icon found
447 if (this.env.flagged_col && (row.flagged_icon = row.obj.getElementsByTagName('td')[this.env.flagged_col].getElementsByTagName('img')[0]))
450 row.flagged_icon.id = 'flaggedicn_'+row.uid;
451 row.flagged_icon._row = row.obj;
452 row.flagged_icon.onmousedown = function(e) { p.command('toggle_flag', this); };
455 this.triggerEvent('insertrow', { uid:uid, row:row });
458 // init message compose form: set focus and eventhandlers
459 this.init_messageform = function()
461 if (!this.gui_objects.messageform)
464 //this.messageform = this.gui_objects.messageform;
465 var input_from = $("[name='_from']");
466 var input_to = $("[name='_to']");
467 var input_subject = $("input[name='_subject']");
468 var input_message = $("[name='_message']").get(0);
470 // init live search events
471 this.init_address_input_events(input_to);
472 this.init_address_input_events($("[name='_cc']"));
473 this.init_address_input_events($("[name='_bcc']"));
475 // add signature according to selected identity
476 if (input_from.attr('type') == 'select-one' && $("input[name='_draft_saveid']").val() == ''
477 && $("input[name='_is_html']").val() != '1') { // if we have HTML editor, signature is added in callback
478 this.change_identity(input_from[0]);
481 if (input_to.val() == '')
483 else if (input_subject.val() == '')
484 input_subject.focus();
485 else if (input_message)
486 input_message.focus();
488 // get summary of all field values
489 this.compose_field_hash(true);
491 // start the auto-save timer
492 this.auto_save_start();
495 this.init_address_input_events = function(obj)
497 var handler = function(e){ return ref.ksearch_keypress(e,this); };
498 obj.bind((bw.safari || bw.ie ? 'keydown' : 'keypress'), handler);
499 obj.attr('autocomplete', 'off');
503 /*********************************************************/
504 /********* client command interface *********/
505 /*********************************************************/
507 // execute a specific command on the web client
508 this.command = function(command, props, obj)
516 // command not supported or allowed
517 if (!this.commands[command])
519 // pass command to parent window
520 if (this.env.framed && parent.rcmail && parent.rcmail.command)
521 parent.rcmail.command(command, props);
526 // check input before leaving compose step
527 if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
529 if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
533 // process external commands
534 if (typeof this.command_handlers[command] == 'function')
536 var ret = this.command_handlers[command](props, obj);
537 return ret !== null ? ret : (obj ? false : true);
539 else if (typeof this.command_handlers[command] == 'string')
541 var ret = window[this.command_handlers[command]](props, obj);
542 return ret !== null ? ret : (obj ? false : true);
545 // trigger plugin hook
546 var event_ret = this.triggerEvent('before'+command, props);
547 if (typeof event_ret != 'undefined') {
548 // abort if one the handlers returned false
549 if (event_ret === false)
555 // process internal command
559 if (this.gui_objects.loginform)
560 this.gui_objects.loginform.submit();
563 // commands to switch task
568 this.switch_task(command);
572 if (obj && obj.href && obj.target)
574 else if (this.env.permaurl)
575 parent.location.href = this.env.permaurl;
580 if (uid = this.get_single_uid())
582 obj.href = '?_task='+this.env.task+'&_action=show&_mbox='+urlencode(this.env.mailbox)+'&_uid='+uid;
587 // misc list commands
589 if (this.task=='mail')
591 if (this.env.search_request<0 || (props != '' && (this.env.search_request && props != this.env.mailbox)))
592 this.reset_qsearch();
594 this.list_mailbox(props);
596 if (this.env.trash_mailbox)
597 this.set_alttext('delete', this.env.mailbox != this.env.trash_mailbox ? 'movemessagetotrash' : 'deletemessage');
599 else if (this.task=='addressbook')
601 if (this.env.search_request<0 || (this.env.search_request && props != this.env.source))
602 this.reset_qsearch();
604 this.list_contacts(props);
605 this.enable_command('add', (this.env.address_sources && !this.env.address_sources[props].readonly));
611 this.load_headers(obj);
616 var sort_order, sort_col = props;
618 if (this.env.sort_col==sort_col)
619 sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
623 // set table header class
624 $('#rcm'+this.env.sort_col).removeClass('sorted'+(this.env.sort_order.toUpperCase()));
625 $('#rcm'+sort_col).addClass('sorted'+sort_order);
627 // save new sort properties
628 this.env.sort_col = sort_col;
629 this.env.sort_order = sort_order;
631 // reload message list
632 this.list_mailbox('', '', sort_col+'_'+sort_order);
636 this.list_page('next');
640 this.list_page('last');
644 this.list_page('prev');
648 this.list_page('first');
652 if (this.env.messagecount)
653 this.expunge_mailbox(this.env.mailbox);
657 case 'empty-mailbox':
658 if (this.env.messagecount)
659 this.purge_mailbox(this.env.mailbox);
663 // common commands used in multiple tasks
665 if (this.task=='mail')
667 var uid = this.get_single_uid();
668 if (uid && (!this.env.uid || uid != this.env.uid))
670 if (this.env.mailbox == this.env.drafts_mailbox)
671 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
673 this.show_message(uid);
676 else if (this.task=='addressbook')
678 var cid = props ? props : this.get_single_cid();
679 if (cid && !(this.env.action=='show' && cid==this.env.cid))
680 this.load_contact(cid, 'show');
685 if (this.task=='addressbook')
686 this.load_contact(0, 'add');
687 else if (this.task=='settings')
689 this.identity_list.clear_selection();
690 this.load_identity(0, 'add-identity');
696 if (this.task=='addressbook' && (cid = this.get_single_cid()))
697 this.load_contact(cid, 'edit');
698 else if (this.task=='settings' && props)
699 this.load_identity(props, 'edit-identity');
700 else if (this.task=='mail' && (cid = this.get_single_uid())) {
701 var url = (this.env.mailbox == this.env.drafts_mailbox) ? '_draft_uid=' : '_uid=';
702 this.goto_url('compose', url+cid+'&_mbox='+urlencode(this.env.mailbox), true);
706 case 'save-identity':
708 if (this.gui_objects.editform)
710 var input_pagesize = $("input[name='_pagesize']");
711 var input_name = $("input[name='_name']");
712 var input_email = $("input[name='_email']");
715 if (input_pagesize.length && isNaN(parseInt(input_pagesize.val())))
717 alert(this.get_label('nopagesizewarning'));
718 input_pagesize.focus();
721 // contacts/identities
724 if (input_name.length && input_name.val() == '')
726 alert(this.get_label('nonamewarning'));
730 else if (input_email.length && !rcube_check_email(input_email.val()))
732 alert(this.get_label('noemailwarning'));
738 this.gui_objects.editform.submit();
744 if (this.task=='mail')
745 this.delete_messages();
747 else if (this.task=='addressbook')
748 this.delete_contacts();
749 // user settings task
750 else if (this.task=='settings')
751 this.delete_identity();
755 // mail task commands
758 if (this.task == 'mail')
759 this.move_messages(props);
760 else if (this.task == 'addressbook' && this.drag_active)
761 this.copy_contact(null, props);
766 this.mark_message(props);
769 case 'toggle_status':
770 if (props && !props._row)
778 uid = props._row.uid;
780 // toggle read/unread
781 if (this.message_list.rows[uid].deleted) {
783 } else if (!this.message_list.rows[uid].unread)
787 this.mark_message(flag, uid);
791 if (props && !props._row)
795 var flag = 'flagged';
799 uid = props._row.uid;
800 // toggle flagged/unflagged
801 if (this.message_list.rows[uid].flagged)
804 this.mark_message(flag, uid);
808 if (this.env.uid && this.env.sender) {
809 this.add_contact(urlencode(this.env.sender));
810 window.setTimeout(function(){ ref.command('load-images'); }, 300);
816 this.show_message(this.env.uid, true, this.env.action=='preview');
819 case 'load-attachment':
820 var qstring = '_mbox='+urlencode(this.env.mailbox)+'&_uid='+this.env.uid+'&_part='+props.part;
822 // open attachment in frame if it's of a supported mimetype
823 if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
825 if (props.mimetype == 'text/html')
826 qstring += '&_safe=1';
827 this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
828 if (this.attachment_win)
830 window.setTimeout(function(){ ref.attachment_win.focus(); }, 10);
835 this.goto_url('get', qstring+'&_download=1', false);
839 if (props == 'invert')
840 this.message_list.invert_selection();
842 this.message_list.select_all(props);
846 this.message_list.clear_selection();
850 if (this.env.next_uid)
851 this.show_message(this.env.next_uid, false, this.env.action=='preview');
855 if (this.env.last_uid)
856 this.show_message(this.env.last_uid);
859 case 'previousmessage':
860 if (this.env.prev_uid)
861 this.show_message(this.env.prev_uid, false, this.env.action=='preview');
865 if (this.env.first_uid)
866 this.show_message(this.env.first_uid);
870 this.check_for_recent(true);
874 var url = this.env.comm_path+'&_action=compose';
876 if (this.task=='mail')
878 url += '&_mbox='+urlencode(this.env.mailbox);
880 if (this.env.mailbox==this.env.drafts_mailbox)
883 if (uid = this.get_single_uid())
884 url += '&_draft_uid='+uid;
887 url += '&_to='+urlencode(props);
889 // modify url if we're in addressbook
890 else if (this.task=='addressbook')
892 // switch to mail compose step directly
893 if (props && props.indexOf('@') > 0)
895 url = this.get_task_url('mail', url);
896 this.redirect(url + '&_to='+urlencode(props));
900 // use contact_id passed as command parameter
901 var a_cids = new Array();
903 a_cids[a_cids.length] = props;
904 // get selected contacts
905 else if (this.contact_list)
907 var selection = this.contact_list.get_selection();
908 for (var n=0; n<selection.length; n++)
909 a_cids[a_cids.length] = selection[n];
913 this.http_request('mailto', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source), true);
918 // don't know if this is necessary...
919 url = url.replace(/&_framed=1/, "");
925 if (window.tinyMCE && tinyMCE.get(this.env.composebody)) {
926 tinyMCE.execCommand('mceSpellCheck', true);
928 else if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready) {
929 this.env.spellcheck.spellCheck();
930 this.set_spellcheck_state('checking');
935 // Reset the auto-save timer
936 self.clearTimeout(this.save_timer);
938 if (!this.gui_objects.messageform)
941 // if saving Drafts is disabled in main.inc.php
942 // or if compose form did not change
943 if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
946 this.set_busy(true, 'savingmessage');
947 var form = this.gui_objects.messageform;
948 form.target = "savetarget";
949 form._draft.value = '1';
954 if (!this.gui_objects.messageform)
957 if (!this.check_compose_input())
960 // Reset the auto-save timer
961 self.clearTimeout(this.save_timer);
963 // all checks passed, send message
964 this.set_busy(true, 'sendingmessage');
965 var form = this.gui_objects.messageform;
966 form.target = "savetarget";
967 form._draft.value = '';
970 // clear timeout (sending could take longer)
971 clearTimeout(this.request_timer);
974 case 'add-attachment':
975 this.show_attachment_form(true);
977 case 'send-attachment':
978 // Reset the auto-save timer
979 self.clearTimeout(this.save_timer);
981 this.upload_file(props)
984 case 'remove-attachment':
985 this.remove_attachment(props);
991 if (uid = this.get_single_uid())
992 this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
997 if (uid = this.get_single_uid())
998 this.goto_url('compose', '_forward_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1003 if (uid = this.get_single_uid())
1005 ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
1008 window.setTimeout(function(){ ref.printwin.focus(); }, 20);
1009 if (this.env.action != 'show')
1010 this.mark_message('read', uid);
1017 if (uid = this.get_single_uid())
1019 ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox));
1021 window.setTimeout(function(){ ref.sourcewin.focus(); }, 20);
1027 if (uid = this.get_single_uid())
1028 this.goto_url('viewsource', '&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+'&_save=1');
1032 this.add_contact(props);
1037 if (!props && this.gui_objects.qsearchbox)
1038 props = this.gui_objects.qsearchbox.value;
1041 this.qsearch(props);
1045 // reset quicksearch
1046 case 'reset-search':
1047 var s = this.env.search_request;
1048 this.reset_qsearch();
1050 if (s && this.env.mailbox)
1051 this.list_mailbox(this.env.mailbox);
1052 else if (s && this.task == 'addressbook')
1053 this.list_contacts(this.env.source);
1057 if (this.env.action == 'import' && this.gui_objects.importform) {
1058 var file = document.getElementById('rcmimportfile');
1059 if (file && !file.value) {
1060 alert(this.get_label('selectimportfile'));
1063 this.gui_objects.importform.submit();
1064 this.set_busy(true, 'importwait');
1065 this.lock_form(this.gui_objects.importform, true);
1068 this.goto_url('import');
1072 if (this.contact_list.rowcount > 0) {
1073 var add_url = (this.env.source ? '_source='+urlencode(this.env.source)+'&' : '');
1074 if (this.env.search_request)
1075 add_url += '_search='+this.env.search_request;
1077 this.goto_url('export', add_url);
1081 // collapse/expand folder
1082 case 'collapse-folder':
1084 this.collapse_folder(props);
1087 // user settings commands
1093 this.goto_url('identities');
1096 case 'delete-identity':
1097 this.delete_identity();
1100 this.goto_url('folders');
1104 this.subscribe_folder(props);
1108 this.unsubscribe_folder(props);
1111 case 'create-folder':
1112 this.create_folder(props);
1115 case 'rename-folder':
1116 this.rename_folder(props);
1119 case 'delete-folder':
1120 this.delete_folder(props);
1125 this.triggerEvent('after'+command, props);
1127 return obj ? false : true;
1130 // set command enabled or disabled
1131 this.enable_command = function()
1133 var args = arguments;
1134 if(!args.length) return -1;
1137 var enable = args[args.length-1];
1139 for(var n=0; n<args.length-1; n++)
1142 this.commands[command] = enable;
1143 this.set_button(command, (enable ? 'act' : 'pas'));
1148 // lock/unlock interface
1149 this.set_busy = function(a, message)
1153 var msg = this.get_label(message);
1157 this.display_message(msg, 'loading', true);
1160 this.hide_message();
1163 //document.body.style.cursor = a ? 'wait' : 'default';
1165 if (this.gui_objects.editform)
1166 this.lock_form(this.gui_objects.editform, a);
1168 // clear pending timer
1169 if (this.request_timer)
1170 clearTimeout(this.request_timer);
1172 // set timer for requests
1173 if (a && this.env.request_timeout)
1174 this.request_timer = window.setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
1177 // return a localized string
1178 this.get_label = function(name, domain)
1180 if (domain && this.labels[domain+'.'+name])
1181 return this.labels[domain+'.'+name];
1182 else if (this.labels[name])
1183 return this.labels[name];
1188 // alias for convenience reasons
1189 this.gettext = this.get_label;
1191 // switch to another application task
1192 this.switch_task = function(task)
1194 if (this.task===task && task!='mail')
1197 var url = this.get_task_url(task);
1199 url += '&_mbox=INBOX';
1204 this.get_task_url = function(task, url)
1207 url = this.env.comm_path;
1209 return url.replace(/_task=[a-z]+/, '_task='+task);
1212 // called when a request timed out
1213 this.request_timed_out = function()
1215 this.set_busy(false);
1216 this.display_message('Request timed out!', 'error');
1219 this.reload = function(delay)
1221 if (this.env.framed && parent.rcmail)
1222 parent.rcmail.reload(delay);
1224 window.setTimeout(function(){ rcmail.reload(); }, delay);
1225 else if (window.location)
1226 location.href = this.env.comm_path;
1230 /*********************************************************/
1231 /********* event handling methods *********/
1232 /*********************************************************/
1234 this.doc_mouse_up = function(e)
1236 var model, list, li;
1238 if (this.message_list) {
1239 if (!rcube_mouse_is_over(e, this.message_list.list))
1240 this.message_list.blur();
1241 list = this.message_list;
1242 model = this.env.mailboxes;
1244 else if (this.contact_list) {
1245 if (!rcube_mouse_is_over(e, this.contact_list.list))
1246 this.contact_list.blur();
1247 list = this.contact_list;
1248 model = this.env.address_sources;
1250 else if (this.ksearch_value) {
1251 this.ksearch_blur();
1254 // handle mouse release when dragging
1255 if (this.drag_active && model && this.env.last_folder_target) {
1256 $(this.get_folder_li(this.env.last_folder_target)).removeClass('droptarget');
1257 this.command('moveto', model[this.env.last_folder_target].id);
1258 this.env.last_folder_target = null;
1259 list.draglayer.hide();
1262 // reset 'pressed' buttons
1263 if (this.buttons_sel) {
1264 for (var id in this.buttons_sel)
1265 if (typeof id != 'function')
1266 this.button_out(this.buttons_sel[id], id);
1267 this.buttons_sel = {};
1271 this.drag_start = function(list)
1273 var model = this.task == 'mail' ? this.env.mailboxes : this.env.address_sources;
1275 this.drag_active = true;
1276 if (this.preview_timer)
1277 clearTimeout(this.preview_timer);
1279 // save folderlist and folders location/sizes for droptarget calculation in drag_move()
1280 if (this.gui_objects.folderlist && model)
1282 this.initialBodyScrollTop = bw.ie ? 0 : window.pageYOffset;
1283 this.initialListScrollTop = this.gui_objects.folderlist.parentNode.scrollTop;
1285 var li, pos, list, height;
1286 list = $(this.gui_objects.folderlist);
1287 pos = list.offset();
1288 this.env.folderlist_coords = { x1:pos.left, y1:pos.top, x2:pos.left + list.width(), y2:pos.top + list.height() };
1290 this.env.folder_coords = new Array();
1291 for (var k in model) {
1292 if (li = this.get_folder_li(k)) {
1293 // only visible folders
1294 if (height = li.firstChild.offsetHeight) {
1295 pos = $(li.firstChild).offset();
1296 this.env.folder_coords[k] = { x1:pos.left, y1:pos.top,
1297 x2:pos.left + li.firstChild.offsetWidth, y2:pos.top + height, on:0 };
1304 this.drag_end = function(e)
1306 this.drag_active = false;
1307 this.env.last_folder_target = null;
1309 if (this.folder_auto_timer) {
1310 window.clearTimeout(this.folder_auto_timer);
1311 this.folder_auto_timer = null;
1312 this.folder_auto_expand = null;
1316 if (this.gui_objects.folderlist && this.env.folder_coords) {
1317 for (var k in this.env.folder_coords) {
1318 if (this.env.folder_coords[k].on)
1319 $(this.get_folder_li(k)).removeClass('droptarget');
1324 this.drag_move = function(e)
1326 if (this.gui_objects.folderlist && this.env.folder_coords) {
1327 // offsets to compensate for scrolling while dragging a message
1328 var boffset = bw.ie ? -document.documentElement.scrollTop : this.initialBodyScrollTop;
1329 var moffset = this.initialListScrollTop-this.gui_objects.folderlist.parentNode.scrollTop;
1330 var toffset = -moffset-boffset;
1332 var li, div, pos, mouse;
1333 mouse = rcube_event.get_mouse_pos(e);
1334 pos = this.env.folderlist_coords;
1337 // if mouse pointer is outside of folderlist
1338 if (mouse.x < pos.x1 || mouse.x >= pos.x2 || mouse.y < pos.y1 || mouse.y >= pos.y2) {
1339 if (this.env.last_folder_target) {
1340 $(this.get_folder_li(this.env.last_folder_target)).removeClass('droptarget');
1341 this.env.folder_coords[this.env.last_folder_target].on = 0;
1342 this.env.last_folder_target = null;
1348 for (var k in this.env.folder_coords) {
1349 pos = this.env.folder_coords[k];
1350 if (mouse.x >= pos.x1 && mouse.x < pos.x2 && mouse.y >= pos.y1 && mouse.y < pos.y2
1351 && this.check_droptarget(k)) {
1353 li = this.get_folder_li(k);
1354 div = $(li.getElementsByTagName("div")[0]);
1356 // if the folder is collapsed, expand it after 1sec and restart the drag & drop process.
1357 if (div.hasClass('collapsed')) {
1358 if (this.folder_auto_timer)
1359 window.clearTimeout(this.folder_auto_timer);
1361 this.folder_auto_expand = k;
1362 this.folder_auto_timer = window.setTimeout(function() {
1363 rcmail.command("collapse-folder", rcmail.folder_auto_expand);
1364 rcmail.drag_start(null);
1366 } else if (this.folder_auto_timer) {
1367 window.clearTimeout(this.folder_auto_timer);
1368 this.folder_auto_timer = null;
1369 this.folder_auto_expand = null;
1372 $(li).addClass('droptarget');
1373 this.env.last_folder_target = k;
1374 this.env.folder_coords[k].on = 1;
1377 $(this.get_folder_li(k)).removeClass('droptarget');
1378 this.env.folder_coords[k].on = 0;
1384 this.collapse_folder = function(id)
1387 if ((li = this.get_folder_li(id)) &&
1388 (div = $(li.getElementsByTagName("div")[0])) &&
1389 (div.hasClass('collapsed') || div.hasClass('expanded')))
1391 var ul = $(li.getElementsByTagName("ul")[0]);
1392 if (div.hasClass('collapsed'))
1395 div.removeClass('collapsed').addClass('expanded');
1396 var reg = new RegExp('&'+urlencode(id)+'&');
1397 this.set_env('collapsed_folders', this.env.collapsed_folders.replace(reg, ''));
1402 div.removeClass('expanded').addClass('collapsed');
1403 this.set_env('collapsed_folders', this.env.collapsed_folders+'&'+urlencode(id)+'&');
1405 // select parent folder if one of its childs is currently selected
1406 if (this.env.mailbox.indexOf(id + this.env.delimiter) == 0)
1407 this.command('list', id);
1410 // Work around a bug in IE6 and IE7, see #1485309
1411 if ((bw.ie6 || bw.ie7) &&
1413 (li.nextSibling.getElementsByTagName("ul").length>0) &&
1414 li.nextSibling.getElementsByTagName("ul")[0].style &&
1415 (li.nextSibling.getElementsByTagName("ul")[0].style.display!='none'))
1417 li.nextSibling.getElementsByTagName("ul")[0].style.display = 'none';
1418 li.nextSibling.getElementsByTagName("ul")[0].style.display = '';
1421 this.http_post('save-pref', '_name=collapsed_folders&_value='+urlencode(this.env.collapsed_folders));
1422 this.set_unread_count_display(id, false);
1426 this.click_on_list = function(e)
1428 if (this.gui_objects.qsearchbox)
1429 this.gui_objects.qsearchbox.blur();
1431 if (this.message_list)
1432 this.message_list.focus();
1433 else if (this.contact_list)
1434 this.contact_list.focus();
1436 return rcube_event.get_button(e) == 2 ? true : rcube_event.cancel(e);
1439 this.msglist_select = function(list)
1441 if (this.preview_timer)
1442 clearTimeout(this.preview_timer);
1444 var selected = list.selection.length==1;
1446 // Hide certain command buttons when Drafts folder is selected
1447 if (this.env.mailbox == this.env.drafts_mailbox)
1449 this.enable_command('reply', 'reply-all', 'forward', false);
1450 this.enable_command('show', 'print', 'open', 'edit', 'download', 'viewsource', selected);
1451 this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
1455 this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', 'edit', 'open', 'download', 'viewsource', selected);
1456 this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
1459 // start timer for message preview (wait for double click)
1460 if (selected && this.env.contentframe && !list.multi_selecting)
1461 this.preview_timer = window.setTimeout(function(){ ref.msglist_get_preview(); }, 200);
1462 else if (this.env.contentframe)
1463 this.show_contentframe(false);
1466 this.msglist_dbl_click = function(list)
1468 if (this.preview_timer)
1469 clearTimeout(this.preview_timer);
1471 var uid = list.get_single_selection();
1472 if (uid && this.env.mailbox == this.env.drafts_mailbox)
1473 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1475 this.show_message(uid, false, false);
1478 this.msglist_keypress = function(list)
1480 if (list.key_pressed == list.ENTER_KEY)
1481 this.command('show');
1482 else if (list.key_pressed == list.DELETE_KEY)
1483 this.command('delete');
1484 else if (list.key_pressed == list.BACKSPACE_KEY)
1485 this.command('delete');
1487 list.shiftkey = false;
1490 this.msglist_get_preview = function()
1492 var uid = this.get_single_uid();
1493 if (uid && this.env.contentframe && !this.drag_active)
1494 this.show_message(uid, false, true);
1495 else if (this.env.contentframe)
1496 this.show_contentframe(false);
1499 this.check_droptarget = function(id)
1501 if (this.task == 'mail')
1502 return (this.env.mailboxes[id] && this.env.mailboxes[id].id != this.env.mailbox && !this.env.mailboxes[id].virtual);
1503 else if (this.task == 'addressbook')
1504 return (id != this.env.source && this.env.address_sources[id] && !this.env.address_sources[id].readonly);
1505 else if (this.task == 'settings')
1506 return (id != this.env.folder);
1510 /*********************************************************/
1511 /********* (message) list functionality *********/
1512 /*********************************************************/
1514 // when user doble-clicks on a row
1515 this.show_message = function(id, safe, preview)
1520 var action = preview ? 'preview': 'show';
1521 var target = window;
1523 if (preview && this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1525 target = window.frames[this.env.contentframe];
1526 add_url = '&_framed=1';
1530 add_url = '&_safe=1';
1532 // also send search request to get the right messages
1533 if (this.env.search_request)
1534 add_url += '&_search='+this.env.search_request;
1536 var url = '&_action='+action+'&_uid='+id+'&_mbox='+urlencode(this.env.mailbox)+add_url;
1537 if (action == 'preview' && String(target.location.href).indexOf(url) >= 0)
1538 this.show_contentframe(true);
1541 this.set_busy(true, 'loading');
1542 target.location.href = this.env.comm_path+url;
1544 // mark as read and change mbox unread counter
1545 if (action == 'preview' && this.message_list && this.message_list.rows[id] && this.message_list.rows[id].unread)
1547 this.set_message(id, 'unread', false);
1548 if (this.env.unread_counts[this.env.mailbox])
1550 this.env.unread_counts[this.env.mailbox] -= 1;
1551 this.set_unread_count(this.env.mailbox, this.env.unread_counts[this.env.mailbox], this.env.mailbox == 'INBOX');
1557 this.show_contentframe = function(show)
1560 if (this.env.contentframe && (frm = $('#'+this.env.contentframe)) && frm.length)
1562 if (!show && window.frames[this.env.contentframe])
1564 if (window.frames[this.env.contentframe].location.href.indexOf(this.env.blankpage)<0)
1565 window.frames[this.env.contentframe].location.href = this.env.blankpage;
1567 else if (!bw.safari && !bw.konq)
1568 frm[show ? 'show' : 'hide']();
1571 if (!show && this.busy)
1572 this.set_busy(false);
1575 // list a specific page
1576 this.list_page = function(page)
1579 page = this.env.current_page+1;
1581 page = this.env.pagecount;
1582 if (page=='prev' && this.env.current_page>1)
1583 page = this.env.current_page-1;
1584 if (page=='first' && this.env.current_page>1)
1587 if (page > 0 && page <= this.env.pagecount)
1589 this.env.current_page = page;
1591 if (this.task=='mail')
1592 this.list_mailbox(this.env.mailbox, page);
1593 else if (this.task=='addressbook')
1594 this.list_contacts(this.env.source, page);
1598 // list messages of a specific mailbox using filter
1599 this.filter_mailbox = function(filter)
1602 if (this.gui_objects.qsearchbox)
1603 search = this.gui_objects.qsearchbox.value;
1605 this.message_list.clear();
1608 this.env.current_page = 1;
1609 this.set_busy(true, 'searching');
1610 this.http_request('search', '_filter='+filter
1611 + (search ? '&_q='+urlencode(search) : '')
1612 + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : ''), true);
1616 // list messages of a specific mailbox
1617 this.list_mailbox = function(mbox, page, sort)
1620 var target = window;
1623 mbox = this.env.mailbox;
1625 // add sort to url if set
1627 add_url += '&_sort=' + sort;
1629 // also send search request to get the right messages
1630 if (this.env.search_request)
1631 add_url += '&_search='+this.env.search_request;
1633 // set page=1 if changeing to another mailbox
1637 this.env.current_page = page;
1638 this.show_contentframe(false);
1641 if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
1642 add_url += '&_refresh=1';
1644 // unselect selected messages
1645 this.last_selected = 0;
1646 if (this.message_list)
1647 this.message_list.clear_selection();
1649 this.select_folder(mbox, this.env.mailbox);
1650 this.env.mailbox = mbox;
1652 // load message list remotely
1653 if (this.gui_objects.messagelist)
1655 this.list_mailbox_remote(mbox, page, add_url);
1659 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1661 target = window.frames[this.env.contentframe];
1662 add_url += '&_framed=1';
1665 // load message list to target frame/window
1668 this.set_busy(true, 'loading');
1669 target.location.href = this.env.comm_path+'&_mbox='+urlencode(mbox)+(page ? '&_page='+page : '')+add_url;
1673 // send remote request to load message list
1674 this.list_mailbox_remote = function(mbox, page, add_url)
1676 // clear message list first
1677 this.message_list.clear();
1679 // send request to server
1680 var url = '_mbox='+urlencode(mbox)+(page ? '&_page='+page : '');
1681 this.set_busy(true, 'loading');
1682 this.http_request('list', url+add_url, true);
1685 this.expunge_mailbox = function(mbox)
1690 // lock interface if it's the active mailbox
1691 if (mbox == this.env.mailbox)
1694 this.set_busy(true, 'loading');
1695 add_url = '&_reload=1';
1698 // send request to server
1699 var url = '_mbox='+urlencode(mbox);
1700 this.http_post('expunge', url+add_url, lock);
1703 this.purge_mailbox = function(mbox)
1708 if (!confirm(this.get_label('purgefolderconfirm')))
1711 // lock interface if it's the active mailbox
1712 if (mbox == this.env.mailbox)
1715 this.set_busy(true, 'loading');
1716 add_url = '&_reload=1';
1719 // send request to server
1720 var url = '_mbox='+urlencode(mbox);
1721 this.http_post('purge', url+add_url, lock);
1725 // test if purge command is allowed
1726 this.purge_mailbox_test = function()
1728 return (this.env.messagecount && (this.env.mailbox == this.env.trash_mailbox || this.env.mailbox == this.env.junk_mailbox
1729 || this.env.mailbox.match('^' + RegExp.escape(this.env.trash_mailbox) + RegExp.escape(this.env.delimiter))
1730 || this.env.mailbox.match('^' + RegExp.escape(this.env.junk_mailbox) + RegExp.escape(this.env.delimiter))));
1734 this.set_message_icon = function(uid)
1737 var rows = this.message_list.rows;
1742 if (rows[uid].deleted && this.env.deletedicon)
1743 icn_src = this.env.deletedicon;
1744 else if (rows[uid].replied && this.env.repliedicon)
1746 if (rows[uid].forwarded && this.env.forwardedrepliedicon)
1747 icn_src = this.env.forwardedrepliedicon;
1749 icn_src = this.env.repliedicon;
1751 else if (rows[uid].forwarded && this.env.forwardedicon)
1752 icn_src = this.env.forwardedicon;
1753 else if (rows[uid].unread && this.env.unreadicon)
1754 icn_src = this.env.unreadicon;
1755 else if (this.env.messageicon)
1756 icn_src = this.env.messageicon;
1758 if (icn_src && rows[uid].icon)
1759 rows[uid].icon.src = icn_src;
1763 if (rows[uid].flagged && this.env.flaggedicon)
1764 icn_src = this.env.flaggedicon;
1765 else if (!rows[uid].flagged && this.env.unflaggedicon)
1766 icn_src = this.env.unflaggedicon;
1768 if (rows[uid].flagged_icon && icn_src)
1769 rows[uid].flagged_icon.src = icn_src;
1772 // set message status
1773 this.set_message_status = function(uid, flag, status)
1775 var rows = this.message_list.rows;
1777 if (!rows[uid]) return false;
1779 if (flag == 'unread')
1780 rows[uid].unread = status;
1781 else if(flag == 'deleted')
1782 rows[uid].deleted = status;
1783 else if (flag == 'replied')
1784 rows[uid].replied = status;
1785 else if (flag == 'forwarded')
1786 rows[uid].forwarded = status;
1787 else if (flag == 'flagged')
1788 rows[uid].flagged = status;
1790 this.env.messages[uid] = rows[uid];
1793 // set message row status, class and icon
1794 this.set_message = function(uid, flag, status)
1796 var rows = this.message_list.rows;
1798 if (!rows[uid]) return false;
1801 this.set_message_status(uid, flag, status);
1803 var rowobj = $(rows[uid].obj);
1804 if (rows[uid].unread && rows[uid].classname.indexOf('unread')<0)
1806 rows[uid].classname += ' unread';
1807 rowobj.addClass('unread');
1809 else if (!rows[uid].unread && rows[uid].classname.indexOf('unread')>=0)
1811 rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
1812 rowobj.removeClass('unread');
1815 if (rows[uid].deleted && rows[uid].classname.indexOf('deleted')<0)
1817 rows[uid].classname += ' deleted';
1818 rowobj.addClass('deleted');
1820 else if (!rows[uid].deleted && rows[uid].classname.indexOf('deleted')>=0)
1822 rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
1823 rowobj.removeClass('deleted');
1826 if (rows[uid].flagged && rows[uid].classname.indexOf('flagged')<0)
1828 rows[uid].classname += ' flagged';
1829 rowobj.addClass('flagged');
1831 else if (!rows[uid].flagged && rows[uid].classname.indexOf('flagged')>=0)
1833 rows[uid].classname = rows[uid].classname.replace(/\s*flagged/, '');
1834 rowobj.removeClass('flagged');
1837 this.set_message_icon(uid);
1840 // move selected messages to the specified mailbox
1841 this.move_messages = function(mbox)
1843 // exit if current or no mailbox specified or if selection is empty
1844 if (!mbox || mbox == this.env.mailbox || (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length)))
1848 var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
1850 // show wait message
1851 if (this.env.action=='show')
1854 this.set_busy(true, 'movingmessage');
1857 this.show_contentframe(false);
1859 // Hide message command buttons until a message is selected
1860 this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', 'open', 'edit', 'viewsource', 'download', false);
1862 this._with_selected_messages('moveto', lock, add_url);
1865 // delete selected messages from the current mailbox
1866 this.delete_messages = function()
1868 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1870 // exit if no mailbox specified or if selection is empty
1871 if (!this.env.uid && !selection.length)
1874 // if config is set to flag for deletion
1875 if (this.env.flag_for_deletion)
1876 this.mark_message('delete');
1877 // if there isn't a defined trash mailbox or we are in it
1878 else if (!this.env.trash_mailbox || String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1879 this.permanently_remove_messages();
1880 // if there is a trash mailbox defined and we're not currently in it
1882 // if shift was pressed delete it immediately
1883 if (this.message_list && this.message_list.shiftkey)
1885 if (confirm(this.get_label('deletemessagesconfirm')))
1886 this.permanently_remove_messages();
1889 this.move_messages(this.env.trash_mailbox);
1893 // delete the selected messages permanently
1894 this.permanently_remove_messages = function()
1896 // exit if no mailbox specified or if selection is empty
1897 if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1900 this.show_contentframe(false);
1901 this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
1904 // Send a specifc request with UIDs of all selected messages
1906 this._with_selected_messages = function(action, lock, add_url, remove)
1908 var a_uids = new Array();
1911 a_uids[0] = this.env.uid;
1914 var selection = this.message_list.get_selection();
1915 var rows = this.message_list.rows;
1917 for (var n=0; n<selection.length; n++) {
1919 a_uids[a_uids.length] = id;
1920 this.message_list.remove_row(id, (this.env.display_next && n == selection.length-1));
1922 // make sure there are no selected rows
1923 if (!this.env.display_next)
1924 this.message_list.clear_selection();
1927 // also send search request to get the right messages
1928 if (this.env.search_request)
1929 add_url += '&_search='+this.env.search_request;
1931 if (this.env.display_next && this.env.next_uid)
1932 add_url += '&_next_uid='+this.env.next_uid;
1934 // send request to server
1935 this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
1938 // set a specific flag to one or more messages
1939 this.mark_message = function(flag, uid)
1941 var a_uids = new Array();
1942 var r_uids = new Array();
1943 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1947 else if (this.env.uid)
1948 a_uids[0] = this.env.uid;
1949 else if (this.message_list)
1951 for (var n=0; n<selection.length; n++)
1953 a_uids[a_uids.length] = selection[n];
1957 if (!this.message_list)
1960 for (var id, n=0; n<a_uids.length; n++)
1963 if ((flag=='read' && this.message_list.rows[id].unread)
1964 || (flag=='unread' && !this.message_list.rows[id].unread)
1965 || (flag=='delete' && !this.message_list.rows[id].deleted)
1966 || (flag=='undelete' && this.message_list.rows[id].deleted)
1967 || (flag=='flagged' && !this.message_list.rows[id].flagged)
1968 || (flag=='unflagged' && this.message_list.rows[id].flagged))
1970 r_uids[r_uids.length] = id;
1982 this.toggle_read_status(flag, r_uids);
1986 this.toggle_delete_status(r_uids);
1990 this.toggle_flagged_status(flag, a_uids);
1995 // set class to read/unread
1996 this.toggle_read_status = function(flag, a_uids)
1998 // mark all message rows as read/unread
1999 for (var i=0; i<a_uids.length; i++)
2000 this.set_message(a_uids[i], 'unread', (flag=='unread' ? true : false));
2002 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
2005 // set image to flagged or unflagged
2006 this.toggle_flagged_status = function(flag, a_uids)
2008 // mark all message rows as flagged/unflagged
2009 for (var i=0; i<a_uids.length; i++)
2010 this.set_message(a_uids[i], 'flagged', (flag=='flagged' ? true : false));
2012 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
2015 // mark all message rows as deleted/undeleted
2016 this.toggle_delete_status = function(a_uids)
2018 var rows = this.message_list ? this.message_list.rows : new Array();
2020 if (a_uids.length==1)
2022 if (!rows.length || (rows[a_uids[0]] && !rows[a_uids[0]].deleted))
2023 this.flag_as_deleted(a_uids);
2025 this.flag_as_undeleted(a_uids);
2030 var all_deleted = true;
2031 for (var i=0; i<a_uids.length; i++)
2035 if (!rows[uid].deleted)
2037 all_deleted = false;
2044 this.flag_as_undeleted(a_uids);
2046 this.flag_as_deleted(a_uids);
2051 this.flag_as_undeleted = function(a_uids)
2053 for (var i=0; i<a_uids.length; i++)
2054 this.set_message(a_uids[i], 'deleted', false);
2056 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
2060 this.flag_as_deleted = function(a_uids)
2063 var r_uids = new Array();
2064 var rows = this.message_list ? this.message_list.rows : new Array();
2066 for (var i=0; i<a_uids.length; i++)
2071 if (rows[uid].unread)
2072 r_uids[r_uids.length] = uid;
2074 if (this.env.skip_deleted)
2075 this.message_list.remove_row(uid, (this.env.display_next && i == this.message_list.selection.length-1));
2077 this.set_message(uid, 'deleted', true);
2081 // make sure there are no selected rows
2082 if (this.env.skip_deleted && !this.env.display_next && this.message_list)
2083 this.message_list.clear_selection();
2085 add_url = '&_from='+(this.env.action ? this.env.action : '');
2088 add_url += '&_ruid='+r_uids.join(',');
2090 if (this.env.skip_deleted) {
2091 // also send search request to get the right messages
2092 if (this.env.search_request)
2093 add_url += '&_search='+this.env.search_request;
2094 if (this.env.display_next && this.env.next_uid)
2095 add_url += '&_next_uid='+this.env.next_uid;
2098 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete'+add_url);
2102 // flag as read without mark request (called from backend)
2103 // argument should be a coma-separated list of uids
2104 this.flag_deleted_as_read = function(uids)
2107 var rows = this.message_list ? this.message_list.rows : new Array();
2108 var str = String(uids);
2109 var a_uids = new Array();
2111 a_uids = str.split(',');
2113 for (var uid, i=0; i<a_uids.length; i++)
2117 this.set_message(uid, 'unread', false);
2122 /*********************************************************/
2123 /********* login form methods *********/
2124 /*********************************************************/
2126 // handler for keyboard events on the _user field
2127 this.login_user_keyup = function(e)
2129 var key = rcube_event.get_keycode(e);
2130 var passwd = $('#rcmloginpwd');
2133 if (key == 13 && passwd.length && !passwd.val()) {
2135 return rcube_event.cancel(e);
2142 /*********************************************************/
2143 /********* message compose methods *********/
2144 /*********************************************************/
2146 // checks the input fields before sending a message
2147 this.check_compose_input = function()
2149 // check input fields
2150 var input_to = $("[name='_to']");
2151 var input_cc = $("[name='_cc']");
2152 var input_bcc = $("[name='_bcc']");
2153 var input_from = $("[name='_from']");
2154 var input_subject = $("[name='_subject']");
2155 var input_message = $("[name='_message']");
2157 // check sender (if have no identities)
2158 if (input_from.attr('type') == 'text' && !rcube_check_email(input_from.val(), true))
2160 alert(this.get_label('nosenderwarning'));
2165 // check for empty recipient
2166 var recipients = input_to.val() ? input_to.val() : (input_cc.val() ? input_cc.val() : input_bcc.val());
2167 if (!rcube_check_email(recipients.replace(/^\s+/, '').replace(/[\s,;]+$/, ''), true))
2169 alert(this.get_label('norecipientwarning'));
2174 // display localized warning for missing subject
2175 if (input_subject.val() == '')
2177 var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
2179 // user hit cancel, so don't send
2180 if (!subject && subject !== '')
2182 input_subject.focus();
2187 input_subject.val((subject ? subject : this.get_label('nosubject')));
2191 // check for empty body
2192 if ((!window.tinyMCE || !tinyMCE.get(this.env.composebody))
2193 && input_message.val() == '' && !confirm(this.get_label('nobodywarning')))
2195 input_message.focus();
2198 else if (window.tinyMCE && tinyMCE.get(this.env.composebody)
2199 && !tinyMCE.get(this.env.composebody).getContent()
2200 && !confirm(this.get_label('nobodywarning')))
2202 tinyMCE.get(this.env.composebody).focus();
2206 // Apply spellcheck changes if spell checker is active
2207 this.stop_spellchecking();
2209 // move body from html editor to textarea (just to be sure, #1485860)
2210 if (window.tinyMCE && tinyMCE.get(this.env.composebody))
2211 tinyMCE.triggerSave();
2216 this.stop_spellchecking = function()
2218 if (this.env.spellcheck && !this.spellcheck_ready) {
2219 $(this.env.spellcheck.spell_span).trigger('click');
2220 this.set_spellcheck_state('ready');
2224 this.display_spellcheck_controls = function(vis)
2226 if (this.env.spellcheck) {
2227 // stop spellchecking process
2229 this.stop_spellchecking();
2231 $(this.env.spellcheck.spell_container).css('visibility', vis ? 'visible' : 'hidden');
2235 this.set_spellcheck_state = function(s)
2237 this.spellcheck_ready = (s == 'ready' || s == 'no_error_found');
2238 this.enable_command('spellcheck', this.spellcheck_ready);
2241 this.set_draft_id = function(id)
2243 $("input[name='_draft_saveid']").val(id);
2246 this.auto_save_start = function()
2248 if (this.env.draft_autosave)
2249 this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
2251 // Unlock interface now that saving is complete
2255 this.compose_field_hash = function(save)
2257 // check input fields
2258 var value_to = $("[name='_to']").val();
2259 var value_cc = $("[name='_cc']").val();
2260 var value_bcc = $("[name='_bcc']").val();
2261 var value_subject = $("[name='_subject']").val();
2265 str += value_to+':';
2267 str += value_cc+':';
2269 str += value_bcc+':';
2271 str += value_subject+':';
2273 var editor = tinyMCE.get(this.env.composebody);
2275 str += editor.getContent();
2277 str += $("[name='_message']").val();
2280 this.cmp_hash = str;
2285 this.change_identity = function(obj)
2287 if (!obj || !obj.options)
2290 var id = obj.options[obj.selectedIndex].value;
2291 var input_message = $("[name='_message']");
2292 var message = input_message.val();
2293 var is_html = ($("input[name='_is_html']").val() == '1');
2296 if (!this.env.identity)
2297 this.env.identity = id
2301 // remove the 'old' signature
2302 if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
2304 if (this.env.signatures[this.env.identity]['is_html'])
2305 sig = this.env.signatures[this.env.identity]['plain_text'];
2307 sig = this.env.signatures[this.env.identity]['text'];
2309 if (sig.indexOf('-- ')!=0)
2312 p = message.lastIndexOf(sig);
2314 message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
2317 message = message.replace(/[\r\n]+$/, '');
2318 len = message.length;
2320 // add the new signature string
2321 if (this.env.signatures && this.env.signatures[id])
2323 sig = this.env.signatures[id]['text'];
2324 if (this.env.signatures[id]['is_html'])
2326 sig = this.env.signatures[id]['plain_text'];
2328 if (sig.indexOf('-- ')!=0)
2330 message += '\n\n'+sig;
2336 var editor = tinyMCE.get(this.env.composebody);
2338 if (this.env.signatures)
2340 // Append the signature as a div within the body
2341 var sigElem = editor.dom.get('_rc_sig');
2347 // add empty line before signature on IE
2349 editor.getBody().appendChild(editor.getDoc().createElement('br'));
2351 sigElem = editor.getDoc().createElement('div');
2352 sigElem.setAttribute('id', '_rc_sig');
2353 editor.getBody().appendChild(sigElem);
2356 if (this.env.signatures[id])
2358 newsig = this.env.signatures[id]['text'];
2359 htmlsig = this.env.signatures[id]['is_html'];
2362 if (htmlsig && this.env.signatures[id]['plain_text'].indexOf('-- ')!=0)
2363 newsig = '<p>-- </p>' + newsig;
2364 else if (!htmlsig && newsig.indexOf('-- ')!=0)
2365 newsig = '-- \n' + newsig;
2370 sigElem.innerHTML = newsig;
2372 sigElem.innerHTML = '<pre>' + newsig + '</pre>';
2376 input_message.val(message);
2378 // move cursor before the signature
2380 this.set_caret_pos(input_message.get(0), len);
2382 this.env.identity = id;
2386 this.show_attachment_form = function(a)
2388 if (!this.gui_objects.uploadbox)
2392 if (elm = this.gui_objects.uploadbox)
2394 if (a && (list = this.gui_objects.attachmentlist))
2396 var pos = $(list).offset();
2397 elm.style.top = (pos.top + list.offsetHeight + 10) + 'px';
2398 elm.style.left = pos.left + 'px';
2401 elm.style.visibility = a ? 'visible' : 'hidden';
2404 // clear upload form
2406 if (!a && this.gui_objects.attachmentform != this.gui_objects.messageform)
2407 this.gui_objects.attachmentform.reset();
2409 catch(e){} // ignore errors
2414 // upload attachment file
2415 this.upload_file = function(form)
2420 // get file input fields
2422 for (var n=0; n<form.elements.length; n++)
2423 if (form.elements[n].type=='file' && form.elements[n].value)
2429 // create hidden iframe and post upload form
2432 var ts = new Date().getTime();
2433 var frame_name = 'rcmupload'+ts;
2435 // have to do it this way for IE
2436 // otherwise the form will be posted to a new window
2439 var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
2440 document.body.insertAdjacentHTML('BeforeEnd',html);
2442 else // for standards-compilant browsers
2444 var frame = document.createElement('iframe');
2445 frame.name = frame_name;
2446 frame.style.border = 'none';
2447 frame.style.width = 0;
2448 frame.style.height = 0;
2449 frame.style.visibility = 'hidden';
2450 document.body.appendChild(frame);
2453 form.target = frame_name;
2454 form.action = this.env.comm_path+'&_action=upload';
2455 form.setAttribute('enctype', 'multipart/form-data');
2459 // set reference to the form object
2460 this.gui_objects.attachmentform = form;
2464 // add file name to attachment list
2465 // called from upload page
2466 this.add2attachment_list = function(name, content)
2468 if (!this.gui_objects.attachmentlist)
2471 $('<li>').attr('id', name).html(content).appendTo(this.gui_objects.attachmentlist);
2475 this.remove_from_attachment_list = function(name)
2477 if (!this.gui_objects.attachmentlist)
2480 var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
2481 for (i=0;i<list.length;i++)
2482 if (list[i].id == name)
2483 this.gui_objects.attachmentlist.removeChild(list[i]);
2486 this.remove_attachment = function(name)
2489 this.http_post('remove-attachment', '_file='+urlencode(name));
2494 // send remote request to add a new contact
2495 this.add_contact = function(value)
2498 this.http_post('addcontact', '_address='+value);
2503 // send remote request to search mail or contacts
2504 this.qsearch = function(value)
2509 if (this.message_list) {
2510 this.message_list.clear();
2511 if (this.env.search_mods) {
2512 var head_arr = new Array();
2513 for (var n in this.env.search_mods)
2515 addurl += '&_headers='+head_arr.join(',');
2517 } else if (this.contact_list) {
2518 this.contact_list.clear(true);
2519 this.show_contentframe(false);
2522 if (this.gui_objects.search_filter)
2523 addurl += '&_filter=' + this.gui_objects.search_filter.value;
2526 this.env.current_page = 1;
2527 this.set_busy(true, 'searching');
2528 this.http_request('search', '_q='+urlencode(value)
2529 + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : '')
2530 + (this.env.source ? '&_source='+urlencode(this.env.source) : '')
2531 + (addurl ? addurl : ''), true);
2536 // reset quick-search form
2537 this.reset_qsearch = function()
2539 if (this.gui_objects.qsearchbox)
2540 this.gui_objects.qsearchbox.value = '';
2542 this.env.search_request = null;
2546 this.sent_successfully = function(type, msg)
2548 this.list_mailbox();
2549 this.display_message(msg, type, true);
2553 /*********************************************************/
2554 /********* keyboard live-search methods *********/
2555 /*********************************************************/
2557 // handler for keyboard events on address-fields
2558 this.ksearch_keypress = function(e, obj)
2560 if (this.ksearch_timer)
2561 clearTimeout(this.ksearch_timer);
2564 var key = rcube_event.get_keycode(e);
2565 var mod = rcube_event.get_modifier(e);
2570 case 40: // key down
2571 if (!this.ksearch_pane)
2574 var dir = key==38 ? 1 : 0;
2576 highlight = document.getElementById('rcmksearchSelected');
2578 highlight = this.ksearch_pane.__ul.firstChild;
2581 this.ksearch_select(dir ? highlight.previousSibling : highlight.nextSibling);
2583 return rcube_event.cancel(e);
2586 if(mod == SHIFT_KEY)
2590 if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2593 // insert selected address and hide ksearch pane
2594 this.insert_recipient(this.ksearch_selected);
2595 this.ksearch_hide();
2597 return rcube_event.cancel(e);
2600 this.ksearch_hide();
2605 if (mod != SHIFT_KEY)
2610 this.ksearch_timer = window.setTimeout(function(){ ref.ksearch_get_results(); }, 200);
2611 this.ksearch_input = obj;
2616 this.ksearch_select = function(node)
2618 var current = $('#rcmksearchSelected');
2619 if (current[0] && node) {
2620 current.removeAttr('id').removeClass('selected');
2624 $(node).attr('id', 'rcmksearchSelected').addClass('selected');
2625 this.ksearch_selected = node._rcm_id;
2629 this.insert_recipient = function(id)
2631 if (!this.env.contacts[id] || !this.ksearch_input)
2635 var inp_value = this.ksearch_input.value.toLowerCase();
2636 var cpos = this.get_caret_pos(this.ksearch_input);
2637 var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2639 // replace search string with full address
2640 var pre = this.ksearch_input.value.substring(0, p);
2641 var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2642 var insert = this.env.contacts[id]+', ';
2643 this.ksearch_input.value = pre + insert + end;
2645 // set caret to insert pos
2646 cpos = p+insert.length;
2647 if (this.ksearch_input.setSelectionRange)
2648 this.ksearch_input.setSelectionRange(cpos, cpos);
2651 // address search processor
2652 this.ksearch_get_results = function()
2654 var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2655 if (inp_value === null)
2658 if (this.ksearch_pane && this.ksearch_pane.is(":visible"))
2659 this.ksearch_pane.hide();
2661 // get string from current cursor pos to last comma
2662 var cpos = this.get_caret_pos(this.ksearch_input);
2663 var p = inp_value.lastIndexOf(',', cpos-1);
2664 var q = inp_value.substring(p+1, cpos);
2666 // trim query string
2667 q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2669 // Don't (re-)search if the last results are still active
2670 if (q == this.ksearch_value)
2673 var old_value = this.ksearch_value;
2674 this.ksearch_value = q;
2676 // ...string is empty
2680 // ...new search value contains old one and previous search result was empty
2681 if (old_value && old_value.length && this.env.contacts && !this.env.contacts.length && q.indexOf(old_value) == 0)
2684 this.display_message(this.get_label('searching'), 'loading', true);
2685 this.http_post('autocomplete', '_search='+urlencode(q));
2688 this.ksearch_query_results = function(results, search)
2690 // ignore this outdated search response
2691 if (this.ksearch_value && search != this.ksearch_value)
2694 this.hide_message();
2695 this.env.contacts = results ? results : [];
2696 this.ksearch_display_results(this.env.contacts);
2699 this.ksearch_display_results = function (a_results)
2701 // display search results
2702 if (a_results.length && this.ksearch_input) {
2705 // create results pane if not present
2706 if (!this.ksearch_pane) {
2708 this.ksearch_pane = $('<div>').attr('id', 'rcmKSearchpane').css({ position:'absolute', 'z-index':30000 }).append(ul).appendTo(document.body);
2709 this.ksearch_pane.__ul = ul[0];
2712 // remove all search results
2713 ul = this.ksearch_pane.__ul;
2716 // add each result line to list
2717 for (i=0; i<a_results.length; i++) {
2718 li = document.createElement('LI');
2719 li.innerHTML = a_results[i].replace(new RegExp('('+this.ksearch_value+')', 'ig'), '##$1%%').replace(/</g, '<').replace(/>/g, '>').replace(/##([^%]+)%%/g, '<b>$1</b>');
2720 li.onmouseover = function(){ ref.ksearch_select(this); };
2721 li.onmouseup = function(){ ref.ksearch_click(this) };
2727 $(ul.firstChild).attr('id', 'rcmksearchSelected').addClass('selected');
2728 this.ksearch_selected = 0;
2730 // move the results pane right under the input box and make it visible
2731 var pos = $(this.ksearch_input).offset();
2732 this.ksearch_pane.css({ left:pos.left+'px', top:(pos.top + this.ksearch_input.offsetHeight)+'px' }).show();
2734 // hide results pane
2736 this.ksearch_hide();
2739 this.ksearch_click = function(node)
2741 if (this.ksearch_input)
2742 this.ksearch_input.focus();
2744 this.insert_recipient(node._rcm_id);
2745 this.ksearch_hide();
2748 this.ksearch_blur = function()
2750 if (this.ksearch_timer)
2751 clearTimeout(this.ksearch_timer);
2753 this.ksearch_value = '';
2754 this.ksearch_input = null;
2756 this.ksearch_hide();
2760 this.ksearch_hide = function()
2762 this.ksearch_selected = null;
2764 if (this.ksearch_pane)
2765 this.ksearch_pane.hide();
2769 /*********************************************************/
2770 /********* address book methods *********/
2771 /*********************************************************/
2773 this.contactlist_keypress = function(list)
2775 if (list.key_pressed == list.DELETE_KEY)
2776 this.command('delete');
2779 this.contactlist_select = function(list)
2781 if (this.preview_timer)
2782 clearTimeout(this.preview_timer);
2784 var id, frame, ref = this;
2785 if (id = list.get_single_selection())
2786 this.preview_timer = window.setTimeout(function(){ ref.load_contact(id, 'show'); }, 200);
2787 else if (this.env.contentframe)
2788 this.show_contentframe(false);
2790 this.enable_command('compose', list.selection.length > 0);
2791 this.enable_command('edit', (id && this.env.address_sources && !this.env.address_sources[this.env.source].readonly) ? true : false);
2792 this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
2797 this.list_contacts = function(src, page)
2800 var target = window;
2803 src = this.env.source;
2805 if (page && this.current_page==page && src == this.env.source)
2808 if (src != this.env.source)
2811 this.env.current_page = page;
2812 this.reset_qsearch();
2815 this.select_folder(src, this.env.source);
2816 this.env.source = src;
2818 // load contacts remotely
2819 if (this.gui_objects.contactslist)
2821 this.list_contacts_remote(src, page);
2825 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2827 target = window.frames[this.env.contentframe];
2828 add_url = '&_framed=1';
2831 // also send search request to get the correct listing
2832 if (this.env.search_request)
2833 add_url += '&_search='+this.env.search_request;
2835 this.set_busy(true, 'loading');
2836 target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
2839 // send remote request to load contacts list
2840 this.list_contacts_remote = function(src, page)
2842 // clear message list first
2843 this.contact_list.clear(true);
2844 this.show_contentframe(false);
2845 this.enable_command('delete', 'compose', false);
2847 // send request to server
2848 var url = (src ? '_source='+urlencode(src) : '') + (page ? (src?'&':'') + '_page='+page : '');
2849 this.env.source = src;
2851 // also send search request to get the right messages
2852 if (this.env.search_request)
2853 url += '&_search='+this.env.search_request;
2855 this.set_busy(true, 'loading');
2856 this.http_request('list', url, true);
2859 // load contact record
2860 this.load_contact = function(cid, action, framed)
2863 var target = window;
2864 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2866 add_url = '&_framed=1';
2867 target = window.frames[this.env.contentframe];
2868 this.show_contentframe(true);
2873 if (action && (cid || action=='add') && !this.drag_active)
2875 this.set_busy(true);
2876 target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
2881 // copy a contact to the specified target (group or directory)
2882 this.copy_contact = function(cid, to)
2885 cid = this.contact_list.get_selection().join(',');
2887 if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2888 this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
2892 this.delete_contacts = function()
2894 // exit if no mailbox specified or if selection is empty
2895 var selection = this.contact_list.get_selection();
2896 if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
2899 var a_cids = new Array();
2903 a_cids[a_cids.length] = this.env.cid;
2907 for (var n=0; n<selection.length; n++)
2910 a_cids[a_cids.length] = id;
2911 this.contact_list.remove_row(id, (n == selection.length-1));
2914 // hide content frame if we delete the currently displayed contact
2915 if (selection.length == 1)
2916 this.show_contentframe(false);
2919 // also send search request to get the right records from the next page
2920 if (this.env.search_request)
2921 qs += '&_search='+this.env.search_request;
2923 // send request to server
2924 this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source)+'&_from='+(this.env.action ? this.env.action : '')+qs);
2928 // update a contact record in the list
2929 this.update_contact_row = function(cid, cols_arr, newcid)
2932 if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj)) {
2933 for (var c=0; c<cols_arr.length; c++)
2935 $(row.cells[c]).html(cols_arr[c]);
2939 row.id = 'rcmrow' + newcid;
2940 this.contact_list.remove_row(cid);
2941 this.contact_list.init_row(row);
2942 this.contact_list.selection[0] = newcid;
2943 row.style.display = '';
2952 // add row to contacts list
2953 this.add_contact_row = function(cid, cols, select)
2955 if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
2958 var tbody = this.gui_objects.contactslist.tBodies[0];
2959 var rowcount = tbody.rows.length;
2960 var even = rowcount%2;
2962 var row = document.createElement('tr');
2963 row.id = 'rcmrow'+cid;
2964 row.className = 'contact '+(even ? 'even' : 'odd');
2966 if (this.contact_list.in_selection(cid))
2967 row.className += ' selected';
2969 // add each submitted col
2970 for (var c in cols) {
2971 col = document.createElement('td');
2972 col.className = String(c).toLowerCase();
2973 col.innerHTML = cols[c];
2974 row.appendChild(col);
2977 this.contact_list.insert_row(row);
2979 this.enable_command('export', (this.contact_list.rowcount > 0));
2983 /*********************************************************/
2984 /********* user settings methods *********/
2985 /*********************************************************/
2987 this.init_subscription_list = function()
2990 this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist, {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
2991 this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
2992 this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
2993 this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
2994 this.subscription_list.row_init = function (row)
2996 var anchors = row.obj.getElementsByTagName('a');
2998 anchors[0].onclick = function() { p.rename_folder(row.id); return false; };
3000 anchors[1].onclick = function() { p.delete_folder(row.id); return false; };
3001 row.obj.onmouseover = function() { p.focus_subscription(row.id); };
3002 row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
3004 this.subscription_list.init();
3007 // preferences section select and load options frame
3008 this.section_select = function(list)
3010 var id = list.get_single_selection();
3014 var target = window;
3015 this.set_busy(true);
3017 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) {
3018 add_url = '&_framed=1';
3019 target = window.frames[this.env.contentframe];
3022 target.location.href = this.env.comm_path+'&_action=edit-prefs&_section='+id+add_url;
3028 this.identity_select = function(list)
3031 if (id = list.get_single_selection())
3032 this.load_identity(id, 'edit-identity');
3035 // load identity record
3036 this.load_identity = function(id, action)
3038 if (action=='edit-identity' && (!id || id==this.env.iid))
3042 var target = window;
3043 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
3045 add_url = '&_framed=1';
3046 target = window.frames[this.env.contentframe];
3047 document.getElementById(this.env.contentframe).style.visibility = 'inherit';
3050 if (action && (id || action=='add-identity'))
3052 this.set_busy(true);
3053 target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
3058 this.delete_identity = function(id)
3060 // exit if no mailbox specified or if selection is empty
3061 var selection = this.identity_list.get_selection();
3062 if (!(selection.length || this.env.iid))
3066 id = this.env.iid ? this.env.iid : selection[0];
3068 // append token to request
3069 this.goto_url('delete-identity', '_iid='+id+'&_token='+this.env.request_token, true);
3074 this.focus_subscription = function(id)
3077 var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3079 if (this.drag_active && this.env.folder && (row = document.getElementById(id)))
3080 if (this.env.subscriptionrows[id] &&
3081 (folder = this.env.subscriptionrows[id][0]))
3083 if (this.check_droptarget(folder) &&
3084 !this.env.subscriptionrows[this.get_folder_row_id(this.env.folder)][2] &&
3085 (folder != this.env.folder.replace(reg, '')) &&
3086 (!folder.match(new RegExp('^'+RegExp.escape(this.env.folder+this.env.delimiter)))))
3088 this.set_env('dstfolder', folder);
3089 $(row).addClass('droptarget');
3092 else if (this.env.folder.match(new RegExp(RegExp.escape(this.env.delimiter))))
3094 this.set_env('dstfolder', this.env.delimiter);
3095 $(this.subscription_list.frame).addClass('droptarget');
3099 this.unfocus_subscription = function(id)
3101 var row = $('#'+id);
3102 this.set_env('dstfolder', null);
3103 if (this.env.subscriptionrows[id] && row[0])
3104 row.removeClass('droptarget');
3106 $(this.subscription_list.frame).removeClass('droptarget');
3109 this.subscription_select = function(list)
3112 if ((id = list.get_single_selection()) &&
3113 this.env.subscriptionrows['rcmrow'+id] &&
3114 (folder = this.env.subscriptionrows['rcmrow'+id][0]))
3115 this.set_env('folder', folder);
3117 this.set_env('folder', null);
3119 if (this.gui_objects.createfolderhint)
3120 $(this.gui_objects.createfolderhint).html(this.env.folder ? this.get_label('addsubfolderhint') : '');
3123 this.subscription_move_folder = function(list)
3125 var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3126 if (this.env.folder && this.env.dstfolder && (this.env.dstfolder != this.env.folder) &&
3127 (this.env.dstfolder != this.env.folder.replace(reg, '')))
3129 var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
3130 var basename = this.env.folder.replace(reg, '');
3131 var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
3133 this.set_busy(true, 'foldermoving');
3134 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname), true);
3136 this.drag_active = false;
3137 this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
3140 // tell server to create and subscribe a new mailbox
3141 this.create_folder = function(name)
3143 if (this.edit_folder)
3144 this.reset_folder_rename();
3147 if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
3149 name = form.elements['_folder_name'].value;
3151 if (name.indexOf(this.env.delimiter)>=0)
3153 alert(this.get_label('forbiddencharacter')+' ('+this.env.delimiter+')');
3157 if (this.env.folder && name != '')
3158 name = this.env.folder+this.env.delimiter+name;
3160 this.set_busy(true, 'foldercreating');
3161 this.http_post('create-folder', '_name='+urlencode(name), true);
3163 else if (form.elements['_folder_name'])
3164 form.elements['_folder_name'].focus();
3167 // start renaming the mailbox name.
3168 // this will replace the name string with an input field
3169 this.rename_folder = function(id)
3171 var temp, row, form;
3173 // reset current renaming
3174 if (temp = this.edit_folder)
3176 this.reset_folder_rename();
3181 if (id && this.env.subscriptionrows[id] && (row = document.getElementById(id)))
3183 var reg = new RegExp('.*['+RegExp.escape(this.env.delimiter)+']');
3184 this.name_input = document.createElement('input');
3185 this.name_input.type = 'text';
3186 this.name_input.value = this.env.subscriptionrows[id][0].replace(reg, '');
3188 reg = new RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3189 this.name_input.__parent = this.env.subscriptionrows[id][0].replace(reg, '');
3190 this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
3192 row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
3193 this.edit_folder = id;
3194 this.name_input.select();
3196 if (form = this.gui_objects.editform)
3197 form.onsubmit = function(){ return false; };
3201 // remove the input field and write the current mailbox name to the table cell
3202 this.reset_folder_rename = function()
3204 var cell = this.name_input ? this.name_input.parentNode : null;
3206 if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
3207 $(cell).html(this.env.subscriptionrows[this.edit_folder][1]);
3209 this.edit_folder = null;
3212 // handler for keyboard events on the input field
3213 this.name_input_keypress = function(e)
3215 var key = rcube_event.get_keycode(e);
3220 var newname = this.name_input ? this.name_input.value : null;
3221 if (this.edit_folder && newname)
3223 if (newname.indexOf(this.env.delimiter)>=0)
3225 alert(this.get_label('forbiddencharacter')+' ('+this.env.delimiter+')');
3229 if (this.name_input.__parent)
3230 newname = this.name_input.__parent + this.env.delimiter + newname;
3232 this.set_busy(true, 'folderrenaming');
3233 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname), true);
3238 this.reset_folder_rename();
3241 // delete a specific mailbox with all its messages
3242 this.delete_folder = function(id)
3244 var folder = this.env.subscriptionrows[id][0];
3246 if (this.edit_folder)
3247 this.reset_folder_rename();
3249 if (folder && confirm(this.get_label('deletefolderconfirm')))
3251 this.set_busy(true, 'folderdeleting');
3252 this.http_post('delete-folder', '_mboxes='+urlencode(folder), true);
3253 this.set_env('folder', null);
3255 $(this.gui_objects.createfolderhint).html('');
3259 // add a new folder to the subscription list by cloning a folder row
3260 this.add_folder_row = function(name, display_name, replace, before)
3262 if (!this.gui_objects.subscriptionlist)
3265 // find not protected folder
3266 for (var refid in this.env.subscriptionrows)
3267 if (this.env.subscriptionrows[refid]!=null && !this.env.subscriptionrows[refid][2])
3271 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
3272 var id = 'rcmrow'+(tbody.childNodes.length+1);
3273 var selection = this.subscription_list.get_single_selection();
3275 if (replace && replace.id)
3281 if (!id || !(refrow = document.getElementById(refid)))
3283 // Refresh page if we don't have a table row to clone
3284 this.goto_url('folders');
3288 // clone a table row if there are existing rows
3289 var row = this.clone_table_row(refrow);
3292 if (before && (before = this.get_folder_row_id(before)))
3293 tbody.insertBefore(row, document.getElementById(before));
3295 tbody.appendChild(row);
3298 tbody.removeChild(replace);
3301 // add to folder/row-ID map
3302 this.env.subscriptionrows[row.id] = [name, display_name, 0];
3305 row.cells[0].innerHTML = display_name;
3307 // set messages count to zero
3309 row.cells[1].innerHTML = '*';
3311 if (!replace && row.cells[2] && row.cells[2].firstChild.tagName.toLowerCase()=='input')
3313 row.cells[2].firstChild.value = name;
3314 row.cells[2].firstChild.checked = true;
3317 // add new folder to rename-folder list and clear input field
3318 if (!replace && (form = this.gui_objects.editform))
3320 if (form.elements['_folder_oldname'])
3321 form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
3322 if (form.elements['_folder_name'])
3323 form.elements['_folder_name'].value = '';
3326 this.init_subscription_list();
3327 if (selection && document.getElementById('rcmrow'+selection))
3328 this.subscription_list.select_row(selection);
3330 if (document.getElementById(id).scrollIntoView)
3331 document.getElementById(id).scrollIntoView();
3334 // replace an existing table row with a new folder line
3335 this.replace_folder_row = function(oldfolder, newfolder, display_name, before)
3337 var id = this.get_folder_row_id(oldfolder);
3338 var row = document.getElementById(id);
3340 // replace an existing table row (if found)
3341 this.add_folder_row(newfolder, display_name, row, before);
3343 // rename folder in rename-folder dropdown
3345 if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
3347 for (var i=0;i<elm.options.length;i++)
3349 if (elm.options[i].value == oldfolder)
3351 elm.options[i].text = display_name;
3352 elm.options[i].value = newfolder;
3357 form.elements['_folder_newname'].value = '';
3361 // remove the table row of a specific mailbox from the table
3362 // (the row will not be removed, just hidden)
3363 this.remove_folder_row = function(folder)
3366 var id = this.get_folder_row_id(folder);
3367 if (id && (row = document.getElementById(id)))
3368 row.style.display = 'none';
3370 // remove folder from rename-folder list
3372 if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
3374 for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
3376 if (form.elements['_folder_oldname'].options[i].value == folder)
3378 form.elements['_folder_oldname'].options[i] = null;
3384 if (form && form.elements['_folder_newname'])
3385 form.elements['_folder_newname'].value = '';
3388 this.subscribe_folder = function(folder)
3391 this.http_post('subscribe', '_mbox='+urlencode(folder));
3394 this.unsubscribe_folder = function(folder)
3397 this.http_post('unsubscribe', '_mbox='+urlencode(folder));
3400 // helper method to find a specific mailbox row ID
3401 this.get_folder_row_id = function(folder)
3403 for (var id in this.env.subscriptionrows)
3404 if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
3410 // duplicate a specific table row
3411 this.clone_table_row = function(row)
3414 var new_row = document.createElement('tr');
3415 for(var n=0; n<row.cells.length; n++)
3417 cell = row.cells[n];
3418 td = document.createElement('td');
3421 td.className = cell.className;
3423 td.setAttribute('align', cell.align);
3425 td.innerHTML = cell.innerHTML;
3426 new_row.appendChild(td);
3433 /*********************************************************/
3434 /********* GUI functionality *********/
3435 /*********************************************************/
3437 // eable/disable buttons for page shifting
3438 this.set_page_buttons = function()
3440 this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
3441 this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
3442 this.enable_command('previouspage', (this.env.current_page > 1));
3443 this.enable_command('firstpage', (this.env.current_page > 1));
3446 // set event handlers on registered buttons
3447 this.init_buttons = function()
3449 for (var cmd in this.buttons) {
3450 if (typeof cmd != 'string')
3453 for (var i=0; i< this.buttons[cmd].length; i++) {
3454 var prop = this.buttons[cmd][i];
3455 var elm = document.getElementById(prop.id);
3459 var preload = false;
3460 if (prop.type == 'image') {
3461 elm = elm.parentNode;
3468 elm.onmousedown = function(e){ return rcmail.button_sel(this._command, this._id); };
3469 elm.onmouseup = function(e){ return rcmail.button_out(this._command, this._id); };
3471 new Image().src = prop.sel;
3474 elm.onmouseover = function(e){ return rcmail.button_over(this._command, this._id); };
3475 elm.onmouseout = function(e){ return rcmail.button_out(this._command, this._id); };
3477 new Image().src = prop.over;
3483 // set button to a specific state
3484 this.set_button = function(command, state)
3486 var a_buttons = this.buttons[command];
3489 if(!a_buttons || !a_buttons.length)
3492 for(var n=0; n<a_buttons.length; n++)
3494 button = a_buttons[n];
3495 obj = document.getElementById(button.id);
3497 // get default/passive setting of the button
3498 if (obj && button.type=='image' && !button.status) {
3499 button.pas = obj._original_src ? obj._original_src : obj.src;
3500 // respect PNG fix on IE browsers
3501 if (obj.runtimeStyle && obj.runtimeStyle.filter && obj.runtimeStyle.filter.match(/src=['"]([^'"]+)['"]/))
3502 button.pas = RegExp.$1;
3504 else if (obj && !button.status)
3505 button.pas = String(obj.className);
3507 // set image according to button state
3508 if (obj && button.type=='image' && button[state])
3510 button.status = state;
3511 obj.src = button[state];
3513 // set class name according to button state
3514 else if (obj && typeof(button[state])!='undefined')
3516 button.status = state;
3517 obj.className = button[state];
3519 // disable/enable input buttons
3520 if (obj && button.type=='input')
3522 button.status = state;
3523 obj.disabled = !state;
3528 // display a specific alttext
3529 this.set_alttext = function(command, label)
3531 if (!this.buttons[command] || !this.buttons[command].length)
3534 var button, obj, link;
3535 for (var n=0; n<this.buttons[command].length; n++)
3537 button = this.buttons[command][n];
3538 obj = document.getElementById(button.id);
3540 if (button.type=='image' && obj)
3542 obj.setAttribute('alt', this.get_label(label));
3543 if ((link = obj.parentNode) && link.tagName.toLowerCase() == 'a')
3544 link.setAttribute('title', this.get_label(label));
3547 obj.setAttribute('title', this.get_label(label));
3551 // mouse over button
3552 this.button_over = function(command, id)
3554 var a_buttons = this.buttons[command];
3557 if(!a_buttons || !a_buttons.length)
3560 for(var n=0; n<a_buttons.length; n++)
3562 button = a_buttons[n];
3563 if(button.id==id && button.status=='act')
3565 elm = document.getElementById(button.id);
3566 if (elm && button.over) {
3567 if (button.type == 'image')
3568 elm.src = button.over;
3570 elm.className = button.over;
3576 // mouse down on button
3577 this.button_sel = function(command, id)
3579 var a_buttons = this.buttons[command];
3582 if(!a_buttons || !a_buttons.length)
3585 for(var n=0; n<a_buttons.length; n++)
3587 button = a_buttons[n];
3588 if(button.id==id && button.status=='act')
3590 elm = document.getElementById(button.id);
3591 if (elm && button.sel) {
3592 if (button.type == 'image')
3593 elm.src = button.sel;
3595 elm.className = button.sel;
3597 this.buttons_sel[id] = command;
3602 // mouse out of button
3603 this.button_out = function(command, id)
3605 var a_buttons = this.buttons[command];
3608 if(!a_buttons || !a_buttons.length)
3611 for(var n=0; n<a_buttons.length; n++)
3613 button = a_buttons[n];
3614 if(button.id==id && button.status=='act')
3616 elm = document.getElementById(button.id);
3617 if (elm && button.act) {
3618 if (button.type == 'image')
3619 elm.src = button.act;
3621 elm.className = button.act;
3627 // write to the document/window title
3628 this.set_pagetitle = function(title)
3630 if (title && document.title)
3631 document.title = title;
3634 // display a system message
3635 this.display_message = function(msg, type, hold)
3637 if (!this.loaded) // save message in order to display after page loaded
3639 this.pending_message = new Array(msg, type);
3643 // pass command to parent window
3644 if (this.env.framed && parent.rcmail)
3645 return parent.rcmail.display_message(msg, type, hold);
3647 if (!this.gui_objects.message)
3650 if (this.message_timer)
3651 clearTimeout(this.message_timer);
3655 cont = '<div class="'+type+'">'+cont+'</div>';
3657 var obj = $(this.gui_objects.message).html(cont).show();
3659 if (type!='loading')
3660 obj.bind('mousedown', function(){ ref.hide_message(); return true; });
3663 this.message_timer = window.setTimeout(function(){ ref.hide_message(true); }, this.message_time);
3666 // make a message row disapear
3667 this.hide_message = function(fade)
3669 if (this.gui_objects.message)
3670 $(this.gui_objects.message).unbind()[(fade?'fadeOut':'hide')]();
3673 // mark a mailbox as selected and set environment variable
3674 this.select_folder = function(name, old)
3676 if (this.gui_objects.folderlist)
3678 var current_li, target_li;
3680 if ((current_li = this.get_folder_li(old))) {
3681 $(current_li).removeClass('selected').removeClass('unfocused');
3683 if ((target_li = this.get_folder_li(name))) {
3684 $(target_li).removeClass('unfocused').addClass('selected');
3687 // trigger event hook
3688 this.triggerEvent('selectfolder', { folder:name, old:old });
3692 // helper method to find a folder list item
3693 this.get_folder_li = function(name)
3695 if (this.gui_objects.folderlist)
3697 name = String(name).replace(this.identifier_expr, '_');
3698 return document.getElementById('rcmli'+name);
3704 // for reordering column array, Konqueror workaround
3705 this.set_message_coltypes = function(coltypes)
3707 this.coltypes = coltypes;
3709 // set correct list titles
3711 var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3712 for (var n=0; thead && n<this.coltypes.length; n++)
3714 col = this.coltypes[n];
3715 if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3717 // if we have links for sorting, it's a bit more complicated...
3718 if (cell.firstChild && cell.firstChild.tagName.toLowerCase()=='a')
3720 cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3721 cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3722 cell.firstChild.__col = col;
3725 cell.innerHTML = this.get_label(this.coltypes[n]);
3727 cell.id = 'rcm'+col;
3729 else if (col == 'subject' && this.message_list)
3730 this.message_list.subject_col = n+1;
3734 // create a table row in the message list
3735 this.add_message_row = function(uid, cols, flags, attachment, attop)
3737 if (!this.gui_objects.messagelist || !this.message_list)
3740 if (this.message_list.background)
3741 var tbody = this.message_list.background;
3743 var tbody = this.gui_objects.messagelist.tBodies[0];
3745 var rowcount = tbody.rows.length;
3746 var even = rowcount%2;
3748 this.env.messages[uid] = {
3749 deleted: flags.deleted?1:0,
3750 replied: flags.replied?1:0,
3751 unread: flags.unread?1:0,
3752 forwarded: flags.forwarded?1:0,
3753 flagged:flags.flagged?1:0
3756 var css_class = 'message'
3757 + (even ? ' even' : ' odd')
3758 + (flags.unread ? ' unread' : '')
3759 + (flags.deleted ? ' deleted' : '')
3760 + (flags.flagged ? ' flagged' : '')
3761 + (this.message_list.in_selection(uid) ? ' selected' : '');
3763 // for performance use DOM instead of jQuery here
3764 var row = document.createElement('tr');
3765 row.id = 'rcmrow'+uid;
3766 row.className = css_class;
3768 var icon = this.env.messageicon;
3769 if (flags.deleted && this.env.deletedicon)
3770 icon = this.env.deletedicon;
3771 else if (flags.replied && this.env.repliedicon)
3773 if (flags.forwarded && this.env.forwardedrepliedicon)
3774 icon = this.env.forwardedrepliedicon;
3776 icon = this.env.repliedicon;
3778 else if (flags.forwarded && this.env.forwardedicon)
3779 icon = this.env.forwardedicon;
3780 else if(flags.unread && this.env.unreadicon)
3781 icon = this.env.unreadicon;
3784 var col = document.createElement('td');
3785 col.className = 'icon';
3786 col.innerHTML = icon ? '<img src="'+icon+'" alt="" />' : '';
3787 row.appendChild(col);
3789 // add each submitted col
3790 for (var n = 0; n < this.coltypes.length; n++) {
3791 var c = this.coltypes[n];
3792 col = document.createElement('td');
3793 col.className = String(c).toLowerCase();
3796 if (flags.flagged && this.env.flaggedicon)
3797 col.innerHTML = '<img src="'+this.env.flaggedicon+'" alt="" />';
3798 else if(!flags.flagged && this.env.unflaggedicon)
3799 col.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
3801 else if (c=='attachment')
3802 col.innerHTML = (attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : ' ');
3804 col.innerHTML = cols[c];
3806 row.appendChild(col);
3809 this.message_list.insert_row(row, attop);
3812 if (attop && this.env.pagesize && this.message_list.rowcount > this.env.pagesize) {
3813 var uid = this.message_list.get_last_row();
3814 this.message_list.remove_row(uid);
3815 this.message_list.clear_selection(uid);
3819 // messages list handling in background (for performance)
3820 this.offline_message_list = function(flag)
3822 if (this.message_list)
3823 this.message_list.set_background_mode(flag);
3826 // replace content of row count display
3827 this.set_rowcount = function(text)
3829 $(this.gui_objects.countdisplay).html(text);
3831 // update page navigation buttons
3832 this.set_page_buttons();
3835 // replace content of mailboxname display
3836 this.set_mailboxname = function(content)
3838 if (this.gui_objects.mailboxname && content)
3839 this.gui_objects.mailboxname.innerHTML = content;
3842 // replace content of quota display
3843 this.set_quota = function(content)
3845 if (content && this.gui_objects.quotadisplay)
3846 $(this.gui_objects.quotadisplay).html(content);
3849 // update the mailboxlist
3850 this.set_unread_count = function(mbox, count, set_title)
3852 if (!this.gui_objects.mailboxlist)
3855 this.env.unread_counts[mbox] = count;
3856 this.set_unread_count_display(mbox, set_title);
3859 // update the mailbox count display
3860 this.set_unread_count_display = function(mbox, set_title)
3862 var reg, text_obj, item, mycount, childcount, div;
3863 if (item = this.get_folder_li(mbox))
3865 mycount = this.env.unread_counts[mbox] ? this.env.unread_counts[mbox] : 0;
3866 text_obj = item.getElementsByTagName('a')[0];
3867 reg = /\s+\([0-9]+\)$/i;
3870 if ((div = item.getElementsByTagName('div')[0]) &&
3871 div.className.match(/collapsed/))
3873 // add children's counters
3874 for (var k in this.env.unread_counts)
3875 if (k.indexOf(mbox + this.env.delimiter) == 0)
3876 childcount += this.env.unread_counts[k];
3879 if (mycount && text_obj.innerHTML.match(reg))
3880 text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+mycount+')');
3882 text_obj.innerHTML += ' ('+mycount+')';
3884 text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
3886 // set parent's display
3887 reg = new RegExp(RegExp.escape(this.env.delimiter) + '[^' + RegExp.escape(this.env.delimiter) + ']+$');
3888 if (mbox.match(reg))
3889 this.set_unread_count_display(mbox.replace(reg, ''), false);
3891 // set the right classes
3892 if ((mycount+childcount)>0)
3893 $(item).addClass('unread');
3895 $(item).removeClass('unread');
3898 // set unread count to window title
3899 reg = /^\([0-9]+\)\s+/i;
3900 if (set_title && document.title)
3902 var doc_title = String(document.title);
3905 if (mycount && doc_title.match(reg))
3906 new_title = doc_title.replace(reg, '('+mycount+') ');
3908 new_title = '('+mycount+') '+doc_title;
3910 new_title = doc_title.replace(reg, '');
3912 this.set_pagetitle(new_title);
3916 // notifies that a new message(s) has hit the mailbox
3917 this.new_message_focus = function()
3919 // focus main window
3920 if (this.env.framed && window.parent)
3921 window.parent.focus();
3926 this.toggle_prefer_html = function(checkbox)
3928 var addrbook_show_images;
3929 if (addrbook_show_images = document.getElementById('rcmfd_addrbook_show_images'))
3930 addrbook_show_images.disabled = !checkbox.checked;
3933 // display fetched raw headers
3934 this.set_headers = function(content)
3936 if (this.gui_objects.all_headers_row && this.gui_objects.all_headers_box && content) {
3937 $(this.gui_objects.all_headers_box).html(content).show();
3939 if (this.env.framed && parent.rcmail)
3940 parent.rcmail.set_busy(false);
3942 this.set_busy(false);
3946 // display all-headers row and fetch raw message headers
3947 this.load_headers = function(elem)
3949 if (!this.gui_objects.all_headers_row || !this.gui_objects.all_headers_box || !this.env.uid)
3952 $(elem).removeClass('show-headers').addClass('hide-headers');
3953 $(this.gui_objects.all_headers_row).show();
3954 elem.onclick = function() { rcmail.hide_headers(elem); }
3956 // fetch headers only once
3957 if (!this.gui_objects.all_headers_box.innerHTML)
3959 this.display_message(this.get_label('loading'), 'loading', true);
3960 this.http_post('headers', '_uid='+this.env.uid);
3964 // hide all-headers row
3965 this.hide_headers = function(elem)
3967 if (!this.gui_objects.all_headers_row || !this.gui_objects.all_headers_box)
3970 $(elem).removeClass('hide-headers').addClass('show-headers');
3971 $(this.gui_objects.all_headers_row).hide();
3972 elem.onclick = function() { rcmail.load_headers(elem); }
3976 /********************************************************/
3977 /********* html to text conversion functions *********/
3978 /********************************************************/
3980 this.html2plain = function(htmlText, id)
3982 var url = this.env.bin_path+'html2text.php';
3985 this.set_busy(true, 'converting');
3986 console.log('HTTP POST: '+url);
3988 $.ajax({ type: 'POST', url: url, data: htmlText, contentType: 'application/octet-stream',
3989 error: function(o) { rcmail.http_error(o); },
3990 success: function(data) { rcmail.set_busy(false); $(document.getElementById(id)).val(data); console.log(data); }
3994 this.plain2html = function(plainText, id)
3996 this.set_busy(true, 'converting');
3997 $(document.getElementById(id)).val('<pre>'+plainText+'</pre>');
3998 this.set_busy(false);
4002 /********************************************************/
4003 /********* remote request methods *********/
4004 /********************************************************/
4006 this.redirect = function(url, lock)
4008 if (lock || lock === null)
4009 this.set_busy(true);
4011 if (this.env.framed && window.parent)
4012 parent.location.href = url;
4014 location.href = url;
4017 this.goto_url = function(action, query, lock)
4019 var querystring = query ? '&'+query : '';
4020 this.redirect(this.env.comm_path+'&_action='+action+querystring, lock);
4023 // send a http request to the server
4024 this.http_request = function(action, querystring, lock)
4026 querystring += (querystring ? '&' : '') + '_remote=1';
4027 var url = this.env.comm_path + '&_action=' + action + '&' + querystring
4030 console.log('HTTP POST: ' + url);
4031 jQuery.get(url, { _unlock:(lock?1:0) }, function(data){ ref.http_response(data); }, 'json');
4034 // send a http POST request to the server
4035 this.http_post = function(action, postdata, lock)
4037 var url = this.env.comm_path+'&_action=' + action;
4039 if (postdata && typeof(postdata) == 'object') {
4040 postdata._remote = 1;
4041 postdata._unlock = (lock ? 1 : 0);
4044 postdata += (postdata ? '&' : '') + '_remote=1' + (lock ? '&_unlock=1' : '');
4047 console.log('HTTP POST: ' + url);
4048 jQuery.post(url, postdata, function(data){ ref.http_response(data); }, 'json');
4051 // handle HTTP response
4052 this.http_response = function(response)
4054 var console_msg = '';
4056 if (response.unlock)
4057 this.set_busy(false);
4061 this.set_env(response.env);
4063 // we have labels to add
4064 if (typeof response.texts == 'object') {
4065 for (var name in response.texts)
4066 if (typeof response.texts[name] == 'string')
4067 this.add_label(name, response.texts[name]);
4070 // if we get javascript code from server -> execute it
4071 if (response.exec) {
4072 console.log(response.exec);
4073 eval(response.exec);
4076 // execute callback functions of plugins
4077 if (response.callbacks && response.callbacks.length) {
4078 for (var i=0; i < response.callbacks.length; i++)
4079 this.triggerEvent(response.callbacks[i][0], response.callbacks[i][1]);
4082 // process the response data according to the sent action
4083 switch (response.action) {
4085 if (this.task == 'addressbook') {
4086 var uid = this.contact_list.get_selection();
4087 this.enable_command('compose', (uid && this.contact_list.rows[uid]));
4088 this.enable_command('delete', 'edit', (uid && this.contact_list.rows[uid] && this.env.address_sources && !this.env.address_sources[this.env.source].readonly));
4089 this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
4093 if (this.env.action == 'show') {
4094 // re-enable commands on move/delete error
4095 this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', 'open', 'edit', 'viewsource', 'download', true);
4096 } else if (this.message_list)
4097 this.message_list.init();
4102 if (!this.env.messagecount && this.task == 'mail') {
4103 // clear preview pane content
4104 if (this.env.contentframe)
4105 this.show_contentframe(false);
4106 // disable commands useless when mailbox is empty
4107 this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete',
4108 'mark', 'viewsource', 'open', 'edit', 'download', 'print', 'load-attachment',
4109 'purge', 'expunge', 'select-all', 'select-none', 'sort', false);
4113 case 'check-recent':
4116 if (this.task == 'mail') {
4117 if (this.message_list && response.action == 'list')
4118 this.msglist_select(this.message_list);
4119 this.enable_command('show', 'expunge', 'select-all', 'select-none', 'sort', (this.env.messagecount > 0));
4120 this.enable_command('purge', this.purge_mailbox_test());
4122 if (response.action == 'list')
4123 this.triggerEvent('listupdate', { folder:this.env.mailbox, rowcount:this.message_list.rowcount });
4125 else if (this.task == 'addressbook') {
4126 this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
4128 if (response.action == 'list')
4129 this.triggerEvent('listupdate', { folder:this.env.source, rowcount:this.contact_list.rowcount });
4135 // handle HTTP request errors
4136 this.http_error = function(request, status, err)
4138 var errmsg = request.statusText;
4140 this.set_busy(false);
4144 this.display_message(this.get_label('servererror') + ' (' + errmsg + ')', 'error');
4147 // use an image to send a keep-alive siganl to the server
4148 this.send_keep_alive = function()
4151 this.http_request('keep-alive', '_t='+d.getTime());
4154 // send periodic request to check for recent messages
4155 this.check_for_recent = function(setbusy)
4161 this.set_busy(true, 'checkingmail');
4163 var addurl = '_t=' + (new Date().getTime());
4165 if (this.gui_objects.messagelist)
4166 addurl += '&_list=1';
4167 if (this.gui_objects.quotadisplay)
4168 addurl += '&_quota=1';
4169 if (this.env.search_request)
4170 addurl += '&_search=' + this.env.search_request;
4172 this.http_request('check-recent', addurl, true);
4176 /********************************************************/
4177 /********* helper methods *********/
4178 /********************************************************/
4180 // check if we're in show mode or if we have a unique selection
4181 // and return the message uid
4182 this.get_single_uid = function()
4184 return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
4187 // same as above but for contacts
4188 this.get_single_cid = function()
4190 return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
4194 this.get_caret_pos = function(obj)
4196 if (typeof(obj.selectionEnd)!='undefined')
4197 return obj.selectionEnd;
4198 else if (document.selection && document.selection.createRange)
4200 var range = document.selection.createRange();
4201 if (range.parentElement()!=obj)
4204 var gm = range.duplicate();
4205 if (obj.tagName=='TEXTAREA')
4206 gm.moveToElementText(obj);
4208 gm.expand('textedit');
4210 gm.setEndPoint('EndToStart', range);
4211 var p = gm.text.length;
4213 return p<=obj.value.length ? p : -1;
4216 return obj.value.length;
4219 this.set_caret_pos = function(obj, pos)
4221 if (obj.setSelectionRange)
4222 obj.setSelectionRange(pos, pos);
4223 else if (obj.createTextRange)
4225 var range = obj.createTextRange();
4226 range.collapse(true);
4227 range.moveEnd('character', pos);
4228 range.moveStart('character', pos);
4233 // set all fields of a form disabled
4234 this.lock_form = function(form, lock)
4236 if (!form || !form.elements)
4240 for (var n=0; n<form.elements.length; n++)
4242 type = form.elements[n];
4246 form.elements[n].disabled = lock;
4250 } // end object rcube_webmail
4253 // copy event engine prototype
4254 rcube_webmail.prototype.addEventListener = rcube_event_engine.prototype.addEventListener;
4255 rcube_webmail.prototype.removeEventListener = rcube_event_engine.prototype.removeEventListener;
4256 rcube_webmail.prototype.triggerEvent = rcube_event_engine.prototype.triggerEvent;