Mercurial > public > sg101
comparison shoutbox/static/js/shoutbox.js @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/shoutbox/static/js/shoutbox.js@85d7b62d5c17 |
children | 69e8aa135c2e |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 $(document).ready(function() { | |
2 $.ajaxSetup({ | |
3 beforeSend: function(xhr, settings) { | |
4 function getCookie(name) { | |
5 var cookieValue = null; | |
6 if (document.cookie && document.cookie != '') { | |
7 var cookies = document.cookie.split(';'); | |
8 for (var i = 0; i < cookies.length; i++) { | |
9 var cookie = jQuery.trim(cookies[i]); | |
10 // Does this cookie string begin with the name we want? | |
11 if (cookie.substring(0, name.length + 1) == (name + '=')) { | |
12 cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); | |
13 break; | |
14 } | |
15 } | |
16 } | |
17 return cookieValue; | |
18 } | |
19 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { | |
20 // Only send the token to relative URLs i.e. locally. | |
21 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); | |
22 } | |
23 } | |
24 }); | |
25 | |
26 $("html").bind("ajaxStart", function() { | |
27 $(this).addClass('busy'); | |
28 }).bind("ajaxStop", function() { | |
29 $(this).removeClass('busy'); | |
30 }); | |
31 | |
32 var numShouts = $('#shoutbox-shout-container > p').size(); | |
33 var sbBox = $('#shoutbox-shout-container'); | |
34 | |
35 if (numShouts < 2) | |
36 { | |
37 sbBox.append('<p>Welcome to SurfGuitar101.com!</p>'); | |
38 ++numShouts; | |
39 } | |
40 if (numShouts < 2) | |
41 { | |
42 sbBox.append('<p>((((( More Reverb )))))</p>'); | |
43 ++numShouts; | |
44 } | |
45 | |
46 var sbCycleOpts = null; | |
47 var sbCycle = sbBox.cycle({ | |
48 fx: 'scrollUp', | |
49 timeout: 5000, | |
50 pause: 1, | |
51 next: '#shoutbox-next', | |
52 prev: '#shoutbox-prev', | |
53 before: function(curr, next, opts) { | |
54 if (!opts.addSlide || sbCycleOpts) return; | |
55 sbCycleOpts = opts; | |
56 } | |
57 }); | |
58 function addShout(shout) { | |
59 ++numShouts; | |
60 sbCycleOpts.addSlide(shout); | |
61 sbBox.cycle(numShouts - 1); | |
62 } | |
63 | |
64 var submit = $('#shoutbox-submit'); | |
65 submit.click(function () { | |
66 var input = $('#shoutbox-smiley-input'); | |
67 var msg = $.trim(input.val()); | |
68 if (msg.length == 0) { | |
69 return false; | |
70 } | |
71 submit.attr('disabled', 'disabled'); | |
72 $.ajax({ | |
73 url: '/shout/shout/', | |
74 type: 'POST', | |
75 data: { msg: msg }, | |
76 dataType: 'html', | |
77 success: function (data, textStatus) { | |
78 input.val(''); | |
79 if (data != '') { | |
80 addShout(data); | |
81 } | |
82 submit.removeAttr('disabled'); | |
83 }, | |
84 error: function (xhr, textStatus, ex) { | |
85 alert('Oops, an error occurred. ' + xhr.statusText + ' - ' + | |
86 xhr.responseText); | |
87 } | |
88 }); | |
89 return false; | |
90 }); | |
91 var smilies_loaded = false; | |
92 var smiley_frame = $('#shoutbox-smiley-frame'); | |
93 $('#shoutbox-smilies').click(function () { | |
94 smiley_frame.toggle(); | |
95 if (!smilies_loaded) { | |
96 smiley_frame.load('/smiley/farm/', function () { | |
97 $('#shoutbox-busy-icon').hide(); | |
98 var txt = $("#shoutbox-smiley-input")[0]; | |
99 $('#shoutbox-smiley-frame img').click(function() { | |
100 txt.value += ' ' + this.alt + ' '; | |
101 txt.focus(); | |
102 }); | |
103 smilies_loaded = true; | |
104 }); | |
105 } | |
106 }); | |
107 }); |