Permalink
Browse files

Private mode replaces preview and mute

  • Loading branch information...
1 parent ce6c602 commit a8f51e23c8c186ce6e82ef36c88d4efacc69b0ed @iain iain committed Dec 17, 2011
Showing with 156 additions and 178 deletions.
  1. +2 −14 lib/messenger.rb
  2. +1 −2 public/index.html
  3. +62 −69 public/js/app.coffee
  4. +91 −93 public/js/app.js
View
16 lib/messenger.rb
@@ -9,26 +9,14 @@ def initialize(all, sender, message)
end
def perform
- if preview?
- listen
- else
- spam
- spam_satisfaction
- end
+ spam
+ spam_satisfaction
end
def hash
@hash ||= JSON.parse(@message)
end
- def preview?
- hash.fetch("args"){{}}["preview"]
- end
-
- def listen
- @sender.send(@message)
- end
-
def spam
@all.each do |receiver|
receiver.send(@message) unless me?(receiver)
View
3 public/index.html
@@ -146,8 +146,7 @@
</div>
<div id="options">
- <div id="mute" class="option">MUTE: <span>[<span class="checkbox disabled"> </span>]</span></div>
- <div id="preview" class="option">PREVIEW: <span>[<span class="checkbox disabled"> </span>]</span></div>
+ <div id="private" class="option">PRIVATE: <span>[<span class="checkbox disabled"> </span>]</span></div>
</div>
<div id="status"><span>DISCONNECTED!</span></div>
<div id="overlay"></div>
View
131 public/js/app.coffee
@@ -10,7 +10,6 @@ class View
disconnected: -> "<span>DISCONNECTED!</span>"
playing: (sound) -> "PLAYING SOUND <span>#{sound}</span>"
playingTo: (listeners) -> "PLAYING TO <span>#{listeners}</span> #{if listeners is 1 then "PERSON" else "PEOPLE"}"
- playingButMute: (sound) -> "MUTED, <span>#{sound}</span> IS PLAYING THOUGH"
error: (message) -> "ERROR: <span>#{message}</span>"
connections: (amount) -> "CONNECTIONS: <span>#{amount}</span>"
@@ -47,52 +46,34 @@ class View
overlay: ->
@__overlay ?= $('#overlay')
- preview: ->
- @__preview ?= $('#preview')
+ private: ->
+ @__private ?= $('#private')
- previewCheckbox: ->
- @__previewCheckbox ?= @preview().find('.checkbox')
+ privateCheckbox: ->
+ @__privateCheckbox ?= @private().find('.checkbox')
- enablePreview: ->
- @previewCheckbox().html('X')
+ enablePrivate: ->
+ @privateCheckbox().html('X')
- disablePreview: ->
- @previewCheckbox().html(' ')
+ disablePrivate: ->
+ @privateCheckbox().html(' ')
- mute: ->
- @__mute ?= $('#mute')
-
- muteCheckbox: ->
- @__muteCheckbox ?= @mute().find('.checkbox')
-
- enableMute: ->
- @muteCheckbox().html('X')
-
- disableMute: ->
- @muteCheckbox().html(' ')
class App
constructor: (@view) ->
@controller = new Controller(@view)
@connect()
@initSounds()
- @initPreview()
- @initMute()
+ @initPrivate()
initSounds: ->
@view.sounds().click (event) => @handleSoundClick(event)
- initPreview: ->
- @view.preview().click (event) => @handlePreviewClick(event)
- @preview = false
- @renderPreviewState()
-
- initMute: ->
- @view.mute().click (event) => @handleMuteClick(event)
- @view.muted = false
- @mute = false
- @renderMuteState()
+ initPrivate: ->
+ @view.private().click (event) => @handlePrivateClick(event)
+ @private = false
+ @renderPrivateState()
connect: ->
L 'starting connection'
@@ -104,29 +85,22 @@ class App
handleSoundClick: (event) ->
sound = @view.sound(event)
- versions = @view.versions(event) || "false"
- @socket.send("""{ "action": "playAudio", "args": { "sound": "#{sound}", "preview": #{@preview}, "versions": #{versions} } }""")
-
- handlePreviewClick: (event) ->
- @preview = !@preview
- @renderPreviewState()
-
- renderPreviewState: ->
- if @preview
- @view.enablePreview()
+ versions = @view.versions(event) || false
+ if @private
+ @controller.onPlayAudio(sound: sound, versions: versions)
else
- @view.disablePreview()
+ @socket.send("""{ "action": "playAudio", "args": { "sound": "#{sound}", "versions": #{versions} } }""")
- handleMuteClick: (event) ->
- @mute = !@mute
- @view.muted = @mute
- @renderMuteState()
+ handlePrivateClick: (event) ->
+ @private = !@private
+ @controller.stopAllSounds()
+ @renderPrivateState()
- renderMuteState: ->
- if @mute
- @view.enableMute()
+ renderPrivateState: ->
+ if @private
+ @view.enablePrivate()
else
- @view.disableMute()
+ @view.disablePrivate()
onSocketMessage: (data) ->
L 'onSocketMessage'
@@ -151,26 +125,21 @@ class App
class Controller
constructor: (@view) ->
+ @soundsPlaying = []
- onPlayAudio: (args) ->
- file_base = file = args.sound
-
- if @view.muted
- @view.setStatus "playingButMute", file_base
- setTimeout((=> @view.revertStatus()), 2000)
- else
- L "Playing '#{file_base}'"
- if args.versions
- sound_index = Math.ceil(Math.random() * parseInt(args.versions))
- L "Version: #{sound_index} / #{args.versions}"
- file = "#{file_base}#{sound_index}"
+ stopAllSounds: ->
+ for sound in @soundsPlaying
+ sound.stop()
+ @handleSoundEnd(sound)
- mySound = new buzz.sound( "/audio/#{file}", { formats: [ "ogg", "mp3" ] } )
- mySound.bind("ended", => @handleSoundEnd(file_base))
- mySound.play()
+ onPlayAudio: (args) ->
+ sound = new Sound(args.sound, args.versions)
+ @soundsPlaying.push(sound)
+ sound.bindEnd => @handleSoundEnd(sound)
+ sound.play()
- @view.highlightSound(file_base)
- @view.setStatus "playing", file_base
+ @view.highlightSound(sound.name)
+ @view.setStatus "playing", sound.name
onConnectionChange: (args) ->
@view.setStatus "connections", args.listeners
@@ -181,12 +150,36 @@ class Controller
@timeoutStatus()
handleSoundEnd: (sound) =>
- @view.dehighlightSound(sound)
+ delete(@soundsPlaying[sound])
+ @view.dehighlightSound(sound.name)
@view.revertStatus()
timeoutStatus: ->
setTimeout((=> @view.revertStatus()), 2500)
+class Sound
+
+ constructor: (@name, @versions) ->
+ @buzz = new buzz.sound( "/audio/#{@file()}", { formats: [ "ogg", "mp3" ] } )
+
+ file: ->
+ if @versions
+ sound_index = Math.ceil(Math.random() * parseInt(@versions))
+ L "Version: #{sound_index} / #{@versions}"
+ "#{@name}#{sound_index}"
+ else
+ @name
+
+ bindEnd: (callback) ->
+ @buzz.bind("ended", callback)
+
+ play: ->
+ @buzz.play()
+
+ stop: ->
+ @buzz.stop()
+
+
jQuery ->
new App(new View)
View
184 public/js/app.js
@@ -1,5 +1,5 @@
(function() {
- var App, Controller, L, View;
+ var App, Controller, L, Sound, View;
var __slice = Array.prototype.slice, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
L = function(text) {
@@ -32,9 +32,6 @@
playingTo: function(listeners) {
return "PLAYING TO <span>" + listeners + "</span> " + (listeners === 1 ? "PERSON" : "PEOPLE");
},
- playingButMute: function(sound) {
- return "MUTED, <span>" + sound + "</span> IS PLAYING THOUGH";
- },
error: function(message) {
return "ERROR: <span>" + message + "</span>";
},
@@ -90,40 +87,22 @@
return (_ref = this.__overlay) != null ? _ref : this.__overlay = $('#overlay');
};
- View.prototype.preview = function() {
- var _ref;
- return (_ref = this.__preview) != null ? _ref : this.__preview = $('#preview');
- };
-
- View.prototype.previewCheckbox = function() {
- var _ref;
- return (_ref = this.__previewCheckbox) != null ? _ref : this.__previewCheckbox = this.preview().find('.checkbox');
- };
-
- View.prototype.enablePreview = function() {
- return this.previewCheckbox().html('X');
- };
-
- View.prototype.disablePreview = function() {
- return this.previewCheckbox().html(' ');
- };
-
- View.prototype.mute = function() {
+ View.prototype.private = function() {
var _ref;
- return (_ref = this.__mute) != null ? _ref : this.__mute = $('#mute');
+ return (_ref = this.__private) != null ? _ref : this.__private = $('#private');
};
- View.prototype.muteCheckbox = function() {
+ View.prototype.privateCheckbox = function() {
var _ref;
- return (_ref = this.__muteCheckbox) != null ? _ref : this.__muteCheckbox = this.mute().find('.checkbox');
+ return (_ref = this.__privateCheckbox) != null ? _ref : this.__privateCheckbox = this.private().find('.checkbox');
};
- View.prototype.enableMute = function() {
- return this.muteCheckbox().html('X');
+ View.prototype.enablePrivate = function() {
+ return this.privateCheckbox().html('X');
};
- View.prototype.disableMute = function() {
- return this.muteCheckbox().html(' ');
+ View.prototype.disablePrivate = function() {
+ return this.privateCheckbox().html(' ');
};
return View;
@@ -137,8 +116,7 @@
this.controller = new Controller(this.view);
this.connect();
this.initSounds();
- this.initPreview();
- this.initMute();
+ this.initPrivate();
}
App.prototype.initSounds = function() {
@@ -148,23 +126,13 @@
});
};
- App.prototype.initPreview = function() {
+ App.prototype.initPrivate = function() {
var _this = this;
- this.view.preview().click(function(event) {
- return _this.handlePreviewClick(event);
+ this.view.private().click(function(event) {
+ return _this.handlePrivateClick(event);
});
- this.preview = false;
- return this.renderPreviewState();
- };
-
- App.prototype.initMute = function() {
- var _this = this;
- this.view.mute().click(function(event) {
- return _this.handleMuteClick(event);
- });
- this.view.muted = false;
- this.mute = false;
- return this.renderMuteState();
+ this.private = false;
+ return this.renderPrivateState();
};
App.prototype.connect = function() {
@@ -188,34 +156,28 @@
App.prototype.handleSoundClick = function(event) {
var sound, versions;
sound = this.view.sound(event);
- versions = this.view.versions(event) || "false";
- return this.socket.send("{ \"action\": \"playAudio\", \"args\": { \"sound\": \"" + sound + "\", \"preview\": " + this.preview + ", \"versions\": " + versions + " } }");
- };
-
- App.prototype.handlePreviewClick = function(event) {
- this.preview = !this.preview;
- return this.renderPreviewState();
- };
-
- App.prototype.renderPreviewState = function() {
- if (this.preview) {
- return this.view.enablePreview();
+ versions = this.view.versions(event) || false;
+ if (this.private) {
+ return this.controller.onPlayAudio({
+ sound: sound,
+ versions: versions
+ });
} else {
- return this.view.disablePreview();
+ return this.socket.send("{ \"action\": \"playAudio\", \"args\": { \"sound\": \"" + sound + "\", \"versions\": " + versions + " } }");
}
};
- App.prototype.handleMuteClick = function(event) {
- this.mute = !this.mute;
- this.view.muted = this.mute;
- return this.renderMuteState();
+ App.prototype.handlePrivateClick = function(event) {
+ this.private = !this.private;
+ this.controller.stopAllSounds();
+ return this.renderPrivateState();
};
- App.prototype.renderMuteState = function() {
- if (this.mute) {
- return this.view.enableMute();
+ App.prototype.renderPrivateState = function() {
+ if (this.private) {
+ return this.view.enablePrivate();
} else {
- return this.view.disableMute();
+ return this.view.disablePrivate();
}
};
@@ -255,34 +217,32 @@
function Controller(view) {
this.view = view;
this.handleSoundEnd = __bind(this.handleSoundEnd, this);
+ this.soundsPlaying = [];
}
+ Controller.prototype.stopAllSounds = function() {
+ var sound, _i, _len, _ref, _results;
+ _ref = this.soundsPlaying;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ sound = _ref[_i];
+ sound.stop();
+ _results.push(this.handleSoundEnd(sound));
+ }
+ return _results;
+ };
+
Controller.prototype.onPlayAudio = function(args) {
- var file, file_base, mySound, sound_index;
+ var sound;
var _this = this;
- file_base = file = args.sound;
- if (this.view.muted) {
- this.view.setStatus("playingButMute", file_base);
- return setTimeout((function() {
- return _this.view.revertStatus();
- }), 2000);
- } else {
- L("Playing '" + file_base + "'");
- if (args.versions) {
- sound_index = Math.ceil(Math.random() * parseInt(args.versions));
- L("Version: " + sound_index + " / " + args.versions);
- file = "" + file_base + sound_index;
- }
- mySound = new buzz.sound("/audio/" + file, {
- formats: ["ogg", "mp3"]
- });
- mySound.bind("ended", function() {
- return _this.handleSoundEnd(file_base);
- });
- mySound.play();
- this.view.highlightSound(file_base);
- return this.view.setStatus("playing", file_base);
- }
+ sound = new Sound(args.sound, args.versions);
+ this.soundsPlaying.push(sound);
+ sound.bindEnd(function() {
+ return _this.handleSoundEnd(sound);
+ });
+ sound.play();
+ this.view.highlightSound(sound.name);
+ return this.view.setStatus("playing", sound.name);
};
Controller.prototype.onConnectionChange = function(args) {
@@ -296,7 +256,8 @@
};
Controller.prototype.handleSoundEnd = function(sound) {
- this.view.dehighlightSound(sound);
+ delete this.soundsPlaying[sound];
+ this.view.dehighlightSound(sound.name);
return this.view.revertStatus();
};
@@ -311,6 +272,43 @@
})();
+ Sound = (function() {
+
+ function Sound(name, versions) {
+ this.name = name;
+ this.versions = versions;
+ this.buzz = new buzz.sound("/audio/" + (this.file()), {
+ formats: ["ogg", "mp3"]
+ });
+ }
+
+ Sound.prototype.file = function() {
+ var sound_index;
+ if (this.versions) {
+ sound_index = Math.ceil(Math.random() * parseInt(this.versions));
+ L("Version: " + sound_index + " / " + this.versions);
+ return "" + this.name + sound_index;
+ } else {
+ return this.name;
+ }
+ };
+
+ Sound.prototype.bindEnd = function(callback) {
+ return this.buzz.bind("ended", callback);
+ };
+
+ Sound.prototype.play = function() {
+ return this.buzz.play();
+ };
+
+ Sound.prototype.stop = function() {
+ return this.buzz.stop();
+ };
+
+ return Sound;
+
+ })();
+
jQuery(function() {
return new App(new View);
});

0 comments on commit a8f51e2

Please sign in to comment.