Skip to content

Commit

Permalink
Added container elements, volume to array (#513) and fixed scoping bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
danpastori committed May 18, 2022
1 parent 8e6d626 commit 063c37d
Show file tree
Hide file tree
Showing 30 changed files with 701 additions and 157 deletions.
558 changes: 457 additions & 101 deletions dist/amplitude.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/amplitude.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions docs/elements/play-button.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Play Button

There are 4 different levels for a play button. To scope a play button to the type of audio you wish to play, you can apply special attributes to the element.

The play button element responds to a `click` event on desktop browsers or a `touchstart` event on mobile browsers.

### Global Play Button

### Collection Play Button

### Audio Play Button

### Audio in Collection Play Button
File renamed without changes.
21 changes: 21 additions & 0 deletions docs/elements/volume-up.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Volume Up Element

### Overview
The volume up element increases the global volume level of AmplitudeJS by the amount defined. This amount is an integer value between `0` and `100` with `0` being muted, and `100` as maximum. As a safe default, the increment value is set to `5` which means that on click/touch the volume will increment 5%.

### Creating a Volume Up Element

### Related Configuration Variables
Default `5`

`config.volume.increment`

On init:
Amplitude.init({
volume: {
increment: 5
}
})

### Notes
Does not work on iOS
3 changes: 3 additions & 0 deletions docs/migrations/5-x-to-6-0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Upgrading from 5.x to 6.0

There are a variety of breaking changes as we move AmplitudeJS into a more professional audio library. The features added aim to improve developer experience while minimizing breaking changes. I'll try to cover all scenarios in this document. If there is anything I've missed, please let me know.
5 changes: 2 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ export const config = {
* @todo BREAKING CHANGE
*/
volume: {
current: 0.5,
initial: 0.5,
current: 50,
increment: 5,
decrement: 5,
pre_mute_level: 0.5
pre_mute_level: 50
},
// volume: 0.5,

Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions src/elements/ContainerElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AudioContainerElement } from "./ContainerElements/AudioContainerElement";
import { CollectionAudioContainerElement } from "./ContainerElements/CollectionAudioContainerElement";

export class ContainerElement{
static containerElementQuery = '.amplitude-audio-container';

setActiveContainers( direct ){
this.#setActiveAudioContainers();
this.#setActiveCollectionAudioContainers( direct );
}

#setActiveAudioContainers(){
let audioContainerElements = new AudioContainerElement();
audioContainerElements.setActive();
}

#setActiveCollectionAudioContainers( direct ){
let collectionAudioContainerElements = new CollectionAudioContainerElement( direct );
collectionAudioContainerElements.setActive();
}
}
40 changes: 40 additions & 0 deletions src/elements/ContainerElements/AudioContainerElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { config } from "@/config";
import { ConfigState } from "@/services/ConfigState";

export class AudioContainerElement {
static audioContainerElementQuery = '.amplitude-audio-container[data-amplitude-audio-index]:not([data-amplitude-collection-key])';

#elements;
#activeIndex;

setActive(){
if( ConfigState.getScope() == 'audio' ){
this.#findElements();
this.#resetElements();
this.#getActiveIndex();
this.#setActiveContainerElements();
}
}

#findElements(){
this.#elements = document.querySelectorAll( AudioContainerElement.audioContainerElementQuery );
}

#resetElements(){
this.#elements.forEach( function( element ){
element.classList.remove('amplitude-active-audio-container');
});
}

#getActiveIndex(){
this.#activeIndex = config.active_index;
}

#setActiveContainerElements(){
let activeContainerElements = document.querySelectorAll('.amplitude-audio-container[data-amplitude-audio-index="'+this.#activeIndex+'"]:not([data-amplitude-collection-key])');

activeContainerElements.forEach( function( element ){
element.classList.add("amplitude-active-audio-container");
});
}
}
57 changes: 57 additions & 0 deletions src/elements/ContainerElements/CollectionAudioContainerElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { config } from "@/config";
import { ConfigState } from "@/services/ConfigState";

