As you may have noticed, The True Tribe has been on a bit of a hiatus for articles over the past couple of weeks. This is because we have been devoting all of our effort into developing an entirely new site, built with Joomla rather than our current implementation on Blogger's platform.
The new site will be more than just a web development blog-- it aims to provide resources such as articles, tutorials/walkthroughs, messageboards and community features.
After much deliberation, we decided on Joomla as the framework for the back end. We will be using MooTools for the front end.
Right now we are finalizing design, and simultaneously setting everything up in the back end. I estimate the new site will launch in August or September. In the meantime, check out our backlog of existing articles or peruse some of the links in the sidebar.
Synergy is a great app that allows you to control your Linux and Windows computers via a single keyboard and mouse. You can plug in the keyboard/mouse to either Linux or Windows machines and fluidly switch between computers just as easily as you would switch between monitors in a dual-monitor set up.
Here is a description and image from the Synergy site which demonstrates this functionality:
In this example, the user is moving the mouse from left to right. When the cursor reaches the right edge of the left screen it jumps instantly to the left edge of the right screen.
You can arrange screens side-by-side, above and below one another, or any combination. You can even have a screen jump to the opposite edge of itself. Synergy also understands multiple screens attached to the same computer.
I've been using it for a couple of years now and I find it an invaluable addition to the arsenal of any developer who works in both Windows and Linux. I built a new computer over the weekend and finished installing Ubuntu 8.08 Gutsy Gibbon tonight. One of the first things I did was install Synergy. Previously, I set it up on Red Hat Enterprise, but either way, the set up is pretty straightforward.
Let's get started installing Synergy, shall we? The Windows set up is easier because it has a GUI. In Windows, go to the Synergy SourceForge page to download the latest release (version 1.3.1 as of this writing). Here's the download link if you don't want to be bothered with navigating the SourceForge page:
Once it's downloaded, install it and open the synergy.exe file to launch the program.
I prefer to set Windows as the client and Linux as the slave, although it was recommended in a forum post to do the opposite in Windows Vista. Regardless, I haven't had issues with Vista or XP in either configuration.
For now, let's assume your keyboard and mouse is hooked into the Linux box and you want to make Windows the slave (client). To do this, simply click the radio button next to Use another computer's shared keyboard and mouse (client). Then, enter the Linux computer's host name next to Other Computer's Host Name. For me, this is 'ubuntu' but you may have been more creative with your host name.
We're not quite ready to test it yet, so leave this window open and go back to Linux for a moment. In Ubuntu, install Synergy by typing:
sudo apt-get install synergy
If you prefer doing things in the console, then you can manually create the config files. Otherwise, you can use a GUI called QuickSynergy to get up and running. Here's how to do either way:
The manual way might take a little longer, or not, depending on how good you are with command line interfaces. To install it manually, first you must create a configuration file called synergy.conf that looks like this:
section: screens
screen1:
screen2:
end
section: links
screen1:
right = screen2screen2:
left = screen1
end
You can place the config file in /etc/ or /usr/local/etc/ (whichever you prefer). Just make sure it is somewhere in the environment PATH for convenience's sake.
For me, this file says:
section: screens
ubuntu:
laptop:
end
section: links
screen1:
right = laptop
screen2:
left = ubuntu
end
My laptop is, of course, named 'laptop' -- again, feel free to use more imaginative names (as long as it is actually the name of the computer). It isn't necessary to use the name of the computer as the name of the screen but it requires extra configuration otherwise. (See the official documentation for more information on this).
Now that you've created the file, you're ready to launch Synergy. Give it a try with this line:
synergys -f --config synergy.conf
Assuming it starts correctly, jump back to Windows and click 'Test' in Synergy there. It should say that it connected OK and everything is fine and dandy. If that's the case, then just click 'Start' and you're done. If not, visit the docs page and scroll towards the bottom to troubleshoot the issue.
If you prefer graphically configuring Synergy, that's an option, too. I guess I should have put the GUI solution first for us lazy developers, but it's good to be familiar with the non-graphical way anyway. I know that I managed to mess up my config file using the GUI and had to dive in to it regardless.
That being said, the GUI (called QuickSynergy) is very straightforward and easy to use. To install QuickSynergy, type the following in a Linux terminal:
sudo apt-get install quicksynergy
Once it's installed, you can type quicksynergy to launch it (add & at the end if you want to retain your terminal window) and a window will popup that allows you to configure Synergy, either as host or client.
The default tab which opens is for host, and all you have to do is enter the correct screen names and then start it. For me, my Linux desktop (named 'ubuntu') is on the left and my Vista laptop (named 'laptop') is on the right, so I just made sure that the fields to the left and right of the computer image said 'ubuntu' and 'laptop', respectively.
QuickSynergy is theoretically easier to use than the command line way detailed above, so I won't go into much depth here. If you get stuck with QuickSynergy, either try the command line way, or visit the QuickSynergy SourceForge site to see screenshots and example configurations.
Extra Features:
Synergy has the ability to auto-sync starting and stopping of screensavers
You can copy and paste between Linux and Windows (this is a huge time-saver!)
Easily lock the mouse/keyboard to the current screen by toggling scroll lock (assignable to any other key)
I'm happy to announce that MooTools (Wikipedia link) has released version 1.2 of their excellent JavaScript library. MooTools, which stands for 'My Object Oriented Tools', was developed in 2006 by Valerio Proietti and his colleagues. It evolved out of Moo.fx, a lightweight effects library which plugged into the Prototype framework. It was similar, although smaller (and in my opinion, better) than the scriptaculous library. Moo.fx has now been fully integrated into the MooTools library and is not being developed further at this time.
Even before MooTools' 1.0 release on January 29, 2007, it had garnered quite a bit of buzz. There were even cheat sheets created for the beta MooTools library.
Thus, it is with great excitement that I announce a new version of this marvelous framework, with a great deal of improvements and additions to the codebase. I've been using 1.2 beta for quite a while and I think the official release is mostly a bug fix of the beta, so if you've been following this blog, chances are you've already been exposed to some of the new features in 1.2. Regardless, here's a full list of features and enhancements you'll find in the new release:
Swiff, support for working with Flash SWF files, similar to the swfobject library
Element storage allows you to store data in custom properties on HTML elements without leaking memory in IE
Overhaul of Fx classes with many improvements, including creating a Tween class to create reusable animation tweens
Overhauled Ajax requests; renamed Ajax class to Request , with JSON and HTML subclasses (for easily handling their respective data formats as Ajax responses)
Element.Dimensions - makes it a breeze to get width, height, x/y coordinates of an element (either relative to document or to positioning context) and scroll height/width
Created a Browser class to store browser, platform and feature information (e.g. whether the browser supports XPath or not). Before, browser info was stored on the window object. Also, this release renamed the properties from browser names to rendering engine names, e.g. trident4 instead of ie6.
In addition to the changes to the API and codebase, the following changes occured as well:
The Hash Object - with get, put, each, some and a whole lot of other methods for manipulating data in a hash
MooTools developed using Git instead of Subversion now - this will only affect you if you're used to grabbing code from svn (or if you're a contributor!)
MooTools uses Lighthouse instead of Trac for bug tracking now
In light of all these improvements to an already excellent library, I think it's apparent that MooTools is really growing up and coming into its own. It's a force to be reckoned with and certainly a heavyweight contender against Prototype, jQuery, YUI and others.
I hope you've enjoyed this brief overview of some of the new features in MooTools 1.2. Now get out there and start coding!
See a demo at http://www.alexgrande.com
It is the images on the top right.
I wrote this in Object Oriented format so you can use it again again on page.
Javascript:
var ImageGallery = new Function();
ImageGallery.prototype = {
initialize: function(mainImage, listWrapper){
// large image that is shown
this.mainImage = document.getElementById(mainImage);
// a list of all the anchors for the thumbnails. Must be a tags for graceful degradation
this.thumbnails = document.getElementById(listWrapper).getElementsByTagName("a");
// 0 image is shown already so the first one we want to switch to is the next image or 1
this.i = 1;
// this is a work around to allow calling this within nested functions
var Scope = this;
// Here starts the rotating of the image by first focusing the thumbnail, then switch the primary image
this.start = setInterval(function(){
Scope.focusCall();
// Here we choose 5 seconds in between each image change. You may want to change this.
}, 5000);
// This lets the browser know to do something if one of the thumbnails is clicked
this.clickEvent();
},
// This stops the rotation of the thumbnails. We do that if the user clicks one of them
stop: function(){
if (this.start)
clearInterval(this.start);
},
// When an thumbnail loses focus we must use this css class now
resetBorderColor: function(reset){
reset.getElementsByTagName("img")[0].className="thumbnailDefault";
},
// When the thumbnail gains focus we most give it the corresponding styles
focusBorderColor: function(focused){
focused.getElementsByTagName("img")[0].className="thumbnailFocus";
},
// Here we grab the href of the a tags and make their path be the path of the current image
imageRotator: function(){
this.mainImage.src = this.thumbnails[this.i].href;
this.previousImage = this.thumbnails[this.i];
this.i++;
// This closes the loop for the rotation
if (this.i == this.thumbnails.length)
this.i = 0;
},
// We focus the thumbnail
focusCall: function(){
// reset the last image that was shown
if (typeof this.previousImage != 'undefined')
this.resetBorderColor(this.previousImage);
// Remember the newer one
this.currentImage = this.thumbnails[this.i];
// Give the newer image some focus
this.focusBorderColor(this.currentImage);
var Scope = this;
// Les that have image rotate to the new one 300 miliseconds after the thumbnails get the css focus
window.setTimeout(function(){
Scope.imageRotator()
// You may want to change this number
}, 300);
},
// This is what happens when you click the thumbnails
clickEvent: function(){
var Scope = this;
for (k = 0; k < this.thumbnails.length; k++) {
this.thumbnails[k].onclick = function(){
if (typeof Scope.previousImage != 'undefined')
Scope.resetBorderColor(Scope.previousImage);
Scope.focusBorderColor(this);
// Stop the rotation
Scope.stop();
// This is where the switching happens for the click
Scope.mainImage.src = this.href;
Scope.previousImage = this;
// Make sure to not allow default behavior of the a tag
return false;
}
}
}
}
This is to load it on the window object and create an instance of the viewer
var imageGallery1 = new ImageGallery();
// Not sure where I got this.. I didn't write this but it allows you to load multiple functions on the window.onload.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != "function") {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
var onLoad = function() {
imageGallery1.initialize("index_largepic_display", "index_thumbnail_display");
}
addLoadEvent(onLoad);
For the version on my homepage alexgrande.com here is the CSS and HTML. The CSS is up to you but I suggest following a similar mode with the HTML
HTML:
<div id="gallery">
<ul id="index_thumbnail_display">
<li>
<a href="images/index/fernlarge.jpg" >
<img class="thumbnailDefault" src="images/index/fernthumb.jpg" alt="A picture of a fern just as it is unraveling in front of a log." />
</a>
</li>
<li>
<a id="partythumb" href="images/index/partylarge.jpg" >
<img class="thumbnailDefault" src="images/index/partythumb.jpg" alt="Downtown Seattle at night." />
</a>
</li>
<li>
<a id="alexthumb" href="images/index/alexlarge.jpg" >
<img class="thumbnailDefault" src="images/index/alexthumb.jpg" alt="I'm on a laptop at night in a field on using the internet via hacking a telephone box...legally." />
</a>
</li>
<li>
<a href="images/index/trucks.jpg" >
<img class="thumbnailDefault" alt="Trucks lined up in Sodo in Seattle at night." src="images/index/trucksthumb.jpg" />
</a>
</li>
</ul>
<img id="index_largepic_display" src="images/index/fernlarge.jpg" alt="A picture of a fern just as it is unraveling in front of a log." />
</div>
In this era of rich JavaScript applications, so much
focus is given to the features of the application that one
crucial element is often overlooked: What happens when the
user leaves the page? We take it for granted that pages
will look the same when we leave and return, but a new
question merges for sites using rich JavaScript
interaction: If the user leaves and returns to the page,
will the application state be preserved?
The effects of losing application state can range from
minor annoyances like losing what page you're on, to
all-out frustration after losing a carefully-typed message
because you accidentally triggered the browser's back
button. (It's easier than you think-- hitting backspace
when the document is in focus triggers the back button in
most browsers). Couple this with the fact that some users
may expect pages to save form data, because of their prior
experience to that effect, and it becomes apparent that a
robust strategy for preserving application state must be
devised.
Browsers automatically save data entered into form
fields, but all JavaScript variables are lost when the user
leaves the page. Furthermore, any form fields that were
created by JavaScript will also be lost. So, for all but
the most simple applications, JavaScript must have a
strategy for saving state that deals with these
limitations.
Some sites like thesixtyone.com
reside entirely on a single page and capture users' back
button clicks with named anchors. But, try writing a wall
post on Facebook and you'll find that it does not save the
post if you leave the page. Accidentally pressing backspace
is all too easy in cases like this where typing is
involved, which is why sites like Gmail and Blogger warn
users that they will lose data before leaving the page.
How To Warn Users Before Leaving the Page
One way you can do this is by assigning a confirmation
message to the return value of the
window.onbeforeunload event handler. The user will
be presented with two choices, OK and
Cancel, along with a custom message of your
choosing.
In the following example, we regsiter an anonymous
function as the event handler for
window.onbeforeunload, and add our own custom
message:
Using window.onbeforeunload to confirm if a
user wants to leave the page (example 1)
window.onbeforeunload = function() {
return "You will lose any unsaved information.";
};
The browser displays your custom message, given in the
return statement of the onbeforeunload event
handler, along with the browser default message. In
Firefox, the result is:
Are you sure you want to navigate away from this
page?
You will lose any unsaved information.
Press OK to continue, or Cancel to stay on the current
page.
Retaining Data When Users Do Leave the Page
You may opt to silently save the user's data when they
leave the page. This may give a better user experience
since they are not confronted with a choice, and their data
is saved automatically.
This is one of the times where Ajax comes in handy.
However, there are also other ways to do this without using
Ajax, such as cleverly storing information in named anchors
or hidden form fields. We'll examine each of these
practices in more depth, but suffice it to say that the
hidden form fields approach works better for conventional
websites that are spread across many pages, whereas storing
data in named anchors is better for single-page, pure
JavaScript applications.
It turns out that while you could (and should) save
state to the server using Ajax, for some cases you will
want to avoid Ajax altogether and use a simpler,
clientside-only model.
Using Hidden Form Fields to Save State
As mentioned, all JavaScript objects are lost when the
user leaves or refreshes the page. But, browsers will
retain data in form fields, provided that the form elements
were not generated using JavaScript. Given this limitation,
it is necessary to save JavaScript variables (or the
serialized JSON strings of such objects) to hidden form
fields if they need to be retained.
Here is a basic example showing how variables can be
stored to hidden form fields and restored on page load:
// The variable userData is some necessary information we need from the user.
// The first time the user visits the page, they must enter this data manually.
// But, when leaving and returning to the page (or refreshing the page), we'll check
// if they already entered the data, and if so, restore it from a hidden form field.
var userData;
// Register event handlers
window.onload = function() {
restoreState();
if (!userData) {
userData = prompt('Please enter the data to save', 'test');
}
document.write("userData: " + userData);
}
window.onbeforeunload = saveState;
// This function is called onbeforeunload and writes the userData to the hidden form field
function saveState() {
document.getElementById('saved-data').value = userData;
}
// This function is called onload and checks if any data is present in the hidden form field
// If so, it defines userData to be the saved data
function restoreState() {
var savedData = document.getElementById('saved-data').value;
if (savedData != "") {
userData = savedData;
}
}
In the above example, all we're saving is one string
from the user. But what about cases where we need to save
many different values? For instance, what if we're using
object-oriented code and have numerous nested objects
within objects we need to store? At times like this,
serializing objects with JSON is the
easiest way to store the data. Without using JSON, you'd
have to create a hidden form field for each value you want
to save, whereas JSON can create string representations of
complex data structures that you can easily eval
back into JavaScript objects once they're fetched from the
DOM.
So What is JSON, Anyway?
JSON (pronounced "Jason"), short for JavaScript
Object Notation is a lightweight, human- and
machine-readable way to represent the string serializations
of objects. These strings can be evaluated back into
JavaScript objects as needed. For instance, say I create a
JavaScript object to represent a person (in this case,
me):
var person = new Object();
person.name = "Jonah";
person.age = 24;
person.gender = "male";
person.location = "Seattle, WA";
The JSON representation of this object is as
follows:
If you've written JavaScript using object literal syntax
before, this should be familiar to you. The only minor
difference between JSON and the standard JavaScript object
literal syntax is that JSON requires quotes around key in a
key/value pair. So, name is a valid JavaScript key
but in JSON it would have to be 'name'. (Note: It
doesn't matter if you use single- or double-quotes, as long
as they are matched).
Stringifying Objects in JSON
To use JSON, it's necessary to include a library of JSON
methods. Don't worry, the library is quite small. The
entire thing shouldn't be more than 2k and can be obtained
from json.org.
Eventually, the JSON methods will be included as part of
the core JavaScript language, but for the time being, we're
left to use the methods provided by json.org or those found
in libraries such as MooTools, Prototype and jQuery.
Depending on the library used, the method names for
serializing an object into a JSON string are different.
But, they are all used in rather similar fashion. For now,
we'll assume you're using the library from json.org and use the
method names provided in its API.
Saving complex JavaScript data structures as JSON
strings (example 3)
// The variable userData is some necessary information we need from the user.
// The first time the user visits the page, they must enter this data manually.
// But, when leaving and returning to the page (or refreshing the page), we'll check
// if they already entered the data, and if so, restore it from a hidden form field.
var userData;
// Register event handlers
window.onload = function() {
restoreState();
if (!userData) {
userData = new Object();
userData.name = prompt('Please enter a name', 'Jonah');
userData.age = parseInt(prompt('Please enter an age', '24'));
userData.gender = prompt('Please enter a gender', 'male');
userData.location = prompt('Please enter a location', 'Seattle, WA');
}
displayData(userData);
}
window.onbeforeunload = saveState;
// This function is called onbeforeunload and writes the userData to the hidden form field
function saveState() {
document.getElementById('saved-data').value = JSON.stringify(userData);
}
// This function is called onload and checks if any data is present in the hidden form field
// If so, it defines userData to be the saved data
function restoreState() {
var savedData = document.getElementById('saved-data').value;
if (savedData != "") {
userData = eval( '(' + savedData + ')' );
}
}
// This is a helper function that iterates through each property in an object and renders it in HTML.
function displayData(obj) {
var list = document.createElement('ul');
for (var property in obj) {
var text = document.createTextNode(property + ': ' + obj[property])
var line = document.createElement('li');
line.appendChild(text);
list.appendChild(line);
}
document.getElementsByTagName('body')[0].appendChild(list);
}
This example is pretty similar to the previous one where
we saved a string. The only difference is that in this
case, the string is a representation of a complex
JavaScript object. In fact, you can save the entire state
of your application in one JSON string, as long as the
application state is completely stored as properties of a
single object. There are a few minor gotchas, such as
having to add parentheses around the JSON string when
evaluating it. But, overall this is a clean and
straightforward approach that is very useful when complex
data structures must be retained.
Using Named Anchors to Save State
An alternate option for retaining state is to not
actually let the user leave the page at all. Rather, when
following links on the site, update the named anchor
(everything after the number sign in a URL), instead of
changing the actual document being displayed.
The problem that this is trying to solve is the fact
that Ajax applications will normally break the back button.
A user loads the application on the homepage and clicks to
visit a different page, but since the new page is loaded in
via Ajax, the browser URL doesn't change. Then the user
clicks the back button and leaves the application
altogether-- not the intention of the user, who just wanted
to get back to the homepage.
Storing data in named anchors offers a solution to this
problem. Each time the application state changes,
JavaScript updates the named anchor with a token
representing the application state. When the page is
loaded, data is read from the named anchors and the state
can be restored.
Say you're on the homepage of an ecommerce Ajax
application and click on a product you'd like to view.
Instead of changing URLs to the detail page, the
application loads in new data with Ajax. So, when a user
clicks on the new Brad Mehldau CD for instance, instead of
going to a different URL (yoursite.com/brad-mehldau/) the
document URL remains the same, but JavaScript updates the
named anchor: yoursite.com/#brad-mehldau.
One site which does this unbelievably well is thesixtyone.com
(Thanks, Derek!). The entire site resides in one document,
truly a rich JavaScript application if I've ever seen one.
But, despite the fact that the entire application is
contained in a single document URL, due to clever use of
named anchors, the site has full back button support and
you can even email working links to friends.
Implementing code to save state in named anchors is out
of scope for this article, but you can see how it is
somewhat similar to saving data in hidden form fields. In
this case, there are a few more issues to mitigate and it's
somewhat tricky, but the reward is an Ajax site with fully
functional back button support and the ability to share
links -- worth all the effort, in my book.
So What Use is Ajax, Then?
Since we've made it this far, you might think that there
is no use for Ajax in all this. Actually, Ajax is great for
saving state to the server, especially for saving data
beyond the lifespan of the browser session. Ajax can be
used to save messages periodically (like how Gmail and
Google Docs automatically save on a timer every few
minutes). It can also be used to send data when the user
leaves the page by capturing the onbeforeunload
event, but this is unreliable and I would not depend on
this Ajax request to complete. Instead, try to save the
data before the user attempts to leave the page, by either
firing the Ajax request on a timer or another event on the
page (leaving focus on a form element, for example).
Some frameworks like Prototype have serialize() methods
that return URL query string representations of objects.
This is perfect for saving data through GET requests. Yes,
GET requests have a 2000-character limit and other
limitations, but in most cases this won't be an issue. Even
without helper methods to serialize objects, it's a fairly
simple matter to construct an Ajax request that will save
the necessary data to the server.
Wrapping Up
To re-cap, it is a good practice to check if users are
sure they want to leave a page when they are entering
information, but it's even better to silently save that
information for them. (Arguably you would want to do both,
like how Gmail and Blogger save state and ask
users if they are sure they want to leave the page). There
are many different ways to save state, some purely
client-side and others relying on saving data to the server
with Ajax. The solutions which save data to the server are
suitable for times when the data needs to be saved beyond
the browsing session.
Of the two client-side solutions explored, hidden form
fields and named anchors, the former is more suitable for
conventional websites spanning many pages while the latter
better suits single-page Ajax applications. Using named
anchors also has the added benefit of allowing users to
bookmark and send links to the JavaScript application in
various states, and the state is preserved beyond the
browsing session.
Whatever strategy you follow, your users will thank you
for the time saved and frustration avoided of having to
re-enter lost information.