annotate gpp/messages/static/js/tabbed_messages.js @ 429:d0f0800eef0c

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