diff options
Diffstat (limited to 'view/poc/class')
| -rw-r--r-- | view/poc/class/chat.js | 164 | ||||
| -rw-r--r-- | view/poc/class/playList.js | 83 |
2 files changed, 247 insertions, 0 deletions
diff --git a/view/poc/class/chat.js b/view/poc/class/chat.js new file mode 100644 index 0000000..23baeda --- /dev/null +++ b/view/poc/class/chat.js @@ -0,0 +1,164 @@ +var Chat=function(){ + function joinRoom(){ + var room=document.getElementById( + 'room-name' + ).value; + + if(socket.room) + leaveRoom(); + + socket.room=room; + socket.emit( + 'room.join', + { + room:socket.room + } + ); + + document.getElementById('user-current-room').innerHTML=socket.room; + var join=document.getElementById('modal-join'); + join.style.opacity=0; + join.style.zIndex=0; + document.getElementById('chat-primary').style.opacity=1; + document.getElementById('playlist-area').style.opacity=1; + document.querySelector('main').style.zIndex=5 + } + + function leaveRoom(){ + socket.emit( + 'room.leave', + { + room:socket.room + } + ); + + document.getElementById('user-current-room').innerHTML='nowhere'; + } + + function setHandle(e){ + if (webkitNotifications.checkPermission() != 0) { + webkitNotifications.requestPermission(); + } + + socket.handle=document.getElementById( + 'user-handle' + ).value + + socket.emit( + 'set.handle', + { + handle:socket.handle + } + ) + + document.getElementById('user-current-handle').innerHTML=socket.handle; + var handle=document.getElementById('modal-handle'); + handle.style.opacity=0; + handle.style.zIndex=0; + var join=document.getElementById('modal-join'); + join.style.opacity=1; + join.style.zIndex=5; + } + + function fromRoom(data){ + var box=document.getElementById( + 'chat-primary-window' + ), + newItem=document.createElement('li'); + + newItem.innerHTML+='<p>'+ + data.handle+':<pre>'+ + data.msg+ + '</pre></p>'; + //box.insertBefore(newItem,box.getElementsByTagName('li')[0]); + box.appendChild(newItem); + box.scrollTop=box.scrollHeight; + //should not scroll down if user is not at the bottom. + } + + function toRoom(){ + if(!socket.room) + return; + + var input=document.getElementById( + 'chat-primary-input' + ); + var data={ + msg:input.value + } + input.value=''; + socket.emit( + 'room.chat', + data + ); + } + + function fromUser(data){ + //show private message + } + + function toUser(){ + //send private message + } + + function userOnline(data){ + var notification=webkitNotifications.createNotification( + '', + data.handle+' is Online', + data.handle+' has just come online on the server' + ); + notification.show(); + ( + function(){ + setTimeout( + function() { + notification.close(); + }, + 1000 + ); + } + )(notification) + } + + function reConnect(){ + if(socket.handle) + socket.emit( + 'set.handle', + { + handle:socket.handle + } + ); + + if(socket.room) + socket.emit( + 'room.join', + { + room:socket.room + } + ); + } + + return { + room : { + join : joinRoom, + leave : leaveRoom, + chat : toRoom + }, + user : { + chat:toUser + }, + set : { + handle:setHandle + }, + on : { + room : { + chat:fromRoom + }, + user : { + chat : fromUser, + online : userOnline + }, + connect : reConnect + } + }; +}
\ No newline at end of file diff --git a/view/poc/class/playList.js b/view/poc/class/playList.js new file mode 100644 index 0000000..b161ce8 --- /dev/null +++ b/view/poc/class/playList.js @@ -0,0 +1,83 @@ +var PlayList=function(el){ + var playList= el, + files={}, + list=[], + loops=4, + status = { + playing : false, + current : 0, + currentLoop : 0 + }, + audioBuffer = null; + + window.AudioContext = window.AudioContext || window.webkitAudioContext; + var context = new AudioContext(); + + function addItem(data){ + list.push(data.url); + playList.innerHTML+='<li>'+ + data.url+ + '</li>'; + + if(!status.playing) + play(data.url); + } + + function play(data){ + audioBuffer = context.createBufferSource(); + audioBuffer.buffer=(typeof data=='string')? + ( + function(){ + status.currentLoop=0; + return files[data]; + } + )():data; + audioBuffer.connect(context.destination); + status.playing=true; + audioBuffer.start(0); + audioBuffer.onended=function(){ + status.currentLoop++; + if(status.currentLoop>=loops){ + status.playing=false; + next(); + return; + } + play(this.buffer); + } + } + + function next(){ + var count = list.length; + status.current++; + if(status.current>=count) + status.current=0; + if(!status.playing) + play(list[status.current]); + } + + function recievedAudio(data){ + if(files[data.url]){ + data.file=null; + addItem(data); + return; + } + + var audioBinary=Base64Binary.decodeArrayBuffer(data.file); + context.decodeAudioData( + audioBinary, + function(buffer) { + files[data.url]=buffer; + data.file=null; + addItem(data); + } + ); + } + + return { + add : addItem, + is : status, + on : { + add:recievedAudio + } + } +}
\ No newline at end of file |
