818Swapping div elements with jQuery

I found myself in need for swapping div elements. On the one hand, elements, which are situated next to each other (in the code), on the other hand I also wanted to swap only sub-elements of these divs.

How can this be done with jQuery?

Let's start with the first case, swapping adjunct divs.

...
...
...
...
...
// get a handle on the elements
var elements = $(".elements");

// let's assume, we want to swap #e2 and #e3
var leftElement = $(elements[2]);
var rightElement = $(elements[3]);

leftElement.insertAfter(rightElement); // inserts leftElement after rightElement

insertAfter takes the object, with with the method is associated (leftElement) and inserts the object's div after the div specified as a method argument. Here's more on SO about it.

In a previous version of the code, i mistakenly though, that it's necessary to replace the leftElement with the right one prior to inserting...

// NG
leftElement.replaceWith(rightElement);
leftElement.insertAfter(rightElement);

This also seemed to work ok. Only later I found out, that any jQuery data() objects connected to the rightElement will get wiped when replaceWith is being called.

Finally you'll also want to do an update to the elements array, to get them in their new order:

var elements = $(".elements");

814Swapping Array Items in Javascript

var myArray = ["a", "c", "b"];
myArray.swap(1, 2);
console.log(myArray);
// ["a", "b", "c"];

Array.prototype.swap=function(a, b) {
    this[a]= this.splice(b, 1, this[a])[0];
    return this;
}

It could also be done with a temp var, but this solution seems to be the most elegant. The key to this is in the additional arguments the splice() function can accept.

splice(start, itemCount, additionalItems ...)

splice() removes elements denominated by the start and itemCount argument, and - if specified - inserts additional items at the same position.

Also important to note is that splice() returns an array. And to turn that array into an item again we need to select it directly with [0].

812jQuery: Getting the element index from a list of identical class elements

Let's say you have a number of identical class elements:

One Title
Another Title
Brrrr
Zzzzzz

And you want to get a particular element from this list.

var myArray = $(".myClass");
var thirdElement = myArray[2];
console.log(thirdElement);
// 
Brrrr

But note, that myArray[2] is the HTML element, not an jQuery object. If you want to access it via jQuery, you'll have to "arm" it...

var myArray = $(".myClass");
var thirdElement = $(myArray[2]);
console.log(thirdElement);
// [
Brrrr
]

Interrating over a jQuery selected list can be done with the each() function:

