Skip to content

Commit

Permalink
implement volume slider which resolves #6
Browse files Browse the repository at this point in the history
- remove slider animations
  • Loading branch information
zachreizner committed Apr 7, 2014
1 parent 47063d3 commit 76d3516
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 11 deletions.
3 changes: 1 addition & 2 deletions beats.css
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ table.playlist {
background-color: #B0B7C1;
border-radius: 5px;
height: 10px;
position: absolute;
transition: width 1s linear; }
position: absolute; }
.control.bar .handle {
background: linear-gradient(to bottom, #7e868c 0%, #374044 100%);
border-radius: 8px;
Expand Down
2 changes: 1 addition & 1 deletion beats.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<div class="control button" title="Pause" data-icon="&#xf04c;" ng-click="pauseSong()"></div>
<div class="control button" title="Skip" data-icon="&#xf051;" ng-click="skipSong()"></div>
<div class="control label" title="Volume" data-icon="&#xf028;"></div>
<div class="control bar volume" title="Volume">
<div class="control bar volume" bar-control="volume" bar-set="setVolume(volume)" bar-dragging="holdVolumeUpdate" bar-min="0" bar-max="100" title="Volume">
<div class="cover" style="width: {{ volume }}%"></div>
<div class="handle" style="left: {{ volume }}%"></div>
</div>
Expand Down
94 changes: 87 additions & 7 deletions beats.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,73 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])
}
};
})
.directive('barControl', function()
{
// Directive for having bar slider based input controls
return {
link: function(scope, element, attrs)
{
// Get the parameters that determine how to set the value
var barMin = attrs.barMin | 0;
var barMax = attrs.barMax | 0;

var dragging = false;

var handleDragging = function(event)
{
if (dragging)
{
// Prevent browser's default dragging behaviour
event.preventDefault();

// Determine ratio from 0 to 1 over the control
var ratioX = (event.clientX - element[0].offsetLeft) / (element[0].offsetWidth);

// Linearly map that ratio to between barMax and barMin
scope[attrs.barControl] = ratioX * (barMax - barMin) + barMin;
if (scope[attrs.barControl] < barMin)
{
scope[attrs.barControl] = barMin;
}
if (scope[attrs.barControl] > barMax)
{
scope[attrs.barControl] = barMax;
}

// Update the view
scope.$digest();
}
};

var finishDragging = function()
{
if (dragging)
{
// Call the callback for whenever the bar is set
scope.$eval(attrs.barSet);
dragging = false;

// Indicate that dragging has stopped
scope[attrs.barDragging] = false;
}
}

element[0].addEventListener('mousedown', function(event)
{
dragging = true;

// Indicate that dragging has started
scope[attrs.barDragging] = true;

handleDragging(event);
});

element[0].addEventListener('mouseup', finishDragging);
element[0].addEventListener('mouseleave', finishDragging);
element[0].addEventListener('mousemove', handleDragging);
}
};
})
.controller('BeatsController', ['$scope', '$http', '$interval', '$cookies', function($scope, $http, $interval, $cookies)
{
var backendBase = 'http://127.0.0.1:5000'
Expand All @@ -47,6 +114,7 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])
$scope.playlist = [];
$scope.queue = [];
$scope.volume = 100;
$scope.holdVolumeUpdate = false;
$scope.playbackTime = 0;
$scope.playbackDuration = 0;

Expand Down Expand Up @@ -78,7 +146,6 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])
})
.success(function(data)
{
console.log(data);
$cookies['crowd.token_key'] = data['token'];
$scope.requestUser();
});
Expand All @@ -96,7 +163,6 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])

$scope.ensureLogin = function()
{
console.log('crowd.token_key = ' + $cookies['crowd.token_key']);
if (!$cookies['crowd.token_key']) {
$scope.showDialog = true;
$scope.usernameFocus = true;
Expand Down Expand Up @@ -172,6 +238,20 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])

$scope.randomSongs();

$scope.setVolume = function(volume)
{
// Set the volume on the client and send it to the server
if (!$scope.ensureLogin()) {
return;
}

$scope.volume = Math.round(volume); // Because of the bar control, this may be a fraction
$http.post(backendBase + '/v1/player/volume', 'volume=' + $scope.volume + '&token=' + $cookies['crowd.token_key'],
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
};

$scope.voteSong = function(song)
{
if (!$scope.ensureLogin()) {
Expand All @@ -181,10 +261,6 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])
$http.post(backendBase + '/v1/queue/add', 'id=' + song.id + '&token=' + $cookies['crowd.token_key'],
{
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data)
{
console.log(data);
});
};

Expand Down Expand Up @@ -223,7 +299,11 @@ angular.module('BeatsApp', ['Beats.filters', 'ngCookies'])
$scope.playbackTime = 0;
$scope.playbackDuration = 0;
}
$scope.volume = data['player_status']['volume'];
// Prevent setting the volume while the user is changing it
if (!$scope.holdVolumeUpdate)
{
$scope.volume = data['player_status']['volume'];
}
});

$http.get(backendBase + '/v1/queue')
Expand Down
1 change: 0 additions & 1 deletion beats.scss
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ table.playlist {
border-radius: 5px;
height: 10px;
position: absolute;
transition: width 1s linear;
}

.handle {
Expand Down

0 comments on commit 76d3516

Please sign in to comment.