export class CollectionAudioContainerElement {
static collectionAudioContainerElementQuery = '.amplitude-audio-container[data-amplitude-audio-index][data-amplitude-collection-key]';

#direct;
#elements;
#activeIndex;
#activeCollection;

constructor( direct ){
this.#direct = direct;
}

setActive(){
if( ConfigState.getScope() == 'collection' ){
this.#findElements();
this.#resetElements();
this.#getActiveIndex();
this.#setActiveContainerElements();
}
}

#findElements(){
this.#elements = document.querySelectorAll( CollectionAudioContainerElement.collectionAudioContainerElementQuery );
}

#resetElements(){
this.#elements.forEach( function( element ){
element.classList.remove('amplitude-active-audio-container');
});
}

#getActiveIndex(){
this.#activeCollection = ConfigState.getActiveCollection();

if( this.#direct ){
this.#activeIndex = config.collections[ this.#activeCollection ].active_index;
}else{
if( ConfigState.isCollectionShuffled( this.#activeCollection ) ){
this.#activeIndex = config.collections[ this.#activeCollection ].shuffle_list[
config.collections[ this.#activeCollection ].active_index
].index;
}else{
this.#activeIndex = config.collections[ this.#activeCollection ].active_index;
}
}
}

#setActiveContainerElements(){
let activeContainerElements = document.querySelectorAll('.amplitude-audio-container[data-amplitude-audio-index="'+this.#activeIndex+'"][data-amplitude-collection-key="'+this.#activeCollection+'"]');
activeContainerElements.forEach( function( element ){
element.classList.add("amplitude-active-audio-container");
});
}
}
7 changes: 4 additions & 3 deletions src/elements/MuteElement.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Audio } from "@/core/Audio";
import { ConfigState } from "@/services/ConfigState";
import { VolumeSliderElement } from "./VolumeSliderElement";
import { Debug } from "@/services/Debug";
Expand Down Expand Up @@ -68,11 +69,11 @@ export class MuteElement {

elements.forEach( function( element ){
if( ConfigState.getVolume() == 0 ){
element.classList.add("amplitude-not-muted");
element.classList.remove("amplitude-muted");
}else{
element.classList.remove("amplitude-not-muted");
element.classList.add("amplitude-muted");
}else{
element.classList.add("amplitude-not-muted");
element.classList.remove("amplitude-muted");
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/NextElements/CollectionNextElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CollectionNextElement {
}

#handleInteraction(){
let collectionKey = this.attribute('data-amplitude-collection-key');
let collectionKey = this.getAttribute('data-amplitude-collection-key');

if( collectionKey == config.active_collection ){
let collectionNavigation = new CollectionNavigation();
Expand Down
2 changes: 1 addition & 1 deletion src/elements/PauseElements/AudioPauseElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class AudioPauseElement{

#handleInteraction(){
if( !ConfigState.isTouchMoving() ){
let audioIndex = this.attribute('data-amplitude-audio-index');
let audioIndex = this.getAttribute('data-amplitude-audio-index');

// If the scope is audio and the index of the element matches the active audio
// index, then we pause the player.
Expand Down
8 changes: 4 additions & 4 deletions src/elements/PauseElements/CollectionAudioPauseElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ export class CollectionAudioPauseElement{
this.#elements.forEach( (element) => {
if( this.#mobile ){
element.removeEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction.bind(this, element) );
}else{
element.removeEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction.bind(this, element) );
}
});
}

#handleInteraction(){
if( !ConfigState.isTouchMoving() ){
let collectionKey = this.attribute('data-amplitude-collection-key');
let audioIndex = this.attribute('data-amplitude-audio-index');
let collectionKey = element.getAttribute('data-amplitude-collection-key');
let audioIndex = element.getAttribute('data-amplitude-audio-index');

if( config.active_collection == collectionKey &&
config.collections[ collectionKey ].active_index == audioIndex ){
Expand Down
2 changes: 1 addition & 1 deletion src/elements/PauseElements/CollectionPauseElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class CollectionPauseElement{

#handleInteraction(){
if( !ConfigState.isTouchMoving() ){
let collectionKey = this.attribute('data-amplitude-collection-key');
let collectionKey = this.getAttribute('data-amplitude-collection-key');

// Ensure we pause the audio if the active collection is
// what is controlled by this pause element.
Expand Down
14 changes: 7 additions & 7 deletions src/elements/PlayElements/AudioPlayElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ export class AudioPlayElement {
}

#bindInteractions(){
this.#elements.forEach( (element) => {;
this.#elements.forEach( (element) => {
if( this.#mobile ){
element.removeEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction.bind(this, element) );
}else{
element.removeEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction.bind(this, element) );
}
} );
}

#handleInteraction(){
if( !ConfigState.isTouchMoving() ){
let index = this.attribute('data-amplitude-audio-index');
#handleInteraction( element ){
if( !ConfigState.isTouchMoving() ){
let index = element.getAttribute('data-amplitude-audio-index');

if( !AudioChecks.audioExists( index ) ){
Debug.writeMessage('Audio with index "'+index+'" does not exist! Please add an audio object at this index in your configuration.');
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/elements/PlayElements/CollectionAudioPlayElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class CollectionAudioPlayElement {

#handleInteraction(){
if( !ConfigState.isTouchMoving() ){
let collectionKey = this.attribute('data-amplitude-collection-key');
let audioIndex = this.attribute('data-amplitude-audio-index');
let collectionKey = this.getAttribute('data-amplitude-collection-key');
let audioIndex = this.getAttribute('data-amplitude-audio-index');

this.#handleCollectionChanges( collectionKey, audioIndex );
this.#handleAudioChanges( collectionKey, audioIndex );
Expand Down
14 changes: 8 additions & 6 deletions src/elements/PlayPauseElements/AudioPlayPauseElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export class AudioPlayPauseElement {
this.#elements.forEach( (element) => {;
if( this.#mobile ){
element.removeEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction.bind(this, element) );
}else{
element.removeEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction.bind(this, element) );
}
} );
}
Expand All @@ -59,9 +59,9 @@ export class AudioPlayPauseElement {
*
* @returns {boolean|null}
*/
#handleInteraction(){
#handleInteraction( element ){
if( !ConfigState.isTouchMoving() ){
let index = this.attribute('data-amplitude-audio-index');
let index = element.getAttribute('data-amplitude-audio-index');

if( !AudioChecks.audioExists( index ) ){
Debug.writeMessage('Audio with index "'+index+'" does not exist! Please add an audio object at this index in your configuration.');
Expand Down Expand Up @@ -99,10 +99,12 @@ export class AudioPlayPauseElement {
}

#toggleAudio(){
let audio = new Audio();

if( config.audio_element.paused ){
Audio.play();
audio.play();
}else{
Audio.pause();
audio.pause();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ export class CollectionAudioPlayPauseElement {
this.#elements.forEach( (element) => {
if( this.#mobile ){
element.removeEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction );
element.addEventListener("touchend", this.#handleInteraction.bind(this, element) );
}else{
element.removeEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction );
element.addEventListener("click", this.#handleInteraction.bind(this, element) );
}
} );
}

#handleInteraction(){
#handleInteraction( element ){
if( !ConfigState.isTouchMoving() ){
let collectionKey = this.attribute('data-amplitude-collection-key');
let audioIndex = this.attribute('data-amplitude-audio-index');
let collectionKey = element.getAttribute('data-amplitude-collection-key');
let audioIndex = element.getAttribute('data-amplitude-audio-index');

this.#handleCollectionChanges( collectionKey, audioIndex );
this.#handleAudioChanges( collectionKey, audioIndex );
Expand Down Expand Up @@ -78,10 +78,12 @@ export class CollectionAudioPlayPauseElement {
}

#toggleAudio(){
let audio = new Audio();

if( config.audio_element.paused ){
Audio.play();
audio.play();
}else{
Audio.pause();
audio.pause();
}
}

Expand Down
Loading

0 comments on commit 063c37d

Please sign in to comment.