/* * The javascript code that brings the webmail to life! */ var Webmail = function(options) { this.initialize(options); }; Webmail.prototype = { constructor: Webmail, defaults: { poller_interval: 300, /* in seconds */ poller_url: "", move_url: "", submboxes_url: "", delattachment_url: "", deflocation: "", defcallback: "", ro_mboxes: ["INBOX"] }, initialize: function(options) { this.options = $.extend({}, this.defaults, options); this.rtimer = null; this.editorid = "id_body"; this.navobject = new History({ deflocation: this.options.deflocation, defcallback: this.options.defcallback }); this.poller = new Poller(this.options.poller_url, { interval: this.options.poller_interval * 1000, success_cb: $.proxy(this.poller_cb, this), args: this.get_visible_mailboxes }); this.record_unseen_messages(); $("#folders").css({ bottom: $("#bottom-bar").outerHeight(true) }); $("#mboxactions").css({ bottom: $("#bottom-bar").outerHeight(true) + 10 }); this.resize(); this.init_droppables(); this.register_navcallbacks(); this.listen(); }, register_navcallbacks: function() { this.navobject.register_callback("listmailbox", $.proxy(this.listmailbox_callback, this)); this.navobject.register_callback("compose", $.proxy(this.compose_callback, this)); this.navobject.register_callback("viewmail", $.proxy(this.viewmail_callback, this)); this.navobject.register_callback("reply", $.proxy(this.compose_callback, this)); this.navobject.register_callback("replyall", $.proxy(this.compose_callback, this)); this.navobject.register_callback("forward", $.proxy(this.compose_callback, this)); this.navobject.register_callback("delete", $.proxy(this.delete_callback, this)); }, listen: function() { $(window).resize(this.resize); $(document).on("click", "a[name=compose]", $.proxy(this.compose_loader, this)); $(document).on("click", "a[name*=mark-]", $.proxy(this.send_mark_request, this)); $(document).on("click", "a[name=empty]", $.proxy(this.empty, this)); $(document).on("click", "#bottom-bar a", $.proxy(this.getpage_loader, this)); $(document).on("click", "a[name=loadfolder]", $.proxy(this.listmailbox_loader, this)); $(document).on("click", "a[name=selectfolder]", this.select_parent_mailbox), $(document).on("click", "div[class*=clickbox]", $.proxy(this.mbox_state_callback, this)); $(document).on("click", "a[name=newmbox]", $.proxy(this.new_mailbox, this)); $(document).on("click", "a[name=editmbox]", $.proxy(this.edit_mbox, this)); $(document).on("click", "a[name=removembox]", $.proxy(this.remove_mbox, this)); $(document).on("dblclick", "tbody>tr", $.proxy(this.viewmail_loader, this)); $(document).on("click", "a[name=reply]", $.proxy(this.reply_loader, this)); $(document).on("click", "a[name=replyall]", $.proxy(this.reply_loader, this)); $(document).on("click", "a[name=forward]", $.proxy(this.reply_loader, this)); $(document).on("click", "a[name=delete]", $.proxy(this.delete_message, this)); $(document).on("click", "a[name=activate_links]", $.proxy(function(e) { this.display_mode(e, "1"); }, this)); $(document).on("click", "a[name=disable_links]", $.proxy(function(e) { this.disable_mode(e, "0"); }, this)); $(document).on("click", "a[name=sendmail]", $.proxy(this.sendmail, this)); $(document).on("click", "#attachments", $.proxy(function(e) { modalbox(e, undefined, $("#attachments").attr("name"), $.proxy(this.attachments_init, this)); }, this)); }, record_unseen_messages: function() { var unseen_counters = {}; $("#folders").find("a.unseen").each(function() { var $this = $(this); unseen_counters[$this.attr("href")] = parseInt($this.attr("data-toggle")); }); this.unseen_counters = unseen_counters; }, resize: function() { $("#mboxes_container").height($("#folders").height() - $("#mboxactions").height()); }, /* * Simple helper to retrieve the currently selected mailbox. */ get_current_mailbox: function() { if (this.navobject.getparam("action") != "listmailbox") { return undefined; } return this.navobject.getparam("name", "INBOX"); }, /* * Enable or disable edit and remove actions for the currently * selected mailbox. */ enable_mb_actions: function(state) { if (state === undefined) { state = true; } if (state) { $("a[name=editmbox], a[name=removembox]").removeClass("disabled"); } else { $("a[name=editmbox], a[name=removembox]").addClass("disabled"); } }, page_update: function(response) { /*if (this.gspinner) { this.gspinner.hide(); }*/ $(window).unbind("resize"); if (response.menu != undefined) { $("#menubar").html(response.menu); $("#searchfield").searchbar({navobj: this.navobject}); } if (response.navbar) { $("#bottom-bar-right").html(response.navbar); } $("#listing").html(response.listing); $("#listing").css({ top: $("#menubar").outerHeight(true) + 60 + "px", bottom: $("#bottom-bar").outerHeight(true) + "px", overflow: "auto" }); }, /* * Set the *unseen messages* counter for a particular mailbox in * the list. If the mailbox is currently selected, we update the * listing. */ set_unseen_messages: function(mailbox, value) { if (this.poller.running_request) { return; } if (this.unseen_counters[mailbox] != undefined && value == this.unseen_counters[mailbox]) { return; } if (this.navobject.params.action == "listmailbox") { var curmb = this.get_current_mailbox(); if (curmb == mailbox) { this.navobject.update(true); } } var $link = $('a[href="' + mailbox + '"]'); var parts = mailbox.split('.'); var dname = " " + parts[parts.length - 1]; var $i = $link.children("i"); this.unseen_counters[mailbox] = value; if (value) { $link.html(dname + " (" + value + ")"); $link.addClass("unseen"); } else { $link.html(dname); $link.removeClass("unseen"); } $link.prepend($i); }, /* * Increment or decrement the *unseen messages* counter of the * given mailbox. */ change_unseen_messages: function(mailbox, offset) { if (this.unseen_counters[mailbox] === undefined) { this.unseen_counters[mailbox] = 0; } this.set_unseen_messages(mailbox, this.unseen_counters[mailbox] + offset); }, /* * Returns a list containing the currently visible mailboxes (in * the left tree). */ get_visible_mailboxes: function() { var res = new Array(); $("#folders").find("ul:visible").children("li.droppable").each(function() { res.push($(this).attr("name")); }); return "mboxes=" + res.join(","); }, /* * Sends a refresh request to the server. This method is called * periodically to check for new messages. (not really optimized) */ update_unseen_messages: function(mailbox) { $.ajax({ url: this.options.poller_url + "?selection=" + mailbox, dataType: 'json', success: $.proxy(this.poller_cb, this) }); }, /* * Poller callback. */ poller_cb: function(data) { for (var mb in data.counters) { this.set_unseen_messages(mb, parseInt(data.counters[mb])); } }, /* * Inject a new *clickbox* somewhere in the tree */ inject_clickbox: function($container) { $container.prepend($("
", {'class' : 'clickbox collapsed'})); }, /* * Injects a single mailbox somewhere in the tree */ inject_mailbox: function($parent, mailbox, linkname, unseen) { var $li = $("
  • ", { name: mailbox, 'class': "droppable" }); var $link = $("", { name: linkname, 'class': "block", href: mailbox }); var parts = mailbox.split("."); var linkcontent = " "; var displayname = linkcontent + parts[parts.length - 1]; $li.append($link); $parent.append($li); if (unseen != undefined) { $link.addClass("unseen"); $link.html(displayname + " (" + unseen + ")"); this.unseen_counters[$link.attr("href")] = unseen; } else { $link.html(displayname); } }, /* * Inject new mailboxes under a given parent in the tree. */ inject_mailboxes: function($parent, mboxes) { var $ul = $("