Archive for the ‘Programming’ Category

jQuery list insertion sort plugin

Thursday, January 22nd, 2009

Here’s a basic jQuery sorting plugin for generic parent child relationships I wrote.

Wherein ‘element’ is the html you wish to insert and ‘value’ is what the associated weight of the element I’m inserting. For now it compares the weight value to the DOM text within the other children.

$.fn.insertSort = function(element, value) {
  selector = this;
  if ($(selector).length > 0) {
    $(selector).attr("inserted", "false");
    $(selector).children().each(function() {
      if (parseInt($(this).text()) > parseInt(value)) {
        $(this).before(element);
        $(selector).attr("inserted", "true");
        return false;
      }
    });
    if ($(selector).attr("inserted") == "false") {
      $(selector).append(element);
    }
  } else {
    $(selector).append(element);
  }
  $(selector).removeAttr("inserted");
};

It should be noted that this is an insertion sort. It’d be good to extend the plugin to be able to sort an existing piece of html for any parent/child relationship. Possibly with the ability to specify a particular level of depth?

Magento: Listing regions from a country code

Thursday, January 15th, 2009

Once and a while you may want to get a collection of the regions that are associated with a country in Magento. For example, the United States is of course a collection of… states. In Magento these are referred to as regions (Japan’s prefectures would hence be referred to as regions as well.)

So for a given country (and that country’s ID or country code) you might want to find all the regions in that country. Which are usually used to populate a dropdown menu on the address selection pages or shipping tax pages.


$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($this->getCountryId())
->load();

It’s great when ORM works for a developer. The problem with Magento I find is the complete lack of documentation. I only discovered the above technique after 20 minutes of grepping the app/code/core directory… :|

And remember to use Magento’s log method:


foreach($collection as $region) {

Mage::log(print_r($region, true));

}

Note some of the more useful attributes of that $region object are:

  • $region->default_name
  • $region->code

Which give responses along the lines of South Australia and SA, respectively.

New Drive

Thursday, January 8th, 2009

Picked up a 500GB 2.5″ drive and an Astone enclosure today. Pretty sweet. Unfortunately it was about $30 more expensive getting it from iStore than from MSY but convenience of getting it on your lunch break is a big trump card.

Easy checkbox toggling with jQuery

Thursday, January 8th, 2009

So I needed to have a regular form checkbox toggle the visibility of a div in my webpage today. It turned out jQuery supports the concept of toggling visibility via .toggle(), which I didn’t realise until I looked it up. And so you can support clicking or even keyboard changing of the checkbox via the change event:


$("#checkbox").change(function() {

$("#areaToToggle").toggle();

});

Pretty sweet, obviously the div should be hidden to begin with. We could add another line after the change event:


$("#areaToToggle").hide();

Too easy :-)

iPhone and Google Streetview

Sunday, November 23rd, 2008

So iPhone update 2.2 has added Streetview to Google Maps. You actually need to specify a location first in order to be able to swap to Streetview. Which is done by clicking the red man next to the address, shown below:

The last one reminds me of the show ‘Fringe’:

The interface was quite responsive and fills the whole screen. It allows for zooming, vertical and horizontal panning. Swish!