StuartDuncan.name

  • Increase font size
  • Default font size
  • Decrease font size
Home My Code - Examples
My Code
Code that I've created or am currently working on.

Dynamically creating and sorting treeitems in xul

E-mail Print PDF
User Rating: / 1
PoorBest 

The problem:

I have XML data populating 'treechildren' in a XUL window and would like to allow the user the option to sort that data. Firefox's treecols are clickable but offer no native sorting options unless you use rdf templates.

The solution:

The first thing I had to do was import the XML data into an array. At first I had it dumped right into the tree and thought about trying to sort that, but javascript has a basic array sorting feature, not a node sorting feature. To do this, I use code like this:

var nodes = document.evaluate(".//node1/node2", document.root, null, XPathResult.ANY_TYPE, null);
var treeArray = new Array();

for(i=0; i < nodes.length; i++){
var count = treeArray.length
treeArray[count] = new Array();
treeArray[count]['info1'] = nodes[i].getAttribute("info1");
treeArray[count]['info2'] = nodes[i].getAttribute("info2");
treeArray[count]['info3'] = nodes[i].getAttribute("info3");
}

Now that you have the data you need, you can can populate your tree with this data. I will leave this up to you. It's pretty self explanatory if you know basic XML formatting. You add treeitem nodes, treerow nodes and treecell nodes using the info1, info2 and info3 information gathered in your array.
Let's say, for the sake of argument that you call that function populateTree().

Now comes the sorting.

In your treecol nodes within your XUL document, you'll want to add  onClick events. For example:

<treecol label="Info1 Column" onClick="sortArray(sortByInfo1);" />
<treecol label="Info2 Column" onClick="sortArray(sortByInfo2);" />
<treecol label="Info3 Column" onClick="sortArray(sortByInfo3);" />

Back to your javascript, you will create not only a sortArray function, but also functions to match sortByInfo1, sortByInfo2 and sortByInfo3.
These will look like this:

 

    // This is a callbackfunction. It's called from within array.sort().  This will take each item within the array and return back whether it comes before or after in either alphabetical or numeric order. 
    sortByInfo1: function(a,b){
        return a['info1'] - b['info1'];
    },
    sortByInfo2: function(a,b){
        return a['info2'] - b['info2'];
    },
sortByInfo3: function(a,b){
        return a['info3'] - b['info3'];
    },
// The 'method' is what calls sortByInfo1 - 3. Store this into an object variable so that if sorted a second time, we can determine if the column should just be reversed or sorted.
    sortArray: function(method){
        if( this.method != method ){
            this.method = method;
            .treeArray.sort(method);
        }
        else {
            treeArray.reverse();
        }
  // Populate the tree again, be sure to remove all child nodes first so that you don't have double the info each time.
        populateTree();
    }

Now, any time you click on a tree column, it will sort the array to the column that you clicked on, and then populate the tree once again. It's important to remember to remove all child nodes for that tree each time or else you will just be adding information on top of what's already in there.

It will also store the column you sorted by and if you sort by the same column a second time, it will simply reverse the array, giving the user the illusion that it's just been sorted again in the opposite direction.

I find it very odd that Firefox wouldn't have a very easy way to do this, since the tree columns are clickable anyhow and once the data is in there, it's basically a spreadsheet of nodes. It would seem to me that it would be easy for them to do.

Hopefully this helps you with sorting those trees.

Last Updated on Wednesday, 25 February 2009 17:38
 

Beginner Tutorial on making a Firefox Extension

E-mail Print PDF

First thing is first, you are going to need to have at least an above average grasp for two things:

  • Javascript
  • XML

If you don't understand the basics of either of these, then perhaps developing extensions isn't for you, at least not yet. Yes Firefox has made it very easy to make them but they can get very complicated very quickly, especially if you have some creative ideas on what to make a Firefox Extension do.

You're still reading so I'd have to assume that you can understand these things and would like to continue. I am going to provide you with some links and basic information to help you move forward but first some advice, don't get ahead of yourself too fast.

The hardest thing I had to over come when starting out was the frustration level right out of the gate. It's very easy to want to get right to the meat of the extension, start making it do things right away but there are some basics to learn first and foremost.

The first link I suggest you see is the official MDC (mozilla developer center) tutorial on making a "Hello World" extension.

View it here: https://developer.mozilla.org/en/Building_an_Extension

This will give you a grasp of the basic file structure as well as what is needed in those pesky files, such as install.rdf. I won't go into detail on those... for starting out, just copy and paste what you find. It may not seem like it but you will learn about those as you go. Most don't need changing until you're quite well beyond the beginner stage.

