annotate gpp/messages/static/js/tabbed_messages.js @ 430:9df368d9775d

Fix bug where messages were never getting replied to.
author Brian Neal <bgneal@gmail.com>
date Thu, 05 May 2011 02:58:44 +0000
parents d0f0800eef0c
children 9f888dbe61ce
rev   line source
bgneal@425 1 $(document).ready(function() {
bgneal@425 2 $tabs = $('#tabs').tabs({
bgneal@425 3 selected: initialTab,
bgneal@425 4 select: function(event, ui) {
bgneal@425 5 $(ui.panel).html('');
bgneal@425 6 },
bgneal@425 7 load: function(event, ui) {
bgneal@425 8 selectedTab = ui;
bgneal@428 9 if (ui.index == 1 && receiver && !doReply)
bgneal@428 10 {
bgneal@428 11 $('#id_receiver').val(receiver);
bgneal@428 12 receiver = '';
bgneal@428 13 }
bgneal@428 14 else if (doReply && ui.index == 1)
bgneal@425 15 {
bgneal@425 16 doReply = false;
bgneal@425 17 var msg = msgCache[$msgDialog.msgId];
bgneal@425 18 $('#id_receiver').val(msg.sender);
bgneal@425 19 $('#id_subject').val(msg.re_subject);
bgneal@425 20 $('#id_message').val(msg.re_content);
bgneal@430 21 $('#msg_compose_form').append('<input type="hidden" name="reply_to" value="' +
bgneal@430 22 $msgDialog.msgId + '" />');
bgneal@425 23 }
bgneal@425 24 },
bgneal@425 25 ajaxOptions: {
bgneal@425 26 error: function(xhr, status, index, anchor) {
bgneal@425 27 $(anchor.hash).html(
bgneal@425 28 "Oops, we couldn't load this tab. We'll try to fix this as soon as possible.");
bgneal@425 29 }
bgneal@425 30 }
bgneal@425 31 });
bgneal@425 32 $msgDialog = $('#msgDialog').dialog({
bgneal@425 33 autoOpen: false,
bgneal@425 34 width: 460,
bgneal@425 35 buttons: [
bgneal@425 36 {
bgneal@425 37 text: "Reply",
bgneal@425 38 click: function() {
bgneal@425 39 doReply = true;
bgneal@425 40 $(this).dialog('close');
bgneal@425 41 $tabs.tabs("select", 1);
bgneal@425 42 }
bgneal@425 43 },
bgneal@425 44 {
bgneal@425 45 text: "Close",
bgneal@425 46 click: function() {
bgneal@425 47 $(this).dialog('close');
bgneal@425 48 }
bgneal@425 49 }
bgneal@425 50 ]
bgneal@425 51 });
bgneal@425 52 });
bgneal@425 53
bgneal@425 54 var $tabs = 0;
bgneal@425 55 var $msgDialog = 0;
bgneal@425 56 var msgCache = {};
bgneal@425 57 var doReply = false;
bgneal@425 58 var selectedTab = 0;
bgneal@425 59
bgneal@429 60 function updateUnreadMsgText(n)
bgneal@429 61 {
bgneal@429 62 var txt = '';
bgneal@429 63 if (n == 1) {
bgneal@429 64 txt = "1 New Message";
bgneal@429 65 }
bgneal@429 66 else if (n > 1) {
bgneal@429 67 txt = n + " New Messages";
bgneal@429 68 }
bgneal@429 69 else {
bgneal@429 70 txt = "Private Messages";
bgneal@429 71 }
bgneal@429 72 $('#unread_msg_text').html(txt);
bgneal@429 73 }
bgneal@429 74
bgneal@425 75 function showMsg(link, id) {
bgneal@425 76 $msgDialog.msgId = id; // create a msgId attribute on the dialog
bgneal@425 77 var msg = msgCache[id];
bgneal@425 78
bgneal@425 79 // mark as read if necessary
bgneal@429 80 var $link = $(link);
bgneal@429 81
bgneal@429 82 if (username == msg.receiver && $link.hasClass('unread')) {
bgneal@425 83 $(link).removeClass('unread');
bgneal@429 84
bgneal@429 85 // decrement count of unread messages in base template
bgneal@429 86 if (unreadMsgCount > 0)
bgneal@429 87 {
bgneal@429 88 updateUnreadMsgText(--unreadMsgCount);
bgneal@429 89 }
bgneal@425 90 }
bgneal@425 91
bgneal@425 92 $msgDialog.html(msg.content);
bgneal@425 93 var title = 'PM From ' + msg.sender + ' To ' + msg.receiver + '<br /> ' + msg.subject;
bgneal@425 94 $msgDialog.dialog('option', 'title', title);
bgneal@425 95 $msgDialog.dialog('open');
bgneal@425 96 }
bgneal@425 97
bgneal@425 98 function msgShow(link, id) {
bgneal@425 99 if (msgCache[id]) {
bgneal@425 100 showMsg(link, id);
bgneal@425 101 return;
bgneal@425 102 }
bgneal@425 103 $.ajax({
bgneal@429 104 url: '/messages/message/',
bgneal@425 105 type: 'POST',
bgneal@425 106 data: {
bgneal@425 107 msg_id : id
bgneal@425 108 },
bgneal@425 109 dataType: 'json',
bgneal@425 110 success: function (data, textStatus) {
bgneal@425 111 msgCache[id] = data;
bgneal@425 112 showMsg(link, id);
bgneal@425 113 },
bgneal@425 114 error: function (xhr, textStatus, ex) {
bgneal@425 115 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 116 xhr.responseText);
bgneal@425 117 }
bgneal@425 118 });
bgneal@425 119 }
bgneal@425 120
bgneal@425 121 function submitOptions(form) {
bgneal@425 122 $.ajax({
bgneal@429 123 url: '/messages/options-tab/',
bgneal@425 124 type: 'POST',
bgneal@425 125 data: $(form).serialize(),
bgneal@425 126 dataType: 'html',
bgneal@425 127 success: function (data, textStatus) {
bgneal@425 128 $(selectedTab.panel).html(data);
bgneal@425 129 },
bgneal@425 130 error: function (xhr, textStatus, ex) {
bgneal@425 131 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 132 xhr.responseText);
bgneal@425 133 }
bgneal@425 134 });
bgneal@425 135 return false;
bgneal@425 136 }
bgneal@425 137
bgneal@425 138 function messageSubmit(form) {
bgneal@425 139 $.ajax({
bgneal@429 140 url: '/messages/compose-tab/',
bgneal@425 141 type: 'POST',
bgneal@425 142 data: $(form).serialize(),
bgneal@425 143 dataType: 'html',
bgneal@425 144 success: function (data, textStatus) {
bgneal@430 145 $(selectedTab.panel).html(data);
bgneal@425 146 },
bgneal@425 147 error: function (xhr, textStatus, ex) {
bgneal@425 148 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 149 xhr.responseText);
bgneal@425 150 }
bgneal@425 151 });
bgneal@425 152 return false;
bgneal@425 153 }
bgneal@425 154
bgneal@425 155 function tabMasterCheckClick(box, name) {
bgneal@425 156 var state = $(box).attr('checked');
bgneal@425 157 $('input[name="' + name + '"]').each(function() {
bgneal@425 158 this.checked = state;
bgneal@425 159 });
bgneal@425 160 }
bgneal@425 161
bgneal@425 162 function bulkMsgAction(form, action) {
bgneal@425 163 if (confirm("Really " + action + " checked messages?")) {
bgneal@425 164 $.ajax({
bgneal@429 165 url: '/messages/bulk/',
bgneal@425 166 type: 'POST',
bgneal@425 167 data: $(form).serialize(),
bgneal@425 168 dataType: 'text',
bgneal@425 169 success: function (data, textStatus) {
bgneal@425 170 $tabs.tabs("load", selectedTab.index);
bgneal@425 171 },
bgneal@425 172 error: function (xhr, textStatus, ex) {
bgneal@425 173 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 174 xhr.responseText);
bgneal@425 175 }
bgneal@425 176 });
bgneal@425 177 }
bgneal@425 178 return false;
bgneal@425 179 }
bgneal@425 180
bgneal@425 181 function ajaxPageFetch(link) {
bgneal@425 182 $.ajax({
bgneal@425 183 url: link.href,
bgneal@425 184 type: 'GET',
bgneal@425 185 dataType: 'html',
bgneal@425 186 success: function (data, textStatus) {
bgneal@425 187 $(selectedTab.panel).html(data);
bgneal@425 188 },
bgneal@425 189 error: function (xhr, textStatus, ex) {
bgneal@425 190 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 191 xhr.responseText);
bgneal@425 192 }
bgneal@425 193 });
bgneal@425 194 return false;
bgneal@425 195 }