/
pushtape-player.js
1161 lines (1044 loc) · 44.5 KB
/
pushtape-player.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Pushtape-Player.js
* https://github.com/zirafa/pushtape-player.js
* -------------------------
* This player was created as part of the Pushtape (pushtape.com) project to support more flexible music player options.
*
* Notes:
* - This player only binds its behavior to CSS classes. You can provide all the necessary controls markup, or optionally let the player generate it.
* - There are individual play/pause controls per link, as well as global playback controls - pause/play, scrubber, previous/next, and time.
* - By default, it grabs all links in document.body. You can optionally set a pushtapePlayer.config.containerClass to limit the scope, and
* set pushtapePlayer.config.linkClass to only add links with a given class.
* - Playlist index is automatically set via the data-pushtape-index attribute, based on the order links are found.
* - Sounds are uniquely identified in each link via the data-pushtape-sound-id attribute. If not specified, an automatic ID will be generated per link.
*
* Requires SoundManager 2 Javascript API: http://schillmania.com/projects/soundmanager2/
*/
/**
* Implement PushtapePlayer() constructor
*/
function PushtapePlayer () {
/**
* Variable declarations
*/
var self = this,
_event = null,
pl = this,
sm = soundManager, // soundManager instance
isIE = (navigator.userAgent.match(/msie/i)),
docTitle = document.title,
ua = navigator.userAgent,
isTouchDevice = (ua.match(/ipad|ipod|iphone/i)),
observer = {},
bodyEl = null,
cleanup;
// Controls Template. You can override this before init() if you need to, or just set
// config.addControlsMarkup.enabled = false and add this manually as HTML.
this.defaultControlsMarkup = [
'<div class="pt-controls pt-hide">',
'<a class="pt-play-pause" href="#" title="Play/Pause"><span class="play-btn"><span class="pt-play-icon">▶</span></span><span class="pause-btn"><span class="pt-pause-icon">❚❚</span></span></a>',
'<a class="pt-next" href="#" title="Next"> <span class="pt-next-icon">»</span></a>',
'<a class="pt-previous" href="#" title="Previous"><span class="pt-previous-icon">«</span> </a>',
'<a class="pt-permalink" href=""><span class="pt-current-track-title"></span></a>',
'<div class="pt-scrubber">',
'<div class="pt-statusbar">',
'<div class="pt-loading"></div>',
'<div class="pt-position"><div class="pt-handle"></div></div>',
'</div>',
'</div>',
'<div class="pt-time">',
'<span class="pt-current-time">--:--</span> / <span class="pt-duration">--:--</span>',
'</div>',
'</div>'
].join('\n');
// This gets setup below in init()...remove this?
this.config = {
playNext: true, // stop after one sound, or play through list until end
autoPlay: false, // start playing the first sound right away
repeatAll: false, // repeat playlist after last track
keyboardControl: true, // allow spacebar to toggle play-pause, right and left arrows to seek +/- 5 seconds
containerClass : '', // Default is to scan entire page for links, if set will scan only inside containerClass
autoScan : true, // Automatically observe changes to container and scan for new links to add to playlist
linkClass : '', // Default will add all audio links found. If set (i.e. pushtape-player), will only add audio links that have the class: <a class="pushtape-player" href="file.mp3"></a>
addControlsMarkup: {
'enabled' : false, // Default is false. If true, global controls markup is inserted inside of containerClass
'controlsMarkupClass' :'pt-controls-wrapper', // Wrapper class
'position' : 'top' // Position the controls inside the top or bottom of the document or containerClass
}
}
this.controls = {
// CSS selectors for control elements
playButtonClass: 'pt-play-pause',
playAllButtonClass: 'pt-play-all', // Similar to playButton behavior, useful for some UX designs
nextButtonClass: 'pt-next',
previousButtonClass: 'pt-previous',
currentTimeClass: 'pt-current-time',
durationClass: 'pt-duration',
scrubberClass: 'pt-scrubber',
statusBarClass: 'pt-statusbar',
positionClass: 'pt-position',
loadingClass: 'pt-loading',
trackTitleClass: 'pt-current-track-title', // current track
permaLinkClass: 'pt-permalink', // permalink for additional info
// DOM elements for controls. These are assigned values in this.init method
playButton: null,
playAllButton: null,
nextButton: null,
previousButton: null,
currentTime: null,
duration: null,
statusBar: null,
loading: null,
position: null,
scrubber: null,
trackTitle: null,
permaLink: null
};
this.playableClass = 'pt-playable'; // CSS class for forcing a link to be playable (eg. doesn't have .MP3 in it)
this.excludeClass = 'pt-exclude'; // CSS class for ignoring MP3 links
this.cueClass = 'pt-cue'; // CSS class for adding track to end of playlist
this.playSymbol = '▶', // Used in document.title when sound is playing
this.pauseSymbol = '❚❚',
this.links = []; // keep track of current links on page
this.prevLinks = []; // keep track of previous links on page, useful for mutation observer
this.lastSound = null; // keep track of last sound played
this.playStatus = 'stopped'; // keep track of status of playback: stopped, paused, playing, finished, resumed
this.soundCount = 0; // Used to create a unique sound ID
this.css = {
// CSS class names appended to each link during various states
sDefault: 'pt-link', // default state
sLoading: 'pt-loading',
sPlaying: 'pt-playing',
sBuffering: 'pt-buffering',
sError: 'pt-error',
sPaused: 'pt-paused',
sContainer: 'pt-container'
}
/**
* DOM helper functions (replace with jQuery?)
*/
_event = (function() {
var old = (window.attachEvent && !window.addEventListener),
_slice = Array.prototype.slice,
evt = {
add: (old?'attachEvent':'addEventListener'),
remove: (old?'detachEvent':'removeEventListener')
};
function getArgs(oArgs) {
var args = _slice.call(oArgs), len = args.length;
if (old) {
args[1] = 'on' + args[1]; // prefix
if (len > 3) {
args.pop(); // no capture
}
} else if (len === 3) {
args.push(false);
}
return args;
}
function apply(args, sType) {
var element = args.shift(),
method = [evt[sType]];
if (old) {
element[method](args[0], args[1]);
} else {
element[method].apply(element, args);
}
}
function add() {
apply(getArgs(arguments), 'add');
}
function remove() {
apply(getArgs(arguments), 'remove');
}
return {
'add': add,
'remove': remove
};
}());
this.hasClass = function(o, cStr) {
if (!o || !cStr) {
return false;
}
return (typeof(o.className)!=='undefined'?new RegExp('(^|\\s)'+cStr+'(\\s|$)').test(o.className):false);
};
this.addClass = function(o, cStr) {
if (!o || !cStr || self.hasClass(o,cStr)) {
return false; // safety net
}
if (o.constructor === Array) {
for (i = 0; i < o.length; i++ ) {
o[i].className = (o[i].className?o[i].className+' ':'')+cStr;
}
}
else {
o.className = (o.className?o.className+' ':'')+cStr;
}
};
this.removeClass = function(o, cStr) {
if (!o || !cStr || !self.hasClass(o,cStr)) {
return false;
}
if (o.constructor === Array) {
for (i = 0; i < o.length; i++ ) {
o[i].className = o[i].className.replace(new RegExp('( '+cStr+')|('+cStr+')','g'),'');
}
}
else {
o.className = o.className.replace(new RegExp('( '+cStr+')|('+cStr+')','g'),'');
}
};
this.isChildOfClass = function(oChild, oClass) {
if (!oChild || !oClass) {
return false;
}
while (oChild.parentNode && !self.hasClass(oChild,oClass)) {
oChild = oChild.parentNode;
}
return (self.hasClass(oChild,oClass));
};
this.getElementsByAttribute = function(attribute) {
return document.querySelectorAll('[' + attribute + ']');
}
this.getParentByNodeName = function(oChild, sParentNodeName) {
if (!oChild || !sParentNodeName) {
return false;
}
sParentNodeName = sParentNodeName.toLowerCase();
while (oChild.parentNode && sParentNodeName !== oChild.parentNode.nodeName.toLowerCase()) {
oChild = oChild.parentNode;
}
return (oChild.parentNode && sParentNodeName === oChild.parentNode.nodeName.toLowerCase()?oChild.parentNode:null);
};
this.getOffX = function(o) {
// http://www.xs4all.nl/~ppk/js/findpos.html
var curleft = 0;
if (o.offsetParent) {
while (o.offsetParent) {
curleft += o.offsetLeft;
o = o.offsetParent;
}
}
else if (o.x) {
curleft += o.x;
}
return curleft;
};
this.isChildOfNode = function(o,sNodeName) {
if (!o || !o.parentNode) {
return false;
}
sNodeName = sNodeName.toLowerCase();
do {
o = o.parentNode;
} while (o && o.parentNode && o.nodeName.toLowerCase() != sNodeName);
return (o.nodeName.toLowerCase() == sNodeName?o:null);
}
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
this.debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
this.getTime = function(nMSec, bAsString) {
// convert milliseconds to mm:ss, return as object literal or string
var nSec = Math.floor(nMSec/1000),
min = Math.floor(nSec/60),
sec = nSec-(min*60);
// if (min === 0 && sec === 0) return null; // return 0:00 as null
return (bAsString?(min+':'+(sec<10?'0'+sec:sec)):{'min':min,'sec':sec});
};
this.addStyleBySoundID = function(oLink, cssClass) {
if (!oLink || !cssClass) {
return false;
}
var matchFound = false;
var elements = pl.getElementsByAttribute('data-pushtape-sound-id');
for (var i = 0; i < elements.length; i++) {
if (elements[i].getAttribute('data-pushtape-sound-id') === oLink.id) {
pl.removeClass(elements[i], oLink._data.className);
oLink._data.className = cssClass;
pl.addClass(elements[i], oLink._data.className);
matchFound = true;
}
}
return matchFound;
}
this.removeStyleBySoundID = function(oLink) {
if (!oLink) {
return false;
}
var elements = pl.getElementsByAttribute('data-pushtape-sound-id');
for (var i = 0; i < elements.length; i++) {
if (elements[i].getAttribute('data-pushtape-sound-id') === oLink.id) {
pl.removeClass(elements[i], oLink._data.className);
oLink._data.className = '';
}
}
}
/**
* Handlers for individual sound events as they're started/stopped/played
*/
this.events = {
play: function() {
self.playStatus = 'playing';
// Show play symbol in <title>
document.title = self.playSymbol + ' ' + docTitle;
// Remove/add class to individual sound link
// Finding elements by attribute allows for continuity in ajax-y situations
// where this._data.oLink is unreliable. Returns false if no matches found
var foundSound = pl.addStyleBySoundID(this, pl.css.sPlaying);
// Remove/add class to global elements
pl.removeClass(self.controls.playButton, self.css.sPaused);
pl.addClass(self.controls.playButton, self.css.sPlaying);
// Play All
pl.removeClass(self.controls.playAllButton, self.css.sPaused);
if (foundSound) {
pl.addClass(self.controls.playAllButton, self.css.sPlaying);
}
// Body
pl.removeClass(bodyEl, self.css.sPaused);
pl.addClass(bodyEl, self.css.sPlaying);
// Add some data attributes to global play controls
if (self.controls.playButton != null) {
self.controls.playButton.setAttribute('data-pushtape-current-sound-id', this.id);
self.controls.playButton.setAttribute('data-pushtape-current-index', this._data.index);
}
if (self.controls.playAllButton != null) {
self.controls.playAllButton.setAttribute('data-pushtape-current-sound-id', this.id);
self.controls.playAllButton.setAttribute('data-pushtape-current-index', this._data.index);
}
// If trackTitle DOM element exists, populate it with current track
if (self.controls.trackTitle != null) {
self.controls.trackTitle.innerHTML = this._data.oTitle;
}
if (self.controls.permaLink != null) {
self.controls.permaLink.href = this._data.permaLink;
}
},
stop: function() {
self.playStatus = 'stopped';
// Remove play symbol from the HTML <title>
document.title = docTitle;
// Remove any CSS classes applied to individual links as well as global elements
pl.removeStyleBySoundID(this);
pl.removeClass(self.controls.playButton, self.css.sPlaying);
pl.removeClass(self.controls.playAllButton, self.css.sPlaying);
pl.removeClass(bodyEl, self.css.sPlaying);
// If controls DOM element exists, reset it
if (self.controls.position != null) {
self.controls.position.style.width = '0px';
}
},
pause: function() {
self.playStatus = 'paused';
// If moving scrubber position...
if (pl.dragActive) {
return false;
}
// Remove play symbol from HTML <title>
document.title = docTitle;
// Add/remove individual link CSS classes
pl.addStyleBySoundID(this, pl.css.sPaused);
// Add/Remove global element CSS classes
pl.removeClass(self.controls.playButton, self.css.sPlaying);
pl.addClass(self.controls.playButton, self.css.sPaused);
// Play All
pl.removeClass(self.controls.playAllButton, self.css.sPlaying);
pl.addClass(self.controls.playAllButton, self.css.sPaused);
// Body
pl.removeClass(bodyEl, self.css.sPlaying);
pl.addClass(bodyEl, self.css.sPaused);
},
resume: function() {
self.playStatus = 'resumed';
// If moving scrubber position...
if (pl.dragActive) {
return false;
}
// Add play symbol to HTML <title>
document.title = self.playSymbol + ' ' + docTitle;
// Add/remove link specific CSS classes
pl.addStyleBySoundID(this, pl.css.sPlaying);
// Add/remove global elements CSS
pl.removeClass(self.controls.playButton, self.css.sPaused);
pl.addClass(self.controls.playButton, self.css.sPlaying);
// Play All
pl.removeClass(self.controls.playAllButton, self.css.sPaused);
pl.addClass(self.controls.playAllButton, self.css.sPlaying);
// Body
pl.removeClass(bodyEl, self.css.sPaused);
pl.addClass(bodyEl, self.css.sPlaying);
},
finish: function() {
self.playStatus = 'finished';
// Restore original HTML <title>
document.title = docTitle;
// Remove individual link CSS classes
pl.removeStyleBySoundID(this);
// Remove global CSS state classes
pl.removeClass(self.controls.playButton, self.css.sPlaying);
pl.removeClass(self.controls.playButton, self.css.sPaused);
// Play All
pl.removeClass(self.controls.playAllButton, self.css.sPlaying);
pl.removeClass(self.controls.playAllButton, self.css.sPaused);
// Body
pl.removeClass(bodyEl, self.css.sPlaying);
pl.removeClass(bodyEl, self.css.sPaused);
// If position DOM element exists, reset it
if (self.controls.position != null) { self.controls.position.style.width = '0px';}
// After sound finishes, play next sound if playNext is true
if (pl.config.playNext && self.lastSound._data.orphanedIndex != true) {
self.globalNext();
}
},
whileplaying: function() {
var currentTime = pl.getTime(this.position, true);
var duration = pl.getTime(this.durationEstimate, true);
// Populate current time and duration elements if they exist
if (self.controls.currentTime != null) { self.controls.currentTime.innerHTML = currentTime; }
if (self.controls.duration != null) { self.controls.duration.innerHTML = duration; }
// If we are dragging...
if (pl.dragActive) {
if ( self.controls.position != null) {
self.controls.position.style.width = (Math.floor((this.position/self.getDurationEstimate(this))*10000)/100+'%');
}
}
// This technique of flooring and (10000/100) allows for more granular control over the num. of significant figures
if (self.controls.position != null) {
self.controls.position.style.width = (Math.floor((this.position/self.getDurationEstimate(this))*10000)/100+'%');
}
},
whileloading: function() {
if (self.controls.loading != null) {
self.controls.loading.style.width = (((this.bytesLoaded/this.bytesTotal)*100)+'%');
}
},
bufferchange: function() {
// If we encounter an error state, 404, etc just try and skip to the next track
if (this.readyState == '2') {
sm._writeDebug('Error loading file. Attempting to play the next track...');
var currentSound = this;
setTimeout(function(){
var bufferSound = currentSound;
pl.removeClass(bufferSound._data.oLink, self.css.sBuffering); //Individual link
pl.removeClass(self.controls.playButton, self.css.sBuffering); //Global button
pl.removeClass(self.controls.playAllButton, self.css.sBuffering); // Play All
pl.removeClass(bodyEl, self.css.sBuffering); // Body
pl.addClass(bufferSound._data.oLink, self.css.sError);
self.globalNext();
}, 250);
return;
}
// If we are buffering and don't have an error state, add buffering classes
if (this.isBuffering) {
pl.addClass(this._data.oLink, self.css.sBuffering); // Individual link
pl.addClass(self.controls.playButton, self.css.sBuffering); // Global button
pl.addClass(self.controls.playAllButton, self.css.sBuffering); // Play All
pl.addClass(bodyEl, self.css.sBuffering); // Body
sm._writeDebug('Buffering...');
}
else {
pl.removeClass(this._data.oLink, self.css.sBuffering); //Individual link
pl.removeClass(self.controls.playButton, self.css.sBuffering); //Global button
pl.removeClass(self.controls.playAllButton, self.css.sBuffering); // Play All
pl.removeClass(bodyEl, self.css.sBuffering); // Body
}
}
}
/**
* handleMouseDown, handleMouseMove, stopDrag, and setPosition are used to make the scrubber work.
*/
this.handleMouseDown = function(e) {
// a sound link was clicked
if (isTouchDevice && e.touches) {
e = e.touches[0];
}
if (e.button === 2) {
if (!pl.config.allowRightClick) {
pl.stopEvent(e);
}
return pl.config.allowRightClick; // ignore right-clicks
}
var o = self.getTheDamnTarget(e);
if (!o) {
return true;
}
if (!self.withinStatusBar(o)) {
return true;
}
if (!self.lastSound) {
return true;
}
self.dragActive = true;
self.lastSound.pause();
self.setPosition(e);
// Set keyboard focus
if (self.config.keyboardControl && self.controls.playButton) {
self.controls.playButton.focus();
}
if (!isTouchDevice) {
_event.add(document,'mousemove',self.handleMouseMove);
} else {
_event.add(document,'touchmove',self.handleMouseMove);
}
if (self.controls.scrubber != null) {
self.addClass(self.controls.scrubber,'dragging');
}
return self.stopEvent(e);
};
this.keyboardToggle = function(e) {
var activeEl = document.activeElement;
// Allow spacebar to play-pause only if we have focus, otherwise default to normal spacebar behavior
if (!self.config.keyboardControl && !self.hasClass(activeEl, self.controls.playButtonClass) && !self.hasClass(activeEl, self.controls.playAllButtonClass) && !self.hasClass(activeEl, self.css.sDefault) && !self.hasClass(activeEl, self.controls.scrubberClass)) {
return true;
}
// 32 is spacebar
if (e.keyCode == "32") {
self.globalTogglePlay();
}
// 39 is right arrow (skip 5 seconds)
if (e.keyCode == "39") {
self.seekTime(5000);
}
// 37 is left arrow (rewind 5 seconds)
if (e.keyCode == "37") {
self.seekTime(-5000);
}
return self.stopEvent(e);
}
this.handleMouseMove = function(e) {
if (isTouchDevice && e.touches) {
e = e.touches[0];
}
// set position accordingly
if (self.dragActive) {
if (self.config.useThrottling) {
// be nice to CPU/externalInterface
var d = new Date();
if (d-self.dragExec>20) {
self.setPosition(e);
} else {
window.clearTimeout(self.dragTimer);
self.dragTimer = window.setTimeout(function(){self.setPosition(e);},20);
}
self.dragExec = d;
} else {
// oh the hell with it
self.setPosition(e);
}
} else {
self.stopDrag();
}
e.stopPropagation = true;
return false;
};
this.stopDrag = function(e) {
if (self.dragActive) {
self.removeClass(self.controls.scrubber,'dragging');
if (!isTouchDevice) {
_event.remove(document,'mousemove',self.handleMouseMove);
} else {
_event.remove(document,'touchmove',self.handleMouseMove);
}
if (!pl.hasClass(bodyEl, self.css.sPaused)) {
self.lastSound.resume();
}
self.dragActive = false;
return self.stopEvent(e);
}
};
this.setPosition = function(e) {
// called from slider control
var oThis = self.getTheDamnTarget(e),
x, oControl, oSound, nMsecOffset;
if (!oThis) {
return true;
}
oControl = oThis;
while (!self.hasClass(oControl, self.controls.scrubberClass) && oControl.parentNode) {
oControl = oControl.parentNode;
}
oSound = self.lastSound;
x = parseInt(e.clientX,10);
// play sound at this position
nMsecOffset = Math.floor((x-self.getOffX(oControl)-4)/(oControl.offsetWidth)*self.getDurationEstimate(oSound));
if (!isNaN(nMsecOffset)) {
nMsecOffset = Math.min(nMsecOffset,oSound.duration);
}
if (!isNaN(nMsecOffset)) {
oSound.setPosition(nMsecOffset);
}
};
this.seekTime = function(nMsecOffset) {
oSound = self.lastSound;
// play sound at this position
if (!isNaN(nMsecOffset)) {
nMsecOffset = oSound.position + nMsecOffset;
nMsecOffset = Math.min(nMsecOffset, oSound.duration);
}
if (!isNaN(nMsecOffset)) {
oSound.setPosition(nMsecOffset);
}
};
this.stopEvent = function(e) {
if (typeof e != 'undefined' && typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else if (typeof e != 'undefined' && typeof e.returnValue != 'undefined') {
e.returnValue = false;
}
return false;
};
this.getTheDamnTarget = function(e) {
return (e.target||(window.event?window.event.srcElement:null));
};
this.withinStatusBar = function(o) {
return (self.isChildOfClass(o,self.controls.scrubberClass));
};
this.addSound = function(o) {
var soundURL = o.href,
soundIndex,
soundID,
pLink;
if (o.getAttribute('data-pushtape-index') != null) {
soundIndex = Number(o.getAttribute('data-pushtape-index'));
}
else if (self.hasClass(o, self.cueClass)) {
soundIndex = self.links.length; // add to end of the playlist
o.setAttribute('data-pushtape-index', soundIndex); // give it a real index
self.links[soundIndex] = o; //add to "found" links
self.addClass(o, self.css.sDefault); // add pt-link class
}
if (o.getAttribute('data-pushtape-sound-id') != null) {
soundID = o.getAttribute('data-pushtape-sound-id');
}
else {
// Assign unique id internally
soundID = '_soundID_' + self.soundCount++;
o.setAttribute('data-pushtape-sound-id', soundID);
}
if (o.getAttribute('data-pushtape-permalink') != null) {
pLink = o.getAttribute('data-pushtape-permalink');
}
thisSound = sm.createSound({
id: soundID,
url:o.href,
autoPlay: false,
onplay:self.events.play,
onstop:self.events.stop,
onpause:self.events.pause,
onresume:self.events.resume,
onfinish:self.events.finish,
type:(o.type||null),
whileloading:self.events.whileloading,
whileplaying:self.events.whileplaying,
onbufferchange:self.events.bufferchange
});
// tack on some custom data
thisSound._data = {
oLink: o, // DOM node for reference within SM2 object event handlers
className: self.css.sPlaying,
permaLink: pLink,
index: soundIndex, // use HTML data-attribute to reference track index
orphanedIndex: false, // used to flag bad indexes
oTitle: o.title ? o.title: o.innerHTML
};
return thisSound;
}
this.handleClick = function(e) {
self.scanPage();
// a sound link was clicked
if (typeof e.button != 'undefined' && e.button > 1) {
// ignore right-click
return true;
}
var o = self.getTheDamnTarget(e);
if (o.nodeName.toLowerCase() != 'a') {
o = self.isChildOfNode(o,'a');
if (!o) return true;
}
var sURL = o.getAttribute('href');
if (!o.href || (!sm.canPlayLink(o) && !self.hasClass(o,self.playableClass)) || self.hasClass(o,self.excludeClass)) {
return true; // pass-thru for non-MP3/non-links
}
// Prevent duplicate links of same link but outside of scope from firing
if (!o.getAttribute('data-pushtape-index')) {
return true;
}
var thisSound, soundIndex, soundID;
if (o.getAttribute('data-pushtape-sound-id') != null) {
soundID = o.getAttribute('data-pushtape-sound-id');
thisSound = sm.getSoundById(String(soundID));
}
//var addCue = (!self.hasClass(o, self.css.sDefault) && self.hasClass(o, self.cueClass));
if (thisSound) {
// already exists
if (thisSound === self.lastSound) {
// and was playing (or paused)
thisSound.togglePause();
} else {
// different sound
if (self.lastSound) {
sm._writeDebug('sound different than last sound: ' + self.lastSound.id);
self.stopSound(self.lastSound);
}
thisSound.togglePause(); // start playing current
}
} else {
// stop last sound
if (self.lastSound) {
self.stopSound(self.lastSound);
}
// Add a new sound
thisSound = self.addSound(o);
// Play the sound
thisSound.play();
}
// Store current state of links in case they change during playback
self.prevLinks = self.links;
self.linksChanged = false;
self.lastSound = thisSound; // reference for next call
// Since we override click behavior, give focus to the clicked element
document.activeElement = o;
if (typeof e != 'undefined' && typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else if (typeof e != 'undefined' && typeof e.returnValue != 'undefined') {
e.returnValue = false;
}
return false;
}
this.stopSound = function(oSound) {
soundManager.stop(oSound.id);
soundManager.unload(oSound.id);
}
this.getDurationEstimate = function(oSound) {
if (oSound.instanceOptions.isMovieStar) {
return (oSound.duration);
} else {
return (!oSound._data.metadata || !oSound._data.metadata.data.givenDuration ? (oSound.durationEstimate||0) : oSound._data.metadata.data.givenDuration);
}
}
/**
* Global Control methods
*/
this.globalTogglePlay = function(e) {
if (self.lastSound != null) {
// Toggle active sound
self.lastSound.togglePause();
}
else {
// Otherwise start playing first track
self.handleClick({target:self.links[0], preventDefault:function(){}});
}
if (typeof e != 'undefined' && typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else if (typeof e != 'undefined' && typeof e.returnValue != 'undefined') {
e.returnValue = false;
}
return false;
}
this.globalTogglePlayAll = function(e) {
var lastIndex = self.links.length - 1;
if (self.lastSound != null && self.lastSound._data.index == lastIndex && self.playStatus == 'finished') {
// The playlist has ended, start over
self.handleClick({target:self.links[0], preventDefault:function(){}});
}
else if (self.lastSound != null && self.lastSound._data.orphanedIndex != true) {
// We are currently in the middle of a playlist, act like the globalTogglePlay button
self.globalTogglePlay(e);
}
else {
// self.lastSound == null || self.lastSound._data.orphanedIndex == true
// Playlist context doesn't exist, start from the first track
self.handleClick({target:self.links[0], preventDefault:function(){}});
}
if (typeof e != 'undefined' && typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else if (typeof e != 'undefined' && typeof e.returnValue != 'undefined') {
e.returnValue = false;
}
return false;
}
this.globalNext = function(e) {
sm._writeDebug('Play next track...');
if (self.lastSound && self.lastSound._data.orphanedIndex != true) {
var nextLink = self.lastSound._data.index + 1;
if (nextLink < self.links.length) {
self.stopSound(self.lastSound);
self.handleClick({'target':self.links[nextLink]});
}
else if (pl.config.repeatAll) {
self.stopSound(self.lastSound);
self.handleClick({target:self.links[0], preventDefault:function(){}});
}
} else {
// nothing playing yet, so start first sound
self.handleClick({target:self.links[0], preventDefault:function(){}});
}
if (typeof e != 'undefined' && typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else if (typeof e != 'undefined' && typeof e.returnValue != 'undefined') {
e.returnValue = false;
}
return false;
}
this.globalPrevious = function(e) {
sm._writeDebug('Play previous track...');
if (self.lastSound && self.lastSound._data.orphanedIndex != true) {
var prevLink = self.lastSound._data.index - 1;
if (prevLink >= 0) {
self.stopSound(self.lastSound);
self.handleClick({'target':self.links[prevLink]});
}
else if (pl.config.repeatAll) {
self.stopSound(self.lastSound);
self.handleClick({'target':self.links[self.links.length - 1]});
}
} else {
// nothing playing yet, so start first sound
self.handleClick({target:self.links[0], preventDefault:function(){}});
}
if (typeof e != 'undefined' && typeof e.preventDefault != 'undefined') {
e.preventDefault();
} else if (typeof e != 'undefined' && typeof e.returnValue != 'undefined') {
e.returnValue = false;
}
return false;
}
// Initialize the player and create self.scanPage based on init values
this.init = function(options) {
// Allow options arguments to override config defaults
if (options) {
self.config = {
playNext: options.playNext === false ? false : true, // stop after one sound, or play through list until end
autoPlay: options.autoPlay === true ? true : false, // start playing the first sound right away
repeatAll: options.repeatAll === true ? true : false, // repeat playlist after last track
containerClass : options.containerClass || '', // Default is to scan entire page for links, if set will scan only inside containerClass
autoScan : options.autoScan === true ? true : false, // Automatically observe changes to container and scan for new links to add to playlist
linkClass : options.linkClass || '', // Default will add all audio links found. If set (i.e. pushtape-player), will only add audio links that have the class: <a class="pushtape-player" href="file.mp3"></a>
addControlsMarkup: {
'enabled' : options.addControlsMarkup && options.addControlsMarkup.enabled === true ? true : false, // Default is false. If true, global controls markup is inserted inside of containerClass
'controlsMarkupClass' : options.addControlsMarkup && options.addControlsMarkup.controlsMarkupClass || 'pt-controls-wrapper', // Wrapper class
'position' : options.addControlsMarkup && options.addControlsMarkup.position || 'top' // Position the controls inside the top or bottom of the document or containerClass
}
}
}
sm._writeDebug('pushtapePlayer.init()');
// Default behavior is to scan the entire HTML document for playable links
var container = document.body;
// If a containerClass class is set, limit the playlist scope to that class.
if (self.config.containerClass.length > 0) {
container = document.getElementsByClassName(self.config.containerClass)[0];
}
self.addClass(container, self.css.sContainer);
/* Bind events (clicks, drags, mouseDown)*/
function doEvents(action) { // action: add / remove
_event[action](document,'click',self.handleClick);
_event[action](document,'keydown',self.keyboardToggle);
if (!isTouchDevice) {
_event[action](document,'mousedown',self.handleMouseDown);
_event[action](document,'mouseup',self.stopDrag);
} else {
_event[action](document,'touchstart',self.handleMouseDown);
_event[action](document,'touchend',self.stopDrag);
}
_event[action](window, 'unload', cleanup);
}
cleanup = function() {
doEvents('remove');
};
var countScan = 0;
// Function that scans for links on the page
self.scanPage = function() {
cleanup();
bodyEl = document.getElementsByTagName('body')[0];
if (self.dragActive) {
return;
}
self.config.playNext = (options.playNext === false) ? false : true;
// Find relevant links
var oLinks = container.getElementsByTagName('a');
// Build playlist
self.links = [];
var foundItems = 0;
var currentItem = null;
for (var i = 0; i < oLinks.length; i++) {
if ((sm.canPlayLink(oLinks[i]) || self.hasClass(oLinks[i], self.playableClass)) && !self.hasClass(oLinks[i],self.excludeClass) && !self.hasClass(oLinks[i], self.cueClass)) {
// If linkClass is not set, add all links found, otherwise only add links with the linkClass
if (self.config.linkClass.length <= 0 || self.hasClass(oLinks[i], self.config.linkClass)) {
self.addClass(oLinks[i], self.css.sDefault); // add default CSS decoration
self.links[foundItems] = oLinks[i];
/**
* We use a unique HTML data-attribute to relate each link to appropriate sound index.
* When link is clicked, we use this to play the right sound on the page - without it, we'd have
* to rely on something less reliable like the URL (which fails if there are duplicates).
* For greater control over how this works, on your link you can use the data-pushtape-sound-id
* attribute, which will allow you to uniquely declare and reference sounds that are loaded.
*/
oLinks[i].setAttribute('data-pushtape-index', foundItems);
// Set a flag if we find the current playing sound
if (self.lastSound != null && self.lastSound.hasOwnProperty('id') && oLinks[i].getAttribute('data-pushtape-sound-id') != null) {
if (oLinks[i].getAttribute('data-pushtape-sound-id') == self.lastSound.id) {
currentItem = foundItems;
}
}
foundItems++;
}
}
}
// If current playing item found, update the object