annotate gpp/shoutbox/static/js/shoutbox.js @ 427:85d7b62d5c17

Added a global method to change the cursor to an hourglass when an AJAX request fires.
author Brian Neal <bgneal@gmail.com>
date Tue, 26 Apr 2011 02:11:00 +0000
parents c3d3d7114749
children
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@427 26 $("html").bind("ajaxStart", function() {
bgneal@427 27 $(this).addClass('busy');
bgneal@427 28 }).bind("ajaxStop", function() {
bgneal@427 29 $(this).removeClass('busy');
bgneal@427 30 });
bgneal@427 31
bgneal@312 32 var numShouts = $('#shoutbox-shout-container > p').size();
bgneal@312 33 var sbBox = $('#shoutbox-shout-container');
bgneal@312 34
bgneal@312 35 if (numShouts < 2)
bgneal@312 36 {
bgneal@312 37 sbBox.append('<p>Welcome to SurfGuitar101.com!</p>');
bgneal@312 38 ++numShouts;
bgneal@312 39 }
bgneal@312 40 if (numShouts < 2)
bgneal@312 41 {
bgneal@312 42 sbBox.append('<p>((((( More Reverb )))))</p>');
bgneal@312 43 ++numShouts;
bgneal@312 44 }
bgneal@312 45
bgneal@312 46 var sbCycleOpts = null;
bgneal@312 47 var sbCycle = sbBox.cycle({
bgneal@312 48 fx: 'scrollUp',
bgneal@312 49 timeout: 5000,
bgneal@312 50 pause: 1,
bgneal@312 51 next: '#shoutbox-next',
bgneal@312 52 prev: '#shoutbox-prev',
bgneal@312 53 before: function(curr, next, opts) {
bgneal@312 54 if (!opts.addSlide || sbCycleOpts) return;
bgneal@312 55 sbCycleOpts = opts;
bgneal@312 56 }
bgneal@312 57 });
bgneal@312 58 function addShout(shout) {
bgneal@312 59 ++numShouts;
bgneal@312 60 sbCycleOpts.addSlide(shout);
bgneal@312 61 sbBox.cycle(numShouts - 1);
bgneal@312 62 }
bgneal@312 63
bgneal@312 64 var submit = $('#shoutbox-submit');
bgneal@312 65 submit.click(function () {
bgneal@312 66 var input = $('#shoutbox-smiley-input');
bgneal@312 67 var msg = $.trim(input.val());
bgneal@312 68 if (msg.length == 0) {
bgneal@312 69 return false;
bgneal@312 70 }
bgneal@312 71 submit.attr('disabled', 'disabled');
bgneal@312 72 $.ajax({
bgneal@312 73 url: '/shout/shout/',
bgneal@312 74 type: 'POST',
bgneal@312 75 data: { msg: msg },
bgneal@312 76 dataType: 'html',
bgneal@312 77 success: function (data, textStatus) {
bgneal@312 78 input.val('');
bgneal@312 79 if (data != '') {
bgneal@312 80 addShout(data);
bgneal@312 81 }
bgneal@312 82 submit.removeAttr('disabled');
bgneal@312 83 },
bgneal@312 84 error: function (xhr, textStatus, ex) {
bgneal@312 85 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' +
bgneal@312 86 xhr.responseText);
bgneal@312 87 }
bgneal@312 88 });
bgneal@312 89 return false;
bgneal@312 90 });
bgneal@312 91 var smilies_loaded = false;
bgneal@312 92 var smiley_frame = $('#shoutbox-smiley-frame');
bgneal@312 93 $('#shoutbox-smilies').click(function () {
bgneal@312 94 smiley_frame.toggle();
bgneal@312 95 if (!smilies_loaded) {
bgneal@312 96 smiley_frame.load('/smiley/farm/', function () {
bgneal@312 97 $('#shoutbox-busy-icon').hide();
bgneal@312 98 var txt = $("#shoutbox-smiley-input")[0];
bgneal@312 99 $('#shoutbox-smiley-frame img').click(function() {
bgneal@312 100 txt.value += ' ' + this.alt + ' ';
bgneal@312 101 txt.focus();
bgneal@312 102 });
bgneal@312 103 smilies_loaded = true;
bgneal@312 104 });
bgneal@312 105 }
bgneal@312 106 });
bgneal@312 107 });