November 5, 2009

jQuery Primer – An Intro to Client-side Functionality

If you’re interested in web development, you’ve doubtlessly come across jQuery and wondered about how you could utilize it in your own projects. jQuery looks a little daunting at first, but hopefully by the end of this little introduction to jQuery you’ll have a much firmer grasp on it and be able to take your projects to the next level.

What is it?
jquery1If you go by their website jQuery touts itself as being able to do damn near everything. And in truth, it does. It simplifies otherwise tedious tasks and allows you to add huge amounts of functionality to your website without much code. What it IS, however, is a Framework that provides you the ability to perform these tedious tasks in a way that’s simple and concise while achieving cross-browser support. This means that you can write your code quickly while knowing that it will work exactly how you expect it to in every browser. This cuts down on the amount of development time that you spend. Another bonus is the way that jQuery naming conventions make sense. Things like adding onclick handlers are as simple as adding .click(); to a selector (more on this later). This means that the learning curve is very shallow allowing you to literally just jump right now.

Getting started
Your first step will be to download jQuery. To do that, just head over to their website (http://www.jquery.com) and you can download it. It will be up to you whether you want to download the production or development versions. Both are the same, it’s just that the production code has been minified so it results in a much smaller file size. If you are just starting out however, I’d recommend going with the development version. If you have a full fledged IDE then your code hinting will come into play and you’ll be able to see descriptions of what the arguments are and such. However, even if you don’t, the development code will definitely be something you’ll want to peruse through in your spare time. You can learn a lot from looking at other peoples code. Once you have it downloaded, you can use it by simply including the following:

<script type="text/javascript" src="path/to/jquery.js"></script>

I’ve intentionally left the “src” attribute pointing to a random file as you will have to change it to point to wherever your jQuery file is.

First Steps
When using jQuery you have to remember that you’ll be spending a lot of time manipulating the DOM. In order for this to occur, you’ll have to wait till the DOM is loaded. However, because of where your script files are generally located, they may load before the DOM has completely finished. If this happens then your scripts will just run in to a lot of errors. jQuery comes with a built in way to check that the DOM is ready for use. All your jQuery scripts should run only AFTER the DOM is ready. In a separate script tag, you can add the following

$(document).ready(function(){ })

jQuery makes use of anonymous functions quite a bit, so you may want to make a note of that. You don’t NEED to have a function name in JavaScript. $ is a global variable created by jQuery as an accessor to all of its methods. This means that all your scripts in jQuery will utilize $ to perform it’s functions. $ will accept multiple types of arguments. You can either provide an ID, a Class or even an html tag for jQuery to work its magic on. I recommend you read up on the documentation for $ as it is one thing that you will definitely need to understand to perform complex manipulations.

You can go ahead and add

<a href="#">Link</a>

to the body section of your code. This is so that we have something to work with in jQuery.

Going back to our anonymous function, you can change it so that it looks like this

$(document).ready(function(){
$('a').click(function(){ alert('Works!'); }); 
}

Because of how powerful jQuery is, that will actually apply itself to EVERY anchor tag in the DOM. You can pick and choose which ones by applying numerous selectors. There are so many involved that that itself is the topic of another discussion. However, if you are looking to work on a particular id the code would be

$('#id')

and if you were working on a class you could use

$('.class')

Keep in mind that jQuery will always work on EVERYTHING that matches the selector that you have provided.

jQuery is also chainable. Because of how the code is written, you can keep tacking on different functions. For example, if we look through the effects section of the documentation you’ll see things like slideDown() and fadeIn(). These effects can be chained together to create multiple tiers of animations or events.

My recommendation is that you definitely check out the full documentation and spend a lot of time playing around with it. There will be a few more jQuery tutorials popping up on the website as I start preparing the gradual jump from no jQuery experience to enough experience to write the client-side functionality of our SWIM!

November 4, 2009

Windows Keyboard Shortcuts

I just set up the wireless keyboard that I bought about a year ago and things are off to a great start. I haven’t had  any problems with it what-so-ever. No sudden signal drops, I love the clicky sound that it makes and I love having access to a full number pad, it makes number crunching much easier. However, since I keep the keyboard on my lap I find the few seconds that it takes me to move my had to my mouse (which is on my desk) is a bit of a hassle. I’ve been trying to find shortcuts for everything and here are a few of my favourites:

WIN+E
This shortcut allows you to pop open a new explorer window pointed at My Computer. I haven’t looked into if there was any way of changing the default directory, but even so being able to open a window like this is pretty nifty.

F2
If you select a file and press F2 you’ll instantly start renaming the file. It even highlights the filename and leaves the extension alone so that you don’t accidentally mess anything up.

ESC
The ultimate cancel. If you ever want to stop doing something, just tap the escape button. It works like instant cancel is most applications, and if you use it after F2, you’ll rename the file back to its original name and stop selecting it

Enter
I don’t think I need to explain this one. Enter acts like a universal accept button.

WIN
This key will pop open the start menu regardless of what application or game you’re in. A constant source of frustration for PC gamers who accidentally hit it during their gaming sessions.

WIN+R
This will pop open a little Run.. window which will let you enter different commands or utilities that you can launch. You can even specify a path to a directory and when you hit enter you’ll automatically launch windows explorer pointed at that directory.

WIN+D
Will toggle the “Show Desktop” button and take you to your desktop. Pressing it again will restore all the windows you had open

Alt+Tab
This is one that most people know. Alt+Tab lets you shift through all open windows.

Alt+Shift+Tab
This one you’re probably not all that familiar with. Instead of shifting forwards through tabs, using the Shift modifier you’ll now move backwards. Great for going back to a window after you passed it with Alt+Tab

Tab
Tab acts as either a “next” button or a larger version of the space button.

Alt
If you have a window open Alt will highlight the keyboard functions that you can perform. For example pressing alt will generally change your focus from whatever you’re working on to the menus in the window itself.

CTRL+Arrow keys
Skip a word in the direction you want

Shift+Arrow keys
Will highlight letters depending on which arrow key you hit

Shift+CTRL+Arrow keys
Will highlight whole words depending on which arrow key you hit

CTRL+A
Highlight the whole document

CTRL+C
Copy whatever you have highlighted and place it on your clipboard

CTRL+X
This will cut whatever you have highlighted from the document and place it on your clipboard

CTRL+V
This will paste whatever is on your clipboard.

There are probably a lot more commands out there for windows users, but to be honest, I haven’t had much time to find them all. If you think of something I missed, definitely post it in the comments and I’ll add it.

November 3, 2009

Simple Website Instant Messenger – The Static Prototype

For the past day I have been laying the ground-work for a little side-project of mine. My eventual goal is to offer the application for free, but in the mean time I thought I’d go through a bit of a tutorial outlining how I came up with the prototype for SWIM. This probably won’t be a project that you will want to include on a large website as the constant back-end polls will probably take their toll on your server. However, as an informative piece outlining complex application development with a multitude of programming languages SWIM will be right at home.

The Vision
I wanted to create a completely self contained Website IM system. Apart from an installation file that would handle the database setup, I wanted developers to be able to copy a few lines of code into their current websites and have the client up and running. It would consist of a little bar along the bottom of the screen that would allow the user to easily keep track of their contacts as well as any open conversations. From the beginning the idea was the integration with a website had to be simple. If I ever felt the need to include it in a website I was developing, I didn’t want the hassle of actually integrating it. I wanted to be able to say something like swim_ui(); and have it take care of everything else. I knew it wouldn’t be THAT simple, but simplicity was something I was aiming for. Here are a few examples of what I expected the app to look like.

improto1

Refining
Once I had established a rough look to the IM client I took a moment to re-evaluate the different elements involved. It is something, that as a designer, I find essential to do. I had to make absolutely sure that every element in the prototype was required 100%. If it could do without, it was useless and removed. The reason was simple. I didn’t want a massive file included with every page. It would become a problem as websites grew.  Even the use of images was something I didn’t want to get in to. As I said before, my motto with this was “If it could do without, it was useless” and there was no room for useless elements.

improto2

The Static Mockup
This is another step that I absolutely have to go through. Before I start adding the dynamic PHP and even Javascript, I build a completely static version of what I want to eventually build. For one thing it lets me re-evaluate everything in the design, and when deadlines start coming up, it’s easy to find things that will give you problems later on. As well, actually having a finished product really boosts your morale and makes you want to work more. The static mockup was very simple. Even though I eliminated the chat taskbar, you’ll notice (or maybe not, since the screenshot is rather hard to read) I left the chat taskbar text in place. This is because that even though we won’t have a physical task bar, it will still exist in our code. This lets us position things properly.

<div class="im-bar">
	<span class="task" id="'user-id'">
		<div class="convo">
			<img src="/icon/cancel.png" id="close-win" alt="Cancel">
			<span class="system">You have entered a conversation with Name</span>
			<textarea name="message" id="message"></textarea>
		</div>
		<div class="win" name="Name">Name</div>
	</span>
	<div class="more">
		<div class="user-info">
			Angelo R. 
			<span><img src="/icon/bullet_green.png" alt="Online" id="status"></span>
		</div>
		<div class="contacts">
			<span id="cname1" name="Name">
				<img src="/icon/user.png" id="cname-dp" alt="Username">
				<ul>
					<li class="name">Name</li>
					<li class="status">Status</li>
				</ul>
			</span>
			<span id="cname2" name="Name 2">
				<img src="/icon/user_gray.png" id="cname-dp" alt="Username">
				<ul>
					<li class="name">Name 2</li>
					<li class="status">Status</li>
				</ul>
			</span>
			<span id="cname3" name="Name 3">
				<img src="/icon/user_gray.png" id="cname-dp" alt="Username">
				<ul>
					<li class="name">Name 3</li>
					<li class="status">Status</li>
				</ul>
			</span>
		</div>
		<div class="bottom-bar">
			<img src="/icon/add.png" id="add" alt="Add User">
			<a href="#">Settings</a>
		</div>
	</div>
	<div class="info">
		<img src="/icon/bullet_green.png" alt="Online" align="top">Online 
		<span><img src="/icon/bullet_arrow_up.png" alt="More"></span>
	</div>
</div>

As you can see I’ve tried to keep the code as self explanatory as possible. It’s very simple HTML without the use of Tables or any inline CSS. This was to ensure that even skinning the SWIM system would be as simple as editing a single file.

The CSS for this file is where things will get a lot more confusing. Because of the fact that this will be included in your website, I had to ensure that my css wouldn’t overwrite any of your css. So everything is explicitly stated. When I wanted to style the .info class, I pointed to it through it’s full “path” of .im-bar .info This was to ensure that only that element would get skinned. The CSS is a little advanced, and has an added line for webkit based browers that the gecko ones won’t see. This is also only going to look exactly as planned in those browsers. Sorry Trident users, I’ll be updating this code once I sort out a few more bugs.

.im-bar{
	position: absolute; 
	right: 20px; 
	bottom: 10px; 
	z-index: 100;
	font-family: "Arial", sans-serif;
	font-size: 0.8em;
	color: #555;
}	
.im-bar a{
	color: #333;
	font-weight: bold; 
	text-decoration: none;
}
	.im-bar a:hover{
	text-decoration: underline;
}
.im-bar .info, .im-bar .win{
	display: inline;
	-webkit-border-radius: 7px; 
	-moz-border-radius: 7px;
	border: 1px solid #EFEFEF;
	width: 100px;
	padding: 5px 10px 5px 5px;
	background-color: #F0F0F0;
	margin: 0px 6px;
	cursor: default;
	-webkit-box-shadow: 0px 0px 4px rgba(0,0,0,0.9);
}
.im-bar .win{
	width: reset;
}	
.im-bar .info span{
	float: right;
}
.im-bar .info:hover, .im-bar .win:hover{
	background-color: #E7E7E7;
}
.im-bar .more{
	display: none; 
	border: 1px solid #EFEFEF;
	width: 200px;
	height: 350px;
	position: absolute; 
	right: 0px;
	bottom: 30px;
	background-color: #F0F0F0;
	-webkit-border-radius: 7px;
	-moz-border-radius: 7px;
	padding: 10px;
	padding: 0px;
	z-index: 100;
	-webkit-box-shadow: 0px 0px 4px rgba(0,0,0,0.9);
}
.im-bar .more .contacts{
	background-color: #FFF;
	height: 86%;
	width: 100%;
}
.im-bar .more .contacts span{
	display: block; 
	margin: 0px;
	border-style: solid;
	border-width: 0px 0px 1px 0px;
	border-color: #F0F0F0;
	text-align: left;
}
.im-bar .more .contacts span:hover{
	background-color: #FAFAFA;
}
.im-bar .more .contacts span img{
	float: right;
	padding: 3px; 
	width: 40px; 
	height: 40px;
	border: 1px solid #F0F0F0;
	margin: 2px;
}
.im-bar .more .contacts span ul{
	padding: 10px;
	margin: 0px;
}
.im-bar .more .contacts span ul li{
	list-style: none;
	cursor: default;
}
.im-bar .more .contacts span ul li.status{
	color: #CCC; 
	font-style: italic;
}
.im-bar .more .user-info,.im-bar .more .bottom-bar{
	padding: 4px;
}
.im-bar .more .user-info span,.im-bar .more .bottom-bar a{
	float: right;
}
.im-bar .task .convo{
	display: none; 
	width: 250px;
	height: 300px;
	position: absolute; 
	bottom: 30px; 
	right: 0px;
	display: block;
	-webkit-border-radius: 7px; 
	-moz-border-radius: 7px;
	border: 2px solid #EFEFEF;
	background-color: #FFF;
	padding: 10px;
	-webkit-box-shadow: 0px 0px 4px rgba(0,0,0,0.9);
}
.im-bar .task .convo #close-win{
	position: relative; 
	bottom: 20px; 
	left: 250px;
	cursor: pointer;
}
.im-bar .task .convo #message{
	width: 100%;
	font-family: "Arial", sans-serif;
	font-size: 1em;
	color: #555;
	height: 60px;
}
.im-bar .system{
	color: #CCC; 
	font-style: italic;
	height: 75%;
	display: block;
}
.im-bar .task .convo .text{
	font-style: normal;
	height: 75%;
	display: block;
	overflow: auto;
}
.im-bar .task .convo .text p{
	padding: 0px;
	margin: 2px;
}

You’ll notice that everything in the stylesheet is actually really simple. Other than a few pieces such as the overflow attribute in the .im-bar .task .convo .text selector which just adds a scrollbar to the chat window if necessary. The z-indexes are also used to place windows on top of each other (obviously) but keep things in the high 90′s. This is so that incase you have an z-indexed elements on your website, this will hopefully not interfere.

Finally, here’s a screenshot of what our HTML mockup looks like. Stay tuned for the next part in which we add jQuery into the mix to really get our Prototype up and running!

SWIM - Minimized Convo Windows with Open Chat Window

SWIM - Minimized Convo Windows with Open Chat Window

SWIM - Open conversation window

SWIM - Open conversation window