Week 10 Project, Part 1: More JavaScript…

No New News Here – I’m Behind!

I’m way behind on my “52 Weekly Websites.” Sigh. I hope to do better going into the new year (2019). I started this venture in October… of 2016! I’ve actually been working on this project – on and off – for a couple of months now. A non-trivial project mixed with a lot of learning apparently takes time.

An Actual Project This “Week”

So, last “week,” I was tinkering with creating content (and menus) using only JavaScript. I learned a lot and I’m happy with the results, so I thought I’d continue with JavaScript for the next project, which is an actual app that I want to be able to use to track business miles in my vehicle(s).

This week’s project will also use the localStorage (or Web Storage) or maybe the IndexedDB features of HTML5 (or maybe both – but I’ll probably start with localStorage). For this project, I want to be able to input data about trips, both local and long distance – date, time, mileage, vehicle, and a short description. I don’t want to have to use a back-end database on a website somewhere for the data storage, so I plan to store it locally in the browser, and then export it to a file that I can use to track business miles.

A Couple of Realizations (Finally)

Woo-hoo! The DOM is a collection of objects! Well, yeah, everyone except for me realized that already. After all, DOM stands for Document Object Model. I guess I just never quite put together that the elements in the DOM for an HTML page were also child elements, which in turn could have child elements. This became important when both sorting the divs for the trip records (but it didn’t dawn on me then) and when creating, harvesting, and destroying the divs for the data entry/edit form (finally, the reality began to seep into my consciousness).

Also, the CSS3 “important!” tag is sometimes, well, important. I spent more time that I should have trying to figure out why some of my styling wasn’t being applied to a specific element. I pored over the styles for the element using the browser’s developer tools, and making changes on the fly to get the result I wanted, but nothing worked. I finally broke down and just used the important! tag and, voila – success.

Details Begin Next Week

Well, that’s the plan. I sure hope I make it. This project will be split up over at least 3 weeks of posts, but not more than 5. Stand by…

Week 9 Project: JavaScript something-or-other…

This “week’s” project started with a desire to write some JavaScript. I added the sub-directory (or folder, in more common parlance) and copied the index.html and style.css files used for the site’s main page (found at http://dev.kobayashicoding.net) into it. I then removed the text variables used to populate the various panel components, and also removed the “About” and “Contact” buttons, and the “Projects” drop-down menu. I then made changes in the code to display two empty Bootstrap “panel” elements, even though I had not yet determined exactly what I was going to put in them. Finally, I wanted to change the remaining “Home” button so that it would open the base site’s main page again, as this week’s project replaces the main page instead of opening and new tab or window.

Go to http://dev.kobayashicoding.net/, drop down the “Projects” menu, and choose “Week 9 (2018-08-01)” to see the result of this week’s tinkering.

Changing the “Home” button to reload the main page sent me searching for a simple, clean, and efficient way to effect this that would work both when opening the site’s files on my local PC and when viewing the live site on the public Internet. Because button elements don’t have an href  attribute, I went looking for how to accomplish this. My search quickly turned up the way to do this (setting the onclick event to assign ‘../index.html’ to window.location, which is part of the Browser Object Model (BOM)). This also got me thinking about the BOM, which I hadn’t really paid too much attention to before. So I decided to delve into it a bit. Which I did. Briefly. Because that’s all it took, really.

The next thing to do was to set sizes for the two panels. Because I’m using Bootstrap, and the panels are created using the div tag with a class of “panel,” I went to the Bootstrap site’s documentation to get the details of how to size and modify them. Alas, the version of Bootstrap I’d used was v3.7.3, and the “panel” class had been removed in the new version of Bootstrap (currently v4.1.3). So… part of this week’s project was to update the site from Bootstrap v3.7.3 to v4.1.3. In was… interesting… and informative.

The first task of the migration was to read the Migrating to v4 summary on the Bootstrap site. This was quite helpful in pointing out things that would break the site and would have to be changed. The main changes for me were going to entail replacing the “panel” references with references to the new “card” component.

As an aside, in upgrading from Bootstrap v3.7.3 to v4.1.3, I also had to decide how I wanted to implement Bootstrap. There are basically two ways to use the various, required files, either (1) download them and put them in a sub-directory of the site being developed or deployed, or (2) link to the files on the Bootstrap CDN. Previously, I had downloaded the files and stored them on the server with my project’s code. This time, I decided to use the link approach. The advantage of downloading the code and including it on the server is that if any changes are made to the framework, they won’t affect the site, so nothing breaks unexpectedly. And, that’s also the disadvantage – if errors are corrected or new features added, they won’t affect the site or be useful until the version on the site is replaced with the newest code. I figure it’s better to get bug fixes and take a chance on something breaking unexpectedly.

It turned out that the only major change I needed to make to accommodate the changes in Bootstrap was switching from “Panels” to “Cards.” The “Panel” component has been removed from Bootstrap and replaced by the “Card” component. Actually, the Card component is the new flexible content container that replaces Panels, Wells, and Thumbnails. It took only a few minutes to do a global search-and-replace, followed by a few more minutes of cleaning up to replace the Panels with Cards.

There were a few minor changes that I had to accommodate as well. The previously used “hidden” class has been replaced with the HTML 5 “hidden” attribute for elements that need to be displayed only at certain times. I also had to adjust some of the padding around the navbar as well as the margins around navbar-btn buttons and navbar-text text strings.

After getting the site back to its previous functionality, it was time to think about what to actually do this week. Working with the BOM, as I mentioned above, combined with a desire to become more familiar with the Document Object Model (DOM), as well as recent interest in Progressive Web Apps (PWAs) got me thinking about using JavaScript to generate the HTML for the page. This was easily done using the DOM methods createElement(), setAttribute(), createTextNode(), and appendChild(). Using these to create the body of the main page allowed me to use nested loops to create the HTML for the rows and columns of Card components. That saved a lot of typing (or, let’s be real – cutting and pasting) and also allowed me to pass in variables to control how many rows and columns to create. Using the DOM methods to create the navigation bar for the page was a bit more tedious, but it helped solidify my mental picture of the Document model and how all of the HTML elements fit together.

So, this week’s page looks fine, but doesn’t do anything. Maybe that will be next week’s project. Maybe.

Week 8 Project: Spans and Divs

Well, it’s been so long since I started/published this “week’s” project that I don’t rightly remember exactly what I was planning to do or even what I did. The code is a “refinement” of Week 7’s project, but not much has been changed. Because I don’t remember what I was planning to do, and because the code was published to the site, I’ll leave it there as a memorial to my (so far) inconsistent approach to the site.

<sigh>Enjoy! http://dev.kobayashicoding.net, then “Projects” and choose “Week 8.”</sigh>