wp-content/uploads/2016/01/PBS_Logo.png

The challenge set at the end of the previous instalment was to build a simple timer web app. This was a much more substantial challenge than those I’ve been setting in the previous handful of instalments, and in involved re-familiarising yourself with concepts we’ve learned before, but not used for some time. For those reasons this instalment will primarily revolve around my sample solution to the challenge. I’ll go through it in much greater detail than I have been doing recently.

It would be a shame to go through an entire instalment without any new content though, so we also meet one very simple but very useful little Bootstrap component, the Spinner. Learning about the spinner sets us up nicely for a new challenge — two simple but important improvement to the timer web app we just built.

You can download this instalment’s ZIP file here.

Listen Along: CCATP Episode 581

MP3 File

PBS 70 Challenge Solution

The challenge set at the end of the previous instalment was to create a simplistic timer web app. The app would consist of a form where the user can enter a number of minutes and a message, and a button to start a timer that will display the message in a modal dialogue when the requested number of minutes have elapsed. Every minute between starting the timer and the final modal a toast dialogue should appear telling the user how long is left. To stop multiple timers being started at once, the form should be disabled while a timer is running. There was bonus credit for creating Toast notifications that did not dismiss automatically, and then dismissing them programatically when the timer ended.

You’ll find my full solution in this instalment’s ZIP file in the folder pbs70-challenge-solution.

I started by creating a page with a jumbotron as a header and a single narrow centred column into which I’d place the form. To keep things nicely aligned I used a single container with two rows, each containing one column. The first row’s column contains the jumbotron, the second row’s column contains the form. To Keep the form centred I used the off-set classes for Bootstrap’s grid. The desired width for the centre column, and hence, the required amount of off-set was different at every breakpoint, so both of the cols ended up with a lot of classes!

You may notice that to get a centred column the rules are quite simple — the width of the centre column must always be even, and the offset will be 12 minus that width divided by two. I.e. at the medium break point the centre column is 8, so the offset is (12 - 8)/2, i.e. 2, hence the two medium breakpoint classes col-md-8 offset-md-2.

Within this single centred column I used Bootstrap’s default form layout, i.e. full-width labels above full-width form controls with full-width help text below that as needed. Each grouping of label, control, and help text is wrapped in a <div> with the class form-group.

For the number of minutes I decided to get a little creative and use a range input which goes from one to five inclusive in steps of one. I figured no one would want to wait more than 5 minutes!

All in all my form is mostly simple and by-the-book:

One small nuance I want to draw your attention to is the label for the range input. When the user slides the slider we would like to show them the current value they’ve selected. I chose to do that by adding a <span> within the label into which I’ll add the number of minutes with JavaScript. I also decided to be grammatically correct, and deal with pluralisation too. So, here’s the code for the label:

Notice that I chose to use classes rather than IDs? Why? Because I figured (rightly as it turns out), that there was a good chance I’d need to deal with pluralisation in more than one place, and, that I’d probably need to show the number of minutes in more than one place too. Using classes lets me update arbitrarily many elements at once.

With the HTML in place, the JavaScript to make it go can be plumbed in. What we need to do is add a handler to the input event to the range slider so the each time the range is adjusted the display of the value and the pluralisation gets updated. We do this by adding the following code inside a jQuery document ready event handler:

Notice the use of jQuery’s .on() function to add the handler, followed immediately by jQuery’s .trigger() function to trigger the event. Firstly, this serves as a good example of jQuery function chaining. If you ignore the arguments to .on() for a moment you’ll see the structure of that code is $min.on().trigger(). To figure out what is going on we need to work from left to right one dot at a time. So, the first thing that happens is that .on() is called on the jQuery object $min (a jQuery object representing the minutes slider). That function adds an event handler to $min, specially, an anonymous function (the second argument) that will be executed each time the input event (first argument) fires on the slider. The key question is, what does .on() return? It returns a reference to the object it was called on, so, in this case, $min.on() returns $min. That means that the .trigger() function is also called on $min. What .trigger() does is execute an event handler, in this case, the input event (the only argument). Putting it all together, the snippet of code adds an event handler for the input event to the slider, and then immediately executes that handler so the page gets initialised correctly.

