annotate messages/static/js/tabbed_messages.js @ 601:af403a9f4fb7

Add to last commit.
author Brian Neal <bgneal@gmail.com>
date Sun, 22 Jul 2012 12:06:40 -0500
parents ee87ea74d46b
children ad69236e8501
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@481 35 height: 'auto',
bgneal@481 36 maxHeight: false,
bgneal@481 37 resizable: false,
bgneal@425 38 buttons: [
bgneal@425 39 {
bgneal@425 40 text: "Reply",
bgneal@425 41 click: function() {
bgneal@425 42 doReply = true;
bgneal@425 43 $(this).dialog('close');
bgneal@425 44 $tabs.tabs("select", 1);
bgneal@425 45 }
bgneal@425 46 },
bgneal@425 47 {
bgneal@425 48 text: "Close",
bgneal@425 49 click: function() {
bgneal@425 50 $(this).dialog('close');
bgneal@425 51 }
bgneal@425 52 }
bgneal@425 53 ]
bgneal@425 54 });
bgneal@425 55 });
bgneal@425 56
bgneal@425 57 var $tabs = 0;
bgneal@425 58 var $msgDialog = 0;
bgneal@425 59 var msgCache = {};
bgneal@425 60 var doReply = false;
bgneal@425 61 var selectedTab = 0;
bgneal@425 62
bgneal@429 63 function updateUnreadMsgText(n)
bgneal@429 64 {
bgneal@429 65 var txt = '';
bgneal@429 66 if (n == 1) {
bgneal@429 67 txt = "1 New Message";
bgneal@429 68 }
bgneal@429 69 else if (n > 1) {
bgneal@429 70 txt = n + " New Messages";
bgneal@429 71 }
bgneal@429 72 else {
bgneal@429 73 txt = "Private Messages";
bgneal@429 74 }
bgneal@429 75 $('#unread_msg_text').html(txt);
bgneal@429 76 }
bgneal@429 77
bgneal@425 78 function showMsg(link, id) {
bgneal@425 79 $msgDialog.msgId = id; // create a msgId attribute on the dialog
bgneal@425 80 var msg = msgCache[id];
bgneal@425 81
bgneal@425 82 // mark as read if necessary
bgneal@429 83 var $link = $(link);
bgneal@429 84
bgneal@429 85 if (username == msg.receiver && $link.hasClass('unread')) {
bgneal@425 86 $(link).removeClass('unread');
bgneal@429 87
bgneal@429 88 // decrement count of unread messages in base template
bgneal@429 89 if (unreadMsgCount > 0)
bgneal@429 90 {
bgneal@429 91 updateUnreadMsgText(--unreadMsgCount);
bgneal@429 92 }
bgneal@425 93 }
bgneal@425 94
bgneal@481 95 var s = '<div style="max-height:450px;overflow:auto">' + msg.content + '</div>';
bgneal@481 96 $msgDialog.html(s);
bgneal@425 97 var title = 'PM From ' + msg.sender + ' To ' + msg.receiver + '<br /> ' + msg.subject;
bgneal@425 98 $msgDialog.dialog('option', 'title', title);
bgneal@425 99 $msgDialog.dialog('open');
bgneal@425 100 }
bgneal@425 101
bgneal@425 102 function msgShow(link, id) {
bgneal@425 103 if (msgCache[id]) {
bgneal@425 104 showMsg(link, id);
bgneal@425 105 return;
bgneal@425 106 }
bgneal@425 107 $.ajax({
bgneal@429 108 url: '/messages/message/',
bgneal@425 109 type: 'POST',
bgneal@425 110 data: {
bgneal@425 111 msg_id : id
bgneal@425 112 },
bgneal@425 113 dataType: 'json',
bgneal@425 114 success: function (data, textStatus) {
bgneal@425 115 msgCache[id] = data;
bgneal@425 116 showMsg(link, id);
bgneal@425 117 },
bgneal@425 118 error: function (xhr, textStatus, ex) {
bgneal@425 119 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 120 xhr.responseText);
bgneal@425 121 }
bgneal@425 122 });
bgneal@425 123 }
bgneal@425 124
bgneal@425 125 function submitOptions(form) {
bgneal@425 126 $.ajax({
bgneal@429 127 url: '/messages/options-tab/',
bgneal@425 128 type: 'POST',
bgneal@425 129 data: $(form).serialize(),
bgneal@425 130 dataType: 'html',
bgneal@425 131 success: function (data, textStatus) {
bgneal@425 132 $(selectedTab.panel).html(data);
bgneal@425 133 },
bgneal@425 134 error: function (xhr, textStatus, ex) {
bgneal@425 135 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 136 xhr.responseText);
bgneal@425 137 }
bgneal@425 138 });
bgneal@425 139 return false;
bgneal@425 140 }
bgneal@425 141
bgneal@425 142 function messageSubmit(form) {
bgneal@425 143 $.ajax({
bgneal@429 144 url: '/messages/compose-tab/',
bgneal@425 145 type: 'POST',
bgneal@425 146 data: $(form).serialize(),
bgneal@425 147 dataType: 'html',
bgneal@425 148 success: function (data, textStatus) {
bgneal@430 149 $(selectedTab.panel).html(data);
bgneal@425 150 },
bgneal@425 151 error: function (xhr, textStatus, ex) {
bgneal@425 152 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 153 xhr.responseText);
bgneal@425 154 }
bgneal@425 155 });
bgneal@425 156 return false;
bgneal@425 157 }
bgneal@425 158
bgneal@425 159 function tabMasterCheckClick(box, name) {
bgneal@425 160 var state = $(box).attr('checked');
bgneal@425 161 $('input[name="' + name + '"]').each(function() {
bgneal@425 162 this.checked = state;
bgneal@425 163 });
bgneal@425 164 }
bgneal@425 165
bgneal@425 166 function bulkMsgAction(form, action) {
bgneal@425 167 if (confirm("Really " + action + " checked messages?")) {
bgneal@425 168 $.ajax({
bgneal@429 169 url: '/messages/bulk/',
bgneal@425 170 type: 'POST',
bgneal@425 171 data: $(form).serialize(),
bgneal@425 172 dataType: 'text',
bgneal@425 173 success: function (data, textStatus) {
bgneal@425 174 $tabs.tabs("load", selectedTab.index);
bgneal@425 175 },
bgneal@425 176 error: function (xhr, textStatus, ex) {
bgneal@425 177 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 178 xhr.responseText);
bgneal@425 179 }
bgneal@425 180 });
bgneal@425 181 }
bgneal@425 182 return false;
bgneal@425 183 }
bgneal@425 184
bgneal@425 185 function ajaxPageFetch(link) {
bgneal@425 186 $.ajax({
bgneal@425 187 url: link.href,
bgneal@425 188 type: 'GET',
bgneal@425 189 dataType: 'html',
bgneal@425 190 success: function (data, textStatus) {
bgneal@425 191 $(selectedTab.panel).html(data);
bgneal@425 192 },
bgneal@425 193 error: function (xhr, textStatus, ex) {
bgneal@425 194 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@425 195 xhr.responseText);
bgneal@425 196 }
bgneal@425 197 });
bgneal@425 198 return false;
bgneal@425 199 }