myArray.each(function(i, element){
    var myElement = myArray[i];
    // this
    // element
}

myElement, element and this would all hold the same value at each iteration. i is obviously the index counter.

It's also described quite clearly at the jQuery Docs, but I think it's important enough to reiterate.

813Copying Arrays in Javascript with slice(0)

Using Arrays in Javascript retain their values by references.

var oneArray = ["a", "b", "c"];
var anotherArray = oneArray;
anotherArray[0] = "z";

console.log(oneArray, anotherArray);
// ["z", "b", "c"], ["z", "b", "c"]

Ok, but let's say you'll need to make a copy of the array. How do you do that?

var oneArray = ["a", "b", "c"];
var anotherArray = oneArray.slice(0);
anotherArray[0] = "z";

console.log(oneArray, anotherArray);
// ["a", "b", "c"], ["z", "b", "c"]

If you'd really, really want you could also wrap it into a prototype function. Although the code-characters savings are minimal.

Array.prototype.copy = function() {
    return this.slice(0);
}

var anotherArray = oneArray.slice(0);
//vs
var anotherArray = oneArray.copy();

Minimal, but maybe contextually for self-explaining.

811Fuji Xerox ApeosPort (DocuCentre) IV C3370 & OSX Lion

Setting up the ApeosPort C3370 in OSX Lion should not be difficult at all. Download the latest drivers, install, select and there you go. Right? Not quite.

Selecting "FX ApeosPort-IV C3370 v3017.104 PS" or similar driver did not produce a workable connection to the printer. Rather it produced some annoying 'beep' sounds at the printer every time I was trying to print. But it did not produce any print-outs.

Thanks to this I found that the solution is to select the following driver: "FX Print Drive for Mac OX v1.2.2" (or whatever the latest version might be). It's working!

810Installing Circos on OS 10.6

Circos, the Perl-package for visualizing data and information in a circular layout is an immensely popular application in the bio-informatics/bio-visualization community. And rightly so, as it able to produce visually stunning and appealing graphics.

Because its the native environment is bio-informaticians, the software is written in Perl. For those without Perl experience, it can be slightly frustrating to set it up on OSX (as it was for me). Here are the steps I needed to do to get it running, hope that this might reduce the trouble and let others get to work on their visualization more quickly.

The whole installation process is also detailed on the Installation page.

1 Getting Circos Download the software, tutorials, tools and courses

2 Testing the modules Go the the circosX-XX/bin folder and run: ./test.modules This gives you a list of the installed Perl modules, and - more importantly - the ones still missing.

3 Install the missing modules Switch to the super user with 'su'. For each missing modules say: cpan -i Modulename::Subname

cpan -i Config::General
cpan -i Graphics::ColorObject
cpan -i Math::VecStat
etc, etc

All worked swimmingly, except GD was making a bit of trouble.

4 Installing GD The graphics library GD has to be present on your system. If it's not, install it. (I am with homebrew, therefore installing GD is done like that: brew install gd)

Still after installing GD, installing the Perl module would not work. But after applying a little force, it went fine:

cpan -i -f GD

5 Adding circos to your path Adding the circos 'bin' directory to your path lets you call the circos program directly without specifying the whole path. That's a good thing.

PATH="/path/to/circos-0.55/bin:${PATH}"
export PATH

After that, everything should work and your Circos adventure can begin...

809Measuring Text in Canvas

HTML Canvas allows for the drawing of text with

ctx.fillStyle = "666";      // text color
ctx.textAlign = "center";       // alignment
ctx.font = "normal 12px Helvetica Neue";        // font
ctx.fillText("my Text, x , y);      // draw

Sometimes, you might want to draw a border around the text. Getting the textHeight should not present a problem, after all we can set the font size directly. But textWidth is not fixed in that way, because it depends (nnnn) on the actual text that is being displayed.

ctx.measureText takes the text as an argument and returns an object with a width property, describing the width of the text.

var texttWidth = ctx.measureText(columnName).width;

width is for now the only inhabitant of that object, but it would be a fair guess, that it might be more densely populate in the future.

806Replacing HTML elements with jQuery’s replaceWith

Let's say you are having some HTML elements that should only be enabled, if the browser sports certain features. By default, the element is disabled, like in the following example:

Open File...

Next, we check in Javascript is this certain feature is supported. If it is, we replace the disabled element with an active version.

if (isSupported()) {
    $('#item').replaceWith('Open File...');
}

Which results in this:

Open File...

Simple, but effective.

805Recursively removing .svn directories

When you'are working with Subversion, the situation might be familiar: You have checked-out some directories and want to check them in at another repository. Subversion is not happy to do that, it complains with a friendly reminder, that 'your directory' is already under version control.

The proper way to do it, would probably be to export a clean directory tree, but sometimes this option is not available.

Here's a simple shell script to recursively remove all the hidden '.svn' directories:

rm -rf `find . -type d -name .svn`

Wether it is better to use backticks or xargs is being discussed elsewhere.

804Freezing Objects and Arrays in Javascript

Since the ECMA-262 specifications (aka Javascript 1.8.5 aka ECMAScript 5th Edition) it is possible prevent Objects from accepting any changes to their properties. After applying Object.freeze(myObj) it won't be possible to change, add or remove properties from myObj.

So far so good.

As in Javascript Arrays also inherit from Object, I could see not reason, why freezing an array should not work.

var obj = {'a':1, 'b:2'};
Object.freeze(obj);
Object.isFrozen(obj);       // returns true
obj.a = 10;                 // new assignment has no affect
obj.a;                      // returns 1

var arr = [1, 2];
Object.freeze(arr);
Object.isFrozen(arr);      // returns true
arr[0] = 10;
arr;                       // returns [10, 2] ... ouch!

It turns out, that it seems to be an implementation bug in Safari (5.1). It's working in the latest Firefox and Chrome releases.

To check whether freezing arrays works in your brower, click the following button:

ps. I asked the question first on Stack Overflow: 'Freezing' Arrays in Javascript?. Thanks for the constructive answers.