annotate gpp/shoutbox/static/js/shoutbox.js @ 322:c3d3d7114749

Fix #148; Django now requires AJAX posts to present the CSRF token. Added code suggested by Django docs to shoutbox.js. Since shoutbox.js is on every page, it should cover all cases.
author Brian Neal <bgneal@gmail.com>
date Sat, 12 Feb 2011 21:37:17 +0000
parents 88b2b9cb8c1f
children 85d7b62d5c17
rev   line source
bgneal@312 1 $(document).ready(function() {
bgneal@322 2 $.ajaxSetup({
bgneal@322 3 beforeSend: function(xhr, settings) {
bgneal@322 4 function getCookie(name) {
bgneal@322 5 var cookieValue = null;
bgneal@322 6 if (document.cookie && document.cookie != '') {
bgneal@322 7 var cookies = document.cookie.split(';');
bgneal@322 8 for (var i = 0; i < cookies.length; i++) {
bgneal@322 9 var cookie = jQuery.trim(cookies[i]);
bgneal@322 10 // Does this cookie string begin with the name we want?
bgneal@322 11 if (cookie.substring(0, name.length + 1) == (name + '=')) {
bgneal@322 12 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
bgneal@322 13 break;
bgneal@322 14 }
bgneal@322 15 }
bgneal@322 16 }
bgneal@322 17 return cookieValue;
bgneal@322 18 }
bgneal@322 19 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
bgneal@322 20 // Only send the token to relative URLs i.e. locally.
bgneal@322 21 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
bgneal@322 22 }
bgneal@322 23 }
bgneal@322 24 });
bgneal@312 25
bgneal@312 26 var numShouts = $('#shoutbox-shout-container > p').size();
bgneal@312 27 var sbBox = $('#shoutbox-shout-container');
bgneal@312 28
bgneal@312 29 if (numShouts < 2)
bgneal@312 30 {
bgneal@312 31 sbBox.append('<p>Welcome to SurfGuitar101.com!</p>');
bgneal@312 32 ++numShouts;
bgneal@312 33 }
bgneal@312 34 if (numShouts < 2)
bgneal@312 35 {
bgneal@312 36 sbBox.append('<p>((((( More Reverb )))))</p>');
bgneal@312 37 ++numShouts;
bgneal@312 38 }
bgneal@312 39
bgneal@312 40 var sbCycleOpts = null;
bgneal@312 41 var sbCycle = sbBox.cycle({
bgneal@312 42 fx: 'scrollUp',
bgneal@312 43 timeout: 5000,
bgneal@312 44 pause: 1,
bgneal@312 45 next: '#shoutbox-next',
bgneal@312 46 prev: '#shoutbox-prev',
bgneal@312 47 before: function(curr, next, opts) {
bgneal@312 48 if (!opts.addSlide || sbCycleOpts) return;
bgneal@312 49 sbCycleOpts = opts;
bgneal@312 50 }
bgneal@312 51 });
bgneal@312 52 function addShout(shout) {
bgneal@312 53 ++numShouts;
bgneal@312 54 sbCycleOpts.addSlide(shout);
bgneal@312 55 sbBox.cycle(numShouts - 1);
bgneal@312 56 }
bgneal@312 57
bgneal@312 58 var submit = $('#shoutbox-submit');
bgneal@312 59 submit.click(function () {
bgneal@312 60 var input = $('#shoutbox-smiley-input');
bgneal@312 61 var msg = $.trim(input.val());
bgneal@312 62 if (msg.length == 0) {
bgneal@312 63 return false;
bgneal@312 64 }
bgneal@312 65 submit.attr('disabled', 'disabled');
bgneal@312 66 $.ajax({
bgneal@312 67 url: '/shout/shout/',
bgneal@312 68 type: 'POST',
bgneal@312 69 data: { msg: msg },
bgneal@312 70 dataType: 'html',
bgneal@312 71 success: function (data, textStatus) {
bgneal@312 72 input.val('');
bgneal@312 73 if (data != '') {
bgneal@312 74 addShout(data);
bgneal@312 75 }
bgneal@312 76 submit.removeAttr('disabled');
bgneal@312 77 },
bgneal@312 78 error: function (xhr, textStatus, ex) {
bgneal@312 79 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 80 xhr.responseText);
bgneal@312 81 }
bgneal@312 82 });
bgneal@312 83 return false;
bgneal@312 84 });
bgneal@312 85 var smilies_loaded = false;
bgneal@312 86 var smiley_frame = $('#shoutbox-smiley-frame');
bgneal@312 87 $('#shoutbox-smilies').click(function () {
bgneal@312 88 smiley_frame.toggle();
bgneal@312 89 if (!smilies_loaded) {
bgneal@312 90 smiley_frame.load('/smiley/farm/', function () {
bgneal@312 91 $('#shoutbox-busy-icon').hide();
bgneal@312 92 var txt = $("#shoutbox-smiley-input")[0];
bgneal@312 93 $('#shoutbox-smiley-frame img').click(function() {
bgneal@312 94 txt.value += ' ' + this.alt + ' ';
bgneal@312 95 txt.focus();
bgneal@312 96 });
bgneal@312 97 smilies_loaded = true;
bgneal@312 98 });
bgneal@312 99 }
bgneal@312 100 });
bgneal@312 101 });