annotate www/martin/js/nodescore-client.js @ 81:ac6c303fbddc

changes to UI for martin version
author Rob Canning <rob@foo.net>
date Sun, 20 Jul 2014 14:26:06 +0100
parents e4db7d4d1d08
children 74367de56cbf
rev   line source
rc@74 1 /////////////////////////////////////////////////
rc@74 2 // connect to websocket
rc@74 3
rc@74 4 var socket = io.connect();
rc@74 5
rc@74 6 //socket.on("metroPulse", metronomeTick);
rc@74 7 /////////////////////////////////////////////////
rc@74 8 socket.on("metroPulse", pulseInClient);
rc@74 9 function pulseInClient(pulse,groupID,metrobeat){
rc@74 10 var groupPage=document.getElementById('group').value
rc@74 11 if (groupID == groupPage) {
rc@74 12 metronomeTick(1000, groupID, metrobeat);
rc@74 13 }
rc@74 14 }
rc@74 15 /////////////////////////////////////////////////
rc@74 16 function metroCss(beat, beatcolor,text){
rc@74 17 var color = beatcolor;
rc@74 18
rc@74 19 $(".metrocase > div").each(function(){$(this).stop()});
rc@74 20 $(".metrocase > div").each(function(){$(this).css('background-color', beatcolor)});
rc@74 21 $(".metrocase > div").each(function(){$(this).text(" ")});
rc@74 22 $(".metrocase > div").each(function(){$(this).animate({opacity: 1},
rc@74 23 50,
rc@74 24 function() { $(this).animate({opacity:0.0}); }
rc@74 25 );
rc@74 26 })
rc@74 27 }
rc@74 28
rc@74 29 function metronomeTick(pulse, voice,metrobeat) {
rc@74 30 var color = "gray"
rc@74 31 metroCss(0, "red", "4")
rc@74 32 };
rc@74 33
rc@74 34 /////////////////////////////////////////////////
rc@74 35
rc@74 36 // update the stopwatch value on the client page in line with server
rc@74 37 socket.on("chronFromServer", function(chron) {
rc@74 38 $("div#client_chronometer").text(chron);
rc@74 39 });
rc@74 40
rc@74 41
rc@74 42 // server time
rc@74 43
rc@74 44 //var groupPage=document.getElementById('group').value
rc@74 45 //if (groupID == groupPage) {
rc@74 46 socket.on("dateTime", function(datetime) {
rc@74 47 // console.log(datetime)
rc@74 48 $("div#datetime").text("Server Time: " +datetime);
rc@74 49
rc@74 50 });
rc@74 51
rc@74 52
rc@74 53 /////////////////////////////////////////////////
rc@74 54 // countdown to change
rc@74 55
rc@74 56 socket.on("countinFromServer", countinClient);
rc@74 57 function countinClient(groupID, currentseconds,mm,text,colour,background){
rc@74 58 var groupPage=document.getElementById('group').value
rc@74 59 if (groupID == groupPage) {
rc@74 60 console.log(groupID, currentseconds,mm,text,colour,background)
rc@74 61 document.getElementById("count").style.visibility="visible";
rc@74 62 //document.getElementById("count").style.visibility="visible";
rc@74 63 $("#counttitle").text(text);
rc@74 64 $("#count").text(currentseconds);
rc@74 65 //$("#cnt").text(currentseconds);
rc@74 66 $("#count").css('background-color', background);
rc@74 67 document.getElementById("count").style.color=colour;
rc@74 68 // if ( currentseconds == 0) {
rc@74 69 //document.getElementById("midcomms").style.visibility='hidden';
rc@74 70 // }
rc@74 71 }}
rc@74 72 /////////////////////////////////////////////////
rc@74 73
rc@74 74 /////////////////////////////////////////////////
rc@74 75 // countdown to change
rc@74 76
rc@74 77 socket.on("counterText", cText);
rc@74 78 function cText(groupID, currentseconds,text){
rc@74 79
rc@74 80 var groupPage=document.getElementById('group').value
rc@74 81 if (groupID == groupPage) {
rc@74 82 // console.log(text)
rc@74 83 $("#totalcountdown").text(text);
rc@74 84 }}
rc@74 85
rc@74 86 ///////////////////////////////////////
rc@74 87
rc@74 88 function pageFlip(unit) {
rc@74 89 console.log("flipping page:"+ unit);
rc@74 90 $('#sections').trigger('goto', [parseFloat(unit)]);
rc@74 91 }
rc@74 92
rc@74 93
rc@74 94 /////////////////////////////////////////////////
rc@74 95
rc@74 96 // call the fancy jquery functions
rc@74 97
rc@74 98 function slideTo (target) { $('#sections').trigger('goto', [target]); }
rc@74 99 function pad2(number) { return (number < 10 ? '0' : '') + number }
rc@74 100
rc@74 101 /////////////////////////////////////////////////
rc@74 102
rc@74 103 socket.on("pageFlipfromserver", pageTurn);
rc@74 104 function pageTurn (group,unit,time,mm) {
rc@74 105 var groupPage=document.getElementById('group').value;
rc@74 106 if (group == groupPage) {
rc@74 107 var g= pad2(group);
rc@74 108 var p= pad2(unit);
rc@74 109 // pageFlip(unit);
rc@74 110 pageTurnB(unit)
rc@74 111 testSound.play();
rc@74 112 console.log("flipping page:"+ unit);
rc@74 113 }
rc@74 114 else {
rc@74 115 console.log("not for this group... ignoring... for group:" + group );
rc@74 116 }
rc@74 117 }
rc@74 118
rc@74 119 function pageTurnB(unit) {
rc@74 120 var units=6;
rc@74 121 var next=(((unit+1)%units)+units)%units
rc@74 122 console.log("HOP TURN" + unit+ "next:" + next);
rc@74 123 $("#live").load("music.html #"+unit +" *");
rc@74 124 $("#preview").html("<h3 style='background:transparent; position: absolute; top:14px; right:14px;';> n e x t : </h3><img src='thumbs/"+next + ".png" + "' width='400'>")
rc@74 125 }
rc@74 126
rc@74 127 ////////////////////////////////////////////////
rc@74 128
rc@74 129 /* commented out for now as interferes with chat
rc@74 130 need to introduce metakey...manyana... not so usefull anyway really...
rc@74 131
rc@74 132 keyboard controls
rc@74 133 +++++++++++++++++++++++++
rc@74 134
rc@74 135 SPACE to toggle visibilty of preview
rc@74 136 m to toggle visibilty of metronome
rc@74 137 s to toggle visibilty of stopwatch
rc@74 138 h to hide all above
rc@74 139
rc@74 140
rc@74 141
rc@74 142 function toggle_visibility(id) {
rc@74 143 var e = document.getElementById(id);
rc@74 144 if(e.style.display == 'block')
rc@74 145 e.style.display = 'none';
rc@74 146 else
rc@74 147 e.style.display = 'block';
rc@74 148 }
rc@74 149
rc@74 150 $(document).keypress(function(e){
rc@74 151 // "space bar" for next unit preview
rc@74 152 var checkWebkitandIE=(e.which==32 ? 1 : 0);
rc@74 153 var checkMoz=(e.which==32 ? 1 : 0);
rc@74 154 // "m" button for metronome
rc@74 155 var mcheckWebkitandIE=(e.which==109 ? 1 : 0);
rc@74 156 var mcheckMoz=(e.which==109 ? 1 : 0);
rc@74 157 // "s" button for stopwatch/chronometer
rc@74 158 var ccheckWebkitandIE=(e.which==115 ? 1 : 0);
rc@74 159 var ccheckMoz=(e.which==115 ? 1 : 0);
rc@74 160
rc@74 161 // "h" button for hideall
rc@74 162 var hcheckWebkitandIE=(e.which==104 ? 1 : 0);
rc@74 163 var hcheckMoz=(e.which==104 ? 1 : 0);
rc@74 164
rc@74 165 // console.log(e.which);
rc@74 166 if (checkWebkitandIE || checkMoz) { toggle_visibility('preview') }
rc@74 167 if (mcheckWebkitandIE || mcheckMoz) { toggle_visibility('metro') }
rc@74 168 if (ccheckWebkitandIE || ccheckMoz) { toggle_visibility('client_chronometer') }
rc@74 169 if (hcheckWebkitandIE || hcheckMoz) {
rc@74 170 toggle_visibility('client_chronometer')
rc@74 171 toggle_visibility('metro')
rc@74 172 toggle_visibility('preview')
rc@74 173 }
rc@74 174 });
rc@74 175
rc@74 176 */
rc@74 177
rc@74 178 ////////////////////////////////////////////////
rc@74 179 // this needs to have a variable to define the websocket
rc@74 180 // otherwise we will pings from all sockets connected
rc@74 181 // no! the server broadcasts the ping and the clients emit the pong!
rc@74 182 // ah but then the time reported back from the server needs to be targeted
rc@74 183 // to specific client..
rc@74 184 //////////////////////////////////////////////
rc@74 185 // Latency "Pong"
rc@74 186
rc@74 187 socket.on("timeFromServer", function(n) {
rc@74 188 socket.emit("clientTimeResponse",n);
rc@74 189 });
rc@74 190 socket.on("latencyFromServer", function(latency) {
rc@74 191 $("#client_latency").text("Latency: "+latency+"ms.")
rc@74 192 //console.log(latency)
rc@74 193 });
rc@74 194
rc@74 195 function getLatencies(x) { socket.emit("getLatencies", x); }
rc@74 196
rc@74 197 //////////////////////////////////////////////
rc@74 198
rc@74 199
rc@74 200 socket.on('connect', function () {
rc@74 201 $('#chat').addClass('connected');
rc@74 202 });
rc@74 203
rc@74 204 socket.on('announcement', function (msg) {
rc@74 205 $('#lines').append($('<p>').append($('<em>').text(msg)));
rc@74 206 });
rc@74 207
rc@74 208 socket.on('nicknames', function (nicknames) {
rc@74 209 $('#nicknames').empty().append($('<span>Online: </span>'));
rc@74 210 for (var i in nicknames) {
rc@74 211 $('#nicknames').append($('<b>').text(nicknames[i]));
rc@74 212 }
rc@74 213 });
rc@74 214
rc@74 215 socket.on('user message', message);
rc@74 216 socket.on('reconnect', function () {
rc@74 217 $('#lines').remove();
rc@74 218 message('System', 'Reconnected to the server');
rc@74 219 });
rc@74 220
rc@74 221 socket.on('reconnecting', function () {
rc@74 222 message('System', 'Attempting to re-connect to the server');
rc@74 223 });
rc@74 224
rc@74 225 socket.on('error', function (e) {
rc@74 226 message('System', e ? e : 'A unknown error occurred');
rc@74 227 });
rc@74 228
rc@74 229 function message (from, msg) {
rc@74 230 $('#lines').append($('<p>').append($('<b>').text(from), msg));
rc@74 231 }
rc@74 232
rc@74 233 // dom manipulation
rc@74 234 $(function () {
rc@74 235 $('#set-nickname').submit(function (ev) {
rc@74 236 socket.emit('nickname', $('#nick').val(), function (set) {
rc@74 237 if (!set) {
rc@74 238 clear();
rc@74 239 return $('#chat').addClass('nickname-set');
rc@74 240 }
rc@74 241 $('#nickname-err').css('visibility', 'visible');
rc@74 242 });
rc@74 243 return false;
rc@74 244 });
rc@74 245
rc@74 246 $('#send-message').submit(function () {
rc@74 247 message('me', $('#message').val());
rc@74 248 socket.emit('user message', $('#message').val());
rc@74 249 clear();
rc@74 250 $('#lines').get(0).scrollTop = 10000000;
rc@74 251 return false;
rc@74 252 });
rc@74 253
rc@74 254 function clear () {
rc@74 255 $('#message').val('').focus();
rc@74 256 };
rob@79 257 });