We can now start thinking about an event handler for the button that starts the timer. We know that we’ll need both a modal and a place to inject our toast notifications. The modal will always have the same basic structure, with just the number of minutes and the message changing, so I decided to add it directly into the document instead of building it up piece-by-piece with jQuery. Because I used classes in the event handler for the minutes slider I can get the number of minutes and any pluralisation I need simply by using the classes .duration_display, .plural_only and .singular_only. The only other customisation that needs to be injected each time the modal is shown is the message, so I gave the tag that will contain the message and ID, #message_display.

Putting all that together, this is the code for my modal:

The ‘toast rack’ for containing the Toast notifications is pretty much identical to the example in the previous instalment:

Before we look at the event handler for the button I just want to note that for convenience I added a bunch of variables to the top of my jQuery document ready handler to hold references to the various elements on the page we’ll need to interact with:

Most of those variables are quite straight-forward, but $formControls deserves a closer look. This jQuery object is built using the CSS selector input, textarea, button, and its scope is constrained to $form, i.e. the form. Remember that in CSS selectors the comma denotes or, so this selector will match all inputs, text areas, and buttons within the form. Why do we need these particular items collected together into a single jQuery variable? Well, these are the items that will need to be disabled and later re-enabled when the timer starts and finishes.

The document ready handler also contains two variables for keeping a record of the timeout and interval IDs we’ll use for triggering the modal and toasts:

Finally, I also added a globally scoped variable to store the current state of the timer:

With the house-keeping out of the way we’re now ready to tackle the big one — the event handler for starting the timer! The code is quite long, so let’s start by ignoring the detail and taking a quick look at the over-all structure:

As you can see, at highest level things are quite simple — a safety check to avoid starting multiple timers, then disable the form’s elements, then start the timeout that will show the modal at the end, then, if needed, start the interval for the toasts. Before we look at the main timeout, let’s look at the interval for the toasts:

You’ll notice that to help keep the code maintainable, I wrote a separate function for displaying a toast, showToast(), which takes two arguments, a string to use as the title, and a string to use as the body. This function consists of a slightly simplified version of the code we saw in the example file pbs70b.html from the previous instalment:

I do want to draw your attention to one detail (highlighted) — to earn the bonus credit on offer, and make the toasts stay around until they are expressly dismissed, notice I pass the toast jQuery plugin an options object that maps the key autohide to false.

The next thing to note is that if the timer is for one minute, we don’t need any toasts at all, hence the if() statement wrapping the creation of the interval. As a reminder a JavaScript interval executes a given function periodically until it is stopped. You create and start an interval with window.setInterval(). That function takes two arguments, the function to execute, and the number of milliseconds to wait between executions. The function returns a numeric ID that identifies that specific interval. It’s important to store that ID because we’ll need to pass it as the only argument to window.clearInterval() to stop the interval running when all the needed toasts have been displayed. We also need a variable outside of the anonymous function being repeatedly executed to store the number of minutes left on the timer so that each subsequent toast can show the correct times, and, so we know when to stop the interval.

Notice that the anonymous function ends by checking if it needs to end the interval. If we omitted that code we’d have what amounts to an infinite loop. Finally, notice that I chose to represent the number of milliseconds as a mathematical equation (1000 * 60). This is something I like to do to make my code clearer and hence easier to maintain, it would be just as valid to directly pass 60000.

We’re now ready to look at the main timer timeout:

Again, the code is very similar to that in the example file pbs70b.html from the previous instalment. Notice we start by using the Bootstrap jQuery plugin toast to hide all the toasts. Next we add our message into the modal. If the user didn’t enter a message I chose to use the ‘speak no evil’ monkey emoji as a placeholder.

Because we want the timer to be able to be used multiple times, it’s important that this code clean up after itself, hence hiding all toasts, re-enabling all form elements, updating the RUNNING flag, and blanking the variable that stores the ID for the timer timeout.

Finally, notice that for clarity I chose to write the timeout’s duration in milliseconds as a Mathematical expression.

Bootstrap Spinners

Something you often need to do in web apps is make it clear to the user that something is happening and that they need to wait. The challenge from last week is an extreme example of this!

The Bootstrap Spinner component exists to fulfil this role. A spinner is an animated icon that moves in such a way as to suggest on-going activity. Bootstrap provides two flavours of spinner that it names Border, and Growing. You can see both in the file pbs71a.html in this instalment’s ZIP file.

