-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
129 lines (112 loc) · 3.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var app = angular.module('clip-launcher', []);
app.directive('fileReader', function($rootScope) {
return {
restrict: 'A',
link: function(scope, element, attr) {
element.bind('change', function(e) {
var files = e.target.files;
$rootScope.$broadcast('files', files);
});
}
};
});
app.directive('audioClip', function($rootScope) {
return {
restrict: 'E',
scope: {src: '=src'},
link: function(scope, element, attr) {
var audio = angular.element('<audio controls></audio>');
var source = angular.element('<source>');
source.attr('type', scope.src.type);
source.attr('src', scope.src.data);
audio.append(source);
element.append(audio);
scope.src.audio = audio[0];
}
};
});
app.directive('keyBinding', function($document, $rootScope) {
return {
restrict: 'E',
scope: {src: '=src'},
replace: true,
template: '<button class="button" ng-click="setKey()">{{src.key}}</button>',
link: function(scope, element, attr) {
scope.setKey = function() {
$rootScope.$broadcast('setting-key');
element.addClass('button-primary');
$document.one('keypress', function(e) {
scope.src.key = e.code;
$rootScope.$broadcast('set-key', scope.src);
scope.$apply();
element.removeClass('button-primary');
});
};
scope.src.toggleColor = function() {
element.toggleClass('button-primary');
};
}
};
})
app.controller('List', function($document, $scope) {
$scope.clips = [];
$scope.playing = false;
$scope.state = 'Play';
$scope.toggleState = function() {
if ($scope.playing) {
$scope.state = 'Play';
} else {
$scope.state = 'Pause';
angular.forEach($scope.clips, function(clip) {
clip.startTime = clip.audio.currentTime;
});
}
$scope.playing = !$scope.playing;
};
$scope.$on('files', function(e, files) {
angular.forEach(files, function(file) {
var reader = new FileReader();
reader.onload = function(fileEvent) {
file.data = fileEvent.target.result;
$scope.clips.push(file);
$scope.$apply();
};
reader.readAsDataURL(file);
});
});
$scope.$on('setting-key', function(e) {
angular.forEach($scope.clips, function(clip) {
if (!clip.audio.paused) {
clip.audio.pause();
}
});
if ($scope.playing) {
$scope.toggleState();
}
});
$document.on('keydown', function(e) {
if ($scope.playing) {
angular.forEach($scope.clips, function(clip) {
if (clip.key && e.code == clip.key) {
if (!clip.toggled) {
clip.toggleColor();
}
clip.toggled = true;
clip.audio.play();
}
});
}
});
$document.on('keyup', function(e) {
if ($scope.playing) {
angular.forEach($scope.clips, function(clip) {
if (clip.key && e.code == clip.key) {
clip.toggleColor();
clip.toggled = false;
clip.audio.pause();
clip.audio.currentTime = clip.startTime;
}
});
}
});
});