2 +-----------------------------------------------------------------------+
3 | RoundCube Webmail Client Script |
5 | This file is part of the RoundCube Webmail client |
6 | Copyright (C) 2005-2008, 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: common.js, list.js |
14 +-----------------------------------------------------------------------+
16 $Id: app.js 1170 2008-03-03 09:06:53Z thomasb $
20 var rcube_webmail_client;
22 function rcube_webmail()
24 this.env = new Object();
25 this.labels = new Object();
26 this.buttons = new Object();
27 this.gui_objects = new Object();
28 this.commands = new Object();
29 this.onloads = new Array();
31 // create protected reference to myself
32 rcube_webmail_client = this;
33 this.ref = 'rcube_webmail_client';
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';
57 // set environment variable(s)
58 this.set_env = function(p, value)
60 if (p != null && typeof(p) == 'object' && !value)
68 // add a localized label to the client environment
69 this.add_label = function(key, value)
71 this.labels[key] = value;
75 // add a button to the button list
76 this.register_button = function(command, id, type, act, sel, over)
78 if (!this.buttons[command])
79 this.buttons[command] = new Array();
81 var button_prop = {id:id, type:type};
82 if (act) button_prop.act = act;
83 if (sel) button_prop.sel = sel;
84 if (over) button_prop.over = over;
86 this.buttons[command][this.buttons[command].length] = button_prop;
89 // register a specific gui object
90 this.gui_object = function(name, id)
92 this.gui_objects[name] = id;
95 // execute the given script on load
96 this.add_onload = function(f)
98 this.onloads[this.onloads.length] = f;
101 // initialize webmail client
102 this.init = function()
105 this.task = this.env.task;
108 if (!bw.dom || !bw.xmlhttp_test())
110 this.goto_url('error', '_code=0x199');
114 // find all registered gui objects
115 for (var n in this.gui_objects)
116 this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
118 // tell parent window that this frame is loaded
119 if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
120 parent.rcmail.set_busy(false);
122 // enable general commands
123 this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
128 if (this.gui_objects.messagelist)
130 this.message_list = new rcube_list_widget(this.gui_objects.messagelist, {multiselect:true, draggable:true, keyboard:true, dblclick_time:this.dblclick_time});
131 this.message_list.row_init = function(o){ p.init_message_row(o); };
132 this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
133 this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
134 this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
135 this.message_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
136 this.message_list.addEventListener('dragend', function(o){ p.drag_active = false; });
138 this.message_list.init();
139 this.enable_command('toggle_status', true);
141 if (this.gui_objects.mailcontframe)
143 this.gui_objects.mailcontframe.onmousedown = function(e){ return p.click_on_list(e); };
144 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
147 this.message_list.focus();
150 if (this.env.coltypes)
151 this.set_message_coltypes(this.env.coltypes);
153 // enable mail commands
154 this.enable_command('list', 'checkmail', 'compose', 'add-contact', 'search', 'reset-search', true);
156 if (this.env.search_text != null && document.getElementById('quicksearchbox') != null)
157 document.getElementById('quicksearchbox').value = this.env.search_text;
159 if (this.env.action=='show' || this.env.action=='preview')
161 this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'mark', 'viewsource', 'print', 'load-attachment', true);
162 if (this.env.next_uid)
164 this.enable_command('nextmessage', true);
165 this.enable_command('lastmessage', true);
167 if (this.env.prev_uid)
169 this.enable_command('previousmessage', true);
170 this.enable_command('firstmessage', true);
174 if (this.env.trash_mailbox && this.env.mailbox != this.env.trash_mailbox)
175 this.set_alttext('delete', 'movemessagetotrash');
177 // make preview/message frame visible
178 if (this.env.action == 'preview' && this.env.framed && parent.rcmail)
180 this.enable_command('compose', 'add-contact', false);
181 parent.rcmail.show_contentframe(true);
182 parent.rcmail.mark_message('read', this.env.uid);
185 if ((this.env.action=='show' || this.env.action=='preview') && this.env.blockedobjects)
187 if (this.gui_objects.remoteobjectsmsg)
188 this.gui_objects.remoteobjectsmsg.style.display = 'block';
189 this.enable_command('load-images', true);
192 if (this.env.action=='compose')
194 this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
195 if (this.env.spellcheck)
197 this.env.spellcheck.spelling_state_observer = function(s){ ref.set_spellcheck_state(s); };
198 this.set_spellcheck_state('ready');
200 if (this.env.drafts_mailbox)
201 this.enable_command('savedraft', true);
204 if (this.env.messagecount)
205 this.enable_command('select-all', 'select-none', 'sort', 'expunge', true);
207 if (this.env.messagecount && (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox))
208 this.enable_command('purge', true);
210 this.set_page_buttons();
213 if (this.env.framed && window.parent)
214 window.parent.focus();
218 // init message compose form
219 if (this.env.action=='compose')
220 this.init_messageform();
222 // show printing dialog
223 if (this.env.action=='print')
226 // get unread count for each mailbox
227 if (this.gui_objects.mailboxlist)
229 this.gui_objects.folderlist = this.gui_objects.mailboxlist;
230 this.http_request('getunread', '');
233 // ask user to send MDN
234 if (this.env.mdn_request && this.env.uid)
236 var mdnurl = '_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox);
237 if (confirm(this.get_label('mdnrequest')))
238 this.http_post('sendmdn', mdnurl);
240 this.http_post('mark', mdnurl+'&_flag=mdnsent');
247 if (this.gui_objects.contactslist)
249 this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
250 this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
251 this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
252 this.contact_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
253 this.contact_list.addEventListener('dragend', function(o){ p.drag_active = false; });
254 this.contact_list.init();
257 this.contact_list.highlight_row(this.env.cid);
259 if (this.gui_objects.contactslist.parentNode)
261 this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
262 document.onmouseup = function(e){ return p.doc_mouse_up(e); };
265 this.contact_list.focus();
268 this.set_page_buttons();
270 if (this.env.address_sources && !this.env.address_sources[this.env.source].readonly)
271 this.enable_command('add', true);
274 this.enable_command('show', 'edit', true);
276 if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
277 this.enable_command('save', true);
279 this.enable_command('search', 'reset-search', 'moveto', true);
281 this.enable_command('list', true);
286 this.enable_command('preferences', 'identities', 'save', 'folders', true);
288 if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
289 this.enable_command('edit', 'add', 'delete', true);
291 if (this.env.action=='edit-identity' || this.env.action=='add-identity')
292 this.enable_command('save', true);
294 if (this.env.action=='folders')
295 this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', true);
297 if (this.gui_objects.identitieslist)
299 this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
300 this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
301 this.identity_list.init();
302 this.identity_list.focus();
305 this.identity_list.highlight_row(this.env.iid);
308 if (this.gui_objects.subscriptionlist)
309 this.init_subscription_list();
314 var input_user = rcube_find_object('rcmloginuser');
315 var input_pass = rcube_find_object('rcmloginpwd');
317 input_user.onkeypress = function(e){ return rcmail.login_user_keypress(e); };
318 if (input_user && input_user.value=='')
323 this.enable_command('login', true);
331 // enable basic commands
332 this.enable_command('logout', true);
334 // flag object as complete
338 if (this.pending_message)
339 this.display_message(this.pending_message[0], this.pending_message[1]);
341 // start keep-alive interval
342 this.start_keepalive();
345 // execute all foreign onload scripts
346 for (var i=0; i<this.onloads.length; i++)
348 if (typeof(this.onloads[i]) == 'string')
349 eval(this.onloads[i]);
350 else if (typeof(this.onloads[i]) == 'function')
356 // start interval for keep-alive/recent_check signal
357 this.start_keepalive = function()
359 if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.messagelist)
360 this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
361 else if (this.env.keep_alive && !this.env.framed && this.task!='login')
362 this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
366 this.init_message_row = function(row)
369 if (uid && this.env.messages[uid])
371 row.deleted = this.env.messages[uid].deleted ? true : false;
372 row.unread = this.env.messages[uid].unread ? true : false;
373 row.replied = this.env.messages[uid].replied ? true : false;
376 // set eventhandler to message icon
377 if ((row.icon = row.obj.cells[0].childNodes[0]) && row.icon.nodeName=='IMG')
380 row.icon.id = 'msgicn_'+row.uid;
381 row.icon._row = row.obj;
382 row.icon.onmousedown = function(e) { p.command('toggle_status', this); };
387 // init message compose form: set focus and eventhandlers
388 this.init_messageform = function()
390 if (!this.gui_objects.messageform)
393 //this.messageform = this.gui_objects.messageform;
394 var input_from = rcube_find_object('_from');
395 var input_to = rcube_find_object('_to');
396 var input_cc = rcube_find_object('_cc');
397 var input_bcc = rcube_find_object('_bcc');
398 var input_replyto = rcube_find_object('_replyto');
399 var input_subject = rcube_find_object('_subject');
400 var input_message = rcube_find_object('_message');
402 // init live search events
404 this.init_address_input_events(input_to);
406 this.init_address_input_events(input_cc);
408 this.init_address_input_events(input_bcc);
410 // add signature according to selected identity
411 if (input_from && input_from.type=='select-one')
412 this.change_identity(input_from);
414 if (input_to && input_to.value=='')
416 else if (input_subject && input_subject.value=='')
417 input_subject.focus();
418 else if (input_message)
419 this.set_caret2start(input_message);
421 // get summary of all field values
422 this.compose_field_hash(true);
424 // start the auto-save timer
425 this.auto_save_start();
428 this.init_address_input_events = function(obj)
430 var handler = function(e){ return ref.ksearch_keypress(e,this); };
431 var handler2 = function(e){ return ref.ksearch_blur(e,this); };
433 if (obj.addEventListener)
435 obj.addEventListener(bw.safari ? 'keydown' : 'keypress', handler, false);
436 obj.addEventListener('blur', handler2, false);
440 obj.onkeydown = handler;
441 obj.onblur = handler2;
444 obj.setAttribute('autocomplete', 'off');
449 /*********************************************************/
450 /********* client command interface *********/
451 /*********************************************************/
454 // execute a specific command on the web client
455 this.command = function(command, props, obj)
463 // command not supported or allowed
464 if (!this.commands[command])
466 // pass command to parent window
467 if (this.env.framed && parent.rcmail && parent.rcmail.command)
468 parent.rcmail.command(command, props);
474 // check input before leaving compose step
475 if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
477 if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
486 if (this.gui_objects.loginform)
487 this.gui_objects.loginform.submit();
491 this.goto_url('logout', true);
494 // commands to switch task
498 this.switch_task(command);
502 // misc list commands
504 if (this.task=='mail')
506 if (this.env.search_request<0 || (props != '' && (this.env.search_request && props != this.env.mailbox)))
507 this.reset_qsearch();
509 this.list_mailbox(props);
511 if (this.env.trash_mailbox)
512 this.set_alttext('delete', this.env.mailbox != this.env.trash_mailbox ? 'movemessagetotrash' : 'deletemessage');
514 else if (this.task=='addressbook')
516 if (this.env.search_request<0 || (this.env.search_request && props != this.env.source))
517 this.reset_qsearch();
519 this.list_contacts(props);
520 this.enable_command('add', (this.env.address_sources && !this.env.address_sources[props].readonly));
526 // get the type of sorting
527 var a_sort = props.split('_');
528 var sort_col = a_sort[0];
529 var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
532 // no sort order specified: toggle
533 if (sort_order==null)
535 if (this.env.sort_col==sort_col)
536 sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
538 sort_order = this.env.sort_order;
541 if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
544 // set table header class
545 if (header = document.getElementById('rcmHead'+this.env.sort_col))
546 this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
547 if (header = document.getElementById('rcmHead'+sort_col))
548 this.set_classname(header, 'sorted'+sort_order, true);
550 // save new sort properties
551 this.env.sort_col = sort_col;
552 this.env.sort_order = sort_order;
554 // reload message list
555 this.list_mailbox('', '', sort_col+'_'+sort_order);
559 this.list_page('next');
563 this.list_page('last');
567 this.list_page('prev');
571 this.list_page('first');
575 if (this.env.messagecount)
576 this.expunge_mailbox(this.env.mailbox);
580 case 'empty-mailbox':
581 if (this.env.messagecount)
582 this.purge_mailbox(this.env.mailbox);
586 // common commands used in multiple tasks
588 if (this.task=='mail')
590 var uid = this.get_single_uid();
591 if (uid && (!this.env.uid || uid != this.env.uid))
593 if (this.env.mailbox == this.env.drafts_mailbox)
594 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
596 this.show_message(uid);
599 else if (this.task=='addressbook')
601 var cid = props ? props : this.get_single_cid();
602 if (cid && !(this.env.action=='show' && cid==this.env.cid))
603 this.load_contact(cid, 'show');
608 if (this.task=='addressbook')
609 this.load_contact(0, 'add');
610 else if (this.task=='settings')
612 this.identity_list.clear_selection();
613 this.load_identity(0, 'add-identity');
619 if (this.task=='addressbook' && (cid = this.get_single_cid()))
620 this.load_contact(cid, 'edit');
621 else if (this.task=='settings' && props)
622 this.load_identity(props, 'edit-identity');
625 case 'save-identity':
627 if (this.gui_objects.editform)
629 var input_pagesize = rcube_find_object('_pagesize');
630 var input_name = rcube_find_object('_name');
631 var input_email = rcube_find_object('_email');
634 if (input_pagesize && isNaN(input_pagesize.value))
636 alert(this.get_label('nopagesizewarning'));
637 input_pagesize.focus();
640 // contacts/identities
643 if (input_name && input_name.value == '')
645 alert(this.get_label('nonamewarning'));
649 else if (input_email && !rcube_check_email(input_email.value))
651 alert(this.get_label('noemailwarning'));
657 this.gui_objects.editform.submit();
663 if (this.task=='mail')
664 this.delete_messages();
666 else if (this.task=='addressbook')
667 this.delete_contacts();
668 // user settings task
669 else if (this.task=='settings')
670 this.delete_identity();
674 // mail task commands
677 if (this.task == 'mail')
678 this.move_messages(props);
679 else if (this.task == 'addressbook' && this.drag_active)
680 this.copy_contact(null, props);
685 this.mark_message(props);
688 case 'toggle_status':
689 if (props && !props._row)
697 uid = props._row.uid;
699 // toggle read/unread
700 if (this.message_list.rows[uid].deleted) {
702 } else if (!this.message_list.rows[uid].unread)
706 this.mark_message(flag, uid);
711 this.show_message(this.env.uid, true, this.env.action=='preview');
714 case 'load-attachment':
715 var qstring = '_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
717 // open attachment in frame if it's of a supported mimetype
718 if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
720 if (props.mimetype == 'text/html')
721 qstring += '&_safe=1';
722 this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
723 if (this.attachment_win)
725 setTimeout(function(){ ref.attachment_win.focus(); }, 10);
730 this.goto_url('get', qstring+'&_download=1', false);
734 this.message_list.select_all(props);
738 this.message_list.clear_selection();
742 if (this.env.next_uid)
743 this.show_message(this.env.next_uid, false, this.env.action=='preview');
747 if (this.env.last_uid)
748 this.show_message(this.env.last_uid);
751 case 'previousmessage':
752 if (this.env.prev_uid)
753 this.show_message(this.env.prev_uid, false, this.env.action=='preview');
757 if (this.env.first_uid)
758 this.show_message(this.env.first_uid);
762 this.check_for_recent();
766 var url = this.env.comm_path+'&_action=compose';
768 if (this.task=='mail' && this.env.mailbox==this.env.drafts_mailbox)
771 if (uid = this.get_single_uid())
772 url += '&_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox);
774 // modify url if we're in addressbook
775 else if (this.task=='addressbook')
777 // switch to mail compose step directly
778 if (props && props.indexOf('@') > 0)
780 url = this.get_task_url('mail', url);
781 this.redirect(url + '&_to='+urlencode(props));
785 // use contact_id passed as command parameter
786 var a_cids = new Array();
788 a_cids[a_cids.length] = props;
789 // get selected contacts
790 else if (this.contact_list)
792 var selection = this.contact_list.get_selection();
793 for (var n=0; n<selection.length; n++)
794 a_cids[a_cids.length] = selection[n];
798 this.http_request('mailto', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source), true);
803 url += '&_to='+urlencode(props);
805 // don't know if this is necessary...
806 url = url.replace(/&_framed=1/, "");
812 if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready)
814 this.env.spellcheck.spellCheck(this.env.spellcheck.check_link);
815 this.set_spellcheck_state('checking');
820 // Reset the auto-save timer
821 self.clearTimeout(this.save_timer);
823 if (!this.gui_objects.messageform)
826 // if saving Drafts is disabled in main.inc.php
827 // or if compose form did not change
828 if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
831 this.set_busy(true, 'savingmessage');
832 var form = this.gui_objects.messageform;
833 form.target = "savetarget";
834 form._draft.value = '1';
839 if (!this.gui_objects.messageform)
842 if (!this.check_compose_input())
845 // Reset the auto-save timer
846 self.clearTimeout(this.save_timer);
848 // all checks passed, send message
849 this.set_busy(true, 'sendingmessage');
850 var form = this.gui_objects.messageform;
851 form.target = "savetarget";
852 form._draft.value = '';
855 // clear timeout (sending could take longer)
856 clearTimeout(this.request_timer);
859 case 'add-attachment':
860 this.show_attachment_form(true);
862 case 'send-attachment':
863 // Reset the auto-save timer
864 self.clearTimeout(this.save_timer);
866 this.upload_file(props)
869 case 'remove-attachment':
870 this.remove_attachment(props);
876 if (uid = this.get_single_uid())
877 this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
882 if (uid = this.get_single_uid())
883 this.goto_url('compose', '_forward_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
888 if (uid = this.get_single_uid())
890 ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
893 setTimeout(function(){ ref.printwin.focus(); }, 20);
894 if (this.env.action != 'show')
895 this.toggle_read_status('read', [uid]);
902 if (uid = this.get_single_uid())
904 ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox));
906 setTimeout(function(){ ref.sourcewin.focus(); }, 20);
911 this.add_contact(props);
916 if (!props && this.gui_objects.qsearchbox)
917 props = this.gui_objects.qsearchbox.value;
926 var s = this.env.search_request;
927 this.reset_qsearch();
929 if (s && this.env.mailbox)
930 this.list_mailbox(this.env.mailbox);
931 else if (s && this.task == 'addressbook')
932 this.list_contacts(this.env.source);
936 // user settings commands
942 this.goto_url('identities');
945 case 'delete-identity':
946 this.delete_identity();
949 this.goto_url('folders');
953 this.subscribe_folder(props);
957 this.unsubscribe_folder(props);
960 case 'create-folder':
961 this.create_folder(props);
964 case 'rename-folder':
965 this.rename_folder(props);
968 case 'delete-folder':
969 this.delete_folder(props);
974 return obj ? false : true;
978 // set command enabled or disabled
979 this.enable_command = function()
981 var args = arguments;
982 if(!args.length) return -1;
985 var enable = args[args.length-1];
987 for(var n=0; n<args.length-1; n++)
990 this.commands[command] = enable;
991 this.set_button(command, (enable ? 'act' : 'pas'));
997 // lock/unlock interface
998 this.set_busy = function(a, message)
1002 var msg = this.get_label(message);
1006 this.display_message(msg, 'loading', true);
1009 this.hide_message();
1012 //document.body.style.cursor = a ? 'wait' : 'default';
1014 if (this.gui_objects.editform)
1015 this.lock_form(this.gui_objects.editform, a);
1017 // clear pending timer
1018 if (this.request_timer)
1019 clearTimeout(this.request_timer);
1021 // set timer for requests
1022 if (a && this.env.request_timeout)
1023 this.request_timer = setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
1027 // return a localized string
1028 this.get_label = function(name)
1030 if (this.labels[name])
1031 return this.labels[name];
1037 // switch to another application task
1038 this.switch_task = function(task)
1040 if (this.task===task && task!='mail')
1043 var url = this.get_task_url(task);
1045 url += '&_mbox=INBOX';
1051 this.get_task_url = function(task, url)
1054 url = this.env.comm_path;
1056 return url.replace(/_task=[a-z]+/, '_task='+task);
1060 // called when a request timed out
1061 this.request_timed_out = function()
1063 this.set_busy(false);
1064 this.display_message('Request timed out!', 'error');
1068 /*********************************************************/
1069 /********* event handling methods *********/
1070 /*********************************************************/
1073 this.doc_mouse_up = function(e)
1075 if (this.message_list)
1076 this.message_list.blur();
1077 else if (this.contact_list)
1078 this.contact_list.blur();
1081 this.focus_folder = function(id)
1084 if (this.drag_active && this.check_droptarget(id) && (li = this.get_folder_li(id)))
1085 this.set_classname(li, 'droptarget', true);
1088 this.unfocus_folder = function(id)
1091 if (this.drag_active && (li = this.get_folder_li(id)))
1092 this.set_classname(li, 'droptarget', false);
1095 // onmouseup handler for folder list item
1096 this.folder_mouse_up = function(id)
1098 if (this.drag_active)
1100 this.unfocus_folder(id);
1101 this.command('moveto', id);
1105 this.click_on_list = function(e)
1107 if (this.message_list)
1108 this.message_list.focus();
1109 else if (this.contact_list)
1110 this.contact_list.focus();
1113 if (mbox_li = this.get_folder_li())
1114 this.set_classname(mbox_li, 'unfocused', true);
1116 rcube_event.cancel(e);
1120 this.msglist_select = function(list)
1122 if (this.preview_timer)
1123 clearTimeout(this.preview_timer);
1125 var selected = list.selection.length==1;
1127 // Hide certain command buttons when Drafts folder is selected
1128 if (this.env.mailbox == this.env.drafts_mailbox)
1130 this.enable_command('reply', 'reply-all', 'forward', false);
1131 this.enable_command('show', selected);
1132 this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
1136 this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
1137 this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
1140 // start timer for message preview (wait for double click)
1141 if (selected && this.env.contentframe)
1142 this.preview_timer = setTimeout(function(){ ref.msglist_get_preview(); }, this.dblclick_time + 10);
1143 else if (this.env.contentframe)
1144 this.show_contentframe(false);
1148 this.msglist_dbl_click = function(list)
1150 if (this.preview_timer)
1151 clearTimeout(this.preview_timer);
1153 var uid = list.get_single_selection();
1154 if (uid && this.env.mailbox == this.env.drafts_mailbox)
1155 this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1157 this.show_message(uid, false, false);
1161 this.msglist_keypress = function(list)
1163 if (list.key_pressed == list.ENTER_KEY)
1164 this.command('show');
1165 else if (list.key_pressed == list.DELETE_KEY)
1166 this.command('delete');
1168 list.shiftkey = false;
1172 this.msglist_get_preview = function()
1174 var uid = this.get_single_uid();
1175 if (uid && this.env.contentframe && !this.drag_active)
1176 this.show_message(uid, false, true);
1177 else if (this.env.contentframe)
1178 this.show_contentframe(false);
1182 this.check_droptarget = function(id)
1184 if (this.task == 'mail')
1185 return (id != this.env.mailbox);
1186 else if (this.task == 'addressbook')
1187 return (id != this.env.source && this.env.address_sources[id] && !this.env.address_sources[id].readonly);
1188 else if (this.task == 'settings')
1189 return (id != this.env.folder);
1193 /*********************************************************/
1194 /********* (message) list functionality *********/
1195 /*********************************************************/
1198 // when user doble-clicks on a row
1199 this.show_message = function(id, safe, preview)
1202 var action = preview ? 'preview': 'show';
1203 var target = window;
1204 if (preview && this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1206 target = window.frames[this.env.contentframe];
1207 add_url = '&_framed=1';
1211 add_url = '&_safe=1';
1213 // also send search request to get the right messages
1214 if (this.env.search_request)
1215 add_url += '&_search='+this.env.search_request;
1219 var url = '&_action='+action+'&_uid='+id+'&_mbox='+urlencode(this.env.mailbox)+add_url;
1220 if (action == 'preview' && String(target.location.href).indexOf(url) >= 0)
1221 this.show_contentframe(true);
1224 this.set_busy(true, 'loading');
1225 target.location.href = this.env.comm_path+url;
1231 this.show_contentframe = function(show)
1234 if (this.env.contentframe && (frm = rcube_find_object(this.env.contentframe)))
1236 if (!show && window.frames[this.env.contentframe] && frames[this.env.contentframe].location.href.indexOf(this.env.blankpage)<0)
1237 frames[this.env.contentframe].location.href = this.env.blankpage;
1239 frm.style.display = show ? 'block' : 'none';
1242 if (!show && this.busy)
1243 this.set_busy(false);
1247 // list a specific page
1248 this.list_page = function(page)
1251 page = this.env.current_page+1;
1253 page = this.env.pagecount;
1254 if (page=='prev' && this.env.current_page>1)
1255 page = this.env.current_page-1;
1256 if (page=='first' && this.env.current_page>1)
1259 if (page > 0 && page <= this.env.pagecount)
1261 this.env.current_page = page;
1263 if (this.task=='mail')
1264 this.list_mailbox(this.env.mailbox, page);
1265 else if (this.task=='addressbook')
1266 this.list_contacts(this.env.source, page);
1271 // list messages of a specific mailbox
1272 this.list_mailbox = function(mbox, page, sort)
1274 this.last_selected = 0;
1276 var target = window;
1279 mbox = this.env.mailbox;
1281 // add sort to url if set
1283 add_url += '&_sort=' + sort;
1285 // also send search request to get the right messages
1286 if (this.env.search_request)
1287 add_url += '&_search='+this.env.search_request;
1289 // set page=1 if changeing to another mailbox
1290 if (!page && mbox != this.env.mailbox)
1293 this.env.current_page = page;
1294 if (this.message_list)
1295 this.message_list.clear_selection();
1296 this.show_contentframe(false);
1299 if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
1300 add_url += '&_refresh=1';
1302 this.select_folder(mbox, this.env.mailbox);
1303 this.env.mailbox = mbox;
1305 // load message list remotely
1306 if (this.gui_objects.messagelist)
1308 this.list_mailbox_remote(mbox, page, add_url);
1312 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1314 target = window.frames[this.env.contentframe];
1315 add_url += '&_framed=1';
1318 // load message list to target frame/window
1321 this.set_busy(true, 'loading');
1322 target.location.href = this.env.comm_path+'&_mbox='+urlencode(mbox)+(page ? '&_page='+page : '')+add_url;
1327 // send remote request to load message list
1328 this.list_mailbox_remote = function(mbox, page, add_url)
1330 // clear message list first
1331 this.message_list.clear();
1333 // send request to server
1334 var url = '_mbox='+urlencode(mbox)+(page ? '&_page='+page : '');
1335 this.set_busy(true, 'loading');
1336 this.http_request('list', url+add_url, true);
1340 this.expunge_mailbox = function(mbox)
1345 // lock interface if it's the active mailbox
1346 if (mbox == this.env.mailbox)
1349 this.set_busy(true, 'loading');
1350 add_url = '&_reload=1';
1353 // send request to server
1354 var url = '_mbox='+urlencode(mbox);
1355 this.http_post('expunge', url+add_url, lock);
1359 this.purge_mailbox = function(mbox)
1364 if (!confirm(this.get_label('purgefolderconfirm')))
1367 // lock interface if it's the active mailbox
1368 if (mbox == this.env.mailbox)
1371 this.set_busy(true, 'loading');
1372 add_url = '&_reload=1';
1375 // send request to server
1376 var url = '_mbox='+urlencode(mbox);
1377 this.http_post('purge', url+add_url, lock);
1382 // move selected messages to the specified mailbox
1383 this.move_messages = function(mbox)
1385 // exit if current or no mailbox specified or if selection is empty
1386 if (!mbox || mbox == this.env.mailbox || (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length)))
1390 var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
1392 // show wait message
1393 if (this.env.action=='show')
1396 this.set_busy(true, 'movingmessage');
1399 this.show_contentframe(false);
1401 // Hide message command buttons until a message is selected
1402 this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', false);
1404 this._with_selected_messages('moveto', lock, add_url);
1407 // delete selected messages from the current mailbox
1408 this.delete_messages = function()
1410 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1412 // exit if no mailbox specified or if selection is empty
1413 if (!this.env.uid && !selection.length)
1416 // if there is a trash mailbox defined and we're not currently in it:
1417 if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() != String(this.env.trash_mailbox).toLowerCase())
1419 // if shift was pressed delete it immediately
1420 if (this.message_list && this.message_list.shiftkey)
1422 if (confirm(this.get_label('deletemessagesconfirm')))
1423 this.permanently_remove_messages();
1426 this.move_messages(this.env.trash_mailbox);
1428 // if there is a trash mailbox defined but we *are* in it:
1429 else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1430 this.permanently_remove_messages();
1431 // if there isn't a defined trash mailbox and the config is set to flag for deletion
1432 else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
1434 this.mark_message('delete');
1435 if(this.env.action=="show")
1436 this.command('nextmessage','',this);
1437 else if (selection.length == 1)
1438 this.message_list.select_next();
1440 // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
1441 else if (!this.env.trash_mailbox)
1442 this.permanently_remove_messages();
1446 // delete the selected messages permanently
1447 this.permanently_remove_messages = function()
1449 // exit if no mailbox specified or if selection is empty
1450 if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1453 this.show_contentframe(false);
1454 this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
1457 // Send a specifc request with UIDs of all selected messages
1459 this._with_selected_messages = function(action, lock, add_url)
1461 var a_uids = new Array();
1463 a_uids[a_uids.length] = this.env.uid;
1466 var selection = this.message_list.get_selection();
1468 for (var n=0; n<selection.length; n++)
1471 a_uids[a_uids.length] = id;
1472 this.message_list.remove_row(id, (n == selection.length-1));
1476 // also send search request to get the right messages
1477 if (this.env.search_request)
1478 add_url += '&_search='+this.env.search_request;
1480 // send request to server
1481 this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
1485 // set a specific flag to one or more messages
1486 this.mark_message = function(flag, uid)
1488 var a_uids = new Array();
1489 var selection = this.message_list ? this.message_list.get_selection() : new Array();
1493 else if (this.env.uid)
1494 a_uids[0] = this.env.uid;
1495 else if (this.message_list)
1497 for (var id, n=0; n<selection.length; n++)
1500 if ((flag=='read' && this.message_list.rows[id].unread) || (flag=='unread' && !this.message_list.rows[id].unread)
1501 || (flag=='delete' && !this.message_list.rows[id].deleted) || (flag=='undelete' && this.message_list.rows[id].deleted))
1502 a_uids[a_uids.length] = id;
1514 this.toggle_read_status(flag, a_uids);
1518 this.toggle_delete_status(a_uids);
1523 // set class to read/unread
1524 this.toggle_read_status = function(flag, a_uids)
1526 // mark all message rows as read/unread
1528 var rows = this.message_list.rows;
1529 for (var i=0; i<a_uids.length; i++)
1534 rows[uid].unread = (flag=='unread' ? true : false);
1536 if (rows[uid].classname.indexOf('unread')<0 && rows[uid].unread)
1538 rows[uid].classname += ' unread';
1539 this.set_classname(rows[uid].obj, 'unread', true);
1541 if (this.env.unreadicon)
1542 icn_src = this.env.unreadicon;
1544 else if (!rows[uid].unread)
1546 rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
1547 this.set_classname(rows[uid].obj, 'unread', false);
1549 if (rows[uid].replied && this.env.repliedicon)
1550 icn_src = this.env.repliedicon;
1551 else if (this.env.messageicon)
1552 icn_src = this.env.messageicon;
1555 if (rows[uid].icon && icn_src)
1556 rows[uid].icon.src = icn_src;
1560 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1563 // mark all message rows as deleted/undeleted
1564 this.toggle_delete_status = function(a_uids)
1566 if (this.env.read_when_deleted)
1567 this.toggle_read_status('read',a_uids);
1569 // if deleting message from "view message" don't bother with delete icon
1570 if (this.env.action == "show")
1573 var rows = this.message_list.rows;
1574 if (a_uids.length==1)
1576 if (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0)
1577 this.flag_as_deleted(a_uids);
1579 this.flag_as_undeleted(a_uids);
1584 var all_deleted = true;
1585 for (var i=0; i<a_uids.length; i++)
1589 if (rows[uid].classname.indexOf('deleted')<0)
1591 all_deleted = false;
1598 this.flag_as_undeleted(a_uids);
1600 this.flag_as_deleted(a_uids);
1606 this.flag_as_undeleted = function(a_uids)
1608 // if deleting message from "view message" don't bother with delete icon
1609 if (this.env.action == "show")
1613 var rows = this.message_list.rows;
1615 for (var i=0; i<a_uids.length; i++)
1619 rows[uid].deleted = false;
1621 if (rows[uid].classname.indexOf('deleted') > 0)
1623 rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
1624 this.set_classname(rows[uid].obj, 'deleted', false);
1626 if (rows[uid].unread && this.env.unreadicon)
1627 icn_src = this.env.unreadicon;
1628 else if (rows[uid].replied && this.env.repliedicon)
1629 icn_src = this.env.repliedicon;
1630 else if (this.env.messageicon)
1631 icn_src = this.env.messageicon;
1632 if (rows[uid].icon && icn_src)
1633 rows[uid].icon.src = icn_src;
1637 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
1642 this.flag_as_deleted = function(a_uids)
1644 // if deleting message from "view message" don't bother with delete icon
1645 if (this.env.action == "show")
1648 var rows = this.message_list.rows;
1649 for (var i=0; i<a_uids.length; i++)
1653 rows[uid].deleted = true;
1655 if (rows[uid].classname.indexOf('deleted')<0) {
1656 rows[uid].classname += ' deleted';
1657 this.set_classname(rows[uid].obj, 'deleted', true);
1659 if (rows[uid].icon && this.env.deletedicon)
1660 rows[uid].icon.src = this.env.deletedicon;
1664 this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete');
1669 /*********************************************************/
1670 /********* login form methods *********/
1671 /*********************************************************/
1673 // handler for keyboard events on the _user field
1674 this.login_user_keypress = function(e)
1676 var key = rcube_event.get_keycode(e);
1680 if ((key==13) && (elm = rcube_find_object('_pass')))
1688 /*********************************************************/
1689 /********* message compose methods *********/
1690 /*********************************************************/
1693 // checks the input fields before sending a message
1694 this.check_compose_input = function()
1696 // check input fields
1697 var input_to = rcube_find_object('_to');
1698 var input_cc = rcube_find_object('_cc');
1699 var input_bcc = rcube_find_object('_bcc');
1700 var input_subject = rcube_find_object('_subject');
1701 var input_message = rcube_find_object('_message');
1703 // check for empty recipient
1704 var recipients = input_to.value ? input_to.value : (input_cc.value ? input_cc.value : input_bcc.value);
1705 if (!rcube_check_email(recipients.replace(/^\s+/, '').replace(/[\s,;]+$/, ''), true))
1707 alert(this.get_label('norecipientwarning'));
1712 // display localized warning for missing subject
1713 if (input_subject && input_subject.value == '')
1715 var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1717 // user hit cancel, so don't send
1718 if (!subject && subject !== '')
1720 input_subject.focus();
1725 input_subject.value = subject ? subject : this.get_label('nosubject');
1729 // check for empty body
1730 if ((input_message.value == '' && (!window.tinyMCE || tinyMCE.getContent() == '')) && !confirm(this.get_label('nobodywarning')))
1732 input_message.focus();
1740 this.set_spellcheck_state = function(s)
1742 this.spellcheck_ready = (s=='check_spelling' || s=='ready');
1743 this.enable_command('spellcheck', this.spellcheck_ready);
1747 this.set_draft_id = function(id)
1750 if (f = rcube_find_object('_draft_saveid'))
1754 this.auto_save_start = function()
1756 if (this.env.draft_autosave)
1757 this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
1759 // Unlock interface now that saving is complete
1764 this.compose_field_hash = function(save)
1766 // check input fields
1767 var input_to = rcube_find_object('_to');
1768 var input_cc = rcube_find_object('_to');
1769 var input_bcc = rcube_find_object('_to');
1770 var input_subject = rcube_find_object('_subject');
1771 var input_message = rcube_find_object('_message');
1774 if (input_to && input_to.value)
1775 str += input_to.value+':';
1776 if (input_cc && input_cc.value)
1777 str += input_cc.value+':';
1778 if (input_bcc && input_bcc.value)
1779 str += input_bcc.value+':';
1780 if (input_subject && input_subject.value)
1781 str += input_subject.value+':';
1782 if (input_message && input_message.value)
1783 str += input_message.value;
1786 this.cmp_hash = str;
1792 this.change_identity = function(obj)
1794 if (!obj || !obj.options)
1797 var id = obj.options[obj.selectedIndex].value;
1798 var input_message = rcube_find_object('_message');
1799 var message = input_message ? input_message.value : '';
1800 var is_html = (rcube_find_object('_is_html').value == '1');
1803 if (!this.env.identity)
1804 this.env.identity = id
1808 // remove the 'old' signature
1809 if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1811 sig = this.env.signatures[this.env.identity]['text'];
1812 if (sig.indexOf('-- ')!=0)
1815 p = message.lastIndexOf(sig);
1817 message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
1820 // add the new signature string
1821 if (this.env.signatures && this.env.signatures[id])
1823 sig = this.env.signatures[id]['text'];
1824 if (this.env.signatures[id]['is_html'])
1826 sig = this.env.signatures[id]['plain_text'];
1828 if (sig.indexOf('-- ')!=0)
1830 message += '\n'+sig;
1835 var eid = tinyMCE.getEditorId('_message');
1836 // editor is a TinyMCE_Control object
1837 var editor = tinyMCE.getInstanceById(eid);
1838 // if this is null, we should exit
1839 if (editor == null) {
1842 var msgDoc = editor.getDoc();
1843 var msgBody = msgDoc.body;
1845 if (this.env.signatures && this.env.signatures[id])
1847 // Append the signature as a span within the body
1848 var sigElem = msgDoc.getElementById("_rc_sig");
1851 sigElem = msgDoc.createElement("span");
1852 sigElem.setAttribute("id", "_rc_sig");
1853 msgBody.appendChild(sigElem);
1855 if (this.env.signatures[id]['is_html'])
1857 sigElem.innerHTML = this.env.signatures[id]['text'];
1861 sigElem.innerHTML = '<pre>' + this.env.signatures[id]['text'] + '</pre>';
1867 input_message.value = message;
1869 this.env.identity = id;
1874 this.show_attachment_form = function(a)
1876 if (!this.gui_objects.uploadbox)
1880 if (elm = this.gui_objects.uploadbox)
1882 if (a && (list = this.gui_objects.attachmentlist))
1884 var pos = rcube_get_object_pos(list);
1886 var top = pos.y + list.offsetHeight + 10;
1888 elm.style.top = top+'px';
1889 elm.style.left = left+'px';
1892 elm.style.visibility = a ? 'visible' : 'hidden';
1895 // clear upload form
1897 if (!a && this.gui_objects.attachmentform != this.gui_objects.messageform)
1898 this.gui_objects.attachmentform.reset();
1900 catch(e){} // ignore errors
1906 // upload attachment file
1907 this.upload_file = function(form)
1912 // get file input fields
1914 for (var n=0; n<form.elements.length; n++)
1915 if (form.elements[n].type=='file' && form.elements[n].value)
1921 // create hidden iframe and post upload form
1924 var ts = new Date().getTime();
1925 var frame_name = 'rcmupload'+ts;
1927 // have to do it this way for IE
1928 // otherwise the form will be posted to a new window
1929 if(document.all && !window.opera)
1931 var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1932 document.body.insertAdjacentHTML('BeforeEnd',html);
1934 else // for standards-compilant browsers
1936 var frame = document.createElement('IFRAME');
1937 frame.name = frame_name;
1940 frame.style.visibility = 'hidden';
1941 document.body.appendChild(frame);
1944 form.target = frame_name;
1945 form.action = this.env.comm_path+'&_action=upload';
1946 form.setAttribute('enctype', 'multipart/form-data');
1950 // set reference to the form object
1951 this.gui_objects.attachmentform = form;
1956 // add file name to attachment list
1957 // called from upload page
1958 this.add2attachment_list = function(name, content)
1960 if (!this.gui_objects.attachmentlist)
1963 var li = document.createElement('LI');
1965 li.innerHTML = content;
1966 this.gui_objects.attachmentlist.appendChild(li);
1970 this.remove_from_attachment_list = function(name)
1972 if (!this.gui_objects.attachmentlist)
1975 var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
1976 for (i=0;i<list.length;i++)
1977 if (list[i].id == name)
1978 this.gui_objects.attachmentlist.removeChild(list[i]);
1981 this.remove_attachment = function(name)
1984 this.http_post('remove-attachment', '_file='+urlencode(name));
1989 // send remote request to add a new contact
1990 this.add_contact = function(value)
1993 this.http_post('addcontact', '_address='+value);
1998 // send remote request to search mail or contacts
1999 this.qsearch = function(value)
2003 if (this.message_list)
2004 this.message_list.clear();
2005 else if (this.contact_list) {
2006 this.contact_list.clear(true);
2007 this.show_contentframe(false);
2011 this.env.current_page = 1;
2012 this.set_busy(true, 'searching');
2013 this.http_request('search', '_q='+urlencode(value)+(this.env.mailbox ? '&_mbox='+this.env.mailbox : '')+(this.env.source ? '&_source='+urlencode(this.env.source) : ''), true);
2018 // reset quick-search form
2019 this.reset_qsearch = function()
2021 if (this.gui_objects.qsearchbox)
2022 this.gui_objects.qsearchbox.value = '';
2024 this.env.search_request = null;
2029 this.sent_successfully = function(msg)
2031 this.list_mailbox();
2032 this.display_message(msg, 'confirmation', true);
2036 /*********************************************************/
2037 /********* keyboard live-search methods *********/
2038 /*********************************************************/
2041 // handler for keyboard events on address-fields
2042 this.ksearch_keypress = function(e, obj)
2044 if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
2047 if (this.ksearch_timer)
2048 clearTimeout(this.ksearch_timer);
2051 var key = rcube_event.get_keycode(e);
2052 var mod = rcube_event.get_modifier(e);
2057 case 40: // key down
2058 if (!this.ksearch_pane)
2061 var dir = key==38 ? 1 : 0;
2064 highlight = document.getElementById('rcmksearchSelected');
2066 highlight = this.ksearch_pane.ul.firstChild;
2068 if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
2070 highlight.removeAttribute('id');
2071 this.set_classname(highlight, 'selected', false);
2076 next.setAttribute('id', 'rcmksearchSelected');
2077 this.set_classname(next, 'selected', true);
2078 this.ksearch_selected = next._rcm_id;
2081 return rcube_event.cancel(e);
2084 if(mod == SHIFT_KEY)
2088 if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2091 // insert selected address and hide ksearch pane
2092 this.insert_recipient(this.ksearch_selected);
2093 this.ksearch_hide();
2095 return rcube_event.cancel(e);
2098 this.ksearch_hide();
2104 this.ksearch_timer = setTimeout(function(){ ref.ksearch_get_results(); }, 200);
2105 this.ksearch_input = obj;
2111 this.insert_recipient = function(id)
2113 if (!this.env.contacts[id] || !this.ksearch_input)
2117 var inp_value = this.ksearch_input.value.toLowerCase();
2118 var cpos = this.get_caret_pos(this.ksearch_input);
2119 var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2121 // replace search string with full address
2122 var pre = this.ksearch_input.value.substring(0, p);
2123 var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2124 var insert = this.env.contacts[id]+', ';
2125 this.ksearch_input.value = pre + insert + end;
2127 // set caret to insert pos
2128 cpos = p+insert.length;
2129 if (this.ksearch_input.setSelectionRange)
2130 this.ksearch_input.setSelectionRange(cpos, cpos);
2135 // address search processor
2136 this.ksearch_get_results = function()
2138 var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2139 if (inp_value===null)
2142 // get string from current cursor pos to last comma
2143 var cpos = this.get_caret_pos(this.ksearch_input);
2144 var p = inp_value.lastIndexOf(',', cpos-1);
2145 var q = inp_value.substring(p+1, cpos);
2147 // trim query string
2148 q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2150 if (!q.length || q==this.ksearch_value)
2152 if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2153 this.ksearch_pane.show(0);
2158 this.ksearch_value = q;
2160 // start searching the contact list
2161 var a_results = new Array();
2162 var a_result_ids = new Array();
2164 for (var i=0; i<this.env.contacts.length; i++)
2166 if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2168 a_results[c] = this.env.contacts[i];
2169 a_result_ids[c++] = i;
2171 if (c==15) // limit search results
2176 // display search results
2177 if (c && a_results.length)
2181 // create results pane if not present
2182 if (!this.ksearch_pane)
2184 ul = document.createElement('UL');
2185 this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2186 this.ksearch_pane.elm.appendChild(ul);
2187 this.ksearch_pane.ul = ul;
2190 ul = this.ksearch_pane.ul;
2192 // remove all search results
2195 // add each result line to list
2196 for (i=0; i<a_results.length; i++)
2198 li = document.createElement('LI');
2199 li.innerHTML = a_results[i].replace(/</, '<').replace(/>/, '>');
2200 li.onmousedown = function(e){ ref.insert_recipient(this._rcm_id); ref.ksearch_pane.show(0); return rcube_event.cancel(e); };
2201 li.style.cursor = 'pointer';
2202 li._rcm_id = a_result_ids[i];
2206 // check if last selected item is still in result list
2207 if (this.ksearch_selected!==null)
2209 p = find_in_array(this.ksearch_selected, a_result_ids);
2210 if (p>=0 && ul.childNodes)
2212 ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2213 this.set_classname(ul.childNodes[p], 'selected', true);
2216 this.ksearch_selected = null;
2219 // if no item selected, select the first one
2220 if (this.ksearch_selected===null)
2222 ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2223 this.set_classname(ul.firstChild, 'selected', true);
2224 this.ksearch_selected = a_result_ids[0];
2227 // move the results pane right under the input box and make it visible
2228 var pos = rcube_get_object_pos(this.ksearch_input);
2229 this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2230 this.ksearch_pane.show(1);
2232 // hide results pane
2234 this.ksearch_hide();
2238 this.ksearch_blur = function(e, obj)
2240 if (this.ksearch_timer)
2241 clearTimeout(this.ksearch_timer);
2243 this.ksearch_value = '';
2244 this.ksearch_input = null;
2246 this.ksearch_hide();
2250 this.ksearch_hide = function()
2252 this.ksearch_selected = null;
2254 if (this.ksearch_pane)
2255 this.ksearch_pane.show(0);
2260 /*********************************************************/
2261 /********* address book methods *********/
2262 /*********************************************************/
2265 this.contactlist_keypress = function(list)
2267 if (list.key_pressed == list.DELETE_KEY)
2268 this.command('delete');
2272 this.contactlist_select = function(list)
2274 if (this.preview_timer)
2275 clearTimeout(this.preview_timer);
2277 var id, frame, ref = this;
2278 if (id = list.get_single_selection())
2279 this.preview_timer = setTimeout(function(){ ref.load_contact(id, 'show'); }, this.dblclick_time + 10);
2280 else if (this.env.contentframe)
2281 this.show_contentframe(false);
2283 this.enable_command('edit', id?true:false);
2284 this.enable_command('compose', list.selection.length > 0);
2285 this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
2291 this.list_contacts = function(src, page)
2294 var target = window;
2297 src = this.env.source;
2299 if (page && this.current_page==page && src == this.env.source)
2302 if (src != this.env.source)
2305 this.env.current_page = page;
2306 this.reset_qsearch();
2309 this.select_folder(src, this.env.source);
2310 this.env.source = src;
2312 // load contacts remotely
2313 if (this.gui_objects.contactslist)
2315 this.list_contacts_remote(src, page);
2319 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2321 target = window.frames[this.env.contentframe];
2322 add_url = '&_framed=1';
2325 // also send search request to get the correct listing
2326 if (this.env.search_request)
2327 add_url += '&_search='+this.env.search_request;
2329 this.set_busy(true, 'loading');
2330 target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
2334 // send remote request to load contacts list
2335 this.list_contacts_remote = function(src, page)
2337 // clear message list first
2338 this.contact_list.clear(true);
2339 this.show_contentframe(false);
2340 this.enable_command('delete', 'compose', false);
2342 // send request to server
2343 var url = (src ? '_source='+urlencode(src) : '') + (page ? (src?'&':'') + '_page='+page : '');
2344 this.env.source = src;
2346 // also send search request to get the right messages
2347 if (this.env.search_request)
2348 url += '&_search='+this.env.search_request;
2350 this.set_busy(true, 'loading');
2351 this.http_request('list', url, true);
2355 // load contact record
2356 this.load_contact = function(cid, action, framed)
2359 var target = window;
2360 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2362 add_url = '&_framed=1';
2363 target = window.frames[this.env.contentframe];
2364 this.show_contentframe(true);
2369 if (action && (cid || action=='add') && !this.drag_active)
2371 this.set_busy(true);
2372 target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
2377 // copy a contact to the specified target (group or directory)
2378 this.copy_contact = function(cid, to)
2381 cid = this.contact_list.get_selection().join(',');
2383 if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2384 this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
2388 this.delete_contacts = function()
2390 // exit if no mailbox specified or if selection is empty
2391 var selection = this.contact_list.get_selection();
2392 if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
2395 var a_cids = new Array();
2399 a_cids[a_cids.length] = this.env.cid;
2403 for (var n=0; n<selection.length; n++)
2406 a_cids[a_cids.length] = id;
2407 this.contact_list.remove_row(id, (n == selection.length-1));
2410 // hide content frame if we delete the currently displayed contact
2411 if (selection.length == 1)
2412 this.show_contentframe(false);
2415 // also send search request to get the right records from the next page
2416 if (this.env.search_request)
2417 qs += '&_search='+this.env.search_request;
2419 // send request to server
2420 this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_from='+(this.env.action ? this.env.action : '')+qs);
2425 // update a contact record in the list
2426 this.update_contact_row = function(cid, cols_arr)
2429 if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj))
2431 for (var c=0; c<cols_arr.length; c++)
2433 row.cells[c].innerHTML = cols_arr[c];
2442 /*********************************************************/
2443 /********* user settings methods *********/
2444 /*********************************************************/
2446 this.init_subscription_list = function()
2449 this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist, {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
2450 this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
2451 this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
2452 this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
2453 this.subscription_list.row_init = function (row)
2455 var anchors = row.obj.getElementsByTagName('A');
2457 anchors[0].onclick = function() { p.rename_folder(row.id); return false; };
2459 anchors[1].onclick = function() { p.delete_folder(row.id); return false; };
2460 row.obj.onmouseover = function() { p.focus_subscription(row.id); };
2461 row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
2463 this.subscription_list.init();
2466 this.identity_select = function(list)
2469 if (id = list.get_single_selection())
2470 this.load_identity(id, 'edit-identity');
2473 // load contact record
2474 this.load_identity = function(id, action)
2476 if (action=='edit-identity' && (!id || id==this.env.iid))
2480 var target = window;
2481 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2483 add_url = '&_framed=1';
2484 target = window.frames[this.env.contentframe];
2485 document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2488 if (action && (id || action=='add-identity'))
2490 this.set_busy(true);
2491 target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2497 this.delete_identity = function(id)
2499 // exit if no mailbox specified or if selection is empty
2500 var selection = this.identity_list.get_selection();
2501 if (!(selection.length || this.env.iid))
2505 id = this.env.iid ? this.env.iid : selection[0];
2507 // if (this.env.framed && id)
2508 this.goto_url('delete-identity', '_iid='+id, true);
2513 this.focus_subscription = function(id)
2516 var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2518 if (this.drag_active && (row = document.getElementById(id)))
2519 if (this.env.subscriptionrows[id] &&
2520 (folder = this.env.subscriptionrows[id][0]))
2522 if (this.check_droptarget(folder) &&
2523 (folder != this.env.folder.replace(reg, '')) &&
2524 (!folder.match(new RegExp('^'+RegExp.escape(this.env.folder+this.env.delimiter)))))
2526 this.set_env('dstfolder', folder);
2527 this.set_classname(row, 'droptarget', true);
2530 else if (this.env.folder.match(new RegExp(RegExp.escape(this.env.delimiter))))
2532 this.set_env('dstfolder', this.env.delimiter);
2533 this.set_classname(this.subscription_list.frame, 'droptarget', true);
2538 this.unfocus_subscription = function(id)
2541 this.set_env('dstfolder', null);
2542 if (this.env.subscriptionrows[id] &&
2543 (row = document.getElementById(id)))
2544 this.set_classname(row, 'droptarget', false);
2546 this.set_classname(this.subscription_list.frame, 'droptarget', false);
2550 this.subscription_select = function(list)
2553 if ((id = list.get_single_selection()) &&
2554 this.env.subscriptionrows['rcmrow'+id] &&
2555 (folder = this.env.subscriptionrows['rcmrow'+id][0]) &&
2556 (find_in_array(this.env.defaultfolders, folder)!=0))
2557 this.set_env('folder', folder);
2559 this.set_env('folder', null);
2561 if (this.gui_objects.createfolderhint)
2562 this.gui_objects.createfolderhint.innerHTML = this.env.folder ? this.get_label('addsubfolderhint') : '';
2566 this.subscription_move_folder = function(list)
2568 var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2569 if (this.env.folder && this.env.dstfolder && (this.env.dstfolder != this.env.folder) &&
2570 (this.env.dstfolder != this.env.folder.replace(reg, '')))
2572 var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2573 var basename = this.env.folder.replace(reg, '');
2574 var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
2575 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname));
2577 this.drag_active = false;
2578 this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
2582 // tell server to create and subscribe a new mailbox
2583 this.create_folder = function(name)
2585 if (this.edit_folder)
2586 this.reset_folder_rename();
2589 if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2590 name = form.elements['_folder_name'].value;
2591 if (this.env.folder && name != '')
2592 name = this.env.folder+this.env.delimiter+name;
2595 this.http_post('create-folder', '_name='+urlencode(name), true);
2596 else if (form.elements['_folder_name'])
2597 form.elements['_folder_name'].focus();
2601 // start renaming the mailbox name.
2602 // this will replace the name string with an input field
2603 this.rename_folder = function(id)
2605 var temp, row, form;
2607 // reset current renaming
2608 if (temp = this.edit_folder)
2610 this.reset_folder_rename();
2615 if (id && this.env.subscriptionrows[id] && (row = document.getElementById(id)))
2617 var reg = new RegExp('.*['+RegExp.escape(this.env.delimiter)+']');
2618 this.name_input = document.createElement('INPUT');
2619 this.name_input.value = this.env.subscriptionrows[id][1].replace(reg, '');
2620 this.name_input.style.width = '100%';
2622 reg = new RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2623 this.name_input.__parent = this.env.subscriptionrows[id][0].replace(reg, '');
2624 this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
2626 row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
2627 this.edit_folder = id;
2628 this.name_input.select();
2630 if (form = this.gui_objects.editform)
2631 form.onsubmit = function(){ return false; };
2636 // remove the input field and write the current mailbox name to the table cell
2637 this.reset_folder_rename = function()
2639 var cell = this.name_input ? this.name_input.parentNode : null;
2640 if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
2642 var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2643 cell.innerHTML = this.env.subscriptionrows[this.edit_folder][1].replace(reg, ' ');
2646 this.edit_folder = null;
2650 // handler for keyboard events on the input field
2651 this.name_input_keypress = function(e)
2653 var key = rcube_event.get_keycode(e);
2658 var newname = this.name_input ? this.name_input.value : null;
2659 if (this.edit_folder && newname)
2661 if (this.name_input.__parent)
2662 newname = this.name_input.__parent + this.env.delimiter + newname;
2663 this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname));
2668 this.reset_folder_rename();
2672 // delete a specific mailbox with all its messages
2673 this.delete_folder = function(id)
2675 var folder = this.env.subscriptionrows[id][0];
2677 if (this.edit_folder)
2678 this.reset_folder_rename();
2680 if (folder && confirm(this.get_label('deletefolderconfirm')))
2682 this.http_post('delete-folder', '_mboxes='+urlencode(folder));
2683 this.set_env('folder', null);
2688 // add a new folder to the subscription list by cloning a folder row
2689 this.add_folder_row = function(name, display_name, replace)
2691 name = name.replace('\\',"");
2692 if (!this.gui_objects.subscriptionlist)
2695 for (var refid in this.env.subscriptionrows)
2696 if (this.env.subscriptionrows[refid]!=null)
2700 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2701 var id = 'rcmrow'+(tbody.childNodes.length+1);
2702 var selection = this.subscription_list.get_single_selection();
2704 if (replace && replace.id)
2710 if (!id || !(refrow = document.getElementById(refid)))
2712 // Refresh page if we don't have a table row to clone
2713 this.goto_url('folders');
2717 // clone a table row if there are existing rows
2718 var row = this.clone_table_row(refrow);
2721 tbody.replaceChild(row, replace);
2723 tbody.appendChild(row);
2726 // add to folder/row-ID map
2727 this.env.subscriptionrows[row.id] = [name, display_name];
2730 row.cells[0].innerHTML = display_name;
2732 // set messages count to zero
2734 row.cells[1].innerHTML = '*';
2736 if (!replace && row.cells[2] && row.cells[2].firstChild.tagName=='INPUT')
2738 row.cells[2].firstChild.value = name;
2739 row.cells[2].firstChild.checked = true;
2742 // add new folder to rename-folder list and clear input field
2743 if (!replace && (form = this.gui_objects.editform))
2745 if (form.elements['_folder_oldname'])
2746 form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
2747 if (form.elements['_folder_name'])
2748 form.elements['_folder_name'].value = '';
2751 this.sort_subscription_list();
2752 this.init_subscription_list();
2753 if (selection && document.getElementById('rcmrow'+selection))
2754 this.subscription_list.select_row(selection);
2756 if (document.getElementById(id).scrollIntoView)
2757 document.getElementById(id).scrollIntoView();
2761 // replace an existing table row with a new folder line
2762 this.replace_folder_row = function(oldfolder, newfolder, display_name)
2764 var id = this.get_folder_row_id(oldfolder);
2765 var row = document.getElementById(id);
2767 // replace an existing table row (if found)
2768 this.add_folder_row(newfolder, display_name, row);
2770 // rename folder in rename-folder dropdown
2772 if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
2774 for (var i=0;i<elm.options.length;i++)
2776 if (elm.options[i].value == oldfolder)
2778 elm.options[i].text = display_name;
2779 elm.options[i].value = newfolder;
2784 form.elements['_folder_newname'].value = '';
2789 // remove the table row of a specific mailbox from the table
2790 // (the row will not be removed, just hidden)
2791 this.remove_folder_row = function(folder)
2794 var id = this.get_folder_row_id(folder);
2795 if (id && (row = document.getElementById(id)))
2796 row.style.display = 'none';
2798 // remove folder from rename-folder list
2800 if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
2802 for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
2804 if (form.elements['_folder_oldname'].options[i].value == folder)
2806 form.elements['_folder_oldname'].options[i] = null;
2812 if (form && form.elements['_folder_newname'])
2813 form.elements['_folder_newname'].value = '';
2817 this.subscribe_folder = function(folder)
2820 if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2821 this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2823 this.http_post('subscribe', '_mboxes='+urlencode(folder));
2827 this.unsubscribe_folder = function(folder)
2830 if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2831 this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2833 this.http_post('unsubscribe', '_mboxes='+urlencode(folder));
2837 this.change_subscription = function(from, to, action)
2840 if (form = this.gui_objects.editform)
2842 var a_folders = new Array();
2843 var list_from = form.elements[from];
2845 for (var i=0; list_from && i<list_from.options.length; i++)
2847 if (list_from.options[i] && list_from.options[i].selected)
2849 a_folders[a_folders.length] = list_from.options[i].value;
2850 list_from[i] = null;
2855 // yes, we have some folders selected
2856 if (a_folders.length)
2858 var list_to = form.elements[to];
2861 for (var n=0; n<a_folders.length; n++)
2863 index = list_to.options.length;
2864 list_to[index] = new Option(a_folders[n]);
2867 this.http_post(action, '_mboxes='+urlencode(a_folders.join(',')));
2873 // helper method to find a specific mailbox row ID
2874 this.get_folder_row_id = function(folder)
2876 for (var id in this.env.subscriptionrows)
2877 if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
2883 // duplicate a specific table row
2884 this.clone_table_row = function(row)
2887 var new_row = document.createElement('TR');
2888 for(var n=0; n<row.childNodes.length; n++)
2890 cell = row.childNodes[n];
2891 td = document.createElement('TD');
2894 td.className = cell.className;
2896 td.setAttribute('align', cell.align);
2898 td.innerHTML = cell.innerHTML;
2899 new_row.appendChild(td);
2905 // sort subscription folder list
2906 this.sort_subscription_list = function()
2908 var index = new Array();
2909 var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2910 var swapped = false;
2911 for (var i = 0; i<tbody.childNodes.length; i++)
2912 if (this.env.subscriptionrows[tbody.childNodes[i].id]!=null)
2914 for (i = 0; i<(index.length-1); i++)
2916 var one = tbody.childNodes[index[i]];
2917 var two = tbody.childNodes[index[i+1]];
2918 if (this.env.subscriptionrows[one.id][0].toLowerCase()>
2919 this.env.subscriptionrows[two.id][0].toLowerCase())
2921 var swap = one.cloneNode(true);
2922 tbody.replaceChild(swap, two);
2923 tbody.replaceChild(two, one);
2928 this.sort_subscription_list();
2932 /*********************************************************/
2933 /********* GUI functionality *********/
2934 /*********************************************************/
2937 // eable/disable buttons for page shifting
2938 this.set_page_buttons = function()
2940 this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
2941 this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
2942 this.enable_command('previouspage', (this.env.current_page > 1));
2943 this.enable_command('firstpage', (this.env.current_page > 1));
2947 // set button to a specific state
2948 this.set_button = function(command, state)
2950 var a_buttons = this.buttons[command];
2953 if(!a_buttons || !a_buttons.length)
2956 for(var n=0; n<a_buttons.length; n++)
2958 button = a_buttons[n];
2959 obj = document.getElementById(button.id);
2961 // get default/passive setting of the button
2962 if (obj && button.type=='image' && !button.status) {
2963 button.pas = obj._original_src ? obj._original_src : obj.src;
2964 // respect PNG fix on IE browsers
2965 if (obj.runtimeStyle && obj.runtimeStyle.filter && obj.runtimeStyle.filter.match(/src=['"]([^'"]+)['"]/))
2966 button.pas = RegExp.$1;
2968 else if (obj && !button.status)
2969 button.pas = String(obj.className);
2971 // set image according to button state
2972 if (obj && button.type=='image' && button[state])
2974 button.status = state;
2975 obj.src = button[state];
2977 // set class name according to button state
2978 else if (obj && typeof(button[state])!='undefined')
2980 button.status = state;
2981 obj.className = button[state];
2983 // disable/enable input buttons
2984 if (obj && button.type=='input')
2986 button.status = state;
2987 obj.disabled = !state;
2992 // display a specific alttext
2993 this.set_alttext = function(command, label)
2995 if (!this.buttons[command] || !this.buttons[command].length)
2998 var button, obj, link;
2999 for (var n=0; n<this.buttons[command].length; n++)
3001 button = this.buttons[command][n];
3002 obj = document.getElementById(button.id);
3004 if (button.type=='image' && obj)
3006 obj.setAttribute('alt', this.get_label(label));
3007 if ((link = obj.parentNode) && link.tagName == 'A')
3008 link.setAttribute('title', this.get_label(label));
3011 obj.setAttribute('title', this.get_label(label));
3015 // mouse over button
3016 this.button_over = function(command, id)
3018 var a_buttons = this.buttons[command];
3021 if(!a_buttons || !a_buttons.length)
3024 for(var n=0; n<a_buttons.length; n++)
3026 button = a_buttons[n];
3027 if(button.id==id && button.status=='act')
3029 img = document.getElementById(button.id);
3030 if (img && button.over)
3031 img.src = button.over;
3036 // mouse down on button
3037 this.button_sel = function(command, id)
3039 var a_buttons = this.buttons[command];
3042 if(!a_buttons || !a_buttons.length)
3045 for(var n=0; n<a_buttons.length; n++)
3047 button = a_buttons[n];
3048 if(button.id==id && button.status=='act')
3050 img = document.getElementById(button.id);
3051 if (img && button.sel)
3052 img.src = button.sel;
3057 // mouse out of button
3058 this.button_out = function(command, id)
3060 var a_buttons = this.buttons[command];
3063 if(!a_buttons || !a_buttons.length)
3066 for(var n=0; n<a_buttons.length; n++)
3068 button = a_buttons[n];
3069 if(button.id==id && button.status=='act')
3071 img = document.getElementById(button.id);
3072 if (img && button.act)
3073 img.src = button.act;
3079 // set/unset a specific class name
3080 this.set_classname = function(obj, classname, set)
3082 var reg = new RegExp('\s*'+classname, 'i');
3083 if (!set && obj.className.match(reg))
3084 obj.className = obj.className.replace(reg, '');
3085 else if (set && !obj.className.match(reg))
3086 obj.className += ' '+classname;
3090 // write to the document/window title
3091 this.set_pagetitle = function(title)
3093 if (title && document.title)
3094 document.title = title;
3098 // display a system message
3099 this.display_message = function(msg, type, hold)
3101 if (!this.loaded) // save message in order to display after page loaded
3103 this.pending_message = new Array(msg, type);
3107 // pass command to parent window
3108 if (this.env.framed && parent.rcmail)
3109 return parent.rcmail.display_message(msg, type, hold);
3111 if (!this.gui_objects.message)
3114 if (this.message_timer)
3115 clearTimeout(this.message_timer);
3119 cont = '<div class="'+type+'">'+cont+'</div>';
3122 this.gui_objects.message.innerHTML = cont;
3123 this.gui_objects.message.style.display = 'block';
3125 if (type!='loading')
3126 this.gui_objects.message.onmousedown = function(){ _rcube.hide_message(); return true; };
3129 this.message_timer = setTimeout(function(){ ref.hide_message(); }, this.message_time);
3133 // make a message row disapear
3134 this.hide_message = function()
3136 if (this.gui_objects.message)
3138 this.gui_objects.message.style.display = 'none';
3139 this.gui_objects.message.onmousedown = null;
3144 // mark a mailbox as selected and set environment variable
3145 this.select_folder = function(name, old)
3147 if (this.gui_objects.folderlist)
3149 var current_li, target_li;
3151 if ((current_li = this.get_folder_li(old)))
3153 this.set_classname(current_li, 'selected', false);
3154 this.set_classname(current_li, 'unfocused', false);
3157 if ((target_li = this.get_folder_li(name)))
3159 this.set_classname(target_li, 'unfocused', false);
3160 this.set_classname(target_li, 'selected', true);
3165 // helper method to find a folder list item
3166 this.get_folder_li = function(name)
3168 if (this.gui_objects.folderlist)
3170 name = String(name).replace(this.identifier_expr, '');
3171 return document.getElementById('rcmli'+name);
3178 // for reordering column array, Konqueror workaround
3179 this.set_message_coltypes = function(coltypes)
3181 this.coltypes = coltypes;
3183 // set correct list titles
3185 var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3186 for (var n=0; thead && n<this.coltypes.length; n++)
3188 col = this.coltypes[n];
3189 if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3191 // if we have links for sorting, it's a bit more complicated...
3192 if (cell.firstChild && cell.firstChild.tagName=='A')
3194 cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3195 cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3196 cell.firstChild.__col = col;
3199 cell.innerHTML = this.get_label(this.coltypes[n]);
3201 cell.id = 'rcmHead'+col;
3204 if (col == 'subject' && this.message_list)
3205 this.message_list.subject_col = n+1;
3209 // create a table row in the message list
3210 this.add_message_row = function(uid, cols, flags, attachment, attop)
3212 if (!this.gui_objects.messagelist || !this.message_list)
3215 var tbody = this.gui_objects.messagelist.tBodies[0];
3216 var rowcount = tbody.rows.length;
3217 var even = rowcount%2;
3219 this.env.messages[uid] = {deleted:flags.deleted?1:0,
3220 replied:flags.replied?1:0,
3221 unread:flags.unread?1:0};
3223 var row = document.createElement('TR');
3224 row.id = 'rcmrow'+uid;
3225 row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
3227 if (this.message_list.in_selection(uid))
3228 row.className += ' selected';
3230 var icon = flags.deleted && this.env.deletedicon ? this.env.deletedicon:
3231 (flags.unread && this.env.unreadicon ? this.env.unreadicon :
3232 (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon));
3234 var col = document.createElement('TD');
3235 col.className = 'icon';
3236 col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
3237 row.appendChild(col);
3239 // add each submitted col
3240 for (var n = 0; n < this.coltypes.length; n++)
3242 var c = this.coltypes[n];
3243 col = document.createElement('TD');
3244 col.className = String(c).toLowerCase();
3245 col.innerHTML = cols[c];
3246 row.appendChild(col);
3249 col = document.createElement('TD');
3250 col.className = 'icon';
3251 col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
3252 row.appendChild(col);
3254 this.message_list.insert_row(row, attop);
3258 // replace content of row count display
3259 this.set_rowcount = function(text)
3261 if (this.gui_objects.countdisplay)
3262 this.gui_objects.countdisplay.innerHTML = text;
3264 // update page navigation buttons
3265 this.set_page_buttons();
3268 // replace content of quota display
3269 this.set_quota = function()
3271 if (this.gui_objects.quotadisplay &&
3272 this.gui_objects.quotadisplay.attributes.getNamedItem('display') &&
3273 this.gui_objects.quotadisplay.attributes.getNamedItem('id'))
3274 this.http_request('quotadisplay', '_display='+
3275 this.gui_objects.quotadisplay.attributes.getNamedItem('display').nodeValue+
3276 '&_id='+this.gui_objects.quotadisplay.attributes.getNamedItem('id').nodeValue, false);
3280 // update the mailboxlist
3281 this.set_unread_count = function(mbox, count, set_title)
3283 if (!this.gui_objects.mailboxlist)
3286 var reg, text_obj, item;
3287 if (item = this.get_folder_li(mbox))
3290 text_obj = item.firstChild;
3291 reg = /\s+\([0-9]+\)$/i;
3293 if (count && text_obj.innerHTML.match(reg))
3294 text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
3296 text_obj.innerHTML += ' ('+count+')';
3298 text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
3300 // set the right classes
3301 this.set_classname(item, 'unread', count>0 ? true : false);
3304 // set unread count to window title
3305 reg = /^\([0-9]+\)\s+/i;
3306 if (set_title && document.title)
3308 var doc_title = String(document.title);
3311 if (count && doc_title.match(reg))
3312 new_title = doc_title.replace(reg, '('+count+') ');
3314 new_title = '('+count+') '+doc_title;
3316 new_title = doc_title.replace(reg, '');
3318 this.set_pagetitle(new_title);
3323 // add row to contacts list
3324 this.add_contact_row = function(cid, cols, select)
3326 if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
3329 var tbody = this.gui_objects.contactslist.tBodies[0];
3330 var rowcount = tbody.rows.length;
3331 var even = rowcount%2;
3333 var row = document.createElement('TR');
3334 row.id = 'rcmrow'+cid;
3335 row.className = 'contact '+(even ? 'even' : 'odd');
3337 if (this.contact_list.in_selection(cid))
3338 row.className += ' selected';
3340 // add each submitted col
3343 col = document.createElement('TD');
3344 col.className = String(c).toLowerCase();
3345 col.innerHTML = cols[c];
3346 row.appendChild(col);
3349 this.contact_list.insert_row(row);
3353 this.toggle_editor = function(checkbox, textElementName)
3355 var ischecked = checkbox.checked;
3358 tinyMCE.execCommand('mceAddControl', true, textElementName);
3362 tinyMCE.execCommand('mceRemoveControl', true, textElementName);
3368 /********************************************************/
3369 /********* remote request methods *********/
3370 /********************************************************/
3372 this.redirect = function(url, lock)
3374 if (lock || lock === null)
3375 this.set_busy(true);
3377 if (this.env.framed && window.parent)
3378 parent.location.href = url;
3380 location.href = url;
3383 this.goto_url = function(action, query, lock)
3385 var querystring = query ? '&'+query : '';
3386 this.redirect(this.env.comm_path+'&_action='+action+querystring, lock);
3390 this.http_sockets = new Array();
3392 // find a non-busy socket or create a new one
3393 this.get_request_obj = function()
3395 for (var n=0; n<this.http_sockets.length; n++)
3397 if (!this.http_sockets[n].busy)
3398 return this.http_sockets[n];
3401 // create a new XMLHTTP object
3402 var i = this.http_sockets.length;
3403 this.http_sockets[i] = new rcube_http_request();
3405 return this.http_sockets[i];
3409 // send a http request to the server
3410 this.http_request = function(action, querystring, lock)
3412 var request_obj = this.get_request_obj();
3413 querystring += (querystring ? '&' : '') + '_remote=1';
3415 // add timestamp to request url to avoid cacheing problems in Safari
3417 querystring += '&_ts='+(new Date().getTime());
3422 console.log('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring);
3425 this.set_busy(true);
3428 request_obj.__lock = lock ? true : false;
3429 request_obj.__action = action;
3430 request_obj.onerror = function(o){ ref.http_error(o); };
3431 request_obj.oncomplete = function(o){ ref.http_response(o); };
3432 request_obj.GET(this.env.comm_path+'&_action='+action+'&'+querystring);
3436 // send a http POST request to the server
3437 this.http_post = function(action, postdata, lock)
3440 if (postdata && typeof(postdata) == 'object')
3441 postdata._remote = 1;
3443 postdata += (postdata ? '&' : '') + '_remote=1';
3446 if (request_obj = this.get_request_obj())
3448 console.log('HTTP POST: '+this.env.comm_path+'&_action='+action);
3451 this.set_busy(true);
3454 request_obj.__lock = lock ? true : false;
3455 request_obj.__action = action;
3456 request_obj.onerror = function(o){ rcm.http_error(o); };
3457 request_obj.oncomplete = function(o){ rcm.http_response(o); };
3458 request_obj.POST(this.env.comm_path+'&_action='+action, postdata);
3462 // handle HTTP response
3463 this.http_response = function(request_obj)
3465 var ctype = request_obj.get_header('Content-Type');
3467 ctype = String(ctype).toLowerCase();
3468 var ctype_array=ctype.split(";");
3469 ctype = ctype_array[0];
3472 if (request_obj.__lock)
3473 this.set_busy(false);
3475 console.log(request_obj.get_text());
3477 // if we get javascript code from server -> execute it
3478 if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
3479 eval(request_obj.get_text());
3481 // process the response data according to the sent action
3482 switch (request_obj.__action)
3486 if (this.env.action=='show')
3487 this.command('list');
3488 else if (this.message_list)
3489 this.message_list.init();
3493 if (this.env.messagecount)
3494 this.enable_command('purge', (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox));
3495 this.msglist_select(this.message_list);
3498 this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
3502 request_obj.reset();
3506 // handle HTTP request errors
3507 this.http_error = function(request_obj)
3509 //alert('Error sending request: '+request_obj.url);
3511 if (request_obj.__lock)
3512 this.set_busy(false);
3514 request_obj.reset();
3515 request_obj.__lock = false;
3519 // use an image to send a keep-alive siganl to the server
3520 this.send_keep_alive = function()
3523 this.http_request('keep-alive', '_t='+d.getTime());
3527 // send periodic request to check for recent messages
3528 this.check_for_recent = function()
3533 this.set_busy(true, 'checkingmail');
3534 this.http_request('check-recent', (this.env.search_request ? '_search='+this.env.search_request+'&' : '') + '_t='+(new Date().getTime()), true);
3538 /********************************************************/
3539 /********* helper methods *********/
3540 /********************************************************/
3542 // check if we're in show mode or if we have a unique selection
3543 // and return the message uid
3544 this.get_single_uid = function()
3546 return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
3549 // same as above but for contacts
3550 this.get_single_cid = function()
3552 return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
3556 this.get_caret_pos = function(obj)
3558 if (typeof(obj.selectionEnd)!='undefined')
3559 return obj.selectionEnd;
3561 else if (document.selection && document.selection.createRange)
3563 var range = document.selection.createRange();
3564 if (range.parentElement()!=obj)
3567 var gm = range.duplicate();
3568 if (obj.tagName=='TEXTAREA')
3569 gm.moveToElementText(obj);
3571 gm.expand('textedit');
3573 gm.setEndPoint('EndToStart', range);
3574 var p = gm.text.length;
3576 return p<=obj.value.length ? p : -1;
3580 return obj.value.length;
3584 this.set_caret2start = function(obj)
3586 if (obj.createTextRange)
3588 var range = obj.createTextRange();
3589 range.collapse(true);
3592 else if (obj.setSelectionRange)
3593 obj.setSelectionRange(0,0);
3599 // set all fields of a form disabled
3600 this.lock_form = function(form, lock)
3602 if (!form || !form.elements)
3606 for (var n=0; n<form.elements.length; n++)
3608 type = form.elements[n];
3612 form.elements[n].disabled = lock;
3616 } // end object rcube_webmail
3621 * Class for sending HTTP requests
3624 function rcube_http_request()
3628 this.xmlhttp = null;
3631 // reset object properties
3632 this.reset = function()
3634 // set unassigned event handlers
3635 this.onloading = function(){ };
3636 this.onloaded = function(){ };
3637 this.oninteractive = function(){ };
3638 this.oncomplete = function(){ };
3639 this.onabort = function(){ };
3640 this.onerror = function(){ };
3644 this.xmlhttp = null;
3648 // create HTMLHTTP object
3649 this.build = function()
3651 if (window.XMLHttpRequest)
3652 this.xmlhttp = new XMLHttpRequest();
3653 else if (window.ActiveXObject)
3655 try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
3656 catch(e) { this.xmlhttp = null; }
3665 this.GET = function(url)
3679 this.xmlhttp.onreadystatechange = function(){ _ref.xmlhttp_onreadystatechange(); };
3680 this.xmlhttp.open('GET', url);
3681 this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
3682 this.xmlhttp.send(null);
3686 this.POST = function(url, body, contentType)
3688 // default value for contentType if not provided
3689 if (typeof(contentType) == 'undefined')
3690 contentType = 'application/x-www-form-urlencoded';
3700 var req_body = body;
3701 if (typeof(body) == 'object')
3705 req_body += (req_body ? '&' : '') + p+'='+urlencode(body[p]);
3712 this.xmlhttp.onreadystatechange = function() { ref.xmlhttp_onreadystatechange(); };
3713 this.xmlhttp.open('POST', url, true);
3714 this.xmlhttp.setRequestHeader('Content-Type', contentType);
3715 this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
3716 this.xmlhttp.send(req_body);
3720 // handle onreadystatechange event
3721 this.xmlhttp_onreadystatechange = function()
3723 if(this.xmlhttp.readyState == 1)
3724 this.onloading(this);
3726 else if(this.xmlhttp.readyState == 2)
3727 this.onloaded(this);
3729 else if(this.xmlhttp.readyState == 3)
3730 this.oninteractive(this);
3732 else if(this.xmlhttp.readyState == 4)
3735 if (this.xmlhttp.status == 0)
3737 else if(this.xmlhttp.status == 200)
3738 this.oncomplete(this);
3752 // getter method for HTTP headers
3753 this.get_header = function(name)
3755 return this.xmlhttp.getResponseHeader(name);
3758 this.get_text = function()
3760 return this.xmlhttp.responseText;
3763 this.get_xml = function()
3765 return this.xmlhttp.responseXML;
3770 } // end class rcube_http_request
3773 // helper function to call the init method with a delay
3774 function call_init(o)
3776 if (window[o] && window[o].init)
3777 setTimeout(o+'.init()', 200);