You create a spinner giving any tag of your choice either the class .spinner-border or .spinner-grow. That’s all you need to do, but you should do more! To aid accessibility you should give your spinner the ARIA role status, and you should add some screen-reader-only text inside the spinner describing the the icon is indicating, usually ‘loading …’.

When you make a tag a spinner it will become an inline block element (unless you add the spinner inside a flexbox). For that reason I like to use the generic inline tag <span>, but you’ll see many examples that use <div>, and you really could use any tag you like.

By default spinners have no margin, so they will come very close to ‘touching’ what ever comes before or after them on the page, but you can use the Bootstrap spacing utilities to address that as desired.

Putting all that together, the following is the code for the first two spinners on pbs71a.html:

Colouring Spinners

You can control the colour of your spinners using Bootstrap’s text colour utility classes (.text-primary, .text-success etc.). Below is the code from pbs71a.html using some of the colour utilities:

Aligning Spinners

Something you’ll often want to do is centre-align a spinner. Since spinners are inline block elements they behave just like text, so you can centre them like you would any piece of text using Boostrap’s utility classes. Below is an example from pbs71a.html:

You can also centre a spinner using Bootstrap’s flexbox utility classes. Again, an example from pbs71a.html:

Since spinners can be used as flexitems, we can of course do much more with them, like perhaps align them with some text as shown in pbs71a.html:

Note that as this example illustrates, you can support assistive devices in different ways. When you have universally visible text there is no need for the screen-reader-only text inside the spinner itself. Instead, you can hide the spinner from the screenreader only using the aria-hidden attribute.

You can also float your spinners left or right, but that gives you much less control over vertical alignment than you get with flexboxes.

Spinner Sizes

Spinners come in three sizes, small, default (medium), and large. You get a small or large spinner by adding one or the classes .spinner-border-sm or .spinner-border-lg, or .spinner-grow-sm or .spinner-grow-lg.

You can see all six size variants in pbs71a.html:

Notice the user of the flexbox utilities to get nice vertical and horizontal alignment of each group of spinners.

Spinners in Buttons

Since spinners are just inline block elements you can use them anywhere you can use any other inline block element (like an image). This means you can use them inside buttons.

The most common use-case for spinners in buttons is to add them dynamically when the user clicks the button. It’s also normal to disable the button at this point to stop the user clicking again. When what ever action the user requested completes the spinner would then be removed or hidden and the button re-enabled.

Two common approaches are to replace the text in the button with the spinner, or, to add the spinner with some visible text. You can see both in action in pbs71a.html.

Let’s start with the first approach. The markup is quite simple:

Note that because in this case the spinner will be the only thing visible in the button once the button is clicked I do have to include the screen-reader-only text inside the spinner. I also chose to use a small spinner because I think that looks better.

With the markup in place we need to add a click hander that will hide the text, show the spinner and disable the button. We do this inside the document ready event handler:

In a real-world situation some other event handler would be responsible for re-enabling the button. To demonstrate how that would work in this contrived situation we can add a timeout into the click handler to re-enable the button after 3 seconds:

The markup for the second button is also quite straight forward:

Since the spinner is purely decorative in this scenario it has not screen-reader-only text within it, but is instead marked as hidden to screen readers with the aria-hidden attribute.

With the markup in place we can add the event handler. Like in our previous example, in the real world the re-enabling would be done in a separate event handler, but in this case we’re using a 3 second timeout set within the click handler:

A Challenge from Bart

Update your timer web app to solve two problems:

  1. Give the user visual feedback that the timer is running.
  2. Allow the user to cancel the timer while its running.

A Challenge From Allison

The documentation shows that you can use the Bootstrap dismiss plugin with Toasts to dismiss them, but it doesn’t seem to actually work with Bootstrap 4.2. Can you get it to work?

Final Thoughts

We’ve now covered almost all I want to cover in our first pass through Bootstrap, but before we make a final push I want to divert us from that course for a one-instalment detour into template strings. Bootstrap Toasts are a great example of where template text can really make your web apps easier to develop and maintain, so this seems like the perfect time for this little detour. There are many templating libraries out there, but the one I enjoy using the most is Mustache, so that’s what we’ll be learning about next time.