The title is a little “metaish” (making a maker), isnt it?. Nevermind, this all started because with my friends we are use to play the sound when you level up in the MuOnline game whenever someone of us did something stupid. One friday night, i was so bored that i made a little soundboard with sounds of the game, you can read the source but if you are going to read this post, you will see it here. The next day, when i showed it to my friends, they went crazy, the wanted to make soundboards for everything you could think of. The first thing that came to my mind was “Why dont i make an app to make soundboards? I bet the web can do that”. Another boring night and i came up with this app which im going to explain in this post. If you just want to use the app, here you have
This post is going to be divided in different sections.
- Maker code
- Getting the dropped files
- Making a reusable component to handle the name and category of the file
- Generating the file
- Making the web offline first
- Soundboard code
- Playing the selected sound
- Making the soundboard offline first
For the visual design, im going to use Material Design Lite (im not going to explain the html and css used, if you want to know about it, visit the mdl docs), an extra-extra small pubsub implementation to handle the events and JSZip to make the final zip file with the soundboard code.
2. Getting the dropped files
To get the dropped files, first of all we have to make an element where files will be dropped, in our case is a div with the class drop__container and add the following js.
In the first line, we get a reference to our DOM object using the querySelector function and then we attach drag and drop events.
In the dragenter and dragleave, we only toggle a class to let the user know that he can drop the files (it turns the div into green).
In the dragover we need to stop the browser from doing the default action when you drag the files, so we are able to handle it ourselves, and we also define the cursor which will be used.
In the drop event is where all the magic happens. First, we stop the browser from handling the files, (usually tries to open it and fallbacks to download it), we loop through each dropped file and add the name to an array. Finally we publish an event with our PubSub implementation, with our filenames as the data.
3. Making a reusable component to handle the name and category of the file
This is just beacuse i dont want to use any framework, but you could use whatever you want, angular, jquery, react, o whatever framework is the hot topic at the moment you are reading this.
You could easily make it an ES6 class (indeed i think its the best idea, but i wanted to give some fallback without any transpiler)
First of all, we define a “class” for our sound object which gets some data and save it internally and then it calls the _buildDOM function which will construct our representation in the dom.
_buildDOM: It just create a bunch of inputs and divs, and stores them in its $ variable for future reuse. The main thing inside this function its that we listen for the blur events in our inputs and updates our internal data to the new values.
getDOMNode: It only returns our main DOM object so we can add it wherever we want.
getJSON: I think its self-explained, but it return our data object
4. Generating the file
Until now, we have the reusable sound element, and the dropped files event, but they arent tie, so we are going to do that.
First of all we make an App object with our entire app logic. The _sounds property will hold our sounds objects and the $ will hold references to our DOM objects.
The init function its our starting point, what make the app run. First, it calls the _buildDOM function, which only gets references of DOM objects of the main form, the input with our app name, and the ul where we are going to store our sounds objects and saves them. It also calls the _bindEvents function, which subscribes to the drop.add event with the addFiles function, and to app.build with the create function. And whenever the main form is submited, it publish to the app.build event.
addFiles: Its a function which will be called whenever the user drops a file. It gets the list of dropped file names, loop through each one, creates a new Sound object, saves it to the _sounds internal property, gets its DOM representation and append it to the list we saved before in our _buildDOM function.
create: This is were the final file is generated. First, we get every representation as object of our saved sounds objects. Then we call our _generateSW function giving this data, which will return an string representing our soundboard service worker. Im not going to explain that function beacuse its just a lot of strings and arrays concatenating. Then we make a new object with our sounds grouped by categories, and call our _generateIndex, similar to the _generateSW function. Im grouping into categories, because we are going to make a tab for everyone. Then we create a new instance of JSZip, add our index and service worker files, and our MDL css and js. Then we call the generateAsync function which resolves to a promise, with the blob of our file. When the promise resolves, we create an url for our blob, add it to an “invisible” anchor tag, set our download file name, and click it. This to user translates to the file being downloaded.
5.Making the web offline first
To make the web offline first, we are going to use the current hot topic, Service workers. The worker its documented itself, and its based in the one from CSSTricks, so you can read the source or read more in his blog post.
Playing the selected sound
To do this, we are going to use a technique called event delegation. This is, we listen for every click in the document, and if we meet a certain criteria, we fire certain callback. You can read more about this technique in this awesome post
What we are doing here in the playAudio variable is calling a self executing fuction, which returns a function, that plays the given url audio. And we listen for every click in the document, and when the target has the data-url attribute, we play that url.
Making the soundboard offline first
This is the same as before, you can read the source of the previous one, or read the blog post.