If you'd like to have your extension be more "your own", copying and pasting just isn't good enough, you can use this tool to quickly make your own basic setup that is more unique to you:

http://ted.mielczarek.org/code/mozilla/extensionwiz/

Once you have your basic package and are ready to get rolling, I strongly suggest you start with this tutorial: http://www.borngeek.com/firefox/toolbar-tutorial/

Granted you likely aren't in the market to make a toolbar, this will still give you a lot of insight into how the whole extension process goes. 

It really expands from there and I will be writing more about the individual aspects as I go. I've written a few extensions myself and have had to figure out a lot of things that simply were impossible to find with trusty'ole Google.

So best of luck, try not to get too frustrated. There's virtually no limit to what you can do which means that you will hit road blocks fairly often. But you will find the answers, people have hit those road blocks as well. And if you can't find them, you can always ask here: http://forums.mozillazine.org/

Now get coding!

 

Download Counter - Very Easy

E-mail Print PDF

My wife presented me with a bit of a problem, how to count the number of times a zip file is downloaded but not have to use an admin interface, go through server stats or need to use some link such as download.php?id=filename.zip

 You see, she's good with a computer but web stuff, such as FTP and puttings links to page... well, she's just barely scraped the surface in these areas. Which is to say, she can get a file on the server, and link to it, but she doesn't understand how it works or how to do much beyond that.

Here's my solution:

First what I did was create an `.htaccess` file turning on mod-rewrte so that I can route all file requests to a php of my choosing. This would give me the leverage I needed to begin tinkering.

My htaccess file looks like this:

RewriteEngine on
RewriteRule .* index.php

From here, I could create my index.php file to recognize the requested name (end(explode('/', $_SERVER['REDIRECT_URL']))) and get to work.

At the top of the file, I have created a few variables that require the user's attention:

$file_path = '../files/';
$xml_path = '../files/';
$xml_file = 'stats.xml';
$not_found = 'http://www.stuartduncan.name/';

These are rather self explanatory but essentially what you do is put the files in the 'files' folder, and make it writable so that the script can create and edit an xml file. The not_found variable is where the user will go should they request a file that is not there.

Then you're good to go.

  1. Put this script in the folder that you want people to download from, such as domain.com/downloads/
  2. Modify the 4 variables at the top of the index.php file
  3. Put the files you want people to download in the folder you specified for $file_path (ie, domain.com/files/)
  4. Link to the file like this: domain.com/downloads/myfile.zip

This will look for the file in the downloads folder but redirect the user to the index.php, the index.php file will either create or update the xml file with a +1 to the download column and the push the myfile.zip file to the user from the files folder.

I've even taken the liberty of putting the download counts into a nice report format for you, simply put ?report=1 onto the url to view it. For example domain.com/downloads/?report=1 and presto, you have a report that you can sort up and down any way you want.

Should you have problems, like the xml file not being created, put ?debug=1 onto the url instead to view any error messages. For example domain.com/downloads/?debug=1

And there you have it. A long convoluted way of saying that this has been made easy. No databases, no tracking scripts, no strange URLs to deal with and no admin area to log into. It's all handled with 1 script, 1 htaccess file and a soon to be created xml file. It's as easy as that!

Download the file: downloadcounter.zip

Last Updated on Thursday, 12 February 2009 04:31
 

DrugWars

E-mail Print PDF

This game is still very much under construction, I will update this when it is complete but for now it's coming along nicely.

This is my first attempt at creating anything entirely out of  http://en.wikipedia.org/wiki/JSON and I must say that I am enjoying the experience quite a bit. The code is quite nice, clean and easy to use. Perhaps I'll have to go back and rework my Deal or No Deal game.

There's not a lot to see from a game standpoint, but feel free to check out the code if you're into that sort of stuff, it can be found here: http://www.stuartduncan.name/drugwars/

Last Updated on Wednesday, 11 February 2009 14:56
 

Deal or No Deal

E-mail Print PDF

I created this game because I really enjoy the game show, and would like to play it from time to time on my own, but I also didn't want to spend a ton of time dealing with design, animations, sound effects and the rest that comes with it.

So... I turned to Javascript. I created this game to include other fun elements such as "Quest for a Million" where it keeps adding million dollar cases, "Christmas Edition" which has amounts up to $5mil. I even put in a cheat option to turn off the randomization of the cases. So you know case 1 has $0.01 and case 26 has $1mil. An easy win.

 Enjoy the game, you can play here: http://www.stuartduncan.name/dond/

Last Updated on Wednesday, 11 February 2009 14:57