Programmed a working calendar for journal
Posted by markhwebster on February 12th, 2018 • 0 Comments • Full Article
On February 3 I posted the working example from a calendar lesson I had learned in JavaScript. All it did was generate months of the year with the correct number of days, depending on which month you selected. It was dumb but it got me thinking.
As I moved forward in my JavaScript studies I was mulling over a way to make use of that calendar. To the right of this story you will see two calendars. The top one is a free WordPress plugin. I didn’t build it, I just installed the plugin. It lets you switch between journal entries going back to 2011 when I first started using WordPress. All the post entry links are automatically populated from the WordPress database.
But I was keeping an online journal long before they invented WordPress. I have been writing since late 1997, a year after I got my first computer. There was a story that year in the Seattle times about a woman who was keeping an online journal. She started it in ’97, and it inspired me to do the same. But as my journal grew it was difficult to build a navigation system back through the entries. I’ve struggled with a number of solutions, as you will see if you prowl around.
The one I used the longest was a flash calendar that I built from scratch. But even I refuse to download flash anymore. So if I was on a smartphone, I couldn’t navigate my own journal until just recently.
My solution? Build a real working calendar in Javascript and embed it on every page , or at least the pages starting at September 2001, which is when I changed the interface drastically. To get it to work I had to create a JavaScript array for every year, about 10 of them.
As you can see to the right, I also embedded that new JavaScript calendar right here in WordPress below the plug in calendar. I got it to appear by using a WordPress plugin called shortcoder. The shortcoder plugin presents itself as a box in which you can paste html, css and js. Then it gives you a short little snippet of code, called a shortcode. That shortcode references the longer code. I pasted the shortcode snippet into a standard text widget, and dropped the widget into my WordPress sidebar.
You can see it all working here. It’s not search engine friendly yet…I have to think about that. I’ve linked to the contents (hand built index of the journal) page. It has links out to most of the good writing, perhaps that will negate the fact that a dynamically generated calendar can’t be spidered by google.
I’m fairly happy with this solution. It’s not perfect, I think Jessamyn does a better job of archiving, but at least mine was a fun way to practice my JavaScript.
Here is the array for the year 2000. Note that I didn’t start writing until March of 1999, or, at least that is the first named file: three99.html
/************** begin 1999 ****************/ //makes an array of the names of the html files for the year. var calendar1999 = ['three99', 'four99', 'five99', 'six99', 'seven99','eight99', 'nine99', 'ten99', 'eleven99', 'twelve99']; function createMonths1999(){ //captures the <ul id="joynal1999"> var joynal1999 = document.getElementById('joynal1999');// for(let i = 0; i < calendar1999.length; i++){ //makes virtual <li></li> but doesn't use it in DOM yet var listItem = document.createElement("li"); //makes virtual <a></a> but doesn't use it in DOM yet var anchorTag = document.createElement("a"); /*makes a snippet of text containing a number based on loop iteration first one will be 0+4 = 4 which is April of 99...later found entries in March, and changed it to 3*/ var monthNumber = document.createTextNode(i+3); //<a>4</a>//inserts 4 into virtual anchor tag anchorTag.appendChild(monthNumber); //adds the href property to the virtual element <a href="four99.html">4</a> anchorTag.setAttribute('href', calendar1999[i] + '.html'); // <li><a href="four99.html">4</a></li> listItem.appendChild(anchorTag); //inserts the virtual "li" with clickable month into <ul> dom element so it can be seen joynal1999.appendChild(listItem); }//end month maker for loop }//end createMonths99 function
Next I modified the simpler calendar function from a week ago so that it dynamically generated the months for the selected year, complete with clickable links for each month. But there was a problem. If you selected say, April of 2004, when you got there, the calendar did not display. You had to choose a year again. This was bad usability. I wanted the calendar to always display all the months of the year you were reading, no matter which month you were reading. But that meant getting the pages to talk to each other. The technical term is: passing data between html pages. I had a vague idea that it might be doable with cookies, but I’ve never baked cookies.
Google to the rescue! I discovered there is a way to store and pass variables between pages that have JavaScript. It’s called localStorage. You can write to it, and pull data from it. As long as they are on a long session on your website, you can make your html pages pass data from page to page.
The first line below listens for you to make a choice from the drop list. If you do ‘change’ it, the script fires off the ‘createYear‘ function.
That function puts the year you chose into a variable called ‘choice‘. At the same time, it places that year into session memory via the localStorage.setItem command. This is like registering at a hotel and telling the concierge that you would like the Seattle Times news paper each time you walk in. He remembers your preference during your session at the hotel…all week long.
As you surf from month to month, the concierge (localStorage data) remembers your year preference and keeps that year’s calendar visible
Next I do a couple checks to see whether this is your second journal month choice, or if you just arrived and haven’t made any previous choices. If the latter, I present the first years calendar, which is 1999.
Further down I have a switch-case conditional that fires off the correct createMonths function for the year you selected.
select.addEventListener('change', createYear); function createYear(){ if(select.value) { //does data exist? var choice = select.value; //ie: 2001 localStorage.setItem('storageName',choice);// hold on to year choice like a cookie but in virtual session //empties out all <ul> from <div id="ulHolder"> ulHolder.innerHTML = ''; } else if (localStorage.getItem){// we found from session memory they've chosen a year on a previous page ulHolder.innerHTML = ''; choice = localStorage.getItem('storageName'); } else {// this is only true on first visit to joynal, new session ulHolder.innerHTML = ''; choice = '1999'; } // create ul var makeUl = document.createElement("ul"); // <ul id="joynal1999"></ul> makeUl.setAttribute('id','joynal' + choice); makeUl.setAttribute('class', 'calendar') ulHolder.appendChild(makeUl); /* They drop the select drop list and make a 'choice' of a year. We capture the year in the 'choice' variable. So they 'switched' it and we are examining the variable 'choice' to see what we should do based on it's value */ switch (choice) { //in 'case' 'choice' is = to 1999 case '1999': createMonths1999(); h1.textContent = '1999'; break; case '2000': createMonths2000(); h1.textContent = '2000'; break; case '2001': createMonths2001(); h1.textContent = '2001'; break; case '2002': createMonths2002(); h1.textContent = '2002'; break; case '2003': createMonths2003(); h1.textContent = '2003'; break; case '2004': createMonths2004(); h1.textContent = '2004'; break; case '2005': createMonths2005(); h1.textContent = '2005'; break; case '2006': createMonths2006(); h1.textContent = '2006'; break; case '2007': createMonths2007(); h1.textContent = '2007'; break; case '2008': createMonths2008(); h1.textContent = '2008'; break; case '2009': createMonths2009(); h1.textContent = '2009'; break; case '2010': createMonths2010(); h1.textContent = '2010'; break; }//end switch(choice) case }//end function createYear()
In other news, Sue and I skied at Paradise yesterday. This was only her second trip of the year. She is still babying her rotator cuff surgery. It was a nice one inch covering of powder over a hard base. It wasn’t grabby, and was lovely skiing, not counting the water runnels, which were as bad as moguls in spots. I went back up for another run, but the fog had settled in, masking the earlier bright blue sky. I turned around before Pan, and finished out the day.
Today I bought a $15 bouquet but discovered I’ve forgotten how to draw. Jeez! I’ve been so lost in programming I’ve not only forgotten how to climb, I can’t draw either. How come I can’t be good at everything? It would be so handy. Perhaps when I get a full time job again and get settled in I can get back to my hobbies. I miss them.
To keep from going crazy with all this studying I’ve been exercising every day and playing a fair amount of ping pong. Last week I played at the local community center with a bunch of seniors. We played doubles, which involves a very intricate dance around the end of the table. You have to hit the ball, but only every other time. This means you hit the ball, then run backwards to get out of the way so your partner can hit it…and repeat. It was so much fun I went to another public game out on 76th.
This was a beast of a different color. They were people of all ages, and they were serious, with a capital S! They literally wiped the floor with me. I lost.every.single.game. That’s nine games in a row, but who is counting? I walked out after an hour. I mean, what was the point? I love the game, but I don’t have the time to develop that kind of skill. I am already neglecting too many hobbies, don’t need to add ping pong to the list. I won’t stop playing, but I will avoid playing with people who live and breath the game.